Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function render_block_core_image( $attributes, $content, $block ) {
return '';
}

$p = new class( $content ) extends WP_HTML_Tag_Processor {
$processor = new class( $content ) extends WP_HTML_Tag_Processor {
/**
* Return input span for an empty FIGCAPTION element.
*
Expand Down Expand Up @@ -53,7 +53,7 @@ public function block_core_image_extract_empty_figcaption_element() {
}
};

if ( ! $p->next_tag( 'img' ) || ! $p->get_attribute( 'src' ) ) {
if ( ! $processor->next_tag( 'img' ) || ! $processor->get_attribute( 'src' ) ) {
return '';
}

Expand All @@ -65,11 +65,11 @@ public function block_core_image_extract_empty_figcaption_element() {
// 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 = $p->get_attribute( 'class' );
$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 );
$p->set_attribute( 'class', $image_classnames );
$processor->set_attribute( 'class', $image_classnames );
}
}

Expand All @@ -83,15 +83,15 @@ public function block_core_image_extract_empty_figcaption_element() {
// 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'];
$p->set_attribute( 'data-id', $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 ( $p->next_tag( 'FIGCAPTION' ) && empty( $attributes['caption'] ) ) {
$figcaption_span = $p->block_core_image_extract_empty_figcaption_element();
if ( $processor->next_tag( 'FIGCAPTION' ) && empty( $attributes['caption'] ) ) {
$figcaption_span = $processor->block_core_image_extract_empty_figcaption_element();
}

$link_destination = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none';
Expand Down Expand Up @@ -125,7 +125,7 @@ public function block_core_image_extract_empty_figcaption_element() {
remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 );
}

$output = $p->get_updated_html();
$output = $processor->get_updated_html();
if ( ! empty( $figcaption_span ) ) {
return substr( $output, 0, $figcaption_span->start ) . substr( $output, $figcaption_span->start + $figcaption_span->length );
}
Expand Down Expand Up @@ -182,18 +182,18 @@ function block_core_image_render_lightbox( $block_content, $block ) {
* as-is. There's nothing that this code can knowingly modify to add the
* lightbox behavior.
*/
$p = new WP_HTML_Tag_Processor( $block_content );
if ( $p->next_tag( 'figure' ) ) {
$p->set_bookmark( 'figure' );
$processor = new WP_HTML_Tag_Processor( $block_content );
if ( $processor->next_tag( 'figure' ) ) {
$processor->set_bookmark( 'figure' );
}
if ( ! $p->next_tag( 'img' ) ) {
if ( ! $processor->next_tag( 'img' ) ) {
return $block_content;
}

$alt = $p->get_attribute( 'alt' );
$img_uploaded_src = $p->get_attribute( 'src' );
$img_class_names = $p->get_attribute( 'class' );
$img_styles = $p->get_attribute( 'style' );
$alt = $processor->get_attribute( 'alt' );
$img_uploaded_src = $processor->get_attribute( 'src' );
$img_class_names = $processor->get_attribute( 'class' );
$img_styles = $processor->get_attribute( 'style' );
$img_width = 'none';
$img_height = 'none';
$aria_label = __( 'Enlarge' );
Expand All @@ -207,9 +207,9 @@ function block_core_image_render_lightbox( $block_content, $block ) {
}

// Figure.
$p->seek( 'figure' );
$figure_class_names = $p->get_attribute( 'class' );
$figure_styles = $p->get_attribute( 'style' );
$processor->seek( 'figure' );
$figure_class_names = $processor->get_attribute( 'class' );
$figure_styles = $processor->get_attribute( 'style' );

// Create unique id and set the image metadata in the state.
$unique_image_id = uniqid();
Expand All @@ -234,9 +234,9 @@ function block_core_image_render_lightbox( $block_content, $block ) {
)
);

$p->add_class( 'wp-lightbox-container' );
$p->set_attribute( 'data-wp-interactive', 'core/image' );
$p->set_attribute(
$processor->add_class( 'wp-lightbox-container' );
$processor->set_attribute( 'data-wp-interactive', 'core/image' );
$processor->set_attribute(
'data-wp-context',
wp_json_encode(
array(
Expand All @@ -245,21 +245,21 @@ function block_core_image_render_lightbox( $block_content, $block ) {
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
)
);
$p->set_attribute( 'data-wp-key', $unique_image_id );
$processor->set_attribute( 'data-wp-key', $unique_image_id );

// Image.
$p->next_tag( 'img' );
$p->set_attribute( 'data-wp-init', 'callbacks.setButtonStyles' );
$p->set_attribute( 'data-wp-on-async--load', 'callbacks.setButtonStyles' );
$p->set_attribute( 'data-wp-on-async-window--resize', 'callbacks.setButtonStyles' );
$processor->next_tag( 'img' );
$processor->set_attribute( 'data-wp-init', 'callbacks.setButtonStyles' );
$processor->set_attribute( 'data-wp-on-async--load', 'callbacks.setButtonStyles' );
$processor->set_attribute( 'data-wp-on-async-window--resize', 'callbacks.setButtonStyles' );
// Sets an event callback on the `img` because the `figure` element can also
// contain a caption, and we don't want to trigger the lightbox when the
// caption is clicked.
$p->set_attribute( 'data-wp-on-async--click', 'actions.showLightbox' );
$p->set_attribute( 'data-wp-class--hide', 'state.isContentHidden' );
$p->set_attribute( 'data-wp-class--show', 'state.isContentVisible' );
$processor->set_attribute( 'data-wp-on-async--click', 'actions.showLightbox' );
$processor->set_attribute( 'data-wp-class--hide', 'state.isContentHidden' );
$processor->set_attribute( 'data-wp-class--show', 'state.isContentVisible' );

$body_content = $p->get_updated_html();
$body_content = $processor->get_updated_html();

// Adds a button alongside image in the body content.
$img = null;
Expand Down
Loading