[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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              $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
 103              ?>
 104  
 105              <option value=""><?php echo esc_attr( $label ); ?></option>
 106              <?php wp_get_archives( $dropdown_args ); ?>
 107  
 108          </select>
 109  
 110  <script<?php echo $type_attr; ?>>
 111  /* <![CDATA[ */
 112  (function() {
 113      var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
 114  	function onSelectChange() {
 115          if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
 116              document.location.href = this.options[ this.selectedIndex ].value;
 117          }
 118      }
 119      dropdown.onchange = onSelectChange;
 120  })();
 121  /* ]]> */
 122  </script>
 123  
 124          <?php } else { ?>
 125          <ul>
 126              <?php
 127              wp_get_archives(
 128                  /**
 129                   * Filters the arguments for the Archives widget.
 130                   *
 131                   * @since 2.8.0
 132                   * @since 4.9.0 Added the `$instance` parameter.
 133                   *
 134                   * @see wp_get_archives()
 135                   *
 136                   * @param array $args     An array of Archives option arguments.
 137                   * @param array $instance Array of settings for the current widget.
 138                   */
 139                  apply_filters(
 140                      'widget_archives_args',
 141                      array(
 142                          'type'            => 'monthly',
 143                          'show_post_count' => $c,
 144                      ),
 145                      $instance
 146                  )
 147              );
 148              ?>
 149          </ul>
 150              <?php
 151          }
 152  
 153          echo $args['after_widget'];
 154      }
 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  
 180          return $instance;
 181      }
 182  
 183      /**
 184       * Outputs the settings form for the Archives widget.
 185       *
 186       * @since 2.8.0
 187       *
 188       * @param array $instance Current settings.
 189       */
 190  	public function form( $instance ) {
 191          $instance = wp_parse_args(
 192              (array) $instance,
 193              array(
 194                  'title'    => '',
 195                  'count'    => 0,
 196                  'dropdown' => '',
 197              )
 198          );
 199          ?>
 200          <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>
 201          <p>
 202              <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>
 203              <br/>
 204              <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>
 205          </p>
 206          <?php
 207      }
 208  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7