[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/widgets/ -> class-wp-widget-recent-posts.php (source)

   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&#8217;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          $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
  49  
  50          /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  51          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  52  
  53          $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
  54          if ( ! $number ) {
  55              $number = 5;
  56          }
  57          $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
  58  
  59          /**
  60           * Filters the arguments for the Recent Posts widget.
  61           *
  62           * @since 3.4.0
  63           * @since 4.9.0 Added the `$instance` parameter.
  64           *
  65           * @see WP_Query::get_posts()
  66           *
  67           * @param array $args     An array of arguments used to retrieve the recent posts.
  68           * @param array $instance Array of settings for the current widget.
  69           */
  70          $r = new WP_Query(
  71              apply_filters(
  72                  'widget_posts_args',
  73                  array(
  74                      'posts_per_page'      => $number,
  75                      'no_found_rows'       => true,
  76                      'post_status'         => 'publish',
  77                      'ignore_sticky_posts' => true,
  78                  ),
  79                  $instance
  80              )
  81          );
  82  
  83          if ( ! $r->have_posts() ) {
  84              return;
  85          }
  86          ?>
  87          <?php echo $args['before_widget']; ?>
  88          <?php
  89          if ( $title ) {
  90              echo $args['before_title'] . $title . $args['after_title'];
  91          }
  92          ?>
  93          <ul>
  94              <?php foreach ( $r->posts as $recent_post ) : ?>
  95                  <?php
  96                  $post_title   = get_the_title( $recent_post->ID );
  97                  $title        = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
  98                  $aria_current = '';
  99  
 100                  if ( get_queried_object_id() === $recent_post->ID ) {
 101                      $aria_current = ' aria-current="page"';
 102                  }
 103                  ?>
 104                  <li>
 105                      <a href="<?php the_permalink( $recent_post->ID ); ?>"<?php echo $aria_current; ?>><?php echo $title; ?></a>
 106                      <?php if ( $show_date ) : ?>
 107                          <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span>
 108                      <?php endif; ?>
 109                  </li>
 110              <?php endforeach; ?>
 111          </ul>
 112          <?php
 113          echo $args['after_widget'];
 114      }
 115  
 116      /**
 117       * Handles updating the settings for the current Recent Posts widget instance.
 118       *
 119       * @since 2.8.0
 120       *
 121       * @param array $new_instance New settings for this instance as input by the user via
 122       *                            WP_Widget::form().
 123       * @param array $old_instance Old settings for this instance.
 124       * @return array Updated settings to save.
 125       */
 126  	public function update( $new_instance, $old_instance ) {
 127          $instance              = $old_instance;
 128          $instance['title']     = sanitize_text_field( $new_instance['title'] );
 129          $instance['number']    = (int) $new_instance['number'];
 130          $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
 131          return $instance;
 132      }
 133  
 134      /**
 135       * Outputs the settings form for the Recent Posts widget.
 136       *
 137       * @since 2.8.0
 138       *
 139       * @param array $instance Current settings.
 140       */
 141  	public function form( $instance ) {
 142          $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
 143          $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
 144          $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
 145          ?>
 146          <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 147          <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>
 148  
 149          <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
 150          <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>
 151  
 152          <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' ); ?>" />
 153          <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
 154          <?php
 155      }
 156  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7