post-title.php (1609B)
1 <?php 2 /** 3 * Server-side rendering of the `core/post-title` block. 4 * 5 * @package WordPress 6 */ 7 8 /** 9 * Renders the `core/post-title` block on the server. 10 * 11 * @param array $attributes Block attributes. 12 * @param string $content Block default content. 13 * @param WP_Block $block Block instance. 14 * 15 * @return string Returns the filtered post title for the current post wrapped inside "h1" tags. 16 */ 17 function render_block_core_post_title( $attributes, $content, $block ) { 18 if ( ! isset( $block->context['postId'] ) ) { 19 return ''; 20 } 21 22 $post_ID = $block->context['postId']; 23 $tag_name = 'h2'; 24 $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; 25 26 if ( isset( $attributes['level'] ) ) { 27 $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level']; 28 } 29 30 $title = get_the_title( $post_ID ); 31 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { 32 $title = sprintf( '<a href="%1s" target="%2s" rel="%3s">%4s</a>', get_the_permalink( $post_ID ), $attributes['linkTarget'], $attributes['rel'], $title ); 33 } 34 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); 35 36 return sprintf( 37 '<%1$s %2$s>%3$s</%1$s>', 38 $tag_name, 39 $wrapper_attributes, 40 $title 41 ); 42 } 43 44 /** 45 * Registers the `core/post-title` block on the server. 46 */ 47 function register_block_core_post_title() { 48 register_block_type_from_metadata( 49 __DIR__ . '/post-title', 50 array( 51 'render_callback' => 'render_block_core_post_title', 52 ) 53 ); 54 } 55 add_action( 'init', 'register_block_core_post_title' );