[ 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          $default_title = __( 'Archives' );
  44          $title         = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
  45  
  46          /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  47          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  48  
  49          $count    = ! empty( $instance['count'] ) ? '1' : '0';
  50          $dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0';
  51  
  52          echo $args['before_widget'];
  53  
  54          if ( $title ) {
  55              echo $args['before_title'] . $title . $args['after_title'];
  56          }
  57  
  58          if ( $dropdown ) {
  59              $dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
  60              ?>
  61          <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
  62          <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown">
  63              <?php
  64              /**
  65               * Filters the arguments for the Archives widget drop-down.
  66               *
  67               * @since 2.8.0
  68               * @since 4.9.0 Added the `$instance` parameter.
  69               *
  70               * @see wp_get_archives()
  71               *
  72               * @param array $args     An array of Archives widget drop-down arguments.
  73               * @param array $instance Settings for the current Archives widget instance.
  74               */
  75              $dropdown_args = apply_filters(
  76                  'widget_archives_dropdown_args',
  77                  array(
  78                      'type'            => 'monthly',
  79                      'format'          => 'option',
  80                      'show_post_count' => $count,
  81                  ),
  82                  $instance
  83              );
  84  
  85              switch ( $dropdown_args['type'] ) {
  86                  case 'yearly':
  87                      $label = __( 'Select Year' );
  88                      break;
  89                  case 'monthly':
  90                      $label = __( 'Select Month' );
  91                      break;
  92                  case 'daily':
  93                      $label = __( 'Select Day' );
  94                      break;
  95                  case 'weekly':
  96                      $label = __( 'Select Week' );
  97                      break;
  98                  default:
  99                      $label = __( 'Select Post' );
 100                      break;
 101              }
 102  
 103              $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
 104              ?>
 105  
 106              <option value=""><?php echo esc_html( $label ); ?></option>
 107              <?php wp_get_archives( $dropdown_args ); ?>
 108  
 109          </select>
 110  
 111  <script<?php echo $type_attr; ?>>
 112  /* <![CDATA[ */
 113  (function() {
 114      var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
 115  	function onSelectChange() {
 116          if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
 117              document.location.href = this.options[ this.selectedIndex ].value;
 118          }
 119      }
 120      dropdown.onchange = onSelectChange;
 121  })();
 122  /* ]]> */
 123  </script>
 124              <?php
 125          } else {
 126              $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
 127  
 128              /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
 129              $format = apply_filters( 'navigation_widgets_format', $format );
 130  
 131              if ( 'html5' === $format ) {
 132                  // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
 133                  $title      = trim( strip_tags( $title ) );
 134                  $aria_label = $title ? $title : $default_title;
 135                  echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
 136              }
 137              ?>
 138  
 139              <ul>
 140                  <?php
 141                  wp_get_archives(
 142                      /**
 143                       * Filters the arguments for the Archives widget.
 144                       *
 145                       * @since 2.8.0
 146                       * @since 4.9.0 Added the `$instance` parameter.
 147                       *
 148                       * @see wp_get_archives()
 149                       *
 150                       * @param array $args     An array of Archives option arguments.
 151                       * @param array $instance Array of settings for the current widget.
 152                       */
 153                      apply_filters(
 154                          'widget_archives_args',
 155                          array(
 156                              'type'            => 'monthly',
 157                              'show_post_count' => $count,
 158                          ),
 159                          $instance
 160                      )
 161                  );
 162                  ?>
 163              </ul>
 164  
 165              <?php
 166              if ( 'html5' === $format ) {
 167                  echo '</nav>';
 168              }
 169          }
 170  
 171          echo $args['after_widget'];
 172      }
 173  
 174      /**
 175       * Handles updating settings for the current Archives widget instance.
 176       *
 177       * @since 2.8.0
 178       *
 179       * @param array $new_instance New settings for this instance as input by the user via
 180       *                            WP_Widget_Archives::form().
 181       * @param array $old_instance Old settings for this instance.
 182       * @return array Updated settings to save.
 183       */
 184  	public function update( $new_instance, $old_instance ) {
 185          $instance             = $old_instance;
 186          $new_instance         = wp_parse_args(
 187              (array) $new_instance,
 188              array(
 189                  'title'    => '',
 190                  'count'    => 0,
 191                  'dropdown' => '',
 192              )
 193          );
 194          $instance['title']    = sanitize_text_field( $new_instance['title'] );
 195          $instance['count']    = $new_instance['count'] ? 1 : 0;
 196          $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
 197  
 198          return $instance;
 199      }
 200  
 201      /**
 202       * Outputs the settings form for the Archives widget.
 203       *
 204       * @since 2.8.0
 205       *
 206       * @param array $instance Current settings.
 207       */
 208  	public function form( $instance ) {
 209          $instance = wp_parse_args(
 210              (array) $instance,
 211              array(
 212                  'title'    => '',
 213                  'count'    => 0,
 214                  'dropdown' => '',
 215              )
 216          );
 217          ?>
 218          <p>
 219              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 220              <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'] ); ?>" />
 221          </p>
 222          <p>
 223              <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' ); ?>" />
 224              <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
 225              <br/>
 226              <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' ); ?>" />
 227              <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
 228          </p>
 229          <?php
 230      }
 231  }


Generated: Sat Aug 8 01:00:03 2020 Cross-referenced by PHPXref 0.7.1