author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
19 | 1 |
<?php |
2 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3 |
* Server-side registering and rendering of the `core/navigation-link` block. |
19 | 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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* @since 5.9.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
14 |
* @param array $context Navigation block context. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
15 |
* @param array $attributes Block attributes. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
16 |
* @param bool $is_sub_menu Whether the link is part of a sub-menu. |
19 | 17 |
* @return array Colors CSS classes and inline styles. |
18 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
19 |
function block_core_navigation_link_build_css_colors( $context, $attributes, $is_sub_menu = false ) { |
19 | 20 |
$colors = array( |
21 |
'css_classes' => array(), |
|
22 |
'inline_styles' => '', |
|
23 |
); |
|
24 |
||
25 |
// Text color. |
|
26 |
$named_text_color = null; |
|
27 |
$custom_text_color = null; |
|
28 |
||
29 |
if ( $is_sub_menu && array_key_exists( 'customOverlayTextColor', $context ) ) { |
|
30 |
$custom_text_color = $context['customOverlayTextColor']; |
|
31 |
} elseif ( $is_sub_menu && array_key_exists( 'overlayTextColor', $context ) ) { |
|
32 |
$named_text_color = $context['overlayTextColor']; |
|
33 |
} elseif ( array_key_exists( 'customTextColor', $context ) ) { |
|
34 |
$custom_text_color = $context['customTextColor']; |
|
35 |
} elseif ( array_key_exists( 'textColor', $context ) ) { |
|
36 |
$named_text_color = $context['textColor']; |
|
37 |
} elseif ( isset( $context['style']['color']['text'] ) ) { |
|
38 |
$custom_text_color = $context['style']['color']['text']; |
|
39 |
} |
|
40 |
||
41 |
// If has text color. |
|
42 |
if ( ! is_null( $named_text_color ) ) { |
|
43 |
// Add the color class. |
|
44 |
array_push( $colors['css_classes'], 'has-text-color', sprintf( 'has-%s-color', $named_text_color ) ); |
|
45 |
} elseif ( ! is_null( $custom_text_color ) ) { |
|
46 |
// Add the custom color inline style. |
|
47 |
$colors['css_classes'][] = 'has-text-color'; |
|
48 |
$colors['inline_styles'] .= sprintf( 'color: %s;', $custom_text_color ); |
|
49 |
} |
|
50 |
||
51 |
// Background color. |
|
52 |
$named_background_color = null; |
|
53 |
$custom_background_color = null; |
|
54 |
||
55 |
if ( $is_sub_menu && array_key_exists( 'customOverlayBackgroundColor', $context ) ) { |
|
56 |
$custom_background_color = $context['customOverlayBackgroundColor']; |
|
57 |
} elseif ( $is_sub_menu && array_key_exists( 'overlayBackgroundColor', $context ) ) { |
|
58 |
$named_background_color = $context['overlayBackgroundColor']; |
|
59 |
} elseif ( array_key_exists( 'customBackgroundColor', $context ) ) { |
|
60 |
$custom_background_color = $context['customBackgroundColor']; |
|
61 |
} elseif ( array_key_exists( 'backgroundColor', $context ) ) { |
|
62 |
$named_background_color = $context['backgroundColor']; |
|
63 |
} elseif ( isset( $context['style']['color']['background'] ) ) { |
|
64 |
$custom_background_color = $context['style']['color']['background']; |
|
65 |
} |
|
66 |
||
67 |
// If has background color. |
|
68 |
if ( ! is_null( $named_background_color ) ) { |
|
69 |
// Add the background-color class. |
|
70 |
array_push( $colors['css_classes'], 'has-background', sprintf( 'has-%s-background-color', $named_background_color ) ); |
|
71 |
} elseif ( ! is_null( $custom_background_color ) ) { |
|
72 |
// Add the custom background-color inline style. |
|
73 |
$colors['css_classes'][] = 'has-background'; |
|
74 |
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $custom_background_color ); |
|
75 |
} |
|
76 |
||
77 |
return $colors; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Build an array with CSS classes and inline styles defining the font sizes |
|
82 |
* which will be applied to the navigation markup in the front-end. |
|
83 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
84 |
* @since 5.9.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
* |
19 | 86 |
* @param array $context Navigation block context. |
87 |
* @return array Font size CSS classes and inline styles. |
|
88 |
*/ |
|
89 |
function block_core_navigation_link_build_css_font_sizes( $context ) { |
|
90 |
// CSS classes. |
|
91 |
$font_sizes = array( |
|
92 |
'css_classes' => array(), |
|
93 |
'inline_styles' => '', |
|
94 |
); |
|
95 |
||
96 |
$has_named_font_size = array_key_exists( 'fontSize', $context ); |
|
97 |
$has_custom_font_size = isset( $context['style']['typography']['fontSize'] ); |
|
98 |
||
99 |
if ( $has_named_font_size ) { |
|
100 |
// Add the font size class. |
|
101 |
$font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] ); |
|
102 |
} elseif ( $has_custom_font_size ) { |
|
103 |
// Add the custom font size inline style. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
$font_sizes['inline_styles'] = sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
'font-size: %s;', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
wp_get_typography_font_size_value( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
108 |
'size' => $context['style']['typography']['fontSize'], |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
); |
19 | 112 |
} |
113 |
||
114 |
return $font_sizes; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Returns the top-level submenu SVG chevron icon. |
|
119 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
* @since 5.9.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
121 |
* |
19 | 122 |
* @return string |
123 |
*/ |
|
124 |
function block_core_navigation_link_render_submenu_icon() { |
|
125 |
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>'; |
|
126 |
} |
|
127 |
||
128 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
129 |
* Decodes a url if it's encoded, returning the same url if not. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
* @since 6.2.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
* @param string $url The url to decode. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
* @return string $url Returns the decoded url. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
function block_core_navigation_link_maybe_urldecode( $url ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
138 |
$is_url_encoded = false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
139 |
$query = parse_url( $url, PHP_URL_QUERY ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
140 |
$query_params = wp_parse_args( $query ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
foreach ( $query_params as $query_param ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
143 |
$can_query_param_be_encoded = is_string( $query_param ) && ! empty( $query_param ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
144 |
if ( ! $can_query_param_be_encoded ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
145 |
continue; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
146 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
147 |
if ( rawurldecode( $query_param ) !== $query_param ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
148 |
$is_url_encoded = true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
149 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
150 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
152 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
153 |
if ( $is_url_encoded ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
return rawurldecode( $url ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
return $url; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
/** |
19 | 162 |
* Renders the `core/navigation-link` block. |
163 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
* @since 5.9.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
* |
19 | 166 |
* @param array $attributes The block attributes. |
167 |
* @param string $content The saved content. |
|
168 |
* @param WP_Block $block The parsed block. |
|
169 |
* |
|
170 |
* @return string Returns the post content with the legacy widget added. |
|
171 |
*/ |
|
172 |
function render_block_core_navigation_link( $attributes, $content, $block ) { |
|
173 |
$navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] ); |
|
174 |
$is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind']; |
|
175 |
$is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] ); |
|
176 |
||
177 |
// Don't render the block's subtree if it is a draft or if the ID does not exist. |
|
178 |
if ( $is_post_type && $navigation_link_has_id ) { |
|
179 |
$post = get_post( $attributes['id'] ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
180 |
/** |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
181 |
* Filter allowed post_status for navigation link block to render. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
182 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
183 |
* @since 6.8.0 |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
184 |
* |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
185 |
* @param array $post_status |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
186 |
* @param array $attributes |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
187 |
* @param WP_Block $block |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
188 |
*/ |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
189 |
$allowed_post_status = (array) apply_filters( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
190 |
'render_block_core_navigation_link_allowed_post_status', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
191 |
array( 'publish' ), |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
192 |
$attributes, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
193 |
$block |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
194 |
); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
195 |
if ( ! $post || ! in_array( $post->post_status, $allowed_post_status, true ) ) { |
19 | 196 |
return ''; |
197 |
} |
|
198 |
} |
|
199 |
||
200 |
// Don't render the block's subtree if it has no label. |
|
201 |
if ( empty( $attributes['label'] ) ) { |
|
202 |
return ''; |
|
203 |
} |
|
204 |
||
205 |
$font_sizes = block_core_navigation_link_build_css_font_sizes( $block->context ); |
|
206 |
$classes = array_merge( |
|
207 |
$font_sizes['css_classes'] |
|
208 |
); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
209 |
$style_attribute = $font_sizes['inline_styles']; |
19 | 210 |
|
211 |
$css_classes = trim( implode( ' ', $classes ) ); |
|
212 |
$has_submenu = count( $block->inner_blocks ) > 0; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
213 |
$kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
214 |
$is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
215 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
216 |
if ( is_post_type_archive() ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
217 |
$queried_archive_link = get_post_type_archive_link( get_queried_object()->name ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
218 |
if ( $attributes['url'] === $queried_archive_link ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
219 |
$is_active = true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
220 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
221 |
} |
19 | 222 |
|
223 |
$wrapper_attributes = get_block_wrapper_attributes( |
|
224 |
array( |
|
225 |
'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) . |
|
226 |
( $is_active ? ' current-menu-item' : '' ), |
|
227 |
'style' => $style_attribute, |
|
228 |
) |
|
229 |
); |
|
230 |
$html = '<li ' . $wrapper_attributes . '>' . |
|
231 |
'<a class="wp-block-navigation-item__content" '; |
|
232 |
||
233 |
// Start appending HTML attributes to anchor tag. |
|
234 |
if ( isset( $attributes['url'] ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
235 |
$html .= ' href="' . esc_url( block_core_navigation_link_maybe_urldecode( $attributes['url'] ) ) . '"'; |
19 | 236 |
} |
237 |
||
238 |
if ( $is_active ) { |
|
239 |
$html .= ' aria-current="page"'; |
|
240 |
} |
|
241 |
||
242 |
if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) { |
|
243 |
$html .= ' target="_blank" '; |
|
244 |
} |
|
245 |
||
246 |
if ( isset( $attributes['rel'] ) ) { |
|
247 |
$html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"'; |
|
248 |
} elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) { |
|
249 |
$html .= ' rel="nofollow"'; |
|
250 |
} |
|
251 |
||
252 |
if ( isset( $attributes['title'] ) ) { |
|
253 |
$html .= ' title="' . esc_attr( $attributes['title'] ) . '"'; |
|
254 |
} |
|
255 |
||
256 |
// End appending HTML attributes to anchor tag. |
|
257 |
||
258 |
// Start anchor tag content. |
|
259 |
$html .= '>' . |
|
260 |
// Wrap title with span to isolate it from submenu icon. |
|
261 |
'<span class="wp-block-navigation-item__label">'; |
|
262 |
||
263 |
if ( isset( $attributes['label'] ) ) { |
|
264 |
$html .= wp_kses_post( $attributes['label'] ); |
|
265 |
} |
|
266 |
||
267 |
$html .= '</span>'; |
|
268 |
||
269 |
// Add description if available. |
|
270 |
if ( ! empty( $attributes['description'] ) ) { |
|
271 |
$html .= '<span class="wp-block-navigation-item__description">'; |
|
272 |
$html .= wp_kses_post( $attributes['description'] ); |
|
273 |
$html .= '</span>'; |
|
274 |
} |
|
275 |
||
276 |
$html .= '</a>'; |
|
277 |
// End anchor tag content. |
|
278 |
||
279 |
if ( isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'] && $has_submenu ) { |
|
280 |
// The submenu icon can be hidden by a CSS rule on the Navigation Block. |
|
281 |
$html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_link_render_submenu_icon() . '</span>'; |
|
282 |
} |
|
283 |
||
284 |
if ( $has_submenu ) { |
|
285 |
$inner_blocks_html = ''; |
|
286 |
foreach ( $block->inner_blocks as $inner_block ) { |
|
287 |
$inner_blocks_html .= $inner_block->render(); |
|
288 |
} |
|
289 |
||
290 |
$html .= sprintf( |
|
291 |
'<ul class="wp-block-navigation__submenu-container">%s</ul>', |
|
292 |
$inner_blocks_html |
|
293 |
); |
|
294 |
} |
|
295 |
||
296 |
$html .= '</li>'; |
|
297 |
||
298 |
return $html; |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Returns a navigation link variation |
|
303 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
304 |
* @since 5.9.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
305 |
* |
19 | 306 |
* @param WP_Taxonomy|WP_Post_Type $entity post type or taxonomy entity. |
307 |
* @param string $kind string of value 'taxonomy' or 'post-type'. |
|
308 |
* |
|
309 |
* @return array |
|
310 |
*/ |
|
311 |
function build_variation_for_navigation_link( $entity, $kind ) { |
|
312 |
$title = ''; |
|
313 |
$description = ''; |
|
314 |
||
315 |
if ( property_exists( $entity->labels, 'item_link' ) ) { |
|
316 |
$title = $entity->labels->item_link; |
|
317 |
} |
|
318 |
if ( property_exists( $entity->labels, 'item_link_description' ) ) { |
|
319 |
$description = $entity->labels->item_link_description; |
|
320 |
} |
|
321 |
||
322 |
$variation = array( |
|
323 |
'name' => $entity->name, |
|
324 |
'title' => $title, |
|
325 |
'description' => $description, |
|
326 |
'attributes' => array( |
|
327 |
'type' => $entity->name, |
|
328 |
'kind' => $kind, |
|
329 |
), |
|
330 |
); |
|
331 |
||
332 |
// Tweak some value for the variations. |
|
333 |
$variation_overrides = array( |
|
334 |
'post_tag' => array( |
|
335 |
'name' => 'tag', |
|
336 |
'attributes' => array( |
|
337 |
'type' => 'tag', |
|
338 |
'kind' => $kind, |
|
339 |
), |
|
340 |
), |
|
341 |
'post_format' => array( |
|
342 |
// The item_link and item_link_description for post formats is the |
|
343 |
// same as for tags, so need to be overridden. |
|
344 |
'title' => __( 'Post Format Link' ), |
|
345 |
'description' => __( 'A link to a post format' ), |
|
346 |
'attributes' => array( |
|
347 |
'type' => 'post_format', |
|
348 |
'kind' => $kind, |
|
349 |
), |
|
350 |
), |
|
351 |
); |
|
352 |
||
353 |
if ( array_key_exists( $entity->name, $variation_overrides ) ) { |
|
354 |
$variation = array_merge( |
|
355 |
$variation, |
|
356 |
$variation_overrides[ $entity->name ] |
|
357 |
); |
|
358 |
} |
|
359 |
||
360 |
return $variation; |
|
361 |
} |
|
362 |
||
363 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
364 |
* Filters the registered variations for a block type. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
365 |
* Returns the dynamically built variations for all post-types and taxonomies. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
366 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
367 |
* @since 6.5.0 |
19 | 368 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
369 |
* @param array $variations Array of registered variations for a block type. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
370 |
* @param WP_Block_Type $block_type The full block type object. |
19 | 371 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
372 |
function block_core_navigation_link_filter_variations( $variations, $block_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
373 |
if ( 'core/navigation-link' !== $block_type->name ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
374 |
return $variations; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
375 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
376 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
377 |
$generated_variations = block_core_navigation_link_build_variations(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
378 |
return array_merge( $variations, $generated_variations ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
379 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
380 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
381 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
382 |
* Returns an array of variations for the navigation link block. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
383 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
384 |
* @since 6.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
385 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
386 |
* @return array |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
387 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
388 |
function block_core_navigation_link_build_variations() { |
19 | 389 |
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); |
390 |
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); |
|
391 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
392 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
393 |
* Use two separate arrays as a way to order the variations in the UI. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
394 |
* Known variations (like Post Link and Page Link) are added to the |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
395 |
* `built_ins` array. Variations for custom post types and taxonomies are |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
396 |
* added to the `variations` array and will always appear after `built-ins. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
397 |
*/ |
19 | 398 |
$built_ins = array(); |
399 |
$variations = array(); |
|
400 |
||
401 |
if ( $post_types ) { |
|
402 |
foreach ( $post_types as $post_type ) { |
|
403 |
$variation = build_variation_for_navigation_link( $post_type, 'post-type' ); |
|
404 |
if ( $post_type->_builtin ) { |
|
405 |
$built_ins[] = $variation; |
|
406 |
} else { |
|
407 |
$variations[] = $variation; |
|
408 |
} |
|
409 |
} |
|
410 |
} |
|
411 |
if ( $taxonomies ) { |
|
412 |
foreach ( $taxonomies as $taxonomy ) { |
|
413 |
$variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); |
|
414 |
if ( $taxonomy->_builtin ) { |
|
415 |
$built_ins[] = $variation; |
|
416 |
} else { |
|
417 |
$variations[] = $variation; |
|
418 |
} |
|
419 |
} |
|
420 |
} |
|
421 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
422 |
return array_merge( $built_ins, $variations ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
423 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
424 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
425 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
426 |
* Registers the navigation link block. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
427 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
428 |
* @since 5.9.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
429 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
430 |
* @uses render_block_core_navigation_link() |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
431 |
* @throws WP_Error An WP_Error exception parsing the block definition. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
432 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
433 |
function register_block_core_navigation_link() { |
19 | 434 |
register_block_type_from_metadata( |
435 |
__DIR__ . '/navigation-link', |
|
436 |
array( |
|
437 |
'render_callback' => 'render_block_core_navigation_link', |
|
438 |
) |
|
439 |
); |
|
440 |
} |
|
441 |
add_action( 'init', 'register_block_core_navigation_link' ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
442 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
443 |
* Creates all variations for post types / taxonomies dynamically (= each time when variations are requested). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
444 |
* Do not use variation_callback, to also account for unregistering post types/taxonomies later on. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
445 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
446 |
add_action( 'get_block_type_variations', 'block_core_navigation_link_filter_variations', 10, 2 ); |