[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> page-list.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/pages` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Returns the submenu visibility value with backward compatibility
  10   * for the deprecated openSubmenusOnClick attribute.
  11   *
  12   * @since 6.9.0
  13   *
  14   * @param array $context Block context from parent Navigation block.
  15   * @return string The visibility mode: 'hover', 'click', or 'always'.
  16   */
  17  function block_core_page_list_get_submenu_visibility( $context ) {
  18      $deprecated_open_submenus_on_click = $context['openSubmenusOnClick'] ?? null;
  19  
  20      // For backward compatibility, prioritize the legacy attribute if present. If it has been loaded and saved in the editor, then
  21      // the deprecated attribute will be replaced by submenuVisibility.
  22      if ( null !== $deprecated_open_submenus_on_click ) {
  23          // Convert boolean to string: true -> 'click', false -> 'hover'.
  24          return ! empty( $deprecated_open_submenus_on_click ) ? 'click' : 'hover';
  25      }
  26  
  27      $submenu_visibility = $context['submenuVisibility'] ?? null;
  28  
  29      // Use submenuVisibility for migrated/new blocks.
  30      return $submenu_visibility ?? 'hover';
  31  }
  32  
  33  /**
  34   * Build an array with CSS classes and inline styles defining the colors
  35   * which will be applied to the pages markup in the front-end when it is a descendant of navigation.
  36   *
  37   * @since 5.8.0
  38   *
  39   * @param  array $attributes Block attributes.
  40   * @param  array $context    Navigation block context.
  41   * @return array Colors CSS classes and inline styles.
  42   */
  43  function block_core_page_list_build_css_colors( $attributes, $context ) {
  44      $colors = array(
  45          'css_classes'           => array(),
  46          'inline_styles'         => '',
  47          'overlay_css_classes'   => array(),
  48          'overlay_inline_styles' => '',
  49      );
  50  
  51      // Text color.
  52      $has_named_text_color  = array_key_exists( 'textColor', $context );
  53      $has_picked_text_color = array_key_exists( 'customTextColor', $context );
  54      $has_custom_text_color = isset( $context['style']['color']['text'] );
  55  
  56      // If has text color.
  57      if ( $has_custom_text_color || $has_picked_text_color || $has_named_text_color ) {
  58          // Add has-text-color class.
  59          $colors['css_classes'][] = 'has-text-color';
  60      }
  61  
  62      if ( $has_named_text_color ) {
  63          // Add the color class.
  64          $colors['css_classes'][] = sprintf( 'has-%s-color', _wp_to_kebab_case( $context['textColor'] ) );
  65      } elseif ( $has_picked_text_color ) {
  66          $colors['inline_styles'] .= sprintf( 'color: %s;', $context['customTextColor'] );
  67      } elseif ( $has_custom_text_color ) {
  68          // Add the custom color inline style.
  69          $colors['inline_styles'] .= sprintf( 'color: %s;', $context['style']['color']['text'] );
  70      }
  71  
  72      // Background color.
  73      $has_named_background_color  = array_key_exists( 'backgroundColor', $context );
  74      $has_picked_background_color = array_key_exists( 'customBackgroundColor', $context );
  75      $has_custom_background_color = isset( $context['style']['color']['background'] );
  76  
  77      // If has background color.
  78      if ( $has_custom_background_color || $has_picked_background_color || $has_named_background_color ) {
  79          // Add has-background class.
  80          $colors['css_classes'][] = 'has-background';
  81      }
  82  
  83      if ( $has_named_background_color ) {
  84          // Add the background-color class.
  85          $colors['css_classes'][] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $context['backgroundColor'] ) );
  86      } elseif ( $has_picked_background_color ) {
  87          $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['customBackgroundColor'] );
  88      } elseif ( $has_custom_background_color ) {
  89          // Add the custom background-color inline style.
  90          $colors['inline_styles'] .= sprintf( 'background-color: %s;', $context['style']['color']['background'] );
  91      }
  92  
  93      // Overlay text color.
  94      $has_named_overlay_text_color  = array_key_exists( 'overlayTextColor', $context );
  95      $has_picked_overlay_text_color = array_key_exists( 'customOverlayTextColor', $context );
  96  
  97      // If it has a text color.
  98      if ( $has_named_overlay_text_color || $has_picked_overlay_text_color ) {
  99          $colors['overlay_css_classes'][] = 'has-text-color';
 100      }
 101  
 102      // Give overlay colors priority, fall back to Navigation block colors, then global styles.
 103      if ( $has_named_overlay_text_color ) {
 104          $colors['overlay_css_classes'][] = sprintf( 'has-%s-color', _wp_to_kebab_case( $context['overlayTextColor'] ) );
 105      } elseif ( $has_picked_overlay_text_color ) {
 106          $colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $context['customOverlayTextColor'] );
 107      }
 108  
 109      // Overlay background colors.
 110      $has_named_overlay_background_color  = array_key_exists( 'overlayBackgroundColor', $context );
 111      $has_picked_overlay_background_color = array_key_exists( 'customOverlayBackgroundColor', $context );
 112  
 113      // If has background color.
 114      if ( $has_named_overlay_background_color || $has_picked_overlay_background_color ) {
 115          $colors['overlay_css_classes'][] = 'has-background';
 116      }
 117  
 118      if ( $has_named_overlay_background_color ) {
 119          $colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $context['overlayBackgroundColor'] ) );
 120      } elseif ( $has_picked_overlay_background_color ) {
 121          $colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $context['customOverlayBackgroundColor'] );
 122      }
 123  
 124      return $colors;
 125  }
 126  
 127  /**
 128   * Build an array with CSS classes and inline styles defining the font sizes
 129   * which will be applied to the pages markup in the front-end when it is a descendant of navigation.
 130   *
 131   * @since 5.8.0
 132   *
 133   * @param  array $context Navigation block context.
 134   * @return array Font size CSS classes and inline styles.
 135   */
 136  function block_core_page_list_build_css_font_sizes( $context ) {
 137      // CSS classes.
 138      $font_sizes = array(
 139          'css_classes'   => array(),
 140          'inline_styles' => '',
 141      );
 142  
 143      $has_named_font_size  = array_key_exists( 'fontSize', $context );
 144      $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
 145  
 146      if ( $has_named_font_size ) {
 147          // Add the font size class.
 148          $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
 149      } elseif ( $has_custom_font_size ) {
 150          // Add the custom font size inline style.
 151          $font_sizes['inline_styles'] = sprintf(
 152              'font-size: %s;',
 153              wp_get_typography_font_size_value(
 154                  array(
 155                      'size' => $context['style']['typography']['fontSize'],
 156                  )
 157              )
 158          );
 159      }
 160  
 161      return $font_sizes;
 162  }
 163  
 164  /**
 165   * Outputs Page list markup from an array of pages with nested children.
 166   *
 167   * @since 5.8.0
 168   *
 169   * @param boolean $open_submenus_on_click Whether to open submenus on click instead of hover.
 170   * @param boolean $show_submenu_icons Whether to show submenu indicator icons.
 171   * @param boolean $is_navigation_child If block is a child of Navigation block.
 172   * @param array   $nested_pages The array of nested pages.
 173   * @param boolean $is_nested Whether the submenu is nested or not.
 174   * @param array   $active_page_ancestor_ids An array of ancestor ids for active page.
 175   * @param array   $colors Color information for overlay styles.
 176   * @param integer $depth The nesting depth.
 177   *
 178   * @return string List markup.
 179   */
 180  function block_core_page_list_render_nested_page_list( $submenu_visibility, $show_submenu_icons, $is_navigation_child, $nested_pages, $is_nested, $active_page_ancestor_ids = array(), $colors = array(), $depth = 0 ) {
 181      if ( empty( $nested_pages ) ) {
 182          return;
 183      }
 184      $front_page_id = (int) get_option( 'page_on_front' );
 185      $markup        = '';
 186  
 187      // Compute visibility mode flags once
 188      $open_on_click = 'click' === $submenu_visibility;
 189      $open_on_hover = 'hover' === $submenu_visibility;
 190      $open_always   = 'always' === $submenu_visibility;
 191  
 192      foreach ( (array) $nested_pages as $page ) {
 193          $css_class       = $page['is_active'] ? ' current-menu-item' : '';
 194          $aria_current    = $page['is_active'] ? ' aria-current="page"' : '';
 195          $style_attribute = '';
 196  
 197          $css_class .= in_array( $page['page_id'], $active_page_ancestor_ids, true ) ? ' current-menu-ancestor' : '';
 198          if ( isset( $page['children'] ) ) {
 199              $css_class .= ' has-child';
 200          }
 201  
 202          if ( $is_navigation_child ) {
 203              $css_class .= ' wp-block-navigation-item';
 204  
 205              // Class assignment logic matches JS editor rendering in page-list-item/edit.js
 206              // Note: elseif ensures open-on-hover-click is mutually exclusive with open-on-click
 207              if ( $open_on_click ) {
 208                  $css_class .= ' open-on-click';
 209              } elseif ( $open_on_hover && $show_submenu_icons ) {
 210                  $css_class .= ' open-on-hover-click';
 211              } elseif ( $open_always ) {
 212                  $css_class .= ' open-always';
 213              }
 214          }
 215  
 216          $navigation_child_content_class = $is_navigation_child ? ' wp-block-navigation-item__content' : '';
 217  
 218          // If this is the first level of submenus, include the overlay colors.
 219          if ( ( ( 0 < $depth && ! $is_nested ) || $is_nested ) && isset( $colors['overlay_css_classes'], $colors['overlay_inline_styles'] ) ) {
 220              $css_class .= ' ' . trim( implode( ' ', $colors['overlay_css_classes'] ) );
 221              if ( '' !== $colors['overlay_inline_styles'] ) {
 222                  $style_attribute = sprintf( ' style="%s"', esc_attr( $colors['overlay_inline_styles'] ) );
 223              }
 224          }
 225  
 226          if ( (int) $page['page_id'] === $front_page_id ) {
 227              $css_class .= ' menu-item-home';
 228          }
 229  
 230          $title = $page['title'] ? $page['title'] : __( '(no title)' );
 231  
 232          $aria_label = sprintf(
 233              /* translators: Accessibility text. %s: Parent page title. */
 234              __( '%s submenu' ),
 235              wp_strip_all_tags( $title )
 236          );
 237  
 238          $markup .= '<li class="wp-block-pages-list__item' . esc_attr( $css_class ) . '"' . $style_attribute . '>';
 239  
 240          if ( isset( $page['children'] ) && $is_navigation_child && $open_on_click ) {
 241              $markup .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="' . esc_attr( $navigation_child_content_class ) . ' wp-block-navigation-submenu__toggle" aria-expanded="false">' . wp_kses_post( $title ) .
 242              '</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>';
 243          } else {
 244              $markup .= '<a class="wp-block-pages-list__item__link' . esc_attr( $navigation_child_content_class ) . '" href="' . esc_url( $page['link'] ) . '"' . $aria_current . '>' . wp_kses_post( $title ) . '</a>';
 245          }
 246  
 247          if ( isset( $page['children'] ) ) {
 248              if ( $is_navigation_child && $show_submenu_icons && ! $open_on_click ) {
 249                  $markup .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">';
 250                  $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>';
 251                  $markup .= '</button>';
 252              }
 253              $markup .= '<ul class="wp-block-navigation__submenu-container">';
 254              $markup .= block_core_page_list_render_nested_page_list( $submenu_visibility, $show_submenu_icons, $is_navigation_child, $page['children'], $is_nested, $active_page_ancestor_ids, $colors, $depth + 1 );
 255              $markup .= '</ul>';
 256          }
 257          $markup .= '</li>';
 258      }
 259      return $markup;
 260  }
 261  
 262  /**
 263   * Outputs nested array of pages
 264   *
 265   * @since 5.8.0
 266   *
 267   * @param array $current_level The level being iterated through.
 268   * @param array $children The children grouped by parent post ID.
 269   *
 270   * @return array The nested array of pages.
 271   */
 272  function block_core_page_list_nest_pages( $current_level, $children ) {
 273      if ( empty( $current_level ) ) {
 274          return;
 275      }
 276      foreach ( (array) $current_level as $key => $current ) {
 277          if ( isset( $children[ $key ] ) ) {
 278              $current_level[ $key ]['children'] = block_core_page_list_nest_pages( $children[ $key ], $children );
 279          }
 280      }
 281      return $current_level;
 282  }
 283  
 284  /**
 285   * Renders the `core/page-list` block on server.
 286   *
 287   * @since 5.8.0
 288   *
 289   * @param array    $attributes The block attributes.
 290   * @param string   $content    The saved content.
 291   * @param WP_Block $block      The parsed block.
 292   *
 293   * @return string Returns the page list markup.
 294   */
 295  function render_block_core_page_list( $attributes, $content, $block ) {
 296      static $block_id = 0;
 297      ++$block_id;
 298  
 299      $parent_page_id = $attributes['parentPageID'];
 300      $is_nested      = $attributes['isNested'];
 301  
 302      $all_pages = get_pages(
 303          array(
 304              'sort_column' => 'menu_order,post_title',
 305              'order'       => 'asc',
 306          )
 307      );
 308  
 309      // If there are no pages, there is nothing to show.
 310      if ( empty( $all_pages ) ) {
 311          return;
 312      }
 313  
 314      $top_level_pages = array();
 315  
 316      $pages_with_children = array();
 317  
 318      $active_page_ancestor_ids = array();
 319  
 320      foreach ( (array) $all_pages as $page ) {
 321          $is_active = ! empty( $page->ID ) && ( get_queried_object_id() === $page->ID );
 322  
 323          if ( $is_active ) {
 324              $active_page_ancestor_ids = get_post_ancestors( $page->ID );
 325          }
 326  
 327          if ( $page->post_parent ) {
 328              $pages_with_children[ $page->post_parent ][ $page->ID ] = array(
 329                  'page_id'   => $page->ID,
 330                  'title'     => $page->post_title,
 331                  'link'      => get_permalink( $page ),
 332                  'is_active' => $is_active,
 333              );
 334          } else {
 335              $top_level_pages[ $page->ID ] = array(
 336                  'page_id'   => $page->ID,
 337                  'title'     => $page->post_title,
 338                  'link'      => get_permalink( $page ),
 339                  'is_active' => $is_active,
 340              );
 341  
 342          }
 343      }
 344  
 345      $colors          = block_core_page_list_build_css_colors( $attributes, $block->context );
 346      $font_sizes      = block_core_page_list_build_css_font_sizes( $block->context );
 347      $classes         = array_merge(
 348          $colors['css_classes'],
 349          $font_sizes['css_classes']
 350      );
 351      $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
 352      $css_classes     = trim( implode( ' ', $classes ) );
 353  
 354      $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );
 355  
 356      if ( 0 !== $parent_page_id ) {
 357          // If the parent page has no child pages, there is nothing to show.
 358          if ( ! array_key_exists( $parent_page_id, $pages_with_children ) ) {
 359              return;
 360          }
 361  
 362          $nested_pages = block_core_page_list_nest_pages(
 363              $pages_with_children[ $parent_page_id ],
 364              $pages_with_children
 365          );
 366      }
 367  
 368      $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context );
 369  
 370      // Get submenu visibility with backward compatibility for openSubmenusOnClick.
 371      $submenu_visibility = $is_navigation_child ? block_core_page_list_get_submenu_visibility( $block->context ) : 'hover';
 372  
 373      $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false;
 374  
 375      $wrapper_markup = $is_nested ? '%2$s' : '<ul %1$s>%2$s</ul>';
 376  
 377      $items_markup = block_core_page_list_render_nested_page_list( $submenu_visibility, $show_submenu_icons, $is_navigation_child, $nested_pages, $is_nested, $active_page_ancestor_ids, $colors );
 378  
 379      $wrapper_attributes = get_block_wrapper_attributes(
 380          array(
 381              'class' => $css_classes,
 382              'style' => $style_attribute,
 383          )
 384      );
 385  
 386      return sprintf(
 387          $wrapper_markup,
 388          $wrapper_attributes,
 389          $items_markup
 390      );
 391  }
 392  
 393  /**
 394   * Registers the `core/pages` block on server.
 395   *
 396   * @since 5.8.0
 397   */
 398  function register_block_core_page_list() {
 399      register_block_type_from_metadata(
 400          __DIR__ . '/page-list',
 401          array(
 402              'render_callback' => 'render_block_core_page_list',
 403          )
 404      );
 405  }
 406  add_action( 'init', 'register_block_core_page_list' );


Generated : Sat Jun 20 08:20:11 2026 Cross-referenced by PHPXref