18
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Server-side rendering of the `core/site-title` block. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
|
|
8 |
/** |
|
9 |
* Renders the `core/site-title` block on the server. |
|
10 |
* |
|
11 |
* @param array $attributes The block attributes. |
|
12 |
* |
|
13 |
* @return string The render. |
|
14 |
*/ |
|
15 |
function render_block_core_site_title( $attributes ) { |
|
16 |
$site_title = get_bloginfo( 'name' ); |
|
17 |
if ( ! $site_title ) { |
|
18 |
return; |
|
19 |
} |
|
20 |
|
|
21 |
$tag_name = 'h1'; |
|
22 |
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; |
|
23 |
|
19
|
24 |
$aria_current = is_home() || ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) ? ' aria-current="page"' : ''; |
|
25 |
|
18
|
26 |
if ( isset( $attributes['level'] ) ) { |
19
|
27 |
$tag_name = 0 === $attributes['level'] ? 'p' : 'h' . (int) $attributes['level']; |
18
|
28 |
} |
|
29 |
|
19
|
30 |
if ( $attributes['isLink'] ) { |
|
31 |
$link_attrs = array( |
|
32 |
'href="' . esc_url( get_bloginfo( 'url' ) ) . '"', |
|
33 |
'rel="' . esc_attr( 'home' ) . '"', |
|
34 |
$aria_current, |
|
35 |
); |
|
36 |
if ( '_blank' === $attributes['linkTarget'] ) { |
|
37 |
$link_attrs[] = 'target="_blank"'; |
|
38 |
} |
|
39 |
$site_title = sprintf( '<a %1$s>%2$s</a>', implode( ' ', $link_attrs ), esc_html( $site_title ) ); |
|
40 |
} |
18
|
41 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); |
|
42 |
|
|
43 |
return sprintf( |
|
44 |
'<%1$s %2$s>%3$s</%1$s>', |
|
45 |
$tag_name, |
|
46 |
$wrapper_attributes, |
19
|
47 |
// already pre-escaped if it is a link. |
|
48 |
$attributes['isLink'] ? $site_title : esc_html( $site_title ) |
18
|
49 |
); |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* Registers the `core/site-title` block on the server. |
|
54 |
*/ |
|
55 |
function register_block_core_site_title() { |
|
56 |
register_block_type_from_metadata( |
|
57 |
__DIR__ . '/site-title', |
|
58 |
array( |
|
59 |
'render_callback' => 'render_block_core_site_title', |
|
60 |
) |
|
61 |
); |
|
62 |
} |
|
63 |
add_action( 'init', 'register_block_core_site_title' ); |