wp/wp-admin/includes/image-edit.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     9 /**
     9 /**
    10  * Loads the WP image-editing interface.
    10  * Loads the WP image-editing interface.
    11  *
    11  *
    12  * @since 2.9.0
    12  * @since 2.9.0
    13  *
    13  *
    14  * @param int         $post_id Post ID.
    14  * @param int         $post_id Attachment post ID.
    15  * @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.
    16  *                             Default false.
    16  *                             Default false.
    17  */
    17  */
    18 function wp_image_editor( $post_id, $msg = false ) {
    18 function wp_image_editor( $post_id, $msg = false ) {
    19 	$nonce     = wp_create_nonce( "image_editor-$post_id" );
    19 	$nonce     = wp_create_nonce( "image_editor-$post_id" );
    31 	$sizer = $big > 400 ? 400 / $big : 1;
    31 	$sizer = $big > 400 ? 400 / $big : 1;
    32 
    32 
    33 	$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 );
    34 	$can_restore  = false;
    34 	$can_restore  = false;
    35 	if ( ! empty( $backup_sizes ) && isset( $backup_sizes['full-orig'], $meta['file'] ) ) {
    35 	if ( ! empty( $backup_sizes ) && isset( $backup_sizes['full-orig'], $meta['file'] ) ) {
    36 		$can_restore = $backup_sizes['full-orig']['file'] != wp_basename( $meta['file'] );
    36 		$can_restore = wp_basename( $meta['file'] ) !== $backup_sizes['full-orig']['file'];
    37 	}
    37 	}
    38 
    38 
    39 	if ( $msg ) {
    39 	if ( $msg ) {
    40 		if ( isset( $msg->error ) ) {
    40 		if ( isset( $msg->error ) ) {
    41 			$note = "<div class='error'><p>$msg->error</p></div>";
    41 			$note = "<div class='notice notice-error' tabindex='-1' role='alert'><p>$msg->error</p></div>";
    42 		} elseif ( isset( $msg->msg ) ) {
    42 		} elseif ( isset( $msg->msg ) ) {
    43 			$note = "<div class='updated'><p>$msg->msg</p></div>";
    43 			$note = "<div class='notice notice-success' tabindex='-1' role='alert'><p>$msg->msg</p></div>";
    44 		}
    44 		}
    45 	}
    45 	}
    46 
    46 
    47 	?>
    47 	?>
    48 	<div class="imgedit-wrap wp-clearfix">
    48 	<div class="imgedit-wrap wp-clearfix">
    49 	<div id="imgedit-panel-<?php echo $post_id; ?>">
    49 	<div id="imgedit-panel-<?php echo $post_id; ?>">
    50 
    50 
    51 	<div class="imgedit-settings">
       
    52 	<div class="imgedit-group">
       
    53 	<div class="imgedit-group-top">
       
    54 		<h2><?php _e( 'Scale Image' ); ?></h2>
       
    55 		<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( 'Scale Image Help' ); ?></span></button>
       
    56 		<div class="imgedit-help">
       
    57 		<p><?php _e( 'You can proportionally scale the original image. For best results, scaling should be done before you crop, flip, or rotate. Images can only be scaled down, not up.' ); ?></p>
       
    58 		</div>
       
    59 		<?php if ( isset( $meta['width'], $meta['height'] ) ) : ?>
       
    60 		<p><?php printf( __( 'Original dimensions %s' ), $meta['width'] . ' &times; ' . $meta['height'] ); ?></p>
       
    61 		<?php endif ?>
       
    62 		<div class="imgedit-submit">
       
    63 
       
    64 		<fieldset class="imgedit-scale">
       
    65 		<legend><?php _e( 'New dimensions:' ); ?></legend>
       
    66 		<div class="nowrap">
       
    67 		<label><span class="screen-reader-text"><?php _e( 'scale width' ); ?></span>
       
    68 		<input type="text" id="imgedit-scale-width-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1, this)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1, this)" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
       
    69 		</label>
       
    70 		<span class="imgedit-separator">&times;</span>
       
    71 		<label><span class="screen-reader-text"><?php _e( 'scale height' ); ?></span>
       
    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; ?>" />
       
    73 		</label>
       
    74 		<span class="imgedit-scale-warn" id="imgedit-scale-warn-<?php echo $post_id; ?>">!</span>
       
    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' ); ?>" />
       
    76 		 </div>
       
    77 		</fieldset>
       
    78 
       
    79 		</div>
       
    80 	</div>
       
    81 	</div>
       
    82 
       
    83 	<?php if ( $can_restore ) { ?>
       
    84 
       
    85 	<div class="imgedit-group">
       
    86 	<div class="imgedit-group-top">
       
    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>
       
    88 		<div class="imgedit-help">
       
    89 		<p>
       
    90 		<?php
       
    91 		_e( 'Discard any changes and restore the original image.' );
       
    92 
       
    93 		if ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) {
       
    94 			echo ' ' . __( 'Previously edited copies of the image will not be deleted.' );
       
    95 		}
       
    96 
       
    97 		?>
       
    98 		</p>
       
    99 		<div class="imgedit-submit">
       
   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; ?> />
       
   101 		</div>
       
   102 		</div>
       
   103 	</div>
       
   104 	</div>
       
   105 
       
   106 	<?php } ?>
       
   107 
       
   108 	<div class="imgedit-group">
       
   109 	<div class="imgedit-group-top">
       
   110 		<h2><?php _e( 'Image Crop' ); ?></h2>
       
   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>
       
   112 
       
   113 		<div class="imgedit-help">
       
   114 		<p><?php _e( 'To crop the image, click on it and drag to make your selection.' ); ?></p>
       
   115 
       
   116 		<p><strong><?php _e( 'Crop Aspect Ratio' ); ?></strong><br />
       
   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>
       
   118 
       
   119 		<p><strong><?php _e( 'Crop Selection' ); ?></strong><br />
       
   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>
       
   121 		</div>
       
   122 	</div>
       
   123 
       
   124 	<fieldset class="imgedit-crop-ratio">
       
   125 		<legend><?php _e( 'Aspect ratio:' ); ?></legend>
       
   126 		<div class="nowrap">
       
   127 		<label><span class="screen-reader-text"><?php _e( 'crop ratio width' ); ?></span>
       
   128 		<input type="text" id="imgedit-crop-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" onblur="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" />
       
   129 		</label>
       
   130 		<span class="imgedit-separator">:</span>
       
   131 		<label><span class="screen-reader-text"><?php _e( 'crop ratio height' ); ?></span>
       
   132 		<input type="text" id="imgedit-crop-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" onblur="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" />
       
   133 		</label>
       
   134 		</div>
       
   135 	</fieldset>
       
   136 
       
   137 	<fieldset id="imgedit-crop-sel-<?php echo $post_id; ?>" class="imgedit-crop-sel">
       
   138 		<legend><?php _e( 'Selection:' ); ?></legend>
       
   139 		<div class="nowrap">
       
   140 		<label><span class="screen-reader-text"><?php _e( 'selection width' ); ?></span>
       
   141 		<input type="text" id="imgedit-sel-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" />
       
   142 		</label>
       
   143 		<span class="imgedit-separator">&times;</span>
       
   144 		<label><span class="screen-reader-text"><?php _e( 'selection height' ); ?></span>
       
   145 		<input type="text" id="imgedit-sel-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" />
       
   146 		</label>
       
   147 		</div>
       
   148 	</fieldset>
       
   149 
       
   150 	</div>
       
   151 
       
   152 	<?php
       
   153 	if ( $thumb && $sub_sizes ) {
       
   154 		$thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
       
   155 		?>
       
   156 
       
   157 	<div class="imgedit-group imgedit-applyto">
       
   158 	<div class="imgedit-group-top">
       
   159 		<h2><?php _e( 'Thumbnail Settings' ); ?></h2>
       
   160 		<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( 'Thumbnail Settings Help' ); ?></span></button>
       
   161 		<p class="imgedit-help"><?php _e( 'You can edit the image while preserving the thumbnail. For example, you may wish to have a square thumbnail that displays just a section of the image.' ); ?></p>
       
   162 	</div>
       
   163 
       
   164 	<figure class="imgedit-thumbnail-preview">
       
   165 		<img src="<?php echo $thumb['url']; ?>" width="<?php echo $thumb_img[0]; ?>" height="<?php echo $thumb_img[1]; ?>" class="imgedit-size-preview" alt="" draggable="false" />
       
   166 		<figcaption class="imgedit-thumbnail-preview-caption"><?php _e( 'Current thumbnail' ); ?></figcaption>
       
   167 	</figure>
       
   168 
       
   169 	<div id="imgedit-save-target-<?php echo $post_id; ?>" class="imgedit-save-target">
       
   170 	<fieldset>
       
   171 		<legend><strong><?php _e( 'Apply changes to:' ); ?></strong></legend>
       
   172 
       
   173 		<label class="imgedit-label">
       
   174 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="all" checked="checked" />
       
   175 		<?php _e( 'All image sizes' ); ?></label>
       
   176 
       
   177 		<label class="imgedit-label">
       
   178 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="thumbnail" />
       
   179 		<?php _e( 'Thumbnail' ); ?></label>
       
   180 
       
   181 		<label class="imgedit-label">
       
   182 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
       
   183 		<?php _e( 'All sizes except thumbnail' ); ?></label>
       
   184 	</fieldset>
       
   185 	</div>
       
   186 	</div>
       
   187 
       
   188 	<?php } ?>
       
   189 
       
   190 	</div>
       
   191 
       
   192 	<div class="imgedit-panel-content wp-clearfix">
    51 	<div class="imgedit-panel-content wp-clearfix">
   193 		<?php echo $note; ?>
    52 		<?php echo $note; ?>
   194 		<div class="imgedit-menu wp-clearfix">
    53 		<div class="imgedit-menu wp-clearfix">
   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>
    54 			<button type="button" onclick="imageEdit.handleCropToolClick( <?php echo "$post_id, '$nonce'"; ?>, this )" class="imgedit-crop button disabled" disabled><?php esc_html_e( 'Crop' ); ?></button>
   196 																		<?php
    55 			<?php
   197 
    56 
   198 																		// On some setups GD library does not provide imagerotate() - Ticket #11536
    57 			// On some setups GD library does not provide imagerotate() - Ticket #11536.
   199 																		if ( wp_image_editor_supports(
    58 			if ( wp_image_editor_supports(
   200 																			array(
    59 				array(
   201 																				'mime_type' => get_post_mime_type( $post_id ),
    60 					'mime_type' => get_post_mime_type( $post_id ),
   202 																				'methods'   => array( 'rotate' ),
    61 					'methods'   => array( 'rotate' ),
   203 																			)
    62 				)
   204 																		) ) {
    63 			) ) {
   205 																			$note_no_rotate = '';
    64 				$note_no_rotate = '';
   206 																			?>
    65 				?>
   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>
    66 				<button type="button" class="imgedit-rleft button" onclick="imageEdit.rotate( 90, <?php echo "$post_id, '$nonce'"; ?>, this)"><?php esc_html_e( 'Rotate left' ); ?></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>
    67 				<button type="button" class="imgedit-rright button" onclick="imageEdit.rotate(-90, <?php echo "$post_id, '$nonce'"; ?>, this)"><?php esc_html_e( 'Rotate right' ); ?></button>
   209 																			<?php
    68 				<?php
   210 																		} else {
    69 			} else {
   211 																					$note_no_rotate = '<p class="note-no-rotate"><em>' . __( 'Image rotation is not supported by your web host.' ) . '</em></p>';
    70 				$note_no_rotate = '<p class="note-no-rotate"><em>' . __( 'Image rotation is not supported by your web host.' ) . '</em></p>';
   212 																			?>
    71 				?>
   213 																				<button type="button" class="imgedit-rleft button disabled" disabled></button>
    72 				<button type="button" class="imgedit-rleft button disabled" disabled></button>
   214 																				<button type="button" class="imgedit-rright button disabled" disabled></button>
    73 				<button type="button" class="imgedit-rright button disabled" disabled></button>
   215 																		<?php } ?>
    74 			<?php } ?>
   216 
    75 
   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>
    76 			<button type="button" onclick="imageEdit.flip(1, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-flipv button"><?php esc_html_e( 'Flip vertical' ); ?></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>
    77 			<button type="button" onclick="imageEdit.flip(2, <?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-fliph button"><?php esc_html_e( 'Flip horizontal' ); ?></button>
   219 
    78 
   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>
    79 			<br class="imgedit-undo-redo-separator" />
   221 			<button type="button" id="image-redo-<?php echo $post_id; ?>" onclick="imageEdit.redo(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-redo button disabled" disabled><span class="screen-reader-text"><?php esc_html_e( 'Redo' ); ?></span></button>
    80 			<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><?php esc_html_e( 'Undo' ); ?></button>
       
    81 			<button type="button" id="image-redo-<?php echo $post_id; ?>" onclick="imageEdit.redo(<?php echo "$post_id, '$nonce'"; ?>, this)" class="imgedit-redo button disabled" disabled><?php esc_html_e( 'Redo' ); ?></button>
   222 			<?php echo $note_no_rotate; ?>
    82 			<?php echo $note_no_rotate; ?>
   223 		</div>
    83 		</div>
   224 
    84 
   225 		<input type="hidden" id="imgedit-sizer-<?php echo $post_id; ?>" value="<?php echo $sizer; ?>" />
    85 		<input type="hidden" id="imgedit-sizer-<?php echo $post_id; ?>" value="<?php echo $sizer; ?>" />
   226 		<input type="hidden" id="imgedit-history-<?php echo $post_id; ?>" value="" />
    86 		<input type="hidden" id="imgedit-history-<?php echo $post_id; ?>" value="" />
   237 			<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button imgedit-cancel-btn" value="<?php esc_attr_e( 'Cancel' ); ?>" />
    97 			<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button imgedit-cancel-btn" value="<?php esc_attr_e( 'Cancel' ); ?>" />
   238 			<input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" disabled="disabled" class="button button-primary imgedit-submit-btn" value="<?php esc_attr_e( 'Save' ); ?>" />
    98 			<input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" disabled="disabled" class="button button-primary imgedit-submit-btn" value="<?php esc_attr_e( 'Save' ); ?>" />
   239 		</div>
    99 		</div>
   240 	</div>
   100 	</div>
   241 
   101 
       
   102 	<div class="imgedit-settings">
       
   103 	<div class="imgedit-group">
       
   104 	<div class="imgedit-group-top">
       
   105 		<h2><?php _e( 'Scale Image' ); ?></h2>
       
   106 		<button type="button" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);" aria-expanded="false"><span class="screen-reader-text"><?php esc_html_e( 'Scale Image Help' ); ?></span></button>
       
   107 		<div class="imgedit-help">
       
   108 		<p><?php _e( 'You can proportionally scale the original image. For best results, scaling should be done before you crop, flip, or rotate. Images can only be scaled down, not up.' ); ?></p>
       
   109 		</div>
       
   110 		<?php if ( isset( $meta['width'], $meta['height'] ) ) : ?>
       
   111 		<p>
       
   112 			<?php
       
   113 			printf(
       
   114 				/* translators: %s: Image width and height in pixels. */
       
   115 				__( 'Original dimensions %s' ),
       
   116 				'<span class="imgedit-original-dimensions">' . $meta['width'] . ' &times; ' . $meta['height'] . '</span>'
       
   117 			);
       
   118 			?>
       
   119 		</p>
       
   120 		<?php endif ?>
       
   121 		<div class="imgedit-submit">
       
   122 
       
   123 		<fieldset class="imgedit-scale">
       
   124 		<legend><?php _e( 'New dimensions:' ); ?></legend>
       
   125 		<div class="nowrap">
       
   126 		<label for="imgedit-scale-width-<?php echo $post_id; ?>" class="screen-reader-text"><?php _e( 'scale width' ); ?></label>
       
   127 		<input type="text" id="imgedit-scale-width-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1, this)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1, this)" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
       
   128 		<span class="imgedit-separator" aria-hidden="true">&times;</span>
       
   129 		<label for="imgedit-scale-height-<?php echo $post_id; ?>" class="screen-reader-text"><?php _e( 'scale height' ); ?></label>
       
   130 		<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; ?>" />
       
   131 		<span class="imgedit-scale-warn" id="imgedit-scale-warn-<?php echo $post_id; ?>">!</span>
       
   132 		<div class="imgedit-scale-button-wrapper"><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' ); ?>" /></div>
       
   133 		</div>
       
   134 		</fieldset>
       
   135 
       
   136 		</div>
       
   137 	</div>
       
   138 	</div>
       
   139 
       
   140 	<?php if ( $can_restore ) { ?>
       
   141 
       
   142 	<div class="imgedit-group">
       
   143 	<div class="imgedit-group-top">
       
   144 		<h2><button type="button" onclick="imageEdit.toggleHelp(this);" class="button-link" aria-expanded="false"><?php _e( 'Restore original image' ); ?> <span class="dashicons dashicons-arrow-down imgedit-help-toggle"></span></button></h2>
       
   145 		<div class="imgedit-help imgedit-restore">
       
   146 		<p>
       
   147 			<?php
       
   148 			_e( 'Discard any changes and restore the original image.' );
       
   149 
       
   150 			if ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) {
       
   151 				echo ' ' . __( 'Previously edited copies of the image will not be deleted.' );
       
   152 			}
       
   153 			?>
       
   154 		</p>
       
   155 		<div class="imgedit-submit">
       
   156 		<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; ?> />
       
   157 		</div>
       
   158 		</div>
       
   159 	</div>
       
   160 	</div>
       
   161 
       
   162 	<?php } ?>
       
   163 
       
   164 	<div class="imgedit-group">
       
   165 	<div class="imgedit-group-top">
       
   166 		<h2><?php _e( 'Image Crop' ); ?></h2>
       
   167 		<button type="button" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);" aria-expanded="false"><span class="screen-reader-text"><?php esc_html_e( 'Image Crop Help' ); ?></span></button>
       
   168 
       
   169 		<div class="imgedit-help">
       
   170 		<p><?php _e( 'To crop the image, click on it and drag to make your selection.' ); ?></p>
       
   171 
       
   172 		<p><strong><?php _e( 'Crop Aspect Ratio' ); ?></strong><br />
       
   173 		<?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>
       
   174 
       
   175 		<p><strong><?php _e( 'Crop Selection' ); ?></strong><br />
       
   176 		<?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>
       
   177 		</div>
       
   178 	</div>
       
   179 
       
   180 	<fieldset class="imgedit-crop-ratio">
       
   181 		<legend><?php _e( 'Aspect ratio:' ); ?></legend>
       
   182 		<div class="nowrap">
       
   183 		<label for="imgedit-crop-width-<?php echo $post_id; ?>" class="screen-reader-text"><?php _e( 'crop ratio width' ); ?></label>
       
   184 		<input type="text" id="imgedit-crop-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" onblur="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" />
       
   185 		<span class="imgedit-separator" aria-hidden="true">:</span>
       
   186 		<label for="imgedit-crop-height-<?php echo $post_id; ?>" class="screen-reader-text"><?php _e( 'crop ratio height' ); ?></label>
       
   187 		<input type="text" id="imgedit-crop-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" onblur="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" />
       
   188 		</div>
       
   189 	</fieldset>
       
   190 
       
   191 	<fieldset id="imgedit-crop-sel-<?php echo $post_id; ?>" class="imgedit-crop-sel">
       
   192 		<legend><?php _e( 'Selection:' ); ?></legend>
       
   193 		<div class="nowrap">
       
   194 		<label for="imgedit-sel-width-<?php echo $post_id; ?>" class="screen-reader-text"><?php _e( 'selection width' ); ?></label>
       
   195 		<input type="text" id="imgedit-sel-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" />
       
   196 		<span class="imgedit-separator" aria-hidden="true">&times;</span>
       
   197 		<label for="imgedit-sel-height-<?php echo $post_id; ?>" class="screen-reader-text"><?php _e( 'selection height' ); ?></label>
       
   198 		<input type="text" id="imgedit-sel-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" />
       
   199 		</div>
       
   200 	</fieldset>
       
   201 
       
   202 	</div>
       
   203 
       
   204 	<?php
       
   205 	if ( $thumb && $sub_sizes ) {
       
   206 		$thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
       
   207 		?>
       
   208 
       
   209 	<div class="imgedit-group imgedit-applyto">
       
   210 	<div class="imgedit-group-top">
       
   211 		<h2><?php _e( 'Thumbnail Settings' ); ?></h2>
       
   212 		<button type="button" class="dashicons dashicons-editor-help imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);" aria-expanded="false"><span class="screen-reader-text"><?php esc_html_e( 'Thumbnail Settings Help' ); ?></span></button>
       
   213 		<div class="imgedit-help">
       
   214 		<p><?php _e( 'You can edit the image while preserving the thumbnail. For example, you may wish to have a square thumbnail that displays just a section of the image.' ); ?></p>
       
   215 		</div>
       
   216 	</div>
       
   217 
       
   218 	<figure class="imgedit-thumbnail-preview">
       
   219 		<img src="<?php echo $thumb['url']; ?>" width="<?php echo $thumb_img[0]; ?>" height="<?php echo $thumb_img[1]; ?>" class="imgedit-size-preview" alt="" draggable="false" />
       
   220 		<figcaption class="imgedit-thumbnail-preview-caption"><?php _e( 'Current thumbnail' ); ?></figcaption>
       
   221 	</figure>
       
   222 
       
   223 	<div id="imgedit-save-target-<?php echo $post_id; ?>" class="imgedit-save-target">
       
   224 	<fieldset>
       
   225 		<legend><?php _e( 'Apply changes to:' ); ?></legend>
       
   226 
       
   227 		<span class="imgedit-label">
       
   228 			<input type="radio" id="imgedit-target-all" name="imgedit-target-<?php echo $post_id; ?>" value="all" checked="checked" />
       
   229 			<label for="imgedit-target-all"><?php _e( 'All image sizes' ); ?></label>
       
   230 		</span>
       
   231 
       
   232 		<span class="imgedit-label">
       
   233 			<input type="radio" id="imgedit-target-thumbnail" name="imgedit-target-<?php echo $post_id; ?>" value="thumbnail" />
       
   234 			<label for="imgedit-target-thumbnail"><?php _e( 'Thumbnail' ); ?></label>
       
   235 		</span>
       
   236 
       
   237 		<span class="imgedit-label">
       
   238 			<input type="radio" id="imgedit-target-nothumb" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
       
   239 			<label for="imgedit-target-nothumb"><?php _e( 'All sizes except thumbnail' ); ?></label>
       
   240 		</span>
       
   241 	</fieldset>
       
   242 	</div>
       
   243 	</div>
       
   244 
       
   245 	<?php } ?>
       
   246 
       
   247 	</div>
       
   248 
   242 	</div>
   249 	</div>
   243 	<div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
   250 	<div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
   244 	<div class="hidden" id="imgedit-leaving-<?php echo $post_id; ?>"><?php _e( "There are unsaved changes that will be lost. 'OK' to continue, 'Cancel' to return to the Image Editor." ); ?></div>
   251 	<div class="hidden" id="imgedit-leaving-<?php echo $post_id; ?>"><?php _e( "There are unsaved changes that will be lost. 'OK' to continue, 'Cancel' to return to the Image Editor." ); ?></div>
   245 	</div>
   252 	</div>
   246 	<?php
   253 	<?php
   273 			return false;
   280 			return false;
   274 		}
   281 		}
   275 
   282 
   276 		return true;
   283 		return true;
   277 	} else {
   284 	} else {
   278 		_deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) );
   285 		/* translators: 1: $image, 2: WP_Image_Editor */
       
   286 		_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
   279 
   287 
   280 		/**
   288 		/**
   281 		 * Filters the GD image resource to be streamed to the browser.
   289 		 * Filters the GD image resource to be streamed to the browser.
   282 		 *
   290 		 *
   283 		 * @since 2.9.0
   291 		 * @since 2.9.0
   284 		 * @deprecated 3.5.0 Use image_editor_save_pre instead.
   292 		 * @deprecated 3.5.0 Use {@see 'image_editor_save_pre'} instead.
   285 		 *
   293 		 *
   286 		 * @param resource $image         Image resource to be streamed.
   294 		 * @param resource $image         Image resource to be streamed.
   287 		 * @param int      $attachment_id The attachment post ID.
   295 		 * @param int      $attachment_id The attachment post ID.
   288 		 */
   296 		 */
   289 		$image = apply_filters( 'image_save_pre', $image, $attachment_id );
   297 		$image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' );
   290 
   298 
   291 		switch ( $mime_type ) {
   299 		switch ( $mime_type ) {
   292 			case 'image/jpeg':
   300 			case 'image/jpeg':
   293 				header( 'Content-Type: image/jpeg' );
   301 				header( 'Content-Type: image/jpeg' );
   294 				return imagejpeg( $image, null, 90 );
   302 				return imagejpeg( $image, null, 90 );
   307 /**
   315 /**
   308  * Saves image to file.
   316  * Saves image to file.
   309  *
   317  *
   310  * @since 2.9.0
   318  * @since 2.9.0
   311  *
   319  *
   312  * @param string $filename
   320  * @param string          $filename  Name of the file to be saved.
   313  * @param WP_Image_Editor $image
   321  * @param WP_Image_Editor $image     The image editor instance.
   314  * @param string $mime_type
   322  * @param string          $mime_type The mime type of the image.
   315  * @param int $post_id
   323  * @param int             $post_id   Attachment post ID.
   316  * @return bool
   324  * @return bool True on success, false on failure.
   317  */
   325  */
   318 function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {
   326 function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {
   319 	if ( $image instanceof WP_Image_Editor ) {
   327 	if ( $image instanceof WP_Image_Editor ) {
   320 
   328 
   321 		/** This filter is documented in wp-admin/includes/image-edit.php */
   329 		/** This filter is documented in wp-admin/includes/image-edit.php */
   327 		 * Returning a non-null value will short-circuit the save method,
   335 		 * Returning a non-null value will short-circuit the save method,
   328 		 * returning that value instead.
   336 		 * returning that value instead.
   329 		 *
   337 		 *
   330 		 * @since 3.5.0
   338 		 * @since 3.5.0
   331 		 *
   339 		 *
       
   340 		 * @param bool|null       $override  Value to return instead of saving. Default null.
       
   341 		 * @param string          $filename  Name of the file to be saved.
       
   342 		 * @param WP_Image_Editor $image     The image editor instance.
       
   343 		 * @param string          $mime_type The mime type of the image.
       
   344 		 * @param int             $post_id   Attachment post ID.
       
   345 		 */
       
   346 		$saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id );
       
   347 
       
   348 		if ( null !== $saved ) {
       
   349 			return $saved;
       
   350 		}
       
   351 
       
   352 		return $image->save( $filename, $mime_type );
       
   353 	} else {
       
   354 		/* translators: 1: $image, 2: WP_Image_Editor */
       
   355 		_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
       
   356 
       
   357 		/** This filter is documented in wp-admin/includes/image-edit.php */
       
   358 		$image = apply_filters_deprecated( 'image_save_pre', array( $image, $post_id ), '3.5.0', 'image_editor_save_pre' );
       
   359 
       
   360 		/**
       
   361 		 * Filters whether to skip saving the image file.
       
   362 		 *
       
   363 		 * Returning a non-null value will short-circuit the save method,
       
   364 		 * returning that value instead.
       
   365 		 *
       
   366 		 * @since 2.9.0
       
   367 		 * @deprecated 3.5.0 Use {@see 'wp_save_image_editor_file'} instead.
       
   368 		 *
   332 		 * @param mixed           $override  Value to return instead of saving. Default null.
   369 		 * @param mixed           $override  Value to return instead of saving. Default null.
   333 		 * @param string          $filename  Name of the file to be saved.
   370 		 * @param string          $filename  Name of the file to be saved.
   334 		 * @param WP_Image_Editor $image     WP_Image_Editor instance.
   371 		 * @param WP_Image_Editor $image     The image editor instance.
   335 		 * @param string          $mime_type Image mime type.
   372 		 * @param string          $mime_type The mime type of the image.
   336 		 * @param int             $post_id   Post ID.
   373 		 * @param int             $post_id   Attachment post ID.
   337 		 */
   374 		 */
   338 		$saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id );
   375 		$saved = apply_filters_deprecated(
   339 
   376 			'wp_save_image_file',
   340 		if ( null !== $saved ) {
   377 			array( null, $filename, $image, $mime_type, $post_id ),
   341 			return $saved;
   378 			'3.5.0',
   342 		}
   379 			'wp_save_image_editor_file'
   343 
   380 		);
   344 		return $image->save( $filename, $mime_type );
       
   345 	} else {
       
   346 		_deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) );
       
   347 
       
   348 		/** This filter is documented in wp-admin/includes/image-edit.php */
       
   349 		$image = apply_filters( 'image_save_pre', $image, $post_id );
       
   350 
       
   351 		/**
       
   352 		 * Filters whether to skip saving the image file.
       
   353 		 *
       
   354 		 * Returning a non-null value will short-circuit the save method,
       
   355 		 * returning that value instead.
       
   356 		 *
       
   357 		 * @since 2.9.0
       
   358 		 * @deprecated 3.5.0 Use wp_save_image_editor_file instead.
       
   359 		 *
       
   360 		 * @param mixed           $override  Value to return instead of saving. Default null.
       
   361 		 * @param string          $filename  Name of the file to be saved.
       
   362 		 * @param WP_Image_Editor $image     WP_Image_Editor instance.
       
   363 		 * @param string          $mime_type Image mime type.
       
   364 		 * @param int             $post_id   Post ID.
       
   365 		 */
       
   366 		$saved = apply_filters( 'wp_save_image_file', null, $filename, $image, $mime_type, $post_id );
       
   367 
   381 
   368 		if ( null !== $saved ) {
   382 		if ( null !== $saved ) {
   369 			return $saved;
   383 			return $saved;
   370 		}
   384 		}
   371 
   385 
   460  * @since 2.9.0
   474  * @since 2.9.0
   461  *
   475  *
   462  * @ignore
   476  * @ignore
   463  * @param resource $img Image resource.
   477  * @param resource $img Image resource.
   464  * @param float    $x   Source point x-coordinate.
   478  * @param float    $x   Source point x-coordinate.
   465  * @param float    $y   Source point y-cooredinate.
   479  * @param float    $y   Source point y-coordinate.
   466  * @param float    $w   Source width.
   480  * @param float    $w   Source width.
   467  * @param float    $h   Source height.
   481  * @param float    $h   Source height.
   468  * @return resource (maybe) cropped image resource.
   482  * @return resource (maybe) cropped image resource.
   469  */
   483  */
   470 function _crop_image_resource( $img, $x, $y, $w, $h ) {
   484 function _crop_image_resource( $img, $x, $y, $w, $h ) {
   487  * @param array           $changes Array of change operations.
   501  * @param array           $changes Array of change operations.
   488  * @return WP_Image_Editor WP_Image_Editor instance with changes applied.
   502  * @return WP_Image_Editor WP_Image_Editor instance with changes applied.
   489  */
   503  */
   490 function image_edit_apply_changes( $image, $changes ) {
   504 function image_edit_apply_changes( $image, $changes ) {
   491 	if ( is_resource( $image ) ) {
   505 	if ( is_resource( $image ) ) {
   492 		_deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) );
   506 		/* translators: 1: $image, 2: WP_Image_Editor */
       
   507 		_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) );
   493 	}
   508 	}
   494 
   509 
   495 	if ( ! is_array( $changes ) ) {
   510 	if ( ! is_array( $changes ) ) {
   496 		return $image;
   511 		return $image;
   497 	}
   512 	}
   555 
   570 
   556 		/**
   571 		/**
   557 		 * Filters the GD image resource before applying changes to the image.
   572 		 * Filters the GD image resource before applying changes to the image.
   558 		 *
   573 		 *
   559 		 * @since 2.9.0
   574 		 * @since 2.9.0
   560 		 * @deprecated 3.5.0 Use wp_image_editor_before_change instead.
   575 		 * @deprecated 3.5.0 Use {@see 'wp_image_editor_before_change'} instead.
   561 		 *
   576 		 *
   562 		 * @param resource $image   GD image resource.
   577 		 * @param resource $image   GD image resource.
   563 		 * @param array    $changes Array of change operations.
   578 		 * @param array    $changes Array of change operations.
   564 		 */
   579 		 */
   565 		$image = apply_filters( 'image_edit_before_change', $image, $changes );
   580 		$image = apply_filters_deprecated( 'image_edit_before_change', array( $image, $changes ), '3.5.0', 'wp_image_editor_before_change' );
   566 	}
   581 	}
   567 
   582 
   568 	foreach ( $changes as $operation ) {
   583 	foreach ( $changes as $operation ) {
   569 		switch ( $operation->type ) {
   584 		switch ( $operation->type ) {
   570 			case 'rotate':
   585 			case 'rotate':
   571 				if ( $operation->angle != 0 ) {
   586 				if ( 0 != $operation->angle ) {
   572 					if ( $image instanceof WP_Image_Editor ) {
   587 					if ( $image instanceof WP_Image_Editor ) {
   573 						$image->rotate( $operation->angle );
   588 						$image->rotate( $operation->angle );
   574 					} else {
   589 					} else {
   575 						$image = _rotate_image_resource( $image, $operation->angle );
   590 						$image = _rotate_image_resource( $image, $operation->angle );
   576 					}
   591 					}
   577 				}
   592 				}
   578 				break;
   593 				break;
   579 			case 'flip':
   594 			case 'flip':
   580 				if ( $operation->axis != 0 ) {
   595 				if ( 0 != $operation->axis ) {
   581 					if ( $image instanceof WP_Image_Editor ) {
   596 					if ( $image instanceof WP_Image_Editor ) {
   582 						$image->flip( ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 );
   597 						$image->flip( ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 );
   583 					} else {
   598 					} else {
   584 						$image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 );
   599 						$image = _flip_image_resource( $image, ( $operation->axis & 1 ) != 0, ( $operation->axis & 2 ) != 0 );
   585 					}
   600 					}
   591 				if ( $image instanceof WP_Image_Editor ) {
   606 				if ( $image instanceof WP_Image_Editor ) {
   592 					$size = $image->get_size();
   607 					$size = $image->get_size();
   593 					$w    = $size['width'];
   608 					$w    = $size['width'];
   594 					$h    = $size['height'];
   609 					$h    = $size['height'];
   595 
   610 
   596 					$scale = 1 / _image_get_preview_ratio( $w, $h ); // discard preview scaling
   611 					$scale = 1 / _image_get_preview_ratio( $w, $h ); // Discard preview scaling.
   597 					$image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
   612 					$image->crop( $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
   598 				} else {
   613 				} else {
   599 					$scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // discard preview scaling
   614 					$scale = 1 / _image_get_preview_ratio( imagesx( $image ), imagesy( $image ) ); // Discard preview scaling.
   600 					$image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
   615 					$image = _crop_image_resource( $image, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale );
   601 				}
   616 				}
   602 				break;
   617 				break;
   603 		}
   618 		}
   604 	}
   619 	}
   607 }
   622 }
   608 
   623 
   609 
   624 
   610 /**
   625 /**
   611  * Streams image in post to browser, along with enqueued changes
   626  * Streams image in post to browser, along with enqueued changes
   612  * in $_REQUEST['history']
   627  * in `$_REQUEST['history']`.
   613  *
   628  *
   614  * @since 2.9.0
   629  * @since 2.9.0
   615  *
   630  *
   616  * @param int $post_id
   631  * @param int $post_id Attachment post ID.
   617  * @return bool
   632  * @return bool True on success, false on failure.
   618  */
   633  */
   619 function stream_preview_image( $post_id ) {
   634 function stream_preview_image( $post_id ) {
   620 	$post = get_post( $post_id );
   635 	$post = get_post( $post_id );
   621 
   636 
   622 	wp_raise_memory_limit( 'admin' );
   637 	wp_raise_memory_limit( 'admin' );
   655  *
   670  *
   656  * @param int $post_id Attachment post ID.
   671  * @param int $post_id Attachment post ID.
   657  * @return stdClass Image restoration message object.
   672  * @return stdClass Image restoration message object.
   658  */
   673  */
   659 function wp_restore_image( $post_id ) {
   674 function wp_restore_image( $post_id ) {
   660 	$meta         = wp_get_attachment_metadata( $post_id );
   675 	$meta             = wp_get_attachment_metadata( $post_id );
   661 	$file         = get_attached_file( $post_id );
   676 	$file             = get_attached_file( $post_id );
   662 	$backup_sizes = $old_backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
   677 	$backup_sizes     = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
   663 	$restored     = false;
   678 	$old_backup_sizes = $backup_sizes;
   664 	$msg          = new stdClass;
   679 	$restored         = false;
       
   680 	$msg              = new stdClass;
   665 
   681 
   666 	if ( ! is_array( $backup_sizes ) ) {
   682 	if ( ! is_array( $backup_sizes ) ) {
   667 		$msg->error = __( 'Cannot load image metadata.' );
   683 		$msg->error = __( 'Cannot load image metadata.' );
   668 		return $msg;
   684 		return $msg;
   669 	}
   685 	}
   736 
   752 
   737 	return $msg;
   753 	return $msg;
   738 }
   754 }
   739 
   755 
   740 /**
   756 /**
   741  * Saves image to post along with enqueued changes
   757  * Saves image to post, along with enqueued changes
   742  * in $_REQUEST['history']
   758  * in `$_REQUEST['history']`.
   743  *
   759  *
   744  * @since 2.9.0
   760  * @since 2.9.0
   745  *
   761  *
   746  * @param int $post_id
   762  * @param int $post_id Attachment post ID.
   747  * @return \stdClass
   763  * @return stdClass
   748  */
   764  */
   749 function wp_save_image( $post_id ) {
   765 function wp_save_image( $post_id ) {
   750 	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
   766 	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
   751 
   767 
   752 	$return  = new stdClass;
   768 	$return  = new stdClass;
   753 	$success = $delete = $scaled = $nocrop = false;
   769 	$success = false;
       
   770 	$delete  = false;
       
   771 	$scaled  = false;
       
   772 	$nocrop  = false;
   754 	$post    = get_post( $post_id );
   773 	$post    = get_post( $post_id );
   755 
   774 
   756 	$img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) );
   775 	$img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) );
   757 	if ( is_wp_error( $img ) ) {
   776 	if ( is_wp_error( $img ) ) {
   758 		$return->error = esc_js( __( 'Unable to create new image.' ) );
   777 		$return->error = esc_js( __( 'Unable to create new image.' ) );
   760 	}
   779 	}
   761 
   780 
   762 	$fwidth  = ! empty( $_REQUEST['fwidth'] ) ? intval( $_REQUEST['fwidth'] ) : 0;
   781 	$fwidth  = ! empty( $_REQUEST['fwidth'] ) ? intval( $_REQUEST['fwidth'] ) : 0;
   763 	$fheight = ! empty( $_REQUEST['fheight'] ) ? intval( $_REQUEST['fheight'] ) : 0;
   782 	$fheight = ! empty( $_REQUEST['fheight'] ) ? intval( $_REQUEST['fheight'] ) : 0;
   764 	$target  = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : '';
   783 	$target  = ! empty( $_REQUEST['target'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['target'] ) : '';
   765 	$scale   = ! empty( $_REQUEST['do'] ) && 'scale' == $_REQUEST['do'];
   784 	$scale   = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do'];
   766 
   785 
   767 	if ( $scale && $fwidth > 0 && $fheight > 0 ) {
   786 	if ( $scale && $fwidth > 0 && $fheight > 0 ) {
   768 		$size = $img->get_size();
   787 		$size = $img->get_size();
   769 		$sX   = $size['width'];
   788 		$sX   = $size['width'];
   770 		$sY   = $size['height'];
   789 		$sY   = $size['height'];
   814 	$suffix   = time() . rand( 100, 999 );
   833 	$suffix   = time() . rand( 100, 999 );
   815 
   834 
   816 	if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE &&
   835 	if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE &&
   817 		isset( $backup_sizes['full-orig'] ) && $backup_sizes['full-orig']['file'] != $basename ) {
   836 		isset( $backup_sizes['full-orig'] ) && $backup_sizes['full-orig']['file'] != $basename ) {
   818 
   837 
   819 		if ( 'thumbnail' == $target ) {
   838 		if ( 'thumbnail' === $target ) {
   820 			$new_path = "{$dirname}/{$filename}-temp.{$ext}";
   839 			$new_path = "{$dirname}/{$filename}-temp.{$ext}";
   821 		} else {
   840 		} else {
   822 			$new_path = $path;
   841 			$new_path = $path;
   823 		}
   842 		}
   824 	} else {
   843 	} else {
   864 
   883 
   865 		$size           = $img->get_size();
   884 		$size           = $img->get_size();
   866 		$meta['width']  = $size['width'];
   885 		$meta['width']  = $size['width'];
   867 		$meta['height'] = $size['height'];
   886 		$meta['height'] = $size['height'];
   868 
   887 
   869 		if ( $success && ( 'nothumb' == $target || 'all' == $target ) ) {
   888 		if ( $success && ( 'nothumb' === $target || 'all' === $target ) ) {
   870 			$sizes = get_intermediate_image_sizes();
   889 			$sizes = get_intermediate_image_sizes();
   871 			if ( 'nothumb' == $target ) {
   890 			if ( 'nothumb' === $target ) {
   872 				$sizes = array_diff( $sizes, array( 'thumbnail' ) );
   891 				$sizes = array_diff( $sizes, array( 'thumbnail' ) );
   873 			}
   892 			}
   874 		}
   893 		}
   875 
   894 
   876 		$return->fw = $meta['width'];
   895 		$return->fw = $meta['width'];
   877 		$return->fh = $meta['height'];
   896 		$return->fh = $meta['height'];
   878 	} elseif ( 'thumbnail' == $target ) {
   897 	} elseif ( 'thumbnail' === $target ) {
   879 		$sizes   = array( 'thumbnail' );
   898 		$sizes   = array( 'thumbnail' );
   880 		$success = $delete = $nocrop = true;
   899 		$success = true;
       
   900 		$delete  = true;
       
   901 		$nocrop  = true;
   881 	}
   902 	}
   882 
   903 
   883 	/*
   904 	/*
   884 	 * We need to remove any existing resized image files because
   905 	 * We need to remove any existing resized image files because
   885 	 * a new crop or rotate could generate different sizes (and hence, filenames),
   906 	 * a new crop or rotate could generate different sizes (and hence, filenames),
   938 
   959 
   939 	if ( $success ) {
   960 	if ( $success ) {
   940 		wp_update_attachment_metadata( $post_id, $meta );
   961 		wp_update_attachment_metadata( $post_id, $meta );
   941 		update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes );
   962 		update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes );
   942 
   963 
   943 		if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
   964 		if ( 'thumbnail' === $target || 'all' === $target || 'full' === $target ) {
   944 			// Check if it's an image edit from attachment edit screen
   965 			// Check if it's an image edit from attachment edit screen.
   945 			if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' == $_REQUEST['context'] ) {
   966 			if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' === $_REQUEST['context'] ) {
   946 				$thumb_url         = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
   967 				$thumb_url         = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
   947 				$return->thumbnail = $thumb_url[0];
   968 				$return->thumbnail = $thumb_url[0];
   948 			} else {
   969 			} else {
   949 				$file_url = wp_get_attachment_url( $post_id );
   970 				$file_url = wp_get_attachment_url( $post_id );
   950 				if ( ! empty( $meta['sizes']['thumbnail'] ) && $thumb = $meta['sizes']['thumbnail'] ) {
   971 				if ( ! empty( $meta['sizes']['thumbnail'] ) ) {
       
   972 					$thumb             = $meta['sizes']['thumbnail'];
   951 					$return->thumbnail = path_join( dirname( $file_url ), $thumb['file'] );
   973 					$return->thumbnail = path_join( dirname( $file_url ), $thumb['file'] );
   952 				} else {
   974 				} else {
   953 					$return->thumbnail = "$file_url?w=128&h=128";
   975 					$return->thumbnail = "$file_url?w=128&h=128";
   954 				}
   976 				}
   955 			}
   977 			}