Make WordPress Core

Changeset 59189


Ignore:
Timestamp:
10/07/2024 05:11:43 PM (22 months ago)
Author:
joemcgill
Message:

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.

Location:
trunk/src/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/load.php

    r59171 r59189  
    877877                                'blog_meta',
    878878                                'global-posts',
     879                                'image_editor',
    879880                                'networks',
    880881                                'network-queries',
  • trunk/src/wp-includes/media.php

    r59118 r59189  
    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 ) {
     
    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;
    4234         }
    4235 
    4236         return $supports_input;
     4248                $editor = $implementation;
     4249                break;
     4250        }
     4251
     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
  • trunk/src/wp-includes/ms-blogs.php

    r57898 r59189  
    555555                                                'blog_meta',
    556556                                                'global-posts',
     557                                                'image_editor',
    557558                                                'networks',
    558559                                                'network-queries',
     
    649650                                                'blog_meta',
    650651                                                'global-posts',
     652                                                'image_editor',
    651653                                                'networks',
    652654                                                'network-queries',
Note: See TracChangeset for help on using the changeset viewer.