wp/wp-includes/blocks/post-title.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     5  * @package WordPress
     5  * @package WordPress
     6  */
     6  */
     7 
     7 
     8 /**
     8 /**
     9  * Renders the `core/post-title` block on the server.
     9  * Renders the `core/post-title` block on the server.
       
    10  *
       
    11  * @since 6.3.0 Omitting the $post argument from the `get_the_title`.
    10  *
    12  *
    11  * @param array    $attributes Block attributes.
    13  * @param array    $attributes Block attributes.
    12  * @param string   $content    Block default content.
    14  * @param string   $content    Block default content.
    13  * @param WP_Block $block      Block instance.
    15  * @param WP_Block $block      Block instance.
    14  *
    16  *
    17 function render_block_core_post_title( $attributes, $content, $block ) {
    19 function render_block_core_post_title( $attributes, $content, $block ) {
    18 	if ( ! isset( $block->context['postId'] ) ) {
    20 	if ( ! isset( $block->context['postId'] ) ) {
    19 		return '';
    21 		return '';
    20 	}
    22 	}
    21 
    23 
    22 	$post_ID = $block->context['postId'];
    24 	/**
    23 	$title   = get_the_title();
    25 	 * The `$post` argument is intentionally omitted so that changes are reflected when previewing a post.
       
    26 	 * See: https://github.com/WordPress/gutenberg/pull/37622#issuecomment-1000932816.
       
    27 	 */
       
    28 	$title = get_the_title();
    24 
    29 
    25 	if ( ! $title ) {
    30 	if ( ! $title ) {
    26 		return '';
    31 		return '';
    27 	}
    32 	}
    28 
    33 
    29 	$tag_name         = 'h2';
    34 	$tag_name = 'h2';
    30 	$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
       
    31 
       
    32 	if ( isset( $attributes['level'] ) ) {
    35 	if ( isset( $attributes['level'] ) ) {
    33 		$tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level'];
    36 		$tag_name = 0 === $attributes['level'] ? 'p' : 'h' . (int) $attributes['level'];
    34 	}
    37 	}
    35 
    38 
    36 	if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
    39 	if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
    37 		$title = sprintf( '<a href="%1$s" target="%2$s" rel="%3$s">%4$s</a>', get_the_permalink( $post_ID ), esc_attr( $attributes['linkTarget'] ), esc_attr( $attributes['rel'] ), $title );
    40 		$rel   = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : '';
       
    41 		$title = sprintf( '<a href="%1$s" target="%2$s" %3$s>%4$s</a>', esc_url( get_the_permalink( $block->context['postId'] ) ), esc_attr( $attributes['linkTarget'] ), $rel, $title );
    38 	}
    42 	}
    39 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
    43 
       
    44 	$classes = array();
       
    45 	if ( isset( $attributes['textAlign'] ) ) {
       
    46 		$classes[] = 'has-text-align-' . $attributes['textAlign'];
       
    47 	}
       
    48 	if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
       
    49 		$classes[] = 'has-link-color';
       
    50 	}
       
    51 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
    40 
    52 
    41 	return sprintf(
    53 	return sprintf(
    42 		'<%1$s %2$s>%3$s</%1$s>',
    54 		'<%1$s %2$s>%3$s</%1$s>',
    43 		$tag_name,
    55 		$tag_name,
    44 		$wrapper_attributes,
    56 		$wrapper_attributes,
    46 	);
    58 	);
    47 }
    59 }
    48 
    60 
    49 /**
    61 /**
    50  * Registers the `core/post-title` block on the server.
    62  * Registers the `core/post-title` block on the server.
       
    63  *
       
    64  * @since 5.8.0
    51  */
    65  */
    52 function register_block_core_post_title() {
    66 function register_block_core_post_title() {
    53 	register_block_type_from_metadata(
    67 	register_block_type_from_metadata(
    54 		__DIR__ . '/post-title',
    68 		__DIR__ . '/post-title',
    55 		array(
    69 		array(