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.
|
2019-01-09 06:43:14 +01:00
|
|
|
* @param array $legacy_args Deprecated.
|
|
|
|
*
|
2019-12-18 01:17:54 +01:00
|
|
|
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
|
2016-08-23 15:42:36 +02:00
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) {
|
2016-08-23 15:42:36 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-01-09 06:43:14 +01:00
|
|
|
/**
|
|
|
|
* Saves an attachment.
|
|
|
|
*
|
|
|
|
* @param string $file The file name to create attachment object for.
|
2019-12-18 01:17:54 +01:00
|
|
|
* @param int $parent ID of the post to attach the file to.
|
2019-01-09 06:43:14 +01:00
|
|
|
*
|
|
|
|
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
|
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function create_upload_object( $file, $parent = 0 ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
$contents = file_get_contents( $file );
|
2019-03-01 21:57:26 +01:00
|
|
|
$upload = wp_upload_bits( wp_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(
|
2019-03-01 21:57:26 +01:00
|
|
|
'post_title' => wp_basename( $upload['file'] ),
|
2017-12-01 00:09:33 +01:00
|
|
|
'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
|
|
|
);
|
|
|
|
|
2020-01-29 01:43:23 +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;
|
|
|
|
}
|
|
|
|
}
|