18
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Server-side rendering of the `core/query-title` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
|
|
8 |
/** |
|
9 |
* Renders the `core/query-title` block on the server. |
|
10 |
* For now it only supports Archive title, |
|
11 |
* using queried object information |
|
12 |
* |
|
13 |
* @param array $attributes Block attributes. |
|
14 |
* |
|
15 |
* @return string Returns the query title based on the queried object. |
|
16 |
*/ |
|
17 |
function render_block_core_query_title( $attributes ) { |
|
18 |
$type = isset( $attributes['type'] ) ? $attributes['type'] : null; |
|
19 |
$is_archive = is_archive(); |
|
20 |
if ( ! $type || ( 'archive' === $type && ! $is_archive ) ) { |
|
21 |
return ''; |
|
22 |
} |
|
23 |
$title = ''; |
|
24 |
if ( $is_archive ) { |
|
25 |
$title = get_the_archive_title(); |
|
26 |
} |
|
27 |
$tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; |
|
28 |
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; |
|
29 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); |
|
30 |
return sprintf( |
|
31 |
'<%1$s %2$s>%3$s</%1$s>', |
|
32 |
$tag_name, |
|
33 |
$wrapper_attributes, |
|
34 |
$title |
|
35 |
); |
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* Registers the `core/query-title` block on the server. |
|
40 |
*/ |
|
41 |
function register_block_core_query_title() { |
|
42 |
register_block_type_from_metadata( |
|
43 |
__DIR__ . '/query-title', |
|
44 |
array( |
|
45 |
'render_callback' => 'render_block_core_query_title', |
|
46 |
) |
|
47 |
); |
|
48 |
} |
|
49 |
add_action( 'init', 'register_block_core_query_title' ); |