| 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 = 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 |
$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 |
$label = esc_html( $label ); |
| 66 |
|
| 67 |
$block_content = '<label for="' . $dropdown_id . '">' . $title . '</label> |
| 68 |
<select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"> |
| 69 |
<option value="">' . $label . '</option>' . $archives . '</select>'; |
| 70 |
|
| 71 |
return sprintf( |
| 72 |
'<div %1$s>%2$s</div>', |
| 73 |
$wrapper_attributes, |
| 74 |
$block_content |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
$class .= ' wp-block-archives-list'; |
| 79 |
|
| 80 |
/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ |
| 81 |
$archives_args = apply_filters( |
| 82 |
'widget_archives_args', |
| 83 |
array( |
| 84 |
'type' => 'monthly', |
| 85 |
'show_post_count' => $show_post_count, |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
$archives_args['echo'] = 0; |
| 90 |
|
| 91 |
$archives = wp_get_archives( $archives_args ); |
| 92 |
|
| 93 |
$classnames = esc_attr( $class ); |
| 94 |
|
| 95 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); |
| 96 |
|
| 97 |
if ( empty( $archives ) ) { |
| 98 |
return sprintf( |
| 99 |
'<div %1$s>%2$s</div>', |
| 100 |
$wrapper_attributes, |
| 101 |
__( 'No archives to show.' ) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
return sprintf( |
| 106 |
'<ul %1$s>%2$s</ul>', |
| 107 |
$wrapper_attributes, |
| 108 |
$archives |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Register archives block. |
| 114 |
*/ |
| 115 |
function gutenberg_register_block_core_archives() { |
| 116 |
register_block_type_from_metadata( |
| 117 |
__DIR__ . '/archives', |
| 118 |
array( |
| 119 |
'render_callback' => 'gutenberg_render_block_core_archives', |
| 120 |
) |
| 121 |
); |
| 122 |
} |
| 123 |
add_action( 'init', 'gutenberg_register_block_core_archives', 20 ); |
| 124 |
|