暂无描述

archives.php 2.8KB

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. $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. $block_content = '<label for="' . esc_attr( $dropdown_id ) . '">' . esc_html( $title ) . '</label>
  55. <select id="' . esc_attr( $dropdown_id ) . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  56. <option value="">' . esc_html( $label ) . '</option>' . $archives . '</select>';
  57. return sprintf(
  58. '<div %1$s>%2$s</div>',
  59. $wrapper_attributes,
  60. $block_content
  61. );
  62. }
  63. $class .= ' wp-block-archives-list';
  64. /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */
  65. $archives_args = apply_filters(
  66. 'widget_archives_args',
  67. array(
  68. 'type' => $type,
  69. 'show_post_count' => $show_post_count,
  70. )
  71. );
  72. $archives_args['echo'] = 0;
  73. $archives = wp_get_archives( $archives_args );
  74. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
  75. if ( empty( $archives ) ) {
  76. return sprintf(
  77. '<div %1$s>%2$s</div>',
  78. $wrapper_attributes,
  79. __( 'No archives to show.' )
  80. );
  81. }
  82. return sprintf(
  83. '<ul %1$s>%2$s</ul>',
  84. $wrapper_attributes,
  85. $archives
  86. );
  87. }
  88. /**
  89. * Register archives block.
  90. */
  91. function register_block_core_archives() {
  92. register_block_type_from_metadata(
  93. __DIR__ . '/archives',
  94. array(
  95. 'render_callback' => 'render_block_core_archives',
  96. )
  97. );
  98. }
  99. add_action( 'init', 'register_block_core_archives' );