| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Widget API: WP_Widget_Archives class 4 * 5 * @package WordPress 6 * @subpackage Widgets 7 * @since 4.4.0 8 */ 9 10 /** 11 * Core class used to implement the Archives widget. 12 * 13 * @since 2.8.0 14 * 15 * @see WP_Widget 16 */ 17 class WP_Widget_Archives extends WP_Widget { 18 19 /** 20 * Sets up a new Archives widget instance. 21 * 22 * @since 2.8.0 23 */ 24 public function __construct() { 25 $widget_ops = array( 26 'classname' => 'widget_archive', 27 'description' => __( 'A monthly archive of your site’s Posts.' ), 28 'customize_selective_refresh' => true, 29 'show_instance_in_rest' => true, 30 ); 31 parent::__construct( 'archives', __( 'Archives' ), $widget_ops ); 32 } 33 34 /** 35 * Outputs the content for the current Archives widget instance. 36 * 37 * @since 2.8.0 38 * 39 * @param array $args Display arguments including 'before_title', 'after_title', 40 * 'before_widget', and 'after_widget'. 41 * @param array $instance Settings for the current Archives widget instance. 42 */ 43 public function widget( $args, $instance ) { 44 $default_title = __( 'Archives' ); 45 $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title; 46 47 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 48 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 49 50 $count = ! empty( $instance['count'] ) ? '1' : '0'; 51 $dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0'; 52 53 echo $args['before_widget']; 54 55 if ( $title ) { 56 echo $args['before_title'] . $title . $args['after_title']; 57 } 58 59 if ( $dropdown ) { 60 $dropdown_id = "{$this->id_base}-dropdown-{$this->number}"; 61 ?> 62 <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label> 63 <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown"> 64 <?php 65 /** 66 * Filters the arguments for the Archives widget drop-down. 67 * 68 * @since 2.8.0 69 * @since 4.9.0 Added the `$instance` parameter. 70 * 71 * @see wp_get_archives() 72 * 73 * @param array $args An array of Archives widget drop-down arguments. 74 * @param array $instance Settings for the current Archives widget instance. 75 */ 76 $dropdown_args = apply_filters( 77 'widget_archives_dropdown_args', 78 array( 79 'type' => 'monthly', 80 'format' => 'option', 81 'show_post_count' => $count, 82 ), 83 $instance 84 ); 85 86 switch ( $dropdown_args['type'] ) { 87 case 'yearly': 88 $label = __( 'Select Year' ); 89 break; 90 case 'monthly': 91 $label = __( 'Select Month' ); 92 break; 93 case 'daily': 94 $label = __( 'Select Day' ); 95 break; 96 case 'weekly': 97 $label = __( 'Select Week' ); 98 break; 99 default: 100 $label = __( 'Select Post' ); 101 break; 102 } 103 ?> 104 105 <option value=""><?php echo esc_html( $label ); ?></option> 106 <?php wp_get_archives( $dropdown_args ); ?> 107 108 </select> 109 110 <?php ob_start(); ?> 111 <script> 112 ( ( dropdownId ) => { 113 const dropdown = document.getElementById( dropdownId ); 114 function onSelectChange() { 115 setTimeout( () => { 116 if ( 'escape' === dropdown.dataset.lastkey ) { 117 return; 118 } 119 if ( dropdown.value ) { 120 document.location.href = dropdown.value; 121 } 122 }, 250 ); 123 } 124 function onKeyUp( event ) { 125 if ( 'Escape' === event.key ) { 126 dropdown.dataset.lastkey = 'escape'; 127 } else { 128 delete dropdown.dataset.lastkey; 129 } 130 } 131 function onClick() { 132 delete dropdown.dataset.lastkey; 133 } 134 dropdown.addEventListener( 'keyup', onKeyUp ); 135 dropdown.addEventListener( 'click', onClick ); 136 dropdown.addEventListener( 'change', onSelectChange ); 137 })( <?php echo wp_json_encode( $dropdown_id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> ); 138 </script> 139 <?php 140 wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) . "\n//# sourceURL=" . rawurlencode( __METHOD__ ) ); 141 } else { 142 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 143 144 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ 145 $format = apply_filters( 'navigation_widgets_format', $format ); 146 147 if ( 'html5' === $format ) { 148 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 149 $title = trim( strip_tags( $title ) ); 150 $aria_label = $title ? $title : $default_title; 151 echo '<nav aria-label="' . esc_attr( $aria_label ) . '">'; 152 } 153 ?> 154 155 <ul> 156 <?php 157 wp_get_archives( 158 /** 159 * Filters the arguments for the Archives widget. 160 * 161 * @since 2.8.0 162 * @since 4.9.0 Added the `$instance` parameter. 163 * 164 * @see wp_get_archives() 165 * 166 * @param array $args An array of Archives option arguments. 167 * @param array $instance Array of settings for the current widget. 168 */ 169 apply_filters( 170 'widget_archives_args', 171 array( 172 'type' => 'monthly', 173 'show_post_count' => $count, 174 ), 175 $instance 176 ) 177 ); 178 ?> 179 </ul> 180 181 <?php 182 if ( 'html5' === $format ) { 183 echo '</nav>'; 184 } 185 } 186 187 echo $args['after_widget']; 188 } 189 190 /** 191 * Handles updating settings for the current Archives widget instance. 192 * 193 * @since 2.8.0 194 * 195 * @param array $new_instance New settings for this instance as input by the user via 196 * WP_Widget_Archives::form(). 197 * @param array $old_instance Old settings for this instance. 198 * @return array Updated settings to save. 199 */ 200 public function update( $new_instance, $old_instance ) { 201 $instance = $old_instance; 202 $new_instance = wp_parse_args( 203 (array) $new_instance, 204 array( 205 'title' => '', 206 'count' => 0, 207 'dropdown' => '', 208 ) 209 ); 210 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 211 $instance['count'] = $new_instance['count'] ? 1 : 0; 212 $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0; 213 214 return $instance; 215 } 216 217 /** 218 * Outputs the settings form for the Archives widget. 219 * 220 * @since 2.8.0 221 * 222 * @param array $instance Current settings. 223 */ 224 public function form( $instance ) { 225 $instance = wp_parse_args( 226 (array) $instance, 227 array( 228 'title' => '', 229 'count' => 0, 230 'dropdown' => '', 231 ) 232 ); 233 ?> 234 <p> 235 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 236 <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'] ); ?>" /> 237 </p> 238 <p> 239 <input class="checkbox" type="checkbox"<?php checked( $instance['dropdown'] ); ?> id="<?php echo $this->get_field_id( 'dropdown' ); ?>" name="<?php echo $this->get_field_name( 'dropdown' ); ?>" /> 240 <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label> 241 <br /> 242 <input class="checkbox" type="checkbox"<?php checked( $instance['count'] ); ?> id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" /> 243 <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label> 244 </p> 245 <?php 246 } 247 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Thu Nov 6 08:20:07 2025 | Cross-referenced by PHPXref |