page-list.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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(
  106. 'font-size: %s;',
  107. wp_get_typography_font_size_value(
  108. array(
  109. 'size' => $context['style']['typography']['fontSize'],
  110. )
  111. )
  112. );
  113. }
  114. return $font_sizes;
  115. }
  116. /**
  117. * Outputs Page list markup from an array of pages with nested children.
  118. *
  119. * @param boolean $open_submenus_on_click Whether to open submenus on click instead of hover.
  120. * @param boolean $show_submenu_icons Whether to show submenu indicator icons.
  121. * @param boolean $is_navigation_child If block is a child of Navigation block.
  122. * @param array $nested_pages The array of nested pages.
  123. * @param array $active_page_ancestor_ids An array of ancestor ids for active page.
  124. * @param array $colors Color information for overlay styles.
  125. * @param integer $depth The nesting depth.
  126. *
  127. * @return string List markup.
  128. */
  129. 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 ) {
  130. if ( empty( $nested_pages ) ) {
  131. return;
  132. }
  133. $markup = '';
  134. foreach ( (array) $nested_pages as $page ) {
  135. $css_class = $page['is_active'] ? ' current-menu-item' : '';
  136. $aria_current = $page['is_active'] ? ' aria-current="page"' : '';
  137. $style_attribute = '';
  138. $css_class .= in_array( $page['page_id'], $active_page_ancestor_ids, true ) ? ' current-menu-ancestor' : '';
  139. if ( isset( $page['children'] ) ) {
  140. $css_class .= ' has-child';
  141. }
  142. if ( $is_navigation_child ) {
  143. $css_class .= ' wp-block-navigation-item';
  144. if ( $open_submenus_on_click ) {
  145. $css_class .= ' open-on-click';
  146. } elseif ( $show_submenu_icons ) {
  147. $css_class .= ' open-on-hover-click';
  148. }
  149. }
  150. $navigation_child_content_class = $is_navigation_child ? ' wp-block-navigation-item__content' : '';
  151. // If this is the first level of submenus, include the overlay colors.
  152. if ( 1 === $depth && isset( $colors['overlay_css_classes'], $colors['overlay_inline_styles'] ) ) {
  153. $css_class .= ' ' . trim( implode( ' ', $colors['overlay_css_classes'] ) );
  154. if ( '' !== $colors['overlay_inline_styles'] ) {
  155. $style_attribute = sprintf( ' style="%s"', esc_attr( $colors['overlay_inline_styles'] ) );
  156. }
  157. }
  158. $front_page_id = (int) get_option( 'page_on_front' );
  159. if ( (int) $page['page_id'] === $front_page_id ) {
  160. $css_class .= ' menu-item-home';
  161. }
  162. $title = wp_kses_post( $page['title'] );
  163. $aria_label = sprintf(
  164. /* translators: Accessibility text. %s: Parent page title. */
  165. __( '%s submenu' ),
  166. wp_strip_all_tags( $title )
  167. );
  168. $markup .= '<li class="wp-block-pages-list__item' . esc_attr( $css_class ) . '"' . $style_attribute . '>';
  169. if ( isset( $page['children'] ) && $is_navigation_child && $open_submenus_on_click ) {
  170. $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 ) .
  171. '</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>';
  172. } else {
  173. $markup .= '<a class="wp-block-pages-list__item__link' . esc_attr( $navigation_child_content_class ) . '" href="' . esc_url( $page['link'] ) . '"' . $aria_current . '>' . $title . '</a>';
  174. }
  175. if ( isset( $page['children'] ) ) {
  176. if ( $is_navigation_child && $show_submenu_icons && ! $open_submenus_on_click ) {
  177. $markup .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">';
  178. $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>';
  179. $markup .= '</button>';
  180. }
  181. $markup .= '<ul class="submenu-container';
  182. // Extra classname is added when the block is a child of Navigation.
  183. if ( $is_navigation_child ) {
  184. $markup .= ' wp-block-navigation__submenu-container';
  185. }
  186. $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>';
  187. }
  188. $markup .= '</li>';
  189. }
  190. return $markup;
  191. }
  192. /**
  193. * Outputs nested array of pages
  194. *
  195. * @param array $current_level The level being iterated through.
  196. * @param array $children The children grouped by parent post ID.
  197. *
  198. * @return array The nested array of pages.
  199. */
  200. function block_core_page_list_nest_pages( $current_level, $children ) {
  201. if ( empty( $current_level ) ) {
  202. return;
  203. }
  204. foreach ( (array) $current_level as $key => $current ) {
  205. if ( isset( $children[ $key ] ) ) {
  206. $current_level[ $key ]['children'] = block_core_page_list_nest_pages( $children[ $key ], $children );
  207. }
  208. }
  209. return $current_level;
  210. }
  211. /**
  212. * Renders the `core/page-list` block on server.
  213. *
  214. * @param array $attributes The block attributes.
  215. * @param string $content The saved content.
  216. * @param WP_Block $block The parsed block.
  217. *
  218. * @return string Returns the page list markup.
  219. */
  220. function render_block_core_page_list( $attributes, $content, $block ) {
  221. static $block_id = 0;
  222. $block_id++;
  223. $all_pages = get_pages(
  224. array(
  225. 'sort_column' => 'menu_order,post_title',
  226. 'order' => 'asc',
  227. )
  228. );
  229. // If there are no pages, there is nothing to show.
  230. if ( empty( $all_pages ) ) {
  231. return;
  232. }
  233. $top_level_pages = array();
  234. $pages_with_children = array();
  235. $active_page_ancestor_ids = array();
  236. foreach ( (array) $all_pages as $page ) {
  237. $is_active = ! empty( $page->ID ) && ( get_the_ID() === $page->ID );
  238. if ( $is_active ) {
  239. $active_page_ancestor_ids = get_post_ancestors( $page->ID );
  240. }
  241. if ( $page->post_parent ) {
  242. $pages_with_children[ $page->post_parent ][ $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. } else {
  249. $top_level_pages[ $page->ID ] = array(
  250. 'page_id' => $page->ID,
  251. 'title' => $page->post_title,
  252. 'link' => get_permalink( $page->ID ),
  253. 'is_active' => $is_active,
  254. );
  255. }
  256. }
  257. $colors = block_core_page_list_build_css_colors( $attributes, $block->context );
  258. $font_sizes = block_core_page_list_build_css_font_sizes( $block->context );
  259. $classes = array_merge(
  260. $colors['css_classes'],
  261. $font_sizes['css_classes']
  262. );
  263. $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
  264. $css_classes = trim( implode( ' ', $classes ) );
  265. $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );
  266. $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context );
  267. $open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false;
  268. $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false;
  269. $wrapper_markup = '<ul %1$s>%2$s</ul>';
  270. $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 );
  271. $wrapper_attributes = get_block_wrapper_attributes(
  272. array(
  273. 'class' => $css_classes,
  274. 'style' => $style_attribute,
  275. )
  276. );
  277. return sprintf(
  278. $wrapper_markup,
  279. $wrapper_attributes,
  280. $items_markup
  281. );
  282. }
  283. /**
  284. * Registers the `core/pages` block on server.
  285. */
  286. function register_block_core_page_list() {
  287. register_block_type_from_metadata(
  288. __DIR__ . '/page-list',
  289. array(
  290. 'render_callback' => 'render_block_core_page_list',
  291. )
  292. );
  293. }
  294. add_action( 'init', 'register_block_core_page_list' );