Make WordPress Core

Changeset 33051


Ignore:
Timestamp:
07/02/2015 08:39:23 PM (11 years ago)
Author:
obenland
Message:

Site Icon: Skip cropping if image has the correct size.

Props jipmoors for initial patch.
See #16434.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-site-icon.php

    r33011 r33051  
    7979
    8080                add_action( "load-$hook", array( $this, 'add_upload_settings' ) );
     81                add_action( "load-$hook", array( $this, 'maybe_skip_cropping' ) );
    8182                add_action( "admin_print_scripts-$hook", array( $this, 'enqueue_scripts' ) );
    8283        }
     
    191192
    192193        /**
     194         * Check if the image needs cropping.
     195         *
     196         * If it doesn't need cropping, proceed to set the icon.
     197         *
     198         * @since 4.3.0
     199         */
     200        public function maybe_skip_cropping() {
     201                if ( empty( $_REQUEST['action'] ) || 'crop_site_icon' !== $_REQUEST['action'] ) {
     202                        return;
     203                }
     204
     205                check_admin_referer( 'crop-site-icon' );
     206
     207                list( $attachment_id, $url, $image_size ) = $this->get_upload_data();
     208
     209                if ( $image_size[0] == $image_size[1] && $image_size[0] == $this->min_size ) {
     210                        // No cropping required.
     211
     212                        $url = add_query_arg( array(
     213                                'attachment_id'         => $attachment_id,
     214                                'skip-cropping'         => true,
     215                                'create-new-attachment' => true,
     216                                'action'                => 'set_site_icon',
     217                        ), wp_nonce_url( admin_url( 'options-general.php' ), 'set-site-icon' ) );
     218
     219                        wp_safe_redirect( $url );
     220                        die();
     221                }
     222        }
     223
     224        /**
    193225         * Crop a the image admin view.
    194226         *
     
    198230                check_admin_referer( 'crop-site-icon' );
    199231
    200                 if ( isset( $_GET['file'] ) ) {
    201                         $attachment_id = absint( $_GET['file'] );
    202                         $file          = get_attached_file( $attachment_id, true );
    203                         $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
    204                         $url           = $url[0];
    205                 } else {
    206                         $upload        = $this->handle_upload();
    207                         $attachment_id = $upload['attachment_id'];
    208                         $file          = $upload['file'];
    209                         $url           = $upload['url'];
    210                 }
    211 
    212                 $image_size = getimagesize( $file );
     232                list( $attachment_id, $url, $image_size ) = $this->get_upload_data();
    213233
    214234                if ( $image_size[0] < $this->min_size ) {
     
    319339                check_admin_referer( 'set-site-icon' );
    320340
    321                 // Delete any existing site icon images.
    322                 $this->delete_site_icon();
    323 
    324                 $attachment_id = absint( $_POST['attachment_id'] );
    325 
    326                 // TODO
    327                 if ( empty( $_POST['skip-cropping'] ) ) {
    328                         $crop_ratio = (float) $_POST['crop_ratio'];
    329                         $crop_data = $this->convert_coordinates_from_resized_to_full( $_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio );
    330                         $cropped = wp_crop_image( $attachment_id, $crop_data['crop_x'], $crop_data['crop_y'], $crop_data['crop_width'], $crop_data['crop_height'], $this->min_size, $this->min_size );
    331                 } elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
    332                         $cropped = _copy_image_file( $attachment_id );
     341                $attachment_id          = absint( $_REQUEST['attachment_id'] );
     342                $create_new_attachement = ! empty( $_REQUEST['create-new-attachment'] );
     343
     344                /*
     345                 * If the current attachment as been set as site icon don't delete it.
     346                 */
     347                if ( get_option( 'site_icon' ) == $attachment_id ) {
     348                        // Get the file path.
     349                        $image_url = get_attached_file( $attachment_id );
     350
     351                        // Update meta data and possibly regenerate intermediate sizes.
     352                        add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     353                        $this->update_attachment_metadata( $attachment_id, $image_url );
     354                        remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     355
    333356                } else {
    334                         $cropped = get_attached_file( $attachment_id );
    335                 }
    336 
    337                 if ( ! $cropped || is_wp_error( $cropped ) ) {
    338                         wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    339                 }
    340 
    341                 $object = $this->create_attachment_object( $cropped, $attachment_id );
    342 
    343                 if ( ! empty( $_POST['create-new-attachment'] ) ) {
    344                         unset( $object['ID'] );
    345                 }
    346 
    347                 // Update the attachment
    348                 add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    349                 $attachment_id = $this->insert_attachment( $object, $cropped );
    350                 remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    351 
    352                 // Save the site_icon data into option
    353                 update_option( 'site_icon', $attachment_id );
     357                        // Delete any existing site icon images.
     358                        $this->delete_site_icon();
     359
     360                        if ( empty( $_REQUEST['skip-cropping'] ) ) {
     361                                $crop_data = $this->convert_coordinates_from_resized_to_full( $_REQUEST['crop-x'], $_REQUEST['crop-y'], $_REQUEST['crop-w'], $_REQUEST['crop-h'], (float) $_REQUEST['crop_ratio'] );
     362                                $cropped   = wp_crop_image( $attachment_id, $crop_data['crop_x'], $crop_data['crop_y'], $crop_data['crop_width'], $crop_data['crop_height'], $this->min_size, $this->min_size );
     363
     364                        } elseif ( $create_new_attachement ) {
     365                                $cropped = _copy_image_file( $attachment_id );
     366
     367                        } else {
     368                                $cropped = get_attached_file( $attachment_id );
     369                        }
     370
     371                        if ( ! $cropped || is_wp_error( $cropped ) ) {
     372                                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
     373                        }
     374
     375                        $object = $this->create_attachment_object( $cropped, $attachment_id );
     376
     377                        if ( $create_new_attachement ) {
     378                                unset( $object['ID'] );
     379                        }
     380
     381                        // Update the attachment.
     382                        add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     383                        $attachment_id = $this->insert_attachment( $object, $cropped );
     384                        remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     385
     386                        // Save the site_icon data into option
     387                        update_option( 'site_icon', $attachment_id );
     388                }
    354389
    355390                add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' );
     
    488523
    489524        /**
    490          * Insert an attachment and its metadata.
    491          *
    492          * @since 4.3.0
    493          *
    494          * @param array  $object  Attachment object.
    495          * @param string $cropped Cropped image URL.
    496          * @return int Attachment ID.
    497          */
    498         public function insert_attachment( $object, $cropped ) {
    499                 $attachment_id = wp_insert_attachment( $object, $cropped );
    500                 $metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
     525         * Insert an attachment.
     526         *
     527         * @since 4.3.0
     528         *
     529         * @param array  $object Attachment object.
     530         * @param string $file   File path of the attached image.
     531         * @return int           Attachment ID
     532         */
     533        public function insert_attachment( $object, $file ) {
     534                $attachment_id = wp_insert_attachment( $object, $file );
     535                $this->update_attachment_metadata( $attachment_id, $file );
     536
     537                return $attachment_id;
     538        }
     539
     540        /**
     541         * Update the metadata of an attachment.
     542         *
     543         * @since 4.3.0
     544         *
     545         * @param int    $attachment_id Attachment ID
     546         * @param string $file          File path of the attached image.
     547         */
     548        public function update_attachment_metadata( $attachment_id, $file ) {
     549                $metadata = wp_generate_attachment_metadata( $attachment_id, $file );
    501550
    502551                /**
     
    511560                $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
    512561                wp_update_attachment_metadata( $attachment_id, $metadata );
    513 
    514                 return $attachment_id;
    515562        }
    516563
     
    630677                return $value;
    631678        }
     679
     680        /**
     681         * Get the data required to work with the uploaded image
     682         *
     683         * @since 4.3.0
     684         *
     685         * @return array containing the collected data
     686         */
     687        private function get_upload_data() {
     688                if ( isset( $_GET['file'] ) ) {
     689                        $attachment_id = absint( $_GET['file'] );
     690                        $file          = get_attached_file( $attachment_id, true );
     691                        $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
     692                        $url           = $url[0];
     693                } else {
     694                        $upload        = $this->handle_upload();
     695                        $attachment_id = $upload['attachment_id'];
     696                        $file          = $upload['file'];
     697                        $url           = $upload['url'];
     698                }
     699
     700                $image_size = getimagesize( $file );
     701
     702                return array( $attachment_id, $url, $image_size );
     703        }
    632704}
    633705
Note: See TracChangeset for help on using the changeset viewer.