[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> home-link.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/home-link` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Build an array with CSS classes and inline styles defining the colors
  10   * which will be applied to the home link markup in the front-end.
  11   *
  12   * @since 6.0.0
  13   *
  14   * @param  array $context home link block context.
  15   * @return array Colors CSS classes and inline styles.
  16   */
  17  function block_core_home_link_build_css_colors( $context ) {
  18      $colors = array(
  19          'css_classes'   => array(),
  20          'inline_styles' => '',
  21      );
  22  
  23      // Text color.
  24      $has_named_text_color  = array_key_exists( 'textColor', $context );
  25      $has_custom_text_color = isset( $context['style']['color']['text'] );
  26  
  27      // If has text color.
  28      if ( $has_custom_text_color || $has_named_text_color ) {
  29          // Add has-text-color class.
  30          $colors['css_classes'][] = 'has-text-color';
  31      }
  32  
  33      if ( $has_named_text_color ) {
  34          // Add the color class.
  35          $colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] );
  36      } elseif ( $has_custom_text_color ) {
  37          // Add the custom color inline style.
  38          $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] );
  39      }
  40  
  41      // Background color.
  42      $has_named_background_color  = array_key_exists( 'backgroundColor', $context );
  43      $has_custom_background_color = isset( $context['style']['color']['background'] );
  44  
  45      // If has background color.
  46      if ( $has_custom_background_color || $has_named_background_color ) {
  47          // Add has-background class.
  48          $colors['css_classes'][] = 'has-background';
  49      }
  50  
  51      if ( $has_named_background_color ) {
  52          // Add the background-color class.
  53          $colors['css_classes'][] = sprintf( 'has-%s-background-color', $context['backgroundColor'] );
  54      } elseif ( $has_custom_background_color ) {
  55          // Add the custom background-color inline style.
  56          $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] );
  57      }
  58  
  59      return $colors;
  60  }
  61  
  62  /**
  63   * Build an array with CSS classes and inline styles defining the font sizes
  64   * which will be applied to the home link markup in the front-end.
  65   *
  66   * @since 6.0.0
  67   *
  68   * @param  array $context Home link block context.
  69   * @return array Font size CSS classes and inline styles.
  70   */
  71  function block_core_home_link_build_css_font_sizes( $context ) {
  72      // CSS classes.
  73      $font_sizes = array(
  74          'css_classes'   => array(),
  75          'inline_styles' => '',
  76      );
  77  
  78      $has_named_font_size  = array_key_exists( 'fontSize', $context );
  79      $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
  80  
  81      if ( $has_named_font_size ) {
  82          // Add the font size class.
  83          $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
  84      } elseif ( $has_custom_font_size ) {
  85          // Add the custom font size inline style.
  86          $font_sizes['inline_styles'] = sprintf( 'font-size: %s;', $context['style']['typography']['fontSize'] );
  87      }
  88  
  89      return $font_sizes;
  90  }
  91  
  92  /**
  93   * Builds an array with classes and style for the li wrapper
  94   *
  95   * @since 6.0.0
  96   *
  97   * @param  array $context    Home link block context.
  98   * @return string The li wrapper attributes.
  99   */
 100  function block_core_home_link_build_li_wrapper_attributes( $context ) {
 101      $colors          = block_core_home_link_build_css_colors( $context );
 102      $font_sizes      = block_core_home_link_build_css_font_sizes( $context );
 103      $classes         = array_merge(
 104          $colors['css_classes'],
 105          $font_sizes['css_classes']
 106      );
 107      $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
 108      $classes[]       = 'wp-block-navigation-item';
 109  
 110      if ( is_front_page() ) {
 111          $classes[] = 'current-menu-item';
 112      } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) {
 113          // Edge case where the Reading settings has a posts page set but not a static homepage.
 114          $classes[] = 'current-menu-item';
 115      }
 116  
 117      $wrapper_attributes = get_block_wrapper_attributes(
 118          array(
 119              'class' => implode( ' ', $classes ),
 120              'style' => $style_attribute,
 121          )
 122      );
 123  
 124      return $wrapper_attributes;
 125  }
 126  
 127  /**
 128   * Renders the `core/home-link` block.
 129   *
 130   * @since 6.0.0
 131   *
 132   * @param array    $attributes The block attributes.
 133   * @param string   $content    The saved content.
 134   * @param WP_Block $block      The parsed block.
 135   *
 136   * @return string Returns the post content with the home url added.
 137   */
 138  function render_block_core_home_link( $attributes, $content, $block ) {
 139      if ( empty( $attributes['label'] ) ) {
 140          // Using a fallback for the label attribute allows rendering the block even if no attributes have been set,
 141          // e.g. when using the block as a hooked block.
 142          // Note that the fallback value needs to be kept in sync with the one set in `edit.js` (upon first loading the block in the editor).
 143          $attributes['label'] = __( 'Home' );
 144      }
 145      $aria_current = '';
 146  
 147      if ( is_front_page() ) {
 148          $aria_current = ' aria-current="page"';
 149      } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) {
 150          // Edge case where the Reading settings has a posts page set but not a static homepage.
 151          $aria_current = ' aria-current="page"';
 152      }
 153  
 154      return sprintf(
 155          '<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>',
 156          block_core_home_link_build_li_wrapper_attributes( $block->context ),
 157          esc_url( home_url() ),
 158          $aria_current,
 159          wp_kses_post( $attributes['label'] )
 160      );
 161  }
 162  
 163  /**
 164   * Register the home block
 165   *
 166   * @since 6.0.0
 167   *
 168   * @uses render_block_core_home_link()
 169   * @throws WP_Error An WP_Error exception parsing the block definition.
 170   */
 171  function register_block_core_home_link() {
 172      register_block_type_from_metadata(
 173          __DIR__ . '/home-link',
 174          array(
 175              'render_callback' => 'render_block_core_home_link',
 176          )
 177      );
 178  }
 179  add_action( 'init', 'register_block_core_home_link' );


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref