Nenhuma Descrição

site-title.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. $aria_current = is_home() || ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) ? ' aria-current="page"' : '';
  22. if ( isset( $attributes['level'] ) ) {
  23. $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . (int) $attributes['level'];
  24. }
  25. if ( $attributes['isLink'] ) {
  26. $link_attrs = array(
  27. 'href="' . esc_url( get_bloginfo( 'url' ) ) . '"',
  28. 'rel="' . esc_attr( 'home' ) . '"',
  29. $aria_current,
  30. );
  31. if ( '_blank' === $attributes['linkTarget'] ) {
  32. $link_attrs[] = 'target="_blank"';
  33. }
  34. $site_title = sprintf( '<a %1$s>%2$s</a>', implode( ' ', $link_attrs ), esc_html( $site_title ) );
  35. }
  36. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
  37. return sprintf(
  38. '<%1$s %2$s>%3$s</%1$s>',
  39. $tag_name,
  40. $wrapper_attributes,
  41. // already pre-escaped if it is a link.
  42. $attributes['isLink'] ? $site_title : esc_html( $site_title )
  43. );
  44. }
  45. /**
  46. * Registers the `core/site-title` block on the server.
  47. */
  48. function register_block_core_site_title() {
  49. register_block_type_from_metadata(
  50. __DIR__ . '/site-title',
  51. array(
  52. 'render_callback' => 'render_block_core_site_title',
  53. )
  54. );
  55. }
  56. add_action( 'init', 'register_block_core_site_title' );