_wp_image_editor_choose( array $args = array() ): string|false
- Since
- 3.5.0
- Source
wp-includes/media.php:4417
Compatibility
- WordPress
- since 3.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$argsarrayoptional- Array of arguments for choosing a capable editor. Default empty array.Default:
array()
Return value
string|false- Class name for the first editor that claims to support the request.
False if no editor claims to support the request.
Performance profile
How much work a call to _wp_image_editor_choose() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Scales with input
- Instructions
- 46–101
- Plugin surface
- 1 hook
- Called by
- 3
Only touches the object cache, via wp_cache_get().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 105.
Third-party callbacks on 'wp_image_editors' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()called directly - serializeserialisation
serialize()called directly
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_image_editor_choose() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
isset($editors[$cache_key]) | 46–47 | apply_filters(), wp_cache_get(), serialize() |
!isset($editors[$cache_key]) | 57–59 | apply_filters(), wp_cache_get(), serialize(), wp_cache_set() |
!isset($editors[$cache_key]) && !$implementations && call_user_func() && !isset($args) | 71–78 | apply_filters(), wp_cache_get(), serialize(), call_user_func(), wp_cache_set() |
!isset($editors[$cache_key]) && !$implementations && call_user_func() | 78–85 | apply_filters(), wp_cache_get(), serialize(), call_user_func(), call_user_func(), wp_cache_set() |
!isset($editors[$cache_key]) && !$implementations && call_user_func() && !array_diff() | 80–87 | apply_filters(), wp_cache_get(), serialize(), call_user_func(), get_class_methods(), array_diff(), wp_cache_set() |
!isset($editors[$cache_key]) && !$implementations && call_user_func() && isset($args) && !array_diff() | 87–94 | apply_filters(), wp_cache_get(), serialize(), call_user_func(), call_user_func(), get_class_methods(), array_diff(), wp_cache_set() |
!isset($editors[$cache_key]) && !$implementations && call_user_func() | 91–92 | apply_filters(), wp_cache_get(), serialize(), call_user_func(), call_user_func(), call_user_func(), wp_cache_set() |
!isset($editors[$cache_key]) && !$implementations && call_user_func() && !array_diff() | 93–94 | apply_filters(), wp_cache_get(), serialize(), call_user_func(), get_class_methods(), array_diff(), call_user_func(), wp_cache_set() |
!isset($editors[$cache_key]) && !$implementations && call_user_func() && isset($args) && !array_diff() | 100–101 | apply_filters(), wp_cache_get(), serialize(), call_user_func(), call_user_func(), get_class_methods(), array_diff(), call_user_func(), wp_cache_set() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 105 instructions, 46–101 executed per call, 13 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while _wp_image_editor_choose() runs, in this order:
- apply_filters( wp_image_editors )filterline 4430 (+13 into the body)
Filters the list of image editing library classes.
Uses · 3
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- wp_cache_set()Saves the data to the cache.
Used by · 3
- WP_Debug_Data::get_wp_media()Gets the WordPress media section of the debug data.
- wp_get_image_editor()Returns a WP_Image_Editor instance and loads file into it.
- wp_image_editor_supports()Tests whether there is an editor that supports a given mime type or methods.
Source code
function _wp_image_editor_choose( $args = array() ) { require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php'; require_once ABSPATH . WPINC . '/class-avif-info.php'; /** * Filters the list of image editing library classes. * * @since 3.5.0 * * @param string[] $image_editors Array of available image editor class names. Defaults are * 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'. */ $implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) ); $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 ) ) { continue; } // Implementation should support the passed mime type. if ( isset( $args['mime_type'] ) && ! call_user_func( array( $implementation, 'supports_mime_type' ), $args['mime_type'] ) ) { continue; } // Implementation should support requested methods. if ( isset( $args['methods'] ) && array_diff( $args['methods'], get_class_methods( $implementation ) ) ) { continue; } // Implementation should ideally support the output mime type as well if set and different than the passed type. if ( isset( $args['mime_type'] ) && isset( $args['output_mime_type'] ) && $args['mime_type'] !== $args['output_mime_type'] && ! call_user_func( array( $implementation, 'supports_mime_type' ), $args['output_mime_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. */ $editor = $implementation; continue; } // Favor the implementation that supports both input and output mime types. $editor = $implementation; break; } $editors[ $cache_key ] = $editor; wp_cache_set( 'wp_image_editor_choose', $editors, 'image_editor', DAY_IN_SECONDS ); return $editor;}Changelog
Introduced in 3.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.