Ticket #16434: 16434.12.diff
File 16434.12.diff, 8.3 KB (added by , 9 years ago) |
---|
-
src/wp-admin/includes/class-wp-site-icon.php
62 62 63 63 add_action( 'admin_menu', array( $this, 'admin_menu_upload_site_icon' ) ); 64 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' ) );65 // Because of the media upload the 'admin_action_crop_site_icon' is not called 66 add_action( 'load-settings_page_site-icon', array( $this, 'maybe_skip_cropping' ) ); 67 67 68 add_action( 'admin_action_set_site_icon', array( $this, 'set_site_icon' ) ); 69 add_action( 'admin_action_remove_site_icon', array( $this, 'remove_site_icon' ) ); 70 68 71 add_action( 'delete_attachment', array( $this, 'delete_attachment_data' ), 10, 1 ); 69 72 add_filter( 'get_post_metadata', array( $this, 'get_post_metadata' ), 10, 4 ); 70 73 } … … 192 195 } 193 196 194 197 /** 195 * Crop a the image admin view. 198 * Check if the image needs cropping 199 * If it doesn't need cropping, proceed to set the icon 196 200 * 197 201 * @since 4.3.0 198 202 */ 199 public function crop_page() { 203 public function maybe_skip_cropping() { 204 if ( ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != 'crop_site_icon' ) { 205 return; 206 } 207 200 208 check_admin_referer( 'crop-site-icon' ); 201 209 210 $upload_data = $this->get_upload_data(); 211 $image_size = $upload_data['image_size']; 212 213 if ( $image_size[0] == $image_size[1] && $image_size[0] == $this->min_size ) { 214 // no cropping is required 215 216 $url = add_query_arg( 217 array( 218 'attachment_id' => $upload_data['attachment_id'], 219 'skip-cropping' => true, 220 'create-new-attachment' => true, 221 'action' => 'set_site_icon' 222 ), 223 wp_nonce_url( admin_url( 'options-general.php' ), 'set-site-icon' ) 224 ); 225 226 wp_redirect( $url ); 227 die(); 228 } 229 } 230 231 /** 232 * Get the data required to work with the uploaded image 233 * 234 * @since 4.3.0 235 * 236 * @return array containing the collected data 237 */ 238 public function get_upload_data() { 202 239 if ( isset( $_GET['file'] ) ) { 203 240 $attachment_id = absint( $_GET['file'] ); 204 241 $file = get_attached_file( $attachment_id, true ); … … 213 250 214 251 $image_size = getimagesize( $file ); 215 252 253 return compact('attachment_id', 'url', 'image_size'); 254 } 255 256 /** 257 * Crop a the image admin view. 258 * 259 * @since 4.3.0 260 */ 261 public function crop_page() { 262 check_admin_referer( 'crop-site-icon' ); 263 264 $upload_data = $this->get_upload_data(); 265 266 $image_size = $upload_data['image_size']; 267 $attachment_id = $upload_data['attachment_id']; 268 $url = $upload_data['url']; 269 216 270 if ( $image_size[0] < $this->min_size ) { 217 271 add_settings_error( 'site-icon', 'too-small', sprintf( __( 'The selected image is smaller than %upx in width.' ), $this->min_size ) ); 218 272 … … 320 374 public function set_site_icon() { 321 375 check_admin_referer( 'set-site-icon' ); 322 376 323 // Delete any existing site icon images.324 $ this->delete_site_icon();377 $attachment_id = absint( $_REQUEST['attachment_id'] ); 378 $current_site_icon_id = get_option( 'site_icon' ); 325 379 326 $attachment_id = absint( $_POST['attachment_id'] ); 380 $create_new_attachement = isset( $_REQUEST['create-new-attachment'] ) && $_REQUEST['create-new-attachment']; 381 $skip_cropping = ! isset( $_REQUEST['skip-cropping'] ) && ! $_REQUEST['skip-cropping']; 327 382 328 // TODO 329 if ( empty( $_POST['skip-cropping'] ) ) { 330 $crop_ratio = (float) $_POST['crop_ratio']; 331 $crop_data = $this->convert_coordinates_from_resized_to_full( $_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio ); 332 $cropped = wp_crop_image( $attachment_id, $crop_data['crop_x'], $crop_data['crop_y'], $crop_data['crop_width'], $crop_data['crop_height'], $this->min_size, $this->min_size ); 333 } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { 334 $cropped = _copy_image_file( $attachment_id ); 335 } else { 336 $cropped = get_attached_file( $attachment_id ); 337 } 383 /* 384 * If the current attachment as been set as site icon don't delete it. 385 */ 386 if ( $current_site_icon_id != $attachment_id ) { 387 // Delete any existing site icon images. 388 $this->delete_site_icon(); 338 389 339 if ( ! $cropped || is_wp_error( $cropped ) ) { 340 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); 341 } 390 if ( false === $skip_cropping ) { 391 $crop_ratio = (float) $_REQUEST['crop_ratio']; 392 $crop_data = $this->convert_coordinates_from_resized_to_full( 393 $_POST['crop-x'], 394 $_POST['crop-y'], 395 $_POST['crop-w'], 396 $_POST['crop-h'], 397 $crop_ratio 398 ); 399 $cropped = wp_crop_image( 400 $attachment_id, 401 $crop_data['crop_x'], 402 $crop_data['crop_y'], 403 $crop_data['crop_width'], 404 $crop_data['crop_height'], 405 $this->min_size, 406 $this->min_size 407 ); 408 } elseif ( $create_new_attachement ) { 409 $cropped = _copy_image_file( $attachment_id ); 410 } else { 411 $cropped = get_attached_file( $attachment_id ); 412 } 342 413 343 $object = $this->create_attachment_object( $cropped, $attachment_id ); 414 if ( ! $cropped || is_wp_error( $cropped ) ) { 415 wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); 416 } 344 417 345 if ( ! empty( $_POST['create-new-attachment'] ) ) { 346 unset( $object['ID'] ); 347 } 418 $object = $this->create_attachment_object( $cropped, $attachment_id ); 348 419 349 // Update the attachment 350 add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); 351 $attachment_id = $this->insert_attachment( $object, $cropped ); 352 remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); 420 if ( $create_new_attachement ) { 421 unset( $object['ID'] ); 422 } 353 423 354 // Save the site_icon data into option 355 update_option( 'site_icon', $attachment_id ); 424 // Update the attachment 425 add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); 426 $attachment_id = $this->insert_attachment( $object, $cropped ); 427 remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); 356 428 429 // Save the site_icon data into option 430 update_option( 'site_icon', $attachment_id ); 431 432 } else { 433 434 // Get the file path 435 $image_url = get_attached_file( $attachment_id ); 436 437 // Update meta-data and possibly regenerate intermediate sizes 438 add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); 439 $this->update_attachment_metadata( $attachment_id, $image_url ); 440 remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); 441 } 442 357 443 add_settings_error( 'site-icon', 'icon-updated', __( 'Site Icon updated.' ), 'updated' ); 358 444 } 359 445 … … 489 575 } 490 576 491 577 /** 492 * Insert an attachment and its metadata.578 * Insert an attachment 493 579 * 494 580 * @since 4.3.0 495 581 * 496 582 * @param array $object Attachment object. 497 * @param string $ croppedCropped image URL.583 * @param string $image_url Cropped image URL. 498 584 * @return int Attachment ID. 499 585 */ 500 public function insert_attachment( $object, $cropped ) { 501 $attachment_id = wp_insert_attachment( $object, $cropped ); 502 $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); 586 public function insert_attachment( $object, $image_url ) { 587 $attachment_id = wp_insert_attachment( $object, $image_url ); 503 588 589 $updated = $this->update_attachment_metadata( $attachment_id, $image_url ); 590 if ( false !== $updated ) { 591 return $attachment_id; 592 } 593 594 return false; 595 } 596 597 /** 598 * Update the metadata of an attachment 599 * 600 * @since 4.3.0 601 * 602 * @param int $attachment_id Attachment ID 603 * @param string $image_url Cropped image URL. 604 * 605 * @return bool|int 606 */ 607 public function update_attachment_metadata( $attachment_id, $image_url ) { 608 $metadata = wp_generate_attachment_metadata( $attachment_id, $image_url ); 609 504 610 /** 505 611 * Filter the site icon attachment metadata. 506 612 * … … 511 617 * @param array $metadata Attachment metadata. 512 618 */ 513 619 $metadata = apply_filters( 'site_icon_attachment_metadata', $metadata ); 514 wp_update_attachment_metadata( $attachment_id, $metadata );620 $updated = wp_update_attachment_metadata( $attachment_id, $metadata ); 515 621 516 return $ attachment_id;622 return $updated; 517 623 } 518 624 519 625 /**