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

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