| 200 | | if ( isset( $_GET['file'] ) ) { |
| 201 | | $attachment_id = absint( $_GET['file'] ); |
| 202 | | $file = get_attached_file( $attachment_id, true ); |
| 203 | | $url = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| 204 | | $url = $url[0]; |
| 205 | | } else { |
| 206 | | $upload = $this->handle_upload(); |
| 207 | | $attachment_id = $upload['attachment_id']; |
| 208 | | $file = $upload['file']; |
| 209 | | $url = $upload['url']; |
| | 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 | 'action' => 'set_site_icon', |
| | 216 | ), wp_nonce_url( admin_url( 'options-general.php' ), 'set-site-icon' ) ); |
| | 217 | |
| | 218 | wp_safe_redirect( $url ); |
| | 219 | die(); |
| 324 | | $attachment_id = absint( $_POST['attachment_id'] ); |
| | 343 | /* |
| | 344 | * If the current attachment as been set as site icon don't delete it. |
| | 345 | */ |
| | 346 | if ( get_option( 'site_icon' ) == $attachment_id ) { |
| | 347 | // Get the file path. |
| | 348 | $image_url = get_attached_file( $attachment_id ); |
| | 349 | |
| | 350 | // Update meta data and possibly regenerate intermediate sizes. |
| | 351 | add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| | 352 | $this->update_attachment_metadata( $attachment_id, $image_url ); |
| | 353 | remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| 326 | | // TODO |
| 327 | | if ( empty( $_POST['skip-cropping'] ) ) { |
| 328 | | $crop_ratio = (float) $_POST['crop_ratio']; |
| 329 | | $crop_data = $this->convert_coordinates_from_resized_to_full( $_POST['crop-x'], $_POST['crop-y'], $_POST['crop-w'], $_POST['crop-h'], $crop_ratio ); |
| 330 | | $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 ); |
| 331 | | } elseif ( ! empty( $_POST['create-new-attachment'] ) ) { |
| 332 | | $cropped = _copy_image_file( $attachment_id ); |
| 337 | | if ( ! $cropped || is_wp_error( $cropped ) ) { |
| 338 | | wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) ); |
| 339 | | } |
| | 359 | if ( empty( $_REQUEST['skip-cropping'] ) ) { |
| | 360 | $crop_data = $this->convert_coordinates_from_resized_to_full( $_REQUEST['crop-x'], $_REQUEST['crop-y'], $_REQUEST['crop-w'], $_REQUEST['crop-h'], (float) $_REQUEST['crop_ratio'] ); |
| | 361 | $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 ); |
| 352 | | // Save the site_icon data into option |
| 353 | | update_option( 'site_icon', $attachment_id ); |
| | 380 | // Update the attachment. |
| | 381 | add_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| | 382 | $attachment_id = $this->insert_attachment( $object, $cropped ); |
| | 383 | remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'additional_sizes' ) ); |
| | 384 | |
| | 385 | // Save the site_icon data into option |
| | 386 | update_option( 'site_icon', $attachment_id ); |
| | 387 | } |
| 498 | | public function insert_attachment( $object, $cropped ) { |
| 499 | | $attachment_id = wp_insert_attachment( $object, $cropped ); |
| 500 | | $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); |
| | 532 | public function insert_attachment( $object, $file ) { |
| | 533 | $attachment_id = wp_insert_attachment( $object, $file ); |
| | 534 | $this->update_attachment_metadata( $attachment_id, $file ); |
| | 535 | |
| | 536 | return $attachment_id; |
| | 537 | } |
| | 538 | |
| | 539 | /** |
| | 540 | * Update the metadata of an attachment |
| | 541 | * |
| | 542 | * @since 4.3.0 |
| | 543 | * |
| | 544 | * @param int $attachment_id Attachment ID |
| | 545 | * @param string $file Filepath of the Attached image. |
| | 546 | */ |
| | 547 | public function update_attachment_metadata( $attachment_id, $file ) { |
| | 548 | $metadata = wp_generate_attachment_metadata( $attachment_id, $file ); |
| | 678 | |
| | 679 | /** |
| | 680 | * Get the data required to work with the uploaded image |
| | 681 | * |
| | 682 | * @since 4.3.0 |
| | 683 | * |
| | 684 | * @return array containing the collected data |
| | 685 | */ |
| | 686 | private function get_upload_data() { |
| | 687 | if ( isset( $_GET['file'] ) ) { |
| | 688 | $attachment_id = absint( $_GET['file'] ); |
| | 689 | $file = get_attached_file( $attachment_id, true ); |
| | 690 | $url = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| | 691 | $url = $url[0]; |
| | 692 | } else { |
| | 693 | $upload = $this->handle_upload(); |
| | 694 | $attachment_id = $upload['attachment_id']; |
| | 695 | $file = $upload['file']; |
| | 696 | $url = $upload['url']; |
| | 697 | } |
| | 698 | |
| | 699 | $image_size = getimagesize( $file ); |
| | 700 | |
| | 701 | return array( $attachment_id, $url, $image_size ); |
| | 702 | } |