wp/wp-admin/includes/image-edit.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     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; ?>">
    47 	<div class="imgedit-group">
    52 	<div class="imgedit-group">
    48 	<div class="imgedit-group-top">
    53 	<div class="imgedit-group-top">
    49 		<h2><?php _e( 'Scale Image' ); ?></h2>
    54 		<h2><?php _e( 'Scale Image' ); ?></h2>
    50 		<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>
    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>
    51 		<div class="imgedit-help">
    56 		<div class="imgedit-help">
    52 		<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>
    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>
    53 		</div>
    58 		</div>
    54 		<?php if ( isset( $meta['width'], $meta['height'] ) ): ?>
    59 		<?php if ( isset( $meta['width'], $meta['height'] ) ) : ?>
    55 		<p><?php printf( __('Original dimensions %s'), $meta['width'] . ' &times; ' . $meta['height'] ); ?></p>
    60 		<p><?php printf( __( 'Original dimensions %s' ), $meta['width'] . ' &times; ' . $meta['height'] ); ?></p>
    56 		<?php endif ?>
    61 		<?php endif ?>
    57 		<div class="imgedit-submit">
    62 		<div class="imgedit-submit">
    58 
    63 
    59 		<fieldset class="imgedit-scale">
    64 		<fieldset class="imgedit-scale">
    60 		<legend><?php _e( 'New dimensions:' ); ?></legend>
    65 		<legend><?php _e( 'New dimensions:' ); ?></legend>
    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>
   138 		</div>
   147 		</div>
   139 	</fieldset>
   148 	</fieldset>
   140 
   149 
   141 	</div>
   150 	</div>
   142 
   151 
   143 	<?php if ( $thumb && $sub_sizes ) {
   152 	<?php
       
   153 	if ( $thumb && $sub_sizes ) {
   144 		$thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
   154 		$thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
   145 	?>
   155 		?>
   146 
   156 
   147 	<div class="imgedit-group imgedit-applyto">
   157 	<div class="imgedit-group imgedit-applyto">
   148 	<div class="imgedit-group-top">
   158 	<div class="imgedit-group-top">
   149 		<h2><?php _e( 'Thumbnail Settings' ); ?></h2>
   159 		<h2><?php _e( 'Thumbnail Settings' ); ?></h2>
   150 		<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>
   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>
   151 		<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>
   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>
   152 	</div>
   162 	</div>
   153 
   163 
   154 	<figure class="imgedit-thumbnail-preview">
   164 	<figure class="imgedit-thumbnail-preview">
   155 		<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" />
   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" />
   156 		<figcaption class="imgedit-thumbnail-preview-caption"><?php _e( 'Current thumbnail' ); ?></figcaption>
   166 		<figcaption class="imgedit-thumbnail-preview-caption"><?php _e( 'Current thumbnail' ); ?></figcaption>
   160 	<fieldset>
   170 	<fieldset>
   161 		<legend><strong><?php _e( 'Apply changes to:' ); ?></strong></legend>
   171 		<legend><strong><?php _e( 'Apply changes to:' ); ?></strong></legend>
   162 
   172 
   163 		<label class="imgedit-label">
   173 		<label class="imgedit-label">
   164 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="all" checked="checked" />
   174 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="all" checked="checked" />
   165 		<?php _e('All image sizes'); ?></label>
   175 		<?php _e( 'All image sizes' ); ?></label>
   166 
   176 
   167 		<label class="imgedit-label">
   177 		<label class="imgedit-label">
   168 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="thumbnail" />
   178 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="thumbnail" />
   169 		<?php _e('Thumbnail'); ?></label>
   179 		<?php _e( 'Thumbnail' ); ?></label>
   170 
   180 
   171 		<label class="imgedit-label">
   181 		<label class="imgedit-label">
   172 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
   182 		<input type="radio" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
   173 		<?php _e('All sizes except thumbnail'); ?></label>
   183 		<?php _e( 'All sizes except thumbnail' ); ?></label>
   174 	</fieldset>
   184 	</fieldset>
   175 	</div>
   185 	</div>
   176 	</div>
   186 	</div>
   177 
   187 
   178 	<?php } ?>
   188 	<?php } ?>
   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>
   211 		<input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
   228 		<input type="hidden" id="imgedit-selection-<?php echo $post_id; ?>" value="" />
   212 		<input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
   229 		<input type="hidden" id="imgedit-x-<?php echo $post_id; ?>" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
   213 		<input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" />
   230 		<input type="hidden" id="imgedit-y-<?php echo $post_id; ?>" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" />
   214 
   231 
   215 		<div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
   232 		<div id="imgedit-crop-<?php echo $post_id; ?>" class="imgedit-crop-wrap">
   216 		<img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')" src="<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>?action=imgedit-preview&amp;_ajax_nonce=<?php echo $nonce; ?>&amp;postid=<?php echo $post_id; ?>&amp;rand=<?php echo rand(1, 99999); ?>" alt="" />
   233 		<img id="image-preview-<?php echo $post_id; ?>" onload="imageEdit.imgLoaded('<?php echo $post_id; ?>')" src="<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>?action=imgedit-preview&amp;_ajax_nonce=<?php echo $nonce; ?>&amp;postid=<?php echo $post_id; ?>&amp;rand=<?php echo rand( 1, 99999 ); ?>" alt="" />
   217 		</div>
   234 		</div>
   218 
   235 
   219 		<div class="imgedit-submit">
   236 		<div class="imgedit-submit">
   220 			<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button imgedit-cancel-btn" value="<?php esc_attr_e( 'Cancel' ); ?>" />
   237 			<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button imgedit-cancel-btn" value="<?php esc_attr_e( 'Cancel' ); ?>" />
   221 			<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' ); ?>" />
   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' ); ?>" />
   222 		</div>
   239 		</div>
   223 	</div>
   240 	</div>
   224 
   241 
   225 	</div>
   242 	</div>
   226 	<div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
   243 	<div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
   227 	<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>
   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>
   228 	</div>
   245 	</div>
   229 <?php
   246 	<?php
   230 }
   247 }
   231 
   248 
   232 /**
   249 /**
   233  * Streams image in WP_Image_Editor to browser.
   250  * Streams image in WP_Image_Editor to browser.
       
   251  *
       
   252  * @since 2.9.0
   234  *
   253  *
   235  * @param WP_Image_Editor $image         The image editor instance.
   254  * @param WP_Image_Editor $image         The image editor instance.
   236  * @param string          $mime_type     The mime type of the image.
   255  * @param string          $mime_type     The mime type of the image.
   237  * @param int             $attachment_id The image's attachment post ID.
   256  * @param int             $attachment_id The image's attachment post ID.
   238  * @return bool True on success, false on failure.
   257  * @return bool True on success, false on failure.
   248 		 * @param WP_Image_Editor $image         The image editor instance.
   267 		 * @param WP_Image_Editor $image         The image editor instance.
   249 		 * @param int             $attachment_id The attachment post ID.
   268 		 * @param int             $attachment_id The attachment post ID.
   250 		 */
   269 		 */
   251 		$image = apply_filters( 'image_editor_save_pre', $image, $attachment_id );
   270 		$image = apply_filters( 'image_editor_save_pre', $image, $attachment_id );
   252 
   271 
   253 		if ( is_wp_error( $image->stream( $mime_type ) ) )
   272 		if ( is_wp_error( $image->stream( $mime_type ) ) ) {
   254 			return false;
   273 			return false;
       
   274 		}
   255 
   275 
   256 		return true;
   276 		return true;
   257 	} else {
   277 	} else {
   258 		_deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) );
   278 		_deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) );
   259 
   279 
   283 		}
   303 		}
   284 	}
   304 	}
   285 }
   305 }
   286 
   306 
   287 /**
   307 /**
   288  * Saves Image to File
   308  * Saves image to file.
       
   309  *
       
   310  * @since 2.9.0
   289  *
   311  *
   290  * @param string $filename
   312  * @param string $filename
   291  * @param WP_Image_Editor $image
   313  * @param WP_Image_Editor $image
   292  * @param string $mime_type
   314  * @param string $mime_type
   293  * @param int $post_id
   315  * @param int $post_id
   313 		 * @param string          $mime_type Image mime type.
   335 		 * @param string          $mime_type Image mime type.
   314 		 * @param int             $post_id   Post ID.
   336 		 * @param int             $post_id   Post ID.
   315 		 */
   337 		 */
   316 		$saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id );
   338 		$saved = apply_filters( 'wp_save_image_editor_file', null, $filename, $image, $mime_type, $post_id );
   317 
   339 
   318 		if ( null !== $saved )
   340 		if ( null !== $saved ) {
   319 			return $saved;
   341 			return $saved;
       
   342 		}
   320 
   343 
   321 		return $image->save( $filename, $mime_type );
   344 		return $image->save( $filename, $mime_type );
   322 	} else {
   345 	} else {
   323 		_deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) );
   346 		_deprecated_argument( __FUNCTION__, '3.5.0', __( '$image needs to be an WP_Image_Editor object' ) );
   324 
   347 
   340 		 * @param string          $mime_type Image mime type.
   363 		 * @param string          $mime_type Image mime type.
   341 		 * @param int             $post_id   Post ID.
   364 		 * @param int             $post_id   Post ID.
   342 		 */
   365 		 */
   343 		$saved = apply_filters( 'wp_save_image_file', null, $filename, $image, $mime_type, $post_id );
   366 		$saved = apply_filters( 'wp_save_image_file', null, $filename, $image, $mime_type, $post_id );
   344 
   367 
   345 		if ( null !== $saved )
   368 		if ( null !== $saved ) {
   346 			return $saved;
   369 			return $saved;
       
   370 		}
   347 
   371 
   348 		switch ( $mime_type ) {
   372 		switch ( $mime_type ) {
   349 			case 'image/jpeg':
   373 			case 'image/jpeg':
   350 
       
   351 				/** This filter is documented in wp-includes/class-wp-image-editor.php */
   374 				/** This filter is documented in wp-includes/class-wp-image-editor.php */
   352 				return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
   375 				return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
   353 			case 'image/png':
   376 			case 'image/png':
   354 				return imagepng( $image, $filename );
   377 				return imagepng( $image, $filename );
   355 			case 'image/gif':
   378 			case 'image/gif':
   368  * @ignore
   391  * @ignore
   369  * @param int $w Image width in pixels.
   392  * @param int $w Image width in pixels.
   370  * @param int $h Image height in pixels.
   393  * @param int $h Image height in pixels.
   371  * @return float|int Image preview ratio.
   394  * @return float|int Image preview ratio.
   372  */
   395  */
   373 function _image_get_preview_ratio($w, $h) {
   396 function _image_get_preview_ratio( $w, $h ) {
   374 	$max = max($w, $h);
   397 	$max = max( $w, $h );
   375 	return $max > 400 ? (400 / $max) : 1;
   398 	return $max > 400 ? ( 400 / $max ) : 1;
   376 }
   399 }
   377 
   400 
   378 /**
   401 /**
   379  * Returns an image resource. Internal use only.
   402  * Returns an image resource. Internal use only.
   380  *
   403  *
   385  * @ignore
   408  * @ignore
   386  * @param resource  $img   Image resource.
   409  * @param resource  $img   Image resource.
   387  * @param float|int $angle Image rotation angle, in degrees.
   410  * @param float|int $angle Image rotation angle, in degrees.
   388  * @return resource|false GD image resource, false otherwise.
   411  * @return resource|false GD image resource, false otherwise.
   389  */
   412  */
   390 function _rotate_image_resource($img, $angle) {
   413 function _rotate_image_resource( $img, $angle ) {
   391 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
   414 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::rotate()' );
   392 	if ( function_exists('imagerotate') ) {
   415 	if ( function_exists( 'imagerotate' ) ) {
   393 		$rotated = imagerotate($img, $angle, 0);
   416 		$rotated = imagerotate( $img, $angle, 0 );
   394 		if ( is_resource($rotated) ) {
   417 		if ( is_resource( $rotated ) ) {
   395 			imagedestroy($img);
   418 			imagedestroy( $img );
   396 			$img = $rotated;
   419 			$img = $rotated;
   397 		}
   420 		}
   398 	}
   421 	}
   399 	return $img;
   422 	return $img;
   400 }
   423 }
   410  * @param resource $img  Image resource.
   433  * @param resource $img  Image resource.
   411  * @param bool     $horz Whether to flip horizontally.
   434  * @param bool     $horz Whether to flip horizontally.
   412  * @param bool     $vert Whether to flip vertically.
   435  * @param bool     $vert Whether to flip vertically.
   413  * @return resource (maybe) flipped image resource.
   436  * @return resource (maybe) flipped image resource.
   414  */
   437  */
   415 function _flip_image_resource($img, $horz, $vert) {
   438 function _flip_image_resource( $img, $horz, $vert ) {
   416 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
   439 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Image_Editor::flip()' );
   417 	$w = imagesx($img);
   440 	$w   = imagesx( $img );
   418 	$h = imagesy($img);
   441 	$h   = imagesy( $img );
   419 	$dst = wp_imagecreatetruecolor($w, $h);
   442 	$dst = wp_imagecreatetruecolor( $w, $h );
   420 	if ( is_resource($dst) ) {
   443 	if ( is_resource( $dst ) ) {
   421 		$sx = $vert ? ($w - 1) : 0;
   444 		$sx = $vert ? ( $w - 1 ) : 0;
   422 		$sy = $horz ? ($h - 1) : 0;
   445 		$sy = $horz ? ( $h - 1 ) : 0;
   423 		$sw = $vert ? -$w : $w;
   446 		$sw = $vert ? -$w : $w;
   424 		$sh = $horz ? -$h : $h;
   447 		$sh = $horz ? -$h : $h;
   425 
   448 
   426 		if ( imagecopyresampled($dst, $img, 0, 0, $sx, $sy, $w, $h, $sw, $sh) ) {
   449 		if ( imagecopyresampled( $dst, $img, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
   427 			imagedestroy($img);
   450 			imagedestroy( $img );
   428 			$img = $dst;
   451 			$img = $dst;
   429 		}
   452 		}
   430 	}
   453 	}
   431 	return $img;
   454 	return $img;
   432 }
   455 }
   442  * @param float    $y   Source point y-cooredinate.
   465  * @param float    $y   Source point y-cooredinate.
   443  * @param float    $w   Source width.
   466  * @param float    $w   Source width.
   444  * @param float    $h   Source height.
   467  * @param float    $h   Source height.
   445  * @return resource (maybe) cropped image resource.
   468  * @return resource (maybe) cropped image resource.
   446  */
   469  */
   447 function _crop_image_resource($img, $x, $y, $w, $h) {
   470 function _crop_image_resource( $img, $x, $y, $w, $h ) {
   448 	$dst = wp_imagecreatetruecolor($w, $h);
   471 	$dst = wp_imagecreatetruecolor( $w, $h );
   449 	if ( is_resource($dst) ) {
   472 	if ( is_resource( $dst ) ) {
   450 		if ( imagecopy($dst, $img, 0, 0, $x, $y, $w, $h) ) {
   473 		if ( imagecopy( $dst, $img, 0, 0, $x, $y, $w, $h ) ) {
   451 			imagedestroy($img);
   474 			imagedestroy( $img );
   452 			$img = $dst;
   475 			$img = $dst;
   453 		}
   476 		}
   454 	}
   477 	}
   455 	return $img;
   478 	return $img;
   456 }
   479 }
   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 
   520 		 * Filters the WP_Image_Editor instance before applying changes to the image.
   546 		 * Filters the WP_Image_Editor instance before applying changes to the image.
   521 		 *
   547 		 *
   522 		 * @since 3.5.0
   548 		 * @since 3.5.0
   523 		 *
   549 		 *
   524 		 * @param WP_Image_Editor $image   WP_Image_Editor instance.
   550 		 * @param WP_Image_Editor $image   WP_Image_Editor instance.
   525  		 * @param array           $changes Array of change operations.
   551 		 * @param array           $changes Array of change operations.
   526 		 */
   552 		 */
   527 		$image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
   553 		$image = apply_filters( 'wp_image_editor_before_change', $image, $changes );
   528 	} elseif ( is_resource( $image ) ) {
   554 	} elseif ( is_resource( $image ) ) {
   529 
   555 
   530 		/**
   556 		/**
   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
   580 
   609 
   581 /**
   610 /**
   582  * Streams image in post to browser, along with enqueued changes
   611  * Streams image in post to browser, along with enqueued changes
   583  * in $_REQUEST['history']
   612  * in $_REQUEST['history']
   584  *
   613  *
       
   614  * @since 2.9.0
       
   615  *
   585  * @param int $post_id
   616  * @param int $post_id
   586  * @return bool
   617  * @return bool
   587  */
   618  */
   588 function stream_preview_image( $post_id ) {
   619 function stream_preview_image( $post_id ) {
   589 	$post = get_post( $post_id );
   620 	$post = get_post( $post_id );
   594 
   625 
   595 	if ( is_wp_error( $img ) ) {
   626 	if ( is_wp_error( $img ) ) {
   596 		return false;
   627 		return false;
   597 	}
   628 	}
   598 
   629 
   599 	$changes = !empty($_REQUEST['history']) ? json_decode( wp_unslash($_REQUEST['history']) ) : null;
   630 	$changes = ! empty( $_REQUEST['history'] ) ? json_decode( wp_unslash( $_REQUEST['history'] ) ) : null;
   600 	if ( $changes )
   631 	if ( $changes ) {
   601 		$img = image_edit_apply_changes( $img, $changes );
   632 		$img = image_edit_apply_changes( $img, $changes );
       
   633 	}
   602 
   634 
   603 	// Scale the image.
   635 	// Scale the image.
   604 	$size = $img->get_size();
   636 	$size = $img->get_size();
   605 	$w = $size['width'];
   637 	$w    = $size['width'];
   606 	$h = $size['height'];
   638 	$h    = $size['height'];
   607 
   639 
   608 	$ratio = _image_get_preview_ratio( $w, $h );
   640 	$ratio = _image_get_preview_ratio( $w, $h );
   609 	$w2 = max ( 1, $w * $ratio );
   641 	$w2    = max( 1, $w * $ratio );
   610 	$h2 = max ( 1, $h * $ratio );
   642 	$h2    = max( 1, $h * $ratio );
   611 
   643 
   612 	if ( is_wp_error( $img->resize( $w2, $h2 ) ) )
   644 	if ( is_wp_error( $img->resize( $w2, $h2 ) ) ) {
   613 		return false;
   645 		return false;
       
   646 	}
   614 
   647 
   615 	return wp_stream_image( $img, $post->post_mime_type, $post_id );
   648 	return wp_stream_image( $img, $post->post_mime_type, $post_id );
   616 }
   649 }
   617 
   650 
   618 /**
   651 /**
   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;
   807 		} else {
   850 		} else {
   808 			$tag = 'full-orig';
   851 			$tag = 'full-orig';
   809 		}
   852 		}
   810 
   853 
   811 		if ( $tag ) {
   854 		if ( $tag ) {
   812 			$backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $basename );
   855 			$backup_sizes[ $tag ] = array(
       
   856 				'width'  => $meta['width'],
       
   857 				'height' => $meta['height'],
       
   858 				'file'   => $basename,
       
   859 			);
   813 		}
   860 		}
   814 		$success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );
   861 		$success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );
   815 
   862 
   816 		$meta['file'] = _wp_relative_upload_path( $new_path );
   863 		$meta['file'] = _wp_relative_upload_path( $new_path );
   817 
   864 
   818 		$size = $img->get_size();
   865 		$size           = $img->get_size();
   819 		$meta['width'] = $size['width'];
   866 		$meta['width']  = $size['width'];
   820 		$meta['height'] = $size['height'];
   867 		$meta['height'] = $size['height'];
   821 
   868 
   822 		if ( $success && ('nothumb' == $target || 'all' == $target) ) {
   869 		if ( $success && ( 'nothumb' == $target || 'all' == $target ) ) {
   823 			$sizes = get_intermediate_image_sizes();
   870 			$sizes = get_intermediate_image_sizes();
   824 			if ( 'nothumb' == $target )
   871 			if ( 'nothumb' == $target ) {
   825 				$sizes = array_diff( $sizes, array('thumbnail') );
   872 				$sizes = array_diff( $sizes, array( 'thumbnail' ) );
       
   873 			}
   826 		}
   874 		}
   827 
   875 
   828 		$return->fw = $meta['width'];
   876 		$return->fw = $meta['width'];
   829 		$return->fh = $meta['height'];
   877 		$return->fh = $meta['height'];
   830 	} elseif ( 'thumbnail' == $target ) {
   878 	} elseif ( 'thumbnail' == $target ) {
   831 		$sizes = array( 'thumbnail' );
   879 		$sizes   = array( 'thumbnail' );
   832 		$success = $delete = $nocrop = true;
   880 		$success = $delete = $nocrop = true;
   833 	}
   881 	}
   834 
   882 
   835 	/*
   883 	/*
   836 	 * We need to remove any existing resized image files because
   884 	 * We need to remove any existing resized image files because
   850 	if ( isset( $sizes ) ) {
   898 	if ( isset( $sizes ) ) {
   851 		$_sizes = array();
   899 		$_sizes = array();
   852 
   900 
   853 		foreach ( $sizes as $size ) {
   901 		foreach ( $sizes as $size ) {
   854 			$tag = false;
   902 			$tag = false;
   855 			if ( isset( $meta['sizes'][$size] ) ) {
   903 			if ( isset( $meta['sizes'][ $size ] ) ) {
   856 				if ( isset($backup_sizes["$size-orig"]) ) {
   904 				if ( isset( $backup_sizes[ "$size-orig" ] ) ) {
   857 					if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes["$size-orig"]['file'] != $meta['sizes'][$size]['file'] )
   905 					if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) && $backup_sizes[ "$size-orig" ]['file'] != $meta['sizes'][ $size ]['file'] ) {
   858 						$tag = "$size-$suffix";
   906 						$tag = "$size-$suffix";
       
   907 					}
   859 				} else {
   908 				} else {
   860 					$tag = "$size-orig";
   909 					$tag = "$size-orig";
   861 				}
   910 				}
   862 
   911 
   863 				if ( $tag )
   912 				if ( $tag ) {
   864 					$backup_sizes[$tag] = $meta['sizes'][$size];
   913 					$backup_sizes[ $tag ] = $meta['sizes'][ $size ];
       
   914 				}
   865 			}
   915 			}
   866 
   916 
   867 			if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
   917 			if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
   868 				$width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
   918 				$width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
   869 				$height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
   919 				$height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
   872 				$height = get_option( "{$size}_size_h" );
   922 				$height = get_option( "{$size}_size_h" );
   873 				$width  = get_option( "{$size}_size_w" );
   923 				$width  = get_option( "{$size}_size_w" );
   874 				$crop   = ( $nocrop ) ? false : get_option( "{$size}_crop" );
   924 				$crop   = ( $nocrop ) ? false : get_option( "{$size}_crop" );
   875 			}
   925 			}
   876 
   926 
   877 			$_sizes[ $size ] = array( 'width' => $width, 'height' => $height, 'crop' => $crop );
   927 			$_sizes[ $size ] = array(
       
   928 				'width'  => $width,
       
   929 				'height' => $height,
       
   930 				'crop'   => $crop,
       
   931 			);
   878 		}
   932 		}
   879 
   933 
   880 		$meta['sizes'] = array_merge( $meta['sizes'], $img->multi_resize( $_sizes ) );
   934 		$meta['sizes'] = array_merge( $meta['sizes'], $img->multi_resize( $_sizes ) );
   881 	}
   935 	}
   882 
   936 
   883 	unset( $img );
   937 	unset( $img );
   884 
   938 
   885 	if ( $success ) {
   939 	if ( $success ) {
   886 		wp_update_attachment_metadata( $post_id, $meta );
   940 		wp_update_attachment_metadata( $post_id, $meta );
   887 		update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes);
   941 		update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes );
   888 
   942 
   889 		if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
   943 		if ( $target == 'thumbnail' || $target == 'all' || $target == 'full' ) {
   890 			// Check if it's an image edit from attachment edit screen
   944 			// Check if it's an image edit from attachment edit screen
   891 			if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' == $_REQUEST['context'] ) {
   945 			if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' == $_REQUEST['context'] ) {
   892 				$thumb_url = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
   946 				$thumb_url         = wp_get_attachment_image_src( $post_id, array( 900, 600 ), true );
   893 				$return->thumbnail = $thumb_url[0];
   947 				$return->thumbnail = $thumb_url[0];
   894 			} else {
   948 			} else {
   895 				$file_url = wp_get_attachment_url($post_id);
   949 				$file_url = wp_get_attachment_url( $post_id );
   896 				if ( ! empty( $meta['sizes']['thumbnail'] ) && $thumb = $meta['sizes']['thumbnail'] ) {
   950 				if ( ! empty( $meta['sizes']['thumbnail'] ) && $thumb = $meta['sizes']['thumbnail'] ) {
   897 					$return->thumbnail = path_join( dirname($file_url), $thumb['file'] );
   951 					$return->thumbnail = path_join( dirname( $file_url ), $thumb['file'] );
   898 				} else {
   952 				} else {
   899 					$return->thumbnail = "$file_url?w=128&h=128";
   953 					$return->thumbnail = "$file_url?w=128&h=128";
   900 				}
   954 				}
   901 			}
   955 			}
   902 		}
   956 		}
   906 
   960 
   907 	if ( $delete ) {
   961 	if ( $delete ) {
   908 		wp_delete_file( $new_path );
   962 		wp_delete_file( $new_path );
   909 	}
   963 	}
   910 
   964 
   911 	$return->msg = esc_js( __('Image saved') );
   965 	$return->msg = esc_js( __( 'Image saved' ) );
   912 	return $return;
   966 	return $return;
   913 }
   967 }