函数文档

_wp_ajax_delete_comment_response()

💡 云策文档标注

概述

_wp_ajax_delete_comment_response() 是一个 WordPress Ajax 响应函数,用于在删除或修改评论后,返回当前评论总数和更新后的分页链接。它根据传入的参数和 POST 数据,生成包含评论状态、计数和国际化文本的 WP_Ajax_Response 对象。

关键要点

  • 函数接收 $comment_id 和 $delta 参数,用于处理评论 ID 和评论总数的增量变化。
  • 从 $_POST 中提取 _total、_per_page、_page 和 _url 参数,用于计算分页和链接。
  • 如果 POST 数据不完整,函数会返回一个包含评论状态、链接和站点评论计数的简化响应。
  • 如果 POST 数据完整,函数会更新评论总数,计算总页数,并返回包含详细信息的响应。
  • 响应通过 WP_Ajax_Response 对象发送,包含 supplemental 数组,提供如 status、postId、total_items_i18n 等数据。
  • 函数内部使用了多个 WordPress 核心函数,如 get_comment()、wp_count_comments() 和 sanitize_url()。

代码示例

function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) {
    $total    = isset( $_POST['_total'] ) ? (int) $_POST['_total'] : 0;
    $per_page = isset( $_POST['_per_page'] ) ? (int) $_POST['_per_page'] : 0;
    $page     = isset( $_POST['_page'] ) ? (int) $_POST['_page'] : 0;
    $url      = isset( $_POST['_url'] ) ? sanitize_url( $_POST['_url'] ) : '';

    // JS didn't send us everything we need to know. Just die with success message.
    if ( ! $total || ! $per_page || ! $page || ! $url ) {
        $time           = time();
        $comment        = get_comment( $comment_id );
        $comment_status = '';
        $comment_link   = '';

        if ( $comment ) {
            $comment_status = $comment->comment_approved;
        }

        if ( 1 === (int) $comment_status ) {
            $comment_link = get_comment_link( $comment );
        }

        $counts = wp_count_comments();

        $x = new WP_Ajax_Response(
            array(
                'what'         => 'comment',
                // Here for completeness - not used.
                'id'           => $comment_id,
                'supplemental' => array(
                    'status'               => $comment_status,
                    'postId'               => $comment ? $comment->comment_post_ID : '',
                    'time'                 => $time,
                    'in_moderation'        => $counts->moderated,
                    'i18n_comments_text'   => sprintf(
                        /* translators: %s: Number of comments. */
                        _n( '%s Comment', '%s Comments', $counts->approved ),
                        number_format_i18n( $counts->approved )
                    ),
                    'i18n_moderation_text' => sprintf(
                        /* translators: %s: Number of comments. */
                        _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ),
                        number_format_i18n( $counts->moderated )
                    ),
                    'comment_link'         => $comment_link,
                ),
            )
        );
        $x->send();
    }

    $total += $delta;
    if ( $total < 1 ) {
        $total = 0;
    }

    if ( $comment_id ) {
        $comment_count = wp_count_comments( $comment->comment_post_ID );
        foreach ( array( 'approved', 'moderated', 'spam', 'trash' ) as $status ) {
            if ( $total > $comment_count->$status ) {
                $total = $comment_count->$status;
            }
        }
        // Else use the decremented value from above.
    }

    // The time since the last comment count.
    $time    = time();
    $comment = get_comment( $comment_id );
    $counts  = wp_count_comments();

    $x = new WP_Ajax_Response(
        array(
            'what'         => 'comment',
            'id'           => $comment_id,
            'supplemental' => array(
                'status'               => $comment ? $comment->comment_approved : '',
                'postId'               => $comment ? $comment->comment_post_ID : '',
                /* translators: %s: Number of comments. */
                'total_items_i18n'     => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ),
                'total_pages'          => (int) ceil( $total / $per_page ),
                'total_pages_i18n'     => number_format_i18n( (int) ceil( $total / $per_page ) ),
                'total'                => $total,
                'time'                 => $time,
                'in_moderation'        => $counts->moderated,
                'i18n_moderation_text' => sprintf(
                    /* translators: %s: Number of comments. */
                    _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ),
                    number_format_i18n( $counts->moderated )
                ),
            ),
        )
    );
    $x->send();
}

