From 855a635c70e751dc3f8c8907aaca2e4bdb4cd51e Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Tue, 16 Jan 2024 10:01:59 +0100 Subject: [PATCH 1/6] Change method name from webp to netgen, rename class --#778 --- classes/Bulk/Bulk.php | 8 ++++---- ...gWebpCommand.php => GenerateMissingNextgenCommand.php} | 4 ++-- classes/Plugin.php | 4 ++-- inc/functions/i18n.php | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) rename classes/CLI/{GenerateMissingWebpCommand.php => GenerateMissingNextgenCommand.php} (89%) diff --git a/classes/Bulk/Bulk.php b/classes/Bulk/Bulk.php index ef13f783a..62d96dd91 100644 --- a/classes/Bulk/Bulk.php +++ b/classes/Bulk/Bulk.php @@ -20,7 +20,7 @@ public function init() { add_action( 'imagify_convert_webp', [ $this, 'generate_webp_versions' ], 10, 2 ); add_action( 'imagify_convert_webp_finished', [ $this, 'clear_webp_transients' ], 10, 2 ); add_action( 'wp_ajax_imagify_bulk_optimize', [ $this, 'bulk_optimize_callback' ] ); - add_action( 'wp_ajax_imagify_missing_webp_generation', [ $this, 'missing_webp_callback' ] ); + add_action( 'wp_ajax_imagify_missing_nextgen_generation', [ $this, 'missing_nextgen_callback' ] ); add_action( 'wp_ajax_imagify_get_folder_type_data', [ $this, 'get_folder_type_data_callback' ] ); add_action( 'wp_ajax_imagify_bulk_info_seen', [ $this, 'bulk_info_seen_callback' ] ); add_action( 'wp_ajax_imagify_bulk_get_stats', [ $this, 'bulk_get_stats_callback' ] ); @@ -219,7 +219,7 @@ public function run_optimize( string $context, int $optimization_level ) { * * @return array */ - public function run_generate_webp( array $contexts ) { + public function run_generate_nextgen( array $contexts ) { if ( ! $this->can_optimize() ) { return [ 'success' => false, @@ -484,7 +484,7 @@ public function bulk_optimize_callback() { * * @return void */ - public function missing_webp_callback() { + public function missing_nextgen_callback() { imagify_check_nonce( 'imagify-bulk-optimize' ); $contexts = explode( '_', sanitize_key( wp_unslash( $_GET['context'] ) ) ); @@ -495,7 +495,7 @@ public function missing_webp_callback() { } } - $data = $this->run_generate_webp( $contexts ); + $data = $this->run_generate_nextgen( $contexts ); if ( false === $data['success'] ) { wp_send_json_error( [ 'message' => $data['message'] ] ); diff --git a/classes/CLI/GenerateMissingWebpCommand.php b/classes/CLI/GenerateMissingNextgenCommand.php similarity index 89% rename from classes/CLI/GenerateMissingWebpCommand.php rename to classes/CLI/GenerateMissingNextgenCommand.php index 33cebc0e4..c8b5f2dae 100644 --- a/classes/CLI/GenerateMissingWebpCommand.php +++ b/classes/CLI/GenerateMissingNextgenCommand.php @@ -6,9 +6,9 @@ use Imagify\Bulk\Bulk; /** - * Command class for the missing WebP generation + * Command class for the missing Nextgen generation */ -class GenerateMissingWebpCommand extends AbstractCommand { +class GenerateMissingNextgenCommand extends AbstractCommand { /** * Executes the command. * diff --git a/classes/Plugin.php b/classes/Plugin.php index eaa0f1b55..c3cf0ad3b 100644 --- a/classes/Plugin.php +++ b/classes/Plugin.php @@ -5,7 +5,7 @@ use Imagify\Bulk\Bulk; use Imagify\CLI\BulkOptimizeCommand; -use Imagify\CLI\GenerateMissingWebpCommand; +use Imagify\CLI\GenerateMissingNextgenCommand; use Imagify\Notices\Notices; use Imagify\Admin\AdminBar; @@ -80,7 +80,7 @@ class_alias( '\\Imagify\\Traits\\InstanceGetterTrait', '\\Imagify\\Traits\\FakeS imagify_load_translations(); imagify_add_command( new BulkOptimizeCommand() ); - imagify_add_command( new GenerateMissingWebpCommand() ); + imagify_add_command( new GenerateMissingNextgenCommand() ); /** * Fires when Imagify is fully loaded. diff --git a/inc/functions/i18n.php b/inc/functions/i18n.php index 425c99a42..b59d885e8 100755 --- a/inc/functions/i18n.php +++ b/inc/functions/i18n.php @@ -93,7 +93,7 @@ function get_imagify_localize_script_translations( $context ) { 'requirements' => $imagifybeat_actions->get_imagifybeat_id( 'requirements' ), ], 'ajaxActions' => [ - 'MissingWebp' => 'imagify_missing_webp_generation', + 'MissingNextGen' => 'imagify_missing_nextgen_generation', ], 'ajaxNonce' => wp_create_nonce( 'imagify-bulk-optimize' ), 'contexts' => $contexts, From 512e14f03266497f580089b71b12e497fe9a90ad Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Thu, 18 Jan 2024 12:53:58 +0100 Subject: [PATCH 2/6] add formats option to next gen method --- classes/Bulk/Bulk.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/classes/Bulk/Bulk.php b/classes/Bulk/Bulk.php index 62d96dd91..49ff77aec 100644 --- a/classes/Bulk/Bulk.php +++ b/classes/Bulk/Bulk.php @@ -216,10 +216,11 @@ public function run_optimize( string $context, int $optimization_level ) { * Runs the WebP generation * * @param array $contexts An array of contexts (WP/Custom folders). + * @param array $formats An array of format to generate * * @return array */ - public function run_generate_nextgen( array $contexts ) { + public function run_generate_nextgen( array $contexts, array $formats ) { if ( ! $this->can_optimize() ) { return [ 'success' => false, @@ -495,7 +496,10 @@ public function missing_nextgen_callback() { } } - $data = $this->run_generate_nextgen( $contexts ); + $format_option = get_imagify_option( 'convert_to_avif' ) ? ['avif'] : []; + $formats = apply_filters( 'imagify_nextgen_images_formats', $format_option ); + + $data = $this->run_generate_nextgen( $contexts, $formats ); if ( false === $data['success'] ) { wp_send_json_error( [ 'message' => $data['message'] ] ); From dc1191cfd4e842af7889b2573b96bc0210bae1be Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Wed, 17 Jan 2024 16:16:29 +0100 Subject: [PATCH 3/6] fixed lint error --- inc/common/attachments.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/inc/common/attachments.php b/inc/common/attachments.php index b58f070e7..a08a99450 100755 --- a/inc/common/attachments.php +++ b/inc/common/attachments.php @@ -67,3 +67,30 @@ function imagify_add_webp_type( $ext2type ) { * @author Grégory Viguier */ add_filter( 'big_image_size_threshold', [ imagify_get_context( 'wp' ), 'get_resizing_threshold' ], IMAGIFY_INT_MAX ); + +/** + * Add filters to manage images formats that will be generated + * + * @param array $formats The format values. Default values are 'webp' and 'avif'. + * + * @return array; + */ +function imagify_nextgen_images_format( array $formats ) { + // If no formats is passed, bail early and default to webp. + if ( empty( $formats ) ) { + return [ 'webp' ]; + } + + if ( isset( $formats['webp'], $formats['avif'] ) + && ( $formats['avif'] && $formats['webp'] ) + ) { + return [ 'avif', 'webp' ]; + } + + return $formats; +} + +/** + * Filter to get the image format to generate. +*/ +add_filter( 'imagify_nextgen_images_formats', 'imagify_nextgen_images_format' ); From 9a55479db7aad8ea60039ca64535e7e1ed688933 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Thu, 18 Jan 2024 13:02:22 +0100 Subject: [PATCH 4/6] fixed lint error --- classes/Bulk/Bulk.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/Bulk/Bulk.php b/classes/Bulk/Bulk.php index 49ff77aec..66e4ef508 100644 --- a/classes/Bulk/Bulk.php +++ b/classes/Bulk/Bulk.php @@ -216,7 +216,7 @@ public function run_optimize( string $context, int $optimization_level ) { * Runs the WebP generation * * @param array $contexts An array of contexts (WP/Custom folders). - * @param array $formats An array of format to generate + * @param array $formats An array of format to generate. * * @return array */ @@ -496,7 +496,7 @@ public function missing_nextgen_callback() { } } - $format_option = get_imagify_option( 'convert_to_avif' ) ? ['avif'] : []; + $format_option = get_imagify_option( 'convert_to_avif' ) ? [ 'avif' ] : []; $formats = apply_filters( 'imagify_nextgen_images_formats', $format_option ); $data = $this->run_generate_nextgen( $contexts, $formats ); From 1cb202ba202f16d52eeda21289cc150f3341d148 Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Fri, 19 Jan 2024 13:26:11 +0100 Subject: [PATCH 5/6] Modified the filter function for image formats -- #777 --- inc/common/attachments.php | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/inc/common/attachments.php b/inc/common/attachments.php index a08a99450..8fcea558f 100755 --- a/inc/common/attachments.php +++ b/inc/common/attachments.php @@ -71,26 +71,30 @@ function imagify_add_webp_type( $ext2type ) { /** * Add filters to manage images formats that will be generated * - * @param array $formats The format values. Default values are 'webp' and 'avif'. - * * @return array; */ -function imagify_nextgen_images_format( array $formats ) { - // If no formats is passed, bail early and default to webp. - if ( empty( $formats ) ) { - return [ 'webp' ]; +function imagify_nextgen_images_formats() { + $formats = [ + 'webp', + ]; + + if ( get_imagify_option( 'convert_to_avif' ) ) { + $formats[] = 'avif'; } + $default = $formats; - if ( isset( $formats['webp'], $formats['avif'] ) - && ( $formats['avif'] && $formats['webp'] ) - ) { - return [ 'avif', 'webp' ]; + /** + * Filters the array of next gen formats to generate with Imagify + * + * @since 2.2 + * + * @param array $formats Array of image formats + */ + $formats = apply_filters( 'imagify_nextgen_images_formats', $formats ); + + if ( ! is_array( $formats ) ) { + $formats = $default; } return $formats; } - -/** - * Filter to get the image format to generate. -*/ -add_filter( 'imagify_nextgen_images_formats', 'imagify_nextgen_images_format' ); From 27cd02b066b3b791142f60726efbf46de89b1d4d Mon Sep 17 00:00:00 2001 From: Opeyemi Ibrahim Date: Fri, 19 Jan 2024 15:23:39 +0100 Subject: [PATCH 6/6] Changed use of filter to function --- classes/Bulk/Bulk.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/classes/Bulk/Bulk.php b/classes/Bulk/Bulk.php index 66e4ef508..202c848ac 100644 --- a/classes/Bulk/Bulk.php +++ b/classes/Bulk/Bulk.php @@ -496,8 +496,7 @@ public function missing_nextgen_callback() { } } - $format_option = get_imagify_option( 'convert_to_avif' ) ? [ 'avif' ] : []; - $formats = apply_filters( 'imagify_nextgen_images_formats', $format_option ); + $formats = imagify_nextgen_images_formats(); $data = $this->run_generate_nextgen( $contexts, $formats );