WP_Widget_Archives

Core class used to implement the Archives widget.

Defined (1)

The class is defined in the following location(s).

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