[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> archives.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/archives` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/archives` block on server.
  10   *
  11   * @see WP_Widget_Archives
  12   *
  13   * @param array $attributes The block attributes.
  14   *
  15   * @return string Returns the post content with archives added.
  16   */
  17  function render_block_core_archives( $attributes ) {
  18      $show_post_count = ! empty( $attributes['showPostCounts'] );
  19      $type            = isset( $attributes['type'] ) ? $attributes['type'] : 'monthly';
  20  
  21      $class = 'wp-block-archives-list';
  22  
  23      if ( ! empty( $attributes['displayAsDropdown'] ) ) {
  24  
  25          $class = 'wp-block-archives-dropdown';
  26  
  27          $dropdown_id = wp_unique_id( 'wp-block-archives-' );
  28          $title       = __( 'Archives' );
  29  
  30          /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
  31          $dropdown_args = apply_filters(
  32              'widget_archives_dropdown_args',
  33              array(
  34                  'type'            => $type,
  35                  'format'          => 'option',
  36                  'show_post_count' => $show_post_count,
  37              )
  38          );
  39  
  40          $dropdown_args['echo'] = 0;
  41  
  42          $archives = wp_get_archives( $dropdown_args );
  43  
  44          $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
  45  
  46          switch ( $dropdown_args['type'] ) {
  47              case 'yearly':
  48                  $label = __( 'Select Year' );
  49                  break;
  50              case 'monthly':
  51                  $label = __( 'Select Month' );
  52                  break;
  53              case 'daily':
  54                  $label = __( 'Select Day' );
  55                  break;
  56              case 'weekly':
  57                  $label = __( 'Select Week' );
  58                  break;
  59              default:
  60                  $label = __( 'Select Post' );
  61                  break;
  62          }
  63  
  64          $show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
  65  
  66          $block_content = '<label for="' . $dropdown_id . '" class="wp-block-archives__label' . $show_label . '">' . esc_html( $title ) . '</label>
  67          <select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  68          <option value="">' . esc_html( $label ) . '</option>' . $archives . '</select>';
  69  
  70          return sprintf(
  71              '<div %1$s>%2$s</div>',
  72              $wrapper_attributes,
  73              $block_content
  74          );
  75      }
  76  
  77      /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
  78      $archives_args = apply_filters(
  79          'widget_archives_args',
  80          array(
  81              'type'            => $type,
  82              'show_post_count' => $show_post_count,
  83          )
  84      );
  85  
  86      $archives_args['echo'] = 0;
  87  
  88      $archives = wp_get_archives( $archives_args );
  89  
  90      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
  91  
  92      if ( empty( $archives ) ) {
  93          return sprintf(
  94              '<div %1$s>%2$s</div>',
  95              $wrapper_attributes,
  96              __( 'No archives to show.' )
  97          );
  98      }
  99  
 100      return sprintf(
 101          '<ul %1$s>%2$s</ul>',
 102          $wrapper_attributes,
 103          $archives
 104      );
 105  }
 106  
 107  /**
 108   * Register archives block.
 109   */
 110  function register_block_core_archives() {
 111      register_block_type_from_metadata(
 112          __DIR__ . '/archives',
 113          array(
 114              'render_callback' => 'render_block_core_archives',
 115          )
 116      );
 117  }
 118  add_action( 'init', 'register_block_core_archives' );


Generated : Thu Mar 28 08:20:01 2024 Cross-referenced by PHPXref