render_block_core_image( array $attributes, string $content, WP_Block $block ): string
- Since
- 5.9.0
- Source
wp-includes/blocks/image.php:20
core/image block on the server, adding a data-id attribute to the element if core/gallery has added on pre-render.Compatibility
- WordPress
- since 5.9.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
$attributesarray- The block attributes.
$contentstring- The block content.
$blockWP_Block- The block object.
Return value
string- The block content with the data-id attribute added.
Performance profile
How much work a call to render_block_core_image() 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
- Trivial
- Scaling
- Constant
- Instructions
- 10–108
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 117.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 23 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost render_block_core_image() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10 | stripos() |
!->next_tag() | 19 | stripos(), ->next_tag() |
->next_tag() && !->get_attribute() | 23 | stripos(), ->next_tag(), ->get_attribute() |
->next_tag() && ->get_attribute() && !isset($attributes) | 56–75 | stripos(), ->next_tag(), ->get_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && !isset($attributes) && empty($attributes) | 61–78 | stripos(), ->next_tag(), ->get_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && isset($attributes) | 63–83 | stripos(), ->next_tag(), ->get_attribute(), ->set_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && !isset($attributes) | 64–85 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && !isset($attributes) && isset($lightbox_settings) && $link_destination === "none" | 68–80 | stripos(), ->next_tag(), ->get_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && isset($attributes) && empty($attributes) | 68–86 | stripos(), ->next_tag(), ->get_attribute(), ->set_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && !isset($attributes) && empty($attributes) | 69–88 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() | 71–93 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && !isset($attributes) && empty($attributes) && isset($lightbox_settings) && $link_destination === "none" | 73–83 | stripos(), ->next_tag(), ->get_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
11 further outcomes, up to 108 instructions
->next_tag() && ->get_attribute() && isset($attributes) && isset($lightbox_settings) && $link_destination === "none" | 75–88 | stripos(), ->next_tag(), ->get_attribute(), ->set_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && !isset($attributes) && isset($lightbox_settings) && $link_destination === "none" | 76–90 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && empty($attributes) | 76–96 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && isset($attributes) && empty($attributes) && isset($lightbox_settings) && $link_destination === "none" | 80–91 | stripos(), ->next_tag(), ->get_attribute(), ->set_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && is_string($image_classnames) && !$image_classnames && isset($attributes) | 80–100 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->set_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && !isset($attributes) && empty($attributes) && isset($lightbox_settings) && $link_destination === "none" | 81–93 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && isset($lightbox_settings) && $link_destination === "none" | 83–98 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && is_string($image_classnames) && !$image_classnames && isset($attributes) && empty($attributes) | 85–103 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->set_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && empty($attributes) && isset($lightbox_settings) && $link_destination === "none" | 88–101 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && is_string($image_classnames) && !$image_classnames && isset($attributes) && isset($lightbox_settings) && $link_destination === "none" | 92–105 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->set_attribute(), ->next_tag(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
->next_tag() && ->get_attribute() && is_string($image_classnames) && !$image_classnames && isset($attributes) && empty($attributes) && isset($lightbox_settings) && $link_destination === "none" | 97–108 | stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->set_attribute(), ->next_tag(), ->block_core_image_extract_empty_figcaption_element(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 117 | 10–108 | 17 | |
| 8.5 | 117 | 10–108 | 17 | |
| 8.4 | 117 | 10–108 | 17 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 129 | 10–120 | 17 | |
| 8.2 | 129 | 10–120 | 17 | |
| 8.1 | 129 | 10–120 | 17 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 131 | 10–121 | 17 |
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 · 7
- stripos()
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- block_core_image_get_lightbox_settings()Adds the lightboxEnabled flag to the block data.
- wp_enqueue_script_module()Marks the script module to be enqueued in the page.
- add_filter()Adds a callback function to a filter hook.
- remove_filter()Removes a callback function from a filter hook.
- class@anonymous::__construct()
Source code
function render_block_core_image( $attributes, $content, $block ) { if ( false === stripos( $content, '<img' ) ) { return ''; } $processor = new class( $content ) extends WP_HTML_Tag_Processor { /** * Return input span for an empty FIGCAPTION element. * * Returns span of input for an empty FIGCAPTION, if currently matched on a * FIGCAPTION opening tag and if the element is properly closed and empty. * * @since 6.9.0 * * @return WP_HTML_Span|false Span of input if the element is empty; otherwise false. */ public function block_core_image_extract_empty_figcaption_element() { $this->set_bookmark( 'here' ); $opener = $this->bookmarks['here']; // Allow comments within the definition of “empty.” while ( $this->next_token() && '#comment' === $this->get_token_name() ) { continue; } if ( 'FIGCAPTION' !== $this->get_tag() || ! $this->is_tag_closer() ) { return false; } $this->set_bookmark( 'here' ); $closer = $this->bookmarks['here']; return new WP_HTML_Span( $opener->start, $closer->start + $closer->length - $opener->start ); } }; if ( ! $processor->next_tag( 'img' ) || ! $processor->get_attribute( 'src' ) ) { return ''; } $has_id_binding = isset( $attributes['metadata']['bindings']['id'] ) && isset( $attributes['id'] ); // Ensure the `wp-image-id` classname on the image block supports block bindings. if ( $has_id_binding ) { // If there's a mismatch with the 'wp-image-' class and the actual id, the id was // probably overridden by block bindings. Update it to the correct value. // See https://github.com/WordPress/gutenberg/issues/62886 for why this is needed. $id = $attributes['id']; $image_classnames = $processor->get_attribute( 'class' ); $class_with_binding_value = "wp-image-$id"; if ( is_string( $image_classnames ) && ! str_contains( $image_classnames, $class_with_binding_value ) ) { $image_classnames = preg_replace( '/wp-image-(\d+)/', $class_with_binding_value, $image_classnames ); $processor->set_attribute( 'class', $image_classnames ); } } // For backwards compatibility, the data-id html attribute is only set for // image blocks nested in a gallery. Detect if the image is in a gallery by // checking the data-id attribute. // See the `block_core_gallery_data_id_backcompatibility` function. if ( isset( $attributes['data-id'] ) ) { // If there's a binding for the `id`, the `id` attribute is used for the // value, since `data-id` does not support block bindings. // Else the `data-id` is used for backwards compatibility, since // third parties may be filtering its value. $data_id = $has_id_binding ? $attributes['id'] : $attributes['data-id']; $processor->set_attribute( 'data-id', $data_id ); } /* * If the `caption` attribute is empty and we encounter a `<figcaption>` element, * we take note of its span so we can remove it later. */ if ( $processor->next_tag( 'FIGCAPTION' ) && empty( $attributes['caption'] ) ) { $figcaption_span = $processor->block_core_image_extract_empty_figcaption_element(); } $link_destination = $attributes['linkDestination'] ?? 'none'; $lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block );Changelog
Introduced in 5.9.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/blocks/image.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.