From d454fa806bf27744f64113aadba8e797ac54231a Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 28 Jun 2024 11:52:23 -0500 Subject: [PATCH 1/8] Cache results from Imagick::queryFormats --- src/wp-includes/class-wp-image-editor-imagick.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index d38ee6fd631ee..302127861161a 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -110,12 +110,23 @@ public static function supports_mime_type( $mime_type ) { return false; } + // Imagick::queryFormats is not performant, so cache the results. + $supports = wp_cache_get( 'imagick_supports' ); + + if ( isset( $supports[ $imagick_extension ] ) ) { + return $supports[ $imagick_extension ]; + } + try { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged - return ( (bool) @Imagick::queryFormats( $imagick_extension ) ); + $supports[ $imagick_extension ] = ( (bool) @Imagick::queryFormats( $imagick_extension ) ); } catch ( Exception $e ) { - return false; + $supports[ $imagick_extension ] = false; } + + wp_cache_set( 'imagick_supports', $supports ); + + return $supports[ $imagick_extension ]; } /** From e017d8aab9604710647dfa62fa8c4cc76f50f18b Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 28 Jun 2024 13:29:43 -0500 Subject: [PATCH 2/8] Create initial supports array. --- src/wp-includes/class-wp-image-editor-imagick.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index 302127861161a..1b19b1f66c1d2 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -113,6 +113,10 @@ public static function supports_mime_type( $mime_type ) { // Imagick::queryFormats is not performant, so cache the results. $supports = wp_cache_get( 'imagick_supports' ); + if ( ! $supports ) { + $supports = array(); + } + if ( isset( $supports[ $imagick_extension ] ) ) { return $supports[ $imagick_extension ]; } From d0a1b15e78c19748ebf218c70b36dcb36a702c33 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 28 Jun 2024 13:53:53 -0500 Subject: [PATCH 3/8] Space invaders --- src/wp-includes/class-wp-image-editor-imagick.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index 1b19b1f66c1d2..d1d7544f4f0f3 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -107,7 +107,7 @@ public static function supports_mime_type( $mime_type ) { * Here, we just say no if you are missing it and aren't loading a jpeg. */ if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && 'image/jpeg' !== $mime_type ) { - return false; + return false; } // Imagick::queryFormats is not performant, so cache the results. From 5d499eddde57619c92f832991cab7eec8dce3441 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 4 Oct 2024 10:43:35 -0500 Subject: [PATCH 4/8] Revert changes to Imagick editor --- .../class-wp-image-editor-imagick.php | 21 +++---------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index 3d22d0e3ade35..819be5e09017c 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -107,30 +107,15 @@ public static function supports_mime_type( $mime_type ) { * Here, we just say no if you are missing it and aren't loading a jpeg. */ if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && 'image/jpeg' !== $mime_type ) { - return false; - } - - // Imagick::queryFormats is not performant, so cache the results. - $supports = wp_cache_get( 'imagick_supports' ); - - if ( ! $supports ) { - $supports = array(); - } - - if ( isset( $supports[ $imagick_extension ] ) ) { - return $supports[ $imagick_extension ]; + return false; } try { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged - $supports[ $imagick_extension ] = ( (bool) @Imagick::queryFormats( $imagick_extension ) ); + return ( (bool) @Imagick::queryFormats( $imagick_extension ) ); } catch ( Exception $e ) { - $supports[ $imagick_extension ] = false; + return false; } - - wp_cache_set( 'imagick_supports', $supports ); - - return $supports[ $imagick_extension ]; } /** From a86a6181953ef5dfb665ac67ddf9af8818b5e120 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 4 Oct 2024 10:44:15 -0500 Subject: [PATCH 5/8] Cache wp_image_editor_supports results --- src/wp-includes/media.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 50c85c8990f2c..f75c117ca0d02 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -4096,7 +4096,21 @@ function wp_get_image_editor( $path, $args = array() ) { * @return bool True if an eligible editor is found; false otherwise. */ function wp_image_editor_supports( $args = array() ) { - return (bool) _wp_image_editor_choose( $args ); + $supports = wp_cache_get( 'wp_image_editor_supports', 'image_editor' ); + + if ( false === $supports ) { + $supports = array(); + } + + $cache_key = md5( serialize( $args ) ); + + if ( ! isset( $supports[ $cache_key ] ) ) { + $supports[ $cache_key ] = (bool) _wp_image_editor_choose( $args ); + + wp_cache_set( 'wp_image_editor_supports', $supports, 'image_editor', DAY_IN_SECONDS ); + } + + return $supports[ $cache_key ]; } /** From c9297b7af4f1416cb99857ba51046f53f10e038f Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 4 Oct 2024 10:44:43 -0500 Subject: [PATCH 6/8] Add new image_editor global cache group --- src/wp-includes/load.php | 1 + src/wp-includes/ms-blogs.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index b0b8209235c3e..9a3102a003f32 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -876,6 +876,7 @@ function wp_start_object_cache() { 'blog-lookup', 'blog_meta', 'global-posts', + 'image_editor', 'networks', 'network-queries', 'sites', diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php index 2cf6d2390aab7..724c4ad7d2255 100644 --- a/src/wp-includes/ms-blogs.php +++ b/src/wp-includes/ms-blogs.php @@ -554,6 +554,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) { 'blog-lookup', 'blog_meta', 'global-posts', + 'image_editor', 'networks', 'network-queries', 'sites', @@ -648,6 +649,7 @@ function restore_current_blog() { 'blog-lookup', 'blog_meta', 'global-posts', + 'image_editor', 'networks', 'network-queries', 'sites', From 33bc1c0035f9d1c1098f4ee7ad6398ce415c5178 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 4 Oct 2024 14:18:15 -0500 Subject: [PATCH 7/8] Revert "Cache wp_image_editor_supports results" This reverts commit a86a6181953ef5dfb665ac67ddf9af8818b5e120. --- src/wp-includes/media.php | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index f75c117ca0d02..50c85c8990f2c 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -4096,21 +4096,7 @@ function wp_get_image_editor( $path, $args = array() ) { * @return bool True if an eligible editor is found; false otherwise. */ function wp_image_editor_supports( $args = array() ) { - $supports = wp_cache_get( 'wp_image_editor_supports', 'image_editor' ); - - if ( false === $supports ) { - $supports = array(); - } - - $cache_key = md5( serialize( $args ) ); - - if ( ! isset( $supports[ $cache_key ] ) ) { - $supports[ $cache_key ] = (bool) _wp_image_editor_choose( $args ); - - wp_cache_set( 'wp_image_editor_supports', $supports, 'image_editor', DAY_IN_SECONDS ); - } - - return $supports[ $cache_key ]; + return (bool) _wp_image_editor_choose( $args ); } /** From 6056c51f2879bbdd63f0b289de90d1b356d0294e Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 4 Oct 2024 15:09:51 -0500 Subject: [PATCH 8/8] Cache result of _wp_image_editor_choose --- src/wp-includes/media.php | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 50c85c8990f2c..fd76264754c71 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -4123,7 +4123,22 @@ function _wp_image_editor_choose( $args = array() ) { * 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'. */ $implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) ); - $supports_input = false; + + $editors = wp_cache_get( 'wp_image_editor_choose', 'image_editor' ); + + if ( ! is_array( $editors ) ) { + $editors = array(); + } + + // Cache the chosen editor implementation based on specific args and available implementations. + $cache_key = md5( serialize( array( $args, $implementations ) ) ); + + if ( isset( $editors[ $cache_key ] ) ) { + return $editors[ $cache_key ]; + } + + // Assume no support until a capable implementation is identified. + $editor = false; foreach ( $implementations as $implementation ) { if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) { @@ -4157,15 +4172,20 @@ function _wp_image_editor_choose( $args = array() ) { * This implementation supports the input type but not the output type. * Keep looking to see if we can find an implementation that supports both. */ - $supports_input = $implementation; + $editor = $implementation; continue; } // Favor the implementation that supports both input and output mime types. - return $implementation; + $editor = $implementation; + break; } - return $supports_input; + $editors[ $cache_key ] = $editor; + + wp_cache_set( 'wp_image_editor_choose', $editors, 'image_editor', DAY_IN_SECONDS ); + + return $editor; } /**