Нет описания

class-wp-widget-categories.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 'show_instance_in_rest' => true,
  28. );
  29. parent::__construct( 'categories', __( 'Categories' ), $widget_ops );
  30. }
  31. /**
  32. * Outputs the content for the current Categories widget instance.
  33. *
  34. * @since 2.8.0
  35. * @since 4.2.0 Creates a unique HTML ID for the `<select>` element
  36. * if more than one instance is displayed on the page.
  37. *
  38. * @param array $args Display arguments including 'before_title', 'after_title',
  39. * 'before_widget', and 'after_widget'.
  40. * @param array $instance Settings for the current Categories widget instance.
  41. */
  42. public function widget( $args, $instance ) {
  43. static $first_dropdown = true;
  44. $default_title = __( 'Categories' );
  45. $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
  46. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  47. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  48. $count = ! empty( $instance['count'] ) ? '1' : '0';
  49. $hierarchical = ! empty( $instance['hierarchical'] ) ? '1' : '0';
  50. $dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0';
  51. echo $args['before_widget'];
  52. if ( $title ) {
  53. echo $args['before_title'] . $title . $args['after_title'];
  54. }
  55. $cat_args = array(
  56. 'orderby' => 'name',
  57. 'show_count' => $count,
  58. 'hierarchical' => $hierarchical,
  59. );
  60. if ( $dropdown ) {
  61. printf( '<form action="%s" method="get">', esc_url( home_url() ) );
  62. $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
  63. $first_dropdown = false;
  64. echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
  65. $cat_args['show_option_none'] = __( 'Select Category' );
  66. $cat_args['id'] = $dropdown_id;
  67. /**
  68. * Filters the arguments for the Categories widget drop-down.
  69. *
  70. * @since 2.8.0
  71. * @since 4.9.0 Added the `$instance` parameter.
  72. *
  73. * @see wp_dropdown_categories()
  74. *
  75. * @param array $cat_args An array of Categories widget drop-down arguments.
  76. * @param array $instance Array of settings for the current widget.
  77. */
  78. wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) );
  79. echo '</form>';
  80. $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"';
  81. ?>
  82. <script<?php echo $type_attr; ?>>
  83. /* <![CDATA[ */
  84. (function() {
  85. var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
  86. function onCatChange() {
  87. if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
  88. dropdown.parentNode.submit();
  89. }
  90. }
  91. dropdown.onchange = onCatChange;
  92. })();
  93. /* ]]> */
  94. </script>
  95. <?php
  96. } else {
  97. $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  98. /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
  99. $format = apply_filters( 'navigation_widgets_format', $format );
  100. if ( 'html5' === $format ) {
  101. // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
  102. $title = trim( strip_tags( $title ) );
  103. $aria_label = $title ? $title : $default_title;
  104. echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
  105. }
  106. ?>
  107. <ul>
  108. <?php
  109. $cat_args['title_li'] = '';
  110. /**
  111. * Filters the arguments for the Categories widget.
  112. *
  113. * @since 2.8.0
  114. * @since 4.9.0 Added the `$instance` parameter.
  115. *
  116. * @param array $cat_args An array of Categories widget options.
  117. * @param array $instance Array of settings for the current widget.
  118. */
  119. wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
  120. ?>
  121. </ul>
  122. <?php
  123. if ( 'html5' === $format ) {
  124. echo '</nav>';
  125. }
  126. }
  127. echo $args['after_widget'];
  128. }
  129. /**
  130. * Handles updating settings for the current Categories widget instance.
  131. *
  132. * @since 2.8.0
  133. *
  134. * @param array $new_instance New settings for this instance as input by the user via
  135. * WP_Widget::form().
  136. * @param array $old_instance Old settings for this instance.
  137. * @return array Updated settings to save.
  138. */
  139. public function update( $new_instance, $old_instance ) {
  140. $instance = $old_instance;
  141. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  142. $instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0;
  143. $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
  144. $instance['dropdown'] = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
  145. return $instance;
  146. }
  147. /**
  148. * Outputs the settings form for the Categories widget.
  149. *
  150. * @since 2.8.0
  151. *
  152. * @param array $instance Current settings.
  153. */
  154. public function form( $instance ) {
  155. // Defaults.
  156. $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  157. $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
  158. $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
  159. $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
  160. ?>
  161. <p>
  162. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  163. <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'] ); ?>" />
  164. </p>
  165. <p>
  166. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>"<?php checked( $dropdown ); ?> />
  167. <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label>
  168. <br />
  169. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>"<?php checked( $count ); ?> />
  170. <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label>
  171. <br />
  172. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>"<?php checked( $hierarchical ); ?> />
  173. <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>"><?php _e( 'Show hierarchy' ); ?></label>
  174. </p>
  175. <?php
  176. }
  177. }