class-wp-widget-archives.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Archives class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement the Archives widget.
  11. *
  12. * @since 2.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Archives extends WP_Widget {
  17. /**
  18. * Sets up a new Archives widget instance.
  19. *
  20. * @since 2.8.0
  21. */
  22. public function __construct() {
  23. $widget_ops = array(
  24. 'classname' => 'widget_archive',
  25. 'description' => __( 'A monthly archive of your site&#8217;s Posts.' ),
  26. 'customize_selective_refresh' => true,
  27. );
  28. parent::__construct('archives', __('Archives'), $widget_ops);
  29. }
  30. /**
  31. * Outputs the content for the current Archives widget instance.
  32. *
  33. * @since 2.8.0
  34. *
  35. * @param array $args Display arguments including 'before_title', 'after_title',
  36. * 'before_widget', and 'after_widget'.
  37. * @param array $instance Settings for the current Archives widget instance.
  38. */
  39. public function widget( $args, $instance ) {
  40. $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Archives' );
  41. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  42. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  43. $c = ! empty( $instance['count'] ) ? '1' : '0';
  44. $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
  45. echo $args['before_widget'];
  46. if ( $title ) {
  47. echo $args['before_title'] . $title . $args['after_title'];
  48. }
  49. if ( $d ) {
  50. $dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
  51. ?>
  52. <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
  53. <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
  54. <?php
  55. /**
  56. * Filters the arguments for the Archives widget drop-down.
  57. *
  58. * @since 2.8.0
  59. * @since 4.9.0 Added the `$instance` parameter.
  60. *
  61. * @see wp_get_archives()
  62. *
  63. * @param array $args An array of Archives widget drop-down arguments.
  64. * @param array $instance Settings for the current Archives widget instance.
  65. */
  66. $dropdown_args = apply_filters( 'widget_archives_dropdown_args', array(
  67. 'type' => 'monthly',
  68. 'format' => 'option',
  69. 'show_post_count' => $c
  70. ), $instance );
  71. switch ( $dropdown_args['type'] ) {
  72. case 'yearly':
  73. $label = __( 'Select Year' );
  74. break;
  75. case 'monthly':
  76. $label = __( 'Select Month' );
  77. break;
  78. case 'daily':
  79. $label = __( 'Select Day' );
  80. break;
  81. case 'weekly':
  82. $label = __( 'Select Week' );
  83. break;
  84. default:
  85. $label = __( 'Select Post' );
  86. break;
  87. }
  88. ?>
  89. <option value=""><?php echo esc_attr( $label ); ?></option>
  90. <?php wp_get_archives( $dropdown_args ); ?>
  91. </select>
  92. <?php } else { ?>
  93. <ul>
  94. <?php
  95. /**
  96. * Filters the arguments for the Archives widget.
  97. *
  98. * @since 2.8.0
  99. * @since 4.9.0 Added the `$instance` parameter.
  100. *
  101. * @see wp_get_archives()
  102. *
  103. * @param array $args An array of Archives option arguments.
  104. * @param array $instance Array of settings for the current widget.
  105. */
  106. wp_get_archives( apply_filters( 'widget_archives_args', array(
  107. 'type' => 'monthly',
  108. 'show_post_count' => $c
  109. ), $instance ) );
  110. ?>
  111. </ul>
  112. <?php
  113. }
  114. echo $args['after_widget'];
  115. }
  116. /**
  117. * Handles updating settings for the current Archives widget instance.
  118. *
  119. * @since 2.8.0
  120. *
  121. * @param array $new_instance New settings for this instance as input by the user via
  122. * WP_Widget_Archives::form().
  123. * @param array $old_instance Old settings for this instance.
  124. * @return array Updated settings to save.
  125. */
  126. public function update( $new_instance, $old_instance ) {
  127. $instance = $old_instance;
  128. $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
  129. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  130. $instance['count'] = $new_instance['count'] ? 1 : 0;
  131. $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
  132. return $instance;
  133. }
  134. /**
  135. * Outputs the settings form for the Archives widget.
  136. *
  137. * @since 2.8.0
  138. *
  139. * @param array $instance Current settings.
  140. */
  141. public function form( $instance ) {
  142. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
  143. $title = sanitize_text_field( $instance['title'] );
  144. ?>
  145. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
  146. <p>
  147. <input class="checkbox" type="checkbox"<?php checked( $instance['dropdown'] ); ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label>
  148. <br/>
  149. <input class="checkbox" type="checkbox"<?php checked( $instance['count'] ); ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
  150. </p>
  151. <?php
  152. }
  153. }