Make WordPress Core

Ticket #16434: 16434.13.diff

File 16434.13.diff, 10.0 KB (added by jipmoors, 10 years ago)

saving settings page doesn't delete site_icon anymore

  • src/wp-admin/includes/class-wp-site-icon.php

     
    6262
    6363                add_action( 'admin_menu', array( $this, 'admin_menu_upload_site_icon' ) );
    6464
    65                 add_action( 'admin_action_set_site_icon',    array( $this, 'set_site_icon'    ) );
    66                 add_action( 'admin_action_remove_site_icon', array( $this, 'remove_site_icon' ) );
     65                // Because of the media upload the 'admin_action_crop_site_icon' is not called
     66                add_action( 'load-settings_page_site-icon',  array( $this, 'maybe_skip_cropping' ) );
     67                add_action( 'admin_action_set_site_icon',    array( $this, 'set_site_icon'       ) );
     68                add_action( 'admin_action_remove_site_icon', array( $this, 'remove_site_icon'    ) );
     69                add_action( 'admin_action_site_icon_set',    array( $this, 'site_icon_set'       ) );
    6770
    6871                add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ), 10, 1 );
    6972                add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 );
     
    181184                </p>
    182185                <form class="hide-if-js" action="<?php echo esc_url( admin_url( 'options-general.php?page=site-icon' ) ); ?>" method="post" enctype="multipart/form-data">
    183186                        <input name="action" value="crop_site_icon" type="hidden" />
    184                         <input name="site-icon" type="file" />
     187                        <input name="new-site-icon" type="file" />
    185188                        <input name="submit" value="<?php esc_attr_e( 'Upload Image' ); ?>" type="submit" class="button button-primary" />
    186189                        <p class="description">
    187190                                <?php printf( __( 'The image is recommended to be a square image of at least %spx in both width and height.' ), "<strong>$this->min_size</strong>" ); ?>
     
    192195        }
    193196
    194197        /**
    195          * Crop a the image admin view.
     198         * Check if the image needs cropping
     199         * If it doesn't need cropping, proceed to set the icon
    196200         *
    197201         * @since 4.3.0
    198202         */
    199         public function crop_page() {
     203        public function maybe_skip_cropping() {
     204                if ( ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != 'crop_site_icon' ) {
     205                        return;
     206                }
     207
    200208                check_admin_referer( 'crop-site-icon' );
    201209
     210                $upload_data = $this->get_upload_data();
     211                $image_size = $upload_data['image_size'];
     212
     213                if ( $image_size[0] == $image_size[1] && $image_size[0] == $this->min_size ) {
     214                        // no cropping is required
     215
     216                        $url = add_query_arg(
     217                                array(
     218                                        'attachment_id' => $upload_data['attachment_id'],
     219                                        'skip-cropping' => true,
     220                                        'create-new-attachment' => true,
     221                                        'action' => 'set_site_icon'
     222                                ),
     223                                wp_nonce_url( admin_url( 'options-general.php' ), 'set-site-icon' )
     224                        );
     225
     226                        wp_redirect( $url );
     227                        die();
     228                }
     229        }
     230
     231        /**
     232         * Get the data required to work with the uploaded image
     233         *
     234         * @since 4.3.0
     235         *
     236         * @return array containing the collected data
     237         */
     238        public function get_upload_data() {
    202239                if ( isset( $_GET['file'] ) ) {
    203240                        $attachment_id = absint( $_GET['file'] );
    204241                        $file          = get_attached_file( $attachment_id, true );
     
    213250
    214251                $image_size = getimagesize( $file );
    215252
     253                return compact('attachment_id', 'url', 'image_size');
     254        }
     255
     256        /**
     257         * Crop a the image admin view.
     258         *
     259         * @since 4.3.0
     260         */
     261        public function crop_page() {
     262                check_admin_referer( 'crop-site-icon' );
     263
     264                $upload_data = $this->get_upload_data();
     265
     266                $image_size = $upload_data['image_size'];
     267                $attachment_id = $upload_data['attachment_id'];
     268                $url = $upload_data['url'];
     269
    216270                if ( $image_size[0] < $this->min_size ) {
    217271                        add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->min_size ) );
    218272
     
    320374        public function set_site_icon() {
    321375                check_admin_referer( 'set-site-icon' );
    322376
    323                 // Delete any existing site icon images.
    324                 $this->delete_site_icon();
     377                $attachment_id = absint( $_REQUEST['attachment_id'] );
     378                $current_site_icon_id = get_option( 'site_icon' );
    325379
    326                 $attachment_id = absint( $_POST['attachment_id'] );
     380                $create_new_attachement = isset( $_REQUEST['create-new-attachment'] ) && $_REQUEST['create-new-attachment'];
     381                $skip_cropping = isset( $_REQUEST['skip-cropping'] ) && $_REQUEST['skip-cropping'];
    327382
    328                 // TODO
    329                 if ( empty( $_POST['skip-cropping'] ) ) {
    330                         $crop_ratio = (float) $_POST['crop_ratio'];
    331                         $crop_data = $this->convert_coordinates_from_resized_to_full( $_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio );
    332                         $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 );
    333                 } elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
    334                         $cropped = _copy_image_file( $attachment_id );
     383                /*
     384                 * If the current attachment as been set as site icon don't delete it.
     385                 */
     386                if ( $current_site_icon_id != $attachment_id ) {
     387                        // Delete any existing site icon images.
     388                        $this->delete_site_icon();
     389
     390                        if ( false === $skip_cropping ) {
     391                                $crop_ratio = (float) $_REQUEST['crop_ratio'];
     392                                $crop_data  = $this->convert_coordinates_from_resized_to_full(
     393                                        $_POST['crop-x'],
     394                                        $_POST['crop-y'],
     395                                        $_POST['crop-w'],
     396                                        $_POST['crop-h'],
     397                                        $crop_ratio
     398                                );
     399                                $cropped = wp_crop_image(
     400                                        $attachment_id,
     401                                        $crop_data['crop_x'],
     402                                        $crop_data['crop_y'],
     403                                        $crop_data['crop_width'],
     404                                        $crop_data['crop_height'],
     405                                        $this->min_size,
     406                                        $this->min_size
     407                                );
     408                        } elseif ( $create_new_attachement ) {
     409                                $cropped = _copy_image_file( $attachment_id );
     410                        } else {
     411                                $cropped = get_attached_file( $attachment_id );
     412                        }
     413
     414                        if ( ! $cropped || is_wp_error( $cropped ) ) {
     415                                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
     416                        }
     417
     418                        $object = $this->create_attachment_object( $cropped, $attachment_id );
     419
     420                        if ( $create_new_attachement ) {
     421                                unset( $object['ID'] );
     422                        }
     423
     424                        // Update the attachment
     425                        add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     426                        $attachment_id = $this->insert_attachment( $object, $cropped );
     427                        remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     428
     429                        // Save the site_icon data into option
     430                        update_option( 'site_icon', $attachment_id );
     431
    335432                } else {
    336                         $cropped = get_attached_file( $attachment_id );
    337                 }
    338433
    339                 if ( ! $cropped || is_wp_error( $cropped ) ) {
    340                         wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    341                 }
     434                        // Get the file path
     435                        $image_url = get_attached_file( $attachment_id );
    342436
    343                 $object = $this->create_attachment_object( $cropped, $attachment_id );
    344 
    345                 if ( ! empty( $_POST['create-new-attachment'] ) ) {
    346                         unset( $object['ID'] );
     437                        // Update meta-data and possibly regenerate intermediate sizes
     438                        add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     439                        $this->update_attachment_metadata( $attachment_id, $image_url );
     440                        remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    347441                }
    348442
    349                 // Update the attachment
    350                 add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    351                 $attachment_id = $this->insert_attachment( $object, $cropped );
    352                 remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
     443                // redirect to settings page
     444                wp_redirect(
     445                        add_query_arg(
     446                                array('action' => 'site_icon_set'),
     447                                admin_url( 'options-general.php' )
     448                        )
     449                );
     450                die();
    353451
    354                 // Save the site_icon data into option
    355                 update_option( 'site_icon', $attachment_id );
     452        }
    356453
     454        public function site_icon_set() {
    357455                add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' );
    358456        }
    359457
     
    489587        }
    490588
    491589        /**
    492          * Insert an attachment and its metadata.
     590         * Insert an attachment
    493591         *
    494592         * @since 4.3.0
    495593         *
    496594         * @param array  $object  Attachment object.
    497          * @param string $cropped Cropped image URL.
     595         * @param string $image_url Cropped image URL.
    498596         * @return int Attachment ID.
    499597         */
    500         public function insert_attachment( $object, $cropped ) {
    501                 $attachment_id = wp_insert_attachment( $object, $cropped );
    502                 $metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
     598        public function insert_attachment( $object, $image_url ) {
     599                $attachment_id = wp_insert_attachment( $object, $image_url );
    503600
     601                $updated = $this->update_attachment_metadata( $attachment_id, $image_url );
     602                if ( false !== $updated ) {
     603                        return $attachment_id;
     604                }
     605
     606                return false;
     607        }
     608
     609        /**
     610         * Update the metadata of an attachment
     611         *
     612         * @since 4.3.0
     613         *
     614         * @param int $attachment_id Attachment ID
     615         * @param string $image_url Cropped image URL.
     616         *
     617         * @return bool|int
     618         */
     619        public function update_attachment_metadata( $attachment_id, $image_url ) {
     620                $metadata = wp_generate_attachment_metadata( $attachment_id, $image_url );
     621
    504622                /**
    505623                 * Filter the site icon attachment metadata.
    506624                 *
     
    511629                 * @param array $metadata Attachment metadata.
    512630                 */
    513631                $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
    514                 wp_update_attachment_metadata( $attachment_id, $metadata );
     632                $updated = wp_update_attachment_metadata( $attachment_id, $metadata );
    515633
    516                 return $attachment_id;
     634                return $updated;
    517635        }
    518636
    519637        /**
  • src/wp-admin/options-general.php

     
    142142                        'action' => 'remove_site_icon',
    143143                ), wp_nonce_url( admin_url( 'options-general.php' ), 'remove-site-icon' ) );
    144144        ?>
    145 
     145        <input type="hidden" name="site_icon" value="<?php echo esc_attr(get_option('site_icon')) ?>" />
    146146        <img class="avatar avatar-150" src="<?php site_icon_url( null, 150 ); ?>" height="150" width="150" />
    147147        <p class="hide-if-no-js">
    148148                <label class="screen-reader-text" for="choose-from-library-link"><?php _e( 'Choose an image from your media library:' ); ?></label>