暫無描述

page-list.php 13KB

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