class-wp-widget-recent-posts.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * Core class used to implement a Recent Posts widget.
  11. *
  12. * @since 2.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Recent_Posts extends WP_Widget {
  17. /**
  18. * Sets up a new Recent Posts widget instance.
  19. *
  20. * @since 2.8.0
  21. */
  22. public function __construct() {
  23. $widget_ops = array(
  24. 'classname' => 'widget_recent_entries',
  25. 'description' => __( 'Your site&#8217;s most recent Posts.' ),
  26. 'customize_selective_refresh' => true,
  27. );
  28. parent::__construct( 'recent-posts', __( 'Recent Posts' ), $widget_ops );
  29. $this->alt_option_name = 'widget_recent_entries';
  30. }
  31. /**
  32. * Outputs the content for the current Recent Posts widget instance.
  33. *
  34. * @since 2.8.0
  35. *
  36. * @param array $args Display arguments including 'before_title', 'after_title',
  37. * 'before_widget', and 'after_widget'.
  38. * @param array $instance Settings for the current Recent Posts widget instance.
  39. */
  40. public function widget( $args, $instance ) {
  41. if ( ! isset( $args['widget_id'] ) ) {
  42. $args['widget_id'] = $this->id;
  43. }
  44. $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
  45. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  46. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  47. $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
  48. if ( ! $number ) {
  49. $number = 5;
  50. }
  51. $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
  52. /**
  53. * Filters the arguments for the Recent Posts widget.
  54. *
  55. * @since 3.4.0
  56. * @since 4.9.0 Added the `$instance` parameter.
  57. *
  58. * @see WP_Query::get_posts()
  59. *
  60. * @param array $args An array of arguments used to retrieve the recent posts.
  61. * @param array $instance Array of settings for the current widget.
  62. */
  63. $r = new WP_Query(
  64. apply_filters(
  65. 'widget_posts_args',
  66. array(
  67. 'posts_per_page' => $number,
  68. 'no_found_rows' => true,
  69. 'post_status' => 'publish',
  70. 'ignore_sticky_posts' => true,
  71. ),
  72. $instance
  73. )
  74. );
  75. if ( ! $r->have_posts() ) {
  76. return;
  77. }
  78. ?>
  79. <?php echo $args['before_widget']; ?>
  80. <?php
  81. if ( $title ) {
  82. echo $args['before_title'] . $title . $args['after_title'];
  83. }
  84. ?>
  85. <ul>
  86. <?php foreach ( $r->posts as $recent_post ) : ?>
  87. <?php
  88. $post_title = get_the_title( $recent_post->ID );
  89. $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
  90. $aria_current = '';
  91. if ( get_queried_object_id() === $recent_post->ID ) {
  92. $aria_current = ' aria-current="page"';
  93. }
  94. ?>
  95. <li>
  96. <a href="<?php the_permalink( $recent_post->ID ); ?>"<?php echo $aria_current; ?>><?php echo $title; ?></a>
  97. <?php if ( $show_date ) : ?>
  98. <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span>
  99. <?php endif; ?>
  100. </li>
  101. <?php endforeach; ?>
  102. </ul>
  103. <?php
  104. echo $args['after_widget'];
  105. }
  106. /**
  107. * Handles updating the settings for the current Recent Posts widget instance.
  108. *
  109. * @since 2.8.0
  110. *
  111. * @param array $new_instance New settings for this instance as input by the user via
  112. * WP_Widget::form().
  113. * @param array $old_instance Old settings for this instance.
  114. * @return array Updated settings to save.
  115. */
  116. public function update( $new_instance, $old_instance ) {
  117. $instance = $old_instance;
  118. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  119. $instance['number'] = (int) $new_instance['number'];
  120. $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
  121. return $instance;
  122. }
  123. /**
  124. * Outputs the settings form for the Recent Posts widget.
  125. *
  126. * @since 2.8.0
  127. *
  128. * @param array $instance Current settings.
  129. */
  130. public function form( $instance ) {
  131. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  132. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  133. $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
  134. ?>
  135. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  136. <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>
  137. <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
  138. <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>
  139. <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' ); ?>" />
  140. <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
  141. <?php
  142. }
  143. }