Bez popisu

page-list.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 $context Navigation block context.
  12. * @return array Colors CSS classes and inline styles.
  13. */
  14. function block_core_page_list_build_css_colors( $context ) {
  15. $colors = array(
  16. 'css_classes' => array(),
  17. 'inline_styles' => '',
  18. );
  19. // Text color.
  20. $has_named_text_color = array_key_exists( 'textColor', $context );
  21. $has_custom_text_color = isset( $context['style']['color']['text'] );
  22. // If has text color.
  23. if ( $has_custom_text_color || $has_named_text_color ) {
  24. // Add has-text-color class.
  25. $colors['css_classes'][] = 'has-text-color';
  26. }
  27. if ( $has_named_text_color ) {
  28. // Add the color class.
  29. $colors['css_classes'][] = sprintf( 'has-%s-color', $context['textColor'] );
  30. } elseif ( $has_custom_text_color ) {
  31. // Add the custom color inline style.
  32. $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] );
  33. }
  34. // Background color.
  35. $has_named_background_color = array_key_exists( 'backgroundColor', $context );
  36. $has_custom_background_color = isset( $context['style']['color']['background'] );
  37. // If has background color.
  38. if ( $has_custom_background_color || $has_named_background_color ) {
  39. // Add has-background class.
  40. $colors['css_classes'][] = 'has-background';
  41. }
  42. if ( $has_named_background_color ) {
  43. // Add the background-color class.
  44. $colors['css_classes'][] = sprintf( 'has-%s-background-color', $context['backgroundColor'] );
  45. } elseif ( $has_custom_background_color ) {
  46. // Add the custom background-color inline style.
  47. $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] );
  48. }
  49. return $colors;
  50. }
  51. /**
  52. * Build an array with CSS classes and inline styles defining the font sizes
  53. * which will be applied to the pages markup in the front-end when it is a descendant of navigation.
  54. *
  55. * @param array $context Navigation block context.
  56. * @return array Font size CSS classes and inline styles.
  57. */
  58. function block_core_page_list_build_css_font_sizes( $context ) {
  59. // CSS classes.
  60. $font_sizes = array(
  61. 'css_classes' => array(),
  62. 'inline_styles' => '',
  63. );
  64. $has_named_font_size = array_key_exists( 'fontSize', $context );
  65. $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
  66. if ( $has_named_font_size ) {
  67. // Add the font size class.
  68. $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
  69. } elseif ( $has_custom_font_size ) {
  70. // Add the custom font size inline style.
  71. $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $context['style']['typography']['fontSize'] );
  72. }
  73. return $font_sizes;
  74. }
  75. /**
  76. * Outputs Page list markup from an array of pages with nested children.
  77. *
  78. * @param array $nested_pages The array of nested pages.
  79. *
  80. * @return string List markup.
  81. */
  82. function block_core_page_list_render_nested_page_list( $nested_pages ) {
  83. if ( empty( $nested_pages ) ) {
  84. return;
  85. }
  86. $markup = '';
  87. foreach ( (array) $nested_pages as $page ) {
  88. $css_class = 'wp-block-pages-list__item';
  89. if ( isset( $page['children'] ) ) {
  90. $css_class .= ' has-child';
  91. }
  92. $markup .= '<li class="' . $css_class . '">';
  93. $markup .= '<a class="wp-block-pages-list__item__link" href="' . esc_url( $page['link'] ) . '">' . wp_kses(
  94. $page['title'],
  95. wp_kses_allowed_html( 'post' )
  96. ) . '</a>';
  97. if ( isset( $page['children'] ) ) {
  98. $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>';
  99. $markup .= '<ul class="submenu-container">' . block_core_page_list_render_nested_page_list( $page['children'] ) . '</ul>';
  100. }
  101. $markup .= '</li>';
  102. }
  103. return $markup;
  104. }
  105. /**
  106. * Outputs nested array of pages
  107. *
  108. * @param array $current_level The level being iterated through.
  109. * @param array $children The children grouped by parent post ID.
  110. *
  111. * @return array The nested array of pages.
  112. */
  113. function block_core_page_list_nest_pages( $current_level, $children ) {
  114. if ( empty( $current_level ) ) {
  115. return;
  116. }
  117. foreach ( (array) $current_level as $key => $current ) {
  118. if ( isset( $children[ $key ] ) ) {
  119. $current_level[ $key ]['children'] = block_core_page_list_nest_pages( $children[ $key ], $children );
  120. }
  121. }
  122. return $current_level;
  123. }
  124. /**
  125. * Renders the `core/page-list` block on server.
  126. *
  127. * @param array $attributes The block attributes.
  128. * @param array $content The saved content.
  129. * @param array $block The parsed block.
  130. *
  131. * @return string Returns the page list markup.
  132. */
  133. function render_block_core_page_list( $attributes, $content, $block ) {
  134. static $block_id = 0;
  135. $block_id++;
  136. // TODO: When https://core.trac.wordpress.org/ticket/39037 REST API support for multiple orderby values is resolved,
  137. // update 'sort_column' to 'menu_order, post_title'. Sorting by both menu_order and post_title ensures a stable sort.
  138. // Otherwise with pages that have the same menu_order value, we can see different ordering depending on how DB
  139. // queries are constructed internally. For example we might see a different order when a limit is set to <499
  140. // versus >= 500.
  141. $all_pages = get_pages(
  142. array(
  143. 'sort_column' => 'menu_order',
  144. 'order' => 'asc',
  145. )
  146. );
  147. $top_level_pages = array();
  148. $pages_with_children = array();
  149. foreach ( (array) $all_pages as $page ) {
  150. if ( $page->post_parent ) {
  151. $pages_with_children[ $page->post_parent ][ $page->ID ] = array(
  152. 'title' => $page->post_title,
  153. 'link' => get_permalink( $page->ID ),
  154. );
  155. } else {
  156. $top_level_pages[ $page->ID ] = array(
  157. 'title' => $page->post_title,
  158. 'link' => get_permalink( $page->ID ),
  159. );
  160. }
  161. }
  162. $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );
  163. $wrapper_markup = '<ul %1$s>%2$s</ul>';
  164. $items_markup = block_core_page_list_render_nested_page_list( $nested_pages );
  165. $colors = block_core_page_list_build_css_colors( $block->context );
  166. $font_sizes = block_core_page_list_build_css_font_sizes( $block->context );
  167. $classes = array_merge(
  168. $colors['css_classes'],
  169. $font_sizes['css_classes']
  170. );
  171. $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
  172. $css_classes = trim( implode( ' ', $classes ) );
  173. if ( $block->context && $block->context['showSubmenuIcon'] ) {
  174. $css_classes .= ' show-submenu-icons';
  175. }
  176. $wrapper_attributes = get_block_wrapper_attributes(
  177. array(
  178. 'class' => $css_classes,
  179. 'style' => $style_attribute,
  180. )
  181. );
  182. return sprintf(
  183. $wrapper_markup,
  184. $wrapper_attributes,
  185. $items_markup
  186. );
  187. }
  188. /**
  189. * Registers the `core/pages` block on server.
  190. */
  191. function register_block_core_page_list() {
  192. register_block_type_from_metadata(
  193. __DIR__ . '/page-list',
  194. array(
  195. 'render_callback' => 'render_block_core_page_list',
  196. )
  197. );
  198. }
  199. add_action( 'init', 'register_block_core_page_list' );