📄 原文内容

Sends back current comment total and new page links if they need to be updated.

Description

Contrary to normal success Ajax response (“1”), die with time() on success.

Parameters

$comment_idintrequired
$deltaintoptional

Default:-1

Source

function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) {
	$total    = isset( $_POST['_total'] ) ? (int) $_POST['_total'] : 0;
	$per_page = isset( $_POST['_per_page'] ) ? (int) $_POST['_per_page'] : 0;
	$page     = isset( $_POST['_page'] ) ? (int) $_POST['_page'] : 0;
	$url      = isset( $_POST['_url'] ) ? sanitize_url( $_POST['_url'] ) : '';

	// JS didn't send us everything we need to know. Just die with success message.
	if ( ! $total || ! $per_page || ! $page || ! $url ) {
		$time           = time();
		$comment        = get_comment( $comment_id );
		$comment_status = '';
		$comment_link   = '';

		if ( $comment ) {
			$comment_status = $comment->comment_approved;
		}

		if ( 1 === (int) $comment_status ) {
			$comment_link = get_comment_link( $comment );
		}

		$counts = wp_count_comments();

		$x = new WP_Ajax_Response(
			array(
				'what'         => 'comment',
				// Here for completeness - not used.
				'id'           => $comment_id,
				'supplemental' => array(
					'status'               => $comment_status,
					'postId'               => $comment ? $comment->comment_post_ID : '',
					'time'                 => $time,
					'in_moderation'        => $counts->moderated,
					'i18n_comments_text'   => sprintf(
						/* translators: %s: Number of comments. */
						_n( '%s Comment', '%s Comments', $counts->approved ),
						number_format_i18n( $counts->approved )
					),
					'i18n_moderation_text' => sprintf(
						/* translators: %s: Number of comments. */
						_n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ),
						number_format_i18n( $counts->moderated )
					),
					'comment_link'         => $comment_link,
				),
			)
		);
		$x->send();
	}

	$total += $delta;
	if ( $total < 0 ) {
		$total = 0;
	}

	// Only do the expensive stuff on a page-break, and about 1 other time per page.
	if ( 0 === $total % $per_page || 1 === mt_rand( 1, $per_page ) ) {
		$post_id = 0;
		// What type of comment count are we looking for?
		$status = 'all';
		$parsed = parse_url( $url );

		if ( isset( $parsed['query'] ) ) {
			parse_str( $parsed['query'], $query_vars );

			if ( ! empty( $query_vars['comment_status'] ) ) {
				$status = $query_vars['comment_status'];
			}

			if ( ! empty( $query_vars['p'] ) ) {
				$post_id = (int) $query_vars['p'];
			}

			if ( ! empty( $query_vars['comment_type'] ) ) {
				$type = $query_vars['comment_type'];
			}
		}

		if ( empty( $type ) ) {
			// Only use the comment count if not filtering by a comment_type.
			$comment_count = wp_count_comments( $post_id );

			// We're looking for a known type of comment count.
			if ( isset( $comment_count->$status ) ) {
				$total = $comment_count->$status;
			}
		}
		// Else use the decremented value from above.
	}

	// The time since the last comment count.
	$time    = time();
	$comment = get_comment( $comment_id );
	$counts  = wp_count_comments();

	$x = new WP_Ajax_Response(
		array(
			'what'         => 'comment',
			'id'           => $comment_id,
			'supplemental' => array(
				'status'               => $comment ? $comment->comment_approved : '',
				'postId'               => $comment ? $comment->comment_post_ID : '',
				/* translators: %s: Number of comments. */
				'total_items_i18n'     => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ),
				'total_pages'          => (int) ceil( $total / $per_page ),
				'total_pages_i18n'     => number_format_i18n( (int) ceil( $total / $per_page ) ),
				'total'                => $total,
				'time'                 => $time,
				'in_moderation'        => $counts->moderated,
				'i18n_moderation_text' => sprintf(
					/* translators: %s: Number of comments. */
					_n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ),
					number_format_i18n( $counts->moderated )
				),
			),
		)
	);
	$x->send();
}

Changelog

Version Description
2.7.0 Introduced.