You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

175 regels
5.5 KiB

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