Skip to content

Commit 15a114e

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@59189 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4c1898d commit 15a114e

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/wp-includes/load.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@ function wp_start_object_cache() {
876876
'blog-lookup',
877877
'blog_meta',
878878
'global-posts',
879+
'image_editor',
879880
'networks',
880881
'network-queries',
881882
'sites',

src/wp-includes/media.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4191,7 +4191,22 @@ function _wp_image_editor_choose( $args = array() ) {
41914191
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
41924192
*/
41934193
$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
4194-
$supports_input = false;
4194+
4195+
$editors = wp_cache_get( 'wp_image_editor_choose', 'image_editor' );
4196+
4197+
if ( ! is_array( $editors ) ) {
4198+
$editors = array();
4199+
}
4200+
4201+
// Cache the chosen editor implementation based on specific args and available implementations.
4202+
$cache_key = md5( serialize( array( $args, $implementations ) ) );
4203+
4204+
if ( isset( $editors[ $cache_key ] ) ) {
4205+
return $editors[ $cache_key ];
4206+
}
4207+
4208+
// Assume no support until a capable implementation is identified.
4209+
$editor = false;
41954210

41964211
foreach ( $implementations as $implementation ) {
41974212
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
@@ -4225,15 +4240,20 @@ function _wp_image_editor_choose( $args = array() ) {
42254240
* This implementation supports the input type but not the output type.
42264241
* Keep looking to see if we can find an implementation that supports both.
42274242
*/
4228-
$supports_input = $implementation;
4243+
$editor = $implementation;
42294244
continue;
42304245
}
42314246

42324247
// Favor the implementation that supports both input and output mime types.
4233-
return $implementation;
4248+
$editor = $implementation;
4249+
break;
42344250
}
42354251

4236-
return $supports_input;
4252+
$editors[ $cache_key ] = $editor;
4253+
4254+
wp_cache_set( 'wp_image_editor_choose', $editors, 'image_editor', DAY_IN_SECONDS );
4255+
4256+
return $editor;
42374257
}
42384258

42394259
/**

src/wp-includes/ms-blogs.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) {
554554
'blog-lookup',
555555
'blog_meta',
556556
'global-posts',
557+
'image_editor',
557558
'networks',
558559
'network-queries',
559560
'sites',
@@ -648,6 +649,7 @@ function restore_current_blog() {
648649
'blog-lookup',
649650
'blog_meta',
650651
'global-posts',
652+
'image_editor',
651653
'networks',
652654
'network-queries',
653655
'sites',

0 commit comments

Comments
 (0)