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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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( apply_filters( 'widget_posts_args', array(
  64. 'posts_per_page' => $number,
  65. 'no_found_rows' => true,
  66. 'post_status' => 'publish',
  67. 'ignore_sticky_posts' => true,
  68. ), $instance ) );
  69. if ( ! $r->have_posts() ) {
  70. return;
  71. }
  72. ?>
  73. <?php echo $args['before_widget']; ?>
  74. <?php
  75. if ( $title ) {
  76. echo $args['before_title'] . $title . $args['after_title'];
  77. }
  78. ?>
  79. <ul>
  80. <?php foreach ( $r->posts as $recent_post ) : ?>
  81. <?php
  82. $post_title = get_the_title( $recent_post->ID );
  83. $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
  84. ?>
  85. <li>
  86. <a href="<?php the_permalink( $recent_post->ID ); ?>"><?php echo $title ; ?></a>
  87. <?php if ( $show_date ) : ?>
  88. <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span>
  89. <?php endif; ?>
  90. </li>
  91. <?php endforeach; ?>
  92. </ul>
  93. <?php
  94. echo $args['after_widget'];
  95. }
  96. /**
  97. * Handles updating the settings for the current Recent Posts widget instance.
  98. *
  99. * @since 2.8.0
  100. *
  101. * @param array $new_instance New settings for this instance as input by the user via
  102. * WP_Widget::form().
  103. * @param array $old_instance Old settings for this instance.
  104. * @return array Updated settings to save.
  105. */
  106. public function update( $new_instance, $old_instance ) {
  107. $instance = $old_instance;
  108. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  109. $instance['number'] = (int) $new_instance['number'];
  110. $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
  111. return $instance;
  112. }
  113. /**
  114. * Outputs the settings form for the Recent Posts widget.
  115. *
  116. * @since 2.8.0
  117. *
  118. * @param array $instance Current settings.
  119. */
  120. public function form( $instance ) {
  121. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  122. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  123. $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
  124. ?>
  125. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  126. <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>
  127. <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
  128. <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>
  129. <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' ); ?>" />
  130. <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
  131. <?php
  132. }
  133. }