BP_REST_Reply_Endpoint::prepare_item_for_response( array $reply, WP_REST_Request $request )

Prepares component data for return as an object.

Description

Parameters

$reply

(Required) The component and its values.

$request

(Required) Full details about the request.

Return

(WP_REST_Response)

Source

File: bp-forums/classes/class-bp-rest-reply-endpoint.php

	public function prepare_item_for_response( $reply, $request ) {
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';

		// Base fields for every post.
		$data = array(
			'id'                    => $reply->ID,
			'date'                  => $this->forum_endpoint->prepare_date_response( $reply->post_date_gmt, $reply->post_date ),
			'date_gmt'              => $this->forum_endpoint->prepare_date_response( $reply->post_date_gmt ),
			'guid'                  => array(
				'rendered' => esc_url( bbp_get_reply_permalink( $reply->ID ) ),
				'raw'      => $reply->guid,
			),
			'modified'              => $this->forum_endpoint->prepare_date_response( $reply->post_modified_gmt, $reply->post_modified ),
			'modified_gmt'          => $this->forum_endpoint->prepare_date_response( $reply->post_modified_gmt ),
			'password'              => $reply->post_password,
			'slug'                  => $reply->post_name,
			'status'                => $reply->post_status,
			'link'                  => bbp_get_reply_permalink( $reply->ID ),
			'author'                => (int) $reply->post_author,
			'parent'                => (int) $reply->post_parent,
			'depth'                 => (int) ( isset( $reply->depth ) && ! empty( $reply->depth ) ) ? $reply->depth : 1,
			'reply_to'              => bbp_get_reply_to( $reply->ID ),
			'is_reply_anonymous'    => (int) bbp_is_reply_anonymous( $reply->ID ),
			'anonymous_author_data' => (
				bbp_is_reply_anonymous( $reply->ID )
				? array(
					'name'    => bbp_get_reply_author_display_name( $reply->ID ),
					'email'   => bbp_get_reply_author_email( $reply->ID ),
					'website' => bbp_get_reply_author_url( $reply->ID ),
					'avatar'  => get_avatar_url( bbp_get_reply_author_email( $reply->ID ) ),
				)
				: false
			),
			'classes'               => bbp_get_reply_class( $reply->ID ),
		);

		$data['title'] = array(
			'raw'      => $reply->post_title,
			'rendered' => bbp_get_reply_title( $reply->ID ),
		);

		/* Prepare content */
		if ( ! empty( $reply->post_password ) ) {
			$this->forum_endpoint->prepare_password_response( $reply->post_password );
		}

		$content = apply_filters( 'the_content', $reply->post_content );

		$data['content'] = array(
			'raw'      => $reply->post_content,
			'rendered' => $content,
		);

		// Don't leave our cookie lying around: https://github.com/WP-API/WP-API/issues/1055.
		if ( ! empty( $reply->post_password ) ) {
			$_COOKIE[ 'wp-postpass_' . COOKIEHASH ] = '';
		}
		/* -- Prepare content */

		// current user permission.
		$data['current_user_permissions'] = $this->get_reply_current_user_permissions( $reply->ID );

		$data['action_states'] = $this->get_reply_action_states( $reply->ID );

		// Revisions.
		$data['revisions'] = $this->get_reply_revisions( $reply->ID );

		$data = $this->add_additional_fields_to_object( $data, $request );
		$data = $this->filter_response_by_context( $data, $context );

		// @todo add prepare_links
		$response = rest_ensure_response( $data );

		$response->add_links( $this->prepare_links( $reply ) );

		/**
		 * Filter a component value returned from the API.
		 *
		 * @param WP_REST_Response $response  The Response data.
		 * @param WP_REST_Request  $request   Request used to generate the response.
		 * @param array            $component The component and its values.
		 *
		 * @since 0.1.0
		 */
		return apply_filters( 'bp_rest_forums_prepare_value', $response, $request, $reply );
	}

Changelog

Changelog
Version Description
0.1.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.