wppaste
WordPress

_wp_image_editor_choose( array $args = array() ): string|false

Since
3.5.0
Source
wp-includes/media.php:4417
Tests which editors are capable of supporting the request.

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

Only touches the object cache, via wp_cache_get().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
46–101

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 105.

Plugin surface
1 hook

Third-party callbacks on 'wp_image_editors' run inside this call, and their cost is not bounded by anything here.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()called directly
  • serializeserialisationserialize()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.

WhenInstructionsCalls it makes
isset($editors[$cache_key])46–47apply_filters(), wp_cache_get(), serialize()
!isset($editors[$cache_key])57–59apply_filters(), wp_cache_get(), serialize(), wp_cache_set()
!isset($editors[$cache_key]) && !$implementations && call_user_func() && !isset($args)71–78apply_filters(), wp_cache_get(), serialize(), call_user_func(), wp_cache_set()
!isset($editors[$cache_key]) && !$implementations && call_user_func()78–85apply_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–87apply_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–94apply_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–92apply_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–94apply_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–101apply_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:

  1. apply_filters( wp_image_editors )filterline 4430 (+13 into the body)

    Filters the list of image editing library classes.

Uses · 3

Used by · 3

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.