]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/widgets/class-wp-widget-recent-posts.php
WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / widgets / class-wp-widget-recent-posts.php
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          * @access public
24          */
25         public function __construct() {
26                 $widget_ops = array(
27                         'classname' => 'widget_recent_entries',
28                         'description' => __( 'Your site&#8217;s most recent Posts.' ),
29                         'customize_selective_refresh' => 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          * @access public
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 Recent Posts widget instance.
44          */
45         public function widget( $args, $instance ) {
46                 if ( ! isset( $args['widget_id'] ) ) {
47                         $args['widget_id'] = $this->id;
48                 }
49
50                 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
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                 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
59
60                 /**
61                  * Filter the arguments for the Recent Posts widget.
62                  *
63                  * @since 3.4.0
64                  *
65                  * @see WP_Query::get_posts()
66                  *
67                  * @param array $args An array of arguments used to retrieve the recent posts.
68                  */
69                 $r = new WP_Query( apply_filters( 'widget_posts_args', array(
70                         'posts_per_page'      => $number,
71                         'no_found_rows'       => true,
72                         'post_status'         => 'publish',
73                         'ignore_sticky_posts' => true
74                 ) ) );
75
76                 if ($r->have_posts()) :
77                 ?>
78                 <?php echo $args['before_widget']; ?>
79                 <?php if ( $title ) {
80                         echo $args['before_title'] . $title . $args['after_title'];
81                 } ?>
82                 <ul>
83                 <?php while ( $r->have_posts() ) : $r->the_post(); ?>
84                         <li>
85                                 <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
86                         <?php if ( $show_date ) : ?>
87                                 <span class="post-date"><?php echo get_the_date(); ?></span>
88                         <?php endif; ?>
89                         </li>
90                 <?php endwhile; ?>
91                 </ul>
92                 <?php echo $args['after_widget']; ?>
93                 <?php
94                 // Reset the global $the_post as this query will have stomped on it
95                 wp_reset_postdata();
96
97                 endif;
98         }
99
100         /**
101          * Handles updating the settings for the current Recent Posts widget instance.
102          *
103          * @since 2.8.0
104          * @access public
105          *
106          * @param array $new_instance New settings for this instance as input by the user via
107          *                            WP_Widget::form().
108          * @param array $old_instance Old settings for this instance.
109          * @return array Updated settings to save.
110          */
111         public function update( $new_instance, $old_instance ) {
112                 $instance = $old_instance;
113                 $instance['title'] = sanitize_text_field( $new_instance['title'] );
114                 $instance['number'] = (int) $new_instance['number'];
115                 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
116                 return $instance;
117         }
118
119         /**
120          * Outputs the settings form for the Recent Posts widget.
121          *
122          * @since 2.8.0
123          * @access public
124          *
125          * @param array $instance Current settings.
126          */
127         public function form( $instance ) {
128                 $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
129                 $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
130                 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
131 ?>
132                 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
133                 <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; ?>" /></p>
134
135                 <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
136                 <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" /></p>
137
138                 <p><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' ); ?>" />
139                 <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
140 <?php
141         }
142 }