[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/widgets/ -> class-wp-widget-archives.php (source)

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


Generated: Sun Sep 15 01:00:03 2019 Cross-referenced by PHPXref 0.7.1