class-wp-widget-categories.php 5.8 KB

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