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 * @since 5.8.0
12 *
13 * @param array $attributes The block attributes.
14 *
15 * @return string The render.
16 */
17function render_block_core_site_title( $attributes ) {
18 $site_title = get_bloginfo( 'name' );
19 if ( ! trim( $site_title ) ) {
20 return '';
21 }
22
23 $tag_name = 'h1';
24 $classes = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
25 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
26 $classes .= ' has-link-color';
27 }
28
29 if ( isset( $attributes['level'] ) ) {
30 $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . (int) $attributes['level'];
31 }
32
33 if ( $attributes['isLink'] ) {
34 $aria_current = ! is_paged() && ( is_front_page() || is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) ? ' aria-current="page"' : '';
35 $link_target = ! empty( $attributes['linkTarget'] ) ? $attributes['linkTarget'] : '_self';
36
37 $site_title = sprintf(
38 '<a href="%1$s" target="%2$s" rel="home"%3$s>%4$s</a>',
39 esc_url( home_url() ),
40 esc_attr( $link_target ),
41 $aria_current,
42 esc_html( $site_title )
43 );
44 }
45 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( $classes ) ) );
46
47 return sprintf(
48 '<%1$s %2$s>%3$s</%1$s>',
49 $tag_name,
50 $wrapper_attributes,
51 // already pre-escaped if it is a link.
52 $attributes['isLink'] ? $site_title : esc_html( $site_title )
53 );
54}
55
56/**
57 * Registers the `core/site-title` block on the server.
58 *
59 * @since 5.8.0
60 */
61function register_block_core_site_title() {
62 register_block_type_from_metadata(
63 __DIR__ . '/site-title',
64 array(
65 'render_callback' => 'render_block_core_site_title',
66 )
67 );
68}
69add_action( 'init', 'register_block_core_site_title' );
70