[ Index ]

PHP Cross Reference of WordPress

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


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1