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