home-link.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/home-link` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Build an array with CSS classes and inline styles defining the colors
  9. * which will be applied to the home link markup in the front-end.
  10. *
  11. * @param array $context home link block context.
  12. * @return array Colors CSS classes and inline styles.
  13. */
  14. function block_core_home_link_build_css_colors( $context ) {
  15. $colors = array(
  16. 'css_classes' => array(),
  17. 'inline_styles' => '',
  18. );
  19. // Text color.
  20. $has_named_text_color = array_key_exists( 'textColor', $context );
  21. $has_custom_text_color = isset( $context['style']['color']['text'] );
  22. // If has text color.
  23. if ( $has_custom_text_color || $has_named_text_color ) {
  24. // Add has-text-color class.
  25. $colors['css_classes'][] = 'has-text-color';
  26. }
  27. if ( $has_named_text_color ) {
  28. // Add the color class.
  29. $colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] );
  30. } elseif ( $has_custom_text_color ) {
  31. // Add the custom color inline style.
  32. $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] );
  33. }
  34. // Background color.
  35. $has_named_background_color = array_key_exists( 'backgroundColor', $context );
  36. $has_custom_background_color = isset( $context['style']['color']['background'] );
  37. // If has background color.
  38. if ( $has_custom_background_color || $has_named_background_color ) {
  39. // Add has-background class.
  40. $colors['css_classes'][] = 'has-background';
  41. }
  42. if ( $has_named_background_color ) {
  43. // Add the background-color class.
  44. $colors['css_classes'][] = sprintf( 'has-%s-background-color', $context['backgroundColor'] );
  45. } elseif ( $has_custom_background_color ) {
  46. // Add the custom background-color inline style.
  47. $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] );
  48. }
  49. return $colors;
  50. }
  51. /**
  52. * Build an array with CSS classes and inline styles defining the font sizes
  53. * which will be applied to the home link markup in the front-end.
  54. *
  55. * @param array $context Home link block context.
  56. * @return array Font size CSS classes and inline styles.
  57. */
  58. function block_core_home_link_build_css_font_sizes( $context ) {
  59. // CSS classes.
  60. $font_sizes = array(
  61. 'css_classes' => array(),
  62. 'inline_styles' => '',
  63. );
  64. $has_named_font_size = array_key_exists( 'fontSize', $context );
  65. $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
  66. if ( $has_named_font_size ) {
  67. // Add the font size class.
  68. $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
  69. } elseif ( $has_custom_font_size ) {
  70. // Add the custom font size inline style.
  71. $font_sizes['inline_styles'] = sprintf( 'font-size: %s;', $context['style']['typography']['fontSize'] );
  72. }
  73. return $font_sizes;
  74. }
  75. /**
  76. * Builds an array with classes and style for the li wrapper
  77. *
  78. * @param array $context Home link block context.
  79. * @return string The li wrapper attributes.
  80. */
  81. function block_core_home_link_build_li_wrapper_attributes( $context ) {
  82. $colors = block_core_home_link_build_css_colors( $context );
  83. $font_sizes = block_core_home_link_build_css_font_sizes( $context );
  84. $classes = array_merge(
  85. $colors['css_classes'],
  86. $font_sizes['css_classes']
  87. );
  88. $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
  89. $css_classes = trim( implode( ' ', $classes ) ) . ' wp-block-navigation-item';
  90. $wrapper_attributes = get_block_wrapper_attributes(
  91. array(
  92. 'class' => $css_classes,
  93. 'style' => $style_attribute,
  94. )
  95. );
  96. return $wrapper_attributes;
  97. }
  98. /**
  99. * Renders the `core/home-link` block.
  100. *
  101. * @param array $attributes The block attributes.
  102. * @param string $content The saved content.
  103. * @param WP_Block $block The parsed block.
  104. *
  105. * @return string Returns the post content with the home url added.
  106. */
  107. function render_block_core_home_link( $attributes, $content, $block ) {
  108. if ( empty( $attributes['label'] ) ) {
  109. return '';
  110. }
  111. $aria_current = is_home() || ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) ? ' aria-current="page"' : '';
  112. return sprintf(
  113. '<li %1$s><a class="wp-block-home-link__content wp-block-navigation-item__content" href="%2$s" "rel="home"%3$s>%4$s</a></li>',
  114. block_core_home_link_build_li_wrapper_attributes( $block->context ),
  115. esc_url( home_url() ),
  116. $aria_current,
  117. wp_kses_post( $attributes['label'] )
  118. );
  119. }
  120. /**
  121. * Register the home block
  122. *
  123. * @uses render_block_core_home_link()
  124. * @throws WP_Error An WP_Error exception parsing the block definition.
  125. */
  126. function register_block_core_home_link() {
  127. register_block_type_from_metadata(
  128. __DIR__ . '/home-link',
  129. array(
  130. 'render_callback' => 'render_block_core_home_link',
  131. )
  132. );
  133. }
  134. add_action( 'init', 'register_block_core_home_link' );