site-title.php 1.6 KB

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