6 * @subpackage Administration |
6 * @subpackage Administration |
7 */ |
7 */ |
8 |
8 |
9 /** |
9 /** |
10 * Loads the WP image-editing interface. |
10 * Loads the WP image-editing interface. |
|
11 * |
|
12 * @since 2.9.0 |
11 * |
13 * |
12 * @param int $post_id Post ID. |
14 * @param int $post_id Post ID. |
13 * @param bool|object $msg Optional. Message to display for image editor updates or errors. |
15 * @param bool|object $msg Optional. Message to display for image editor updates or errors. |
14 * Default false. |
16 * Default false. |
15 */ |
17 */ |
16 function wp_image_editor($post_id, $msg = false) { |
18 function wp_image_editor( $post_id, $msg = false ) { |
17 $nonce = wp_create_nonce("image_editor-$post_id"); |
19 $nonce = wp_create_nonce( "image_editor-$post_id" ); |
18 $meta = wp_get_attachment_metadata($post_id); |
20 $meta = wp_get_attachment_metadata( $post_id ); |
19 $thumb = image_get_intermediate_size($post_id, 'thumbnail'); |
21 $thumb = image_get_intermediate_size( $post_id, 'thumbnail' ); |
20 $sub_sizes = isset($meta['sizes']) && is_array($meta['sizes']); |
22 $sub_sizes = isset( $meta['sizes'] ) && is_array( $meta['sizes'] ); |
21 $note = ''; |
23 $note = ''; |
22 |
24 |
23 if ( isset( $meta['width'], $meta['height'] ) ) |
25 if ( isset( $meta['width'], $meta['height'] ) ) { |
24 $big = max( $meta['width'], $meta['height'] ); |
26 $big = max( $meta['width'], $meta['height'] ); |
25 else |
27 } else { |
26 die( __('Image data does not exist. Please re-upload the image.') ); |
28 die( __( 'Image data does not exist. Please re-upload the image.' ) ); |
|
29 } |
27 |
30 |
28 $sizer = $big > 400 ? 400 / $big : 1; |
31 $sizer = $big > 400 ? 400 / $big : 1; |
29 |
32 |
30 $backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true ); |
33 $backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true ); |
31 $can_restore = false; |
34 $can_restore = false; |
32 if ( ! empty( $backup_sizes ) && isset( $backup_sizes['full-orig'], $meta['file'] ) ) |
35 if ( ! empty( $backup_sizes ) && isset( $backup_sizes['full-orig'], $meta['file'] ) ) { |
33 $can_restore = $backup_sizes['full-orig']['file'] != basename( $meta['file'] ); |
36 $can_restore = $backup_sizes['full-orig']['file'] != wp_basename( $meta['file'] ); |
|
37 } |
34 |
38 |
35 if ( $msg ) { |
39 if ( $msg ) { |
36 if ( isset($msg->error) ) |
40 if ( isset( $msg->error ) ) { |
37 $note = "<div class='error'><p>$msg->error</p></div>"; |
41 $note = "<div class='error'><p>$msg->error</p></div>"; |
38 elseif ( isset($msg->msg) ) |
42 } elseif ( isset( $msg->msg ) ) { |
39 $note = "<div class='updated'><p>$msg->msg</p></div>"; |
43 $note = "<div class='updated'><p>$msg->msg</p></div>"; |
|
44 } |
40 } |
45 } |
41 |
46 |
42 ?> |
47 ?> |
43 <div class="imgedit-wrap wp-clearfix"> |
48 <div class="imgedit-wrap wp-clearfix"> |
44 <div id="imgedit-panel-<?php echo $post_id; ?>"> |
49 <div id="imgedit-panel-<?php echo $post_id; ?>"> |
66 <label><span class="screen-reader-text"><?php _e( 'scale height' ); ?></span> |
71 <label><span class="screen-reader-text"><?php _e( 'scale height' ); ?></span> |
67 <input type="text" id="imgedit-scale-height-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0, this)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0, this)" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" /> |
72 <input type="text" id="imgedit-scale-height-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0, this)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0, this)" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" /> |
68 </label> |
73 </label> |
69 <span class="imgedit-scale-warn" id="imgedit-scale-warn-<?php echo $post_id; ?>">!</span> |
74 <span class="imgedit-scale-warn" id="imgedit-scale-warn-<?php echo $post_id; ?>">!</span> |
70 <input id="imgedit-scale-button" type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'scale')" class="button button-primary" value="<?php esc_attr_e( 'Scale' ); ?>" /> |
75 <input id="imgedit-scale-button" type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'scale')" class="button button-primary" value="<?php esc_attr_e( 'Scale' ); ?>" /> |
71 </div> |
76 </div> |
72 </fieldset> |
77 </fieldset> |
73 |
78 |
74 </div> |
79 </div> |
75 </div> |
80 </div> |
76 </div> |
81 </div> |
77 |
82 |
78 <?php if ( $can_restore ) { ?> |
83 <?php if ( $can_restore ) { ?> |
79 |
84 |
80 <div class="imgedit-group"> |
85 <div class="imgedit-group"> |
81 <div class="imgedit-group-top"> |
86 <div class="imgedit-group-top"> |
82 <h2><button type="button" onclick="imageEdit.toggleHelp(this);" class="button-link"><?php _e( 'Restore Original Image' ); ?> <span class="dashicons dashicons-arrow-down imgedit-help-toggle"></span></button></h2> |
87 <h2><button type="button" onclick="imageEdit.toggleHelp(this);" class="button-link"><?php _e( 'Restore Original Image' ); ?> <span class="dashicons dashicons-arrow-down imgedit-help-toggle"></span></button></h2> |
83 <div class="imgedit-help"> |
88 <div class="imgedit-help"> |
84 <p><?php _e('Discard any changes and restore the original image.'); |
89 <p> |
85 |
90 <?php |
86 if ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) |
91 _e( 'Discard any changes and restore the original image.' ); |
87 echo ' '.__('Previously edited copies of the image will not be deleted.'); |
92 |
88 |
93 if ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) { |
89 ?></p> |
94 echo ' ' . __( 'Previously edited copies of the image will not be deleted.' ); |
|
95 } |
|
96 |
|
97 ?> |
|
98 </p> |
90 <div class="imgedit-submit"> |
99 <div class="imgedit-submit"> |
91 <input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'restore')" class="button button-primary" value="<?php esc_attr_e( 'Restore image' ); ?>" <?php echo $can_restore; ?> /> |
100 <input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'restore')" class="button button-primary" value="<?php esc_attr_e( 'Restore image' ); ?>" <?php echo $can_restore; ?> /> |
92 </div> |
101 </div> |
93 </div> |
102 </div> |
94 </div> |
103 </div> |
95 </div> |
104 </div> |
96 |
105 |
97 <?php } ?> |
106 <?php } ?> |
98 |
107 |
99 <div class="imgedit-group"> |
108 <div class="imgedit-group"> |
100 <div class="imgedit-group-top"> |
109 <div class="imgedit-group-top"> |
101 <h2><?php _e( 'Image Crop' ); ?></h2> |
110 <h2><?php _e( 'Image Crop' ); ?></h2> |
102 <button type="button" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" aria-expanded="false"><span class="screen-reader-text"><?php esc_html_e( 'Image Crop Help' ); ?></span></button> |
111 <button type="button" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" aria-expanded="false"><span class="screen-reader-text"><?php esc_html_e( 'Image Crop Help' ); ?></span></button> |
103 |
112 |
104 <div class="imgedit-help"> |
113 <div class="imgedit-help"> |
105 <p><?php _e('To crop the image, click on it and drag to make your selection.'); ?></p> |
114 <p><?php _e( 'To crop the image, click on it and drag to make your selection.' ); ?></p> |
106 |
115 |
107 <p><strong><?php _e('Crop Aspect Ratio'); ?></strong><br /> |
116 <p><strong><?php _e( 'Crop Aspect Ratio' ); ?></strong><br /> |
108 <?php _e('The aspect ratio is the relationship between the width and height. You can preserve the aspect ratio by holding down the shift key while resizing your selection. Use the input box to specify the aspect ratio, e.g. 1:1 (square), 4:3, 16:9, etc.'); ?></p> |
117 <?php _e( 'The aspect ratio is the relationship between the width and height. You can preserve the aspect ratio by holding down the shift key while resizing your selection. Use the input box to specify the aspect ratio, e.g. 1:1 (square), 4:3, 16:9, etc.' ); ?></p> |
109 |
118 |
110 <p><strong><?php _e('Crop Selection'); ?></strong><br /> |
119 <p><strong><?php _e( 'Crop Selection' ); ?></strong><br /> |
111 <?php _e('Once you have made your selection, you can adjust it by entering the size in pixels. The minimum selection size is the thumbnail size as set in the Media settings.'); ?></p> |
120 <?php _e( 'Once you have made your selection, you can adjust it by entering the size in pixels. The minimum selection size is the thumbnail size as set in the Media settings.' ); ?></p> |
112 </div> |
121 </div> |
113 </div> |
122 </div> |
114 |
123 |
115 <fieldset class="imgedit-crop-ratio"> |
124 <fieldset class="imgedit-crop-ratio"> |
116 <legend><?php _e( 'Aspect ratio:' ); ?></legend> |
125 <legend><?php _e( 'Aspect ratio:' ); ?></legend> |
180 </div> |
190 </div> |
181 |
191 |
182 <div class="imgedit-panel-content wp-clearfix"> |
192 <div class="imgedit-panel-content wp-clearfix"> |
183 <?php echo $note; ?> |
193 <?php echo $note; ?> |
184 <div class="imgedit-menu wp-clearfix"> |
194 <div class="imgedit-menu wp-clearfix"> |
185 <button type="button" onclick="imageEdit.crop(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-crop button disabled" disabled><span class="screen-reader-text"><?php esc_html_e( 'Crop' ); ?></span></button><?php |
195 <button type="button" onclick="imageEdit.handleCropToolClick( <?php echo "$post_id, '$nonce'"; ?>, this )" class="imgedit-crop button disabled" disabled><span class="screen-reader-text"><?php esc_html_e( 'Crop' ); ?></span></button> |
186 |
196 <?php |
187 // On some setups GD library does not provide imagerotate() - Ticket #11536 |
197 |
188 if ( wp_image_editor_supports( array( 'mime_type' => get_post_mime_type( $post_id ), 'methods' => array( 'rotate' ) ) ) ) { |
198 // On some setups GD library does not provide imagerotate() - Ticket #11536 |
189 $note_no_rotate = ''; |
199 if ( wp_image_editor_supports( |
190 ?> |
200 array( |
191 <button type="button" class="imgedit-rleft button" onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)"><span class="screen-reader-text"><?php esc_html_e( 'Rotate counter-clockwise' ); ?></span></button> |
201 'mime_type' => get_post_mime_type( $post_id ), |
|
202 'methods' => array( 'rotate' ), |
|
203 ) |
|
204 ) ) { |
|
205 $note_no_rotate = ''; |
|
206 ?> |
|
207 <button type="button" class="imgedit-rleft button" onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)"><span class="screen-reader-text"><?php esc_html_e( 'Rotate counter-clockwise' ); ?></span></button> |
192 <button type="button" class="imgedit-rright button" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)"><span class="screen-reader-text"><?php esc_html_e( 'Rotate clockwise' ); ?></span></button> |
208 <button type="button" class="imgedit-rright button" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)"><span class="screen-reader-text"><?php esc_html_e( 'Rotate clockwise' ); ?></span></button> |
193 <?php } else { |
209 <?php |
194 $note_no_rotate = '<p class="note-no-rotate"><em>' . __( 'Image rotation is not supported by your web host.' ) . '</em></p>'; |
210 } else { |
195 ?> |
211 $note_no_rotate = '<p class="note-no-rotate"><em>' . __( 'Image rotation is not supported by your web host.' ) . '</em></p>'; |
196 <button type="button" class="imgedit-rleft button disabled" disabled></button> |
212 ?> |
197 <button type="button" class="imgedit-rright button disabled" disabled></button> |
213 <button type="button" class="imgedit-rleft button disabled" disabled></button> |
198 <?php } ?> |
214 <button type="button" class="imgedit-rright button disabled" disabled></button> |
|
215 <?php } ?> |
199 |
216 |
200 <button type="button" onclick="imageEdit.flip(1, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-flipv button"><span class="screen-reader-text"><?php esc_html_e( 'Flip vertically' ); ?></span></button> |
217 <button type="button" onclick="imageEdit.flip(1, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-flipv button"><span class="screen-reader-text"><?php esc_html_e( 'Flip vertically' ); ?></span></button> |
201 <button type="button" onclick="imageEdit.flip(2, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-fliph button"><span class="screen-reader-text"><?php esc_html_e( 'Flip horizontally' ); ?></span></button> |
218 <button type="button" onclick="imageEdit.flip(2, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-fliph button"><span class="screen-reader-text"><?php esc_html_e( 'Flip horizontally' ); ?></span></button> |
202 |
219 |
203 <button type="button" id="image-undo-<?php echo $post_id; ?>" onclick="imageEdit.undo(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-undo button disabled" disabled><span class="screen-reader-text"><?php esc_html_e( 'Undo' ); ?></span></button> |
220 <button type="button" id="image-undo-<?php echo $post_id; ?>" onclick="imageEdit.undo(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-undo button disabled" disabled><span class="screen-reader-text"><?php esc_html_e( 'Undo' ); ?></span></button> |
463 * @param WP_Image_Editor $image WP_Image_Editor instance. |
486 * @param WP_Image_Editor $image WP_Image_Editor instance. |
464 * @param array $changes Array of change operations. |
487 * @param array $changes Array of change operations. |
465 * @return WP_Image_Editor WP_Image_Editor instance with changes applied. |
488 * @return WP_Image_Editor WP_Image_Editor instance with changes applied. |
466 */ |
489 */ |
467 function image_edit_apply_changes( $image, $changes ) { |
490 function image_edit_apply_changes( $image, $changes ) { |
468 if ( is_resource( $image ) ) |
491 if ( is_resource( $image ) ) { |
469 _deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) ); |
492 _deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) ); |
470 |
493 } |
471 if ( !is_array($changes) ) |
494 |
|
495 if ( ! is_array( $changes ) ) { |
472 return $image; |
496 return $image; |
|
497 } |
473 |
498 |
474 // Expand change operations. |
499 // Expand change operations. |
475 foreach ( $changes as $key => $obj ) { |
500 foreach ( $changes as $key => $obj ) { |
476 if ( isset($obj->r) ) { |
501 if ( isset( $obj->r ) ) { |
477 $obj->type = 'rotate'; |
502 $obj->type = 'rotate'; |
478 $obj->angle = $obj->r; |
503 $obj->angle = $obj->r; |
479 unset($obj->r); |
504 unset( $obj->r ); |
480 } elseif ( isset($obj->f) ) { |
505 } elseif ( isset( $obj->f ) ) { |
481 $obj->type = 'flip'; |
506 $obj->type = 'flip'; |
482 $obj->axis = $obj->f; |
507 $obj->axis = $obj->f; |
483 unset($obj->f); |
508 unset( $obj->f ); |
484 } elseif ( isset($obj->c) ) { |
509 } elseif ( isset( $obj->c ) ) { |
485 $obj->type = 'crop'; |
510 $obj->type = 'crop'; |
486 $obj->sel = $obj->c; |
511 $obj->sel = $obj->c; |
487 unset($obj->c); |
512 unset( $obj->c ); |
488 } |
513 } |
489 $changes[$key] = $obj; |
514 $changes[ $key ] = $obj; |
490 } |
515 } |
491 |
516 |
492 // Combine operations. |
517 // Combine operations. |
493 if ( count($changes) > 1 ) { |
518 if ( count( $changes ) > 1 ) { |
494 $filtered = array($changes[0]); |
519 $filtered = array( $changes[0] ); |
495 for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) { |
520 for ( $i = 0, $j = 1, $c = count( $changes ); $j < $c; $j++ ) { |
496 $combined = false; |
521 $combined = false; |
497 if ( $filtered[$i]->type == $changes[$j]->type ) { |
522 if ( $filtered[ $i ]->type == $changes[ $j ]->type ) { |
498 switch ( $filtered[$i]->type ) { |
523 switch ( $filtered[ $i ]->type ) { |
499 case 'rotate': |
524 case 'rotate': |
500 $filtered[$i]->angle += $changes[$j]->angle; |
525 $filtered[ $i ]->angle += $changes[ $j ]->angle; |
501 $combined = true; |
526 $combined = true; |
502 break; |
527 break; |
503 case 'flip': |
528 case 'flip': |
504 $filtered[$i]->axis ^= $changes[$j]->axis; |
529 $filtered[ $i ]->axis ^= $changes[ $j ]->axis; |
505 $combined = true; |
530 $combined = true; |
506 break; |
531 break; |
507 } |
532 } |
508 } |
533 } |
509 if ( !$combined ) |
534 if ( ! $combined ) { |
510 $filtered[++$i] = $changes[$j]; |
535 $filtered[ ++$i ] = $changes[ $j ]; |
|
536 } |
511 } |
537 } |
512 $changes = $filtered; |
538 $changes = $filtered; |
513 unset($filtered); |
539 unset( $filtered ); |
514 } |
540 } |
515 |
541 |
516 // Image resource before applying the changes. |
542 // Image resource before applying the changes. |
517 if ( $image instanceof WP_Image_Editor ) { |
543 if ( $image instanceof WP_Image_Editor ) { |
518 |
544 |
532 * |
558 * |
533 * @since 2.9.0 |
559 * @since 2.9.0 |
534 * @deprecated 3.5.0 Use wp_image_editor_before_change instead. |
560 * @deprecated 3.5.0 Use wp_image_editor_before_change instead. |
535 * |
561 * |
536 * @param resource $image GD image resource. |
562 * @param resource $image GD image resource. |
537 * @param array $changes Array of change operations. |
563 * @param array $changes Array of change operations. |
538 */ |
564 */ |
539 $image = apply_filters( 'image_edit_before_change', $image, $changes ); |
565 $image = apply_filters( 'image_edit_before_change', $image, $changes ); |
540 } |
566 } |
541 |
567 |
542 foreach ( $changes as $operation ) { |
568 foreach ( $changes as $operation ) { |
543 switch ( $operation->type ) { |
569 switch ( $operation->type ) { |
544 case 'rotate': |
570 case 'rotate': |
545 if ( $operation->angle != 0 ) { |
571 if ( $operation->angle != 0 ) { |
546 if ( $image instanceof WP_Image_Editor ) |
572 if ( $image instanceof WP_Image_Editor ) { |
547 $image->rotate( $operation->angle ); |
573 $image->rotate( $operation->angle ); |
548 else |
574 } else { |
549 $image = _rotate_image_resource( $image, $operation->angle ); |
575 $image = _rotate_image_resource( $image, $operation->angle ); |
|
576 } |
550 } |
577 } |
551 break; |
578 break; |
552 case 'flip': |
579 case 'flip': |
553 if ( $operation->axis != 0 ) |
580 if ( $operation->axis != 0 ) { |
554 if ( $image instanceof WP_Image_Editor ) |
581 if ( $image instanceof WP_Image_Editor ) { |
555 $image->flip( ($operation->axis & 1) != 0, ($operation->axis & 2) != 0 ); |
582 $image->flip( ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 ); |
556 else |
583 } else { |
557 $image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 ); |
584 $image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 ); |
|
585 } |
|
586 } |
558 break; |
587 break; |
559 case 'crop': |
588 case 'crop': |
560 $sel = $operation->sel; |
589 $sel = $operation->sel; |
561 |
590 |
562 if ( $image instanceof WP_Image_Editor ) { |
591 if ( $image instanceof WP_Image_Editor ) { |
563 $size = $image->get_size(); |
592 $size = $image->get_size(); |
564 $w = $size['width']; |
593 $w = $size['width']; |
565 $h = $size['height']; |
594 $h = $size['height']; |
566 |
595 |
567 $scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling |
596 $scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling |
568 $image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); |
597 $image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale ); |
569 } else { |
598 } else { |
570 $scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling |
599 $scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling |
621 * @since 2.9.0 |
654 * @since 2.9.0 |
622 * |
655 * |
623 * @param int $post_id Attachment post ID. |
656 * @param int $post_id Attachment post ID. |
624 * @return stdClass Image restoration message object. |
657 * @return stdClass Image restoration message object. |
625 */ |
658 */ |
626 function wp_restore_image($post_id) { |
659 function wp_restore_image( $post_id ) { |
627 $meta = wp_get_attachment_metadata($post_id); |
660 $meta = wp_get_attachment_metadata( $post_id ); |
628 $file = get_attached_file($post_id); |
661 $file = get_attached_file( $post_id ); |
629 $backup_sizes = $old_backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true ); |
662 $backup_sizes = $old_backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true ); |
630 $restored = false; |
663 $restored = false; |
631 $msg = new stdClass; |
664 $msg = new stdClass; |
632 |
665 |
633 if ( !is_array($backup_sizes) ) { |
666 if ( ! is_array( $backup_sizes ) ) { |
634 $msg->error = __('Cannot load image metadata.'); |
667 $msg->error = __( 'Cannot load image metadata.' ); |
635 return $msg; |
668 return $msg; |
636 } |
669 } |
637 |
670 |
638 $parts = pathinfo($file); |
671 $parts = pathinfo( $file ); |
639 $suffix = time() . rand(100, 999); |
672 $suffix = time() . rand( 100, 999 ); |
640 $default_sizes = get_intermediate_image_sizes(); |
673 $default_sizes = get_intermediate_image_sizes(); |
641 |
674 |
642 if ( isset($backup_sizes['full-orig']) && is_array($backup_sizes['full-orig']) ) { |
675 if ( isset( $backup_sizes['full-orig'] ) && is_array( $backup_sizes['full-orig'] ) ) { |
643 $data = $backup_sizes['full-orig']; |
676 $data = $backup_sizes['full-orig']; |
644 |
677 |
645 if ( $parts['basename'] != $data['file'] ) { |
678 if ( $parts['basename'] != $data['file'] ) { |
646 if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) { |
679 if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) { |
647 |
680 |
648 // Delete only if it's an edited image. |
681 // Delete only if it's an edited image. |
649 if ( preg_match('/-e[0-9]{13}\./', $parts['basename']) ) { |
682 if ( preg_match( '/-e[0-9]{13}\./', $parts['basename'] ) ) { |
650 wp_delete_file( $file ); |
683 wp_delete_file( $file ); |
651 } |
684 } |
652 } elseif ( isset( $meta['width'], $meta['height'] ) ) { |
685 } elseif ( isset( $meta['width'], $meta['height'] ) ) { |
653 $backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']); |
686 $backup_sizes[ "full-$suffix" ] = array( |
654 } |
687 'width' => $meta['width'], |
655 } |
688 'height' => $meta['height'], |
656 |
689 'file' => $parts['basename'], |
657 $restored_file = path_join($parts['dirname'], $data['file']); |
690 ); |
658 $restored = update_attached_file($post_id, $restored_file); |
691 } |
659 |
692 } |
660 $meta['file'] = _wp_relative_upload_path( $restored_file ); |
693 |
661 $meta['width'] = $data['width']; |
694 $restored_file = path_join( $parts['dirname'], $data['file'] ); |
|
695 $restored = update_attached_file( $post_id, $restored_file ); |
|
696 |
|
697 $meta['file'] = _wp_relative_upload_path( $restored_file ); |
|
698 $meta['width'] = $data['width']; |
662 $meta['height'] = $data['height']; |
699 $meta['height'] = $data['height']; |
663 } |
700 } |
664 |
701 |
665 foreach ( $default_sizes as $default_size ) { |
702 foreach ( $default_sizes as $default_size ) { |
666 if ( isset($backup_sizes["$default_size-orig"]) ) { |
703 if ( isset( $backup_sizes[ "$default_size-orig" ] ) ) { |
667 $data = $backup_sizes["$default_size-orig"]; |
704 $data = $backup_sizes[ "$default_size-orig" ]; |
668 if ( isset($meta['sizes'][$default_size]) && $meta['sizes'][$default_size]['file'] != $data['file'] ) { |
705 if ( isset( $meta['sizes'][ $default_size ] ) && $meta['sizes'][ $default_size ]['file'] != $data['file'] ) { |
669 if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) { |
706 if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) { |
670 |
707 |
671 // Delete only if it's an edited image. |
708 // Delete only if it's an edited image. |
672 if ( preg_match('/-e[0-9]{13}-/', $meta['sizes'][$default_size]['file']) ) { |
709 if ( preg_match( '/-e[0-9]{13}-/', $meta['sizes'][ $default_size ]['file'] ) ) { |
673 $delete_file = path_join( $parts['dirname'], $meta['sizes'][$default_size]['file'] ); |
710 $delete_file = path_join( $parts['dirname'], $meta['sizes'][ $default_size ]['file'] ); |
674 wp_delete_file( $delete_file ); |
711 wp_delete_file( $delete_file ); |
675 } |
712 } |
676 } else { |
713 } else { |
677 $backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size]; |
714 $backup_sizes[ "$default_size-{$suffix}" ] = $meta['sizes'][ $default_size ]; |
678 } |
715 } |
679 } |
716 } |
680 |
717 |
681 $meta['sizes'][$default_size] = $data; |
718 $meta['sizes'][ $default_size ] = $data; |
682 } else { |
719 } else { |
683 unset($meta['sizes'][$default_size]); |
720 unset( $meta['sizes'][ $default_size ] ); |
684 } |
721 } |
685 } |
722 } |
686 |
723 |
687 if ( ! wp_update_attachment_metadata( $post_id, $meta ) || |
724 if ( ! wp_update_attachment_metadata( $post_id, $meta ) || |
688 ( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) ) ) { |
725 ( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) ) ) { |
689 |
726 |
690 $msg->error = __('Cannot save image metadata.'); |
727 $msg->error = __( 'Cannot save image metadata.' ); |
691 return $msg; |
728 return $msg; |
692 } |
729 } |
693 |
730 |
694 if ( !$restored ) |
731 if ( ! $restored ) { |
695 $msg->error = __('Image metadata is inconsistent.'); |
732 $msg->error = __( 'Image metadata is inconsistent.' ); |
696 else |
733 } else { |
697 $msg->msg = __('Image restored successfully.'); |
734 $msg->msg = __( 'Image restored successfully.' ); |
|
735 } |
698 |
736 |
699 return $msg; |
737 return $msg; |
700 } |
738 } |
701 |
739 |
702 /** |
740 /** |
703 * Saves image to post along with enqueued changes |
741 * Saves image to post along with enqueued changes |
704 * in $_REQUEST['history'] |
742 * in $_REQUEST['history'] |
705 * |
743 * |
|
744 * @since 2.9.0 |
|
745 * |
706 * @param int $post_id |
746 * @param int $post_id |
707 * @return \stdClass |
747 * @return \stdClass |
708 */ |
748 */ |
709 function wp_save_image( $post_id ) { |
749 function wp_save_image( $post_id ) { |
710 $_wp_additional_image_sizes = wp_get_additional_image_sizes(); |
750 $_wp_additional_image_sizes = wp_get_additional_image_sizes(); |
711 |
751 |
712 $return = new stdClass; |
752 $return = new stdClass; |
713 $success = $delete = $scaled = $nocrop = false; |
753 $success = $delete = $scaled = $nocrop = false; |
714 $post = get_post( $post_id ); |
754 $post = get_post( $post_id ); |
715 |
755 |
716 $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) ); |
756 $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) ); |
717 if ( is_wp_error( $img ) ) { |
757 if ( is_wp_error( $img ) ) { |
718 $return->error = esc_js( __('Unable to create new image.') ); |
758 $return->error = esc_js( __( 'Unable to create new image.' ) ); |
719 return $return; |
759 return $return; |
720 } |
760 } |
721 |
761 |
722 $fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0; |
762 $fwidth = ! empty( $_REQUEST['fwidth'] ) ? intval( $_REQUEST['fwidth'] ) : 0; |
723 $fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0; |
763 $fheight = ! empty( $_REQUEST['fheight'] ) ? intval( $_REQUEST['fheight'] ) : 0; |
724 $target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : ''; |
764 $target = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : ''; |
725 $scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do']; |
765 $scale = ! empty( $_REQUEST['do'] ) && 'scale' == $_REQUEST['do']; |
726 |
766 |
727 if ( $scale && $fwidth > 0 && $fheight > 0 ) { |
767 if ( $scale && $fwidth > 0 && $fheight > 0 ) { |
728 $size = $img->get_size(); |
768 $size = $img->get_size(); |
729 $sX = $size['width']; |
769 $sX = $size['width']; |
730 $sY = $size['height']; |
770 $sY = $size['height']; |
731 |
771 |
732 // Check if it has roughly the same w / h ratio. |
772 // Check if it has roughly the same w / h ratio. |
733 $diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2); |
773 $diff = round( $sX / $sY, 2 ) - round( $fwidth / $fheight, 2 ); |
734 if ( -0.1 < $diff && $diff < 0.1 ) { |
774 if ( -0.1 < $diff && $diff < 0.1 ) { |
735 // Scale the full size image. |
775 // Scale the full size image. |
736 if ( $img->resize( $fwidth, $fheight ) ) |
776 if ( $img->resize( $fwidth, $fheight ) ) { |
737 $scaled = true; |
777 $scaled = true; |
738 } |
778 } |
739 |
779 } |
740 if ( !$scaled ) { |
780 |
741 $return->error = esc_js( __('Error while saving the scaled image. Please reload the page and try again.') ); |
781 if ( ! $scaled ) { |
|
782 $return->error = esc_js( __( 'Error while saving the scaled image. Please reload the page and try again.' ) ); |
742 return $return; |
783 return $return; |
743 } |
784 } |
744 } elseif ( !empty($_REQUEST['history']) ) { |
785 } elseif ( ! empty( $_REQUEST['history'] ) ) { |
745 $changes = json_decode( wp_unslash($_REQUEST['history']) ); |
786 $changes = json_decode( wp_unslash( $_REQUEST['history'] ) ); |
746 if ( $changes ) |
787 if ( $changes ) { |
747 $img = image_edit_apply_changes($img, $changes); |
788 $img = image_edit_apply_changes( $img, $changes ); |
|
789 } |
748 } else { |
790 } else { |
749 $return->error = esc_js( __('Nothing to save, the image has not changed.') ); |
791 $return->error = esc_js( __( 'Nothing to save, the image has not changed.' ) ); |
750 return $return; |
792 return $return; |
751 } |
793 } |
752 |
794 |
753 $meta = wp_get_attachment_metadata($post_id); |
795 $meta = wp_get_attachment_metadata( $post_id ); |
754 $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); |
796 $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); |
755 |
797 |
756 if ( !is_array($meta) ) { |
798 if ( ! is_array( $meta ) ) { |
757 $return->error = esc_js( __('Image data does not exist. Please re-upload the image.') ); |
799 $return->error = esc_js( __( 'Image data does not exist. Please re-upload the image.' ) ); |
758 return $return; |
800 return $return; |
759 } |
801 } |
760 |
802 |
761 if ( !is_array($backup_sizes) ) |
803 if ( ! is_array( $backup_sizes ) ) { |
762 $backup_sizes = array(); |
804 $backup_sizes = array(); |
|
805 } |
763 |
806 |
764 // Generate new filename. |
807 // Generate new filename. |
765 $path = get_attached_file( $post_id ); |
808 $path = get_attached_file( $post_id ); |
766 |
809 |
767 $basename = pathinfo( $path, PATHINFO_BASENAME ); |
810 $basename = pathinfo( $path, PATHINFO_BASENAME ); |
768 $dirname = pathinfo( $path, PATHINFO_DIRNAME ); |
811 $dirname = pathinfo( $path, PATHINFO_DIRNAME ); |
769 $ext = pathinfo( $path, PATHINFO_EXTENSION ); |
812 $ext = pathinfo( $path, PATHINFO_EXTENSION ); |
770 $filename = pathinfo( $path, PATHINFO_FILENAME ); |
813 $filename = pathinfo( $path, PATHINFO_FILENAME ); |
771 $suffix = time() . rand(100, 999); |
814 $suffix = time() . rand( 100, 999 ); |
772 |
815 |
773 if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && |
816 if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE && |
774 isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $basename ) { |
817 isset( $backup_sizes['full-orig'] ) && $backup_sizes['full-orig']['file'] != $basename ) { |
775 |
818 |
776 if ( 'thumbnail' == $target ) { |
819 if ( 'thumbnail' == $target ) { |
777 $new_path = "{$dirname}/{$filename}-temp.{$ext}"; |
820 $new_path = "{$dirname}/{$filename}-temp.{$ext}"; |
778 } else { |
821 } else { |
779 $new_path = $path; |
822 $new_path = $path; |
780 } |
823 } |
781 } else { |
824 } else { |
782 while ( true ) { |
825 while ( true ) { |
783 $filename = preg_replace( '/-e([0-9]+)$/', '', $filename ); |
826 $filename = preg_replace( '/-e([0-9]+)$/', '', $filename ); |
784 $filename .= "-e{$suffix}"; |
827 $filename .= "-e{$suffix}"; |
785 $new_filename = "{$filename}.{$ext}"; |
828 $new_filename = "{$filename}.{$ext}"; |
786 $new_path = "{$dirname}/$new_filename"; |
829 $new_path = "{$dirname}/$new_filename"; |
787 if ( file_exists($new_path) ) { |
830 if ( file_exists( $new_path ) ) { |
788 $suffix++; |
831 $suffix++; |
789 } else { |
832 } else { |
790 break; |
833 break; |
791 } |
834 } |
792 } |
835 } |
793 } |
836 } |
794 |
837 |
795 // Save the full-size file, also needed to create sub-sizes. |
838 // Save the full-size file, also needed to create sub-sizes. |
796 if ( !wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id) ) { |
839 if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) { |
797 $return->error = esc_js( __('Unable to save the image.') ); |
840 $return->error = esc_js( __( 'Unable to save the image.' ) ); |
798 return $return; |
841 return $return; |
799 } |
842 } |
800 |
843 |
801 if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) { |
844 if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) { |
802 $tag = false; |
845 $tag = false; |