PluginProbe
Gutenberg / 23.7.2
Gutenberg v23.7.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / page-list.php

page-list.php in Gutenberg 23.7.2, at build/scripts/block-library/page-list.php

369 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/pages` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Returns the submenu visibility value with backward compatibility
10 * for the deprecated openSubmenusOnClick attribute.
11 *
12 * @since 7.0.0
13 *
14 * @param array $context Block context from parent Navigation block.
15 * @return string The visibility mode: 'hover', 'click', or 'always'.
16 */
17 function gutenberg_block_core_page_list_get_submenu_visibility( $context ) {
18 $deprecated_open_submenus_on_click = $context['openSubmenusOnClick'] ?? null;
19
20 // For backward compatibility, prioritize the legacy attribute if present. If it has been loaded and saved in the editor, then
21 // the deprecated attribute will be replaced by submenuVisibility.
22 if ( null !== $deprecated_open_submenus_on_click ) {
23 // Convert boolean to string: true -> 'click', false -> 'hover'.
24 return ! empty( $deprecated_open_submenus_on_click ) ? 'click' : 'hover';
25 }
26
27 $submenu_visibility = $context['submenuVisibility'] ?? null;
28
29 // Use submenuVisibility for migrated/new blocks.
30 return $submenu_visibility ?? 'hover';
31 }
32
33 /**
34 * Build an array with CSS classes and inline styles defining the colors
35 * which will be applied to the pages markup in the front-end when it is a descendant of navigation.
36 *
37 * @since 5.8.0
38 *
39 * @param array $attributes Block attributes.
40 * @param array $context Navigation block context.
41 * @return array Colors CSS classes and inline styles.
42 */
43 function gutenberg_block_core_page_list_build_css_colors( $attributes, $context ) {
44 $colors = array(
45 'css_classes' => array(),
46 'inline_styles' => '',
47 'overlay_css_classes' => array(),
48 'overlay_inline_styles' => '',
49 );
50
51 // Text color.
52 $has_named_text_color = array_key_exists( 'textColor', $context );
53 $has_picked_text_color = array_key_exists( 'customTextColor', $context );
54 $has_custom_text_color = isset( $context['style']['color']['text'] );
55
56 // If has text color.
57 if ( $has_custom_text_color || $has_picked_text_color || $has_named_text_color ) {
58 // Add has-text-color class.
59 $colors['css_classes'][] = 'has-text-color';
60 }
61
62 if ( $has_named_text_color ) {
63 // Add the color class.
64 $colors['css_classes'][] = sprintf( 'has-%s-color', _wp_to_kebab_case( $context['textColor'] ) );
65 } elseif ( $has_picked_text_color ) {
66 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['customTextColor'] );
67 } elseif ( $has_custom_text_color ) {
68 // Add the custom color inline style.
69 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] );
70 }
71
72 // Background color.
73 $has_named_background_color = array_key_exists( 'backgroundColor', $context );
74 $has_picked_background_color = array_key_exists( 'customBackgroundColor', $context );
75 $has_custom_background_color = isset( $context['style']['color']['background'] );
76
77 // If has background color.
78 if ( $has_custom_background_color || $has_picked_background_color || $has_named_background_color ) {
79 // Add has-background class.
80 $colors['css_classes'][] = 'has-background';
81 }
82
83 if ( $has_named_background_color ) {
84 // Add the background-color class.
85 $colors['css_classes'][] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $context['backgroundColor'] ) );
86 } elseif ( $has_picked_background_color ) {
87 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['customBackgroundColor'] );
88 } elseif ( $has_custom_background_color ) {
89 // Add the custom background-color inline style.
90 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] );
91 }
92
93 // Overlay text color.
94 $has_named_overlay_text_color = array_key_exists( 'overlayTextColor', $context );
95 $has_picked_overlay_text_color = array_key_exists( 'customOverlayTextColor', $context );
96
97 // If it has a text color.
98 if ( $has_named_overlay_text_color || $has_picked_overlay_text_color ) {
99 $colors['overlay_css_classes'][] = 'has-text-color';
100 }
101
102 // Give overlay colors priority, fall back to Navigation block colors, then global styles.
103 if ( $has_named_overlay_text_color ) {
104 $colors['overlay_css_classes'][] = sprintf( 'has-%s-color', _wp_to_kebab_case( $context['overlayTextColor'] ) );
105 } elseif ( $has_picked_overlay_text_color ) {
106 $colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $context['customOverlayTextColor'] );
107 }
108
109 // Overlay background colors.
110 $has_named_overlay_background_color = array_key_exists( 'overlayBackgroundColor', $context );
111 $has_picked_overlay_background_color = array_key_exists( 'customOverlayBackgroundColor', $context );
112
113 // If has background color.
114 if ( $has_named_overlay_background_color || $has_picked_overlay_background_color ) {
115 $colors['overlay_css_classes'][] = 'has-background';
116 }
117
118 if ( $has_named_overlay_background_color ) {
119 $colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $context['overlayBackgroundColor'] ) );
120 } elseif ( $has_picked_overlay_background_color ) {
121 $colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $context['customOverlayBackgroundColor'] );
122 }
123
124 return $colors;
125 }
126 /**
127 * Outputs Page list markup from an array of pages with nested children.
128 *
129 * @since 5.8.0
130 *
131 * @param string $submenu_visibility The submenu visibility mode: 'hover', 'click', or 'always'.
132 * @param boolean $show_submenu_icons Whether to show submenu indicator icons.
133 * @param boolean $is_navigation_child If block is a child of Navigation block.
134 * @param array $nested_pages The array of nested pages.
135 * @param boolean $is_nested Whether the submenu is nested or not.
136 * @param array $active_page_ancestor_ids An array of ancestor ids for active page.
137 * @param array $colors Color information for overlay styles.
138 * @param integer $depth The nesting depth.
139 *
140 * @return string List markup.
141 */
142 function gutenberg_block_core_page_list_render_nested_page_list( $submenu_visibility, $show_submenu_icons, $is_navigation_child, $nested_pages, $is_nested, $active_page_ancestor_ids = array(), $colors = array(), $depth = 0 ) {
143 if ( empty( $nested_pages ) ) {
144 return;
145 }
146 $front_page_id = (int) get_option( 'page_on_front' );
147 $markup = '';
148
149 // Compute visibility mode flags once
150 $open_on_click = 'click' === $submenu_visibility;
151 $open_on_hover = 'hover' === $submenu_visibility;
152 $open_always = 'always' === $submenu_visibility;
153
154 foreach ( (array) $nested_pages as $page ) {
155 $css_class = $page['is_active'] ? ' current-menu-item' : '';
156 $aria_current = $page['is_active'] ? ' aria-current="page"' : '';
157 $style_attribute = '';
158
159 $css_class .= in_array( $page['page_id'], $active_page_ancestor_ids, true ) ? ' current-menu-ancestor' : '';
160 if ( isset( $page['children'] ) ) {
161 $css_class .= ' has-child';
162 }
163
164 if ( $is_navigation_child ) {
165 $css_class .= ' wp-block-navigation-item';
166
167 // Class assignment logic matches JS editor rendering in page-list-item/edit.js
168 // Note: elseif ensures open-on-hover-click is mutually exclusive with open-on-click
169 if ( $open_on_click ) {
170 $css_class .= ' open-on-click';
171 } elseif ( $open_on_hover && $show_submenu_icons ) {
172 $css_class .= ' open-on-hover-click';
173 } elseif ( $open_always ) {
174 $css_class .= ' open-always';
175 }
176 }
177
178 $navigation_child_content_class = $is_navigation_child ? ' wp-block-navigation-item__content' : '';
179
180 // If this is the first level of submenus, include the overlay colors.
181 if ( ( ( 0 < $depth && ! $is_nested ) || $is_nested ) && isset( $colors['overlay_css_classes'], $colors['overlay_inline_styles'] ) ) {
182 $css_class .= ' ' . trim( implode( ' ', $colors['overlay_css_classes'] ) );
183 if ( '' !== $colors['overlay_inline_styles'] ) {
184 $style_attribute = sprintf( ' style="%s"', esc_attr( $colors['overlay_inline_styles'] ) );
185 }
186 }
187
188 if ( (int) $page['page_id'] === $front_page_id ) {
189 $css_class .= ' menu-item-home';
190 }
191
192 $title = $page['title'] ? $page['title'] : __( '(no title)' );
193
194 $aria_label = sprintf(
195 /* translators: Accessibility text. %s: Parent page title. */
196 __( '%s submenu' ),
197 wp_strip_all_tags( $title )
198 );
199
200 $markup .= '<li class="wp-block-pages-list__item' . esc_attr( $css_class ) . '"' . $style_attribute . '>';
201
202 if ( isset( $page['children'] ) && $is_navigation_child && $open_on_click ) {
203 $markup .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="' . esc_attr( $navigation_child_content_class ) . ' wp-block-navigation-submenu__toggle" aria-expanded="false">' . wp_kses_post( $title ) .
204 '</button><span class="wp-block-page-list__submenu-icon wp-block-navigation__submenu-icon"><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></span>';
205 } else {
206 $markup .= '<a class="wp-block-pages-list__item__link' . esc_attr( $navigation_child_content_class ) . '" href="' . esc_url( $page['link'] ) . '"' . $aria_current . '>' . wp_kses_post( $title ) . '</a>';
207 }
208
209 if ( isset( $page['children'] ) ) {
210 if ( $is_navigation_child && $show_submenu_icons && ! $open_on_click ) {
211 $markup .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">';
212 $markup .= '<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>';
213 $markup .= '</button>';
214 }
215 $markup .= '<ul class="wp-block-navigation__submenu-container">';
216 $markup .= gutenberg_block_core_page_list_render_nested_page_list( $submenu_visibility, $show_submenu_icons, $is_navigation_child, $page['children'], $is_nested, $active_page_ancestor_ids, $colors, $depth + 1 );
217 $markup .= '</ul>';
218 }
219 $markup .= '</li>';
220 }
221 return $markup;
222 }
223
224 /**
225 * Outputs nested array of pages
226 *
227 * @since 5.8.0
228 *
229 * @param array $current_level The level being iterated through.
230 * @param array $children The children grouped by parent post ID.
231 *
232 * @return array The nested array of pages.
233 */
234 function gutenberg_block_core_page_list_nest_pages( $current_level, $children ) {
235 if ( empty( $current_level ) ) {
236 return;
237 }
238 foreach ( (array) $current_level as $key => $current ) {
239 if ( isset( $children[ $key ] ) ) {
240 $current_level[ $key ]['children'] = gutenberg_block_core_page_list_nest_pages( $children[ $key ], $children );
241 }
242 }
243 return $current_level;
244 }
245
246 /**
247 * Renders the `core/page-list` block on server.
248 *
249 * @since 5.8.0
250 *
251 * @param array $attributes The block attributes.
252 * @param string $content The saved content.
253 * @param WP_Block $block The parsed block.
254 *
255 * @return string Returns the page list markup.
256 */
257 function gutenberg_render_block_core_page_list( $attributes, $content, $block ) {
258 static $block_id = 0;
259 ++$block_id;
260
261 $parent_page_id_attr = $attributes['parentPageID'] ?? null;
262 $parent_page_id = is_scalar( $parent_page_id_attr ) ? (int) $parent_page_id_attr : 0;
263 $is_nested = ! empty( $block->context['core/isInsideSubmenu'] );
264
265 $all_pages = get_pages(
266 array(
267 'sort_column' => 'menu_order,post_title',
268 'order' => 'asc',
269 )
270 );
271
272 // If there are no pages, there is nothing to show.
273 if ( empty( $all_pages ) ) {
274 return;
275 }
276
277 $top_level_pages = array();
278
279 $pages_with_children = array();
280
281 $active_page_ancestor_ids = array();
282
283 foreach ( (array) $all_pages as $page ) {
284 $is_active = ! empty( $page->ID ) && ( get_queried_object_id() === $page->ID );
285
286 if ( $is_active ) {
287 $active_page_ancestor_ids = get_post_ancestors( $page->ID );
288 }
289
290 if ( $page->post_parent ) {
291 $pages_with_children[ $page->post_parent ][ $page->ID ] = array(
292 'page_id' => $page->ID,
293 'title' => $page->post_title,
294 'link' => get_permalink( $page ),
295 'is_active' => $is_active,
296 );
297 } else {
298 $top_level_pages[ $page->ID ] = array(
299 'page_id' => $page->ID,
300 'title' => $page->post_title,
301 'link' => get_permalink( $page ),
302 'is_active' => $is_active,
303 );
304
305 }
306 }
307
308 $colors = gutenberg_block_core_page_list_build_css_colors( $attributes, $block->context );
309 $classes = array_merge(
310 $colors['css_classes']
311 );
312
313 $style_attribute = $colors['inline_styles'];
314 $css_classes = trim( implode( ' ', $classes ) );
315
316 $nested_pages = gutenberg_block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );
317
318 if ( 0 !== $parent_page_id ) {
319 // If the parent page has no child pages, there is nothing to show.
320 if ( ! array_key_exists( $parent_page_id, $pages_with_children ) ) {
321 return;
322 }
323
324 $nested_pages = gutenberg_block_core_page_list_nest_pages(
325 $pages_with_children[ $parent_page_id ],
326 $pages_with_children
327 );
328 }
329
330 $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context );
331
332 // Get submenu visibility with backward compatibility for openSubmenusOnClick.
333 $submenu_visibility = $is_navigation_child ? gutenberg_block_core_page_list_get_submenu_visibility( $block->context ) : 'hover';
334
335 $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false;
336
337 $wrapper_markup = $is_nested ? '%2$s' : '<ul %1$s>%2$s</ul>';
338
339 $items_markup = gutenberg_block_core_page_list_render_nested_page_list( $submenu_visibility, $show_submenu_icons, $is_navigation_child, $nested_pages, $is_nested, $active_page_ancestor_ids, $colors );
340
341 $wrapper_attributes = get_block_wrapper_attributes(
342 array(
343 'class' => $css_classes,
344 'style' => $style_attribute,
345 )
346 );
347
348 return sprintf(
349 $wrapper_markup,
350 $wrapper_attributes,
351 $items_markup
352 );
353 }
354
355 /**
356 * Registers the `core/pages` block on server.
357 *
358 * @since 5.8.0
359 */
360 function gutenberg_register_block_core_page_list() {
361 register_block_type_from_metadata(
362 __DIR__ . '/page-list',
363 array(
364 'render_callback' => 'gutenberg_render_block_core_page_list',
365 )
366 );
367 }
368 add_action( 'init', 'gutenberg_register_block_core_page_list', 20 );
369