ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

archives.php (2768B)


      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 
     20 	$class = '';
     21 
     22 	if ( ! empty( $attributes['displayAsDropdown'] ) ) {
     23 
     24 		$class .= ' wp-block-archives-dropdown';
     25 
     26 		$dropdown_id = esc_attr( uniqid( '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'            => 'monthly',
     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 		switch ( $dropdown_args['type'] ) {
     44 			case 'yearly':
     45 				$label = __( 'Select Year' );
     46 				break;
     47 			case 'monthly':
     48 				$label = __( 'Select Month' );
     49 				break;
     50 			case 'daily':
     51 				$label = __( 'Select Day' );
     52 				break;
     53 			case 'weekly':
     54 				$label = __( 'Select Week' );
     55 				break;
     56 			default:
     57 				$label = __( 'Select Post' );
     58 				break;
     59 		}
     60 
     61 		$label = esc_html( $label );
     62 
     63 		$block_content = '<label class="screen-reader-text" for="' . $dropdown_id . '">' . $title . '</label>
     64 	<select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
     65 	<option value="">' . $label . '</option>' . $archives . '</select>';
     66 
     67 		return sprintf(
     68 			'<div class="%1$s">%2$s</div>',
     69 			esc_attr( $class ),
     70 			$block_content
     71 		);
     72 	}
     73 
     74 	$class .= ' wp-block-archives-list';
     75 
     76 	/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
     77 	$archives_args = apply_filters(
     78 		'widget_archives_args',
     79 		array(
     80 			'type'            => 'monthly',
     81 			'show_post_count' => $show_post_count,
     82 		)
     83 	);
     84 
     85 	$archives_args['echo'] = 0;
     86 
     87 	$archives = wp_get_archives( $archives_args );
     88 
     89 	$classnames = esc_attr( $class );
     90 
     91 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
     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' );