PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / archives.php

archives.php in Gutenberg 12.6.0, at build/block-library/blocks/archives.php

120 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gutenberg_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 = 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 $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' => 'monthly',
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 gutenberg_register_block_core_archives() {
112 register_block_type_from_metadata(
113 __DIR__ . '/archives',
114 array(
115 'render_callback' => 'gutenberg_render_block_core_archives',
116 )
117 );
118 }
119 add_action( 'init', 'gutenberg_register_block_core_archives', 20 );
120