class-wp-widget-archives.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. 'show_instance_in_rest' => true,
  28. );
  29. parent::__construct( 'archives', __( 'Archives' ), $widget_ops );
  30. }
  31. /**
  32. * Outputs the content for the current Archives widget instance.
  33. *
  34. * @since 2.8.0
  35. *
  36. * @param array $args Display arguments including 'before_title', 'after_title',
  37. * 'before_widget', and 'after_widget'.
  38. * @param array $instance Settings for the current Archives widget instance.
  39. */
  40. public function widget( $args, $instance ) {
  41. $default_title = __( 'Archives' );
  42. $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
  43. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  44. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  45. $count = ! empty( $instance['count'] ) ? '1' : '0';
  46. $dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0';
  47. echo $args['before_widget'];
  48. if ( $title ) {
  49. echo $args['before_title'] . $title . $args['after_title'];
  50. }
  51. if ( $dropdown ) {
  52. $dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
  53. ?>
  54. <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
  55. <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown">
  56. <?php
  57. /**
  58. * Filters the arguments for the Archives widget drop-down.
  59. *
  60. * @since 2.8.0
  61. * @since 4.9.0 Added the `$instance` parameter.
  62. *
  63. * @see wp_get_archives()
  64. *
  65. * @param array $args An array of Archives widget drop-down arguments.
  66. * @param array $instance Settings for the current Archives widget instance.
  67. */
  68. $dropdown_args = apply_filters(
  69. 'widget_archives_dropdown_args',
  70. array(
  71. 'type' => 'monthly',
  72. 'format' => 'option',
  73. 'show_post_count' => $count,
  74. ),
  75. $instance
  76. );
  77. switch ( $dropdown_args['type'] ) {
  78. case 'yearly':
  79. $label = __( 'Select Year' );
  80. break;
  81. case 'monthly':
  82. $label = __( 'Select Month' );
  83. break;
  84. case 'daily':
  85. $label = __( 'Select Day' );
  86. break;
  87. case 'weekly':
  88. $label = __( 'Select Week' );
  89. break;
  90. default:
  91. $label = __( 'Select Post' );
  92. break;
  93. }
  94. $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  95. ?>
  96. <option value=""><?php echo esc_html( $label ); ?></option>
  97. <?php wp_get_archives( $dropdown_args ); ?>
  98. </select>
  99. <script<?php echo $type_attr; ?>>
  100. /* <![CDATA[ */
  101. (function() {
  102. var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
  103. function onSelectChange() {
  104. if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
  105. document.location.href = this.options[ this.selectedIndex ].value;
  106. }
  107. }
  108. dropdown.onchange = onSelectChange;
  109. })();
  110. /* ]]> */
  111. </script>
  112. <?php
  113. } else {
  114. $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  115. /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
  116. $format = apply_filters( 'navigation_widgets_format', $format );
  117. if ( 'html5' === $format ) {
  118. // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
  119. $title = trim( strip_tags( $title ) );
  120. $aria_label = $title ? $title : $default_title;
  121. echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
  122. }
  123. ?>
  124. <ul>
  125. <?php
  126. wp_get_archives(
  127. /**
  128. * Filters the arguments for the Archives widget.
  129. *
  130. * @since 2.8.0
  131. * @since 4.9.0 Added the `$instance` parameter.
  132. *
  133. * @see wp_get_archives()
  134. *
  135. * @param array $args An array of Archives option arguments.
  136. * @param array $instance Array of settings for the current widget.
  137. */
  138. apply_filters(
  139. 'widget_archives_args',
  140. array(
  141. 'type' => 'monthly',
  142. 'show_post_count' => $count,
  143. ),
  144. $instance
  145. )
  146. );
  147. ?>
  148. </ul>
  149. <?php
  150. if ( 'html5' === $format ) {
  151. echo '</nav>';
  152. }
  153. }
  154. echo $args['after_widget'];
  155. }
  156. /**
  157. * Handles updating settings for the current Archives widget instance.
  158. *
  159. * @since 2.8.0
  160. *
  161. * @param array $new_instance New settings for this instance as input by the user via
  162. * WP_Widget_Archives::form().
  163. * @param array $old_instance Old settings for this instance.
  164. * @return array Updated settings to save.
  165. */
  166. public function update( $new_instance, $old_instance ) {
  167. $instance = $old_instance;
  168. $new_instance = wp_parse_args(
  169. (array) $new_instance,
  170. array(
  171. 'title' => '',
  172. 'count' => 0,
  173. 'dropdown' => '',
  174. )
  175. );
  176. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  177. $instance['count'] = $new_instance['count'] ? 1 : 0;
  178. $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
  179. return $instance;
  180. }
  181. /**
  182. * Outputs the settings form for the Archives widget.
  183. *
  184. * @since 2.8.0
  185. *
  186. * @param array $instance Current settings.
  187. */
  188. public function form( $instance ) {
  189. $instance = wp_parse_args(
  190. (array) $instance,
  191. array(
  192. 'title' => '',
  193. 'count' => 0,
  194. 'dropdown' => '',
  195. )
  196. );
  197. ?>
  198. <p>
  199. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  200. <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'] ); ?>" />
  201. </p>
  202. <p>
  203. <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' ); ?>" />
  204. <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
  205. <br />
  206. <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' ); ?>" />
  207. <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
  208. </p>
  209. <?php
  210. }
  211. }