[ Index ]

PHP Cross Reference of WordPress

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              $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  96              ?>
  97  
  98  <script<?php echo $type_attr; ?>>
  99  /* <![CDATA[ */
 100  (function() {
 101      var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
 102  	function onCatChange() {
 103          if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
 104              dropdown.parentNode.submit();
 105          }
 106      }
 107      dropdown.onchange = onCatChange;
 108  })();
 109  /* ]]> */
 110  </script>
 111  
 112              <?php
 113          } else {
 114              $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
 115  
 116              /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
 117              $format = apply_filters( 'navigation_widgets_format', $format );
 118  
 119              if ( 'html5' === $format ) {
 120                  // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
 121                  $title      = trim( strip_tags( $title ) );
 122                  $aria_label = $title ? $title : $default_title;
 123                  echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
 124              }
 125              ?>
 126  
 127              <ul>
 128                  <?php
 129                  $cat_args['title_li'] = '';
 130  
 131                  /**
 132                   * Filters the arguments for the Categories widget.
 133                   *
 134                   * @since 2.8.0
 135                   * @since 4.9.0 Added the `$instance` parameter.
 136                   *
 137                   * @param array $cat_args An array of Categories widget options.
 138                   * @param array $instance Array of settings for the current widget.
 139                   */
 140                  wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
 141                  ?>
 142              </ul>
 143  
 144              <?php
 145              if ( 'html5' === $format ) {
 146                  echo '</nav>';
 147              }
 148          }
 149  
 150          echo $args['after_widget'];
 151      }
 152  
 153      /**
 154       * Handles updating settings for the current Categories widget instance.
 155       *
 156       * @since 2.8.0
 157       *
 158       * @param array $new_instance New settings for this instance as input by the user via
 159       *                            WP_Widget::form().
 160       * @param array $old_instance Old settings for this instance.
 161       * @return array Updated settings to save.
 162       */
 163  	public function update( $new_instance, $old_instance ) {
 164          $instance                 = $old_instance;
 165          $instance['title']        = sanitize_text_field( $new_instance['title'] );
 166          $instance['count']        = ! empty( $new_instance['count'] ) ? 1 : 0;
 167          $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
 168          $instance['dropdown']     = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
 169  
 170          return $instance;
 171      }
 172  
 173      /**
 174       * Outputs the settings form for the Categories widget.
 175       *
 176       * @since 2.8.0
 177       *
 178       * @param array $instance Current settings.
 179       */
 180  	public function form( $instance ) {
 181          // Defaults.
 182          $instance     = wp_parse_args( (array) $instance, array( 'title' => '' ) );
 183          $count        = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
 184          $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
 185          $dropdown     = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
 186          ?>
 187          <p>
 188              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 189              <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'] ); ?>" />
 190          </p>
 191  
 192          <p>
 193              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
 194              <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
 195              <br />
 196  
 197              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
 198              <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
 199              <br />
 200  
 201              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
 202              <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label>
 203          </p>
 204          <?php
 205      }
 206  
 207  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1