From 3d71278f580be59c4d02c5d562b6e4fd37a71a49 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Thu, 2 Jul 2015 20:39:23 +0000 Subject: [PATCH] Site Icon: Skip cropping if image has the correct size. Props jipmoors for initial patch. See #16434. git-svn-id: https://develop.svn.wordpress.org/trunk@33051 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-wp-site-icon.php | 174 +++++++++++++------ 1 file changed, 123 insertions(+), 51 deletions(-) diff --git a/src/wp-admin/includes/class-wp-site-icon.php b/src/wp-admin/includes/class-wp-site-icon.php index a4a08e0d51..baec522a34 100644 --- a/src/wp-admin/includes/class-wp-site-icon.php +++ b/src/wp-admin/includes/class-wp-site-icon.php @@ -78,6 +78,7 @@ class WP_Site_Icon { $hook = add_submenu_page( null, __( 'Site Icon' ), __( 'Site Icon' ), 'manage_options', 'site-icon', array( $this, 'upload_site_icon_page' ) ); add_action( "load-$hook", array( $this, 'add_upload_settings' ) ); + add_action( "load-$hook", array( $this, 'maybe_skip_cropping' ) ); add_action( "admin_print_scripts-$hook", array( $this, 'enqueue_scripts' ) ); } @@ -189,6 +190,37 @@ class WP_Site_Icon { get_upload_data(); + + if ( $image_size[0] == $image_size[1] && $image_size[0] == $this->min_size ) { + // No cropping required. + + $url = add_query_arg( array( + 'attachment_id' => $attachment_id, + 'skip-cropping' => true, + 'create-new-attachment' => true, + 'action' => 'set_site_icon', + ), wp_nonce_url( admin_url( 'options-general.php' ), 'set-site-icon' ) ); + + wp_safe_redirect( $url ); + die(); + } + } + /** * Crop a the image admin view. * @@ -197,19 +229,7 @@ class WP_Site_Icon { public function crop_page() { check_admin_referer( 'crop-site-icon' ); - if ( isset( $_GET['file'] ) ) { - $attachment_id = absint( $_GET['file'] ); - $file = get_attached_file( $attachment_id, true ); - $url = wp_get_attachment_image_src( $attachment_id, 'full' ); - $url = $url[0]; - } else { - $upload = $this->handle_upload(); - $attachment_id = $upload['attachment_id']; - $file = $upload['file']; - $url = $upload['url']; - } - - $image_size = getimagesize( $file ); + list( $attachment_id, $url, $image_size ) = $this->get_upload_data(); if ( $image_size[0] < $this->min_size ) { add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->min_size ) ); @@ -318,40 +338,55 @@ class WP_Site_Icon { public function set_site_icon() { check_admin_referer( 'set-site-icon' ); - // Delete any existing site icon images. - $this->delete_site_icon(); + $attachment_id = absint( $_REQUEST['attachment_id'] ); + $create_new_attachement = ! empty( $_REQUEST['create-new-attachment'] ); - $attachment_id = absint( $_POST['attachment_id'] ); + /* + * If the current attachment as been set as site icon don't delete it. + */ + if ( get_option( 'site_icon' ) == $attachment_id ) { + // Get the file path. + $image_url = get_attached_file( $attachment_id ); + + // Update meta data and possibly regenerate intermediate sizes. + add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); + $this->update_attachment_metadata( $attachment_id, $image_url ); + remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); - // TODO - if ( empty( $_POST['skip-cropping'] ) ) { - $crop_ratio = (float) $_POST['crop_ratio']; - $crop_data = $this->convert_coordinates_from_resized_to_full( $_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio ); - $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 ); - } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { - $cropped = _copy_image_file( $attachment_id ); } else { - $cropped = get_attached_file( $attachment_id ); + // Delete any existing site icon images. + $this->delete_site_icon(); + + if ( empty( $_REQUEST['skip-cropping'] ) ) { + $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'] ); + $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 ); + + } elseif ( $create_new_attachement ) { + $cropped = _copy_image_file( $attachment_id ); + + } else { + $cropped = get_attached_file( $attachment_id ); + } + + if ( ! $cropped || is_wp_error( $cropped ) ) { + wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); + } + + $object = $this->create_attachment_object( $cropped, $attachment_id ); + + if ( $create_new_attachement ) { + unset( $object['ID'] ); + } + + // Update the attachment. + add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); + $attachment_id = $this->insert_attachment( $object, $cropped ); + remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); + + // Save the site_icon data into option + update_option( 'site_icon', $attachment_id ); } - if ( ! $cropped || is_wp_error( $cropped ) ) { - wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); - } - - $object = $this->create_attachment_object( $cropped, $attachment_id ); - - if ( ! empty( $_POST['create-new-attachment'] ) ) { - unset( $object['ID'] ); - } - - // Update the attachment - add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); - $attachment_id = $this->insert_attachment( $object, $cropped ); - remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); - - // Save the site_icon data into option - update_option( 'site_icon', $attachment_id ); - add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' ); } @@ -487,17 +522,31 @@ class WP_Site_Icon { } /** - * Insert an attachment and its metadata. + * Insert an attachment. * * @since 4.3.0 * - * @param array $object Attachment object. - * @param string $cropped Cropped image URL. - * @return int Attachment ID. + * @param array $object Attachment object. + * @param string $file File path of the attached image. + * @return int Attachment ID */ - public function insert_attachment( $object, $cropped ) { - $attachment_id = wp_insert_attachment( $object, $cropped ); - $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); + public function insert_attachment( $object, $file ) { + $attachment_id = wp_insert_attachment( $object, $file ); + $this->update_attachment_metadata( $attachment_id, $file ); + + return $attachment_id; + } + + /** + * Update the metadata of an attachment. + * + * @since 4.3.0 + * + * @param int $attachment_id Attachment ID + * @param string $file File path of the attached image. + */ + public function update_attachment_metadata( $attachment_id, $file ) { + $metadata = wp_generate_attachment_metadata( $attachment_id, $file ); /** * Filter the site icon attachment metadata. @@ -510,8 +559,6 @@ class WP_Site_Icon { */ $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata ); wp_update_attachment_metadata( $attachment_id, $metadata ); - - return $attachment_id; } /** @@ -629,6 +676,31 @@ class WP_Site_Icon { return $value; } + + /** + * Get the data required to work with the uploaded image + * + * @since 4.3.0 + * + * @return array containing the collected data + */ + private function get_upload_data() { + if ( isset( $_GET['file'] ) ) { + $attachment_id = absint( $_GET['file'] ); + $file = get_attached_file( $attachment_id, true ); + $url = wp_get_attachment_image_src( $attachment_id, 'full' ); + $url = $url[0]; + } else { + $upload = $this->handle_upload(); + $attachment_id = $upload['attachment_id']; + $file = $upload['file']; + $url = $upload['url']; + } + + $image_size = getimagesize( $file ); + + return array( $attachment_id, $url, $image_size ); + } } /**