[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Server-side rendering of the `core/cover` block. 4 * 5 * @package WordPress 6 */ 7 8 /** 9 * Renders the `core/cover` block on server. 10 * 11 * @since 6.0.0 12 * 13 * @param array $attributes The block attributes. 14 * @param string $content The block rendered content. 15 * 16 * @return string Returns the cover block markup, if useFeaturedImage is true. 17 */ 18 function render_block_core_cover( $attributes, $content ) { 19 if ( 'image' !== $attributes['backgroundType'] || false === $attributes['useFeaturedImage'] ) { 20 return $content; 21 } 22 23 $object_position = isset( $attributes['focalPoint'] ) 24 ? round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%' 25 : null; 26 27 if ( ! ( $attributes['hasParallax'] || $attributes['isRepeated'] ) ) { 28 $attr = array( 29 'class' => 'wp-block-cover__image-background', 30 'data-object-fit' => 'cover', 31 ); 32 33 if ( $object_position ) { 34 $attr['data-object-position'] = $object_position; 35 $attr['style'] = 'object-position:' . $object_position . ';'; 36 } 37 38 $image = get_the_post_thumbnail( null, 'post-thumbnail', $attr ); 39 } else { 40 if ( in_the_loop() ) { 41 update_post_thumbnail_cache(); 42 } 43 $current_featured_image = get_the_post_thumbnail_url(); 44 if ( ! $current_featured_image ) { 45 return $content; 46 } 47 48 $current_thumbnail_id = get_post_thumbnail_id(); 49 50 $processor = new WP_HTML_Tag_Processor( '<div></div>' ); 51 $processor->next_tag(); 52 53 $current_alt = trim( strip_tags( get_post_meta( $current_thumbnail_id, '_wp_attachment_image_alt', true ) ) ); 54 if ( $current_alt ) { 55 $processor->set_attribute( 'role', 'img' ); 56 $processor->set_attribute( 'aria-label', $current_alt ); 57 } 58 59 $processor->add_class( 'wp-block-cover__image-background' ); 60 $processor->add_class( 'wp-image-' . $current_thumbnail_id ); 61 if ( $attributes['hasParallax'] ) { 62 $processor->add_class( 'has-parallax' ); 63 } 64 if ( $attributes['isRepeated'] ) { 65 $processor->add_class( 'is-repeated' ); 66 } 67 68 $styles = 'background-position:' . ( $object_position ?? '50% 50%' ) . ';'; 69 $styles .= 'background-image:url(' . esc_url( $current_featured_image ) . ');'; 70 $processor->set_attribute( 'style', $styles ); 71 72 $image = $processor->get_updated_html(); 73 } 74 75 /* 76 * Inserts the featured image between the (1st) cover 'background' `span` and 'inner_container' `div`, 77 * and removes eventual whitespace characters between the two (typically introduced at template level) 78 */ 79 $inner_container_start = '/<div\b[^>]+wp-block-cover__inner-container[\s|"][^>]*>/U'; 80 if ( 1 === preg_match( $inner_container_start, $content, $matches, PREG_OFFSET_CAPTURE ) ) { 81 $offset = $matches[0][1]; 82 $content = substr( $content, 0, $offset ) . $image . substr( $content, $offset ); 83 } 84 85 return $content; 86 } 87 88 /** 89 * Registers the `core/cover` block renderer on server. 90 * 91 * @since 6.0.0 92 */ 93 function register_block_core_cover() { 94 register_block_type_from_metadata( 95 __DIR__ . '/cover', 96 array( 97 'render_callback' => 'render_block_core_cover', 98 ) 99 ); 100 } 101 add_action( 'init', 'register_block_core_cover' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |