| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 * Builds an array with classes and style for the li wrapper 64 * 65 * @since 6.0.0 66 * 67 * @param array $context Home link block context. 68 * @return string The li wrapper attributes. 69 */ 70 function block_core_home_link_build_li_wrapper_attributes( $context ) { 71 $colors = block_core_home_link_build_css_colors( $context ); 72 $classes = array_merge( 73 $colors['css_classes'] 74 ); 75 76 $style_attribute = ( $colors['inline_styles'] ); 77 $classes[] = 'wp-block-navigation-item'; 78 79 if ( is_front_page() ) { 80 $classes[] = 'current-menu-item'; 81 } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) { 82 // Edge case where the Reading settings has a posts page set but not a static homepage. 83 $classes[] = 'current-menu-item'; 84 } 85 86 $wrapper_attributes = get_block_wrapper_attributes( 87 array( 88 'class' => implode( ' ', $classes ), 89 'style' => $style_attribute, 90 ) 91 ); 92 93 return $wrapper_attributes; 94 } 95 96 /** 97 * Renders the `core/home-link` block. 98 * 99 * @since 6.0.0 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 $attributes['label'] = __( 'Home' ); 110 } 111 $aria_current = ''; 112 113 if ( is_front_page() ) { 114 $aria_current = ' aria-current="page"'; 115 } elseif ( is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) { 116 // Edge case where the Reading settings has a posts page set but not a static homepage. 117 $aria_current = ' aria-current="page"'; 118 } 119 120 $target = ''; 121 if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) { 122 $target = ' target="_blank"'; 123 } 124 125 $description = ''; 126 if ( ! empty( $attributes['description'] ) ) { 127 $description = '<span class="wp-block-navigation-item__description">' . wp_kses_post( $attributes['description'] ) . '</span>'; 128 } 129 130 return sprintf( 131 '<li %1$s><a class="wp-block-home-link__content wp-block-navigation-item__content" href="%2$s" rel="home" %3$s%4$s>%5$s%6$s</a></li>', 132 block_core_home_link_build_li_wrapper_attributes( $block->context ), 133 esc_url( home_url() ), 134 $target, 135 $aria_current, 136 wp_kses_post( $attributes['label'] ), 137 $description 138 ); 139 } 140 141 /** 142 * Register the home block 143 * 144 * @since 6.0.0 145 * 146 * @uses render_block_core_home_link() 147 * @throws WP_Error An WP_Error exception parsing the block definition. 148 */ 149 function register_block_core_home_link() { 150 register_block_type_from_metadata( 151 __DIR__ . '/home-link', 152 array( 153 'render_callback' => 'render_block_core_home_link', 154 ) 155 ); 156 } 157 add_action( 'init', 'register_block_core_home_link' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Jul 4 08:20:12 2026 | Cross-referenced by PHPXref |