archives.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $class = 'wp-block-archives';
  19. if ( isset( $attributes['align'] ) ) {
  20. $class .= " align{$attributes['align']}";
  21. }
  22. if ( isset( $attributes['className'] ) ) {
  23. $class .= " {$attributes['className']}";
  24. }
  25. if ( ! empty( $attributes['displayAsDropdown'] ) ) {
  26. $class .= ' wp-block-archives-dropdown';
  27. $dropdown_id = esc_attr( uniqid( 'wp-block-archives-' ) );
  28. $title = __( 'Archives' );
  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. $dropdown_args['echo'] = 0;
  39. $archives = wp_get_archives( $dropdown_args );
  40. switch ( $dropdown_args['type'] ) {
  41. case 'yearly':
  42. $label = __( 'Select Year' );
  43. break;
  44. case 'monthly':
  45. $label = __( 'Select Month' );
  46. break;
  47. case 'daily':
  48. $label = __( 'Select Day' );
  49. break;
  50. case 'weekly':
  51. $label = __( 'Select Week' );
  52. break;
  53. default:
  54. $label = __( 'Select Post' );
  55. break;
  56. }
  57. $label = esc_attr( $label );
  58. $block_content = '<label class="screen-reader-text" for="' . $dropdown_id . '">' . $title . '</label>
  59. <select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  60. <option value="">' . $label . '</option>' . $archives . '</select>';
  61. return sprintf(
  62. '<div class="%1$s">%2$s</div>',
  63. esc_attr( $class ),
  64. $block_content
  65. );
  66. }
  67. $class .= ' wp-block-archives-list';
  68. /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
  69. $archives_args = apply_filters(
  70. 'widget_archives_args',
  71. array(
  72. 'type' => 'monthly',
  73. 'show_post_count' => $show_post_count,
  74. )
  75. );
  76. $archives_args['echo'] = 0;
  77. $archives = wp_get_archives( $archives_args );
  78. $classnames = esc_attr( $class );
  79. if ( empty( $archives ) ) {
  80. return sprintf(
  81. '<div class="%1$s">%2$s</div>',
  82. $classnames,
  83. __( 'No archives to show.' )
  84. );
  85. }
  86. return sprintf(
  87. '<ul class="%1$s">%2$s</ul>',
  88. $classnames,
  89. $archives
  90. );
  91. }
  92. /**
  93. * Register archives block.
  94. */
  95. function register_block_core_archives() {
  96. register_block_type(
  97. 'core/archives',
  98. array(
  99. 'attributes' => array(
  100. 'align' => array(
  101. 'type' => 'string',
  102. 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
  103. ),
  104. 'className' => array(
  105. 'type' => 'string',
  106. ),
  107. 'displayAsDropdown' => array(
  108. 'type' => 'boolean',
  109. 'default' => false,
  110. ),
  111. 'showPostCounts' => array(
  112. 'type' => 'boolean',
  113. 'default' => false,
  114. ),
  115. ),
  116. 'render_callback' => 'render_block_core_archives',
  117. )
  118. );
  119. }
  120. add_action( 'init', 'register_block_core_archives' );