65 | | add_action( 'admin_action_set_site_icon', array( $this, 'set_site_icon' ) ); |
66 | | add_action( 'admin_action_remove_site_icon', array( $this, 'remove_site_icon' ) ); |
| 65 | // Because of the media upload the 'admin_action_crop_site_icon' is not called |
| 66 | add_action( 'load-settings_page_site-icon', array( $this, 'maybe_skip_cropping' ) ); |
| 67 | add_action( 'admin_action_set_site_icon', array( $this, 'set_site_icon' ) ); |
| 68 | add_action( 'admin_action_remove_site_icon', array( $this, 'remove_site_icon' ) ); |
| 69 | add_action( 'admin_action_site_icon_set', array( $this, 'site_icon_set' ) ); |
| 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() { |
| 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 | |
328 | | // TODO |
329 | | if ( empty( $_POST['skip-cropping'] ) ) { |
330 | | $crop_ratio = (float) $_POST['crop_ratio']; |
331 | | $crop_data = $this->convert_coordinates_from_resized_to_full( $_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio ); |
332 | | $cropped = wp_crop_image( $attachment_id, $crop_data['crop_x'], $crop_data['crop_y'], $crop_data['crop_width'], $crop_data['crop_height'], $this->min_size, $this->min_size ); |
333 | | } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { |
334 | | $cropped = _copy_image_file( $attachment_id ); |
| 383 | /* |
| 384 | * If the current attachment as been set as site icon don't delete it. |
| 385 | */ |
| 386 | if ( $current_site_icon_id != $attachment_id ) { |
| 387 | // Delete any existing site icon images. |
| 388 | $this->delete_site_icon(); |
| 389 | |
| 390 | if ( false === $skip_cropping ) { |
| 391 | $crop_ratio = (float) $_REQUEST['crop_ratio']; |
| 392 | $crop_data = $this->convert_coordinates_from_resized_to_full( |
| 393 | $_POST['crop-x'], |
| 394 | $_POST['crop-y'], |
| 395 | $_POST['crop-w'], |
| 396 | $_POST['crop-h'], |
| 397 | $crop_ratio |
| 398 | ); |
| 399 | $cropped = wp_crop_image( |
| 400 | $attachment_id, |
| 401 | $crop_data['crop_x'], |
| 402 | $crop_data['crop_y'], |
| 403 | $crop_data['crop_width'], |
| 404 | $crop_data['crop_height'], |
| 405 | $this->min_size, |
| 406 | $this->min_size |
| 407 | ); |
| 408 | } elseif ( $create_new_attachement ) { |
| 409 | $cropped = _copy_image_file( $attachment_id ); |
| 410 | } else { |
| 411 | $cropped = get_attached_file( $attachment_id ); |
| 412 | } |
| 413 | |
| 414 | if ( ! $cropped || is_wp_error( $cropped ) ) { |
| 415 | wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); |
| 416 | } |
| 417 | |
| 418 | $object = $this->create_attachment_object( $cropped, $attachment_id ); |
| 419 | |
| 420 | if ( $create_new_attachement ) { |
| 421 | unset( $object['ID'] ); |
| 422 | } |
| 423 | |
| 424 | // Update the attachment |
| 425 | add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| 426 | $attachment_id = $this->insert_attachment( $object, $cropped ); |
| 427 | remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| 428 | |
| 429 | // Save the site_icon data into option |
| 430 | update_option( 'site_icon', $attachment_id ); |
| 431 | |
343 | | $object = $this->create_attachment_object( $cropped, $attachment_id ); |
344 | | |
345 | | if ( ! empty( $_POST['create-new-attachment'] ) ) { |
346 | | unset( $object['ID'] ); |
| 437 | // Update meta-data and possibly regenerate intermediate sizes |
| 438 | add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| 439 | $this->update_attachment_metadata( $attachment_id, $image_url ); |
| 440 | remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
349 | | // Update the attachment |
350 | | add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
351 | | $attachment_id = $this->insert_attachment( $object, $cropped ); |
352 | | remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| 443 | // redirect to settings page |
| 444 | wp_redirect( |
| 445 | add_query_arg( |
| 446 | array('action' => 'site_icon_set'), |
| 447 | admin_url( 'options-general.php' ) |
| 448 | ) |
| 449 | ); |
| 450 | die(); |
| 601 | $updated = $this->update_attachment_metadata( $attachment_id, $image_url ); |
| 602 | if ( false !== $updated ) { |
| 603 | return $attachment_id; |
| 604 | } |
| 605 | |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * Update the metadata of an attachment |
| 611 | * |
| 612 | * @since 4.3.0 |
| 613 | * |
| 614 | * @param int $attachment_id Attachment ID |
| 615 | * @param string $image_url Cropped image URL. |
| 616 | * |
| 617 | * @return bool|int |
| 618 | */ |
| 619 | public function update_attachment_metadata( $attachment_id, $image_url ) { |
| 620 | $metadata = wp_generate_attachment_metadata( $attachment_id, $image_url ); |
| 621 | |