| @@ -1,56 +1,112 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); | |
| 2 | +defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 3 | 3 | |
| 4 | +add_action( 'delete_attachment', 'imagify_trigger_delete_attachment_hook' ); | |
| 4 | 5 | /** |
| 5 | - * Auto-optimize when a new attachment is generated | |
| 6 | + * Trigger a common Imagify hook when an attachment is deleted. | |
| 6 | 7 | * |
| 7 | - * @since 1.0 | |
| 8 | - * @since 1.5 Async job | |
| 8 | + * @since 1.9 | |
| 9 | + * @author Grégory Viguier | |
| 10 | + * | |
| 11 | + * @param int $post_id Attachment ID. | |
| 9 | 12 | */ |
| 10 | -add_filter( 'wp_generate_attachment_metadata', '_imagify_optimize_attachment', PHP_INT_MAX, 2 ); | |
| 11 | -function _imagify_optimize_attachment( $metadata, $attachment_id ) { | |
| 12 | - $api_key = get_imagify_option( 'api_key', false ); | |
| 13 | +function imagify_trigger_delete_attachment_hook( $post_id ) { | |
| 14 | + $process = imagify_get_optimization_process( $post_id, 'wp' ); | |
| 13 | 15 | |
| 14 | - if ( ! empty( $api_key ) && get_imagify_option( 'auto_optimize', false ) ) { | |
| 15 | - $context = 'wp'; | |
| 16 | - $action = 'imagify_async_optimize_upload_new_media'; | |
| 17 | - $_ajax_nonce = wp_create_nonce( 'new_media-' . $attachment_id ); | |
| 16 | + if ( ! $process->is_valid() ) { | |
| 17 | + return; | |
| 18 | + } | |
| 18 | 19 | |
| 19 | - imagify_do_async_job( compact( 'action', '_ajax_nonce', 'metadata', 'attachment_id', 'context' ) ); | |
| 20 | + imagify_trigger_delete_media_hook( $process ); | |
| 21 | +} | |
| 22 | + | |
| 23 | +add_action( 'imagify_delete_media', 'imagify_cleanup_after_media_deletion' ); | |
| 24 | +/** | |
| 25 | + * Delete the backup file and the next-gen files when an attachement is deleted. | |
| 26 | + * | |
| 27 | + * @since 1.9 | |
| 28 | + * @author Grégory Viguier | |
| 29 | + * | |
| 30 | + * @param ProcessInterface $process An optimization process. | |
| 31 | + */ | |
| 32 | +function imagify_cleanup_after_media_deletion( $process ) { | |
| 33 | + if ( 'wp' !== $process->get_media()->get_context() ) { | |
| 34 | + return; | |
| 20 | 35 | } |
| 21 | 36 | |
| 22 | - return $metadata; | |
| 37 | + /** | |
| 38 | + * The optimization data will be automatically deleted by WP (post metas). | |
| 39 | + * Delete the Nextgen versions and the backup file. | |
| 40 | + */ | |
| 41 | + $process->delete_nextgen_files( false, true ); | |
| 42 | + | |
| 43 | + $process->delete_backup(); | |
| 23 | 44 | } |
| 24 | 45 | |
| 46 | +add_filter( 'ext2type', 'imagify_add_avif_type' ); | |
| 25 | 47 | /** |
| 26 | - * Delete the backup file when an attachement is deleted. | |
| 48 | + * Add the AVIF extension to wp_get_ext_types(). | |
| 27 | 49 | * |
| 28 | - * @since 1.0 | |
| 50 | + * @since 1.9 | |
| 51 | + * @author Grégory Viguier | |
| 52 | + * | |
| 53 | + * @param array $ext2type Multi-dimensional array with extensions for a default set of file types. | |
| 54 | + * @return array | |
| 29 | 55 | */ |
| 30 | -add_action( 'delete_attachment', '_imagify_delete_backup_file' ); | |
| 31 | -function _imagify_delete_backup_file( $post_id ) { | |
| 32 | - $attachment = new Imagify_Attachment( $post_id ); | |
| 33 | - $attachment->delete_backup(); | |
| 56 | +function imagify_add_avif_type( $ext2type ) { | |
| 57 | + if ( ! in_array( 'avif', $ext2type['image'], true ) ) { | |
| 58 | + $ext2type['image'][] = 'avif'; | |
| 59 | + } | |
| 60 | + return $ext2type; | |
| 34 | 61 | } |
| 35 | 62 | |
| 36 | 63 | /** |
| 37 | - * Optimize a resized attachment | |
| 64 | + * Set WP’s "big images threshold" to Imagify’s resizing value. | |
| 38 | 65 | * |
| 39 | - * @since 1.3.6 | |
| 40 | - * @since 1.4 Async job | |
| 66 | + * @since 1.9.8 | |
| 67 | + * @since WP 5.3 | |
| 68 | + * @author Grégory Viguier | |
| 41 | 69 | */ |
| 42 | -add_action( 'shutdown', '_imagify_optimize_save_image_editor_file' ); | |
| 43 | -function _imagify_optimize_save_image_editor_file() { | |
| 44 | - if ( isset( $_POST['action'], $_POST['do'], $_POST['postid'] ) | |
| 45 | - && 'image-editor' === $_POST['action'] | |
| 46 | - && check_ajax_referer( 'image_editor-' . $_POST['postid'] ) | |
| 47 | - && get_post_meta( $_POST['postid'], '_imagify_data', true ) | |
| 48 | - && 'open' != $_POST['do'] | |
| 49 | - ) { | |
| 70 | +add_filter( 'big_image_size_threshold', [ imagify_get_context( 'wp' ), 'filter_big_image_size_threshold' ], IMAGIFY_INT_MAX ); | |
| 50 | 71 | |
| 51 | - $body = $_POST; | |
| 52 | - $body['action'] = 'imagify_async_optimize_save_image_editor_file'; | |
| 72 | +/** | |
| 73 | + * Note the uploads WordPress 7.1 hands to the browser, which scales them itself. | |
| 74 | + * | |
| 75 | + * Fires before the attachment metadata is generated, so the threshold filter above already | |
| 76 | + * knows about it by the time it runs. | |
| 77 | + * | |
| 78 | + * @since 2.3.3 | |
| 79 | + * @since WP 7.1 | |
| 80 | + */ | |
| 81 | +add_action( 'rest_after_insert_attachment', [ imagify_get_context( 'wp' ), 'maybe_flag_client_side_scaling' ], 10, 3 ); | |
| 53 | 82 | |
| 54 | - imagify_do_async_job( $body ); | |
| 83 | +/** | |
| 84 | + * Add filters to manage images formats that will be generated | |
| 85 | + * | |
| 86 | + * @return array | |
| 87 | + */ | |
| 88 | +function imagify_nextgen_images_formats() { | |
| 89 | + $value = get_imagify_option( 'optimization_format' ); | |
| 90 | + $formats = []; | |
| 91 | + | |
| 92 | + if ( 'off' !== $value ) { | |
| 93 | + $formats[ $value ] = $value; | |
| 55 | 94 | } |
| 56 | -} | |
| 95 | + | |
| 96 | + $default = $formats; | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Filters the array of next gen formats to generate with Imagify | |
| 100 | + * | |
| 101 | + * @since 2.2 | |
| 102 | + * | |
| 103 | + * @param array $formats Array of image formats | |
| 104 | + */ | |
| 105 | + $formats = apply_filters( 'imagify_nextgen_images_formats', $formats ); | |
| 106 | + | |
| 107 | + if ( ! is_array( $formats ) ) { | |
| 108 | + $formats = $default; | |
| 109 | + } | |
| 110 | + | |
| 111 | + return $formats; | |
| 112 | +} | |