Make WordPress Core

Ticket #32967: 32967.diff

File 32967.diff, 44.8 KB (added by obenland, 10 years ago)

Authored by Frank Klein

  • wp-admin/includes/admin.php

     
    6666
    6767/** WordPress Site Icon API */
    6868require_once(ABSPATH . 'wp-admin/includes/class-wp-site-icon.php');
     69require_once(ABSPATH . 'wp-admin/includes/class-wp-site-icon-admin-processing.php');
     70require_once(ABSPATH . 'wp-admin/includes/class-wp-site-icon-admin-ui.php');
    6971
    7072/** WordPress Update Administration API */
    7173require_once(ABSPATH . 'wp-admin/includes/update.php');
  • wp-admin/includes/class-wp-site-icon-admin-processing.php

     
     1<?php
     2
     3class WP_Site_Icon_Admin_Processing {
     4        public function __construct() {
     5
     6                add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ), 10, 1 );
     7                add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 );
     8        }
     9
     10        /**
     11         * Upload a Site Icon image.
     12         *
     13         * This method is only used by the no Javascript admin UI flow.
     14         *
     15         * @since 4.3.0
     16         */
     17        public function upload_site_icon_image() {
     18                // Verify that the uploaded file is an image.
     19                $file_type = wp_check_filetype_and_ext( $_FILES['site-icon']['tmp_name'], $_FILES['site-icon']['name'] );
     20                if ( ! wp_match_mime_types( 'image', $file_type['type'] ) ) {
     21                        wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
     22                }
     23
     24                // Upload image.
     25                $file_attributes = wp_handle_upload( $_FILES['site-icon'], array( 'test_form' => false ) );
     26                if ( isset( $file_attributes['error'] ) ) {
     27                        wp_die( $file_attributes['error'], __( 'Image Upload Error' ) );
     28                }
     29
     30                // Save image as an attachment.
     31                $attachment_id = wp_insert_attachment( array(
     32                        'post_title'     => basename( $file_attributes['file'] ),
     33                        'post_content'   => $file_attributes['url'],
     34                        'post_mime_type' => $file_attributes['type'],
     35                        'guid'           => $file_attributes['url'],
     36                        'context'        => 'site-icon',
     37                ),
     38                        $file_attributes['file'] );
     39
     40                if ( 0 === $attachment_id || is_wp_error( $attachment_id ) ) {
     41                        wp_die( __( 'Could not save the uploaded image.' ) );
     42                }
     43
     44                return $attachment_id;
     45        }
     46
     47        public function get_image_size_from_attachment( $attachment_id ) {
     48                $image_file = get_attached_file( absint( $attachment_id ), true );
     49
     50                if ( ! $image_file ) {
     51                        wp_die( __( 'The uploaded image is corrupted.' ) );
     52                }
     53
     54                return getimagesize( $image_file );
     55        }
     56
     57        /**
     58         * Checks whether the uploaded image is large enough to be a Site Icon.
     59         *
     60         * @param int $attachment_id ID of the attachment containing the image.
     61         * @param int $min_width     Minimum width in pixels.
     62         * @param int $min_height    Minimum height in pixels.
     63         *
     64         * @return bool|array True if the image is large enough. An array with the errors if the image is too small.
     65         */
     66        public function verify_site_icon_image_min_size( $attachment_id, $min_width, $min_height ) {
     67                $image_file = get_attached_file( absint( $attachment_id ), true );
     68
     69                if ( ! $image_file ) {
     70                        wp_die( __( 'The uploaded image is corrupted.' ) );
     71                }
     72
     73                $image_size = getimagesize( $image_file );
     74
     75                $errors = array();
     76                if ( $image_size[0] < $min_width ) {
     77                        $errors['width'] = true;
     78                }
     79
     80                if ( $image_size[1] < $min_height ) {
     81                        $errors['height'] = true;
     82                }
     83
     84                if ( $errors ) {
     85                        return $errors;
     86                }
     87
     88                return true;
     89        }
     90
     91        public function get_attachment_image_size( $attachment_id ) {
     92                $image_file = get_attached_file( absint( $attachment_id ), true );
     93
     94                if ( ! $image_file ) {
     95                        wp_die( __( 'The uploaded image is corrupted.' ) );
     96                }
     97
     98                return getimagesize( $image_file );
     99        }
     100
     101        public function get_site_icon_image_url( $attachment_id ) {
     102                $image_data = wp_get_attachment_image_src( $attachment_id, 'full' );
     103
     104                if ( ! $image_data ) {
     105                        return false;
     106                }
     107
     108                return $image_data[0];
     109        }
     110
     111        /**
     112         * Calculate the size of the image displayed on the cropping screen.
     113         *
     114         * @param $attachment_id
     115         * @param $max_size
     116         */
     117        public function calculate_height_of_image_for_cropping( $attachment_id, $max_width ) {
     118                // Get the size of the full image.
     119                $full_size = $this->get_attachment_image_size( absint( $attachment_id ) );
     120
     121                // Calculate the relationship between the height and the width of the full image.
     122                $ratio = $full_size[1] / $full_size[0];
     123
     124                // Multiply the max width of the image in the admin to get the correct height to keeping the ratio intact.
     125                return (int) floor( $max_width * $ratio );
     126        }
     127
     128        public function calculate_default_values_for_crop( $width, $height ) {
     129                if ( $width < $height ) {
     130                        // Portrait
     131                        $crop_x    = 0;
     132                        $crop_y    = absint( ( $height - $width ) / 2 );
     133                        $crop_size = $width;
     134                } elseif ( $width > $height ) {
     135                        // Landscape
     136                        $crop_x    = absint( ( $width - $height ) / 2 );
     137                        $crop_y    = 0;
     138                        $crop_size = $height;
     139                } else {
     140                        // Square
     141                        $crop_x    = 0;
     142                        $crop_y    = 0;
     143                        $crop_size = $width;
     144                }
     145
     146                return compact( 'crop_x', 'crop_y', 'crop_size' );
     147        }
     148
     149        /**
     150         * This function is used to pass data to the localize script
     151         * so that we can center the cropper and also set the minimum
     152         * cropper.
     153         *
     154         * @since 4.3.0
     155         *
     156         * @param float $ratio
     157         * @param array $cropped_size
     158         * @return array
     159         */
     160        public function calculate_default_values_for_javascript_cropper( $ratio, $max_size,  $resized_width, $resized_height, $orginial_width, $original_height ) {
     161                $init_x = $init_y = $init_size = 0;
     162                $min_crop_size  = ( $max_size / $ratio );
     163
     164                if ( $resized_width < $resized_height ) {
     165                        // Portrait
     166                        $init_y    = ( $max_size - $resized_width ) / 2;
     167                        $init_size = $resized_height;
     168                } elseif ( $resized_width > $resized_height ) {
     169                        // Landscape
     170                        $init_x    = ( $max_size - $resized_height ) / 2;
     171                        $init_size = $resized_height;
     172                } else {
     173                        // Square
     174                        $init_size = $resized_height;
     175                }
     176
     177                return array(
     178                        'init_x'    => (int) floor( $init_x ),
     179                        'init_y'    => (int) floor($init_y ),
     180                        'init_size' => $init_size,
     181                        'min_size'  => (int) floor( $min_crop_size ),
     182                        'width'     => $orginial_width,
     183                        'height'    => $original_height,
     184                );
     185        }
     186
     187        /**
     188         * Converts the coordinates from the downsized image to the original image for accurate cropping.
     189         *
     190         * @since 4.3.0
     191         *
     192         * @param int   $crop_x
     193         * @param int   $crop_y
     194         * @param int   $crop_width
     195         * @param int   $crop_height
     196         * @param float $ratio
     197         * @return array
     198         */
     199        public function convert_coordinates_from_resized_to_full( $crop_x, $crop_y, $crop_width, $crop_height, $ratio ) {
     200                return array(
     201                        'crop_x'      => floor( $crop_x * $ratio ),
     202                        'crop_y'      => floor( $crop_y * $ratio ),
     203                        'crop_width'  => floor( $crop_width * $ratio ),
     204                        'crop_height' => floor( $crop_height * $ratio ),
     205                );
     206        }
     207
     208        /**
     209         * Create an attachment 'object'.
     210         *
     211         * @since 4.3.0
     212         *
     213         * @param string $cropped              Cropped image URL.
     214         * @param int    $parent_attachment_id Attachment ID of parent image.
     215         * @return array Attachment object.
     216         */
     217        public function create_attachment_object( $cropped, $parent_attachment_id ) {
     218                $parent     = get_post( $parent_attachment_id );
     219                $parent_url = $parent->guid;
     220                $url        = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
     221
     222                $size       = @getimagesize( $cropped );
     223                $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
     224
     225                $object = array(
     226                        'ID'             => $parent_attachment_id,
     227                        'post_title'     => basename( $cropped ),
     228                        'post_content'   => $url,
     229                        'post_mime_type' => $image_type,
     230                        'guid'           => $url,
     231                        'context'        => 'site-icon'
     232                );
     233
     234                return $object;
     235        }
     236
     237        /**
     238         * Insert an attachment and its metadata.
     239         *
     240         * @since 4.3.0
     241         *
     242         * @param array  $object  Attachment object.
     243         * @param string $cropped Cropped image URL.
     244         * @return int Attachment ID.
     245         */
     246        public function insert_attachment( $object, $cropped ) {
     247                $attachment_id = wp_insert_attachment( $object, $cropped );
     248                $metadata      = wp_generate_attachment_metadata( $attachment_id, $cropped );
     249
     250                /**
     251                 * Filter the site icon attachment metadata.
     252                 *
     253                 * @since 4.3.0
     254                 *
     255                 * @see wp_generate_attachment_metadata()
     256                 *
     257                 * @param array $metadata Attachment metadata.
     258                 */
     259                $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
     260                wp_update_attachment_metadata( $attachment_id, $metadata );
     261
     262                return $attachment_id;
     263        }
     264
     265        /**
     266         * Deletes all additional image sizes, used for site icons.
     267         *
     268         * @since 4.3.0
     269         *
     270         * @return bool
     271         */
     272        public function delete_site_icon() {
     273                // We add the filter to make sure that we also delete all the added images.
     274                add_filter( 'intermediate_image_sizes', array( WP_Site_Icon::get_instance(), 'intermediate_image_sizes' ) );
     275                wp_delete_attachment( get_option( 'site_icon' ), true );
     276                remove_filter( 'intermediate_image_sizes', array( WP_Site_Icon::get_instance(), 'intermediate_image_sizes' ) );
     277
     278                return delete_option( 'site_icon' );
     279        }
     280
     281        /**
     282         * Deletes the Site Icon when the image file is deleted.
     283         *
     284         * @since 4.3.0
     285         *
     286         * @param int $post_id Attachment ID.
     287         */
     288        public function delete_attachment_data( $post_id ) {
     289                $site_icon_id = get_option( 'site_icon' );
     290
     291                if ( $site_icon_id && $post_id == $site_icon_id ) {
     292                        delete_option( 'site_icon' );
     293                }
     294        }
     295
     296        /**
     297         * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
     298         *
     299         * @since 4.3.0
     300         *
     301         * @param null|array|string $value    The value get_metadata() should
     302         *                                    return - a single metadata value,
     303         *                                    or an array of values.
     304         * @param int               $post_id  Post ID.
     305         * @param string            $meta_key Meta key.
     306         * @param string|array      $single   Meta value, or an array of values.
     307         * @return array|null|string
     308         */
     309        public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
     310                $site_icon_id = get_option( 'site_icon' );
     311
     312                if ( $post_id == $site_icon_id && '_wp_attachment_backup_sizes' == $meta_key && $single ) {
     313                        add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
     314                }
     315
     316                return $value;
     317        }
     318
     319}
     320
     321
     322
     323
  • wp-admin/includes/class-wp-site-icon-admin-ui.php

    Property changes on: wp-admin/includes/class-wp-site-icon-admin-processing.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
     1<?php
     2
     3class WP_Site_Icon_Admin_UI {
     4        /**
     5         * The size to which to crop the image so that we can display it in the UI nicely.
     6         *
     7         * @since 4.3.0
     8         *
     9         * @var int
     10         *
     11         * TO DO: Rename
     12         */
     13        public $page_crop = 512;
     14
     15        var $admin_ui_processing;
     16        var $site_icon;
     17        var $admin_page_hook;
     18
     19        public function __construct( $admin_ui_processing, $site_icon ) {
     20                $this->admin_ui_processing = $admin_ui_processing;
     21                $this->site_icon = $site_icon;
     22
     23                // Add the favicon to the backend.
     24                add_action( 'admin_head', 'wp_site_icon' );
     25
     26                add_action( 'admin_menu', array( $this, 'add_site_icon_page_to_menu' ) );
     27                add_action( 'admin_init', array( $this, 'add_upload_page_settings' ) );
     28
     29                add_action( 'admin_action_set_site_icon',    array( $this, 'set_site_icon'    ) );
     30                add_action( 'admin_action_remove_site_icon', array( $this, 'remove_site_icon' ) );
     31        }
     32
     33        // To Do: nonce checking
     34        public function site_icon_page_controller() {
     35                $action = isset( $_GET['action'] ) ? $_GET['action'] : 'select-site-icon';
     36
     37                switch ( $action ) {
     38                        case 'select-site-icon':
     39                                $this->display_upload_page();
     40                                break;
     41
     42                        case 'crop-site-icon':
     43                                // Handle image upload for the no Javascript version.
     44                                if ( isset ( $_GET['upload'] ) ) {
     45                                        $attachment_id = $this->admin_ui_processing->upload_site_icon_image();
     46                                } else {
     47                                        // The Javascript upload skips the select and upload steps, providing the attachment ID.
     48                                        $attachment_id = $_GET['file'];
     49                                }
     50
     51                                // Verify that the image is large enough.
     52                                $image_size = $this->admin_ui_processing->get_image_size_from_attachment( $attachment_id );
     53
     54                                // TO DO: Move this to a method.
     55                                if ( $image_size[0] < $this->site_icon->min_size ) {
     56                                        add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->site_icon->min_size ) );
     57                                        $this->display_upload_page();
     58                                } elseif ( $image_size[1] < $this->site_icon->min_size ) {
     59                                        add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in height.' ), $this->site_icon->min_size ) );
     60                                        $this->display_upload_page();
     61                                } else {
     62                                        // TO DO: Move this to a method.
     63                                        // URL of the uploaded image.
     64                                        $image_url = $this->admin_ui_processing->get_site_icon_image_url( $attachment_id );
     65                                        // Dimensions of the uploaded image adapted to the admin.
     66                                        $max_height = $this->admin_ui_processing->calculate_height_of_image_for_cropping( $attachment_id, $this->page_crop );
     67                                        // Add the default crop values for the no JS version.
     68                                        $default_crop = $this->admin_ui_processing->calculate_default_values_for_crop( $this->page_crop, $max_height );
     69                                        // Ratio for cropping
     70                                        $crop_ratio = $image_size[0] / $this->page_crop;
     71
     72                                        $this->display_crop_page( $attachment_id, $image_url, $this->page_crop, $max_height, $default_crop, $crop_ratio );
     73                                }
     74                                break;
     75
     76                        default:
     77                                wp_safe_redirect( admin_url( 'options-general.php#site-icon' ) );
     78                                exit;
     79                }
     80        }
     81
     82        /**
     83         * Adds the Site Icon Page to the admin menu.
     84         *
     85         * The `parent_slug` argument in `add_menu_page()` is set to `null`, so that no link is added in the admin interface.
     86         *
     87         * @since 4.3.0
     88         */
     89        public function add_site_icon_page_to_menu() {
     90                $hook = add_submenu_page( null, __( 'Site Icon' ), __( 'Site Icon' ), 'manage_options', 'site-icon', array( $this, 'site_icon_page_controller' ) );
     91
     92                add_action( "admin_print_scripts-$hook", array( $this, 'enqueue_scripts' ) );
     93        }
     94
     95        /**
     96         * Register a settings section for uploading a Site Icon image.
     97         *
     98         * @since 4.3.0
     99         */
     100        public function add_upload_page_settings() {
     101                add_settings_section( 'site-icon-upload', false, false, 'site-icon-upload' );
     102                add_settings_field( 'site-icon-upload', __( 'Upload Image' ), array( $this, 'display_upload_page_image_selection' ), 'site-icon-upload', 'site-icon-upload', array(
     103                        'label_for' => 'site-icon-upload',
     104                ) );
     105        }
     106
     107        /**
     108         * Add scripts to admin settings pages.
     109         *
     110         * @since 4.3.0
     111         */
     112        public function enqueue_scripts() {
     113                wp_enqueue_style( 'jcrop' );
     114                wp_enqueue_script( 'site-icon-crop' );
     115        }
     116
     117        /**
     118         * Display the admin page for uploading a Site Icon image.
     119         *
     120         * This screen is only visible when Javascript is deactivated.
     121         *
     122         * @since 4.3.0
     123         */
     124        public function display_upload_page() {
     125                ?>
     126                <div class="wrap">
     127                        <h2><?php _e( 'Add Site Icon' ); ?></h2>
     128                        <?php settings_errors( 'site-icon' ); ?>
     129                        <?php do_settings_sections( 'site-icon-upload' ); ?>
     130                </div>
     131        <?php
     132        }
     133
     134        /**
     135         * Settings field for file upload.
     136         *
     137         * @since 4.3.0
     138         */
     139        public function display_upload_page_image_selection() {
     140                // Render modal for uploading via Javascript.
     141                // TO DO: Script enqueuing should be done in a single method.
     142                wp_enqueue_media();
     143                wp_enqueue_script( 'site-icon' );
     144                wp_dequeue_script( 'site-icon-crop' );
     145
     146                $update_url = esc_url( add_query_arg( array(
     147                        'page' => 'site-icon',
     148                        'action' => 'crop-site-icon',
     149                ), wp_nonce_url( admin_url( 'options-general.php' ), 'crop-site-icon' ) ) );
     150                ?>
     151                <p class="hide-if-no-js">
     152                        <label class="screen-reader-text" for="choose-from-library-link"><?php _e( 'Choose an image from your media library:' ); ?></label>
     153                        <?php
     154                                printf( '<button type="button" id="choose-from-library-link" class="button" data-update-link="%$1s" data-choose="%$2s" data-update="%$3s">%$4s</button>',
     155                                        esc_attr( $update_url ),
     156                                        esc_attr__( 'Choose a Site Icon' ),
     157                                        esc_attr__( 'Set as Site Icon' ),
     158                                        __( 'Choose Image' )
     159                                );
     160                        ?>
     161                </p>
     162
     163                <?php // Upload form for when Javascript is deactivated ?>
     164                <form class="hide-if-js" action="<?php echo esc_url( admin_url( 'options-general.php?page=site-icon&action=crop-site-icon&upload' ) ); ?>" method="post" enctype="multipart/form-data">
     165                        <input name="action" value="crop_site_icon" type="hidden" />
     166                        <input name="site-icon" type="file" />
     167                        <input name="submit" value="<?php esc_attr_e( 'Upload Image' ); ?>" type="submit" class="button button-primary" />
     168                        <p class="description">
     169                                <?php printf( __( 'The image is recommended to be a square image of at least %spx in both width and height.' ), "<strong>" . absint( $this->site_icon->min_size ) . '</strong>' ); ?>
     170                        </p>
     171                        <?php wp_nonce_field( 'crop-site-icon' ); ?>
     172                </form>
     173        <?php
     174        }
     175
     176        public function display_crop_page( $attachment_id, $image_url, $resized_image_width, $resized_image_height, $default_crop, $crop_ratio ) {
     177                check_admin_referer( 'crop-site-icon' );
     178
     179                // TO DO: Script enqueuing should be done in a single method.
     180                $image_size = $this->admin_ui_processing->get_attachment_image_size( $attachment_id );
     181                wp_localize_script( 'site-icon-crop', 'wpSiteIconCropData', $this->admin_ui_processing->calculate_default_values_for_javascript_cropper( $crop_ratio, $this->page_crop, $resized_image_width, $resized_image_height, $image_size[0], $image_size[1] ) );
     182        ?>
     183                <div class="wrap">
     184                        <h2 class="site-icon-title"><?php _e( 'Site Icon' ); ?></h2>
     185                        <?php settings_errors( 'site-icon' ); ?>
     186
     187                        <div class="site-icon-crop-shell">
     188                                <form action="options-general.php" method="post" enctype="multipart/form-data">
     189                                        <p class="hide-if-no-js description"><?php _e('Choose the part of the image you want to use as your site icon.'); ?></p>
     190                                        <p class="hide-if-js description"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p>
     191                                        <?php
     192                                                // Image which is used to select the right cropping.
     193                                                // This image needs to be resized to allow for easy cropping.
     194                                                printf( '<img src="%1$s" id="crop-image" class="site-icon-crop-image" width="%2$s" height="%3$s" alt="%4$s" />',
     195                                                        esc_url( $image_url ),
     196                                                        absint( $resized_image_width ),
     197                                                        absint( $resized_image_height ),
     198                                                        esc_attr__( 'Image to be cropped' )
     199                                                );
     200
     201                                                // Site icon preview in browser bar and on mobile.
     202                                        ?>
     203                                        <div class="site-icon-crop-preview-shell hide-if-no-js">
     204                                                <h3><?php _e( 'Preview' ); ?></h3>
     205                                                <strong><?php _e( 'As your favicon' ); ?></strong>
     206                                                <div class="site-icon-crop-favicon-preview-shell">
     207                                                        <img src="images/browser.png" class="site-icon-browser-preview" width="182" height="" alt="<?php esc_attr_e( 'Browser Chrome' ); ?>"/>
     208                                                        <div class="site-icon-crop-preview-favicon">
     209                                                                <img src="<?php echo esc_url( $image_url ); ?>" id="preview-favicon" alt="<?php esc_attr_e( 'Preview Favicon' ); ?>"/>
     210                                                        </div>
     211                                                        <span class="site-icon-browser-title"><?php bloginfo( 'name' ); ?></span>
     212                                                </div>
     213                                                <strong><?php _e( 'As a mobile icon' ); ?></strong>
     214                                                <div class="site-icon-crop-preview-homeicon">
     215                                                        <img src="<?php echo esc_url( $image_url ); ?>" id="preview-homeicon" alt="<?php esc_attr_e( 'Preview Home Icon' ); ?>"/>
     216                                                </div>
     217                                        </div>
     218
     219                                        <?php // Default crop values for no JS fallback. ?>
     220                                        <input type="hidden" id="crop-x" name="crop-x" value="<?php echo absint( $default_crop['crop_x'] ); ?>" />
     221                                        <input type="hidden" id="crop-y" name="crop-y" value="<?php echo absint( $default_crop['crop_y'] ); ?>" />
     222                                        <input type="hidden" id="crop-width" name="crop-w" value="<?php echo absint( $default_crop['crop_size'] ); ?>" />
     223                                        <input type="hidden" id="crop-height" name="crop-h" value="<?php echo absint( $default_crop['crop_size'] ); ?>" />
     224
     225                                        <input type="hidden" name="action" value="set_site_icon" />
     226                                        <input type="hidden" name="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
     227
     228                                        <input type="hidden" name="crop_ratio" value="<?php echo esc_attr( $crop_ratio ); ?>" />
     229                                        <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) : ?>
     230                                                <input type="hidden" name="create-new-attachment" value="true" />
     231                                        <?php endif; ?>
     232                                        <?php wp_nonce_field( 'set-site-icon' ); ?>
     233
     234                                        <p class="submit">
     235                                                <?php submit_button( __( 'Crop and Publish' ), 'primary hide-if-no-js', 'submit', false ); ?>
     236                                                <?php submit_button( __( 'Publish' ), 'primary hide-if-js', 'submit', false ); ?>
     237                                                <a class="button secondary" href="options-general.php"><?php _e( 'Cancel' ); ?></a>
     238                                        </p>
     239                                </form>
     240                        </div>
     241                </div>
     242        <?php
     243        }
     244
     245        /**
     246         * Saves a new Site Icon.
     247         *
     248         * TO DO: Split this apart, and move code to smaller methods in `WP_Site_Icon_Admin_Processing`.
     249         *
     250         * @since 4.3.0
     251         */
     252        public function set_site_icon() {
     253                check_admin_referer( 'set-site-icon' );
     254
     255                // Delete any existing site icon images.
     256                $this->admin_ui_processing->delete_site_icon();
     257
     258                $attachment_id = absint( $_POST['attachment_id'] );
     259
     260                // TODO
     261                if ( empty( $_POST['skip-cropping'] ) ) {
     262                        $crop_ratio = (float) $_POST['crop_ratio'];
     263                        $crop_data = $this->admin_ui_processing->convert_coordinates_from_resized_to_full( $_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio );
     264                        $cropped = wp_crop_image( $attachment_id, $crop_data['crop_x'], $crop_data['crop_y'], $crop_data['crop_width'], $crop_data['crop_height'], $this->site_icon->min_size, $this->site_icon->min_size );
     265                } elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
     266                        $cropped = _copy_image_file( $attachment_id );
     267                } else {
     268                        $cropped = get_attached_file( $attachment_id );
     269                }
     270
     271                if ( ! $cropped || is_wp_error( $cropped ) ) {
     272                        wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
     273                }
     274
     275                $object = $this->admin_ui_processing->create_attachment_object( $cropped, $attachment_id );
     276
     277                if ( ! empty( $_POST['create-new-attachment'] ) ) {
     278                        unset( $object['ID'] );
     279                }
     280
     281                // Update the attachment
     282                add_filter( 'intermediate_image_sizes_advanced', array( $this->site_icon, 'additional_sizes' ) );
     283                $attachment_id = $this->admin_ui_processing->insert_attachment( $object, $cropped );
     284                remove_filter( 'intermediate_image_sizes_advanced', array( $this->site_icon, 'additional_sizes' ) );
     285
     286                // Save the site_icon data into option
     287                update_option( 'site_icon', $attachment_id );
     288
     289                add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' );
     290        }
     291
     292        /**
     293         * Removes site icon.
     294         *
     295         * @since 4.3.0
     296         */
     297        public function remove_site_icon() {
     298                check_admin_referer( 'remove-site-icon' );
     299
     300                // TO DO: Check return value to display success message.
     301                $this->admin_ui_processing->delete_site_icon();
     302
     303                add_settings_error( 'site-icon', 'icon-removed', __( 'Site Icon removed.' ), 'updated' );
     304        }
     305
     306}
     307
     308$site_icon_admin = new WP_Site_Icon_Admin_UI( new WP_Site_Icon_Admin_Processing(), WP_Site_Icon::get_instance() );
     309 No newline at end of file
  • wp-admin/includes/class-wp-site-icon.php

    Property changes on: wp-admin/includes/class-wp-site-icon-admin-ui.php
    ___________________________________________________________________
    Added: svn:executable
    ## -0,0 +1 ##
    +*
    \ No newline at end of property
     
    11<?php
    22
    3 /**
    4  * Class WP_Site_Icon.
    5  *
    6  * @since 4.3.0
    7  */
    83class WP_Site_Icon {
     4        private static $instance = null;
    95
    106        /**
    117         * The minimum size of the site icon.
     
    1713        public $min_size  = 512;
    1814
    1915        /**
    20          * The size to which to crop the image so that we can display it in the UI nicely.
    2116         *
    2217         * @since 4.3.0
    2318         *
    24          * @var int
    25          */
    26         public $page_crop = 512;
    27 
    28         /**
    29          *
    30          * @since 4.3.0
    31          *
    3219         * @var array
    3320         */
    3421        public $site_icon_sizes = array(
     
    5037                32,
    5138        );
    5239
    53         /**
    54          * Register our actions and filters.
    55          *
    56          * @since 4.3.0
    57          */
    58         public function __construct() {
    59 
    60                 // Add the favicon to the backend.
    61                 add_action( 'admin_menu', array( $this, 'admin_menu_upload_site_icon' ) );
    62 
    63                 add_action( 'admin_action_set_site_icon',    array( $this, 'set_site_icon'    ) );
    64                 add_action( 'admin_action_remove_site_icon', array( $this, 'remove_site_icon' ) );
    65 
    66                 add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ), 10, 1 );
    67                 add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 );
    68         }
    69 
    70         /**
    71          * Add a hidden upload page.
    72          *
    73          * There is no need to access it directly.
    74          *
    75          * @since 4.3.0
    76          */
    77         public function admin_menu_upload_site_icon() {
    78                 $hook = add_submenu_page( null, __( 'Site Icon' ), __( 'Site Icon' ), 'manage_options', 'site-icon', array( $this, 'upload_site_icon_page' ) );
    79 
    80                 add_action( "load-$hook", array( $this, 'add_upload_settings' ) );
    81                 add_action( "load-$hook", array( $this, 'maybe_skip_cropping' ) );
    82                 add_action( "admin_print_scripts-$hook", array( $this, 'enqueue_scripts' ) );
    83         }
    84 
    85         /**
    86          * Add scripts to admin settings pages.
    87          *
    88          * @since 4.3.0
    89          */
    90         public function enqueue_scripts() {
    91                 wp_enqueue_style( 'jcrop' );
    92                 wp_enqueue_script( 'site-icon-crop' );
    93         }
    94 
    95         /**
    96          * Load on when the admin is initialized.
    97          *
    98          * @since 4.3.0
    99          */
    100         public function add_upload_settings() {
    101                 add_settings_section( 'site-icon-upload', false, false, 'site-icon-upload' );
    102                 add_settings_field( 'site-icon-upload', __( 'Upload Image' ), array( $this, 'upload_field' ), 'site-icon-upload', 'site-icon-upload', array(
    103                         'label_for' => 'site-icon-upload',
    104                 ) );
    105         }
    106 
    107         /**
    108          * Removes site icon.
    109          *
    110          * @since 4.3.0
    111          */
    112         public function remove_site_icon() {
    113                 check_admin_referer( 'remove-site-icon' );
    114 
    115                 $this->delete_site_icon();
    116 
    117                 add_settings_error( 'site-icon', 'icon-removed', __( 'Site Icon removed.' ), 'updated' );
    118         }
    119 
    120         /**
    121          * Uploading a site_icon is a 3 step process
    122          *
    123          * 1. Select the file to upload
    124          * 2. Crop the file
    125          * 3. Confirmation
    126          *
    127          * @since 4.3.0
    128          */
    129         public function upload_site_icon_page() {
    130                 $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'select_site_icon';
    131 
    132                 switch ( $action ) {
    133                         case 'select_site_icon':
    134                                 $this->select_page();
    135                                 break;
    136 
    137                         case 'crop_site_icon':
    138                                 $this->crop_page();
    139                                 break;
    140 
    141                         default:
    142                                 wp_safe_redirect( admin_url( 'options-general.php#site-icon' ) );
    143                                 exit;
     40        static function get_instance() {
     41                if ( is_null( self::$instance ) ) {
     42                        self::$instance = new WP_Site_Icon;
    14443                }
     44                return self::$instance;
    14545        }
    14646
    14747        /**
    148          * Displays the site_icon form to upload the image.
     48         * Add Site Icon sizes to the array of image sizes on demand.
    14949         *
    15050         * @since 4.3.0
    151          */
    152         public function select_page() {
    153                 ?>
    154                 <div class="wrap">
    155                         <h1><?php _e( 'Add Site Icon' ); ?></h1>
    156                         <?php settings_errors( 'site-icon' ); ?>
    157                         <?php do_settings_sections( 'site-icon-upload' ); ?>
    158                 </div>
    159         <?php
    160         }
    161 
    162         /**
    163          * Settings field for file upload.
    16451         *
    165          * @since 4.3.0
     52         * @param array $sizes
     53         * @return array
    16654         */
    167         public function upload_field() {
    168                 wp_enqueue_media();
    169                 wp_enqueue_script( 'site-icon' );
    170                 wp_dequeue_script( 'site-icon-crop' );
    171 
    172                 $update_url = esc_url( add_query_arg( array(
    173                         'page' => 'site-icon',
    174                         'action' => 'crop_site_icon',
    175                 ), wp_nonce_url( admin_url( 'options-general.php' ), 'crop-site-icon' ) ) );
    176                 ?>
    177                 <p class="hide-if-no-js">
    178                         <label class="screen-reader-text" for="choose-from-library-link"><?php _e( 'Choose an image from your media library:' ); ?></label>
    179                         <button type="button" id="choose-from-library-link" class="button" data-size="<?php echo absint( $this->min_size ); ?>" data-update-link="<?php echo esc_attr( $update_url ); ?>" data-choose="<?php esc_attr_e( 'Choose a Site Icon' ); ?>" data-update="<?php esc_attr_e( 'Set as Site Icon' ); ?>"><?php _e( 'Choose Image' ); ?></button>
    180                 </p>
    181                 <form class="hide-if-js" action="<?php echo esc_url( admin_url( 'options-general.php?page=site-icon' ) ); ?>" method="post" enctype="multipart/form-data">
    182                         <input name="action" value="crop_site_icon" type="hidden" />
    183                         <input name="site-icon" type="file" />
    184                         <input name="submit" value="<?php esc_attr_e( 'Upload Image' ); ?>" type="submit" class="button button-primary" />
    185                         <p class="description">
    186                                 <?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>" ); ?>
    187                         </p>
    188                         <?php wp_nonce_field( 'crop-site-icon' ); ?>
    189                 </form>
    190         <?php
    191         }
    192 
    193         /**
    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;
     55        public function intermediate_image_sizes( $sizes = array() ) {
     56                /** This filter is documented in wp-admin/includes/site-icon.php */
     57                $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
     58                foreach ( $this->site_icon_sizes as $size ) {
     59                        $sizes[] = 'site_icon-' . $size;
    20360                }
    20461
    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                 }
     62                return $sizes;
    22263        }
    22364
    22465        /**
    225          * Crop a the image admin view.
    226          *
    227          * @since 4.3.0
    228          */
    229         public function crop_page() {
    230                 check_admin_referer( 'crop-site-icon' );
    231 
    232                 list( $attachment_id, $url, $image_size ) = $this->get_upload_data();
    233 
    234                 if ( $image_size[0] < $this->min_size ) {
    235                         add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->min_size ) );
    236 
    237                         // back to step one
    238                         $this->select_page();
    239 
    240                         return;
    241                 }
    242 
    243                 if ( $image_size[1] < $this->min_size ) {
    244                         add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in height.' ), $this->min_size ) );
    245 
    246                         // Back to step one.
    247                         $this->select_page();
    248 
    249                         return;
    250                 }
    251 
    252                 wp_localize_script( 'site-icon-crop', 'wpSiteIconCropData', array(
    253                         'init_x'    => 0,
    254                         'init_y'    => 0,
    255                         'init_size' => $this->min_size,
    256                         'min_size'  => $this->min_size,
    257                         'width'     => $image_size[0],
    258                         'height'    => $image_size[1],
    259                 ) );
    260                 ?>
    261 
    262                 <div class="wrap">
    263                         <h1 class="site-icon-title"><?php _e( 'Site Icon' ); ?></h1>
    264                         <?php settings_errors( 'site-icon' ); ?>
    265 
    266                         <div class="site-icon-crop-shell">
    267                                 <form action="options-general.php" method="post" enctype="multipart/form-data">
    268                                         <p class="hide-if-no-js description"><?php _e('Choose the part of the image you want to use as your site icon.'); ?></p>
    269                                         <p class="hide-if-js description"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p>
    270 
    271                                         <div class="site-icon-crop-wrapper">
    272                                                 <img src="<?php echo esc_url( $url ); ?>" id="crop-image" class="site-icon-crop-image" width="512" height="" alt="<?php esc_attr_e( 'Image to be cropped' ); ?>"/>
    273                                         </div>
    274 
    275                                         <div class="site-icon-crop-preview-shell hide-if-no-js">
    276                                                 <h3><?php _e( 'Preview' ); ?></h3>
    277                                                 <strong><?php _e( 'As a browser icon' ); ?></strong>
    278                                                 <div class="site-icon-crop-favicon-preview-shell">
    279                                                         <img src="images/browser.png" class="site-icon-browser-preview" width="182" height="" alt="<?php esc_attr_e( 'Browser Chrome' ); ?>"/>
    280 
    281                                                         <div class="site-icon-crop-preview-favicon">
    282                                                                 <img src="<?php echo esc_url( $url ); ?>" id="preview-favicon" alt="<?php esc_attr_e( 'Preview Favicon' ); ?>"/>
    283                                                         </div>
    284                                                         <span class="site-icon-browser-title"><?php bloginfo( 'name' ); ?></span>
    285                                                 </div>
    286 
    287                                                 <strong><?php _e( 'As an app icon' ); ?></strong>
    288                                                 <div class="site-icon-crop-preview-homeicon">
    289                                                         <img src="<?php echo esc_url( $url ); ?>" id="preview-homeicon" alt="<?php esc_attr_e( 'Preview Home Icon' ); ?>"/>
    290                                                 </div>
    291                                         </div>
    292 
    293                                         <input type="hidden" id="crop-x" name="crop-x" value="0" />
    294                                         <input type="hidden" id="crop-y" name="crop-y" value="0" />
    295                                         <input type="hidden" id="crop-width" name="crop-w" value="<?php echo esc_attr( $this->min_size ); ?>" />
    296                                         <input type="hidden" id="crop-height" name="crop-h" value="<?php echo esc_attr( $this->min_size ); ?>" />
    297 
    298                                         <input type="hidden" name="action" value="set_site_icon" />
    299                                         <input type="hidden" name="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
    300                                         <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) : ?>
    301                                                 <input type="hidden" name="create-new-attachment" value="true" />
    302                                         <?php endif; ?>
    303                                         <?php wp_nonce_field( 'set-site-icon' ); ?>
    304 
    305                                         <p class="submit">
    306                                                 <?php submit_button( __( 'Crop and Publish' ), 'primary hide-if-no-js', 'submit', false ); ?>
    307                                                 <?php submit_button( __( 'Publish' ), 'primary hide-if-js', 'submit', false ); ?>
    308                                                 <a class="button secondary" href="options-general.php"><?php _e( 'Cancel' ); ?></a>
    309                                         </p>
    310                                 </form>
    311                         </div>
    312                 </div>
    313         <?php
    314         }
    315 
    316         /**
    317          * Saves a new Site Icon.
    318          *
    319          * @since 4.3.0
    320          */
    321         public function set_site_icon() {
    322                 check_admin_referer( 'set-site-icon' );
    323 
    324                 $attachment_id          = absint( $_REQUEST['attachment_id'] );
    325                 $create_new_attachement = ! empty( $_REQUEST['create-new-attachment'] );
    326 
    327                 /*
    328                  * If the current attachment as been set as site icon don't delete it.
    329                  */
    330                 if ( get_option( 'site_icon' ) == $attachment_id ) {
    331                         // Get the file path.
    332                         $image_url = get_attached_file( $attachment_id );
    333 
    334                         // Update meta data and possibly regenerate intermediate sizes.
    335                         add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    336                         $this->update_attachment_metadata( $attachment_id, $image_url );
    337                         remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    338 
    339                 } else {
    340                         // Delete any existing site icon images.
    341                         $this->delete_site_icon();
    342 
    343                         if ( empty( $_REQUEST['skip-cropping'] ) ) {
    344                                 $cropped = wp_crop_image( $attachment_id, $_REQUEST['crop-x'], $_REQUEST['crop-y'], $_REQUEST['crop-w'], $_REQUEST['crop-h'], $this->min_size, $this->min_size );
    345 
    346                         } elseif ( $create_new_attachement ) {
    347                                 $cropped = _copy_image_file( $attachment_id );
    348 
    349                         } else {
    350                                 $cropped = get_attached_file( $attachment_id );
    351                         }
    352 
    353                         if ( ! $cropped || is_wp_error( $cropped ) ) {
    354                                 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    355                         }
    356 
    357                         $object = $this->create_attachment_object( $cropped, $attachment_id );
    358 
    359                         if ( $create_new_attachement ) {
    360                                 unset( $object['ID'] );
    361                         }
    362 
    363                         // Update the attachment.
    364                         add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    365                         $attachment_id = $this->insert_attachment( $object, $cropped );
    366                         remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) );
    367 
    368                         // Save the site_icon data into option
    369                         update_option( 'site_icon', $attachment_id );
    370                 }
    371 
    372                 add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' );
    373         }
    374 
    375         /**
    376          * Upload the file to be cropped in the second step.
    377          *
    378          * @since 4.3.0
    379          */
    380         public function handle_upload() {
    381                 $uploaded_file = $_FILES['site-icon'];
    382                 $file_type     = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] );
    383                 if ( ! wp_match_mime_types( 'image', $file_type['type'] ) ) {
    384                         wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) );
    385                 }
    386 
    387                 $file = wp_handle_upload( $uploaded_file, array( 'test_form' => false ) );
    388 
    389                 if ( isset( $file['error'] ) ) {
    390                         wp_die( $file['error'], __( 'Image Upload Error' ) );
    391                 }
    392 
    393                 $url      = $file['url'];
    394                 $type     = $file['type'];
    395                 $file     = $file['file'];
    396                 $filename = basename( $file );
    397 
    398                 // Construct the object array
    399                 $object = array(
    400                         'post_title'     => $filename,
    401                         'post_content'   => $url,
    402                         'post_mime_type' => $type,
    403                         'guid'           => $url,
    404                         'context'        => 'site-icon',
    405                 );
    406 
    407                 // Save the data
    408                 $attachment_id = wp_insert_attachment( $object, $file );
    409 
    410                 return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
    411         }
    412 
    413         /**
    414          * Create an attachment 'object'.
    415          *
    416          * @since 4.3.0
    417          *
    418          * @param string $cropped              Cropped image URL.
    419          * @param int    $parent_attachment_id Attachment ID of parent image.
    420          * @return array Attachment object.
    421          */
    422         public function create_attachment_object( $cropped, $parent_attachment_id ) {
    423                 $parent     = get_post( $parent_attachment_id );
    424                 $parent_url = $parent->guid;
    425                 $url        = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
    426 
    427                 $size       = @getimagesize( $cropped );
    428                 $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
    429 
    430                 $object = array(
    431                         'ID'             => $parent_attachment_id,
    432                         'post_title'     => basename( $cropped ),
    433                         'post_content'   => $url,
    434                         'post_mime_type' => $image_type,
    435                         'guid'           => $url,
    436                         'context'        => 'site-icon'
    437                 );
    438 
    439                 return $object;
    440         }
    441 
    442         /**
    443          * Insert an attachment.
    444          *
    445          * @since 4.3.0
    446          *
    447          * @param array  $object Attachment object.
    448          * @param string $file   File path of the attached image.
    449          * @return int           Attachment ID
    450          */
    451         public function insert_attachment( $object, $file ) {
    452                 $attachment_id = wp_insert_attachment( $object, $file );
    453                 $this->update_attachment_metadata( $attachment_id, $file );
    454 
    455                 return $attachment_id;
    456         }
    457 
    458         /**
    459          * Update the metadata of an attachment.
    460          *
    461          * @since 4.3.0
    462          *
    463          * @param int    $attachment_id Attachment ID
    464          * @param string $file          File path of the attached image.
    465          */
    466         public function update_attachment_metadata( $attachment_id, $file ) {
    467                 $metadata = wp_generate_attachment_metadata( $attachment_id, $file );
    468 
    469                 /**
    470                  * Filter the site icon attachment metadata.
    471                  *
    472                  * @since 4.3.0
    473                  *
    474                  * @see wp_generate_attachment_metadata()
    475                  *
    476                  * @param array $metadata Attachment metadata.
    477                  */
    478                 $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata );
    479                 wp_update_attachment_metadata( $attachment_id, $metadata );
    480         }
    481 
    482         /**
    48366         * Add additional sizes to be made when creating the site_icon images.
    48467         *
    48568         * @since 4.3.0
     
    50588
    50689                // ensure that we only resize the image into
    50790                foreach ( $sizes as $name => $size_array ) {
    508                         if ( isset( $size_array['crop'] ) ) {
     91                        if ( $size_array['crop'] ) {
    50992                                $only_crop_sizes[ $name ] = $size_array;
    51093                        }
    51194                }
     
    523106                return $only_crop_sizes;
    524107        }
    525108
    526         /**
    527          * Add Site Icon sizes to the array of image sizes on demand.
    528          *
    529          * @since 4.3.0
    530          *
    531          * @param array $sizes
    532          * @return array
    533          */
    534         public function intermediate_image_sizes( $sizes = array() ) {
    535                 /** This filter is documented in wp-admin/includes/site-icon.php */
    536                 $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes );
    537                 foreach ( $this->site_icon_sizes as $size ) {
    538                         $sizes[] = 'site_icon-' . $size;
    539                 }
    540109
    541                 return $sizes;
    542         }
    543 
    544         /**
    545          * Deletes all additional image sizes, used for site icons.
    546          *
    547          * @since 4.3.0
    548          *
    549          * @return bool
    550          */
    551         public function delete_site_icon() {
    552                 // We add the filter to make sure that we also delete all the added images.
    553                 add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
    554                 wp_delete_attachment( get_option( 'site_icon' ), true );
    555                 remove_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
    556 
    557                 return delete_option( 'site_icon' );
    558         }
    559 
    560         /**
    561          * Deletes the Site Icon when the image file is deleted.
    562          *
    563          * @since 4.3.0
    564          *
    565          * @param int $post_id Attachment ID.
    566          */
    567         public function delete_attachment_data( $post_id ) {
    568                 $site_icon_id = get_option( 'site_icon' );
    569 
    570                 if ( $site_icon_id && $post_id == $site_icon_id ) {
    571                         delete_option( 'site_icon' );
    572                 }
    573         }
    574 
    575         /**
    576          * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon.
    577          *
    578          * @since 4.3.0
    579          *
    580          * @param null|array|string $value    The value get_metadata() should
    581          *                                    return - a single metadata value,
    582          *                                    or an array of values.
    583          * @param int               $post_id  Post ID.
    584          * @param string            $meta_key Meta key.
    585          * @param string|array      $single   Meta value, or an array of values.
    586          * @return array|null|string
    587          */
    588         public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
    589                 $site_icon_id = get_option( 'site_icon' );
    590 
    591                 if ( $post_id == $site_icon_id && '_wp_attachment_backup_sizes' == $meta_key && $single ) {
    592                         add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) );
    593                 }
    594 
    595                 return $value;
    596         }
    597 
    598         /**
    599          * Get the data required to work with the uploaded image
    600          *
    601          * @since 4.3.0
    602          *
    603          * @return array containing the collected data
    604          */
    605         private function get_upload_data() {
    606                 if ( isset( $_GET['file'] ) ) {
    607                         $attachment_id = absint( $_GET['file'] );
    608                         $file          = get_attached_file( $attachment_id, true );
    609                         $url           = wp_get_attachment_image_src( $attachment_id, 'full' );
    610                         $url           = $url[0];
    611                 } else {
    612                         $upload        = $this->handle_upload();
    613                         $attachment_id = $upload['attachment_id'];
    614                         $file          = $upload['file'];
    615                         $url           = $upload['url'];
    616                 }
    617 
    618                 $image_size = getimagesize( $file );
    619 
    620                 return array( $attachment_id, $url, $image_size );
    621         }
    622110}
    623 
    624 /**
    625  * @global WP_Site_Icon $wp_site_icon
    626  */
    627 $GLOBALS['wp_site_icon'] = new WP_Site_Icon;
  • wp-admin/options-general.php

     
    131131        $upload_url = admin_url( 'options-general.php?page=site-icon' );
    132132        $update_url = esc_url( add_query_arg( array(
    133133                'page'   => 'site-icon',
    134                 'action' => 'crop_site_icon',
     134                'action' => 'crop-site-icon',
    135135        ), wp_nonce_url( admin_url( 'options-general.php' ), 'crop-site-icon' ) ) );
    136136
    137137        wp_enqueue_media();
     
    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>
    149                 <button type="button" id="choose-from-library-link" class="button" data-size="<?php echo absint( $GLOBALS['wp_site_icon']->min_size ); ?>" data-update-link="<?php echo esc_attr( $update_url ); ?>" data-choose="<?php esc_attr_e( 'Choose a Site Icon' ); ?>" data-update="<?php esc_attr_e( 'Set as Site Icon' ); ?>"><?php _e( 'Update Site Icon' ); ?></button>
     149                <button type="button" id="choose-from-library-link" class="button" data-size="<?php echo absint( WP_Site_Icon::get_instance()->min_size ); ?>" data-update-link="<?php echo esc_attr( $update_url ); ?>" data-choose="<?php esc_attr_e( 'Choose a Site Icon' ); ?>" data-update="<?php esc_attr_e( 'Set as Site Icon' ); ?>"><?php _e( 'Update Site Icon' ); ?></button>
    150150                <a href="<?php echo esc_url( $remove_url ); ?>"><?php _e( 'Remove Site Icon' ); ?></a>
    151151        </p>
    152152        <p class="hide-if-js">
     
    158158
    159159        <p class="hide-if-no-js">
    160160                <label class="screen-reader-text" for="choose-from-library-link"><?php _e( 'Choose an image from your media library:' ); ?></label>
    161                 <button type="button" id="choose-from-library-link" class="button" data-size="<?php echo absint( $GLOBALS['wp_site_icon']->min_size ); ?>" data-update-link="<?php echo esc_attr( $update_url ); ?>" data-choose="<?php esc_attr_e( 'Choose a Site Icon' ); ?>" data-update="<?php esc_attr_e( 'Set as Site Icon' ); ?>"><?php _e( 'Choose Image' ); ?></button>
     161                <button type="button" id="choose-from-library-link" class="button" data-size="<?php echo absint( WP_Site_Icon::get_instance()->min_size ); ?>" data-update-link="<?php echo esc_attr( $update_url ); ?>" data-choose="<?php esc_attr_e( 'Choose a Site Icon' ); ?>" data-update="<?php esc_attr_e( 'Set as Site Icon' ); ?>"><?php _e( 'Choose Image' ); ?></button>
    162162        </p>
    163163        <a class="button hide-if-js" href="<?php echo esc_url( $upload_url ); ?>"><?php _e( 'Add a Site Icon' ); ?></a>
    164164