archives.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/archives` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/archives` block on server.
  9. *
  10. * @see WP_Widget_Archives
  11. *
  12. * @param array $attributes The block attributes.
  13. *
  14. * @return string Returns the post content with archives added.
  15. */
  16. function render_block_core_archives( $attributes ) {
  17. $show_post_count = ! empty( $attributes['showPostCounts'] );
  18. $type = isset( $attributes['type'] ) ? $attributes['type'] : 'monthly';
  19. $class = '';
  20. if ( ! empty( $attributes['displayAsDropdown'] ) ) {
  21. $class .= ' wp-block-archives-dropdown';
  22. $dropdown_id = wp_unique_id( 'wp-block-archives-' );
  23. $title = __( 'Archives' );
  24. /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
  25. $dropdown_args = apply_filters(
  26. 'widget_archives_dropdown_args',
  27. array(
  28. 'type' => $type,
  29. 'format' => 'option',
  30. 'show_post_count' => $show_post_count,
  31. )
  32. );
  33. $dropdown_args['echo'] = 0;
  34. $archives = wp_get_archives( $dropdown_args );
  35. $classnames = esc_attr( $class );
  36. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
  37. switch ( $dropdown_args['type'] ) {
  38. case 'yearly':
  39. $label = __( 'Select Year' );
  40. break;
  41. case 'monthly':
  42. $label = __( 'Select Month' );
  43. break;
  44. case 'daily':
  45. $label = __( 'Select Day' );
  46. break;
  47. case 'weekly':
  48. $label = __( 'Select Week' );
  49. break;
  50. default:
  51. $label = __( 'Select Post' );
  52. break;
  53. }
  54. $show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
  55. $block_content = '<label for="' . $dropdown_id . '" class="wp-block-archives__label' . $show_label . '">' . esc_html( $title ) . '</label>
  56. <select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  57. <option value="">' . esc_html( $label ) . '</option>' . $archives . '</select>';
  58. return sprintf(
  59. '<div %1$s>%2$s</div>',
  60. $wrapper_attributes,
  61. $block_content
  62. );
  63. }
  64. $class .= ' wp-block-archives-list';
  65. /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
  66. $archives_args = apply_filters(
  67. 'widget_archives_args',
  68. array(
  69. 'type' => $type,
  70. 'show_post_count' => $show_post_count,
  71. )
  72. );
  73. $archives_args['echo'] = 0;
  74. $archives = wp_get_archives( $archives_args );
  75. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
  76. if ( empty( $archives ) ) {
  77. return sprintf(
  78. '<div %1$s>%2$s</div>',
  79. $wrapper_attributes,
  80. __( 'No archives to show.' )
  81. );
  82. }
  83. return sprintf(
  84. '<ul %1$s>%2$s</ul>',
  85. $wrapper_attributes,
  86. $archives
  87. );
  88. }
  89. /**
  90. * Register archives block.
  91. */
  92. function register_block_core_archives() {
  93. register_block_type_from_metadata(
  94. __DIR__ . '/archives',
  95. array(
  96. 'render_callback' => 'render_block_core_archives',
  97. )
  98. );
  99. }
  100. add_action( 'init', 'register_block_core_archives' );