Changeset 52042 for trunk/src/wp-includes/blocks/page-list.php
- Timestamp:
- 11/08/2021 02:26:27 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/page-list.php
r51199 r52042 10 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 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 17 $colors = array( 17 'css_classes' => array(), 18 'inline_styles' => '', 18 'css_classes' => array(), 19 'inline_styles' => '', 20 'overlay_css_classes' => array(), 21 'overlay_inline_styles' => '', 19 22 ); 20 23 21 24 // Text color. 22 25 $has_named_text_color = array_key_exists( 'textColor', $context ); 26 $has_picked_text_color = array_key_exists( 'customTextColor', $context ); 23 27 $has_custom_text_color = isset( $context['style']['color']['text'] ); 24 28 25 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 31 // Add has-text-color class. 28 32 $colors['css_classes'][] = 'has-text-color'; … … 31 35 if ( $has_named_text_color ) { 32 36 // Add the color class. 33 $colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] ); 37 $colors['css_classes'][] = sprintf( 'has-%s-color', gutenberg_experimental_to_kebab_case( $context['textColor'] ) ); 38 } elseif ( $has_picked_text_color ) { 39 $colors['inline_styles'] .= sprintf( 'color: %s;', $context['customTextColor'] ); 34 40 } elseif ( $has_custom_text_color ) { 35 41 // Add the custom color inline style. … … 39 45 // Background color. 40 46 $has_named_background_color = array_key_exists( 'backgroundColor', $context ); 47 $has_picked_background_color = array_key_exists( 'customBackgroundColor', $context ); 41 48 $has_custom_background_color = isset( $context['style']['color']['background'] ); 42 49 43 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 52 // Add has-background class. 46 53 $colors['css_classes'][] = 'has-background'; … … 49 56 if ( $has_named_background_color ) { 50 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', gutenberg_experimental_to_kebab_case( $context['backgroundColor'] ) ); 59 } elseif ( $has_picked_background_color ) { 60 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['customBackgroundColor'] ); 52 61 } elseif ( $has_custom_background_color ) { 53 62 // Add the custom background-color inline style. 54 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', gutenberg_experimental_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', gutenberg_experimental_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 … … 89 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 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 142 if ( empty( $nested_pages ) ) { 97 143 return; … … 99 145 $markup = ''; 100 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 152 if ( isset( $page['children'] ) ) { 103 153 $css_class .= ' has-child'; 104 154 } 105 $markup .= '<li class="' . $css_class . '">'; 106 $markup .= '<a class="wp-block-pages-list__item__link" href="' . esc_url( $page['link'] ) . '">' . wp_kses( 107 $page['title'], 108 wp_kses_allowed_html( 'post' ) 109 ) . '</a>'; 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 $markup .= '<li class="wp-block-pages-list__item' . $css_class . '"' . $style_attribute . '>'; 177 178 if ( isset( $page['children'] ) && $is_navigation_child && $open_submenus_on_click ) { 179 $markup .= '<button class="' . $navigation_child_content_class . ' wp-block-navigation-submenu__toggle" aria-expanded="false">' . wp_kses( 180 $page['title'], 181 wp_kses_allowed_html( 'post' ) 182 ) . '<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>' . 183 '</button>'; 184 } else { 185 $markup .= '<a class="wp-block-pages-list__item__link' . $navigation_child_content_class . ' "href="' . esc_url( $page['link'] ) . '"' . $aria_current . '>' . wp_kses( 186 $page['title'], 187 wp_kses_allowed_html( 'post' ) 188 ) . '</a>'; 189 } 190 110 191 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>'; 112 $markup .= '<ul class="submenu-container">' . block_core_page_list_render_nested_page_list( $page['children'] ) . '</ul>'; 192 if ( $is_navigation_child && $show_submenu_icons && ! $open_submenus_on_click ) { 193 $markup .= '<button class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">'; 194 $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>'; 195 $markup .= '</button>'; 196 } 197 $markup .= '<ul class="submenu-container'; 198 // Extra classname is added when the block is a child of Navigation. 199 if ( $is_navigation_child ) { 200 $markup .= ' wp-block-navigation__submenu-container'; 201 } 202 $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 203 } 114 204 $markup .= '</li>'; … … 150 240 $block_id++; 151 241 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 DB155 // queries are constructed internally. For example we might see a different order when a limit is set to <499156 // versus >= 500.157 242 $all_pages = get_pages( 158 243 array( 159 'sort_column' => 'menu_order ',244 'sort_column' => 'menu_order,post_title', 160 245 'order' => 'asc', 161 246 ) 162 247 ); 163 248 249 // If thare are no pages, there is nothing to show. 250 if ( empty( $all_pages ) ) { 251 return; 252 } 253 164 254 $top_level_pages = array(); 165 255 166 256 $pages_with_children = array(); 167 257 258 $active_page_ancestor_ids = array(); 259 168 260 foreach ( (array) $all_pages as $page ) { 261 $is_active = ! empty( $page->ID ) && ( get_the_ID() === $page->ID ); 262 263 if ( $is_active ) { 264 $active_page_ancestor_ids = get_post_ancestors( $page->ID ); 265 } 266 169 267 if ( $page->post_parent ) { 170 268 $pages_with_children[ $page->post_parent ][ $page->ID ] = array( 171 'title' => $page->post_title, 172 'link' => get_permalink( $page->ID ), 269 'page_id' => $page->ID, 270 'title' => $page->post_title, 271 'link' => get_permalink( $page->ID ), 272 'is_active' => $is_active, 173 273 ); 174 274 } else { 175 275 $top_level_pages[ $page->ID ] = array( 176 'title' => $page->post_title, 177 'link' => get_permalink( $page->ID ), 276 'page_id' => $page->ID, 277 'title' => $page->post_title, 278 'link' => get_permalink( $page->ID ), 279 'is_active' => $is_active, 178 280 ); 179 281 … … 181 283 } 182 284 183 $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children ); 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 ); 285 $colors = block_core_page_list_build_css_colors( $attributes, $block->context ); 190 286 $font_sizes = block_core_page_list_build_css_font_sizes( $block->context ); 191 287 $classes = array_merge( … … 196 292 $css_classes = trim( implode( ' ', $classes ) ); 197 293 198 if ( $block->context && $block->context['showSubmenuIcon'] ) { 199 $css_classes .= ' show-submenu-icons'; 200 } 294 $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children ); 295 296 $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context ); 297 298 $open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false; 299 300 $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false; 301 302 $wrapper_markup = '<ul %1$s>%2$s</ul>'; 303 304 $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 305 202 306 $wrapper_attributes = get_block_wrapper_attributes(
Note: See TracChangeset
for help on using the changeset viewer.