WP_REST_Comments_Controller::prepare_item_for_response( WP_Comment $item, WP_REST_Request $request ): WP_REST_Response

Prepares a single comment output for response.

Parameters

$itemWP_Commentrequired
Comment object.
$requestWP_REST_Requestrequired
Request object.

Return

WP_REST_Response Response object.

Source

public function prepare_item_for_response( $item, $request ) {
	// Restores the more descriptive, specific name for use within this method.
	$comment = $item;

	$fields = $this->get_fields_for_response( $request );
	$data   = array();

	if ( in_array( 'id', $fields, true ) ) {
		$data['id'] = (int) $comment->comment_ID;
	}

	if ( in_array( 'post', $fields, true ) ) {
		$data['post'] = (int) $comment->comment_post_ID;
	}

	if ( in_array( 'parent', $fields, true ) ) {
		$data['parent'] = (int) $comment->comment_parent;
	}

	if ( in_array( 'author', $fields, true ) ) {
		$data['author'] = (int) $comment->user_id;
	}

	if ( in_array( 'author_name', $fields, true ) ) {
		$data['author_name'] = $comment->comment_author;
	}

	if ( in_array( 'author_email', $fields, true ) ) {
		$data['author_email'] = $comment->comment_author_email;
	}

	if ( in_array( 'author_url', $fields, true ) ) {
		$data['author_url'] = $comment->comment_author_url;
	}

	if ( in_array( 'author_ip', $fields, true ) ) {
		$data['author_ip'] = $comment->comment_author_IP;
	}

	if ( in_array( 'author_user_agent', $fields, true ) ) {
		$data['author_user_agent'] = $comment->comment_agent;
	}

	if ( in_array( 'date', $fields, true ) ) {
		$data['date'] = mysql_to_rfc3339( $comment->comment_date );
	}

	if ( in_array( 'date_gmt', $fields, true ) ) {
		$data['date_gmt'] = mysql_to_rfc3339( $comment->comment_date_gmt );
	}

	if ( in_array( 'content', $fields, true ) ) {
		$data['content'] = array(
			/** This filter is documented in wp-includes/comment-template.php */
			'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ),
			'raw'      => $comment->comment_content,
		);
	}

	if ( in_array( 'link', $fields, true ) ) {
		$data['link'] = get_comment_link( $comment );
	}

	if ( in_array( 'status', $fields, true ) ) {
		$data['status'] = $this->prepare_status_response( $comment->comment_approved );
	}

	if ( in_array( 'type', $fields, true ) ) {
		$data['type'] = get_comment_type( $comment->comment_ID );
	}

	if ( in_array( 'author_avatar_urls', $fields, true ) ) {
		$data['author_avatar_urls'] = rest_get_avatar_urls( $comment );
	}

	if ( in_array( 'meta', $fields, true ) ) {
		$data['meta'] = $this->meta->get_value( $comment->comment_ID, $request );
	}

	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
	$data    = $this->add_additional_fields_to_object( $data, $request );
	$data    = $this->filter_response_by_context( $data, $context );

	// Wrap the data in a response object.
	$response = rest_ensure_response( $data );

	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
		$response->add_links( $this->prepare_links( $comment ) );
	}

	/**
	 * Filters a comment returned from the REST API.
	 *
	 * Allows modification of the comment right before it is returned.
	 *
	 * @since 4.7.0
	 *
	 * @param WP_REST_Response  $response The response object.
	 * @param WP_Comment        $comment  The original comment object.
	 * @param WP_REST_Request   $request  Request used to generate the response.
	 */
	return apply_filters( 'rest_prepare_comment', $response, $comment, $request );
}

Hooks

apply_filters( ‘comment_text’, string $comment_text, WP_Comment|null $comment, array $args )

Filters the text of a comment to be displayed.

apply_filters( ‘rest_prepare_comment’, WP_REST_Response $response, WP_Comment $comment, WP_REST_Request $request )

Filters a comment returned from the REST API.

Changelog

VersionDescription
5.9.0Renamed $comment to $item to match parent class for PHP 8 named parameter support.
4.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.