2016-01-19 04:55:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
|
|
|
|
|
2016-08-23 15:42:36 +02:00
|
|
|
/**
|
|
|
|
* Create an attachment fixture.
|
|
|
|
*
|
|
|
|
* @param array $args {
|
|
|
|
* Array of arguments. Accepts all arguments that can be passed to
|
|
|
|
* wp_insert_attachment(), in addition to the following:
|
|
|
|
* @type int $post_parent ID of the post to which the attachment belongs.
|
|
|
|
* @type string $file Path of the attached file.
|
|
|
|
* }
|
|
|
|
* @param int $legacy_parent Deprecated.
|
|
|
|
* @param array $legacy_args Deprecated
|
|
|
|
*/
|
|
|
|
function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) {
|
|
|
|
// Backward compatibility for legacy argument format.
|
|
|
|
if ( is_string( $args ) ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
$file = $args;
|
|
|
|
$args = $legacy_args;
|
2016-08-23 15:42:36 +02:00
|
|
|
$args['post_parent'] = $legacy_parent;
|
2017-12-01 00:09:33 +01:00
|
|
|
$args['file'] = $file;
|
2016-08-23 15:42:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 00:09:33 +01:00
|
|
|
$r = array_merge(
|
|
|
|
array(
|
|
|
|
'file' => '',
|
|
|
|
'post_parent' => 0,
|
2018-08-17 03:50:26 +02:00
|
|
|
),
|
|
|
|
$args
|
2017-12-01 00:09:33 +01:00
|
|
|
);
|
2016-08-23 15:42:36 +02:00
|
|
|
|
|
|
|
return wp_insert_attachment( $r, $r['file'], $r['post_parent'] );
|
2016-01-19 04:55:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function create_upload_object( $file, $parent = 0 ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
$contents = file_get_contents( $file );
|
|
|
|
$upload = wp_upload_bits( basename( $file ), null, $contents );
|
2016-01-19 04:55:19 +01:00
|
|
|
|
|
|
|
$type = '';
|
2017-12-01 00:09:33 +01:00
|
|
|
if ( ! empty( $upload['type'] ) ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
$type = $upload['type'];
|
|
|
|
} else {
|
|
|
|
$mime = wp_check_filetype( $upload['file'] );
|
2017-12-01 00:09:33 +01:00
|
|
|
if ( $mime ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
$type = $mime['type'];
|
2017-12-01 00:09:33 +01:00
|
|
|
}
|
2016-01-19 04:55:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$attachment = array(
|
2017-12-01 00:09:33 +01:00
|
|
|
'post_title' => basename( $upload['file'] ),
|
|
|
|
'post_content' => '',
|
|
|
|
'post_type' => 'attachment',
|
|
|
|
'post_parent' => $parent,
|
2016-01-19 04:55:19 +01:00
|
|
|
'post_mime_type' => $type,
|
2017-12-01 00:09:33 +01:00
|
|
|
'guid' => $upload['url'],
|
2016-01-19 04:55:19 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Save the data
|
2017-12-01 00:09:33 +01:00
|
|
|
$id = wp_insert_attachment( $attachment, $upload['file'], $parent );
|
2016-01-19 04:55:19 +01:00
|
|
|
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
}
|