WP_Widget_Media::display_media_state( array $states, WP_Post $post = null ): array
- Since
- 4.8.0
- Source
wp-includes/widgets/class-wp-widget-media.php:367
Compatibility
- WordPress
- since 4.8.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
$statesarray- An array of media states.
$postWP_Postoptional- The current attachment object.Default:
null
Return value
array
Performance profile
How much work a call to WP_Widget_Media::display_media_state() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 13–33
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_post().
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 46.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()one call below WP_Widget_Media::display_media_state()
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Widget_Media::display_media_state() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13–16 | ->get_settings() |
| always | 16–19 | get_post(), ->get_settings() |
$use_count !== 1 && $use_count | 29–30 | ->get_settings(), translate_nooped_plural(), number_format_i18n(), sprintf() |
$use_count !== 1 && $use_count | 32–33 | get_post(), ->get_settings(), translate_nooped_plural(), number_format_i18n(), sprintf() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 46 instructions, 13–33 executed per call, 7 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.
Uses · 4
- get_post()Retrieves post data given a post ID or post object.
- translate_nooped_plural()Translates and returns the singular or plural form of a string that's been registered with _n_noop() or _nx_noop().
- number_format_i18n()Converts float number to format based on the locale.
- WP_Widget_Media::get_settings()
Source code
public function display_media_state( $states, $post = null ) { if ( ! $post ) { $post = get_post(); } // Count how many times this attachment is used in widgets. $use_count = 0; foreach ( $this->get_settings() as $instance ) { if ( isset( $instance['attachment_id'] ) && $instance['attachment_id'] === $post->ID ) { ++$use_count; } } if ( 1 === $use_count ) { $states[] = $this->l10n['media_library_state_single']; } elseif ( $use_count > 0 ) { $states[] = sprintf( translate_nooped_plural( $this->l10n['media_library_state_multi'], $use_count ), number_format_i18n( $use_count ) ); } return $states; }Changelog
Introduced in 4.8.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$post retyped from WP_Post to WP_Post|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/widgets/class-wp-widget-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.