wp/wp-includes/widgets/class-wp-widget-recent-comments.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    47 		 * @since 3.1.0
    47 		 * @since 3.1.0
    48 		 *
    48 		 *
    49 		 * @param bool   $active  Whether the widget is active. Default true.
    49 		 * @param bool   $active  Whether the widget is active. Default true.
    50 		 * @param string $id_base The widget ID.
    50 		 * @param string $id_base The widget ID.
    51 		 */
    51 		 */
    52 		if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
    52 		if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876.
    53 			|| ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) ) {
    53 			|| ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) ) {
    54 			return;
    54 			return;
    55 		}
    55 		}
    56 		?>
    56 
    57 		<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
    57 		$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
    58 		<?php
    58 
       
    59 		printf(
       
    60 			'<style%s>.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>',
       
    61 			$type_attr
       
    62 		);
    59 	}
    63 	}
    60 
    64 
    61 	/**
    65 	/**
    62 	 * Outputs the content for the current Recent Comments widget instance.
    66 	 * Outputs the content for the current Recent Comments widget instance.
    63 	 *
    67 	 *
    64 	 * @since 2.8.0
    68 	 * @since 2.8.0
       
    69 	 * @since 5.4.0 Creates a unique HTML ID for the `<ul>` element
       
    70 	 *              if more than one instance is displayed on the page.
    65 	 *
    71 	 *
    66 	 * @param array $args     Display arguments including 'before_title', 'after_title',
    72 	 * @param array $args     Display arguments including 'before_title', 'after_title',
    67 	 *                        'before_widget', and 'after_widget'.
    73 	 *                        'before_widget', and 'after_widget'.
    68 	 * @param array $instance Settings for the current Recent Comments widget instance.
    74 	 * @param array $instance Settings for the current Recent Comments widget instance.
    69 	 */
    75 	 */
    70 	public function widget( $args, $instance ) {
    76 	public function widget( $args, $instance ) {
       
    77 		static $first_instance = true;
       
    78 
    71 		if ( ! isset( $args['widget_id'] ) ) {
    79 		if ( ! isset( $args['widget_id'] ) ) {
    72 			$args['widget_id'] = $this->id;
    80 			$args['widget_id'] = $this->id;
    73 		}
    81 		}
    74 
    82 
    75 		$output = '';
    83 		$output = '';
    76 
    84 
    77 		$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' );
    85 		$default_title = __( 'Recent Comments' );
       
    86 		$title         = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title;
    78 
    87 
    79 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    88 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    80 		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    89 		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    81 
    90 
    82 		$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
    91 		$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
    83 		if ( ! $number ) {
    92 		if ( ! $number ) {
    84 			$number = 5;
    93 			$number = 5;
    85 		}
    94 		}
    86 
    95 
    87 		/**
       
    88 		 * Filters the arguments for the Recent Comments widget.
       
    89 		 *
       
    90 		 * @since 3.4.0
       
    91 		 * @since 4.9.0 Added the `$instance` parameter.
       
    92 		 *
       
    93 		 * @see WP_Comment_Query::query() for information on accepted arguments.
       
    94 		 *
       
    95 		 * @param array $comment_args An array of arguments used to retrieve the recent comments.
       
    96 		 * @param array $instance     Array of settings for the current widget.
       
    97 		 */
       
    98 		$comments = get_comments(
    96 		$comments = get_comments(
       
    97 			/**
       
    98 			 * Filters the arguments for the Recent Comments widget.
       
    99 			 *
       
   100 			 * @since 3.4.0
       
   101 			 * @since 4.9.0 Added the `$instance` parameter.
       
   102 			 *
       
   103 			 * @see WP_Comment_Query::query() for information on accepted arguments.
       
   104 			 *
       
   105 			 * @param array $comment_args An array of arguments used to retrieve the recent comments.
       
   106 			 * @param array $instance     Array of settings for the current widget.
       
   107 			 */
    99 			apply_filters(
   108 			apply_filters(
   100 				'widget_comments_args',
   109 				'widget_comments_args',
   101 				array(
   110 				array(
   102 					'number'      => $number,
   111 					'number'      => $number,
   103 					'status'      => 'approve',
   112 					'status'      => 'approve',
   110 		$output .= $args['before_widget'];
   119 		$output .= $args['before_widget'];
   111 		if ( $title ) {
   120 		if ( $title ) {
   112 			$output .= $args['before_title'] . $title . $args['after_title'];
   121 			$output .= $args['before_title'] . $title . $args['after_title'];
   113 		}
   122 		}
   114 
   123 
   115 		$output .= '<ul id="recentcomments">';
   124 		$recent_comments_id = ( $first_instance ) ? 'recentcomments' : "recentcomments-{$this->number}";
       
   125 		$first_instance     = false;
       
   126 
       
   127 		$format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
       
   128 
       
   129 		/** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
       
   130 		$format = apply_filters( 'navigation_widgets_format', $format );
       
   131 
       
   132 		if ( 'html5' === $format ) {
       
   133 			// The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
       
   134 			$title      = trim( strip_tags( $title ) );
       
   135 			$aria_label = $title ? $title : $default_title;
       
   136 			$output    .= '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
       
   137 		}
       
   138 
       
   139 		$output .= '<ul id="' . esc_attr( $recent_comments_id ) . '">';
   116 		if ( is_array( $comments ) && $comments ) {
   140 		if ( is_array( $comments ) && $comments ) {
   117 			// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
   141 			// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
   118 			$post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
   142 			$post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
   119 			_prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
   143 			_prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
   120 
   144 
   121 			foreach ( (array) $comments as $comment ) {
   145 			foreach ( (array) $comments as $comment ) {
   122 				$output .= '<li class="recentcomments">';
   146 				$output .= '<li class="recentcomments">';
   123 				/* translators: comments widget: 1: comment author, 2: post link */
       
   124 				$output .= sprintf(
   147 				$output .= sprintf(
       
   148 					/* translators: Comments widget. 1: Comment author, 2: Post link. */
   125 					_x( '%1$s on %2$s', 'widgets' ),
   149 					_x( '%1$s on %2$s', 'widgets' ),
   126 					'<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>',
   150 					'<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>',
   127 					'<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>'
   151 					'<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>'
   128 				);
   152 				);
   129 				$output .= '</li>';
   153 				$output .= '</li>';
   130 			}
   154 			}
   131 		}
   155 		}
   132 		$output .= '</ul>';
   156 		$output .= '</ul>';
       
   157 
       
   158 		if ( 'html5' === $format ) {
       
   159 			$output .= '</nav>';
       
   160 		}
       
   161 
   133 		$output .= $args['after_widget'];
   162 		$output .= $args['after_widget'];
   134 
   163 
   135 		echo $output;
   164 		echo $output;
   136 	}
   165 	}
   137 
   166 
   161 	 */
   190 	 */
   162 	public function form( $instance ) {
   191 	public function form( $instance ) {
   163 		$title  = isset( $instance['title'] ) ? $instance['title'] : '';
   192 		$title  = isset( $instance['title'] ) ? $instance['title'] : '';
   164 		$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
   193 		$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
   165 		?>
   194 		?>
   166 		<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
   195 		<p>
   167 		<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 ); ?>" /></p>
   196 			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
   168 
   197 			<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 ); ?>" />
   169 		<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:' ); ?></label>
   198 		</p>
   170 		<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>
   199 
       
   200 		<p>
       
   201 			<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:' ); ?></label>
       
   202 			<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" />
       
   203 		</p>
   171 		<?php
   204 		<?php
   172 	}
   205 	}
   173 
   206 
   174 	/**
   207 	/**
   175 	 * Flushes the Recent Comments widget cache.
   208 	 * Flushes the Recent Comments widget cache.