[ Index ] |
PHP Cross Reference of WordPress |
[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 $type_attr = current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"'; 105 ?> 106 107 <option value=""><?php echo esc_html( $label ); ?></option> 108 <?php wp_get_archives( $dropdown_args ); ?> 109 110 </select> 111 112 <script<?php echo $type_attr; ?>> 113 /* <![CDATA[ */ 114 (function() { 115 var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" ); 116 function onSelectChange() { 117 if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) { 118 document.location.href = this.options[ this.selectedIndex ].value; 119 } 120 } 121 dropdown.onchange = onSelectChange; 122 })(); 123 /* ]]> */ 124 </script> 125 <?php 126 } else { 127 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 128 129 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ 130 $format = apply_filters( 'navigation_widgets_format', $format ); 131 132 if ( 'html5' === $format ) { 133 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 134 $title = trim( strip_tags( $title ) ); 135 $aria_label = $title ? $title : $default_title; 136 echo '<nav aria-label="' . esc_attr( $aria_label ) . '">'; 137 } 138 ?> 139 140 <ul> 141 <?php 142 wp_get_archives( 143 /** 144 * Filters the arguments for the Archives widget. 145 * 146 * @since 2.8.0 147 * @since 4.9.0 Added the `$instance` parameter. 148 * 149 * @see wp_get_archives() 150 * 151 * @param array $args An array of Archives option arguments. 152 * @param array $instance Array of settings for the current widget. 153 */ 154 apply_filters( 155 'widget_archives_args', 156 array( 157 'type' => 'monthly', 158 'show_post_count' => $count, 159 ), 160 $instance 161 ) 162 ); 163 ?> 164 </ul> 165 166 <?php 167 if ( 'html5' === $format ) { 168 echo '</nav>'; 169 } 170 } 171 172 echo $args['after_widget']; 173 } 174 175 /** 176 * Handles updating settings for the current Archives widget instance. 177 * 178 * @since 2.8.0 179 * 180 * @param array $new_instance New settings for this instance as input by the user via 181 * WP_Widget_Archives::form(). 182 * @param array $old_instance Old settings for this instance. 183 * @return array Updated settings to save. 184 */ 185 public function update( $new_instance, $old_instance ) { 186 $instance = $old_instance; 187 $new_instance = wp_parse_args( 188 (array) $new_instance, 189 array( 190 'title' => '', 191 'count' => 0, 192 'dropdown' => '', 193 ) 194 ); 195 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 196 $instance['count'] = $new_instance['count'] ? 1 : 0; 197 $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0; 198 199 return $instance; 200 } 201 202 /** 203 * Outputs the settings form for the Archives widget. 204 * 205 * @since 2.8.0 206 * 207 * @param array $instance Current settings. 208 */ 209 public function form( $instance ) { 210 $instance = wp_parse_args( 211 (array) $instance, 212 array( 213 'title' => '', 214 'count' => 0, 215 'dropdown' => '', 216 ) 217 ); 218 ?> 219 <p> 220 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 221 <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'] ); ?>" /> 222 </p> 223 <p> 224 <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' ); ?>" /> 225 <label for="<?php echo $this->get_field_id( 'dropdown' ); ?>"><?php _e( 'Display as dropdown' ); ?></label> 226 <br/> 227 <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' ); ?>" /> 228 <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show post counts' ); ?></label> 229 </p> 230 <?php 231 } 232 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |