[ 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       * @staticvar bool $first_dropdown
  41       *
  42       * @param array $args     Display arguments including 'before_title', 'after_title',
  43       *                        'before_widget', and 'after_widget'.
  44       * @param array $instance Settings for the current Categories widget instance.
  45       */
  46  	public function widget( $args, $instance ) {
  47          static $first_dropdown = true;
  48  
  49          $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories' );
  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              echo sprintf( '<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              ?>
 115          <ul>
 116              <?php
 117              $cat_args['title_li'] = '';
 118  
 119              /**
 120               * Filters the arguments for the Categories widget.
 121               *
 122               * @since 2.8.0
 123               * @since 4.9.0 Added the `$instance` parameter.
 124               *
 125               * @param array $cat_args An array of Categories widget options.
 126               * @param array $instance Array of settings for the current widget.
 127               */
 128              wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
 129              ?>
 130          </ul>
 131              <?php
 132          }
 133  
 134          echo $args['after_widget'];
 135      }
 136  
 137      /**
 138       * Handles updating settings for the current Categories widget instance.
 139       *
 140       * @since 2.8.0
 141       *
 142       * @param array $new_instance New settings for this instance as input by the user via
 143       *                            WP_Widget::form().
 144       * @param array $old_instance Old settings for this instance.
 145       * @return array Updated settings to save.
 146       */
 147  	public function update( $new_instance, $old_instance ) {
 148          $instance                 = $old_instance;
 149          $instance['title']        = sanitize_text_field( $new_instance['title'] );
 150          $instance['count']        = ! empty( $new_instance['count'] ) ? 1 : 0;
 151          $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
 152          $instance['dropdown']     = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
 153  
 154          return $instance;
 155      }
 156  
 157      /**
 158       * Outputs the settings form for the Categories widget.
 159       *
 160       * @since 2.8.0
 161       *
 162       * @param array $instance Current settings.
 163       */
 164  	public function form( $instance ) {
 165          // Defaults.
 166          $instance     = wp_parse_args( (array) $instance, array( 'title' => '' ) );
 167          $count        = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
 168          $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
 169          $dropdown     = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
 170          ?>
 171          <p>
 172              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 173              <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'] ); ?>" />
 174          </p>
 175  
 176          <p>
 177              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
 178              <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
 179              <br />
 180  
 181              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
 182              <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
 183              <br />
 184  
 185              <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
 186              <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label>
 187          </p>
 188          <?php
 189      }
 190  
 191  }


Generated: Tue May 26 01:00:04 2020 Cross-referenced by PHPXref 0.7.1