[ 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          $title      = wp_kses( $page['title'], wp_kses_allowed_html( 'post' ) );
 177          $aria_label = sprintf(
 178              /* translators: Accessibility text. %s: Parent page title. */
 179              __( '%s submenu' ),
 180              wp_strip_all_tags( $title )
 181          );
 182  
 183          $markup .= '<li class="wp-block-pages-list__item' . $css_class . '"' . $style_attribute . '>';
 184  
 185          if ( isset( $page['children'] ) && $is_navigation_child && $open_submenus_on_click ) {
 186              $markup .= '<button aria-label="' . $aria_label . '" class="' . $navigation_child_content_class . ' wp-block-navigation-submenu__toggle" aria-expanded="false">' . $title . '<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" role="img" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg></span>' .
 187              '</button>';
 188          } else {
 189              $markup .= '<a class="wp-block-pages-list__item__link' . $navigation_child_content_class . '" href="' . esc_url( $page['link'] ) . '"' . $aria_current . '>' . $title . '</a>';
 190          }
 191  
 192          if ( isset( $page['children'] ) ) {
 193              if ( $is_navigation_child && $show_submenu_icons && ! $open_submenus_on_click ) {
 194                  $markup .= '<button aria-label="' . $aria_label . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">';
 195                  $markup .= '<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" role="img" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg></span>';
 196                  $markup .= '</button>';
 197              }
 198              $markup .= '<ul class="submenu-container';
 199              // Extra classname is added when the block is a child of Navigation.
 200              if ( $is_navigation_child ) {
 201                  $markup .= ' wp-block-navigation__submenu-container';
 202              }
 203              $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>';
 204          }
 205          $markup .= '</li>';
 206      }
 207      return $markup;
 208  }
 209  
 210  /**
 211   * Outputs nested array of pages
 212   *
 213   * @param array $current_level The level being iterated through.
 214   * @param array $children The children grouped by parent post ID.
 215   *
 216   * @return array The nested array of pages.
 217   */
 218  function block_core_page_list_nest_pages( $current_level, $children ) {
 219      if ( empty( $current_level ) ) {
 220          return;
 221      }
 222      foreach ( (array) $current_level as $key => $current ) {
 223          if ( isset( $children[ $key ] ) ) {
 224              $current_level[ $key ]['children'] = block_core_page_list_nest_pages( $children[ $key ], $children );
 225          }
 226      }
 227      return $current_level;
 228  }
 229  
 230  /**
 231   * Renders the `core/page-list` block on server.
 232   *
 233   * @param array $attributes The block attributes.
 234   * @param array $content The saved content.
 235   * @param array $block The parsed block.
 236   *
 237   * @return string Returns the page list markup.
 238   */
 239  function render_block_core_page_list( $attributes, $content, $block ) {
 240      static $block_id = 0;
 241      $block_id++;
 242  
 243      $all_pages = get_pages(
 244          array(
 245              'sort_column' => 'menu_order,post_title',
 246              'order'       => 'asc',
 247          )
 248      );
 249  
 250      // If thare are no pages, there is nothing to show.
 251      if ( empty( $all_pages ) ) {
 252          return;
 253      }
 254  
 255      $top_level_pages = array();
 256  
 257      $pages_with_children = array();
 258  
 259      $active_page_ancestor_ids = array();
 260  
 261      foreach ( (array) $all_pages as $page ) {
 262          $is_active = ! empty( $page->ID ) && ( get_the_ID() === $page->ID );
 263  
 264          if ( $is_active ) {
 265              $active_page_ancestor_ids = get_post_ancestors( $page->ID );
 266          }
 267  
 268          if ( $page->post_parent ) {
 269              $pages_with_children[ $page->post_parent ][ $page->ID ] = array(
 270                  'page_id'   => $page->ID,
 271                  'title'     => $page->post_title,
 272                  'link'      => get_permalink( $page->ID ),
 273                  'is_active' => $is_active,
 274              );
 275          } else {
 276              $top_level_pages[ $page->ID ] = array(
 277                  'page_id'   => $page->ID,
 278                  'title'     => $page->post_title,
 279                  'link'      => get_permalink( $page->ID ),
 280                  'is_active' => $is_active,
 281              );
 282  
 283          }
 284      }
 285  
 286      $colors          = block_core_page_list_build_css_colors( $attributes, $block->context );
 287      $font_sizes      = block_core_page_list_build_css_font_sizes( $block->context );
 288      $classes         = array_merge(
 289          $colors['css_classes'],
 290          $font_sizes['css_classes']
 291      );
 292      $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
 293      $css_classes     = trim( implode( ' ', $classes ) );
 294  
 295      $nested_pages = block_core_page_list_nest_pages( $top_level_pages, $pages_with_children );
 296  
 297      // Limit the number of items to be visually displayed.
 298      if ( ! empty( $attributes['__unstableMaxPages'] ) ) {
 299          $nested_pages = array_slice( $nested_pages, 0, $attributes['__unstableMaxPages'] );
 300      }
 301  
 302      $is_navigation_child = array_key_exists( 'showSubmenuIcon', $block->context );
 303  
 304      $open_submenus_on_click = array_key_exists( 'openSubmenusOnClick', $block->context ) ? $block->context['openSubmenusOnClick'] : false;
 305  
 306      $show_submenu_icons = array_key_exists( 'showSubmenuIcon', $block->context ) ? $block->context['showSubmenuIcon'] : false;
 307  
 308      $wrapper_markup = '<ul %1$s>%2$s</ul>';
 309  
 310      $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 );
 311  
 312      $wrapper_attributes = get_block_wrapper_attributes(
 313          array(
 314              'class' => $css_classes,
 315              'style' => $style_attribute,
 316          )
 317      );
 318  
 319      return sprintf(
 320          $wrapper_markup,
 321          $wrapper_attributes,
 322          $items_markup
 323      );
 324  }
 325  
 326      /**
 327       * Registers the `core/pages` block on server.
 328       */
 329  function register_block_core_page_list() {
 330      register_block_type_from_metadata(
 331          __DIR__ . '/page-list',
 332          array(
 333              'render_callback' => 'render_block_core_page_list',
 334          )
 335      );
 336  }
 337      add_action( 'init', 'register_block_core_page_list' );


Generated: Wed Jan 19 01:00:04 2022 Cross-referenced by PHPXref 0.7.1