class-wp-widget-categories.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  79. ?>
  80. <script<?php echo $type_attr; ?>>
  81. /* <![CDATA[ */
  82. (function() {
  83. var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
  84. function onCatChange() {
  85. if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
  86. dropdown.parentNode.submit();
  87. }
  88. }
  89. dropdown.onchange = onCatChange;
  90. })();
  91. /* ]]> */
  92. </script>
  93. <?php
  94. } else {
  95. ?>
  96. <ul>
  97. <?php
  98. $cat_args['title_li'] = '';
  99. /**
  100. * Filters the arguments for the Categories widget.
  101. *
  102. * @since 2.8.0
  103. * @since 4.9.0 Added the `$instance` parameter.
  104. *
  105. * @param array $cat_args An array of Categories widget options.
  106. * @param array $instance Array of settings for the current widget.
  107. */
  108. wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
  109. ?>
  110. </ul>
  111. <?php
  112. }
  113. echo $args['after_widget'];
  114. }
  115. /**
  116. * Handles updating settings for the current Categories widget instance.
  117. *
  118. * @since 2.8.0
  119. *
  120. * @param array $new_instance New settings for this instance as input by the user via
  121. * WP_Widget::form().
  122. * @param array $old_instance Old settings for this instance.
  123. * @return array Updated settings to save.
  124. */
  125. public function update( $new_instance, $old_instance ) {
  126. $instance = $old_instance;
  127. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  128. $instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0;
  129. $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
  130. $instance['dropdown'] = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
  131. return $instance;
  132. }
  133. /**
  134. * Outputs the settings form for the Categories widget.
  135. *
  136. * @since 2.8.0
  137. *
  138. * @param array $instance Current settings.
  139. */
  140. public function form( $instance ) {
  141. //Defaults
  142. $instance = wp_parse_args( (array) $instance, array( '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( $instance['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. }