wp/wp-includes/blocks/cover.php
changeset 19 3d72ae0968f4
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
       
     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  * @param array $attributes The block attributes.
       
    12  * @param array $content    The block rendered content.
       
    13  *
       
    14  * @return string Returns the cover block markup, if useFeaturedImage is true.
       
    15  */
       
    16 function render_block_core_cover( $attributes, $content ) {
       
    17 	if ( 'image' !== $attributes['backgroundType'] || false === $attributes['useFeaturedImage'] ) {
       
    18 		return $content;
       
    19 	}
       
    20 
       
    21 	if ( ! ( $attributes['hasParallax'] || $attributes['isRepeated'] ) ) {
       
    22 		$attr = array(
       
    23 			'class'           => 'wp-block-cover__image-background',
       
    24 			'data-object-fit' => 'cover',
       
    25 		);
       
    26 
       
    27 		if ( isset( $attributes['focalPoint'] ) ) {
       
    28 			$object_position              = round( $attributes['focalPoint']['x'] * 100 ) . '%' . ' ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
       
    29 			$attr['data-object-position'] = $object_position;
       
    30 			$attr['style']                = 'object-position: ' . $object_position;
       
    31 		}
       
    32 
       
    33 		$image = get_the_post_thumbnail( null, 'post-thumbnail', $attr );
       
    34 
       
    35 		/*
       
    36 		 * Inserts the featured image between the (1st) cover 'background' `span` and 'inner_container' `div`,
       
    37 		 * and removes eventual withespace characters between the two (typically introduced at template level)
       
    38 		 */
       
    39 		$inner_container_start = '/<div\b[^>]+wp-block-cover__inner-container[\s|"][^>]*>/U';
       
    40 		if ( 1 === preg_match( $inner_container_start, $content, $matches, PREG_OFFSET_CAPTURE ) ) {
       
    41 			$offset  = $matches[0][1];
       
    42 			$content = substr( $content, 0, $offset ) . $image . substr( $content, $offset );
       
    43 		}
       
    44 	} else {
       
    45 		if ( in_the_loop() ) {
       
    46 			update_post_thumbnail_cache();
       
    47 		}
       
    48 		$current_featured_image = get_the_post_thumbnail_url();
       
    49 		$content                = preg_replace(
       
    50 			'/class=\".*?\"/',
       
    51 			'${0} style="background-image:url(' . esc_url( $current_featured_image ) . ')"',
       
    52 			$content,
       
    53 			1
       
    54 		);
       
    55 	}
       
    56 
       
    57 	return $content;
       
    58 }
       
    59 
       
    60 /**
       
    61  * Registers the `core/cover` block renderer on server.
       
    62  */
       
    63 function register_block_core_cover() {
       
    64 	register_block_type_from_metadata(
       
    65 		__DIR__ . '/cover',
       
    66 		array(
       
    67 			'render_callback' => 'render_block_core_cover',
       
    68 		)
       
    69 	);
       
    70 }
       
    71 add_action( 'init', 'register_block_core_cover' );