webp-uploads
Last commit date
.wordpress-org
2 years ago
.gitattributes
2 years ago
can-load.php
2 years ago
fallback.js
3 years ago
helper.php
2 years ago
hooks.php
2 years ago
image-edit.php
2 years ago
load.php
2 years ago
readme.txt
2 years ago
rest-api.php
2 years ago
settings.php
2 years ago
image-edit.php
439 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Edit images integration for the module, including backup and restore support. |
| 4 | * |
| 5 | * @package webp-uploads |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; // Exit if accessed directly. |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Adds sources to metadata for an attachment. |
| 15 | * |
| 16 | * @since 1.0.0 |
| 17 | * |
| 18 | * @param array $metadata Metadata of the attachment. |
| 19 | * @param array $valid_mime_transforms List of valid mime transforms for current image mime type. |
| 20 | * @param array $main_images Path of all main image files of all mime types. |
| 21 | * @param array $subsized_images Path of all subsized image file of all mime types. |
| 22 | * @return array Metadata with sources added. |
| 23 | */ |
| 24 | function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_images, $subsized_images ) { |
| 25 | foreach ( $valid_mime_transforms as $targeted_mime ) { |
| 26 | // Make sure the path and file exists as those values are required. |
| 27 | $image_directory = null; |
| 28 | if ( isset( $main_images[ $targeted_mime ]['path'], $main_images[ $targeted_mime ]['file'] ) && file_exists( $main_images[ $targeted_mime ]['path'] ) ) { |
| 29 | // Add sources to original image metadata. |
| 30 | $metadata['sources'][ $targeted_mime ] = array( |
| 31 | 'file' => $main_images[ $targeted_mime ]['file'], |
| 32 | 'filesize' => wp_filesize( $main_images[ $targeted_mime ]['path'] ), |
| 33 | ); |
| 34 | $image_directory = pathinfo( $main_images[ $targeted_mime ]['path'], PATHINFO_DIRNAME ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * If no original image was provided the image_directory can't be determined, in that scenario try to |
| 39 | * find it from the `file` property. |
| 40 | * |
| 41 | * @see get_attached_file() |
| 42 | */ |
| 43 | if ( |
| 44 | null === $image_directory |
| 45 | && isset( $metadata['file'] ) |
| 46 | && 0 !== strpos( $metadata['file'], '/' ) |
| 47 | && ':\\' !== substr( $metadata['file'], 1, 2 ) |
| 48 | ) { |
| 49 | $uploads = wp_get_upload_dir(); |
| 50 | if ( false === $uploads['error'] && isset( $uploads['basedir'] ) ) { |
| 51 | $file = path_join( $uploads['basedir'], $metadata['file'] ); |
| 52 | if ( file_exists( $file ) ) { |
| 53 | $image_directory = pathinfo( $file, PATHINFO_DIRNAME ); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if ( null === $image_directory ) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | foreach ( $metadata['sizes'] as $size_name => $size_details ) { |
| 63 | if ( empty( $subsized_images[ $targeted_mime ][ $size_name ]['file'] ) ) { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | // Add sources to resized image metadata. |
| 68 | $subsize_path = path_join( $image_directory, $subsized_images[ $targeted_mime ][ $size_name ]['file'] ); |
| 69 | if ( ! file_exists( $subsize_path ) ) { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | $metadata['sizes'][ $size_name ]['sources'][ $targeted_mime ] = array( |
| 74 | 'file' => $subsized_images[ $targeted_mime ][ $size_name ]['file'], |
| 75 | 'filesize' => wp_filesize( $subsize_path ), |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return $metadata; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Creates additional image formats when original image is edited. |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | * |
| 88 | * @param bool|null $override Value to return instead of saving. Default null. |
| 89 | * @param string $file_path Name of the file to be saved. |
| 90 | * @param WP_Image_Editor $editor The image editor instance. |
| 91 | * @param string $mime_type The mime type of the image. |
| 92 | * @param int $post_id Attachment post ID. |
| 93 | * @return bool|null Potentially modified $override value. |
| 94 | */ |
| 95 | function webp_uploads_update_image_onchange( $override, $file_path, $editor, $mime_type, $post_id ) { |
| 96 | if ( null !== $override ) { |
| 97 | return $override; |
| 98 | } |
| 99 | |
| 100 | $transforms = webp_uploads_get_upload_image_mime_transforms(); |
| 101 | if ( empty( $transforms[ $mime_type ] ) ) { |
| 102 | return $override; |
| 103 | } |
| 104 | |
| 105 | $mime_transforms = $transforms[ $mime_type ]; |
| 106 | // This variable allows to unhook the logic from within the closure without the need for a function name. |
| 107 | $callback_executed = false; |
| 108 | add_filter( |
| 109 | 'wp_update_attachment_metadata', |
| 110 | static function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $editor, $mime_transforms, &$callback_executed ) { |
| 111 | if ( $post_meta_id !== $post_id ) { |
| 112 | return $metadata; |
| 113 | } |
| 114 | |
| 115 | // This callback was already executed for this post, nothing to do at this point. |
| 116 | if ( $callback_executed ) { |
| 117 | return $metadata; |
| 118 | } |
| 119 | $callback_executed = true; |
| 120 | // No sizes to be created. |
| 121 | if ( empty( $metadata['sizes'] ) ) { |
| 122 | return $metadata; |
| 123 | } |
| 124 | |
| 125 | $old_metadata = wp_get_attachment_metadata( $post_id ); |
| 126 | $resize_sizes = array(); |
| 127 | // PHPCS ignore reason: A nonce check is not necessary here as this logic directly ties in with WordPress core |
| 128 | // function `wp_ajax_image_editor()` which already has one. |
| 129 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 130 | $target = isset( $_REQUEST['target'] ) ? sanitize_key( $_REQUEST['target'] ) : 'all'; |
| 131 | |
| 132 | foreach ( $old_metadata['sizes'] as $size_name => $size_details ) { |
| 133 | // If the target is 'nothumb', skip generating the 'thumbnail' size. |
| 134 | if ( webp_uploads_image_edit_thumbnails_separately() && 'nothumb' === $target && 'thumbnail' === $size_name ) { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | if ( isset( $metadata['sizes'][ $size_name ] ) && ! empty( $metadata['sizes'][ $size_name ] ) && |
| 139 | $metadata['sizes'][ $size_name ]['file'] !== $old_metadata['sizes'][ $size_name ]['file'] ) { |
| 140 | $resize_sizes[ $size_name ] = $metadata['sizes'][ $size_name ]; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | $allowed_mimes = array_flip( wp_get_mime_types() ); |
| 145 | $original_directory = pathinfo( $file_path, PATHINFO_DIRNAME ); |
| 146 | $filename = pathinfo( $file_path, PATHINFO_FILENAME ); |
| 147 | $main_images = array(); |
| 148 | $subsized_images = array(); |
| 149 | |
| 150 | foreach ( $mime_transforms as $targeted_mime ) { |
| 151 | if ( $targeted_mime === $mime_type ) { |
| 152 | // If the target is `thumbnail` make sure it is the only selected size. |
| 153 | if ( webp_uploads_image_edit_thumbnails_separately() && 'thumbnail' === $target ) { |
| 154 | if ( isset( $metadata['sizes']['thumbnail'] ) ) { |
| 155 | $subsized_images[ $targeted_mime ] = array( 'thumbnail' => $metadata['sizes']['thumbnail'] ); |
| 156 | } |
| 157 | // When the targeted thumbnail is selected no additional size and subsize is set. |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | $main_images[ $targeted_mime ] = array( |
| 162 | 'path' => $file_path, |
| 163 | 'file' => pathinfo( $file_path, PATHINFO_BASENAME ), |
| 164 | ); |
| 165 | $subsized_images[ $targeted_mime ] = $metadata['sizes']; |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) { |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | if ( ! $editor::supports_mime_type( $targeted_mime ) ) { |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); |
| 178 | $extension = $extension[0]; |
| 179 | |
| 180 | // If the target is `thumbnail` make sure only that size is generated. |
| 181 | if ( webp_uploads_image_edit_thumbnails_separately() && 'thumbnail' === $target ) { |
| 182 | if ( ! isset( $subsized_images[ $mime_type ]['thumbnail']['file'] ) ) { |
| 183 | continue; |
| 184 | } |
| 185 | $thumbnail_file = $subsized_images[ $mime_type ]['thumbnail']['file']; |
| 186 | $image_path = path_join( $original_directory, $thumbnail_file ); |
| 187 | $editor = wp_get_image_editor( $image_path, array( 'mime_type' => $targeted_mime ) ); |
| 188 | |
| 189 | if ( is_wp_error( $editor ) ) { |
| 190 | continue; |
| 191 | } |
| 192 | |
| 193 | $current_extension = pathinfo( $thumbnail_file, PATHINFO_EXTENSION ); |
| 194 | // Create a file with then new extension out of the targeted file. |
| 195 | $target_file_name = preg_replace( "/\.$current_extension$/", ".$extension", $thumbnail_file ); |
| 196 | $target_file_location = path_join( $original_directory, $target_file_name ); |
| 197 | |
| 198 | remove_filter( 'image_editor_output_format', 'webp_uploads_filter_image_editor_output_format', 10, 3 ); |
| 199 | $result = $editor->save( $target_file_location, $targeted_mime ); |
| 200 | add_filter( 'image_editor_output_format', 'webp_uploads_filter_image_editor_output_format', 10, 3 ); |
| 201 | |
| 202 | if ( is_wp_error( $result ) ) { |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | $subsized_images[ $targeted_mime ] = array( 'thumbnail' => $result ); |
| 207 | } else { |
| 208 | $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; |
| 209 | |
| 210 | remove_filter( 'image_editor_output_format', 'webp_uploads_filter_image_editor_output_format', 10, 3 ); |
| 211 | $result = $editor->save( $destination, $targeted_mime ); |
| 212 | add_filter( 'image_editor_output_format', 'webp_uploads_filter_image_editor_output_format', 10, 3 ); |
| 213 | |
| 214 | if ( is_wp_error( $result ) ) { |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | $main_images[ $targeted_mime ] = $result; |
| 219 | $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return webp_uploads_update_sources( $metadata, $mime_transforms, $main_images, $subsized_images ); |
| 224 | }, |
| 225 | 10, |
| 226 | 2 |
| 227 | ); |
| 228 | |
| 229 | return $override; |
| 230 | } |
| 231 | add_filter( 'wp_save_image_editor_file', 'webp_uploads_update_image_onchange', 10, 5 ); |
| 232 | |
| 233 | /** |
| 234 | * Inspect if the current call to `wp_update_attachment_metadata()` was done from within the context |
| 235 | * of an edit to an attachment either restore or other type of edit, in that case we perform operations |
| 236 | * to save the sources properties, specifically for the `full` size image due this is a virtual image size. |
| 237 | * |
| 238 | * @since 1.0.0 |
| 239 | * |
| 240 | * @see wp_update_attachment_metadata() |
| 241 | * |
| 242 | * @param array $data The current metadata of the attachment. |
| 243 | * @param int $attachment_id The ID of the current attachment. |
| 244 | * @return array The updated metadata for the attachment to be stored in the meta table. |
| 245 | */ |
| 246 | function webp_uploads_update_attachment_metadata( $data, $attachment_id ) { |
| 247 | // PHPCS ignore reason: Update the attachment's metadata by either restoring or editing it. |
| 248 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 249 | $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 10 ); |
| 250 | |
| 251 | foreach ( $trace as $element ) { |
| 252 | if ( ! isset( $element['function'] ) ) { |
| 253 | continue; |
| 254 | } |
| 255 | |
| 256 | switch ( $element['function'] ) { |
| 257 | case 'wp_save_image': |
| 258 | // Right after an image has been edited. |
| 259 | return webp_uploads_backup_sources( $attachment_id, $data ); |
| 260 | case 'wp_restore_image': |
| 261 | // When an image has been restored. |
| 262 | $data = webp_uploads_backup_sources( $attachment_id, $data ); |
| 263 | return webp_uploads_restore_image( $attachment_id, $data ); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return $data; |
| 268 | } |
| 269 | add_filter( 'wp_update_attachment_metadata', 'webp_uploads_update_attachment_metadata', 10, 2 ); |
| 270 | |
| 271 | /** |
| 272 | * Before saving the metadata of the image store a backup values for the sources and file property |
| 273 | * those files would be used and deleted by the backup mechanism, right after the metadata has |
| 274 | * been updated. It removes the current sources property due once this function is executed |
| 275 | * right after an edit has taken place and the current sources are no longer accurate. |
| 276 | * |
| 277 | * @since 1.0.0 |
| 278 | * |
| 279 | * @param int $attachment_id The ID representing the attachment. |
| 280 | * @param array $data The current metadata of the attachment. |
| 281 | * @return array The updated metadata for the attachment. |
| 282 | */ |
| 283 | function webp_uploads_backup_sources( $attachment_id, $data ) { |
| 284 | // PHPCS ignore reason: A nonce check is not necessary here as this logic directly ties in with WordPress core |
| 285 | // function `wp_ajax_image_editor()` which already has one. |
| 286 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 287 | $target = isset( $_REQUEST['target'] ) ? sanitize_key( $_REQUEST['target'] ) : 'all'; |
| 288 | |
| 289 | // When an edit to an image is only applied to a thumbnail there's nothing we need to back up. |
| 290 | if ( webp_uploads_image_edit_thumbnails_separately() && 'thumbnail' === $target ) { |
| 291 | return $data; |
| 292 | } |
| 293 | |
| 294 | $metadata = wp_get_attachment_metadata( $attachment_id ); |
| 295 | // Nothing to back up. |
| 296 | if ( ! isset( $metadata['sources'] ) ) { |
| 297 | return $data; |
| 298 | } |
| 299 | |
| 300 | $sources = $metadata['sources']; |
| 301 | // Prevent execution of the callbacks more than once if the callback was already executed. |
| 302 | $has_been_processed = false; |
| 303 | |
| 304 | $hook = static function ( $meta_id, $post_id, $meta_name ) use ( $attachment_id, $sources, &$has_been_processed ) { |
| 305 | // Make sure this hook is only executed in the same context for the provided $attachment_id. |
| 306 | if ( $post_id !== $attachment_id ) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | // This logic should work only if we are looking at the meta key: `_wp_attachment_backup_sizes`. |
| 311 | if ( '_wp_attachment_backup_sizes' !== $meta_name ) { |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | if ( $has_been_processed ) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | $has_been_processed = true; |
| 320 | webp_uploads_backup_full_image_sources( $post_id, $sources ); |
| 321 | }; |
| 322 | |
| 323 | add_action( 'added_post_meta', $hook, 10, 3 ); |
| 324 | add_action( 'updated_post_meta', $hook, 10, 3 ); |
| 325 | |
| 326 | // Remove the current sources as at this point the current values are no longer accurate. |
| 327 | // TODO: Requires to be updated from https://github.com/WordPress/performance/issues/158. |
| 328 | unset( $data['sources'] ); |
| 329 | |
| 330 | return $data; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Stores the provided sources for the attachment ID in the `_wp_attachment_backup_sources` with |
| 335 | * the next available target if target is `null` no source would be stored. |
| 336 | * |
| 337 | * @since 1.0.0 |
| 338 | * |
| 339 | * @param int $attachment_id The ID of the attachment. |
| 340 | * @param array $sources An array with the full sources to be stored on the next available key. |
| 341 | */ |
| 342 | function webp_uploads_backup_full_image_sources( $attachment_id, $sources ) { |
| 343 | if ( empty( $sources ) ) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | $target = webp_uploads_get_next_full_size_key_from_backup( $attachment_id ); |
| 348 | if ( null === $target ) { |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | $backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ); |
| 353 | $backup_sources = is_array( $backup_sources ) ? $backup_sources : array(); |
| 354 | $backup_sources[ $target ] = $sources; |
| 355 | // Store the `sources` property into the full size if present. |
| 356 | update_post_meta( $attachment_id, '_wp_attachment_backup_sources', $backup_sources ); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * It finds the next available `full-{orig or hash}` key on the images if the name |
| 361 | * has not been used as part of the backup sources it would be used if no size is |
| 362 | * found or backup exists `null` would be returned instead. |
| 363 | * |
| 364 | * @since 1.0.0 |
| 365 | * |
| 366 | * @param int $attachment_id The ID of the attachment. |
| 367 | * @return null|string The next available full size name. |
| 368 | */ |
| 369 | function webp_uploads_get_next_full_size_key_from_backup( $attachment_id ) { |
| 370 | $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); |
| 371 | $backup_sizes = is_array( $backup_sizes ) ? $backup_sizes : array(); |
| 372 | |
| 373 | if ( empty( $backup_sizes ) ) { |
| 374 | return null; |
| 375 | } |
| 376 | |
| 377 | $backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ); |
| 378 | $backup_sources = is_array( $backup_sources ) ? $backup_sources : array(); |
| 379 | foreach ( array_keys( $backup_sizes ) as $size_name ) { |
| 380 | // If the target already has the sources attributes find the next one. |
| 381 | if ( isset( $backup_sources[ $size_name ] ) ) { |
| 382 | continue; |
| 383 | } |
| 384 | |
| 385 | // We are only interested in the `full-` sizes. |
| 386 | if ( strpos( $size_name, 'full-' ) === false ) { |
| 387 | continue; |
| 388 | } |
| 389 | |
| 390 | return $size_name; |
| 391 | } |
| 392 | |
| 393 | return null; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Restore an image from the backup sizes, the current hook moves the `sources` from the `full-orig` key into |
| 398 | * the top level `sources` into the metadata, in order to ensure the restore process has a reference to the right |
| 399 | * images. |
| 400 | * |
| 401 | * @since 1.0.0 |
| 402 | * |
| 403 | * @param int $attachment_id The ID of the attachment. |
| 404 | * @param array $data The current metadata to be stored in the attachment. |
| 405 | * @return array The updated metadata of the attachment. |
| 406 | */ |
| 407 | function webp_uploads_restore_image( $attachment_id, $data ) { |
| 408 | $backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ); |
| 409 | if ( ! is_array( $backup_sources ) ) { |
| 410 | $backup_sources = array(); |
| 411 | } |
| 412 | |
| 413 | if ( ! isset( $backup_sources['full-orig'] ) || ! is_array( $backup_sources['full-orig'] ) ) { |
| 414 | return $data; |
| 415 | } |
| 416 | |
| 417 | // TODO: Handle the case If `IMAGE_EDIT_OVERWRITE` is defined and is truthy remove any edited images if present before replacing the metadata. |
| 418 | // See: https://github.com/WordPress/performance/issues/158. |
| 419 | $data['sources'] = $backup_sources['full-orig']; |
| 420 | |
| 421 | return $data; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Compatibility function to check whether editing image thumbnails separately is enabled. |
| 426 | * |
| 427 | * The filter {@see 'image_edit_thumbnails_separately'} was introduced in WordPress 6.3 with default value of `false`, |
| 428 | * for a behavior that previously was always enabled. |
| 429 | * |
| 430 | * @since 2.6.0 |
| 431 | * @see https://core.trac.wordpress.org/ticket/57685 |
| 432 | * |
| 433 | * @return bool True if editing image thumbnails is enabled, false otherwise. |
| 434 | */ |
| 435 | function webp_uploads_image_edit_thumbnails_separately() { |
| 436 | /** This filter is documented in wp-admin/includes/image-edit.php */ |
| 437 | return (bool) apply_filters( 'image_edit_thumbnails_separately', false ); |
| 438 | } |
| 439 |