[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Server-side rendering of the `core/navigation-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 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_link_build_css_colors( $context, $attributes ) { 17 $colors = array( 18 'css_classes' => array(), 19 'inline_styles' => '', 20 ); 21 22 $is_sub_menu = isset( $attributes['isTopLevelLink'] ) ? ( ! $attributes['isTopLevelLink'] ) : 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_link_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( 'font-size: %s;', $context['style']['typography']['fontSize'] ); 102 } 103 104 return $font_sizes; 105 } 106 107 /** 108 * Returns the top-level submenu SVG chevron icon. 109 * 110 * @return string 111 */ 112 function block_core_navigation_link_render_submenu_icon() { 113 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>'; 114 } 115 116 /** 117 * Renders the `core/navigation-link` block. 118 * 119 * @param array $attributes The block attributes. 120 * @param string $content The saved content. 121 * @param WP_Block $block The parsed block. 122 * 123 * @return string Returns the post content with the legacy widget added. 124 */ 125 function render_block_core_navigation_link( $attributes, $content, $block ) { 126 $navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] ); 127 $is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind']; 128 $is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] ); 129 130 // Don't render the block's subtree if it is a draft or if the ID does not exist. 131 if ( $is_post_type && $navigation_link_has_id ) { 132 $post = get_post( $attributes['id'] ); 133 if ( ! $post || 'publish' !== $post->post_status ) { 134 return ''; 135 } 136 } 137 138 // Don't render the block's subtree if it has no label. 139 if ( empty( $attributes['label'] ) ) { 140 return ''; 141 } 142 143 $colors = block_core_navigation_link_build_css_colors( $block->context, $attributes ); 144 $font_sizes = block_core_navigation_link_build_css_font_sizes( $block->context ); 145 $classes = array_merge( 146 $colors['css_classes'], 147 $font_sizes['css_classes'] 148 ); 149 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); 150 151 $css_classes = trim( implode( ' ', $classes ) ); 152 $has_submenu = count( $block->inner_blocks ) > 0; 153 $is_active = ! empty( $attributes['id'] ) && ( get_the_ID() === $attributes['id'] ); 154 155 $wrapper_attributes = get_block_wrapper_attributes( 156 array( 157 'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) . 158 ( $is_active ? ' current-menu-item' : '' ), 159 'style' => $style_attribute, 160 ) 161 ); 162 $html = '<li ' . $wrapper_attributes . '>' . 163 '<a class="wp-block-navigation-item__content" '; 164 165 // Start appending HTML attributes to anchor tag. 166 if ( isset( $attributes['url'] ) ) { 167 $html .= ' href="' . esc_url( $attributes['url'] ) . '"'; 168 } 169 170 if ( $is_active ) { 171 $html .= ' aria-current="page"'; 172 } 173 174 if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) { 175 $html .= ' target="_blank" '; 176 } 177 178 if ( isset( $attributes['rel'] ) ) { 179 $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"'; 180 } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) { 181 $html .= ' rel="nofollow"'; 182 } 183 184 if ( isset( $attributes['title'] ) ) { 185 $html .= ' title="' . esc_attr( $attributes['title'] ) . '"'; 186 } 187 188 // End appending HTML attributes to anchor tag. 189 190 // Start anchor tag content. 191 $html .= '>' . 192 // Wrap title with span to isolate it from submenu icon. 193 '<span class="wp-block-navigation-item__label">'; 194 195 if ( isset( $attributes['label'] ) ) { 196 $html .= wp_kses_post( $attributes['label'] ); 197 } 198 199 $html .= '</span>'; 200 201 // Add description if available. 202 if ( ! empty( $attributes['description'] ) ) { 203 $html .= '<span class="wp-block-navigation-item__description">'; 204 $html .= wp_kses_post( $attributes['description'] ); 205 $html .= '</span>'; 206 } 207 208 $html .= '</a>'; 209 // End anchor tag content. 210 211 if ( isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'] && $has_submenu ) { 212 // The submenu icon can be hidden by a CSS rule on the Navigation Block. 213 $html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_link_render_submenu_icon() . '</span>'; 214 } 215 216 if ( $has_submenu ) { 217 $inner_blocks_html = ''; 218 foreach ( $block->inner_blocks as $inner_block ) { 219 $inner_blocks_html .= $inner_block->render(); 220 } 221 222 $html .= sprintf( 223 '<ul class="wp-block-navigation__submenu-container">%s</ul>', 224 $inner_blocks_html 225 ); 226 } 227 228 $html .= '</li>'; 229 230 return $html; 231 } 232 233 /** 234 * Returns a navigation link variation 235 * 236 * @param WP_Taxonomy|WP_Post_Type $entity post type or taxonomy entity. 237 * @param string $kind string of value 'taxonomy' or 'post-type'. 238 * 239 * @return array 240 */ 241 function build_variation_for_navigation_link( $entity, $kind ) { 242 $title = ''; 243 $description = ''; 244 245 if ( property_exists( $entity->labels, 'item_link' ) ) { 246 $title = $entity->labels->item_link; 247 } 248 if ( property_exists( $entity->labels, 'item_link_description' ) ) { 249 $description = $entity->labels->item_link_description; 250 } 251 252 $variation = array( 253 'name' => $entity->name, 254 'title' => $title, 255 'description' => $description, 256 'attributes' => array( 257 'type' => $entity->name, 258 'kind' => $kind, 259 ), 260 ); 261 262 // Tweak some value for the variations. 263 $variation_overrides = array( 264 'post_tag' => array( 265 'name' => 'tag', 266 'attributes' => array( 267 'type' => 'tag', 268 'kind' => $kind, 269 ), 270 ), 271 'post_format' => array( 272 // The item_link and item_link_description for post formats is the 273 // same as for tags, so need to be overridden. 274 'title' => __( 'Post Format Link' ), 275 'description' => __( 'A link to a post format' ), 276 'attributes' => array( 277 'type' => 'post_format', 278 'kind' => $kind, 279 ), 280 ), 281 ); 282 283 if ( array_key_exists( $entity->name, $variation_overrides ) ) { 284 $variation = array_merge( 285 $variation, 286 $variation_overrides[ $entity->name ] 287 ); 288 } 289 290 return $variation; 291 } 292 293 /** 294 * Register the navigation link block. 295 * 296 * @uses render_block_core_navigation() 297 * @throws WP_Error An WP_Error exception parsing the block definition. 298 */ 299 function register_block_core_navigation_link() { 300 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); 301 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); 302 303 // Use two separate arrays as a way to order the variations in the UI. 304 // Known variations (like Post Link and Page Link) are added to the 305 // `built_ins` array. Variations for custom post types and taxonomies are 306 // added to the `variations` array and will always appear after `built-ins. 307 $built_ins = array(); 308 $variations = array(); 309 310 if ( $post_types ) { 311 foreach ( $post_types as $post_type ) { 312 $variation = build_variation_for_navigation_link( $post_type, 'post-type' ); 313 if ( $post_type->_builtin ) { 314 $built_ins[] = $variation; 315 } else { 316 $variations[] = $variation; 317 } 318 } 319 } 320 if ( $taxonomies ) { 321 foreach ( $taxonomies as $taxonomy ) { 322 $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); 323 if ( $taxonomy->_builtin ) { 324 $built_ins[] = $variation; 325 } else { 326 $variations[] = $variation; 327 } 328 } 329 } 330 331 register_block_type_from_metadata( 332 __DIR__ . '/navigation-link', 333 array( 334 'render_callback' => 'render_block_core_navigation_link', 335 'variations' => array_merge( $built_ins, $variations ), 336 ) 337 ); 338 } 339 add_action( 'init', 'register_block_core_navigation_link' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |