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