[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Server-side rendering of the `core/navigation-submenu` 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 navigation markup in the front-end. 11 * 12 * @param array $context Navigation block context. 13 * @param array $attributes Block attributes. 14 * @return array Colors CSS classes and inline styles. 15 */ 16 function block_core_navigation_submenu_build_css_colors( $context, $attributes ) { 17 $colors = array( 18 'css_classes' => array(), 19 'inline_styles' => '', 20 ); 21 22 $is_sub_menu = isset( $attributes['isTopLevelItem'] ) ? ( ! $attributes['isTopLevelItem'] ) : false; 23 24 // Text color. 25 $named_text_color = null; 26 $custom_text_color = null; 27 28 if ( $is_sub_menu && array_key_exists( 'customOverlayTextColor', $context ) ) { 29 $custom_text_color = $context['customOverlayTextColor']; 30 } elseif ( $is_sub_menu && array_key_exists( 'overlayTextColor', $context ) ) { 31 $named_text_color = $context['overlayTextColor']; 32 } elseif ( array_key_exists( 'customTextColor', $context ) ) { 33 $custom_text_color = $context['customTextColor']; 34 } elseif ( array_key_exists( 'textColor', $context ) ) { 35 $named_text_color = $context['textColor']; 36 } elseif ( isset( $context['style']['color']['text'] ) ) { 37 $custom_text_color = $context['style']['color']['text']; 38 } 39 40 // If has text color. 41 if ( ! is_null( $named_text_color ) ) { 42 // Add the color class. 43 array_push( $colors['css_classes'], 'has-text-color', sprintf( 'has-%s-color', $named_text_color ) ); 44 } elseif ( ! is_null( $custom_text_color ) ) { 45 // Add the custom color inline style. 46 $colors['css_classes'][] = 'has-text-color'; 47 $colors['inline_styles'] .= sprintf( 'color: %s;', $custom_text_color ); 48 } 49 50 // Background color. 51 $named_background_color = null; 52 $custom_background_color = null; 53 54 if ( $is_sub_menu && array_key_exists( 'customOverlayBackgroundColor', $context ) ) { 55 $custom_background_color = $context['customOverlayBackgroundColor']; 56 } elseif ( $is_sub_menu && array_key_exists( 'overlayBackgroundColor', $context ) ) { 57 $named_background_color = $context['overlayBackgroundColor']; 58 } elseif ( array_key_exists( 'customBackgroundColor', $context ) ) { 59 $custom_background_color = $context['customBackgroundColor']; 60 } elseif ( array_key_exists( 'backgroundColor', $context ) ) { 61 $named_background_color = $context['backgroundColor']; 62 } elseif ( isset( $context['style']['color']['background'] ) ) { 63 $custom_background_color = $context['style']['color']['background']; 64 } 65 66 // If has background color. 67 if ( ! is_null( $named_background_color ) ) { 68 // Add the background-color class. 69 array_push( $colors['css_classes'], 'has-background', sprintf( 'has-%s-background-color', $named_background_color ) ); 70 } elseif ( ! is_null( $custom_background_color ) ) { 71 // Add the custom background-color inline style. 72 $colors['css_classes'][] = 'has-background'; 73 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $custom_background_color ); 74 } 75 76 return $colors; 77 } 78 79 /** 80 * Build an array with CSS classes and inline styles defining the font sizes 81 * which will be applied to the navigation markup in the front-end. 82 * 83 * @param array $context Navigation block context. 84 * @return array Font size CSS classes and inline styles. 85 */ 86 function block_core_navigation_submenu_build_css_font_sizes( $context ) { 87 // CSS classes. 88 $font_sizes = array( 89 'css_classes' => array(), 90 'inline_styles' => '', 91 ); 92 93 $has_named_font_size = array_key_exists( 'fontSize', $context ); 94 $has_custom_font_size = isset( $context['style']['typography']['fontSize'] ); 95 96 if ( $has_named_font_size ) { 97 // Add the font size class. 98 $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] ); 99 } elseif ( $has_custom_font_size ) { 100 // Add the custom font size inline style. 101 $font_sizes['inline_styles'] = sprintf( 102 'font-size: %s;', 103 wp_get_typography_font_size_value( 104 array( 105 'size' => $context['style']['typography']['fontSize'], 106 ) 107 ) 108 ); 109 } 110 111 return $font_sizes; 112 } 113 114 /** 115 * Returns the top-level submenu SVG chevron icon. 116 * 117 * @return string 118 */ 119 function block_core_navigation_submenu_render_submenu_icon() { 120 return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg>'; 121 } 122 123 /** 124 * Renders the `core/navigation-submenu` block. 125 * 126 * @param array $attributes The block attributes. 127 * @param string $content The saved content. 128 * @param WP_Block $block The parsed block. 129 * 130 * @return string Returns the post content with the legacy widget added. 131 */ 132 function render_block_core_navigation_submenu( $attributes, $content, $block ) { 133 134 $navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] ); 135 $is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind']; 136 $is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] ); 137 138 // Don't render the block's subtree if it is a draft. 139 if ( $is_post_type && $navigation_link_has_id && 'publish' !== get_post_status( $attributes['id'] ) ) { 140 return ''; 141 } 142 143 // Don't render the block's subtree if it has no label. 144 if ( empty( $attributes['label'] ) ) { 145 return ''; 146 } 147 148 $colors = block_core_navigation_submenu_build_css_colors( $block->context, $attributes ); 149 $font_sizes = block_core_navigation_submenu_build_css_font_sizes( $block->context ); 150 $classes = array_merge( 151 $colors['css_classes'], 152 $font_sizes['css_classes'] 153 ); 154 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); 155 156 $css_classes = trim( implode( ' ', $classes ) ); 157 $has_submenu = count( $block->inner_blocks ) > 0; 158 $is_active = ! empty( $attributes['id'] ) && ( get_the_ID() === (int) $attributes['id'] ); 159 160 $show_submenu_indicators = isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon']; 161 $open_on_click = isset( $block->context['openSubmenusOnClick'] ) && $block->context['openSubmenusOnClick']; 162 $open_on_hover_and_click = isset( $block->context['openSubmenusOnClick'] ) && ! $block->context['openSubmenusOnClick'] && 163 $show_submenu_indicators; 164 165 $wrapper_attributes = get_block_wrapper_attributes( 166 array( 167 'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) . 168 ( $open_on_click ? ' open-on-click' : '' ) . ( $open_on_hover_and_click ? ' open-on-hover-click' : '' ) . 169 ( $is_active ? ' current-menu-item' : '' ), 170 'style' => $style_attribute, 171 ) 172 ); 173 174 $label = ''; 175 176 if ( isset( $attributes['label'] ) ) { 177 $label .= wp_kses_post( $attributes['label'] ); 178 } 179 180 $aria_label = sprintf( 181 /* translators: Accessibility text. %s: Parent page title. */ 182 __( '%s submenu' ), 183 wp_strip_all_tags( $label ) 184 ); 185 186 $html = '<li ' . $wrapper_attributes . '>'; 187 188 // If Submenus open on hover, we render an anchor tag with attributes. 189 // If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click. 190 if ( ! $open_on_click ) { 191 $item_url = isset( $attributes['url'] ) ? $attributes['url'] : ''; 192 // Start appending HTML attributes to anchor tag. 193 $html .= '<a class="wp-block-navigation-item__content"'; 194 195 // The href attribute on a and area elements is not required; 196 // when those elements do not have href attributes they do not create hyperlinks. 197 // But also The href attribute must have a value that is a valid URL potentially 198 // surrounded by spaces. 199 // see: https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements. 200 if ( ! empty( $item_url ) ) { 201 $html .= ' href="' . esc_url( $item_url ) . '"'; 202 } 203 204 if ( $is_active ) { 205 $html .= ' aria-current="page"'; 206 } 207 208 if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) { 209 $html .= ' target="_blank" '; 210 } 211 212 if ( isset( $attributes['rel'] ) ) { 213 $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"'; 214 } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) { 215 $html .= ' rel="nofollow"'; 216 } 217 218 if ( isset( $attributes['title'] ) ) { 219 $html .= ' title="' . esc_attr( $attributes['title'] ) . '"'; 220 } 221 222 $html .= '>'; 223 // End appending HTML attributes to anchor tag. 224 225 $html .= $label; 226 227 $html .= '</a>'; 228 // End anchor tag content. 229 230 if ( $show_submenu_indicators ) { 231 // The submenu icon is rendered in a button here 232 // so that there's a clickable element to open the submenu. 233 $html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">' . block_core_navigation_submenu_render_submenu_icon() . '</button>'; 234 } 235 } else { 236 // If menus open on click, we render the parent as a button. 237 $html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">'; 238 239 // Wrap title with span to isolate it from submenu icon. 240 $html .= '<span class="wp-block-navigation-item__label">'; 241 242 $html .= $label; 243 244 $html .= '</span>'; 245 246 $html .= '</button>'; 247 248 $html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_submenu_render_submenu_icon() . '</span>'; 249 250 } 251 252 if ( $has_submenu ) { 253 $inner_blocks_html = ''; 254 foreach ( $block->inner_blocks as $inner_block ) { 255 $inner_blocks_html .= $inner_block->render(); 256 } 257 258 $html .= sprintf( 259 '<ul class="wp-block-navigation__submenu-container">%s</ul>', 260 $inner_blocks_html 261 ); 262 } 263 264 $html .= '</li>'; 265 266 return $html; 267 } 268 269 /** 270 * Register the navigation submenu block. 271 * 272 * @uses render_block_core_navigation_submenu() 273 * @throws WP_Error An WP_Error exception parsing the block definition. 274 */ 275 function register_block_core_navigation_submenu() { 276 register_block_type_from_metadata( 277 __DIR__ . '/navigation-submenu', 278 array( 279 'render_callback' => 'render_block_core_navigation_submenu', 280 ) 281 ); 282 } 283 add_action( 'init', 'register_block_core_navigation_submenu' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Mon Jan 30 08:20:01 2023 | Cross-referenced by PHPXref |