You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

166 lines
5.1 KiB

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