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