[ 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 ); 30 parent::__construct( 'recent-posts', __( 'Recent Posts' ), $widget_ops ); 31 $this->alt_option_name = 'widget_recent_entries'; 32 } 33 34 /** 35 * Outputs the content for the current Recent Posts 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 Recent Posts widget instance. 42 */ 43 public function widget( $args, $instance ) { 44 if ( ! isset( $args['widget_id'] ) ) { 45 $args['widget_id'] = $this->id; 46 } 47 48 $default_title = __( 'Recent Posts' ); 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 $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; 55 if ( ! $number ) { 56 $number = 5; 57 } 58 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; 59 60 $r = new WP_Query( 61 /** 62 * Filters the arguments for the Recent Posts widget. 63 * 64 * @since 3.4.0 65 * @since 4.9.0 Added the `$instance` parameter. 66 * 67 * @see WP_Query::get_posts() 68 * 69 * @param array $args An array of arguments used to retrieve the recent posts. 70 * @param array $instance Array of settings for the current widget. 71 */ 72 apply_filters( 73 'widget_posts_args', 74 array( 75 'posts_per_page' => $number, 76 'no_found_rows' => true, 77 'post_status' => 'publish', 78 'ignore_sticky_posts' => true, 79 ), 80 $instance 81 ) 82 ); 83 84 if ( ! $r->have_posts() ) { 85 return; 86 } 87 ?> 88 89 <?php echo $args['before_widget']; ?> 90 91 <?php 92 if ( $title ) { 93 echo $args['before_title'] . $title . $args['after_title']; 94 } 95 96 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 97 98 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ 99 $format = apply_filters( 'navigation_widgets_format', $format ); 100 101 if ( 'html5' === $format ) { 102 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 103 $title = trim( strip_tags( $title ) ); 104 $aria_label = $title ? $title : $default_title; 105 echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">'; 106 } 107 ?> 108 109 <ul> 110 <?php foreach ( $r->posts as $recent_post ) : ?> 111 <?php 112 $post_title = get_the_title( $recent_post->ID ); 113 $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' ); 114 $aria_current = ''; 115 116 if ( get_queried_object_id() === $recent_post->ID ) { 117 $aria_current = ' aria-current="page"'; 118 } 119 ?> 120 <li> 121 <a href="<?php the_permalink( $recent_post->ID ); ?>"<?php echo $aria_current; ?>><?php echo $title; ?></a> 122 <?php if ( $show_date ) : ?> 123 <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span> 124 <?php endif; ?> 125 </li> 126 <?php endforeach; ?> 127 </ul> 128 129 <?php 130 if ( 'html5' === $format ) { 131 echo '</nav>'; 132 } 133 134 echo $args['after_widget']; 135 } 136 137 /** 138 * Handles updating the settings for the current Recent Posts widget instance. 139 * 140 * @since 2.8.0 141 * 142 * @param array $new_instance New settings for this instance as input by the user via 143 * WP_Widget::form(). 144 * @param array $old_instance Old settings for this instance. 145 * @return array Updated settings to save. 146 */ 147 public function update( $new_instance, $old_instance ) { 148 $instance = $old_instance; 149 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 150 $instance['number'] = (int) $new_instance['number']; 151 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; 152 return $instance; 153 } 154 155 /** 156 * Outputs the settings form for the Recent Posts widget. 157 * 158 * @since 2.8.0 159 * 160 * @param array $instance Current settings. 161 */ 162 public function form( $instance ) { 163 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; 164 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; 165 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; 166 ?> 167 <p> 168 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 169 <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; ?>" /> 170 </p> 171 172 <p> 173 <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> 174 <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" /> 175 </p> 176 177 <p> 178 <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' ); ?>" /> 179 <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label> 180 </p> 181 <?php 182 } 183 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 28 01:00:03 2021 | Cross-referenced by PHPXref 0.7.1 |