[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Widget API: WP_Widget_Recent_Posts class 4 * 5 * @package WordPress 6 * @subpackage Widgets 7 * @since 4.4.0 8 */ 9 10 /** 11 * Core class used to implement a Recent Posts widget. 12 * 13 * @since 2.8.0 14 * 15 * @see WP_Widget 16 */ 17 class WP_Widget_Recent_Posts extends WP_Widget { 18 19 /** 20 * Sets up a new Recent Posts widget instance. 21 * 22 * @since 2.8.0 23 */ 24 public function __construct() { 25 $widget_ops = array( 26 'classname' => 'widget_recent_entries', 27 'description' => __( 'Your site’s most recent Posts.' ), 28 'customize_selective_refresh' => true, 29 'show_instance_in_rest' => true, 30 ); 31 parent::__construct( 'recent-posts', __( 'Recent Posts' ), $widget_ops ); 32 $this->alt_option_name = 'widget_recent_entries'; 33 } 34 35 /** 36 * Outputs the content for the current Recent Posts widget instance. 37 * 38 * @since 2.8.0 39 * 40 * @param array $args Display arguments including 'before_title', 'after_title', 41 * 'before_widget', and 'after_widget'. 42 * @param array $instance Settings for the current Recent Posts widget instance. 43 */ 44 public function widget( $args, $instance ) { 45 if ( ! isset( $args['widget_id'] ) ) { 46 $args['widget_id'] = $this->id; 47 } 48 49 $default_title = __( 'Recent Posts' ); 50 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title; 51 52 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 53 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 54 55 $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; 56 if ( ! $number ) { 57 $number = 5; 58 } 59 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; 60 61 $r = new WP_Query( 62 /** 63 * Filters the arguments for the Recent Posts widget. 64 * 65 * @since 3.4.0 66 * @since 4.9.0 Added the `$instance` parameter. 67 * 68 * @see WP_Query::get_posts() 69 * 70 * @param array $args An array of arguments used to retrieve the recent posts. 71 * @param array $instance Array of settings for the current widget. 72 */ 73 apply_filters( 74 'widget_posts_args', 75 array( 76 'posts_per_page' => $number, 77 'no_found_rows' => true, 78 'post_status' => 'publish', 79 'ignore_sticky_posts' => true, 80 ), 81 $instance 82 ) 83 ); 84 85 if ( ! $r->have_posts() ) { 86 return; 87 } 88 ?> 89 90 <?php echo $args['before_widget']; ?> 91 92 <?php 93 if ( $title ) { 94 echo $args['before_title'] . $title . $args['after_title']; 95 } 96 97 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 98 99 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ 100 $format = apply_filters( 'navigation_widgets_format', $format ); 101 102 if ( 'html5' === $format ) { 103 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 104 $title = trim( strip_tags( $title ) ); 105 $aria_label = $title ? $title : $default_title; 106 echo '<nav aria-label="' . esc_attr( $aria_label ) . '">'; 107 } 108 ?> 109 110 <ul> 111 <?php foreach ( $r->posts as $recent_post ) : ?> 112 <?php 113 $post_title = get_the_title( $recent_post->ID ); 114 $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' ); 115 $aria_current = ''; 116 117 if ( get_queried_object_id() === $recent_post->ID ) { 118 $aria_current = ' aria-current="page"'; 119 } 120 ?> 121 <li> 122 <a href="<?php the_permalink( $recent_post->ID ); ?>"<?php echo $aria_current; ?>><?php echo $title; ?></a> 123 <?php if ( $show_date ) : ?> 124 <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span> 125 <?php endif; ?> 126 </li> 127 <?php endforeach; ?> 128 </ul> 129 130 <?php 131 if ( 'html5' === $format ) { 132 echo '</nav>'; 133 } 134 135 echo $args['after_widget']; 136 } 137 138 /** 139 * Handles updating the settings for the current Recent Posts widget instance. 140 * 141 * @since 2.8.0 142 * 143 * @param array $new_instance New settings for this instance as input by the user via 144 * WP_Widget::form(). 145 * @param array $old_instance Old settings for this instance. 146 * @return array Updated settings to save. 147 */ 148 public function update( $new_instance, $old_instance ) { 149 $instance = $old_instance; 150 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 151 $instance['number'] = (int) $new_instance['number']; 152 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; 153 return $instance; 154 } 155 156 /** 157 * Outputs the settings form for the Recent Posts widget. 158 * 159 * @since 2.8.0 160 * 161 * @param array $instance Current settings. 162 */ 163 public function form( $instance ) { 164 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; 165 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; 166 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; 167 ?> 168 <p> 169 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 170 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /> 171 </p> 172 173 <p> 174 <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> 175 <input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" /> 176 </p> 177 178 <p> 179 <input class="checkbox" type="checkbox"<?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" /> 180 <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label> 181 </p> 182 <?php 183 } 184 }
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 |