[ 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          );
  30          parent::__construct( 'categories', __( 'Categories' ), $widget_ops );
  31      }
  32  
  33      /**
  34       * Outputs the content for the current Categories widget instance.
  35       *
  36       * @since 2.8.0
  37       * @since 4.2.0 Creates a unique HTML ID for the `<select>` element
  38       *              if more than one instance is displayed on the page.
  39       *
  40       * @param array $args     Display arguments including 'before_title', 'after_title',
  41       *                        'before_widget', and 'after_widget'.
  42       * @param array $instance Settings for the current Categories widget instance.
  43       */
  44  	public function widget( $args, $instance ) {
  45          static $first_dropdown = true;
  46  
  47          $default_title = __( 'Categories' );
  48          $title         = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
  49  
  50          /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  51          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  52  
  53          $count        = ! empty( $instance['count'] ) ? '1' : '0';
  54          $hierarchical = ! empty( $instance['hierarchical'] ) ? '1' : '0';
  55          $dropdown     = ! empty( $instance['dropdown'] ) ? '1' : '0';
  56  
  57          echo $args['before_widget'];
  58  
  59          if ( $title ) {
  60              echo $args['before_title'] . $title . $args['after_title'];
  61          }
  62  
  63          $cat_args = array(
  64              'orderby'      => 'name',
  65              'show_count'   => $count,
  66              'hierarchical' => $hierarchical,
  67          );
  68  
  69          if ( $dropdown ) {
  70              printf( '<form action="%s" method="get">', esc_url( home_url() ) );
  71              $dropdown_id    = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
  72              $first_dropdown = false;
  73  
  74              echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
  75  
  76              $cat_args['show_option_none'] = __( 'Select Category' );
  77              $cat_args['id']               = $dropdown_id;
  78  
  79              /**
  80               * Filters the arguments for the Categories widget drop-down.
  81               *
  82               * @since 2.8.0
  83               * @since 4.9.0 Added the `$instance` parameter.
  84               *
  85               * @see wp_dropdown_categories()
  86               *
  87               * @param array $cat_args An array of Categories widget drop-down arguments.
  88               * @param array $instance Array of settings for the current widget.
  89               */
  90              wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) );
  91  
  92              echo '</form>';
  93  
  94              $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  95              ?>
  96  
  97  <script<?php echo $type_attr; ?>>
  98  /* <![CDATA[ */
  99  (function() {
 100      var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
 101  	function onCatChange() {
 102          if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
 103              dropdown.parentNode.submit();
 104          }
 105      }
 106      dropdown.onchange = onCatChange;
 107  })();
 108  /* ]]> */
 109  </script>
 110  
 111              <?php
 112          } else {
 113              $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
 114  
 115              /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
 116              $format = apply_filters( 'navigation_widgets_format', $format );
 117  
 118              if ( 'html5' === $format ) {
 119                  // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
 120                  $title      = trim( strip_tags( $title ) );
 121                  $aria_label = $title ? $title : $default_title;
 122                  echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
 123              }
 124              ?>
 125  
 126              <ul>
 127                  <?php
 128                  $cat_args['title_li'] = '';
 129  
 130                  /**
 131                   * Filters the arguments for the Categories widget.
 132                   *
 133                   * @since 2.8.0
 134                   * @since 4.9.0 Added the `$instance` parameter.
 135                   *
 136                   * @param array $cat_args An array of Categories widget options.
 137                   * @param array $instance Array of settings for the current widget.
 138                   */
 139                  wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
 140                  ?>
 141              </ul>
 142  
 143              <?php
 144              if ( 'html5' === $format ) {
 145                  echo '</nav>';
 146              }
 147          }
 148  
 149          echo $args['after_widget'];
 150      }
 151  
 152      /**
 153       * Handles updating settings for the current Categories widget instance.
 154       *
 155       * @since 2.8.0
 156       *
 157       * @param array $new_instance New settings for this instance as input by the user via
 158       *                            WP_Widget::form().
 159       * @param array $old_instance Old settings for this instance.
 160       * @return array Updated settings to save.
 161       */
 162  	public function update( $new_instance, $old_instance ) {
 163          $instance                 = $old_instance;
 164          $instance['title']        = sanitize_text_field( $new_instance['title'] );
 165          $instance['count']        = ! empty( $new_instance['count'] ) ? 1 : 0;
 166          $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
 167          $instance['dropdown']     = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
 168  
 169          return $instance;
 170      }
 171  
 172      /**
 173       * Outputs the settings form for the Categories widget.
 174       *
 175       * @since 2.8.0
 176       *
 177       * @param array $instance Current settings.
 178       */
 179  	public function form( $instance ) {
 180          // Defaults.
 181          $instance     = wp_parse_args( (array) $instance, array( 'title' => '' ) );
 182          $count        = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
 183          $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
 184          $dropdown     = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
 185          ?>
 186          <p>
 187              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 188              <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'] ); ?>" />
 189          </p>
 190  
 191          <p>
 192              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
 193              <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
 194              <br />
 195  
 196              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
 197              <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
 198              <br />
 199  
 200              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
 201              <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label>
 202          </p>
 203          <?php
 204      }
 205  
 206  }


Generated: Mon Aug 10 01:00:03 2020 Cross-referenced by PHPXref 0.7.1