Make WordPress Core


Ignore:
Timestamp:
03/17/2022 08:17:48 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Rename parameters that use reserved keywords in wp-admin/includes/class-custom-image-header.php.

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 $object variable in Custom_Image_Header class methods to $attachment for clarity and consistency, as the variable type is actually an array, and updates the documentation accordingly.

Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-custom-image-header.php

    r52610 r52946  
    976976                $filename = wp_basename( $file );
    977977
    978                 // Construct the object array.
    979                 $object = array(
     978                // Construct the array with attachment object data.
     979                $attachment = array(
    980980                        'post_title'     => $filename,
    981981                        'post_content'   => $url,
     
    986986
    987987                // Save the data.
    988                 $attachment_id = wp_insert_attachment( $object, $file );
     988                $attachment_id = wp_insert_attachment( $attachment, $file );
    989989
    990990                return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
     
    10621062                $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
    10631063
    1064                 $object = $this->create_attachment_object( $cropped, $attachment_id );
     1064                $attachment = $this->create_attachment_object( $cropped, $attachment_id );
    10651065
    10661066                if ( ! empty( $_POST['create-new-attachment'] ) ) {
    1067                         unset( $object['ID'] );
     1067                        unset( $attachment['ID'] );
    10681068                }
    10691069
    10701070                // Update the attachment.
    1071                 $attachment_id = $this->insert_attachment( $object, $cropped );
     1071                $attachment_id = $this->insert_attachment( $attachment, $cropped );
    10721072
    10731073                $url = wp_get_attachment_url( $attachment_id );
     
    13011301         * @param string $cropped              Cropped image URL.
    13021302         * @param int    $parent_attachment_id Attachment ID of parent image.
    1303          * @return array Attachment object.
     1303         * @return array An array with attachment object data.
    13041304         */
    13051305        final public function create_attachment_object( $cropped, $parent_attachment_id ) {
     
    13111311                $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
    13121312
    1313                 $object = array(
     1313                $attachment = array(
    13141314                        'ID'             => $parent_attachment_id,
    13151315                        'post_title'     => wp_basename( $cropped ),
     
    13201320                );
    13211321
    1322                 return $object;
     1322                return $attachment;
    13231323        }
    13241324
     
    13281328         * @since 3.9.0
    13291329         *
    1330          * @param array  $object  Attachment object.
    1331          * @param string $cropped File path to cropped image.
     1330         * @param array  $attachment An array with attachment object data.
     1331         * @param string $cropped    File path to cropped image.
    13321332         * @return int Attachment ID.
    13331333         */
    1334         final public function insert_attachment( $object, $cropped ) {
    1335                 $parent_id = isset( $object['post_parent'] ) ? $object['post_parent'] : null;
    1336                 unset( $object['post_parent'] );
    1337 
    1338                 $attachment_id = wp_insert_attachment( $object, $cropped );
     1334        final public function insert_attachment( $attachment, $cropped ) {
     1335                $parent_id = isset( $attachment['post_parent'] ) ? $attachment['post_parent'] : null;
     1336                unset( $attachment['post_parent'] );
     1337
     1338                $attachment_id = wp_insert_attachment( $attachment, $cropped );
    13391339                $metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
    13401340
     
    14051405                $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
    14061406
    1407                 $object = $this->create_attachment_object( $cropped, $attachment_id );
    1408 
    1409                 $previous = $this->get_previous_crop( $object );
     1407                $attachment = $this->create_attachment_object( $cropped, $attachment_id );
     1408
     1409                $previous = $this->get_previous_crop( $attachment );
    14101410
    14111411                if ( $previous ) {
    1412                         $object['ID'] = $previous;
     1412                        $attachment['ID'] = $previous;
    14131413                } else {
    1414                         unset( $object['ID'] );
    1415                 }
    1416 
    1417                 $new_attachment_id = $this->insert_attachment( $object, $cropped );
    1418 
    1419                 $object['attachment_id'] = $new_attachment_id;
    1420                 $object['url']           = wp_get_attachment_url( $new_attachment_id );
    1421 
    1422                 $object['width']  = $dimensions['dst_width'];
    1423                 $object['height'] = $dimensions['dst_height'];
    1424 
    1425                 wp_send_json_success( $object );
     1414                        unset( $attachment['ID'] );
     1415                }
     1416
     1417                $new_attachment_id = $this->insert_attachment( $attachment, $cropped );
     1418
     1419                $attachment['attachment_id'] = $new_attachment_id;
     1420                $attachment['url']           = wp_get_attachment_url( $new_attachment_id );
     1421
     1422                $attachment['width']  = $dimensions['dst_width'];
     1423                $attachment['height'] = $dimensions['dst_height'];
     1424
     1425                wp_send_json_success( $attachment );
    14261426        }
    14271427
     
    15781578         * @since 4.9.0
    15791579         *
    1580          * @param array $object A crop attachment object.
     1580         * @param array $attachment An array with a cropped attachment object data.
    15811581         * @return int|false An attachment ID if one exists. False if none.
    15821582         */
    1583         public function get_previous_crop( $object ) {
     1583        public function get_previous_crop( $attachment ) {
    15841584                $header_images = $this->get_uploaded_header_images();
    15851585
     
    15921592
    15931593                foreach ( $header_images as $image ) {
    1594                         if ( $image['attachment_parent'] === $object['post_parent'] ) {
     1594                        if ( $image['attachment_parent'] === $attachment['post_parent'] ) {
    15951595                                $previous = $image['attachment_id'];
    15961596                                break;
Note: See TracChangeset for help on using the changeset viewer.