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
This commit is contained in:
Joe McGill 2024-10-07 17:13:14 +00:00
parent 12cf82f950
commit 804c3d0416
4 changed files with 28 additions and 5 deletions

View File

@ -876,6 +876,7 @@ function wp_start_object_cache() {
'blog-lookup', 'blog-lookup',
'blog_meta', 'blog_meta',
'global-posts', 'global-posts',
'image_editor',
'networks', 'networks',
'network-queries', 'network-queries',
'sites', 'sites',

View File

@ -4191,7 +4191,22 @@ function _wp_image_editor_choose( $args = array() ) {
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'. * 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
*/ */
$implementations = apply_filters( 'wp_image_editors', 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 ) { foreach ( $implementations as $implementation ) {
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) { 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. * This implementation supports the input type but not the output type.
* Keep looking to see if we can find an implementation that supports both. * Keep looking to see if we can find an implementation that supports both.
*/ */
$supports_input = $implementation; $editor = $implementation;
continue; continue;
} }
// Favor the implementation that supports both input and output mime types. // 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;
} }
/** /**

View File

@ -554,6 +554,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) {
'blog-lookup', 'blog-lookup',
'blog_meta', 'blog_meta',
'global-posts', 'global-posts',
'image_editor',
'networks', 'networks',
'network-queries', 'network-queries',
'sites', 'sites',
@ -648,6 +649,7 @@ function restore_current_blog() {
'blog-lookup', 'blog-lookup',
'blog_meta', 'blog_meta',
'global-posts', 'global-posts',
'image_editor',
'networks', 'networks',
'network-queries', 'network-queries',
'sites', 'sites',

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.