wp/wp-includes/blocks/home-link.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     6  */
     6  */
     7 
     7 
     8 /**
     8 /**
     9  * Build an array with CSS classes and inline styles defining the colors
     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.
    10  * which will be applied to the home link markup in the front-end.
       
    11  *
       
    12  * @since 6.0.0
    11  *
    13  *
    12  * @param  array $context home link block context.
    14  * @param  array $context home link block context.
    13  * @return array Colors CSS classes and inline styles.
    15  * @return array Colors CSS classes and inline styles.
    14  */
    16  */
    15 function block_core_home_link_build_css_colors( $context ) {
    17 function block_core_home_link_build_css_colors( $context ) {
    59 
    61 
    60 /**
    62 /**
    61  * Build an array with CSS classes and inline styles defining the font sizes
    63  * Build an array with CSS classes and inline styles defining the font sizes
    62  * which will be applied to the home link markup in the front-end.
    64  * which will be applied to the home link markup in the front-end.
    63  *
    65  *
       
    66  * @since 6.0.0
       
    67  *
    64  * @param  array $context Home link block context.
    68  * @param  array $context Home link block context.
    65  * @return array Font size CSS classes and inline styles.
    69  * @return array Font size CSS classes and inline styles.
    66  */
    70  */
    67 function block_core_home_link_build_css_font_sizes( $context ) {
    71 function block_core_home_link_build_css_font_sizes( $context ) {
    68 	// CSS classes.
    72 	// CSS classes.
    86 }
    90 }
    87 
    91 
    88 /**
    92 /**
    89  * Builds an array with classes and style for the li wrapper
    93  * Builds an array with classes and style for the li wrapper
    90  *
    94  *
       
    95  * @since 6.0.0
       
    96  *
    91  * @param  array $context    Home link block context.
    97  * @param  array $context    Home link block context.
    92  * @return array The li wrapper attributes.
    98  * @return string The li wrapper attributes.
    93  */
    99  */
    94 function block_core_home_link_build_li_wrapper_attributes( $context ) {
   100 function block_core_home_link_build_li_wrapper_attributes( $context ) {
    95 	$colors          = block_core_home_link_build_css_colors( $context );
   101 	$colors          = block_core_home_link_build_css_colors( $context );
    96 	$font_sizes      = block_core_home_link_build_css_font_sizes( $context );
   102 	$font_sizes      = block_core_home_link_build_css_font_sizes( $context );
    97 	$classes         = array_merge(
   103 	$classes         = array_merge(
    98 		$colors['css_classes'],
   104 		$colors['css_classes'],
    99 		$font_sizes['css_classes']
   105 		$font_sizes['css_classes']
   100 	);
   106 	);
   101 	$style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
   107 	$style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
   102 	$css_classes     = trim( implode( ' ', $classes ) ) . ' wp-block-navigation-item';
   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 	}
   103 
   116 
   104 	$wrapper_attributes = get_block_wrapper_attributes(
   117 	$wrapper_attributes = get_block_wrapper_attributes(
   105 		array(
   118 		array(
   106 			'class' => $css_classes,
   119 			'class' => implode( ' ', $classes ),
   107 			'style' => $style_attribute,
   120 			'style' => $style_attribute,
   108 		)
   121 		)
   109 	);
   122 	);
   110 
   123 
   111 	return $wrapper_attributes;
   124 	return $wrapper_attributes;
   112 }
   125 }
   113 
   126 
   114 /**
   127 /**
   115  * Renders the `core/home-link` block.
   128  * Renders the `core/home-link` block.
   116  *
   129  *
       
   130  * @since 6.0.0
       
   131  *
   117  * @param array    $attributes The block attributes.
   132  * @param array    $attributes The block attributes.
   118  * @param string   $content    The saved content.
   133  * @param string   $content    The saved content.
   119  * @param WP_Block $block      The parsed block.
   134  * @param WP_Block $block      The parsed block.
   120  *
   135  *
   121  * @return string Returns the post content with the home url added.
   136  * @return string Returns the post content with the home url added.
   122  */
   137  */
   123 function render_block_core_home_link( $attributes, $content, $block ) {
   138 function render_block_core_home_link( $attributes, $content, $block ) {
   124 	if ( empty( $attributes['label'] ) ) {
   139 	if ( empty( $attributes['label'] ) ) {
   125 		return '';
   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"';
   126 	}
   152 	}
   127 
   153 
   128 	$wrapper_attributes = block_core_home_link_build_li_wrapper_attributes( $block->context );
   154 	return sprintf(
   129 
   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>',
   130 	$aria_current = is_home() || ( is_front_page() && 'page' === get_option( 'show_on_front' ) ) ? ' aria-current="page"' : '';
   156 		block_core_home_link_build_li_wrapper_attributes( $block->context ),
   131 
   157 		esc_url( home_url() ),
   132 	$html = '<li ' . $wrapper_attributes . '><a class="wp-block-home-link__content wp-block-navigation-item__content" rel="home"' . $aria_current;
   158 		$aria_current,
   133 
   159 		wp_kses_post( $attributes['label'] )
   134 	// Start appending HTML attributes to anchor tag.
   160 	);
   135 	$html .= ' href="' . esc_url( home_url() ) . '"';
       
   136 
       
   137 	// End appending HTML attributes to anchor tag.
       
   138 	$html .= '>';
       
   139 
       
   140 	if ( isset( $attributes['label'] ) ) {
       
   141 		$html .= wp_kses_post( $attributes['label'] );
       
   142 	}
       
   143 
       
   144 	$html .= '</a></li>';
       
   145 	return $html;
       
   146 }
   161 }
   147 
   162 
   148 /**
   163 /**
   149  * Register the home block
   164  * Register the home block
       
   165  *
       
   166  * @since 6.0.0
   150  *
   167  *
   151  * @uses render_block_core_home_link()
   168  * @uses render_block_core_home_link()
   152  * @throws WP_Error An WP_Error exception parsing the block definition.
   169  * @throws WP_Error An WP_Error exception parsing the block definition.
   153  */
   170  */
   154 function register_block_core_home_link() {
   171 function register_block_core_home_link() {