From 804c3d041616d6fd65ef1cbda483a8a433de8234 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Mon, 7 Oct 2024 17:13:14 +0000 Subject: [PATCH] Media: Cache the results of _wp_image_editor_choose. This saves the `WP_Image_Editor` implementation that supports the queried options to a cache to avoid performing redundant compatibility checks, which can be expensive. For example, `WP_Image_Editor_Imagick::supports_mime_type()` can get called in the editor multiple times to determine which image formats can be supported during `wp_plupload_default_settings()`. With this cache, the support will be stored for 1 day, speeding up loading times for the editor. This also introduces a new global caching group, `image_editor` to manage any subsequent caches that are related to image editor optimizations. Props joemcgill, desrosj, westonruter, flixos90, adamsilverstein, mukesh27, joehoyle. Fixes #61532. Built from https://develop.svn.wordpress.org/trunk@59189 git-svn-id: http://core.svn.wordpress.org/trunk@58584 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/load.php | 1 + wp-includes/media.php | 28 ++++++++++++++++++++++++---- wp-includes/ms-blogs.php | 2 ++ wp-includes/version.php | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/wp-includes/load.php b/wp-includes/load.php index 8c549d3660..9803590fc1 100644 --- a/wp-includes/load.php +++ b/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/wp-includes/media.php b/wp-includes/media.php index e16e5c7ff3..56389c5e14 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -4191,7 +4191,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 ) ) { @@ -4225,15 +4240,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; } /** diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index 2cf6d2390a..724c4ad7d2 100644 --- a/wp-includes/ms-blogs.php +++ b/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', diff --git a/wp-includes/version.php b/wp-includes/version.php index c9dabf58ff..4722082908 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.7-beta1-59188'; +$wp_version = '6.7-beta1-59189'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.