[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 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 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( 123 'font-size: %s;', 124 wp_get_typography_font_size_value( 125 array( 126 'size' => $context['style']['typography']['fontSize'], 127 ) 128 ) 129 ); 130 } 131 132 return $font_sizes; 133 } 134 135 /** 136 * Outputs Page list markup from an array of pages with nested children. 137 * 138 * @param boolean $open_submenus_on_click Whether to open submenus on click instead of hover. 139 * @param boolean $show_submenu_icons Whether to show submenu indicator icons. 140 * @param boolean $is_navigation_child If block is a child of Navigation block. 141 * @param array $nested_pages The array of nested pages. 142 * @param boolean $is_nested Whether the submenu is nested or not. 143 * @param array $active_page_ancestor_ids An array of ancestor ids for active page. 144 * @param array $colors Color information for overlay styles. 145 * @param integer $depth The nesting depth. 146 * 147 * @return string List markup. 148 */ 149 function block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $nested_pages, $is_nested, $active_page_ancestor_ids = array(), $colors = array(), $depth = 0 ) { 150 if ( empty( $nested_pages ) ) { 151 return; 152 } 153 $markup = ''; 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 if ( $open_submenus_on_click ) { 168 $css_class .= ' open-on-click'; 169 } elseif ( $show_submenu_icons ) { 170 $css_class .= ' open-on-hover-click'; 171 } 172 } 173 174 $navigation_child_content_class = $is_navigation_child ? ' wp-block-navigation-item__content' : ''; 175 176 // If this is the first level of submenus, include the overlay colors. 177 if ( ( ( 0 < $depth && ! $is_nested ) || $is_nested ) && isset( $colors['overlay_css_classes'], $colors['overlay_inline_styles'] ) ) { 178 $css_class .= ' ' . trim( implode( ' ', $colors['overlay_css_classes'] ) ); 179 if ( '' !== $colors['overlay_inline_styles'] ) { 180 $style_attribute = sprintf( ' style="%s"', esc_attr( $colors['overlay_inline_styles'] ) ); 181 } 182 } 183 184 $front_page_id = (int) get_option( 'page_on_front' ); 185 if ( (int) $page['page_id'] === $front_page_id ) { 186 $css_class .= ' menu-item-home'; 187 } 188 189 $title = wp_kses_post( $page['title'] ); 190 $aria_label = sprintf( 191 /* translators: Accessibility text. %s: Parent page title. */ 192 __( '%s submenu' ), 193 wp_strip_all_tags( $title ) 194 ); 195 196 $markup .= '<li class="wp-block-pages-list__item' . esc_attr( $css_class ) . '"' . $style_attribute . '>'; 197 198 if ( isset( $page['children'] ) && $is_navigation_child && $open_submenus_on_click ) { 199 $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 ) . 200 '</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>'; 201 } else { 202 $markup .= '<a class="wp-block-pages-list__item__link' . esc_attr( $navigation_child_content_class ) . '" href="' . esc_url( $page['link'] ) . '"' . $aria_current . '>' . $title . '</a>'; 203 } 204 205 if ( isset( $page['children'] ) ) { 206 if ( $is_navigation_child && $show_submenu_icons && ! $open_submenus_on_click ) { 207 $markup .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">'; 208 $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>'; 209 $markup .= '</button>'; 210 } 211 $markup .= '<ul class="wp-block-navigation__submenu-container">'; 212 $markup .= block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $page['children'], $is_nested, $active_page_ancestor_ids, $colors, $depth + 1 ); 213 $markup .= '</ul>'; 214 } 215 $markup .= '</li>'; 216 } 217 return $markup; 218 } 219 220 /** 221 * Outputs nested array of pages 222 * 223 * @param array $current_level The level being iterated through. 224 * @param array $children The children grouped by parent post ID. 225 * 226 * @return array The nested array of pages. 227 */ 228 function block_core_page_list_nest_pages( $current_level, $children ) { 229 if ( empty( $current_level ) ) { 230 return; 231 } 232 foreach ( (array) $current_level as $key => $current ) { 233 if ( isset( $children[ $key ] ) ) { 234 $current_level[ $key ]['children'] = block_core_page_list_nest_pages( $children[ $key ], $children ); 235 } 236 } 237 return $current_level; 238 } 239 240 /** 241 * Renders the `core/page-list` block on server. 242 * 243 * @param array $attributes The block attributes. 244 * @param string $content The saved content. 245 * @param WP_Block $block The parsed block. 246 * 247 * @return string Returns the page list markup. 248 */ 249 function render_block_core_page_list( $attributes, $content, $block ) { 250 static $block_id = 0; 251 ++$block_id; 252 253 $parent_page_id = $attributes['parentPageID']; 254 $is_nested = $attributes['isNested']; 255 256 $all_pages = get_pages( 257 array( 258 'sort_column' => 'menu_order,post_title', 259 'order' => 'asc', 260 ) 261 ); 262 263 // If there are no pages, there is nothing to show. 264 if ( empty( $all_pages ) ) { 265 return; 266 } 267 268 $top_level_pages = array(); 269 270 $pages_with_children = array(); 271 272 $active_page_ancestor_ids = array(); 273 274 foreach ( (array) $all_pages as $page ) { 275 $is_active = ! empty( $page->ID ) && ( get_queried_object_id() === $page->ID ); 276 277 if ( $is_active ) { 278 $active_page_ancestor_ids = get_post_ancestors( $page->ID ); 279 } 280 281 if ( $page->post_parent ) { 282 $pages_with_children[ $page->post_parent ][ $page->ID ] = array( 283 'page_id' => $page->ID, 284 'title' => $page->post_title, 285 'link' => get_permalink( $page->ID ), 286 'is_active' => $is_active, 287 ); 288 } else { 289 $top_level_pages[ $page->ID ] = array( 290 'page_id' => $page->ID, 291 'title' => $page->post_title, 292 'link' => get_permalink( $page->ID ), 293 'is_active' => $is_active, 294 ); 295 296 } 297 } 298 299 $colors = block_core_page_list_build_css_colors( $attributes, $block->context ); 300 $font_sizes = block_core_page_list_build_css_font_sizes( $block->context ); 301 $classes = array_merge( 302 $colors['css_classes'], 303 $font_sizes['css_classes'] 304 ); 305 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] ); 306 $css_classes = trim( implode( ' ', $classes ) ); 307 308 $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children ); 309 310 if ( 0 !== $parent_page_id ) { 311 // If the parent page has no child pages, there is nothing to show. 312 if ( ! array_key_exists( $parent_page_id, $pages_with_children ) ) { 313 return; 314 } 315 316 $nested_pages = block_core_page_list_nest_pages( 317 $pages_with_children[ $parent_page_id ], 318 $pages_with_children 319 ); 320 } 321 322 $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context ); 323 324 $open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false; 325 326 $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false; 327 328 $wrapper_markup = $is_nested ? '%2$s' : '<ul %1$s>%2$s</ul>'; 329 330 $items_markup = block_core_page_list_render_nested_page_list( $open_submenus_on_click, $show_submenu_icons, $is_navigation_child, $nested_pages, $is_nested, $active_page_ancestor_ids, $colors ); 331 332 $wrapper_attributes = get_block_wrapper_attributes( 333 array( 334 'class' => $css_classes, 335 'style' => $style_attribute, 336 ) 337 ); 338 339 return sprintf( 340 $wrapper_markup, 341 $wrapper_attributes, 342 $items_markup 343 ); 344 } 345 346 /** 347 * Registers the `core/pages` block on server. 348 */ 349 function register_block_core_page_list() { 350 register_block_type_from_metadata( 351 __DIR__ . '/page-list', 352 array( 353 'render_callback' => 'render_block_core_page_list', 354 ) 355 ); 356 } 357 add_action( 'init', 'register_block_core_page_list' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Mar 25 08:20:01 2023 | Cross-referenced by PHPXref |