| 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 gutenberg_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 |
$title = get_the_title( $post_ID ); |
| 24 |
|
| 25 |
if ( ! $title ) { |
| 26 |
return ''; |
| 27 |
} |
| 28 |
|
| 29 |
$tag_name = 'h2'; |
| 30 |
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; |
| 31 |
|
| 32 |
if ( isset( $attributes['level'] ) ) { |
| 33 |
$tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level']; |
| 34 |
} |
| 35 |
|
| 36 |
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 ), $attributes['linkTarget'], $attributes['rel'], $title ); |
| 38 |
} |
| 39 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); |
| 40 |
|
| 41 |
return sprintf( |
| 42 |
'<%1$s %2$s>%3$s</%1$s>', |
| 43 |
$tag_name, |
| 44 |
$wrapper_attributes, |
| 45 |
$title |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Registers the `core/post-title` block on the server. |
| 51 |
*/ |
| 52 |
function gutenberg_register_block_core_post_title() { |
| 53 |
register_block_type_from_metadata( |
| 54 |
__DIR__ . '/post-title', |
| 55 |
array( |
| 56 |
'render_callback' => 'gutenberg_render_block_core_post_title', |
| 57 |
) |
| 58 |
); |
| 59 |
} |
| 60 |
add_action( 'init', 'gutenberg_register_block_core_post_title', 20 ); |
| 61 |
|