[ 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       *
  38       * @staticvar bool $first_dropdown
  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          $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories' );
  48  
  49          /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  50          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  51  
  52          $c = ! empty( $instance['count'] ) ? '1' : '0';
  53          $h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
  54          $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
  55  
  56          echo $args['before_widget'];
  57  
  58          if ( $title ) {
  59              echo $args['before_title'] . $title . $args['after_title'];
  60          }
  61  
  62          $cat_args = array(
  63              'orderby'      => 'name',
  64              'show_count'   => $c,
  65              'hierarchical' => $h,
  66          );
  67  
  68          if ( $d ) {
  69              echo sprintf( '<form action="%s" method="get">', esc_url( home_url() ) );
  70              $dropdown_id    = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
  71              $first_dropdown = false;
  72  
  73              echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
  74  
  75              $cat_args['show_option_none'] = __( 'Select Category' );
  76              $cat_args['id']               = $dropdown_id;
  77  
  78              /**
  79               * Filters the arguments for the Categories widget drop-down.
  80               *
  81               * @since 2.8.0
  82               * @since 4.9.0 Added the `$instance` parameter.
  83               *
  84               * @see wp_dropdown_categories()
  85               *
  86               * @param array $cat_args An array of Categories widget drop-down arguments.
  87               * @param array $instance Array of settings for the current widget.
  88               */
  89              wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) );
  90  
  91              echo '</form>';
  92  
  93              $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  94              ?>
  95  
  96  <script<?php echo $type_attr; ?>>
  97  /* <![CDATA[ */
  98  (function() {
  99      var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
 100  	function onCatChange() {
 101          if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
 102              dropdown.parentNode.submit();
 103          }
 104      }
 105      dropdown.onchange = onCatChange;
 106  })();
 107  /* ]]> */
 108  </script>
 109  
 110              <?php
 111          } else {
 112              ?>
 113          <ul>
 114              <?php
 115              $cat_args['title_li'] = '';
 116  
 117              /**
 118               * Filters the arguments for the Categories widget.
 119               *
 120               * @since 2.8.0
 121               * @since 4.9.0 Added the `$instance` parameter.
 122               *
 123               * @param array $cat_args An array of Categories widget options.
 124               * @param array $instance Array of settings for the current widget.
 125               */
 126              wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
 127              ?>
 128          </ul>
 129              <?php
 130          }
 131  
 132          echo $args['after_widget'];
 133      }
 134  
 135      /**
 136       * Handles updating settings for the current Categories widget instance.
 137       *
 138       * @since 2.8.0
 139       *
 140       * @param array $new_instance New settings for this instance as input by the user via
 141       *                            WP_Widget::form().
 142       * @param array $old_instance Old settings for this instance.
 143       * @return array Updated settings to save.
 144       */
 145  	public function update( $new_instance, $old_instance ) {
 146          $instance                 = $old_instance;
 147          $instance['title']        = sanitize_text_field( $new_instance['title'] );
 148          $instance['count']        = ! empty( $new_instance['count'] ) ? 1 : 0;
 149          $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
 150          $instance['dropdown']     = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
 151  
 152          return $instance;
 153      }
 154  
 155      /**
 156       * Outputs the settings form for the Categories widget.
 157       *
 158       * @since 2.8.0
 159       *
 160       * @param array $instance Current settings.
 161       */
 162  	public function form( $instance ) {
 163          //Defaults
 164          $instance     = wp_parse_args( (array) $instance, array( 'title' => '' ) );
 165          $count        = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
 166          $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
 167          $dropdown     = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
 168          ?>
 169          <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 170          <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'] ); ?>" /></p>
 171  
 172          <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
 173          <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
 174  
 175          <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
 176          <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label><br />
 177  
 178          <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
 179          <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
 180          <?php
 181      }
 182  
 183  }


Generated: Sun Dec 8 01:00:03 2019 Cross-referenced by PHPXref 0.7.1