| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Server-side registering and rendering of the `core/navigation-link` block. 4 * 5 * @package WordPress 6 */ 7 8 // Path differs between source and build: './shared/' in source, './navigation-link/shared/' in build. 9 if ( file_exists( __DIR__ . '/shared/item-should-render.php' ) ) { 10 require_once __DIR__ . '/shared/item-should-render.php'; 11 require_once __DIR__ . '/shared/render-submenu-icon.php'; 12 } else { 13 require_once __DIR__ . '/navigation-link/shared/item-should-render.php'; 14 require_once __DIR__ . '/navigation-link/shared/render-submenu-icon.php'; 15 } 16 17 /** 18 * Build an array with CSS classes and inline styles defining the colors 19 * which will be applied to the navigation markup in the front-end. 20 * 21 * @since 5.9.0 22 * 23 * @param array $context Navigation block context. 24 * @param array $attributes Block attributes. 25 * @param bool $is_sub_menu Whether the link is part of a sub-menu. Default false. 26 * @return array Colors CSS classes and inline styles. 27 */ 28 function block_core_navigation_link_build_css_colors( $context, $attributes, $is_sub_menu = false ) { 29 $colors = array( 30 'css_classes' => array(), 31 'inline_styles' => '', 32 ); 33 34 // Text color. 35 $named_text_color = null; 36 $custom_text_color = null; 37 38 if ( $is_sub_menu && array_key_exists( 'customOverlayTextColor', $context ) ) { 39 $custom_text_color = $context['customOverlayTextColor']; 40 } elseif ( $is_sub_menu && array_key_exists( 'overlayTextColor', $context ) ) { 41 $named_text_color = $context['overlayTextColor']; 42 } elseif ( array_key_exists( 'customTextColor', $context ) ) { 43 $custom_text_color = $context['customTextColor']; 44 } elseif ( array_key_exists( 'textColor', $context ) ) { 45 $named_text_color = $context['textColor']; 46 } elseif ( isset( $context['style']['color']['text'] ) ) { 47 $custom_text_color = $context['style']['color']['text']; 48 } 49 50 // If has text color. 51 if ( ! is_null( $named_text_color ) ) { 52 // Add the color class. 53 array_push( $colors['css_classes'], 'has-text-color', sprintf( 'has-%s-color', $named_text_color ) ); 54 } elseif ( ! is_null( $custom_text_color ) ) { 55 // Add the custom color inline style. 56 $colors['css_classes'][] = 'has-text-color'; 57 $colors['inline_styles'] .= sprintf( 'color: %s;', $custom_text_color ); 58 } 59 60 // Background color. 61 $named_background_color = null; 62 $custom_background_color = null; 63 64 if ( $is_sub_menu && array_key_exists( 'customOverlayBackgroundColor', $context ) ) { 65 $custom_background_color = $context['customOverlayBackgroundColor']; 66 } elseif ( $is_sub_menu && array_key_exists( 'overlayBackgroundColor', $context ) ) { 67 $named_background_color = $context['overlayBackgroundColor']; 68 } elseif ( array_key_exists( 'customBackgroundColor', $context ) ) { 69 $custom_background_color = $context['customBackgroundColor']; 70 } elseif ( array_key_exists( 'backgroundColor', $context ) ) { 71 $named_background_color = $context['backgroundColor']; 72 } elseif ( isset( $context['style']['color']['background'] ) ) { 73 $custom_background_color = $context['style']['color']['background']; 74 } 75 76 // If has background color. 77 if ( ! is_null( $named_background_color ) ) { 78 // Add the background-color class. 79 array_push( $colors['css_classes'], 'has-background', sprintf( 'has-%s-background-color', $named_background_color ) ); 80 } elseif ( ! is_null( $custom_background_color ) ) { 81 // Add the custom background-color inline style. 82 $colors['css_classes'][] = 'has-background'; 83 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $custom_background_color ); 84 } 85 86 return $colors; 87 } 88 89 /** 90 * Build an array with CSS classes and inline styles defining the font sizes 91 * which will be applied to the navigation markup in the front-end. 92 * 93 * @since 5.9.0 94 * 95 * @param array $context Navigation block context. 96 * @return array Font size CSS classes and inline styles. 97 */ 98 function block_core_navigation_link_build_css_font_sizes( $context ) { 99 // CSS classes. 100 $font_sizes = array( 101 'css_classes' => array(), 102 'inline_styles' => '', 103 ); 104 105 $has_named_font_size = array_key_exists( 'fontSize', $context ); 106 $has_custom_font_size = isset( $context['style']['typography']['fontSize'] ); 107 108 if ( $has_named_font_size ) { 109 // Add the font size class. 110 $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] ); 111 } elseif ( $has_custom_font_size ) { 112 // Add the custom font size inline style. 113 $font_sizes['inline_styles'] = sprintf( 114 'font-size: %s;', 115 wp_get_typography_font_size_value( 116 array( 117 'size' => $context['style']['typography']['fontSize'], 118 ) 119 ) 120 ); 121 } 122 123 return $font_sizes; 124 } 125 126 /** 127 * Decodes a url if it's encoded, returning the same url if not. 128 * 129 * @since 6.2.0 130 * 131 * @param string $url The url to decode. 132 * 133 * @return string $url Returns the decoded url. 134 */ 135 function block_core_navigation_link_maybe_urldecode( $url ) { 136 $is_url_encoded = false; 137 $query = parse_url( $url, PHP_URL_QUERY ); 138 $query_params = wp_parse_args( $query ); 139 140 foreach ( $query_params as $query_param ) { 141 $can_query_param_be_encoded = is_string( $query_param ) && ! empty( $query_param ); 142 if ( ! $can_query_param_be_encoded ) { 143 continue; 144 } 145 if ( rawurldecode( $query_param ) !== $query_param ) { 146 $is_url_encoded = true; 147 break; 148 } 149 } 150 151 if ( $is_url_encoded ) { 152 return rawurldecode( $url ); 153 } 154 155 return $url; 156 } 157 158 159 /** 160 * Renders the `core/navigation-link` block. 161 * 162 * @since 5.9.0 163 * 164 * @param array $attributes The block attributes. 165 * @param string $content The saved content. 166 * @param WP_Block $block The parsed block. 167 * 168 * @return string Returns the post content with the legacy widget added. 169 */ 170 function render_block_core_navigation_link( $attributes, $content, $block ) { 171 // Check if this navigation item should render based on post status. 172 if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { 173 if ( ! gutenberg_block_core_shared_navigation_item_should_render( $attributes, $block ) ) { 174 return ''; 175 } 176 } 177 178 // Don't render the block's subtree if it has no label. 179 if ( empty( $attributes['label'] ) ) { 180 return ''; 181 } 182 183 $font_sizes = block_core_navigation_link_build_css_font_sizes( $block->context ); 184 $classes = array_merge( 185 $font_sizes['css_classes'] 186 ); 187 $style_attribute = $font_sizes['inline_styles']; 188 189 // Render inner blocks first to check if any menu items will actually display. 190 $inner_blocks_html = ''; 191 foreach ( $block->inner_blocks as $inner_block ) { 192 $inner_blocks_html .= $inner_block->render(); 193 } 194 $has_submenu = ! empty( trim( $inner_blocks_html ) ); 195 196 $css_classes = trim( implode( ' ', $classes ) ); 197 $kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] ); 198 $is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind ); 199 200 if ( is_post_type_archive() && ! empty( $attributes['url'] ) ) { 201 $queried_archive_link = get_post_type_archive_link( get_queried_object()->name ); 202 if ( $attributes['url'] === $queried_archive_link ) { 203 $is_active = true; 204 } 205 } 206 207 $wrapper_attributes = get_block_wrapper_attributes( 208 array( 209 'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) . 210 ( $is_active ? ' current-menu-item' : '' ), 211 'style' => $style_attribute, 212 ) 213 ); 214 $html = '<li ' . $wrapper_attributes . '>' . 215 '<a class="wp-block-navigation-item__content" '; 216 217 // Start appending HTML attributes to anchor tag. 218 if ( isset( $attributes['url'] ) ) { 219 $html .= ' href="' . esc_url( block_core_navigation_link_maybe_urldecode( $attributes['url'] ) ) . '"'; 220 } 221 222 if ( $is_active ) { 223 $html .= ' aria-current="page"'; 224 } 225 226 if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) { 227 $html .= ' target="_blank" '; 228 } 229 230 if ( isset( $attributes['rel'] ) ) { 231 $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"'; 232 } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) { 233 $html .= ' rel="nofollow"'; 234 } 235 236 if ( isset( $attributes['title'] ) ) { 237 $html .= ' title="' . esc_attr( $attributes['title'] ) . '"'; 238 } 239 240 // End appending HTML attributes to anchor tag. 241 242 // Start anchor tag content. 243 $html .= '>' . 244 // Wrap title with span to isolate it from submenu icon. 245 '<span class="wp-block-navigation-item__label">'; 246 247 if ( isset( $attributes['label'] ) ) { 248 $html .= wp_kses_post( $attributes['label'] ); 249 } 250 251 $html .= '</span>'; 252 253 // Add description if available. 254 if ( ! empty( $attributes['description'] ) ) { 255 $html .= '<span class="wp-block-navigation-item__description">'; 256 $html .= wp_kses_post( $attributes['description'] ); 257 $html .= '</span>'; 258 } 259 260 $html .= '</a>'; 261 // End anchor tag content. 262 263 if ( isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'] && $has_submenu ) { 264 // The submenu icon can be hidden by a CSS rule on the Navigation Block. 265 $html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_render_submenu_icon() . '</span>'; 266 } 267 268 if ( $has_submenu ) { 269 $html .= sprintf( 270 '<ul class="wp-block-navigation__submenu-container">%s</ul>', 271 $inner_blocks_html 272 ); 273 } 274 275 $html .= '</li>'; 276 277 return $html; 278 } 279 280 /** 281 * Returns a navigation link variation 282 * 283 * @since 5.9.0 284 * 285 * @param WP_Taxonomy|WP_Post_Type $entity post type or taxonomy entity. 286 * @param string $kind string of value 'taxonomy' or 'post-type'. 287 * 288 * @return array 289 */ 290 function build_variation_for_navigation_link( $entity, $kind ) { 291 $title = ''; 292 $description = ''; 293 294 // Get default labels based on entity type 295 $default_labels = null; 296 if ( $entity instanceof WP_Post_Type ) { 297 $default_labels = WP_Post_Type::get_default_labels(); 298 } elseif ( $entity instanceof WP_Taxonomy ) { 299 $default_labels = WP_Taxonomy::get_default_labels(); 300 } 301 302 // Get title and check if it's default 303 $is_default_title = false; 304 if ( property_exists( $entity->labels, 'item_link' ) ) { 305 $title = $entity->labels->item_link; 306 if ( isset( $default_labels['item_link'] ) ) { 307 $is_default_title = in_array( $title, $default_labels['item_link'], true ); 308 } 309 } 310 311 // Get description and check if it's default 312 $is_default_description = false; 313 if ( property_exists( $entity->labels, 'item_link_description' ) ) { 314 $description = $entity->labels->item_link_description; 315 if ( isset( $default_labels['item_link_description'] ) ) { 316 $is_default_description = in_array( $description, $default_labels['item_link_description'], true ); 317 } 318 } 319 320 // Calculate singular name once (used for both title and description) 321 $singular = $entity->labels->singular_name ?? ucfirst( $entity->name ); 322 323 // Set default title if needed 324 if ( $is_default_title || '' === $title ) { 325 /* translators: %s: Singular label of the entity. */ 326 $title = sprintf( __( '%s link' ), $singular ); 327 } 328 329 // Default description if needed. 330 // Use a single space character instead of an empty string to prevent fallback to the 331 // block.json default description ("Add a page, link, or another item to your navigation."). 332 // An empty string would be treated as missing and trigger the fallback, while a single 333 // space appears blank in the UI but prevents the fallback behavior. 334 // We avoid generating descriptions like "A link to a %s" to prevent grammatical errors 335 // (e.g., "A link to a event" should be "A link to an event"). 336 if ( $is_default_description || '' === $description ) { 337 $description = ' '; 338 } 339 340 $variation = array( 341 'name' => $entity->name, 342 'title' => $title, 343 'description' => $description, 344 'attributes' => array( 345 'type' => $entity->name, 346 'kind' => $kind, 347 ), 348 ); 349 350 // Tweak some value for the variations. 351 $variation_overrides = array( 352 'post_tag' => array( 353 'name' => 'tag', 354 'attributes' => array( 355 'type' => 'tag', 356 'kind' => $kind, 357 ), 358 ), 359 'post_format' => array( 360 // The item_link and item_link_description for post formats is the 361 // same as for tags, so need to be overridden. 362 'title' => __( 'Post Format Link' ), 363 'description' => __( 'A link to a post format' ), 364 'attributes' => array( 365 'type' => 'post_format', 366 'kind' => $kind, 367 ), 368 ), 369 ); 370 371 if ( array_key_exists( $entity->name, $variation_overrides ) ) { 372 $variation = array_merge( 373 $variation, 374 $variation_overrides[ $entity->name ] 375 ); 376 } 377 378 return $variation; 379 } 380 381 /** 382 * Filters the registered variations for a block type. 383 * Returns the dynamically built variations for all post-types and taxonomies. 384 * 385 * @since 6.5.0 386 * 387 * @param array $variations Array of registered variations for a block type. 388 * @param WP_Block_Type $block_type The full block type object. 389 * @return array Numerically indexed array of block variations. 390 */ 391 function block_core_navigation_link_filter_variations( $variations, $block_type ) { 392 if ( 'core/navigation-link' !== $block_type->name ) { 393 return $variations; 394 } 395 396 $generated_variations = block_core_navigation_link_build_variations(); 397 398 /* 399 * IMPORTANT: Order matters for deduplication. 400 * 401 * The variations returned from this filter are bootstrapped to JavaScript and 402 * processed by the block variations reducer. The reducer uses `getUniqueItemsByName()` 403 * (packages/blocks/src/store/reducer.js:51-57) which keeps the FIRST variation with 404 * a given 'name' and discards later duplicates when processing the array in order. 405 * 406 * By placing generated variations first in `array_merge()`, the improved 407 * labels (e.g., "Product link" instead of generic "Post Link") are processed first 408 * and preserved. The generic incoming variations are then discarded as duplicates. 409 * 410 * Why `array_merge()` instead of manual deduplication? 411 * - Both arrays use numeric indices (0, 1, 2...), so `array_merge()` concatenates 412 * and re-indexes them sequentially, preserving order 413 * - The reducer handles deduplication, so it is not needed here 414 * - This keeps the PHP code simple and relies on the established JavaScript behavior 415 * 416 * See: https://github.com/WordPress/gutenberg/pull/72517 417 */ 418 return array_merge( $generated_variations, $variations ); 419 } 420 421 /** 422 * Returns an array of variations for the navigation link block. 423 * 424 * @since 6.5.0 425 * 426 * @return array 427 */ 428 function block_core_navigation_link_build_variations() { 429 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); 430 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); 431 432 /* 433 * Use two separate arrays as a way to order the variations in the UI. 434 * Known variations (like Post Link and Page Link) are added to the 435 * `built_ins` array. Variations for custom post types and taxonomies are 436 * added to the `variations` array and will always appear after `built-ins. 437 */ 438 $built_ins = array(); 439 $variations = array(); 440 441 if ( $post_types ) { 442 foreach ( $post_types as $post_type ) { 443 $variation = build_variation_for_navigation_link( $post_type, 'post-type' ); 444 if ( $post_type->_builtin ) { 445 $built_ins[] = $variation; 446 } else { 447 $variations[] = $variation; 448 } 449 } 450 } 451 if ( $taxonomies ) { 452 foreach ( $taxonomies as $taxonomy ) { 453 $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); 454 if ( $taxonomy->_builtin ) { 455 $built_ins[] = $variation; 456 } else { 457 $variations[] = $variation; 458 } 459 } 460 } 461 462 $all_variations = array_merge( $built_ins, $variations ); 463 464 return $all_variations; 465 } 466 467 /** 468 * Registers the navigation link block. 469 * 470 * @since 5.9.0 471 * 472 * @uses render_block_core_navigation_link() 473 * @throws WP_Error An WP_Error exception parsing the block definition. 474 */ 475 function register_block_core_navigation_link() { 476 register_block_type_from_metadata( 477 __DIR__ . '/navigation-link', 478 array( 479 'render_callback' => 'render_block_core_navigation_link', 480 ) 481 ); 482 } 483 add_action( 'init', 'register_block_core_navigation_link' ); 484 /** 485 * Creates all variations for post types / taxonomies dynamically (= each time when variations are requested). 486 * Do not use variation_callback, to also account for unregistering post types/taxonomies later on. 487 */ 488 add_action( 'get_block_type_variations', 'block_core_navigation_link_filter_variations', 10, 2 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Jun 17 08:20:09 2026 | Cross-referenced by PHPXref |