BP_REST_Topics_Endpoint::prepare_item_for_response( array $topic, WP_REST_Request $request )

Prepares component data for return as an object.

Description

Parameters

$topic

(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-topics-endpoint.php

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

		// Base fields for every post.
		$data = array(
			'id'                    => $topic->ID,
			'date'                  => $this->forum_endpoint->prepare_date_response( $topic->post_date_gmt, $topic->post_date ),
			'date_gmt'              => $this->forum_endpoint->prepare_date_response( $topic->post_date_gmt ),
			'guid'                  => array(
				'rendered' => esc_url( bbp_get_topic_permalink( $topic->ID ) ),
				'raw'      => $topic->guid,
			),
			'modified'              => $this->forum_endpoint->prepare_date_response( $topic->post_modified_gmt, $topic->post_modified ),
			'modified_gmt'          => $this->forum_endpoint->prepare_date_response( $topic->post_modified_gmt ),
			'password'              => $topic->post_password,
			'slug'                  => $topic->post_name,
			'status'                => $topic->post_status,
			'link'                  => bbp_get_topic_permalink( $topic->ID ),
			'author'                => (int) $topic->post_author,
			'parent'                => (int) $topic->post_parent,
			'sticky'                => bbp_is_topic_sticky( $topic->ID ),
			'total_reply_count'     => ( bbp_show_lead_topic() ? bbp_get_topic_reply_count( $topic->ID ) : bbp_get_topic_post_count( $topic->ID ) ),
			'last_reply_id'         => bbp_get_topic_last_reply_id( $topic->ID ),
			'last_active_author'    => bbp_get_topic_last_active_id( $topic->ID ),
			'last_active_time'      => $this->forum_endpoint->bbp_rest_get_topic_last_active_time( $topic->ID ),
			'is_closed'             => bbp_is_topic_closed( $topic->ID ),
			'voice_count'           => (int) get_post_meta( $topic->ID, '_bbp_voice_count', true ),
			'forum_id'              => (int) bbp_get_topic_forum_id( $topic->ID ),
			'is_topic_anonymous'    => (int) bbp_is_topic_anonymous( $topic->ID ),
			'anonymous_author_data' => (
			bbp_is_topic_anonymous( $topic->ID )
				? array(
					'name'    => bbp_get_topic_author_display_name( $topic->ID ),
					'email'   => bbp_get_topic_author_email( $topic->ID ),
					'website' => bbp_get_topic_author_url( $topic->ID ),
					'avatar'  => get_avatar_url( bbp_get_topic_author_email( $topic->ID ) ),
				)
				: false
			),
			'classes'               => bbp_get_topic_class( $topic->ID ),
		);

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

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

		$data['short_content'] = wp_trim_excerpt( $topic->post_content );

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

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

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

		$data['group'] = (
		(
			function_exists( 'bbp_is_forum_group_forum' )
			&& bbp_get_topic_forum_id( $topic->ID )
			&& bbp_is_forum_group_forum( bbp_get_topic_forum_id( $topic->ID ) )
			&& function_exists( 'groups_get_group' )
		)
			? (
		! empty( bbp_get_forum_group_ids( bbp_get_topic_forum_id( $topic->ID ) ) )
			? groups_get_group( current( bbp_get_forum_group_ids( bbp_get_topic_forum_id( $topic->ID ) ) ) )
			: ''
		)
			: ''
		);

		// Setup subscribe/unsubscribe state.
		$data['action_states'] = $this->get_topic_action_states( $topic->ID );

		$data['topic_tags'] = $this->get_topic_tags( $topic->ID );

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

		// Revisions.
		$data['revisions'] = $this->get_topic_revisions( $topic->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( $topic ) );

		/**
		 * 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_topic_prepare_value', $response, $request, $topic );
	}

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.