class-wp-widget-archives.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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">
  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(
  67. 'widget_archives_dropdown_args',
  68. array(
  69. 'type' => 'monthly',
  70. 'format' => 'option',
  71. 'show_post_count' => $c,
  72. ),
  73. $instance
  74. );
  75. switch ( $dropdown_args['type'] ) {
  76. case 'yearly':
  77. $label = __( 'Select Year' );
  78. break;
  79. case 'monthly':
  80. $label = __( 'Select Month' );
  81. break;
  82. case 'daily':
  83. $label = __( 'Select Day' );
  84. break;
  85. case 'weekly':
  86. $label = __( 'Select Week' );
  87. break;
  88. default:
  89. $label = __( 'Select Post' );
  90. break;
  91. }
  92. $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  93. ?>
  94. <option value=""><?php echo esc_attr( $label ); ?></option>
  95. <?php wp_get_archives( $dropdown_args ); ?>
  96. </select>
  97. <script<?php echo $type_attr; ?>>
  98. /* <![CDATA[ */
  99. (function() {
  100. var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
  101. function onSelectChange() {
  102. if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
  103. document.location.href = this.options[ this.selectedIndex ].value;
  104. }
  105. }
  106. dropdown.onchange = onSelectChange;
  107. })();
  108. /* ]]> */
  109. </script>
  110. <?php } else { ?>
  111. <ul>
  112. <?php
  113. /**
  114. * Filters the arguments for the Archives widget.
  115. *
  116. * @since 2.8.0
  117. * @since 4.9.0 Added the `$instance` parameter.
  118. *
  119. * @see wp_get_archives()
  120. *
  121. * @param array $args An array of Archives option arguments.
  122. * @param array $instance Array of settings for the current widget.
  123. */
  124. wp_get_archives(
  125. apply_filters(
  126. 'widget_archives_args',
  127. array(
  128. 'type' => 'monthly',
  129. 'show_post_count' => $c,
  130. ),
  131. $instance
  132. )
  133. );
  134. ?>
  135. </ul>
  136. <?php
  137. }
  138. echo $args['after_widget'];
  139. }
  140. /**
  141. * Handles updating settings for the current Archives widget instance.
  142. *
  143. * @since 2.8.0
  144. *
  145. * @param array $new_instance New settings for this instance as input by the user via
  146. * WP_Widget_Archives::form().
  147. * @param array $old_instance Old settings for this instance.
  148. * @return array Updated settings to save.
  149. */
  150. public function update( $new_instance, $old_instance ) {
  151. $instance = $old_instance;
  152. $new_instance = wp_parse_args(
  153. (array) $new_instance,
  154. array(
  155. 'title' => '',
  156. 'count' => 0,
  157. 'dropdown' => '',
  158. )
  159. );
  160. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  161. $instance['count'] = $new_instance['count'] ? 1 : 0;
  162. $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
  163. return $instance;
  164. }
  165. /**
  166. * Outputs the settings form for the Archives widget.
  167. *
  168. * @since 2.8.0
  169. *
  170. * @param array $instance Current settings.
  171. */
  172. public function form( $instance ) {
  173. $instance = wp_parse_args(
  174. (array) $instance,
  175. array(
  176. 'title' => '',
  177. 'count' => 0,
  178. 'dropdown' => '',
  179. )
  180. );
  181. ?>
  182. <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( $instance['title'] ); ?>" /></p>
  183. <p>
  184. <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>
  185. <br/>
  186. <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>
  187. </p>
  188. <?php
  189. }
  190. }