Make WordPress Core

Ticket #16434: 16434.14.diff

File 16434.14.diff, 8.2 KB (added by obenland, 10 years ago)
  • src/wp-admin/includes/class-wp-site-icon.php

     
    7878                $hook = add_submenu_page( null, __( 'Site Icon' ), __( 'Site Icon' ), 'manage_options', 'site-icon', array( $this, 'upload_site_icon_page' ) );
    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        }
    8384
     
    190191        }
    191192
    192193        /**
    193          * Crop a the image admin view.
     194         * Check if the image needs cropping.
     195         *
     196         * If it doesn't need cropping, proceed to set the icon.
    194197         *
    195198         * @since 4.3.0
    196199         */
    197         public function crop_page() {
     200        public function maybe_skip_cropping() {
     201                if ( empty( $_REQUEST['action'] ) || 'crop_site_icon' !== $_REQUEST['action'] ) {
     202                        return;
     203                }
     204
    198205                check_admin_referer( 'crop-site-icon' );
    199206
    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'];
     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                                'action'                => 'set_site_icon',
     216                        ), wp_nonce_url( admin_url( 'options-general.php' ), 'set-site-icon' ) );
     217
     218                        wp_safe_redirect( $url );
     219                        die();
    210220                }
     221        }
    211222
    212                 $image_size = getimagesize( $file );
     223        /**
     224         * Crop a the image admin view.
     225         *
     226         * @since 4.3.0
     227         */
     228        public function crop_page() {
     229                check_admin_referer( 'crop-site-icon' );
     230
     231                list( $attachment_id, $url, $image_size ) = $this->get_upload_data();
    213232
    214233                if ( $image_size[0] < $this->min_size ) {
    215234                        add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->min_size ) );
     
    318337        public function set_site_icon() {
    319338                check_admin_referer( 'set-site-icon' );
    320339
    321                 // Delete any existing site icon images.
    322                 $this->delete_site_icon();
     340                $attachment_id          = absint( $_REQUEST['attachment_id'] );
     341                $create_new_attachement = ! empty( $_REQUEST['create-new-attachment'] );
    323342
    324                 $attachment_id = absint( $_POST['attachment_id'] );
     343                /*
     344                 * If the current attachment as been set as site icon don't delete it.
     345                 */
     346                if ( get_option( 'site_icon' ) == $attachment_id ) {
     347                        // Get the file path.
     348                        $image_url = get_attached_file( $attachment_id );
     349
     350                        // Update meta data and possibly regenerate intermediate sizes.
     351                        add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     352                        $this->update_attachment_metadata( $attachment_id, $image_url );
     353                        remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    325354
    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 );
    333355                } else {
    334                         $cropped = get_attached_file( $attachment_id );
    335                 }
     356                        // Delete any existing site icon images.
     357                        $this->delete_site_icon();
    336358
    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                 }
     359                        if ( empty( $_REQUEST['skip-cropping'] ) ) {
     360                                $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'] );
     361                                $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 );
    340362
    341                 $object = $this->create_attachment_object( $cropped, $attachment_id );
     363                        } elseif ( $create_new_attachement ) {
     364                                $cropped = _copy_image_file( $attachment_id );
    342365
    343                 if ( ! empty( $_POST['create-new-attachment'] ) ) {
    344                         unset( $object['ID'] );
    345                 }
     366                        } else {
     367                                $cropped = get_attached_file( $attachment_id );
     368                        }
     369
     370                        if ( ! $cropped || is_wp_error( $cropped ) ) {
     371                                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
     372                        }
     373
     374                        $object = $this->create_attachment_object( $cropped, $attachment_id );
    346375
    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' ) );
     376                        if ( $create_new_attachement ) {
     377                                unset( $object['ID'] );
     378                        }
    351379
    352                 // Save the site_icon data into option
    353                 update_option( 'site_icon', $attachment_id );
     380                        // Update the attachment.
     381                        add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     382                        $attachment_id = $this->insert_attachment( $object, $cropped );
     383                        remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     384
     385                        // Save the site_icon data into option
     386                        update_option( 'site_icon', $attachment_id );
     387                }
    354388
    355389                add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' );
    356390        }
     
    487521        }
    488522
    489523        /**
    490          * Insert an attachment and its metadata.
     524         * Insert an attachment.
    491525         *
    492526         * @since 4.3.0
    493527         *
    494          * @param array  $object  Attachment object.
    495          * @param string $cropped Cropped image URL.
    496          * @return int Attachment ID.
     528         * @param array  $object Attachment object.
     529         * @param string $file   Filepath of the attached image.
     530         * @return int           Attachment ID
    497531         */
    498         public function insert_attachment( $object, $cropped ) {
    499                 $attachment_id = wp_insert_attachment( $object, $cropped );
    500                 $metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
     532        public function insert_attachment( $object, $file ) {
     533                $attachment_id = wp_insert_attachment( $object, $file );
     534                $this->update_attachment_metadata( $attachment_id, $file );
     535
     536                return $attachment_id;
     537        }
     538
     539        /**
     540         * Update the metadata of an attachment
     541         *
     542         * @since 4.3.0
     543         *
     544         * @param int    $attachment_id Attachment ID
     545         * @param string $file          Filepath of the Attached image.
     546         */
     547        public function update_attachment_metadata( $attachment_id, $file ) {
     548                $metadata = wp_generate_attachment_metadata( $attachment_id, $file );
    501549
    502550                /**
    503551                 * Filter the site icon attachment metadata.
     
    510558                 */
    511559                $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
    512560                wp_update_attachment_metadata( $attachment_id, $metadata );
    513 
    514                 return $attachment_id;
    515561        }
    516562
    517563        /**
     
    629675
    630676                return $value;
    631677        }
     678
     679        /**
     680         * Get the data required to work with the uploaded image
     681         *
     682         * @since 4.3.0
     683         *
     684         * @return array containing the collected data
     685         */
     686        private function get_upload_data() {
     687                if ( isset( $_GET['file'] ) ) {
     688                        $attachment_id = absint( $_GET['file'] );
     689                        $file          = get_attached_file( $attachment_id, true );
     690                        $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
     691                        $url           = $url[0];
     692                } else {
     693                        $upload        = $this->handle_upload();
     694                        $attachment_id = $upload['attachment_id'];
     695                        $file          = $upload['file'];
     696                        $url           = $upload['url'];
     697                }
     698
     699                $image_size = getimagesize( $file );
     700
     701                return array( $attachment_id, $url, $image_size );
     702        }
    632703}
    633704
    634705/**