函数文档

_get_comment_reply_id()

💡 云策文档标注

概述

_get_comment_reply_id() 函数用于从 $_GET['replytocom'] 中获取评论的回复目标 ID,并进行验证以确保其有效性和与指定文章的匹配。

关键要点

  • 函数从 $_GET['replytocom'] 获取回复目标 ID,并验证其为数字类型。
  • 通过 get_comment() 获取评论对象,检查评论是否存在、是否已批准,以及其 comment_post_ID 是否与给定文章 ID 匹配。
  • 如果验证失败,函数返回 0;否则返回有效的回复目标 ID。

代码示例

function _get_comment_reply_id( $post = null ) {
    $post = get_post( $post );

    if ( ! $post || ! isset( $_GET['replytocom'] ) || ! is_numeric( $_GET['replytocom'] ) ) {
        return 0;
    }

    $reply_to_id = (int) $_GET['replytocom'];

    /*
     * Validate the comment.
     * Bail out if it does not exist, is not approved, or its
     * `comment_post_ID` does not match the given post ID.
     */
    $comment = get_comment( $reply_to_id );

    if (
        ! $comment instanceof WP_Comment ||
        0 === (int) $comment->comment_approved ||
        $post->ID !== (int) $comment->comment_post_ID
    ) {
        return 0;
    }

    return $reply_to_id;
}

注意事项

  • 函数依赖于 $_GET['replytocom'] 参数,需确保其在请求中正确传递。
  • 验证过程包括检查评论状态和文章匹配,以避免无效或未授权的回复。
  • 相关函数包括 get_post()、get_comment()、get_cancel_comment_reply_link()、get_comment_id_fields() 和 comment_form_title()。
  • 此函数自 WordPress 6.2.0 版本引入。

📄 原文内容

Gets the comment’s reply to ID from the $_GET[‘replytocom’].

Parameters

$postint|WP_Postoptional
The post the comment is being displayed for.
Defaults to the current global post.

Default:null

Return

int Comment’s reply to ID.

Source

function _get_comment_reply_id( $post = null ) {
	$post = get_post( $post );

	if ( ! $post || ! isset( $_GET['replytocom'] ) || ! is_numeric( $_GET['replytocom'] ) ) {
		return 0;
	}

	$reply_to_id = (int) $_GET['replytocom'];

	/*
	 * Validate the comment.
	 * Bail out if it does not exist, is not approved, or its
	 * `comment_post_ID` does not match the given post ID.
	 */
	$comment = get_comment( $reply_to_id );

	if (
		! $comment instanceof WP_Comment ||
		0 === (int) $comment->comment_approved ||
		$post->ID !== (int) $comment->comment_post_ID
	) {
		return 0;
	}

	return $reply_to_id;
}

Changelog

Version Description
6.2.0 Introduced.