14 * |
14 * |
15 * @return string Returns the post content with archives added. |
15 * @return string Returns the post content with archives added. |
16 */ |
16 */ |
17 function render_block_core_archives( $attributes ) { |
17 function render_block_core_archives( $attributes ) { |
18 $show_post_count = ! empty( $attributes['showPostCounts'] ); |
18 $show_post_count = ! empty( $attributes['showPostCounts'] ); |
19 |
19 $type = isset( $attributes['type'] ) ? $attributes['type'] : 'monthly'; |
20 $class = ''; |
20 $class = ''; |
21 |
21 |
22 if ( ! empty( $attributes['displayAsDropdown'] ) ) { |
22 if ( ! empty( $attributes['displayAsDropdown'] ) ) { |
23 |
23 |
24 $class .= ' wp-block-archives-dropdown'; |
24 $class .= ' wp-block-archives-dropdown'; |
25 |
25 |
26 $dropdown_id = esc_attr( uniqid( 'wp-block-archives-' ) ); |
26 $dropdown_id = wp_unique_id( 'wp-block-archives-' ); |
27 $title = __( 'Archives' ); |
27 $title = __( 'Archives' ); |
28 |
28 |
29 /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ |
29 /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ |
30 $dropdown_args = apply_filters( |
30 $dropdown_args = apply_filters( |
31 'widget_archives_dropdown_args', |
31 'widget_archives_dropdown_args', |
32 array( |
32 array( |
33 'type' => 'monthly', |
33 'type' => $type, |
34 'format' => 'option', |
34 'format' => 'option', |
35 'show_post_count' => $show_post_count, |
35 'show_post_count' => $show_post_count, |
36 ) |
36 ) |
37 ); |
37 ); |
38 |
38 |
39 $dropdown_args['echo'] = 0; |
39 $dropdown_args['echo'] = 0; |
40 |
40 |
41 $archives = wp_get_archives( $dropdown_args ); |
41 $archives = wp_get_archives( $dropdown_args ); |
|
42 |
|
43 $classnames = esc_attr( $class ); |
|
44 |
|
45 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); |
42 |
46 |
43 switch ( $dropdown_args['type'] ) { |
47 switch ( $dropdown_args['type'] ) { |
44 case 'yearly': |
48 case 'yearly': |
45 $label = __( 'Select Year' ); |
49 $label = __( 'Select Year' ); |
46 break; |
50 break; |
56 default: |
60 default: |
57 $label = __( 'Select Post' ); |
61 $label = __( 'Select Post' ); |
58 break; |
62 break; |
59 } |
63 } |
60 |
64 |
61 $label = esc_html( $label ); |
65 $block_content = '<label for="' . esc_attr( $dropdown_id ) . '">' . esc_html( $title ) . '</label> |
62 |
66 <select id="' . esc_attr( $dropdown_id ) . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"> |
63 $block_content = '<label class="screen-reader-text" for="' . $dropdown_id . '">' . $title . '</label> |
67 <option value="">' . esc_html( $label ) . '</option>' . $archives . '</select>'; |
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 |
68 |
67 return sprintf( |
69 return sprintf( |
68 '<div class="%1$s">%2$s</div>', |
70 '<div %1$s>%2$s</div>', |
69 esc_attr( $class ), |
71 $wrapper_attributes, |
70 $block_content |
72 $block_content |
71 ); |
73 ); |
72 } |
74 } |
73 |
75 |
74 $class .= ' wp-block-archives-list'; |
76 $class .= ' wp-block-archives-list'; |
75 |
77 |
76 /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ |
78 /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ |
77 $archives_args = apply_filters( |
79 $archives_args = apply_filters( |
78 'widget_archives_args', |
80 'widget_archives_args', |
79 array( |
81 array( |
80 'type' => 'monthly', |
82 'type' => $type, |
81 'show_post_count' => $show_post_count, |
83 'show_post_count' => $show_post_count, |
82 ) |
84 ) |
83 ); |
85 ); |
84 |
86 |
85 $archives_args['echo'] = 0; |
87 $archives_args['echo'] = 0; |
86 |
88 |
87 $archives = wp_get_archives( $archives_args ); |
89 $archives = wp_get_archives( $archives_args ); |
88 |
90 |
89 $classnames = esc_attr( $class ); |
91 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) ); |
90 |
|
91 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); |
|
92 |
92 |
93 if ( empty( $archives ) ) { |
93 if ( empty( $archives ) ) { |
94 return sprintf( |
94 return sprintf( |
95 '<div %1$s>%2$s</div>', |
95 '<div %1$s>%2$s</div>', |
96 $wrapper_attributes, |
96 $wrapper_attributes, |