| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class WP_Site_Icon. |
| 5 | * |
| 6 | * @since 4.3.0 |
| 7 | */ |
| 8 | class WP_Site_Icon { |
| 9 | |
| 10 | /** |
| 11 | * The minimum size of the site icon. |
| 12 | * |
| 13 | * @since 4.3.0 |
| 14 | * |
| 15 | * @var int |
| 16 | */ |
| 17 | public $min_size = 512; |
| 18 | |
| 19 | /** |
| 20 | * The size to which to crop the image so that we can display it in the UI nicely. |
| 21 | * |
| 22 | * @since 4.3.0 |
| 23 | * |
| 24 | * @var int |
| 25 | */ |
| 26 | public $page_crop = 512; |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @since 4.3.0 |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | public $site_icon_sizes = array( |
| 35 | /** |
| 36 | * Square, medium sized tiles for IE11+. |
| 37 | * |
| 38 | * @link https://msdn.microsoft.com/library/dn455106(v=vs.85).aspx |
| 39 | */ |
| 40 | 270, |
| 41 | |
| 42 | /** |
| 43 | * App icons up to iPhone 6 Plus. |
| 44 | * |
| 45 | * @link https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html |
| 46 | */ |
| 47 | 180, |
| 48 | |
| 49 | // Our regular Favicon. |
| 50 | 32, |
| 51 | ); |
| 52 | |
| 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_head', 'wp_site_icon' ); |
| 62 | |
| 63 | add_action( 'admin_menu', array( $this, 'admin_menu_upload_site_icon' ) ); |
| 64 | |
| 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' ) ); |
| 67 | |
| 68 | add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ), 10, 1 ); |
| 69 | add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add a hidden upload page. |
| 74 | * |
| 75 | * There is no need to access it directly. |
| 76 | * |
| 77 | * @since 4.3.0 |
| 78 | */ |
| 79 | public function admin_menu_upload_site_icon() { |
| 80 | $hook = add_submenu_page( null, __( 'Site Icon' ), __( 'Site Icon' ), 'manage_options', 'site-icon', array( $this, 'upload_site_icon_page' ) ); |
| 81 | |
| 82 | add_action( "load-$hook", array( $this, 'add_upload_settings' ) ); |
| 83 | add_action( "admin_print_scripts-$hook", array( $this, 'enqueue_scripts' ) ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Add scripts to admin settings pages. |
| 88 | * |
| 89 | * @since 4.3.0 |
| 90 | */ |
| 91 | public function enqueue_scripts() { |
| 92 | wp_enqueue_style( 'jcrop' ); |
| 93 | wp_enqueue_script( 'site-icon-crop' ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Load on when the admin is initialized. |
| 98 | * |
| 99 | * @since 4.3.0 |
| 100 | */ |
| 101 | public function add_upload_settings() { |
| 102 | add_settings_section( 'site-icon-upload', false, false, 'site-icon-upload' ); |
| 103 | add_settings_field( 'site-icon-upload', __( 'Upload Image' ), array( $this, 'upload_field' ), 'site-icon-upload', 'site-icon-upload', array( |
| 104 | 'label_for' => 'site-icon-upload', |
| 105 | ) ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Removes site icon. |
| 110 | * |
| 111 | * @since 4.3.0 |
| 112 | */ |
| 113 | public function remove_site_icon() { |
| 114 | check_admin_referer( 'remove-site-icon' ); |
| 115 | |
| 116 | $this->delete_site_icon(); |
| 117 | |
| 118 | add_settings_error( 'site-icon', 'icon-removed', __( 'Site Icon removed.' ), 'updated' ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Uploading a site_icon is a 3 step process |
| 123 | * |
| 124 | * 1. Select the file to upload |
| 125 | * 2. Crop the file |
| 126 | * 3. Confirmation |
| 127 | * |
| 128 | * @since 4.3.0 |
| 129 | */ |
| 130 | public function upload_site_icon_page() { |
| 131 | $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'select_site_icon'; |
| 132 | |
| 133 | switch ( $action ) { |
| 134 | case 'select_site_icon': |
| 135 | $this->select_page(); |
| 136 | break; |
| 137 | |
| 138 | case 'crop_site_icon': |
| 139 | $this->crop_page(); |
| 140 | break; |
| 141 | |
| 142 | default: |
| 143 | wp_safe_redirect( admin_url( 'options-general.php#site-icon' ) ); |
| 144 | exit; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Displays the site_icon form to upload the image. |
| 150 | * |
| 151 | * @since 4.3.0 |
| 152 | */ |
| 153 | public function select_page() { |
| 154 | ?> |
| 155 | <div class="wrap"> |
| 156 | <h2><?php _e( 'Add Site Icon' ); ?></h2> |
| 157 | <?php settings_errors( 'site-icon' ); ?> |
| 158 | <?php do_settings_sections( 'site-icon-upload' ); ?> |
| 159 | </div> |
| 160 | <?php |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Settings field for file upload. |
| 165 | * |
| 166 | * @since 4.3.0 |
| 167 | */ |
| 168 | public function upload_field() { |
| 169 | wp_enqueue_media(); |
| 170 | wp_enqueue_script( 'site-icon' ); |
| 171 | wp_dequeue_script( 'site-icon-crop' ); |
| 172 | |
| 173 | $update_url = esc_url( add_query_arg( array( |
| 174 | 'page' => 'site-icon', |
| 175 | 'action' => 'crop_site_icon', |
| 176 | ), wp_nonce_url( admin_url( 'options-general.php' ), 'crop-site-icon' ) ) ); |
| 177 | ?> |
| 178 | <p class="hide-if-no-js"> |
| 179 | <label class="screen-reader-text" for="choose-from-library-link"><?php _e( 'Choose an image from your media library:' ); ?></label> |
| 180 | <button type="button" id="choose-from-library-link" class="button" 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> |
| 181 | </p> |
| 182 | <form class="hide-if-js" action="<?php echo esc_url( admin_url( 'options-general.php?page=site-icon' ) ); ?>" method="post" enctype="multipart/form-data"> |
| 183 | <input name="action" value="crop_site_icon" type="hidden" /> |
| 184 | <input name="site-icon" type="file" /> |
| 185 | <input name="submit" value="<?php esc_attr_e( 'Upload Image' ); ?>" type="submit" class="button button-primary" /> |
| 186 | <p class="description"> |
| 187 | <?php printf( __( 'The image needs to be exactly %spx in both width and height.' ), "<strong>$this->min_size</strong>" ); ?> |
| 188 | </p> |
| 189 | <?php wp_nonce_field( 'crop-site-icon' ); ?> |
| 190 | </form> |
| 191 | <?php |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Crop a the image admin view. |
| 196 | * |
| 197 | * @since 4.3.0 |
| 198 | */ |
| 199 | public function crop_page() { |
| 200 | check_admin_referer( 'crop-site-icon' ); |
| 201 | |
| 202 | if ( isset( $_GET['file'] ) ) { |
| 203 | $attachment_id = absint( $_GET['file'] ); |
| 204 | $file = get_attached_file( $attachment_id, true ); |
| 205 | $url = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| 206 | $url = $url[0]; |
| 207 | } else { |
| 208 | $upload = $this->handle_upload(); |
| 209 | $attachment_id = $upload['attachment_id']; |
| 210 | $file = $upload['file']; |
| 211 | $url = $upload['url']; |
| 212 | } |
| 213 | |
| 214 | $image_size = getimagesize( $file ); |
| 215 | |
| 216 | if ( $image_size[0] < $this->min_size ) { |
| 217 | add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->min_size ) ); |
| 218 | |
| 219 | // back to step one |
| 220 | $this->select_page(); |
| 221 | |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | if ( $image_size[1] < $this->min_size ) { |
| 226 | add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in height.' ), $this->min_size ) ); |
| 227 | |
| 228 | // Back to step one. |
| 229 | $this->select_page(); |
| 230 | |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | // Let's resize the image so that the user can easier crop a image that in the admin view. |
| 235 | $cropped = wp_crop_image( $attachment_id, 0, 0, 0, 0, $this->page_crop, 0 ); |
| 236 | if ( ! $cropped || is_wp_error( $cropped ) ) { |
| 237 | wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); |
| 238 | } |
| 239 | $cropped_size = getimagesize( $cropped ); |
| 240 | $crop_ratio = $image_size[0] / $cropped_size[0]; |
| 241 | wp_delete_file( $cropped ); |
| 242 | |
| 243 | wp_localize_script( 'site-icon-crop', 'wpSiteIconCropData', $this->initial_crop_data( $crop_ratio, $cropped_size ) ); |
| 244 | ?> |
| 245 | |
| 246 | <div class="wrap"> |
| 247 | <h2 class="site-icon-title"><?php esc_html_e( 'Site Icon' ); ?></h2> |
| 248 | <?php settings_errors( 'site-icon' ); ?> |
| 249 | |
| 250 | <div class="site-icon-crop-shell"> |
| 251 | <form action="options-general.php" method="post" enctype="multipart/form-data"> |
| 252 | <p class="hide-if-no-js description"><?php _e('Choose the part of the image you want to use as your site icon.'); ?></p> |
| 253 | <p class="hide-if-js description"><strong><?php _e( 'You need Javascript to choose a part of the image.'); ?></strong></p> |
| 254 | |
| 255 | <div class="site-icon-crop-preview-shell"> |
| 256 | <h3><?php esc_html_e( 'Preview' ); ?></h3> |
| 257 | <strong><?php esc_html_e( 'As your favicon' ); ?></strong> |
| 258 | <div class="site-icon-crop-favicon-preview-shell"> |
| 259 | <img src="images/browser.png" class="site-icon-browser-preview" width="172" height="79" alt="<?php esc_attr_e( 'Browser Chrome' ); ?>"/> |
| 260 | |
| 261 | <div class="site-icon-crop-preview-favicon"> |
| 262 | <img src="<?php echo esc_url( $url ); ?>" id="preview-favicon" alt="<?php esc_attr_e( 'Preview Favicon' ); ?>"/> |
| 263 | </div> |
| 264 | <span class="site-icon-browser-title"><?php bloginfo( 'name' ); ?></span> |
| 265 | </div> |
| 266 | |
| 267 | <strong><?php esc_html_e( 'As a mobile icon' ); ?></strong> |
| 268 | <div class="site-icon-crop-preview-homeicon"> |
| 269 | <img src="<?php echo esc_url( $url ); ?>" id="preview-homeicon" alt="<?php esc_attr_e( 'Preview Home Icon' ); ?>"/> |
| 270 | </div> |
| 271 | </div> |
| 272 | <img src="<?php echo esc_url( $url ); ?>" id="crop-image" class="site-icon-crop-image" width="<?php echo esc_attr( $cropped_size[0] ); ?>" height="<?php echo esc_attr( $cropped_size[1] ); ?>" alt="<?php esc_attr_e( 'Image to be cropped' ); ?>"/> |
| 273 | |
| 274 | <input type="hidden" id="crop-x" name="crop-x" /> |
| 275 | <input type="hidden" id="crop-y" name="crop-y" /> |
| 276 | <input type="hidden" id="crop-width" name="crop-w" /> |
| 277 | <input type="hidden" id="crop-height" name="crop-h" /> |
| 278 | |
| 279 | <input type="hidden" name="action" value="set_site_icon" /> |
| 280 | <input type="hidden" name="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" /> |
| 281 | <input type="hidden" name="crop_ratio" value="<?php echo esc_attr( $crop_ratio ); ?>" /> |
| 282 | <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) : ?> |
| 283 | <input type="hidden" name="create-new-attachment" value="true" /> |
| 284 | <?php endif; ?> |
| 285 | <?php wp_nonce_field( 'set-site-icon' ); ?> |
| 286 | |
| 287 | <p class="submit"> |
| 288 | <?php submit_button( __( 'Crop and Publish' ), 'primary', 'submit', false ); ?> |
| 289 | <a class="button secondary" href="options-general.php"><?php _e( 'Cancel' ); ?></a> |
| 290 | </p> |
| 291 | </form> |
| 292 | </div> |
| 293 | </div> |
| 294 | <?php |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Saves a new Site Icon. |
| 299 | * |
| 300 | * @since 4.3.0 |
| 301 | */ |
| 302 | public function set_site_icon() { |
| 303 | check_admin_referer( 'set-site-icon' ); |
| 304 | |
| 305 | // Delete any existing site icon images. |
| 306 | $this->delete_site_icon(); |
| 307 | |
| 308 | $attachment_id = absint( $_POST['attachment_id'] ); |
| 309 | |
| 310 | $crop_ratio = (float) $_POST['crop_ratio']; |
| 311 | $crop_data = $this->convert_coordinates_from_resized_to_full( $_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio ); |
| 312 | |
| 313 | // TODO |
| 314 | if ( empty( $_POST['skip-cropping'] ) ) { |
| 315 | $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 ); |
| 316 | } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { |
| 317 | $cropped = _copy_image_file( $attachment_id ); |
| 318 | } else { |
| 319 | $cropped = get_attached_file( $attachment_id ); |
| 320 | } |
| 321 | |
| 322 | if ( ! $cropped || is_wp_error( $cropped ) ) { |
| 323 | wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); |
| 324 | } |
| 325 | |
| 326 | $object = $this->create_attachment_object( $cropped, $attachment_id ); |
| 327 | |
| 328 | if ( ! empty( $_POST['create-new-attachment'] ) ) { |
| 329 | unset( $object['ID'] ); |
| 330 | } |
| 331 | |
| 332 | // Update the attachment |
| 333 | add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| 334 | $attachment_id = $this->insert_attachment( $object, $cropped ); |
| 335 | remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| 336 | |
| 337 | // Save the site_icon data into option |
| 338 | update_option( 'site_icon', $attachment_id ); |
| 339 | |
| 340 | add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' ); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * This function is used to pass data to the localize script |
| 345 | * so that we can center the cropper and also set the minimum |
| 346 | * cropper if we still want to show the |
| 347 | * |
| 348 | * @since 4.3.0 |
| 349 | * |
| 350 | * @param float $ratio |
| 351 | * @param array $cropped_size |
| 352 | * @return array |
| 353 | */ |
| 354 | public function initial_crop_data( $ratio, $cropped_size ) { |
| 355 | $init_x = $init_y = $init_size = 0; |
| 356 | |
| 357 | $min_crop_size = ( $this->min_size / $ratio ); |
| 358 | $resized_width = $cropped_size[0]; |
| 359 | $resized_height = $cropped_size[1]; |
| 360 | |
| 361 | // Landscape format ( width > height ) |
| 362 | if ( $resized_width > $resized_height ) { |
| 363 | $init_x = ( $this->page_crop - $resized_height ) / 2; |
| 364 | $init_size = $resized_height; |
| 365 | } |
| 366 | |
| 367 | // Portrait format ( height > width ) |
| 368 | if ( $resized_width < $resized_height ) { |
| 369 | $init_y = ( $this->page_crop - $resized_width ) / 2; |
| 370 | $init_size = $resized_height; |
| 371 | } |
| 372 | |
| 373 | // Square height == width |
| 374 | if ( $resized_width = $resized_height ) { |
| 375 | $init_size = $resized_height; |
| 376 | } |
| 377 | |
| 378 | return array( |
| 379 | 'init_x' => $init_x, |
| 380 | 'init_y' => $init_y, |
| 381 | 'init_size' => $init_size, |
| 382 | 'min_size' => $min_crop_size, |
| 383 | ); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Converts the coordinates from the downsized image to the original image for accurate cropping. |
| 388 | * |
| 389 | * @since 4.3.0 |
| 390 | * |
| 391 | * @param int $crop_x |
| 392 | * @param int $crop_y |
| 393 | * @param int $crop_width |
| 394 | * @param int $crop_height |
| 395 | * @param float $ratio |
| 396 | * @return array |
| 397 | */ |
| 398 | public function convert_coordinates_from_resized_to_full( $crop_x, $crop_y, $crop_width, $crop_height, $ratio ) { |
| 399 | return array( |
| 400 | 'crop_x' => floor( $crop_x * $ratio ), |
| 401 | 'crop_y' => floor( $crop_y * $ratio ), |
| 402 | 'crop_width' => floor( $crop_width * $ratio ), |
| 403 | 'crop_height' => floor( $crop_height * $ratio ), |
| 404 | ); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Upload the file to be cropped in the second step. |
| 409 | * |
| 410 | * @since 4.3.0 |
| 411 | */ |
| 412 | public function handle_upload() { |
| 413 | $uploaded_file = $_FILES['site-icon']; |
| 414 | $file_type = wp_check_filetype_and_ext( $uploaded_file['tmp_name'], $uploaded_file['name'] ); |
| 415 | if ( ! wp_match_mime_types( 'image', $file_type['type'] ) ) { |
| 416 | wp_die( __( 'The uploaded file is not a valid image. Please try again.' ) ); |
| 417 | } |
| 418 | |
| 419 | $file = wp_handle_upload( $uploaded_file, array( 'test_form' => false ) ); |
| 420 | |
| 421 | if ( isset( $file['error'] ) ) { |
| 422 | wp_die( $file['error'], __( 'Image Upload Error' ) ); |
| 423 | } |
| 424 | |
| 425 | $url = $file['url']; |
| 426 | $type = $file['type']; |
| 427 | $file = $file['file']; |
| 428 | $filename = basename( $file ); |
| 429 | |
| 430 | // Construct the object array |
| 431 | $object = array( |
| 432 | 'post_title' => $filename, |
| 433 | 'post_content' => $url, |
| 434 | 'post_mime_type' => $type, |
| 435 | 'guid' => $url, |
| 436 | 'context' => 'site-icon', |
| 437 | ); |
| 438 | |
| 439 | // Save the data |
| 440 | $attachment_id = wp_insert_attachment( $object, $file ); |
| 441 | |
| 442 | return compact( 'attachment_id', 'file', 'filename', 'url', 'type' ); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Create an attachment 'object'. |
| 447 | * |
| 448 | * @since 4.3.0 |
| 449 | * |
| 450 | * @param string $cropped Cropped image URL. |
| 451 | * @param int $parent_attachment_id Attachment ID of parent image. |
| 452 | * @return array Attachment object. |
| 453 | */ |
| 454 | public function create_attachment_object( $cropped, $parent_attachment_id ) { |
| 455 | $parent = get_post( $parent_attachment_id ); |
| 456 | $parent_url = $parent->guid; |
| 457 | $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); |
| 458 | |
| 459 | $size = @getimagesize( $cropped ); |
| 460 | $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; |
| 461 | |
| 462 | $object = array( |
| 463 | 'ID' => $parent_attachment_id, |
| 464 | 'post_title' => basename( $cropped ), |
| 465 | 'post_content' => $url, |
| 466 | 'post_mime_type' => $image_type, |
| 467 | 'guid' => $url, |
| 468 | 'context' => 'site-icon' |
| 469 | ); |
| 470 | |
| 471 | return $object; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Insert an attachment and its metadata. |
| 476 | * |
| 477 | * @since 4.3.0 |
| 478 | * |
| 479 | * @param array $object Attachment object. |
| 480 | * @param string $cropped Cropped image URL. |
| 481 | * @return int Attachment ID. |
| 482 | */ |
| 483 | public function insert_attachment( $object, $cropped ) { |
| 484 | $attachment_id = wp_insert_attachment( $object, $cropped ); |
| 485 | $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); |
| 486 | |
| 487 | /** |
| 488 | * Filter the site icon attachment metadata. |
| 489 | * |
| 490 | * @since 4.3.0 |
| 491 | * |
| 492 | * @see wp_generate_attachment_metadata() |
| 493 | * |
| 494 | * @param array $metadata Attachment metadata. |
| 495 | */ |
| 496 | $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata ); |
| 497 | wp_update_attachment_metadata( $attachment_id, $metadata ); |
| 498 | |
| 499 | return $attachment_id; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Add additional sizes to be made when creating the site_icon images. |
| 504 | * |
| 505 | * @since 4.3.0 |
| 506 | * |
| 507 | * @param array $sizes |
| 508 | * @return array |
| 509 | */ |
| 510 | public function additional_sizes( $sizes = array() ) { |
| 511 | $only_crop_sizes = array(); |
| 512 | |
| 513 | /** |
| 514 | * Filters the different dimensions that a site icon is saved in. |
| 515 | * |
| 516 | * @since 4.3.0 |
| 517 | * |
| 518 | * @param array $site_icon_sizes Sizes available for the Site Icon. |
| 519 | */ |
| 520 | $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes ); |
| 521 | // use a natural sort of numbers |
| 522 | natsort( $this->site_icon_sizes ); |
| 523 | $this->site_icon_sizes = array_reverse( $this->site_icon_sizes ); |
| 524 | |
| 525 | // ensure that we only resize the image into |
| 526 | foreach ( $sizes as $name => $size_array ) { |
| 527 | if ( $size_array['crop'] ) { |
| 528 | $only_crop_sizes[ $name ] = $size_array; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | foreach ( $this->site_icon_sizes as $size ) { |
| 533 | if ( $size < $this->min_size ) { |
| 534 | $only_crop_sizes[ 'site_icon-' . $size ] = array( |
| 535 | 'width ' => $size, |
| 536 | 'height' => $size, |
| 537 | 'crop' => true, |
| 538 | ); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return $only_crop_sizes; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Add Site Icon sizes to the array of image sizes on demand. |
| 547 | * |
| 548 | * @since 4.3.0 |
| 549 | * |
| 550 | * @param array $sizes |
| 551 | * @return array |
| 552 | */ |
| 553 | public function intermediate_image_sizes( $sizes = array() ) { |
| 554 | /** This filter is documented in wp-admin/site-icon.php */ |
| 555 | $this->site_icon_sizes = apply_filters( 'site_icon_image_sizes', $this->site_icon_sizes ); |
| 556 | foreach ( $this->site_icon_sizes as $size ) { |
| 557 | $sizes[] = 'site_icon-' . $size; |
| 558 | } |
| 559 | |
| 560 | return $sizes; |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Deletes all additional image sizes, used for site icons. |
| 565 | * |
| 566 | * @since 4.3.0 |
| 567 | * |
| 568 | * @return bool |
| 569 | */ |
| 570 | public function delete_site_icon() { |
| 571 | // We add the filter to make sure that we also delete all the added images |
| 572 | add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) ); |
| 573 | wp_delete_attachment( get_option( 'site_icon' ), true ); |
| 574 | remove_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) ); |
| 575 | |
| 576 | return delete_option( 'site_icon' ); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Deletes the Site Icon when the image file is deleted. |
| 581 | * |
| 582 | * @since 4.3.0 |
| 583 | * |
| 584 | * @param int $post_id Attachment ID. |
| 585 | */ |
| 586 | public function delete_attachment_data( $post_id ) { |
| 587 | // The user could be deleting the site_icon image |
| 588 | $site_icon_id = get_option( 'site_icon' ); |
| 589 | |
| 590 | if ( $site_icon_id && $post_id == $site_icon_id ) { |
| 591 | delete_option( 'site_icon' ); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Adds custom image sizes when meta data for an image is requested, that happens to be used as Site Icon. |
| 597 | * |
| 598 | * @since 4.3.0 |
| 599 | * |
| 600 | * @param null|array|string $value The value get_metadata() should |
| 601 | * return - a single metadata value, |
| 602 | * or an array of values. |
| 603 | * @param int $post_id Post ID. |
| 604 | * @param string $meta_key Meta key. |
| 605 | * @param string|array $single Meta value, or an array of values. |
| 606 | * @return array|null|string |
| 607 | */ |
| 608 | public function get_post_metadata( $value, $post_id, $meta_key, $single ) { |
| 609 | $site_icon_id = get_option( 'site_icon' ); |
| 610 | |
| 611 | if ( $post_id == $site_icon_id && '_wp_attachment_backup_sizes' == $meta_key && $single ) { |
| 612 | add_filter( 'intermediate_image_sizes', array( $this, 'intermediate_image_sizes' ) ); |
| 613 | } |
| 614 | |
| 615 | return $value; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | $GLOBALS['wp_site_icon'] = new WP_Site_Icon; |