[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Widget API: WP_Widget_Categories class
   4   *
   5   * @package WordPress
   6   * @subpackage Widgets
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core class used to implement a Categories widget.
  12   *
  13   * @since 2.8.0
  14   *
  15   * @see WP_Widget
  16   */
  17  class WP_Widget_Categories extends WP_Widget {
  18  
  19      /**
  20       * Sets up a new Categories widget instance.
  21       *
  22       * @since 2.8.0
  23       */
  24  	public function __construct() {
  25          $widget_ops = array(
  26              'classname'                   => 'widget_categories',
  27              'description'                 => __( 'A list or dropdown of categories.' ),
  28              'customize_selective_refresh' => true,
  29              'show_instance_in_rest'       => true,
  30          );
  31          parent::__construct( 'categories', __( 'Categories' ), $widget_ops );
  32      }
  33  
  34      /**
  35       * Outputs the content for the current Categories widget instance.
  36       *
  37       * @since 2.8.0
  38       * @since 4.2.0 Creates a unique HTML ID for the `<select>` element
  39       *              if more than one instance is displayed on the page.
  40       *
  41       * @param array $args     Display arguments including 'before_title', 'after_title',
  42       *                        'before_widget', and 'after_widget'.
  43       * @param array $instance Settings for the current Categories widget instance.
  44       */
  45  	public function widget( $args, $instance ) {
  46          static $first_dropdown = true;
  47  
  48          $default_title = __( 'Categories' );
  49          $title         = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
  50  
  51          /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  52          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  53  
  54          $count        = ! empty( $instance['count'] ) ? '1' : '0';
  55          $hierarchical = ! empty( $instance['hierarchical'] ) ? '1' : '0';
  56          $dropdown     = ! empty( $instance['dropdown'] ) ? '1' : '0';
  57  
  58          echo $args['before_widget'];
  59  
  60          if ( $title ) {
  61              echo $args['before_title'] . $title . $args['after_title'];
  62          }
  63  
  64          $cat_args = array(
  65              'orderby'      => 'name',
  66              'show_count'   => $count,
  67              'hierarchical' => $hierarchical,
  68          );
  69  
  70          if ( $dropdown ) {
  71              printf( '<form action="%s" method="get">', esc_url( home_url() ) );
  72              $dropdown_id    = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
  73              $first_dropdown = false;
  74  
  75              echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
  76  
  77              $cat_args['show_option_none'] = __( 'Select Category' );
  78              $cat_args['id']               = $dropdown_id;
  79  
  80              /**
  81               * Filters the arguments for the Categories widget drop-down.
  82               *
  83               * @since 2.8.0
  84               * @since 4.9.0 Added the `$instance` parameter.
  85               *
  86               * @see wp_dropdown_categories()
  87               *
  88               * @param array $cat_args An array of Categories widget drop-down arguments.
  89               * @param array $instance Array of settings for the current widget.
  90               */
  91              wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) );
  92  
  93              echo '</form>';
  94  
  95              ob_start();
  96              ?>
  97  
  98  <script>
  99  ( ( dropdownId ) => {
 100      const dropdown = document.getElementById( dropdownId );
 101  	function onSelectChange() {
 102          setTimeout( () => {
 103              if ( 'escape' === dropdown.dataset.lastkey ) {
 104                  return;
 105              }
 106              if ( dropdown.value && parseInt( dropdown.value ) > 0 && dropdown instanceof HTMLSelectElement ) {
 107                  dropdown.parentElement.submit();
 108              }
 109          }, 250 );
 110      }
 111  	function onKeyUp( event ) {
 112          if ( 'Escape' === event.key ) {
 113              dropdown.dataset.lastkey = 'escape';
 114          } else {
 115              delete dropdown.dataset.lastkey;
 116          }
 117      }
 118  	function onClick() {
 119          delete dropdown.dataset.lastkey;
 120      }
 121      dropdown.addEventListener( 'keyup', onKeyUp );
 122      dropdown.addEventListener( 'click', onClick );
 123      dropdown.addEventListener( 'change', onSelectChange );
 124  })( <?php echo wp_json_encode( $dropdown_id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
 125  </script>
 126  
 127              <?php
 128              wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) . "\n//# sourceURL=" . rawurlencode( __METHOD__ ) );
 129          } else {
 130              $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
 131  
 132              /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
 133              $format = apply_filters( 'navigation_widgets_format', $format );
 134  
 135              if ( 'html5' === $format ) {
 136                  // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
 137                  $title      = trim( strip_tags( $title ) );
 138                  $aria_label = $title ? $title : $default_title;
 139                  echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
 140              }
 141              ?>
 142  
 143              <ul>
 144                  <?php
 145                  $cat_args['title_li'] = '';
 146  
 147                  /**
 148                   * Filters the arguments for the Categories widget.
 149                   *
 150                   * @since 2.8.0
 151                   * @since 4.9.0 Added the `$instance` parameter.
 152                   *
 153                   * @param array $cat_args An array of Categories widget options.
 154                   * @param array $instance Array of settings for the current widget.
 155                   */
 156                  wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
 157                  ?>
 158              </ul>
 159  
 160              <?php
 161              if ( 'html5' === $format ) {
 162                  echo '</nav>';
 163              }
 164          }
 165  
 166          echo $args['after_widget'];
 167      }
 168  
 169      /**
 170       * Handles updating settings for the current Categories widget instance.
 171       *
 172       * @since 2.8.0
 173       *
 174       * @param array $new_instance New settings for this instance as input by the user via
 175       *                            WP_Widget::form().
 176       * @param array $old_instance Old settings for this instance.
 177       * @return array Updated settings to save.
 178       */
 179  	public function update( $new_instance, $old_instance ) {
 180          $instance                 = $old_instance;
 181          $instance['title']        = sanitize_text_field( $new_instance['title'] );
 182          $instance['count']        = ! empty( $new_instance['count'] ) ? 1 : 0;
 183          $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
 184          $instance['dropdown']     = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
 185  
 186          return $instance;
 187      }
 188  
 189      /**
 190       * Outputs the settings form for the Categories widget.
 191       *
 192       * @since 2.8.0
 193       *
 194       * @param array $instance Current settings.
 195       */
 196  	public function form( $instance ) {
 197          // Defaults.
 198          $instance     = wp_parse_args( (array) $instance, array( 'title' => '' ) );
 199          $count        = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
 200          $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
 201          $dropdown     = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
 202          ?>
 203          <p>
 204              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 205              <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'] ); ?>" />
 206          </p>
 207  
 208          <p>
 209              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
 210              <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
 211              <br />
 212  
 213              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
 214              <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
 215              <br />
 216  
 217              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
 218              <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label>
 219          </p>
 220          <?php
 221      }
 222  }


Generated : Thu Nov 6 08:20:07 2025 Cross-referenced by PHPXref