Keine Beschreibung

archives.php 2.7KB

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