PluginProbe
Gutenberg / 23.6.2
Gutenberg v23.6.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.6.2, at build/scripts/block-library/page-list.php

368 lines 13.7 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 = $attributes['parentPageID'];
262 $is_nested = ! empty( $block->context['core/isInsideSubmenu'] );
263
264 $all_pages = get_pages(
265 array(
266 'sort_column' => 'menu_order,post_title',
267 'order' => 'asc',
268 )
269 );
270
271 // If there are no pages, there is nothing to show.
272 if ( empty( $all_pages ) ) {
273 return;
274 }
275
276 $top_level_pages = array();
277
278 $pages_with_children = array();
279
280 $active_page_ancestor_ids = array();
281
282 foreach ( (array) $all_pages as $page ) {
283 $is_active = ! empty( $page->ID ) && ( get_queried_object_id() === $page->ID );
284
285 if ( $is_active ) {
286 $active_page_ancestor_ids = get_post_ancestors( $page->ID );
287 }
288
289 if ( $page->post_parent ) {
290 $pages_with_children[ $page->post_parent ][ $page->ID ] = array(
291 'page_id' => $page->ID,
292 'title' => $page->post_title,
293 'link' => get_permalink( $page ),
294 'is_active' => $is_active,
295 );
296 } else {
297 $top_level_pages[ $page->ID ] = array(
298 'page_id' => $page->ID,
299 'title' => $page->post_title,
300 'link' => get_permalink( $page ),
301 'is_active' => $is_active,
302 );
303
304 }
305 }
306
307 $colors = gutenberg_block_core_page_list_build_css_colors( $attributes, $block->context );
308 $classes = array_merge(
309 $colors['css_classes']
310 );
311
312 $style_attribute = $colors['inline_styles'];
313 $css_classes = trim( implode( ' ', $classes ) );
314
315 $nested_pages = gutenberg_block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );
316
317 if ( 0 !== $parent_page_id ) {
318 // If the parent page has no child pages, there is nothing to show.
319 if ( ! array_key_exists( $parent_page_id, $pages_with_children ) ) {
320 return;
321 }
322
323 $nested_pages = gutenberg_block_core_page_list_nest_pages(
324 $pages_with_children[ $parent_page_id ],
325 $pages_with_children
326 );
327 }
328
329 $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context );
330
331 // Get submenu visibility with backward compatibility for openSubmenusOnClick.
332 $submenu_visibility = $is_navigation_child ? gutenberg_block_core_page_list_get_submenu_visibility( $block->context ) : 'hover';
333
334 $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false;
335
336 $wrapper_markup = $is_nested ? '%2$s' : '<ul %1$s>%2$s</ul>';
337
338 $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 );
339
340 $wrapper_attributes = get_block_wrapper_attributes(
341 array(
342 'class' => $css_classes,
343 'style' => $style_attribute,
344 )
345 );
346
347 return sprintf(
348 $wrapper_markup,
349 $wrapper_attributes,
350 $items_markup
351 );
352 }
353
354 /**
355 * Registers the `core/pages` block on server.
356 *
357 * @since 5.8.0
358 */
359 function gutenberg_register_block_core_page_list() {
360 register_block_type_from_metadata(
361 __DIR__ . '/page-list',
362 array(
363 'render_callback' => 'gutenberg_render_block_core_page_list',
364 )
365 );
366 }
367 add_action( 'init', 'gutenberg_register_block_core_page_list', 20 );
368