Changeset 56808 for trunk/src/wp-includes/blocks/image.php
- Timestamp:
- 10/09/2023 05:21:30 PM (11 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/image.php
r56755 r56808 10 10 * adding a data-id attribute to the element if core/gallery has added on pre-render. 11 11 * 12 * @param array $attributes The block attributes. 13 * @param string $content The block content. 14 * @param WP_Block $block The block object. 15 * @return string Returns the block content with the data-id attribute added. 12 * @param array $attributes The block attributes. 13 * @param string $content The block content. 14 * @param WP_Block $block The block object. 15 * 16 * @return string The block content with the data-id attribute added. 16 17 */ 17 18 function render_block_core_image( $attributes, $content, $block ) { … … 77 78 78 79 /** 79 * Add the lightboxEnabled flag to the block data.80 * Adds the lightboxEnabled flag to the block data. 80 81 * 81 82 * This is used to determine whether the lightbox should be rendered or not. 82 83 * 83 * @param array $block Block data. 84 * @return array Filtered block data. 84 * @param array $block Block data. 85 * 86 * @return array Filtered block data. 85 87 */ 86 88 function block_core_image_get_lightbox_settings( $block ) { … … 114 116 115 117 /** 116 * Add the directives and layout needed for the lightbox behavior. 117 * 118 * @param string $block_content Rendered block content. 119 * @param array $block Block object. 120 * @return string Filtered block content. 118 * Adds the directives and layout needed for the lightbox behavior. 119 * 120 * @param string $block_content Rendered block content. 121 * @param array $block Block object. 122 * 123 * @return string Filtered block content. 121 124 */ 122 125 function block_core_image_render_lightbox( $block_content, $block ) { … … 125 128 $aria_label = __( 'Enlarge image' ); 126 129 130 $processor->next_tag( 'img' ); 127 131 $alt_attribute = $processor->get_attribute( 'alt' ); 128 132 129 if ( null !== $alt_attribute ) { 133 // An empty alt attribute `alt=""` is valid for decorative images. 134 if ( is_string( $alt_attribute ) ) { 130 135 $alt_attribute = trim( $alt_attribute ); 131 136 } 132 137 138 // It only makes sense to append the alt text to the button aria-label when the alt text is non-empty. 133 139 if ( $alt_attribute ) { 134 140 /* translators: %s: Image alt text. */ 135 141 $aria_label = sprintf( __( 'Enlarge image: %s' ), $alt_attribute ); 136 142 } 137 $content = $processor->get_updated_html();138 143 139 144 // Currently, we are only enabling the zoom animation. 140 145 $lightbox_animation = 'zoom'; 141 146 142 // We want to store the src in the context so we can set it dynamically when the lightbox is opened. 143 $z = new WP_HTML_Tag_Processor( $content ); 144 $z->next_tag( 'img' ); 145 147 // Note: We want to store the `src` in the context so we 148 // can set it dynamically when the lightbox is opened. 146 149 if ( isset( $block['attrs']['id'] ) ) { 147 150 $img_uploaded_src = wp_get_attachment_url( $block['attrs']['id'] ); … … 150 153 $img_height = $img_metadata['height']; 151 154 } else { 152 $img_uploaded_src = $ z->get_attribute( 'src' );155 $img_uploaded_src = $processor->get_attribute( 'src' ); 153 156 $img_width = 'none'; 154 157 $img_height = 'none'; … … 161 164 } 162 165 163 $w = new WP_HTML_Tag_Processor( $ content );166 $w = new WP_HTML_Tag_Processor( $block_content ); 164 167 $w->next_tag( 'figure' ); 165 168 $w->add_class( 'wp-lightbox-container' ); … … 181 184 "targetWidth": "%s", 182 185 "targetHeight": "%s", 183 "scaleAttr": "%s" 186 "scaleAttr": "%s", 187 "dialogLabel": "%s" 184 188 } 185 189 } … … 189 193 $img_width, 190 194 $img_height, 191 $scale_attr 195 $scale_attr, 196 __( 'Enlarged image' ) 192 197 ) 193 198 ); … … 201 206 $img = null; 202 207 preg_match( '/<img[^>]+>/', $body_content, $img ); 203 $button = 204 '<button 205 type="button" 206 aria-haspopup="dialog" 207 aria-label="' . esc_attr( $aria_label ) . '" 208 data-wp-on--click="actions.core.image.showLightbox" 209 data-wp-style--width="context.core.image.imageButtonWidth" 210 data-wp-style--height="context.core.image.imageButtonHeight" 211 data-wp-style--left="context.core.image.imageButtonLeft" 212 data-wp-style--top="context.core.image.imageButtonTop" 213 > 214 </button>' 215 . $img[0]; 208 209 $button = 210 $img[0] 211 . '<button 212 type="button" 213 aria-haspopup="dialog" 214 aria-label="' . esc_attr( $aria_label ) . '" 215 data-wp-on--click="actions.core.image.showLightbox" 216 data-wp-style--width="context.core.image.imageButtonWidth" 217 data-wp-style--height="context.core.image.imageButtonHeight" 218 data-wp-style--left="context.core.image.imageButtonLeft" 219 data-wp-style--top="context.core.image.imageButtonTop" 220 ></button>'; 221 216 222 $body_content = preg_replace( '/<img[^>]+>/', $button, $body_content ); 217 223 … … 221 227 // as the lightbox is opened, while the enlarged one is a full-sized 222 228 // version that will likely still be loading as the animation begins. 223 $m = new WP_HTML_Tag_Processor( $ content );229 $m = new WP_HTML_Tag_Processor( $block_content ); 224 230 $m->next_tag( 'figure' ); 225 231 $m->add_class( 'responsive-image' ); … … 237 243 $initial_image_content = $m->get_updated_html(); 238 244 239 $q = new WP_HTML_Tag_Processor( $ content );245 $q = new WP_HTML_Tag_Processor( $block_content ); 240 246 $q->next_tag( 'figure' ); 241 247 $q->add_class( 'enlarged-image' ); … … 253 259 $enlarged_image_content = $q->get_updated_html(); 254 260 255 $background_color = esc_attr( wp_get_global_styles( array( 'color', 'background' ) ) ); 261 // If the current theme does NOT have a `theme.json`, or the colors are not defined, 262 // we need to set the background color & close button color to some default values 263 // because we can't get them from the Global Styles. 264 $background_color = '#fff'; 265 $close_button_color = '#000'; 266 if ( wp_theme_has_theme_json() ) { 267 $global_styles_color = wp_get_global_styles( array( 'color' ) ); 268 if ( ! empty( $global_styles_color['background'] ) ) { 269 $background_color = esc_attr( $global_styles_color['background'] ); 270 } 271 if ( ! empty( $global_styles_color['text'] ) ) { 272 $close_button_color = esc_attr( $global_styles_color['text'] ); 273 } 274 } 256 275 257 276 $close_button_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="15" height="15" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg>'; 258 $close_button_color = esc_attr( wp_get_global_styles( array( 'color', 'text' ) ) );259 $dialog_label = $alt_attribute ? esc_attr( $alt_attribute ) : esc_attr__( 'Image' );260 277 $close_button_label = esc_attr__( 'Close' ); 261 278 … … 263 280 <div data-wp-body="" class="wp-lightbox-overlay $lightbox_animation" 264 281 data-wp-bind--role="selectors.core.image.roleAttribute" 265 aria-label="$dialog_label"282 data-wp-bind--aria-label="selectors.core.image.dialogLabel" 266 283 data-wp-class--initialized="context.core.image.initialized" 267 284 data-wp-class--active="context.core.image.lightboxEnabled" 268 285 data-wp-class--hideAnimationEnabled="context.core.image.hideAnimationEnabled" 269 data-wp-bind--aria-hidden="!context.core.image.lightboxEnabled" 270 aria-hidden="true" 271 data-wp-bind--aria-modal="context.core.image.lightboxEnabled" 272 aria-modal="false" 286 data-wp-bind--aria-modal="selectors.core.image.ariaModal" 273 287 data-wp-effect="effects.core.image.initLightbox" 274 288 data-wp-on--keydown="actions.core.image.handleKeydown" … … 283 297 <div class="lightbox-image-container">$initial_image_content</div> 284 298 <div class="lightbox-image-container">$enlarged_image_content</div> 285 <div class="scrim" style="background-color: $background_color" ></div>299 <div class="scrim" style="background-color: $background_color" aria-hidden="true"></div> 286 300 </div> 287 301 HTML; … … 291 305 292 306 /** 293 * Ensure that the view script has the `wp-interactivity` dependency.307 * Ensures that the view script has the `wp-interactivity` dependency. 294 308 * 295 309 * @since 6.4.0 296 310 * 297 311 * @global WP_Scripts $wp_scripts 312 * 313 * @return void 298 314 */ 299 315 function block_core_image_ensure_interactivity_dependency() { … … 311 327 /** 312 328 * Registers the `core/image` block on server. 329 * 330 * @return void 313 331 */ 314 332 function register_block_core_image() {
Note: See TracChangeset
for help on using the changeset viewer.