[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Widget API: WP_Widget_Recent_Comments class
   4   *
   5   * @package WordPress
   6   * @subpackage Widgets
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core class used to implement a Recent Comments widget.
  12   *
  13   * @since 2.8.0
  14   *
  15   * @see WP_Widget
  16   */
  17  class WP_Widget_Recent_Comments extends WP_Widget {
  18  
  19      /**
  20       * Sets up a new Recent Comments widget instance.
  21       *
  22       * @since 2.8.0
  23       */
  24  	public function __construct() {
  25          $widget_ops = array(
  26              'classname'                   => 'widget_recent_comments',
  27              'description'                 => __( 'Your site&#8217;s most recent comments.' ),
  28              'customize_selective_refresh' => true,
  29              'show_instance_in_rest'       => true,
  30          );
  31          parent::__construct( 'recent-comments', __( 'Recent Comments' ), $widget_ops );
  32          $this->alt_option_name = 'widget_recent_comments';
  33  
  34          if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
  35              add_action( 'wp_head', array( $this, 'recent_comments_style' ) );
  36          }
  37      }
  38  
  39      /**
  40       * Outputs the default styles for the Recent Comments widget.
  41       *
  42       * @since 2.8.0
  43       */
  44  	public function recent_comments_style() {
  45          /**
  46           * Filters the Recent Comments default widget styles.
  47           *
  48           * @since 3.1.0
  49           *
  50           * @param bool   $active  Whether the widget is active. Default true.
  51           * @param string $id_base The widget ID.
  52           */
  53          if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876.
  54              || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) ) {
  55              return;
  56          }
  57  
  58          $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
  59  
  60          printf(
  61              '<style%s>.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>',
  62              $type_attr
  63          );
  64      }
  65  
  66      /**
  67       * Outputs the content for the current Recent Comments widget instance.
  68       *
  69       * @since 2.8.0
  70       * @since 5.4.0 Creates a unique HTML ID for the `<ul>` element
  71       *              if more than one instance is displayed on the page.
  72       *
  73       * @param array $args     Display arguments including 'before_title', 'after_title',
  74       *                        'before_widget', and 'after_widget'.
  75       * @param array $instance Settings for the current Recent Comments widget instance.
  76       */
  77  	public function widget( $args, $instance ) {
  78          static $first_instance = true;
  79  
  80          if ( ! isset( $args['widget_id'] ) ) {
  81              $args['widget_id'] = $this->id;
  82          }
  83  
  84          $output = '';
  85  
  86          $default_title = __( 'Recent Comments' );
  87          $title         = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title;
  88  
  89          /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  90          $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  91  
  92          $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
  93          if ( ! $number ) {
  94              $number = 5;
  95          }
  96  
  97          $comments = get_comments(
  98              /**
  99               * Filters the arguments for the Recent Comments widget.
 100               *
 101               * @since 3.4.0
 102               * @since 4.9.0 Added the `$instance` parameter.
 103               *
 104               * @see WP_Comment_Query::query() for information on accepted arguments.
 105               *
 106               * @param array $comment_args An array of arguments used to retrieve the recent comments.
 107               * @param array $instance     Array of settings for the current widget.
 108               */
 109              apply_filters(
 110                  'widget_comments_args',
 111                  array(
 112                      'number'      => $number,
 113                      'status'      => 'approve',
 114                      'post_status' => 'publish',
 115                  ),
 116                  $instance
 117              )
 118          );
 119  
 120          $output .= $args['before_widget'];
 121          if ( $title ) {
 122              $output .= $args['before_title'] . $title . $args['after_title'];
 123          }
 124  
 125          $recent_comments_id = ( $first_instance ) ? 'recentcomments' : "recentcomments-{$this->number}";
 126          $first_instance     = false;
 127  
 128          $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
 129  
 130          /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
 131          $format = apply_filters( 'navigation_widgets_format', $format );
 132  
 133          if ( 'html5' === $format ) {
 134              // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
 135              $title      = trim( strip_tags( $title ) );
 136              $aria_label = $title ? $title : $default_title;
 137              $output    .= '<nav aria-label="' . esc_attr( $aria_label ) . '">';
 138          }
 139  
 140          $output .= '<ul id="' . esc_attr( $recent_comments_id ) . '">';
 141          if ( is_array( $comments ) && $comments ) {
 142              // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
 143              $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
 144              _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
 145  
 146              foreach ( (array) $comments as $comment ) {
 147                  $output .= '<li class="recentcomments">';
 148                  $output .= sprintf(
 149                      /* translators: Comments widget. 1: Comment author, 2: Post link. */
 150                      _x( '%1$s on %2$s', 'widgets' ),
 151                      '<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>',
 152                      '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>'
 153                  );
 154                  $output .= '</li>';
 155              }
 156          }
 157          $output .= '</ul>';
 158  
 159          if ( 'html5' === $format ) {
 160              $output .= '</nav>';
 161          }
 162  
 163          $output .= $args['after_widget'];
 164  
 165          echo $output;
 166      }
 167  
 168      /**
 169       * Handles updating settings for the current Recent Comments widget instance.
 170       *
 171       * @since 2.8.0
 172       *
 173       * @param array $new_instance New settings for this instance as input by the user via
 174       *                            WP_Widget::form().
 175       * @param array $old_instance Old settings for this instance.
 176       * @return array Updated settings to save.
 177       */
 178  	public function update( $new_instance, $old_instance ) {
 179          $instance           = $old_instance;
 180          $instance['title']  = sanitize_text_field( $new_instance['title'] );
 181          $instance['number'] = absint( $new_instance['number'] );
 182          return $instance;
 183      }
 184  
 185      /**
 186       * Outputs the settings form for the Recent Comments widget.
 187       *
 188       * @since 2.8.0
 189       *
 190       * @param array $instance Current settings.
 191       */
 192  	public function form( $instance ) {
 193          $title  = isset( $instance['title'] ) ? $instance['title'] : '';
 194          $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
 195          ?>
 196          <p>
 197              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 198              <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
 199          </p>
 200  
 201          <p>
 202              <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:' ); ?></label>
 203              <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" />
 204          </p>
 205          <?php
 206      }
 207  
 208      /**
 209       * Flushes the Recent Comments widget cache.
 210       *
 211       * @since 2.8.0
 212       *
 213       * @deprecated 4.4.0 Fragment caching was removed in favor of split queries.
 214       */
 215  	public function flush_widget_cache() {
 216          _deprecated_function( __METHOD__, '4.4.0' );
 217      }
 218  }


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1