wp/wp-includes/blocks/archives.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    70 
    70 
    71 		$block_content = '<label class="screen-reader-text" for="' . $dropdown_id . '">' . $title . '</label>
    71 		$block_content = '<label class="screen-reader-text" for="' . $dropdown_id . '">' . $title . '</label>
    72 	<select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
    72 	<select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
    73 	<option value="">' . $label . '</option>' . $archives . '</select>';
    73 	<option value="">' . $label . '</option>' . $archives . '</select>';
    74 
    74 
    75 		$block_content = sprintf(
    75 		return sprintf(
    76 			'<div class="%1$s">%2$s</div>',
    76 			'<div class="%1$s">%2$s</div>',
    77 			esc_attr( $class ),
    77 			esc_attr( $class ),
    78 			$block_content
    78 			$block_content
    79 		);
    79 		);
    80 	} else {
       
    81 
       
    82 		$class .= ' wp-block-archives-list';
       
    83 
       
    84 		/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
       
    85 		$archives_args = apply_filters(
       
    86 			'widget_archives_args',
       
    87 			array(
       
    88 				'type'            => 'monthly',
       
    89 				'show_post_count' => $show_post_count,
       
    90 			)
       
    91 		);
       
    92 
       
    93 		$archives_args['echo'] = 0;
       
    94 
       
    95 		$archives = wp_get_archives( $archives_args );
       
    96 
       
    97 		$classnames = esc_attr( $class );
       
    98 
       
    99 		if ( empty( $archives ) ) {
       
   100 
       
   101 			$block_content = sprintf(
       
   102 				'<div class="%1$s">%2$s</div>',
       
   103 				$classnames,
       
   104 				__( 'No archives to show.' )
       
   105 			);
       
   106 		} else {
       
   107 
       
   108 			$block_content = sprintf(
       
   109 				'<ul class="%1$s">%2$s</ul>',
       
   110 				$classnames,
       
   111 				$archives
       
   112 			);
       
   113 		}
       
   114 	}
    80 	}
   115 
    81 
   116 	return $block_content;
    82 	$class .= ' wp-block-archives-list';
       
    83 
       
    84 	/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
       
    85 	$archives_args = apply_filters(
       
    86 		'widget_archives_args',
       
    87 		array(
       
    88 			'type'            => 'monthly',
       
    89 			'show_post_count' => $show_post_count,
       
    90 		)
       
    91 	);
       
    92 
       
    93 	$archives_args['echo'] = 0;
       
    94 
       
    95 	$archives = wp_get_archives( $archives_args );
       
    96 
       
    97 	$classnames = esc_attr( $class );
       
    98 
       
    99 	if ( empty( $archives ) ) {
       
   100 
       
   101 		return sprintf(
       
   102 			'<div class="%1$s">%2$s</div>',
       
   103 			$classnames,
       
   104 			__( 'No archives to show.' )
       
   105 		);
       
   106 	}
       
   107 
       
   108 	return sprintf(
       
   109 		'<ul class="%1$s">%2$s</ul>',
       
   110 		$classnames,
       
   111 		$archives
       
   112 	);
   117 }
   113 }
   118 
   114 
   119 /**
   115 /**
   120  * Register archives block.
   116  * Register archives block.
   121  */
   117  */
   122 function register_block_core_archives() {
   118 function register_block_core_archives() {
   123 	register_block_type(
   119 	register_block_type_from_metadata(
   124 		'core/archives',
   120 		__DIR__ . '/archives',
   125 		array(
   121 		array(
   126 			'attributes'      => array(
       
   127 				'align'             => array(
       
   128 					'type' => 'string',
       
   129 				),
       
   130 				'className'         => array(
       
   131 					'type' => 'string',
       
   132 				),
       
   133 				'displayAsDropdown' => array(
       
   134 					'type'    => 'boolean',
       
   135 					'default' => false,
       
   136 				),
       
   137 				'showPostCounts'    => array(
       
   138 					'type'    => 'boolean',
       
   139 					'default' => false,
       
   140 				),
       
   141 			),
       
   142 			'render_callback' => 'render_block_core_archives',
   122 			'render_callback' => 'render_block_core_archives',
   143 		)
   123 		)
   144 	);
   124 	);
   145 }
   125 }
   146 
       
   147 add_action( 'init', 'register_block_core_archives' );
   126 add_action( 'init', 'register_block_core_archives' );