[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Thu Jul 18 08:20:01 2024 Cross-referenced by PHPXref