7 |
7 |
8 /** |
8 /** |
9 * Build an array with CSS classes and inline styles defining the colors |
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. |
10 * which will be applied to the pages markup in the front-end when it is a descendant of navigation. |
11 * |
11 * |
12 * @param array $context Navigation block context. |
12 * @param array $attributes Block attributes. |
|
13 * @param array $context Navigation block context. |
13 * @return array Colors CSS classes and inline styles. |
14 * @return array Colors CSS classes and inline styles. |
14 */ |
15 */ |
15 function block_core_page_list_build_css_colors( $context ) { |
16 function block_core_page_list_build_css_colors( $attributes, $context ) { |
16 $colors = array( |
17 $colors = array( |
17 'css_classes' => array(), |
18 'css_classes' => array(), |
18 'inline_styles' => '', |
19 'inline_styles' => '', |
|
20 'overlay_css_classes' => array(), |
|
21 'overlay_inline_styles' => '', |
19 ); |
22 ); |
20 |
23 |
21 // Text color. |
24 // Text color. |
22 $has_named_text_color = array_key_exists( 'textColor', $context ); |
25 $has_named_text_color = array_key_exists( 'textColor', $context ); |
|
26 $has_picked_text_color = array_key_exists( 'customTextColor', $context ); |
23 $has_custom_text_color = isset( $context['style']['color']['text'] ); |
27 $has_custom_text_color = isset( $context['style']['color']['text'] ); |
24 |
28 |
25 // If has text color. |
29 // If has text color. |
26 if ( $has_custom_text_color || $has_named_text_color ) { |
30 if ( $has_custom_text_color || $has_picked_text_color || $has_named_text_color ) { |
27 // Add has-text-color class. |
31 // Add has-text-color class. |
28 $colors['css_classes'][] = 'has-text-color'; |
32 $colors['css_classes'][] = 'has-text-color'; |
29 } |
33 } |
30 |
34 |
31 if ( $has_named_text_color ) { |
35 if ( $has_named_text_color ) { |
32 // Add the color class. |
36 // Add the color class. |
33 $colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] ); |
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'] ); |
34 } elseif ( $has_custom_text_color ) { |
40 } elseif ( $has_custom_text_color ) { |
35 // Add the custom color inline style. |
41 // Add the custom color inline style. |
36 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] ); |
42 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] ); |
37 } |
43 } |
38 |
44 |
39 // Background color. |
45 // Background color. |
40 $has_named_background_color = array_key_exists( 'backgroundColor', $context ); |
46 $has_named_background_color = array_key_exists( 'backgroundColor', $context ); |
|
47 $has_picked_background_color = array_key_exists( 'customBackgroundColor', $context ); |
41 $has_custom_background_color = isset( $context['style']['color']['background'] ); |
48 $has_custom_background_color = isset( $context['style']['color']['background'] ); |
42 |
49 |
43 // If has background color. |
50 // If has background color. |
44 if ( $has_custom_background_color || $has_named_background_color ) { |
51 if ( $has_custom_background_color || $has_picked_background_color || $has_named_background_color ) { |
45 // Add has-background class. |
52 // Add has-background class. |
46 $colors['css_classes'][] = 'has-background'; |
53 $colors['css_classes'][] = 'has-background'; |
47 } |
54 } |
48 |
55 |
49 if ( $has_named_background_color ) { |
56 if ( $has_named_background_color ) { |
50 // Add the background-color class. |
57 // Add the background-color class. |
51 $colors['css_classes'][] = sprintf( 'has-%s-background-color', $context['backgroundColor'] ); |
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'] ); |
52 } elseif ( $has_custom_background_color ) { |
61 } elseif ( $has_custom_background_color ) { |
53 // Add the custom background-color inline style. |
62 // Add the custom background-color inline style. |
54 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] ); |
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'] ); |
55 } |
95 } |
56 |
96 |
57 return $colors; |
97 return $colors; |
58 } |
98 } |
59 |
99 |
77 if ( $has_named_font_size ) { |
117 if ( $has_named_font_size ) { |
78 // Add the font size class. |
118 // Add the font size class. |
79 $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] ); |
119 $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] ); |
80 } elseif ( $has_custom_font_size ) { |
120 } elseif ( $has_custom_font_size ) { |
81 // Add the custom font size inline style. |
121 // Add the custom font size inline style. |
82 $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $context['style']['typography']['fontSize'] ); |
122 $font_sizes['inline_styles'] = sprintf( 'font-size: %s;', $context['style']['typography']['fontSize'] ); |
83 } |
123 } |
84 |
124 |
85 return $font_sizes; |
125 return $font_sizes; |
86 } |
126 } |
87 |
127 |
88 /** |
128 /** |
89 * Outputs Page list markup from an array of pages with nested children. |
129 * Outputs Page list markup from an array of pages with nested children. |
90 * |
130 * |
91 * @param array $nested_pages The array of nested pages. |
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. |
92 * |
138 * |
93 * @return string List markup. |
139 * @return string List markup. |
94 */ |
140 */ |
95 function block_core_page_list_render_nested_page_list( $nested_pages ) { |
141 function 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 ) { |
96 if ( empty( $nested_pages ) ) { |
142 if ( empty( $nested_pages ) ) { |
97 return; |
143 return; |
98 } |
144 } |
99 $markup = ''; |
145 $markup = ''; |
100 foreach ( (array) $nested_pages as $page ) { |
146 foreach ( (array) $nested_pages as $page ) { |
101 $css_class = 'wp-block-pages-list__item'; |
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' : ''; |
102 if ( isset( $page['children'] ) ) { |
152 if ( isset( $page['children'] ) ) { |
103 $css_class .= ' has-child'; |
153 $css_class .= ' has-child'; |
104 } |
154 } |
105 $markup .= '<li class="' . $css_class . '">'; |
155 |
106 $markup .= '<a class="wp-block-pages-list__item__link" href="' . esc_url( $page['link'] ) . '">' . wp_kses( |
156 if ( $is_navigation_child ) { |
107 $page['title'], |
157 $css_class .= ' wp-block-navigation-item'; |
108 wp_kses_allowed_html( 'post' ) |
158 |
109 ) . '</a>'; |
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 $front_page_id = (int) get_option( 'page_on_front' ); |
|
177 if ( (int) $page['page_id'] === $front_page_id ) { |
|
178 $css_class .= ' menu-item-home'; |
|
179 } |
|
180 |
|
181 $title = wp_kses_post( $page['title'] ); |
|
182 $aria_label = sprintf( |
|
183 /* translators: Accessibility text. %s: Parent page title. */ |
|
184 __( '%s submenu' ), |
|
185 wp_strip_all_tags( $title ) |
|
186 ); |
|
187 |
|
188 $markup .= '<li class="wp-block-pages-list__item' . esc_attr( $css_class ) . '"' . $style_attribute . '>'; |
|
189 |
|
190 if ( isset( $page['children'] ) && $is_navigation_child && $open_submenus_on_click ) { |
|
191 $markup .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="' . esc_attr( $navigation_child_content_class ) . ' wp-block-navigation-submenu__toggle" aria-expanded="false">' . esc_html( $title ) . |
|
192 '</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>'; |
|
193 } else { |
|
194 $markup .= '<a class="wp-block-pages-list__item__link' . esc_attr( $navigation_child_content_class ) . '" href="' . esc_url( $page['link'] ) . '"' . $aria_current . '>' . $title . '</a>'; |
|
195 } |
|
196 |
110 if ( isset( $page['children'] ) ) { |
197 if ( isset( $page['children'] ) ) { |
111 $markup .= '<span class="wp-block-page-list__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>'; |
198 if ( $is_navigation_child && $show_submenu_icons && ! $open_submenus_on_click ) { |
112 $markup .= '<ul class="submenu-container">' . block_core_page_list_render_nested_page_list( $page['children'] ) . '</ul>'; |
199 $markup .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">'; |
|
200 $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>'; |
|
201 $markup .= '</button>'; |
|
202 } |
|
203 $markup .= '<ul class="submenu-container'; |
|
204 // Extra classname is added when the block is a child of Navigation. |
|
205 if ( $is_navigation_child ) { |
|
206 $markup .= ' wp-block-navigation__submenu-container'; |
|
207 } |
|
208 $markup .= '">' . 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>'; |
113 } |
209 } |
114 $markup .= '</li>'; |
210 $markup .= '</li>'; |
115 } |
211 } |
116 return $markup; |
212 return $markup; |
117 } |
213 } |
137 } |
233 } |
138 |
234 |
139 /** |
235 /** |
140 * Renders the `core/page-list` block on server. |
236 * Renders the `core/page-list` block on server. |
141 * |
237 * |
142 * @param array $attributes The block attributes. |
238 * @param array $attributes The block attributes. |
143 * @param array $content The saved content. |
239 * @param string $content The saved content. |
144 * @param array $block The parsed block. |
240 * @param WP_Block $block The parsed block. |
145 * |
241 * |
146 * @return string Returns the page list markup. |
242 * @return string Returns the page list markup. |
147 */ |
243 */ |
148 function render_block_core_page_list( $attributes, $content, $block ) { |
244 function render_block_core_page_list( $attributes, $content, $block ) { |
149 static $block_id = 0; |
245 static $block_id = 0; |
150 $block_id++; |
246 $block_id++; |
151 |
247 |
152 // TODO: When https://core.trac.wordpress.org/ticket/39037 REST API support for multiple orderby values is resolved, |
|
153 // update 'sort_column' to 'menu_order, post_title'. Sorting by both menu_order and post_title ensures a stable sort. |
|
154 // Otherwise with pages that have the same menu_order value, we can see different ordering depending on how DB |
|
155 // queries are constructed internally. For example we might see a different order when a limit is set to <499 |
|
156 // versus >= 500. |
|
157 $all_pages = get_pages( |
248 $all_pages = get_pages( |
158 array( |
249 array( |
159 'sort_column' => 'menu_order', |
250 'sort_column' => 'menu_order,post_title', |
160 'order' => 'asc', |
251 'order' => 'asc', |
161 ) |
252 ) |
162 ); |
253 ); |
163 |
254 |
|
255 // If there are no pages, there is nothing to show. |
|
256 if ( empty( $all_pages ) ) { |
|
257 return; |
|
258 } |
|
259 |
164 $top_level_pages = array(); |
260 $top_level_pages = array(); |
165 |
261 |
166 $pages_with_children = array(); |
262 $pages_with_children = array(); |
167 |
263 |
|
264 $active_page_ancestor_ids = array(); |
|
265 |
168 foreach ( (array) $all_pages as $page ) { |
266 foreach ( (array) $all_pages as $page ) { |
|
267 $is_active = ! empty( $page->ID ) && ( get_the_ID() === $page->ID ); |
|
268 |
|
269 if ( $is_active ) { |
|
270 $active_page_ancestor_ids = get_post_ancestors( $page->ID ); |
|
271 } |
|
272 |
169 if ( $page->post_parent ) { |
273 if ( $page->post_parent ) { |
170 $pages_with_children[ $page->post_parent ][ $page->ID ] = array( |
274 $pages_with_children[ $page->post_parent ][ $page->ID ] = array( |
171 'title' => $page->post_title, |
275 'page_id' => $page->ID, |
172 'link' => get_permalink( $page->ID ), |
276 'title' => $page->post_title, |
|
277 'link' => get_permalink( $page->ID ), |
|
278 'is_active' => $is_active, |
173 ); |
279 ); |
174 } else { |
280 } else { |
175 $top_level_pages[ $page->ID ] = array( |
281 $top_level_pages[ $page->ID ] = array( |
176 'title' => $page->post_title, |
282 'page_id' => $page->ID, |
177 'link' => get_permalink( $page->ID ), |
283 'title' => $page->post_title, |
|
284 'link' => get_permalink( $page->ID ), |
|
285 'is_active' => $is_active, |
178 ); |
286 ); |
179 |
287 |
180 } |
288 } |
181 } |
289 } |
182 |
290 |
183 $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children ); |
291 $colors = block_core_page_list_build_css_colors( $attributes, $block->context ); |
184 |
|
185 $wrapper_markup = '<ul %1$s>%2$s</ul>'; |
|
186 |
|
187 $items_markup = block_core_page_list_render_nested_page_list( $nested_pages ); |
|
188 |
|
189 $colors = block_core_page_list_build_css_colors( $block->context ); |
|
190 $font_sizes = block_core_page_list_build_css_font_sizes( $block->context ); |
292 $font_sizes = block_core_page_list_build_css_font_sizes( $block->context ); |
191 $classes = array_merge( |
293 $classes = array_merge( |
192 $colors['css_classes'], |
294 $colors['css_classes'], |
193 $font_sizes['css_classes'] |
295 $font_sizes['css_classes'] |
194 ); |
296 ); |
195 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); |
297 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); |
196 $css_classes = trim( implode( ' ', $classes ) ); |
298 $css_classes = trim( implode( ' ', $classes ) ); |
197 |
299 |
198 if ( $block->context && $block->context['showSubmenuIcon'] ) { |
300 $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children ); |
199 $css_classes .= ' show-submenu-icons'; |
301 |
200 } |
302 // Limit the number of items to be visually displayed. |
|
303 if ( ! empty( $attributes['__unstableMaxPages'] ) ) { |
|
304 $nested_pages = array_slice( $nested_pages, 0, $attributes['__unstableMaxPages'] ); |
|
305 } |
|
306 |
|
307 $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context ); |
|
308 |
|
309 $open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false; |
|
310 |
|
311 $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false; |
|
312 |
|
313 $wrapper_markup = '<ul %1$s>%2$s</ul>'; |
|
314 |
|
315 $items_markup = 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 ); |
201 |
316 |
202 $wrapper_attributes = get_block_wrapper_attributes( |
317 $wrapper_attributes = get_block_wrapper_attributes( |
203 array( |
318 array( |
204 'class' => $css_classes, |
319 'class' => $css_classes, |
205 'style' => $style_attribute, |
320 'style' => $style_attribute, |