/wp-includes/widgets/class-wp-widget-categories.php

  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. * @access public 
  24. */ 
  25. public function __construct() { 
  26. $widget_ops = array( 
  27. 'classname' => 'widget_categories',  
  28. 'description' => __( 'A list or dropdown of categories.' ),  
  29. 'customize_selective_refresh' => true,  
  30. ); 
  31. parent::__construct( 'categories', __( 'Categories' ), $widget_ops ); 
  32.  
  33. /** 
  34. * Outputs the content for the current Categories widget instance. 
  35. * 
  36. * @since 2.8.0 
  37. * @access public 
  38. * 
  39. * @param array $args Display arguments including 'before_title', 'after_title',  
  40. * 'before_widget', and 'after_widget'. 
  41. * @param array $instance Settings for the current Categories widget instance. 
  42. */ 
  43. public function widget( $args, $instance ) { 
  44. static $first_dropdown = true; 
  45.  
  46. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  47. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base ); 
  48.  
  49. $c = ! empty( $instance['count'] ) ? '1' : '0'; 
  50. $h = ! empty( $instance['hierarchical'] ) ? '1' : '0'; 
  51. $d = ! empty( $instance['dropdown'] ) ? '1' : '0'; 
  52.  
  53. echo $args['before_widget']; 
  54. if ( $title ) { 
  55. echo $args['before_title'] . $title . $args['after_title']; 
  56.  
  57. $cat_args = array( 
  58. 'orderby' => 'name',  
  59. 'show_count' => $c,  
  60. 'hierarchical' => $h 
  61. ); 
  62.  
  63. if ( $d ) { 
  64. $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}"; 
  65. $first_dropdown = false; 
  66.  
  67. echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>'; 
  68.  
  69. $cat_args['show_option_none'] = __( 'Select Category' ); 
  70. $cat_args['id'] = $dropdown_id; 
  71.  
  72. /** 
  73. * Filters the arguments for the Categories widget drop-down. 
  74. * 
  75. * @since 2.8.0 
  76. * 
  77. * @see wp_dropdown_categories() 
  78. * 
  79. * @param array $cat_args An array of Categories widget drop-down arguments. 
  80. */ 
  81. wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args ) ); 
  82. ?> 
  83.  
  84. <script type='text/javascript'> 
  85. /* <![CDATA[ */ 
  86. (function() { 
  87. var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" ); 
  88. function onCatChange() { 
  89. if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) { 
  90. location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value; 
  91. dropdown.onchange = onCatChange; 
  92. })(); 
  93. /* ]]> */ 
  94. </script> 
  95.  
  96. <?php 
  97. } else { 
  98. ?> 
  99. <ul> 
  100. <?php 
  101. $cat_args['title_li'] = ''; 
  102.  
  103. /** 
  104. * Filters the arguments for the Categories widget. 
  105. * 
  106. * @since 2.8.0 
  107. * 
  108. * @param array $cat_args An array of Categories widget options. 
  109. */ 
  110. wp_list_categories( apply_filters( 'widget_categories_args', $cat_args ) ); 
  111. ?> 
  112. </ul> 
  113. <?php 
  114.  
  115. echo $args['after_widget']; 
  116.  
  117. /** 
  118. * Handles updating settings for the current Categories widget instance. 
  119. * 
  120. * @since 2.8.0 
  121. * @access public 
  122. * 
  123. * @param array $new_instance New settings for this instance as input by the user via 
  124. * WP_Widget::form(). 
  125. * @param array $old_instance Old settings for this instance. 
  126. * @return array Updated settings to save. 
  127. */ 
  128. public function update( $new_instance, $old_instance ) { 
  129. $instance = $old_instance; 
  130. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  131. $instance['count'] = !empty($new_instance['count']) ? 1 : 0; 
  132. $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0; 
  133. $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0; 
  134.  
  135. return $instance; 
  136.  
  137. /** 
  138. * Outputs the settings form for the Categories widget. 
  139. * 
  140. * @since 2.8.0 
  141. * @access public 
  142. * 
  143. * @param array $instance Current settings. 
  144. */ 
  145. public function form( $instance ) { 
  146. //Defaults 
  147. $instance = wp_parse_args( (array) $instance, array( 'title' => '') ); 
  148. $title = sanitize_text_field( $instance['title'] ); 
  149. $count = isset($instance['count']) ? (bool) $instance['count'] :false; 
  150. $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false; 
  151. $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false; 
  152. ?> 
  153. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label> 
  154. <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( $title ); ?>" /></p> 
  155.  
  156. <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 ); ?> /> 
  157. <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br /> 
  158.  
  159. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> /> 
  160. <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br /> 
  161.  
  162. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> /> 
  163. <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p> 
  164. <?php 
  165.  
.