[ Index ]

PHP Cross Reference of WordPress

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      $class           = '';
  21  
  22      if ( ! empty( $attributes['displayAsDropdown'] ) ) {
  23  
  24          $class .= ' wp-block-archives-dropdown';
  25  
  26          $dropdown_id = wp_unique_id( 'wp-block-archives-' );
  27          $title       = __( 'Archives' );
  28  
  29          /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
  30          $dropdown_args = apply_filters(
  31              'widget_archives_dropdown_args',
  32              array(
  33                  'type'            => $type,
  34                  'format'          => 'option',
  35                  'show_post_count' => $show_post_count,
  36              )
  37          );
  38  
  39          $dropdown_args['echo'] = 0;
  40  
  41          $archives = wp_get_archives( $dropdown_args );
  42  
  43          $classnames = esc_attr( $class );
  44  
  45          $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  46  
  47          switch ( $dropdown_args['type'] ) {
  48              case 'yearly':
  49                  $label = __( 'Select Year' );
  50                  break;
  51              case 'monthly':
  52                  $label = __( 'Select Month' );
  53                  break;
  54              case 'daily':
  55                  $label = __( 'Select Day' );
  56                  break;
  57              case 'weekly':
  58                  $label = __( 'Select Week' );
  59                  break;
  60              default:
  61                  $label = __( 'Select Post' );
  62                  break;
  63          }
  64  
  65          $block_content = '<label for="' . esc_attr( $dropdown_id ) . '">' . esc_html( $title ) . '</label>
  66      <select id="' . esc_attr( $dropdown_id ) . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  67      <option value="">' . esc_html( $label ) . '</option>' . $archives . '</select>';
  68  
  69          return sprintf(
  70              '<div %1$s>%2$s</div>',
  71              $wrapper_attributes,
  72              $block_content
  73          );
  74      }
  75  
  76      $class .= ' wp-block-archives-list';
  77  
  78      /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
  79      $archives_args = apply_filters(
  80          'widget_archives_args',
  81          array(
  82              'type'            => $type,
  83              'show_post_count' => $show_post_count,
  84          )
  85      );
  86  
  87      $archives_args['echo'] = 0;
  88  
  89      $archives = wp_get_archives( $archives_args );
  90  
  91      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
  92  
  93      if ( empty( $archives ) ) {
  94          return sprintf(
  95              '<div %1$s>%2$s</div>',
  96              $wrapper_attributes,
  97              __( 'No archives to show.' )
  98          );
  99      }
 100  
 101      return sprintf(
 102          '<ul %1$s>%2$s</ul>',
 103          $wrapper_attributes,
 104          $archives
 105      );
 106  }
 107  
 108  /**
 109   * Register archives block.
 110   */
 111  function register_block_core_archives() {
 112      register_block_type_from_metadata(
 113          __DIR__ . '/archives',
 114          array(
 115              'render_callback' => 'render_block_core_archives',
 116          )
 117      );
 118  }
 119  add_action( 'init', 'register_block_core_archives' );


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1