PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.0
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 / block-library / blocks / page-list.php

page-list.php in Gutenberg 12.1.0, at build/block-library/blocks/page-list.php

333 lines 12.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 * Build an array with CSS classes and inline styles defining the colors
10 * which will be applied to the pages markup in the front-end when it is a descendant of navigation.
11 *
12 * @param array $attributes Block attributes.
13 * @param array $context Navigation block context.
14 * @return array Colors CSS classes and inline styles.
15 */
16 function gutenberg_block_core_page_list_build_css_colors( $attributes, $context ) {
17 $colors = array(
18 'css_classes' => array(),
19 'inline_styles' => '',
20 'overlay_css_classes' => array(),
21 'overlay_inline_styles' => '',
22 );
23
24 // Text color.
25 $has_named_text_color = array_key_exists( 'textColor', $context );
26 $has_picked_text_color = array_key_exists( 'customTextColor', $context );
27 $has_custom_text_color = isset( $context['style']['color']['text'] );
28
29 // If has text color.
30 if ( $has_custom_text_color || $has_picked_text_color || $has_named_text_color ) {
31 // Add has-text-color class.
32 $colors['css_classes'][] = 'has-text-color';
33 }
34
35 if ( $has_named_text_color ) {
36 // Add the color class.
37 $colors['css_classes'][] = sprintf( 'has-%s-color', _wp_to_kebab_case( $context['textColor'] ) );
38 } elseif ( $has_picked_text_color ) {
39 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['customTextColor'] );
40 } elseif ( $has_custom_text_color ) {
41 // Add the custom color inline style.
42 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] );
43 }
44
45 // Background color.
46 $has_named_background_color = array_key_exists( 'backgroundColor', $context );
47 $has_picked_background_color = array_key_exists( 'customBackgroundColor', $context );
48 $has_custom_background_color = isset( $context['style']['color']['background'] );
49
50 // If has background color.
51 if ( $has_custom_background_color || $has_picked_background_color || $has_named_background_color ) {
52 // Add has-background class.
53 $colors['css_classes'][] = 'has-background';
54 }
55
56 if ( $has_named_background_color ) {
57 // Add the background-color class.
58 $colors['css_classes'][] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $context['backgroundColor'] ) );
59 } elseif ( $has_picked_background_color ) {
60 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['customBackgroundColor'] );
61 } elseif ( $has_custom_background_color ) {
62 // Add the custom background-color inline style.
63 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] );
64 }
65
66 // Overlay text color.
67 $has_named_overlay_text_color = array_key_exists( 'overlayTextColor', $context );
68 $has_picked_overlay_text_color = array_key_exists( 'customOverlayTextColor', $context );
69
70 // If it has a text color.
71 if ( $has_named_overlay_text_color || $has_picked_overlay_text_color ) {
72 $colors['overlay_css_classes'][] = 'has-text-color';
73 }
74
75 // Give overlay colors priority, fall back to Navigation block colors, then global styles.
76 if ( $has_named_overlay_text_color ) {
77 $colors['overlay_css_classes'][] = sprintf( 'has-%s-color', _wp_to_kebab_case( $context['overlayTextColor'] ) );
78 } elseif ( $has_picked_overlay_text_color ) {
79 $colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $context['customOverlayTextColor'] );
80 }
81
82 // Overlay background colors.
83 $has_named_overlay_background_color = array_key_exists( 'overlayBackgroundColor', $context );
84 $has_picked_overlay_background_color = array_key_exists( 'customOverlayBackgroundColor', $context );
85
86 // If has background color.
87 if ( $has_named_overlay_background_color || $has_picked_overlay_background_color ) {
88 $colors['overlay_css_classes'][] = 'has-background';
89 }
90
91 if ( $has_named_overlay_background_color ) {
92 $colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $context['overlayBackgroundColor'] ) );
93 } elseif ( $has_picked_overlay_background_color ) {
94 $colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $context['customOverlayBackgroundColor'] );
95 }
96
97 return $colors;
98 }
99
100 /**
101 * Build an array with CSS classes and inline styles defining the font sizes
102 * which will be applied to the pages markup in the front-end when it is a descendant of navigation.
103 *
104 * @param array $context Navigation block context.
105 * @return array Font size CSS classes and inline styles.
106 */
107 function gutenberg_block_core_page_list_build_css_font_sizes( $context ) {
108 // CSS classes.
109 $font_sizes = array(
110 'css_classes' => array(),
111 'inline_styles' => '',
112 );
113
114 $has_named_font_size = array_key_exists( 'fontSize', $context );
115 $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
116
117 if ( $has_named_font_size ) {
118 // Add the font size class.
119 $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
120 } elseif ( $has_custom_font_size ) {
121 // Add the custom font size inline style.
122 $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $context['style']['typography']['fontSize'] );
123 }
124
125 return $font_sizes;
126 }
127
128 /**
129 * Outputs Page list markup from an array of pages with nested children.
130 *
131 * @param boolean $open_submenus_on_click Whether to open submenus on click instead of hover.
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 array $active_page_ancestor_ids An array of ancestor ids for active page.
136 * @param array $colors Color information for overlay styles.
137 * @param integer $depth The nesting depth.
138 *
139 * @return string List markup.
140 */
141 function gutenberg_block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $nested_pages, $active_page_ancestor_ids = array(), $colors = array(), $depth = 0 ) {
142 if ( empty( $nested_pages ) ) {
143 return;
144 }
145 $markup = '';
146 foreach ( (array) $nested_pages as $page ) {
147 $css_class = $page['is_active'] ? ' current-menu-item' : '';
148 $aria_current = $page['is_active'] ? ' aria-current="page"' : '';
149 $style_attribute = '';
150
151 $css_class .= in_array( $page['page_id'], $active_page_ancestor_ids, true ) ? ' current-menu-ancestor' : '';
152 if ( isset( $page['children'] ) ) {
153 $css_class .= ' has-child';
154 }
155
156 if ( $is_navigation_child ) {
157 $css_class .= ' wp-block-navigation-item';
158
159 if ( $open_submenus_on_click ) {
160 $css_class .= ' open-on-click';
161 } elseif ( $show_submenu_icons ) {
162 $css_class .= ' open-on-hover-click';
163 }
164 }
165
166 $navigation_child_content_class = $is_navigation_child ? ' wp-block-navigation-item__content' : '';
167
168 // If this is the first level of submenus, include the overlay colors.
169 if ( 1 === $depth && isset( $colors['overlay_css_classes'], $colors['overlay_inline_styles'] ) ) {
170 $css_class .= ' ' . trim( implode( ' ', $colors['overlay_css_classes'] ) );
171 if ( '' !== $colors['overlay_inline_styles'] ) {
172 $style_attribute = sprintf( ' style="%s"', esc_attr( $colors['overlay_inline_styles'] ) );
173 }
174 }
175
176 $title = wp_kses( $page['title'], wp_kses_allowed_html( 'post' ) );
177
178 $markup .= '<li class="wp-block-pages-list__item' . $css_class . '"' . $style_attribute . '>';
179
180 if ( isset( $page['children'] ) && $is_navigation_child && $open_submenus_on_click ) {
181 $markup .= '<button aria-label="' . $title . ' ' . __( 'submenu', 'gutenberg' ) . '" class="' . $navigation_child_content_class . ' wp-block-navigation-submenu__toggle" aria-expanded="false">' . $title . '<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" role="img" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg></span>' .
182 '</button>';
183 } else {
184 $markup .= '<a class="wp-block-pages-list__item__link' . $navigation_child_content_class . '" href="' . esc_url( $page['link'] ) . '"' . $aria_current . '>' . $title . '</a>';
185 }
186
187 if ( isset( $page['children'] ) ) {
188 if ( $is_navigation_child && $show_submenu_icons && ! $open_submenus_on_click ) {
189 $markup .= '<button aria-label="' . $title . ' ' . __( 'submenu', 'gutenberg' ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">';
190 $markup .= '<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" role="img" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg></span>';
191 $markup .= '</button>';
192 }
193 $markup .= '<ul class="submenu-container';
194 // Extra classname is added when the block is a child of Navigation.
195 if ( $is_navigation_child ) {
196 $markup .= ' wp-block-navigation__submenu-container';
197 }
198 $markup .= '">' . gutenberg_block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $page['children'], $active_page_ancestor_ids, $colors, $depth + 1 ) . '</ul>';
199 }
200 $markup .= '</li>';
201 }
202 return $markup;
203 }
204
205 /**
206 * Outputs nested array of pages
207 *
208 * @param array $current_level The level being iterated through.
209 * @param array $children The children grouped by parent post ID.
210 *
211 * @return array The nested array of pages.
212 */
213 function gutenberg_block_core_page_list_nest_pages( $current_level, $children ) {
214 if ( empty( $current_level ) ) {
215 return;
216 }
217 foreach ( (array) $current_level as $key => $current ) {
218 if ( isset( $children[ $key ] ) ) {
219 $current_level[ $key ]['children'] = gutenberg_block_core_page_list_nest_pages( $children[ $key ], $children );
220 }
221 }
222 return $current_level;
223 }
224
225 /**
226 * Renders the `core/page-list` block on server.
227 *
228 * @param array $attributes The block attributes.
229 * @param array $content The saved content.
230 * @param array $block The parsed block.
231 *
232 * @return string Returns the page list markup.
233 */
234 function gutenberg_render_block_core_page_list( $attributes, $content, $block ) {
235 static $block_id = 0;
236 $block_id++;
237
238 $all_pages = get_pages(
239 array(
240 'sort_column' => 'menu_order,post_title',
241 'order' => 'asc',
242 )
243 );
244
245 // If thare are no pages, there is nothing to show.
246 if ( empty( $all_pages ) ) {
247 return;
248 }
249
250 $top_level_pages = array();
251
252 $pages_with_children = array();
253
254 $active_page_ancestor_ids = array();
255
256 foreach ( (array) $all_pages as $page ) {
257 $is_active = ! empty( $page->ID ) && ( get_the_ID() === $page->ID );
258
259 if ( $is_active ) {
260 $active_page_ancestor_ids = get_post_ancestors( $page->ID );
261 }
262
263 if ( $page->post_parent ) {
264 $pages_with_children[ $page->post_parent ][ $page->ID ] = array(
265 'page_id' => $page->ID,
266 'title' => $page->post_title,
267 'link' => get_permalink( $page->ID ),
268 'is_active' => $is_active,
269 );
270 } else {
271 $top_level_pages[ $page->ID ] = array(
272 'page_id' => $page->ID,
273 'title' => $page->post_title,
274 'link' => get_permalink( $page->ID ),
275 'is_active' => $is_active,
276 );
277
278 }
279 }
280
281 $colors = gutenberg_block_core_page_list_build_css_colors( $attributes, $block->context );
282 $font_sizes = gutenberg_block_core_page_list_build_css_font_sizes( $block->context );
283 $classes = array_merge(
284 $colors['css_classes'],
285 $font_sizes['css_classes']
286 );
287 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
288 $css_classes = trim( implode( ' ', $classes ) );
289
290 $nested_pages = gutenberg_block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );
291
292 // Limit the number of items to be visually displayed.
293 if ( ! empty( $attributes['__unstableMaxPages'] ) ) {
294 $nested_pages = array_slice( $nested_pages, 0, $attributes['__unstableMaxPages'] );
295 }
296
297 $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context );
298
299 $open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false;
300
301 $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false;
302
303 $wrapper_markup = '<ul %1$s>%2$s</ul>';
304
305 $items_markup = gutenberg_block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $nested_pages, $active_page_ancestor_ids, $colors );
306
307 $wrapper_attributes = get_block_wrapper_attributes(
308 array(
309 'class' => $css_classes,
310 'style' => $style_attribute,
311 )
312 );
313
314 return sprintf(
315 $wrapper_markup,
316 $wrapper_attributes,
317 $items_markup
318 );
319 }
320
321 /**
322 * Registers the `core/pages` block on server.
323 */
324 function gutenberg_register_block_core_page_list() {
325 register_block_type_from_metadata(
326 __DIR__ . '/page-list',
327 array(
328 'render_callback' => 'gutenberg_render_block_core_page_list',
329 )
330 );
331 }
332 add_action( 'init', 'gutenberg_register_block_core_page_list', 20 );
333