_wp_dashboard_recent_comments_row()
概述
_wp_dashboard_recent_comments_row() 函数用于在 WordPress 仪表盘的“最近评论”小部件中输出单行评论信息。它处理评论数据的显示,包括作者、内容、日期和操作链接,并支持通过 Hook 进行自定义。
关键要点
- 函数输出评论行,包含评论详情和操作链接(如批准、编辑、垃圾邮件等)。
- 参数包括必需的 WP_Comment 对象和可选的 $show_date 布尔值(默认 true 以显示日期)。
- 使用 Hook:apply_filters('comment_row_actions', $actions, $comment) 过滤操作链接。
- 依赖多个 WordPress 核心函数,如 get_comment_author_link()、esc_url() 和 wp_create_nonce()。
代码示例
function _wp_dashboard_recent_comments_row( &$comment, $show_date = true ) {
$GLOBALS['comment'] = clone $comment;
// 处理评论帖子的链接
if ( $comment->comment_post_ID > 0 ) {
$comment_post_title = _draft_or_post_title( $comment->comment_post_ID );
$comment_post_url = get_the_permalink( $comment->comment_post_ID );
$comment_post_link = '<a href="' . $comment_post_url . '">' . $comment_post_title . '</a>';
} else {
$comment_post_link = '';
}
// 生成操作链接字符串
$actions_string = '';
if ( current_user_can( 'edit_comment', $comment->comment_ID ) ) {
$actions = array( 'approve' => '', 'unapprove' => '', 'reply' => '', 'edit' => '', 'spam' => '', 'trash' => '', 'delete' => '', 'view' => '' );
// 构建 URL 和链接
$approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( 'approve-comment_' . $comment->comment_ID ) );
$action_string = 'comment.php?action=%s&p;=' . $comment->comment_post_ID . '&c;=' . $comment->comment_ID . '&%s';
$approve_url = sprintf( $action_string, 'approvecomment', $approve_nonce );
// 更多操作链接设置...
$actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );
foreach ( $actions as $action => $link ) {
$actions_string .= $link;
}
}
// 输出 HTML 结构
?>
<div id="comment-<?php echo $comment->comment_ID; ?>" class="comment-item">
<?php if ( 'comment' === $comment->comment_type || 'comment' === $comment->comment_type ) : ?>
<div class="comment-author">
<?php printf( __( 'From %1$s %2$s' ), '<a href="' . get_comment_author_link( $comment ) . '">' . get_comment_author_link( $comment ) . '</a>', $comment_post_link ); ?>
</div>
<?php endif; ?>
<div class="comment-content">
<?php comment_excerpt(); ?>
</div>
<?php if ( $show_date ) : ?>
<div class="comment-date">
<?php printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() ); ?>
</div>
<?php endif; ?>
<div class="comment-actions">
<?php echo $actions_string; ?>
</div>
</div>
<?php
}注意事项
- 函数内部使用 $GLOBALS['comment'] 设置全局变量,需注意避免冲突。
- 操作链接的生成依赖于用户权限(current_user_can('edit_comment')),确保安全控制。
- 通过 comment_row_actions Hook 可自定义操作链接,方便扩展。
- 代码中包含国际化函数(如 __() 和 _x()),确保多语言支持。
Outputs a row for the Recent Comments widget.
Parameters
$commentWP_Commentrequired- The current comment.
$show_datebooloptional- Whether to display the date.
Default:
true
Source
function _wp_dashboard_recent_comments_row( &$comment, $show_date = true ) {
$GLOBALS['comment'] = clone $comment;
if ( $comment->comment_post_ID > 0 ) {
$comment_post_title = _draft_or_post_title( $comment->comment_post_ID );
$comment_post_url = get_the_permalink( $comment->comment_post_ID );
$comment_post_link = '<a href="' . esc_url( $comment_post_url ) . '">' . $comment_post_title . '</a>';
} else {
$comment_post_link = '';
}
$actions_string = '';
if ( current_user_can( 'edit_comment', $comment->comment_ID ) ) {
// Pre-order it: Approve | Reply | Edit | Spam | Trash.
$actions = array(
'approve' => '',
'unapprove' => '',
'reply' => '',
'edit' => '',
'spam' => '',
'trash' => '',
'delete' => '',
'view' => '',
);
$approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( 'approve-comment_' . $comment->comment_ID ) );
$del_nonce = esc_html( '_wpnonce=' . wp_create_nonce( 'delete-comment_' . $comment->comment_ID ) );
$action_string = 'comment.php?action=%s&p;=' . $comment->comment_post_ID . '&c;=' . $comment->comment_ID . '&%s';
$approve_url = sprintf( $action_string, 'approvecomment', $approve_nonce );
$unapprove_url = sprintf( $action_string, 'unapprovecomment', $approve_nonce );
$spam_url = sprintf( $action_string, 'spamcomment', $del_nonce );
$trash_url = sprintf( $action_string, 'trashcomment', $del_nonce );
$delete_url = sprintf( $action_string, 'deletecomment', $del_nonce );
$actions['approve'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-a aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $approve_url ),
"dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved",
esc_attr__( 'Approve this comment' ),
__( 'Approve' )
);
$actions['unapprove'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-u aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $unapprove_url ),
"dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=unapproved",
esc_attr__( 'Unapprove this comment' ),
__( 'Unapprove' )
);
$actions['edit'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
"comment.php?action=editcomment&c={$comment->comment_ID}",
esc_attr__( 'Edit this comment' ),
__( 'Edit' )
);
$actions['reply'] = sprintf(
'<button type="button" onclick="window.commentReply && commentReply.open('%s','%s');" class="vim-r button-link hide-if-no-js" aria-label="%s">%s</button>',
$comment->comment_ID,
$comment->comment_post_ID,
esc_attr__( 'Reply to this comment' ),
__( 'Reply' )
);
$actions['spam'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="vim-s vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $spam_url ),
"delete:the-comment-list:comment-{$comment->comment_ID}::spam=1",
esc_attr__( 'Mark this comment as spam' ),
/* translators: "Mark as spam" link. */
_x( 'Spam', 'verb' )
);
if ( ! EMPTY_TRASH_DAYS ) {
$actions['delete'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $delete_url ),
"delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
esc_attr__( 'Delete this comment permanently' ),
__( 'Delete Permanently' )
);
} else {
$actions['trash'] = sprintf(
'<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
esc_url( $trash_url ),
"delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
esc_attr__( 'Move this comment to the Trash' ),
_x( 'Trash', 'verb' )
);
}
$actions['view'] = sprintf(
'<a class="comment-link" href="%s" aria-label="%s">%s</a>',
esc_url( get_comment_link( $comment ) ),
esc_attr__( 'View this comment' ),
__( 'View' )
);
/** This filter is documented in wp-admin/includes/class-wp-comments-list-table.php */
$actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );
$i = 0;
foreach ( $actions as $action => $link ) {
++$i;
if ( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i )
|| 1 === $i
) {
$separator = '';
} else {
$separator = ' | ';
}
// Reply and quickedit need a hide-if-no-js span.
if ( 'reply' === $action || 'quickedit' === $action ) {
$action .= ' hide-if-no-js';
}
if ( 'view' === $action && '1' !== $comment->comment_approved ) {
$action .= ' hidden';
}
$actions_string .= "<span class='$action'>{$separator}{$link}</span>";
}
}
?>
<li id="comment-<?php echo $comment->comment_ID; ?>" <?php comment_class( array( 'comment-item', wp_get_comment_status( $comment ) ), $comment ); ?>>
comment_type || 'comment' === $comment->comment_type ) : ?>
<div class="dashboard-comment-wrap has-row-actions <?php echo $comment_row_class; ?>">
<p class="comment-meta">
' . get_comment_author_link( $comment ) . '</cite>',
$comment_post_link,
'<span class="approve">' . __( '[Pending]' ) . '</span>'
);
} else {
printf(
/* translators: 1: Comment author, 2: Notification if the comment is pending. */
__( 'From %1$s %2$s' ),
'<cite class="comment-author">' . get_comment_author_link( $comment ) . '</cite>',
'<span class="approve">' . __( '[Pending]' ) . '</span>'
);
}
?>
</p>
comment_type ) {
case 'pingback':
$type = __( 'Pingback' );
break;
case 'trackback':
$type = __( 'Trackback' );
break;
default:
$type = ucwords( $comment->comment_type );
}
$type = esc_html( $type );
?>
<div class="dashboard-comment-wrap has-row-actions">
<p class="comment-meta">
$type</strong>",
$comment_post_link,
'<span class="approve">' . __( '[Pending]' ) . '</span>'
);
} else {
printf(
/* translators: 1: Type of comment, 2: Notification if the comment is pending. */
_x( '%1$s %2$s', 'dashboard' ),
"<strong>$type</strong>",
'<span class="approve">' . __( '[Pending]' ) . '</span>'
);
}
?>
</p>
<p class="comment-author"></p>
<blockquote><p></p></blockquote>
<p class="row-actions"></p>
</div>
</li>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/dashboard.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/dashboard.php#L704">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/dashboard.php#L704-L918">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_row_actions/"><span class="hook-func">apply_filters</span>( ‘comment_row_actions’, <nobr><span class="arg-type">string[]</span> <span class="arg-name">$actions</span></nobr>, <nobr><span class="arg-type">WP_Comment</span> <span class="arg-name">$comment</span></nobr> )</a></dt><dd><p>Filters the action links displayed for each comment in the Comments list table.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/_draft_or_post_title/">_draft_or_post_title()</a><code>wp-admin/includes/template.phpGets the post title.
esc_attr__()wp-includes/l10n.php Retrieves the translation of $text and escapes it for safe use in an attribute.
get_avatar()wp-includes/pluggable.php Retrieves the avatar <img> tag for a user, email address, MD5 hash, comment, or post.
get_the_permalink()wp-includes/link-template.php Retrieves the full permalink for the current post or post ID.
get_comment_link()wp-includes/comment-template.php Retrieves the link to a given comment.
comment_class()wp-includes/comment-template.php Generates semantic classes for each comment element.
comment_excerpt()wp-includes/comment-template.php Displays the excerpt of the current comment.
get_comment_author_link()wp-includes/comment-template.php Retrieves the HTML link to the URL of the author of the current comment.
comment_author_link()wp-includes/comment-template.php Displays the HTML link to the URL of the author of the current comment.
wp_get_comment_status()wp-includes/comment.php Retrieves the status of a comment by comment ID.
current_user_can()wp-includes/capabilities.php Returns whether the current user has the specified capability.
__()wp-includes/l10n.php Retrieves the translation of $text.
_x()wp-includes/l10n.php Retrieves translated string with gettext context.
esc_url()wp-includes/formatting.php Checks and cleans a URL.
esc_html()wp-includes/formatting.php Escaping for HTML blocks.
wp_create_nonce()wp-includes/pluggable.php Creates a cryptographic token tied to a specific action, user, user session, and window of time.
apply_filters()wp-includes/plugin.php Calls the callback functions that have been added to a filter hook.
get_option()wp-includes/option.php Retrieves an option value based on an option name.
Show 13 moreShow less| Used by | Description |
|---|---|
wp_dashboard_recent_comments()wp-admin/includes/dashboard.php | Show Comments section. |
wp_ajax_replyto_comment()wp-admin/includes/ajax-actions.php | Handles replying to a comment via AJAX. |
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.