12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
-
- public function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) {
-
- if ( is_string( $args ) ) {
- $file = $args;
- $args = $legacy_args;
- $args['post_parent'] = $legacy_parent;
- $args['file'] = $file;
- }
- $r = array_merge(
- array(
- 'file' => '',
- 'post_parent' => 0,
- ),
- $args
- );
- return wp_insert_attachment( $r, $r['file'], $r['post_parent'] );
- }
-
- public function create_upload_object( $file, $parent = 0 ) {
- $contents = file_get_contents( $file );
- $upload = wp_upload_bits( wp_basename( $file ), null, $contents );
- $type = '';
- if ( ! empty( $upload['type'] ) ) {
- $type = $upload['type'];
- } else {
- $mime = wp_check_filetype( $upload['file'] );
- if ( $mime ) {
- $type = $mime['type'];
- }
- }
- $attachment = array(
- 'post_title' => wp_basename( $upload['file'] ),
- 'post_content' => '',
- 'post_type' => 'attachment',
- 'post_parent' => $parent,
- 'post_mime_type' => $type,
- 'guid' => $upload['url'],
- );
-
- $id = wp_insert_attachment( $attachment, $upload['file'], $parent );
- wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
- return $id;
- }
- }
|