Changeset 61009 for trunk/src/wp-includes/blocks/image.php
- Timestamp:
- 10/21/2025 07:11:53 AM (3 months ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/blocks/image.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/image.php
r59775 r61009 23 23 } 24 24 25 $p = new WP_HTML_Tag_Processor( $content ); 26 27 if ( ! $p->next_tag( 'img' ) || ! $p->get_attribute( 'src' ) ) { 25 $processor = new class( $content ) extends WP_HTML_Tag_Processor { 26 /** 27 * Return input span for an empty FIGCAPTION element. 28 * 29 * Returns span of input for an empty FIGCAPTION, if currently matched on a 30 * FIGCAPTION opening tag and if the element is properly closed and empty. 31 * 32 * @since 6.9.0 33 * 34 * @return WP_HTML_Span|false Span of input if the element is empty; otherwise false. 35 */ 36 public function block_core_image_extract_empty_figcaption_element() { 37 $this->set_bookmark( 'here' ); 38 $opener = $this->bookmarks['here']; 39 40 // Allow comments within the definition of “empty.” 41 while ( $this->next_token() && '#comment' === $this->get_token_name() ) { 42 continue; 43 } 44 45 if ( 'FIGCAPTION' !== $this->get_tag() || ! $this->is_tag_closer() ) { 46 return false; 47 } 48 49 $this->set_bookmark( 'here' ); 50 $closer = $this->bookmarks['here']; 51 52 return new WP_HTML_Span( $opener->start, $closer->start + $closer->length - $opener->start ); 53 } 54 }; 55 56 if ( ! $processor->next_tag( 'img' ) || ! $processor->get_attribute( 'src' ) ) { 28 57 return ''; 29 58 } … … 37 66 // See https://github.com/WordPress/gutenberg/issues/62886 for why this is needed. 38 67 $id = $attributes['id']; 39 $image_classnames = $p ->get_attribute( 'class' );68 $image_classnames = $processor->get_attribute( 'class' ); 40 69 $class_with_binding_value = "wp-image-$id"; 41 70 if ( is_string( $image_classnames ) && ! str_contains( $image_classnames, $class_with_binding_value ) ) { 42 71 $image_classnames = preg_replace( '/wp-image-(\d+)/', $class_with_binding_value, $image_classnames ); 43 $p ->set_attribute( 'class', $image_classnames );72 $processor->set_attribute( 'class', $image_classnames ); 44 73 } 45 74 } … … 55 84 // third parties may be filtering its value. 56 85 $data_id = $has_id_binding ? $attributes['id'] : $attributes['data-id']; 57 $p->set_attribute( 'data-id', $data_id ); 86 $processor->set_attribute( 'data-id', $data_id ); 87 } 88 89 /* 90 * If the `caption` attribute is empty and we encounter a `<figcaption>` element, 91 * we take note of its span so we can remove it later. 92 */ 93 if ( $processor->next_tag( 'FIGCAPTION' ) && empty( $attributes['caption'] ) ) { 94 $figcaption_span = $processor->block_core_image_extract_empty_figcaption_element(); 58 95 } 59 96 … … 89 126 } 90 127 91 return $p->get_updated_html(); 128 $output = $processor->get_updated_html(); 129 if ( ! empty( $figcaption_span ) ) { 130 return substr( $output, 0, $figcaption_span->start ) . substr( $output, $figcaption_span->start + $figcaption_span->length ); 131 } 132 return $output; 92 133 } 93 134 … … 101 142 * @param array $block Block data. 102 143 * 103 * @return array Filtered block data.144 * @return array|null Filtered block data. 104 145 */ 105 146 function block_core_image_get_lightbox_settings( $block ) { … … 142 183 * lightbox behavior. 143 184 */ 144 $p = new WP_HTML_Tag_Processor( $block_content );145 if ( $p ->next_tag( 'figure' ) ) {146 $p ->set_bookmark( 'figure' );147 } 148 if ( ! $p ->next_tag( 'img' ) ) {185 $processor = new WP_HTML_Tag_Processor( $block_content ); 186 if ( $processor->next_tag( 'figure' ) ) { 187 $processor->set_bookmark( 'figure' ); 188 } 189 if ( ! $processor->next_tag( 'img' ) ) { 149 190 return $block_content; 150 191 } 151 192 152 $alt = $p ->get_attribute( 'alt' );153 $img_uploaded_src = $p ->get_attribute( 'src' );154 $img_class_names = $p ->get_attribute( 'class' );155 $img_styles = $p ->get_attribute( 'style' );193 $alt = $processor->get_attribute( 'alt' ); 194 $img_uploaded_src = $processor->get_attribute( 'src' ); 195 $img_class_names = $processor->get_attribute( 'class' ); 196 $img_styles = $processor->get_attribute( 'style' ); 156 197 $img_width = 'none'; 157 198 $img_height = 'none'; … … 167 208 168 209 // Figure. 169 $p ->seek( 'figure' );170 $figure_class_names = $p ->get_attribute( 'class' );171 $figure_styles = $p ->get_attribute( 'style' );210 $processor->seek( 'figure' ); 211 $figure_class_names = $processor->get_attribute( 'class' ); 212 $figure_styles = $processor->get_attribute( 'style' ); 172 213 173 214 // Create unique id and set the image metadata in the state. … … 194 235 ); 195 236 196 $p ->add_class( 'wp-lightbox-container' );197 $p ->set_attribute( 'data-wp-interactive', 'core/image' );198 $p ->set_attribute(237 $processor->add_class( 'wp-lightbox-container' ); 238 $processor->set_attribute( 'data-wp-interactive', 'core/image' ); 239 $processor->set_attribute( 199 240 'data-wp-context', 200 241 wp_json_encode( … … 205 246 ) 206 247 ); 248 $processor->set_attribute( 'data-wp-key', $unique_image_id ); 207 249 208 250 // Image. 209 $p ->next_tag( 'img' );210 $p ->set_attribute( 'data-wp-init', 'callbacks.setButtonStyles' );211 $p ->set_attribute( 'data-wp-on-async--load', 'callbacks.setButtonStyles' );212 $p ->set_attribute( 'data-wp-on-async-window--resize', 'callbacks.setButtonStyles' );251 $processor->next_tag( 'img' ); 252 $processor->set_attribute( 'data-wp-init', 'callbacks.setButtonStyles' ); 253 $processor->set_attribute( 'data-wp-on-async--load', 'callbacks.setButtonStyles' ); 254 $processor->set_attribute( 'data-wp-on-async-window--resize', 'callbacks.setButtonStyles' ); 213 255 // Sets an event callback on the `img` because the `figure` element can also 214 256 // contain a caption, and we don't want to trigger the lightbox when the 215 257 // caption is clicked. 216 $p ->set_attribute( 'data-wp-on-async--click', 'actions.showLightbox' );217 $p ->set_attribute( 'data-wp-class--hide', 'state.isContentHidden' );218 $p ->set_attribute( 'data-wp-class--show', 'state.isContentVisible' );219 220 $body_content = $p ->get_updated_html();258 $processor->set_attribute( 'data-wp-on-async--click', 'actions.showLightbox' ); 259 $processor->set_attribute( 'data-wp-class--hide', 'state.isContentHidden' ); 260 $processor->set_attribute( 'data-wp-class--show', 'state.isContentVisible' ); 261 262 $body_content = $processor->get_updated_html(); 221 263 222 264 // Adds a button alongside image in the body content. … … 273 315 class="wp-lightbox-overlay zoom" 274 316 data-wp-interactive="core/image" 317 data-wp-router-region='{ "id": "core/image-overlay", "attachTo": "body" }' 318 data-wp-key="wp-lightbox-overlay" 275 319 data-wp-context='{}' 276 320 data-wp-bind--role="state.roleAttribute" … … 278 322 data-wp-bind--aria-modal="state.ariaModal" 279 323 data-wp-class--active="state.overlayEnabled" 280 data-wp-class--show-closing-animation="state. showClosingAnimation"324 data-wp-class--show-closing-animation="state.overlayOpened" 281 325 data-wp-watch="callbacks.setOverlayFocus" 282 326 data-wp-on--keydown="actions.handleKeydown"
Note: See TracChangeset
for help on using the changeset viewer.