函数文档

wp_dashboard_recent_comments()

💡 云策文档标注

概述

wp_dashboard_recent_comments() 是 WordPress 中用于在仪表板显示最近评论部分的函数。它查询并输出指定数量的评论,同时处理用户权限和评论访问控制。

关键要点

  • 函数参数 $total_items 可选,默认查询 5 条评论,返回布尔值表示是否找到评论。
  • 查询逻辑包括性能优化(如初始查询更多评论以过滤垃圾评论)和用户权限检查(如 edit_posts 和 read_post 能力)。
  • 输出部分包括评论列表、相关链接(如“查看更多评论”)和评论管理功能(如回复和垃圾箱通知)。
  • 函数依赖于多个核心函数,如 get_comments()、current_user_can() 和 _wp_dashboard_recent_comments_row()。

代码示例

function wp_dashboard_recent_comments( $total_items = 5 ) {
    // 查询评论的逻辑
    $comments_query = array(
        'number' => $total_items * 5,
        'offset' => 0,
    );
    if ( ! current_user_can( 'edit_posts' ) ) {
        $comments_query['status'] = 'approve';
    }
    // 循环获取评论直到达到指定数量
    do {
        $possible = get_comments( $comments_query );
        // 处理评论并检查用户访问权限
        foreach ( $possible as $comment ) {
            if ( ! current_user_can( 'edit_post', $comment->comment_post_ID )
                && ( post_password_required( $comment->comment_post_ID )
                    || ! current_user_can( 'read_post', $comment->comment_post_ID ) )
            ) {
                continue;
            }
            $comments[] = $comment;
            if ( count( $comments ) === $total_items ) {
                break 2;
            }
        }
        $comments_query['offset'] += $comments_query['number'];
        $comments_query['number'] = $total_items * 10;
    } while ( count( $comments ) < $total_items );
    // 输出评论部分
    if ( ! empty( $comments ) ) {
        echo '<div id="latest-comments" class="activity-block">';
        echo '<h3>' . __( 'Recent Comments' ) . '</h3>';
        echo '<ul id="the-comment-list" data-wp-lists="list:comment">';
        foreach ( $comments as $comment ) {
            _wp_dashboard_recent_comments_row( $comment );
        }
        echo '</ul>';
        if ( current_user_can( 'edit_posts' ) ) {
            echo '<a href="edit-comments.php" class="activity-block__more-link">' . __( 'View more comments' ) . '</a>';
            _get_list_table( 'WP_Comments_List_Table' )->views();
        }
        wp_comment_reply( -1, false, 'dashboard', false );
        wp_comment_trashnotice();
        echo '</div>';
        return true;
    } else {
        return false;
    }
}

注意事项

  • 函数自 WordPress 3.8.0 版本引入,主要用于仪表板小部件,如 wp_dashboard_site_activity()。
  • 用户权限检查是关键部分,确保只有有权限的用户能看到相关评论,避免泄露敏感信息。
  • 查询使用 do-while 循环以高效获取所需数量的评论,同时处理可能的访问限制。

📄 原文内容

Show Comments section.

Parameters

$total_itemsintoptional
Number of comments to query.

Default:5

Return

bool False if no comments were found. True otherwise.

Source

function wp_dashboard_recent_comments( $total_items = 5 ) {
	// Select all comment types and filter out spam later for better query performance.
	$comments = array();

	$comments_query = array(
		'number' => $total_items * 5,
		'offset' => 0,
	);

	if ( ! current_user_can( 'edit_posts' ) ) {
		$comments_query['status'] = 'approve';
	}

	$comments_count = 0;
	do {
		$possible = get_comments( $comments_query );

		if ( empty( $possible ) || ! is_array( $possible ) ) {
			break;
		}

		foreach ( $possible as $comment ) {
			if ( ! current_user_can( 'edit_post', $comment->comment_post_ID )
				&& ( post_password_required( $comment->comment_post_ID )
					|| ! current_user_can( 'read_post', $comment->comment_post_ID ) )
			) {
				// The user has no access to the post and thus cannot see the comments.
				continue;
			}

			$comments[]     = $comment;
			$comments_count = count( $comments );

			if ( $comments_count === $total_items ) {
				break 2;
			}
		}

		$comments_query['offset'] += $comments_query['number'];
		$comments_query['number']  = $total_items * 10;
	} while ( $comments_count < $total_items );

	if ( $comments ) {
		echo '<div id="latest-comments" class="activity-block table-view-list">';
		echo '<h3>' . __( 'Recent Comments' ) . '</h3>';

		echo '<ul id="the-comment-list" data-wp-lists="list:comment">';
		foreach ( $comments as $comment ) {
			_wp_dashboard_recent_comments_row( $comment );
		}
		echo '</ul>';

		if ( current_user_can( 'edit_posts' ) ) {
			echo '<h3 class="screen-reader-text">' .
				/* translators: Hidden accessibility text. */
				__( 'View more comments' ) .
			'</h3>';
			_get_list_table( 'WP_Comments_List_Table' )->views();
		}

		wp_comment_reply( -1, false, 'dashboard', false );
		wp_comment_trashnotice();

		echo '</div>';
	} else {
		return false;
	}
	return true;
}

Changelog

Version Description
3.8.0 Introduced.