9
|
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 = 'wp-block-archives'; |
|
21 |
|
|
22 |
if ( isset( $attributes['align'] ) ) { |
|
23 |
$class .= " align{$attributes['align']}"; |
|
24 |
} |
|
25 |
|
|
26 |
if ( isset( $attributes['className'] ) ) { |
|
27 |
$class .= " {$attributes['className']}"; |
|
28 |
} |
|
29 |
|
|
30 |
if ( ! empty( $attributes['displayAsDropdown'] ) ) { |
|
31 |
|
|
32 |
$class .= ' wp-block-archives-dropdown'; |
|
33 |
|
|
34 |
$dropdown_id = esc_attr( uniqid( 'wp-block-archives-' ) ); |
|
35 |
$title = __( 'Archives' ); |
|
36 |
|
|
37 |
/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ |
|
38 |
$dropdown_args = apply_filters( |
|
39 |
'widget_archives_dropdown_args', |
|
40 |
array( |
|
41 |
'type' => 'monthly', |
|
42 |
'format' => 'option', |
|
43 |
'show_post_count' => $show_post_count, |
|
44 |
) |
|
45 |
); |
|
46 |
|
|
47 |
$dropdown_args['echo'] = 0; |
|
48 |
|
|
49 |
$archives = wp_get_archives( $dropdown_args ); |
|
50 |
|
|
51 |
switch ( $dropdown_args['type'] ) { |
|
52 |
case 'yearly': |
|
53 |
$label = __( 'Select Year' ); |
|
54 |
break; |
|
55 |
case 'monthly': |
|
56 |
$label = __( 'Select Month' ); |
|
57 |
break; |
|
58 |
case 'daily': |
|
59 |
$label = __( 'Select Day' ); |
|
60 |
break; |
|
61 |
case 'weekly': |
|
62 |
$label = __( 'Select Week' ); |
|
63 |
break; |
|
64 |
default: |
|
65 |
$label = __( 'Select Post' ); |
|
66 |
break; |
|
67 |
} |
|
68 |
|
|
69 |
$label = esc_attr( $label ); |
|
70 |
|
|
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;"> |
|
73 |
<option value="">' . $label . '</option>' . $archives . '</select>'; |
|
74 |
|
16
|
75 |
return sprintf( |
9
|
76 |
'<div class="%1$s">%2$s</div>', |
|
77 |
esc_attr( $class ), |
|
78 |
$block_content |
|
79 |
); |
|
80 |
} |
|
81 |
|
16
|
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 |
); |
9
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* Register archives block. |
|
117 |
*/ |
|
118 |
function register_block_core_archives() { |
16
|
119 |
register_block_type_from_metadata( |
|
120 |
__DIR__ . '/archives', |
9
|
121 |
array( |
|
122 |
'render_callback' => 'render_block_core_archives', |
|
123 |
) |
|
124 |
); |
|
125 |
} |
|
126 |
add_action( 'init', 'register_block_core_archives' ); |