<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[33051] trunk/src/wp-admin/includes/class-wp-site-icon.php: Site Icon: Skip cropping if image has the correct size.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/33051">33051</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/33051","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>obenland</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-07-02 20:39:23 +0000 (Thu, 02 Jul 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Site Icon: Skip cropping if image has the correct size.

Props jipmoors for initial patch.
See <a href="https://core.trac.wordpress.org/ticket/16434">#16434</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpsiteiconphp">trunk/src/wp-admin/includes/class-wp-site-icon.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpsiteiconphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-site-icon.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-site-icon.php        2015-07-02 17:39:31 UTC (rev 33050)
+++ trunk/src/wp-admin/includes/class-wp-site-icon.php  2015-07-02 20:39:23 UTC (rev 33051)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -78,6 +78,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $hook = add_submenu_page( null, __( 'Site Icon' ), __( 'Site Icon' ), 'manage_options', 'site-icon', array( $this, 'upload_site_icon_page' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( "load-$hook", array( $this, 'add_upload_settings' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_action( "load-$hook", array( $this, 'maybe_skip_cropping' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_action( "admin_print_scripts-$hook", array( $this, 'enqueue_scripts' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,6 +191,37 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Check if the image needs cropping.
+        *
+        * If it doesn't need cropping, proceed to set the icon.
+        *
+        * @since 4.3.0
+        */
+       public function maybe_skip_cropping() {
+               if ( empty( $_REQUEST['action'] ) || 'crop_site_icon' !== $_REQUEST['action'] ) {
+                       return;
+               }
+
+               check_admin_referer( 'crop-site-icon' );
+
+               list( $attachment_id, $url, $image_size ) = $this->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();
+               }
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Crop a the image admin view.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.3.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -197,20 +229,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function crop_page() {
</span><span class="cx" style="display: block; padding: 0 10px">                check_admin_referer( 'crop-site-icon' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                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'];
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         list( $attachment_id, $url, $image_size ) = $this->get_upload_data();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $image_size = getimagesize( $file );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( $image_size[0] < $this->min_size ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->min_size ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -318,40 +338,55 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function set_site_icon() {
</span><span class="cx" style="display: block; padding: 0 10px">                check_admin_referer( 'set-site-icon' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Delete any existing site icon images.
-               $this->delete_site_icon();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $attachment_id          = absint( $_REQUEST['attachment_id'] );
+               $create_new_attachement = ! empty( $_REQUEST['create-new-attachment'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $attachment_id = absint( $_POST['attachment_id'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /*
+                * 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 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // 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 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // 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' ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $cropped = get_attached_file( $attachment_id );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Delete any existing site icon images.
+                       $this->delete_site_icon();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $cropped || is_wp_error( $cropped ) ) {
-                       wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 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 );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $object = $this->create_attachment_object( $cropped, $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 } elseif ( $create_new_attachement ) {
+                               $cropped = _copy_image_file( $attachment_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $_POST['create-new-attachment'] ) ) {
-                       unset( $object['ID'] );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 } else {
+                               $cropped = get_attached_file( $attachment_id );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // 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' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! $cropped || is_wp_error( $cropped ) ) {
+                               wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Save the site_icon data into option
-               update_option( 'site_icon', $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $object = $this->create_attachment_object( $cropped, $attachment_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        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 );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -487,18 +522,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Insert an attachment and its metadata.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Insert an attachment.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.3.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array  $object  Attachment object.
-        * @param string $cropped Cropped image URL.
-        * @return int Attachment ID.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param array  $object Attachment object.
+        * @param string $file   File path of the attached image.
+        * @return int           Attachment ID
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function insert_attachment( $object, $cropped ) {
-               $attachment_id = wp_insert_attachment( $object, $cropped );
-               $metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function insert_attachment( $object, $file ) {
+               $attachment_id = wp_insert_attachment( $object, $file );
+               $this->update_attachment_metadata( $attachment_id, $file );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                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 );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Filter the site icon attachment metadata.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -510,8 +559,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
</span><span class="cx" style="display: block; padding: 0 10px">                wp_update_attachment_metadata( $attachment_id, $metadata );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               return $attachment_id;
</del><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -629,6 +676,31 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $value;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * 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 );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre>
</div>
</div>

</body>
</html>