mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names. This commit: * Renames the `$parent` parameter to `$parent_post_id` in `WP_UnitTest_Factory_For_Attachment::create_upload_object()`. * Amends the `$parent_post` parameter in `wp_insert_attachment()` and `WP_REST_Revisions_Controller::get_parent()` for consistency. Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952], [54956], [54959], [54960], [54961], [54962], [54964], [54965], [54969], [54970], [54971], [54972], [54996], [55000], [55011], [55013], [55014], [55015], [55016], [55017], [55020]. Props jrf, aristath, poena, justinahinon, SergeyBiryukov. See #56788. git-svn-id: https://develop.svn.wordpress.org/trunk@55021 602fd350-edb4-49c9-b593-d223f7449a82
101 lines
3.0 KiB
PHP
101 lines
3.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Unit test factory for attachments.
|
|
*
|
|
* Note: The below @method notations are defined solely for the benefit of IDEs,
|
|
* as a way to indicate expected return values from the given factory methods.
|
|
*
|
|
* @method int|WP_Error create( $args = array(), $generation_definitions = null )
|
|
* @method WP_Post|WP_Error create_and_get( $args = array(), $generation_definitions = null )
|
|
* @method (int|WP_Error)[] create_many( $count, $args = array(), $generation_definitions = null )
|
|
*/
|
|
class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
|
|
|
|
/**
|
|
* Create an attachment fixture.
|
|
*
|
|
* @since UT (3.7.0)
|
|
* @since 6.2.0 Returns a WP_Error object on failure.
|
|
*
|
|
* @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.
|
|
*
|
|
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
|
|
*/
|
|
public function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) {
|
|
// Backward compatibility for legacy argument format.
|
|
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'], true );
|
|
}
|
|
|
|
/**
|
|
* Saves an attachment.
|
|
*
|
|
* @since 4.4.0
|
|
* @since 6.2.0 Returns a WP_Error object on failure.
|
|
*
|
|
* @param string $file The file name to create attachment object for.
|
|
* @param int $parent_post_id ID of the post to attach the file to.
|
|
*
|
|
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
|
|
*/
|
|
public function create_upload_object( $file, $parent_post_id = 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_id,
|
|
'post_mime_type' => $type,
|
|
'guid' => $upload['url'],
|
|
);
|
|
|
|
// Save the data.
|
|
$attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id, true );
|
|
|
|
if ( is_wp_error( $attachment_id ) ) {
|
|
return $attachment_id;
|
|
}
|
|
|
|
wp_update_attachment_metadata(
|
|
$attachment_id,
|
|
wp_generate_attachment_metadata( $attachment_id, $upload['file'] )
|
|
);
|
|
|
|
return $attachment_id;
|
|
}
|
|
}
|