BP_REST_Forums_Endpoint::prepare_item_for_response( array $forum, WP_REST_Request $request )

Prepares component data for return as an object.

Description

Parameters

$forum

(array) (Required) The component and its values.

$request

(WP_REST_Request) (Required) Full details about the request.

Return

(WP_REST_Response)

Source

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

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

		// Base fields for every post.
		$data = array(
			'id'                 => $forum->ID,
			'date'               => $this->prepare_date_response( $forum->post_date_gmt, $forum->post_date ),
			'date_gmt'           => $this->prepare_date_response( $forum->post_date_gmt ),
			'guid'               => array(
				'rendered' => apply_filters( 'get_the_guid', $forum->guid ),
				'raw'      => $forum->guid,
			),
			'modified'           => $this->prepare_date_response( $forum->post_modified_gmt, $forum->post_modified ),
			'modified_gmt'       => $this->prepare_date_response( $forum->post_modified_gmt ),
			'password'           => $forum->post_password,
			'slug'               => $forum->post_name,
			'status'             => $forum->post_status,
			'link'               => bbp_get_forum_permalink( $forum->ID ),
			'author'             => (int) $forum->post_author,
			'parent'             => (int) $forum->post_parent,
			'menu_order'         => (int) $forum->menu_order,
			'sticky'             => is_sticky( $forum->ID ),
			'featured_media'     => array(),
			'total_topic_count'  => (int) get_post_meta( $forum->ID, '_bbp_total_topic_count', true ),
			'last_topic_id'      => (int) get_post_meta( $forum->ID, '_bbp_last_topic_id', true ),
			'total_reply_count'  => (int) get_post_meta( $forum->ID, '_bbp_total_reply_count', true ),
			'last_reply_id'      => (int) get_post_meta( $forum->ID, '_bbp_last_reply_id', true ),
			'last_active_author' => $this->get_forum_last_active_author_id( $forum->ID ),
			'last_active_time'   => $this->get_forum_last_active_time( $forum->ID ),
			'is_closed'          => bbp_is_forum_closed( $forum->ID ),
			'is_forum_category'  => bbp_is_forum_category( $forum->ID ),
		);

		$data['featured_media']['full'] = (
			function_exists( 'bbp_get_forum_thumbnail_src' )
			? bbp_get_forum_thumbnail_src( $forum->ID, 'full', 'full' )
			: get_the_post_thumbnail_url( $forum->ID, 'full' )
		);

		$data['featured_media']['thumb'] = (
			function_exists( 'bbp_get_forum_thumbnail_src' )
			? bbp_get_forum_thumbnail_src( $forum->ID, 'large', 'large' )
			: get_the_post_thumbnail_url( $forum->ID, 'large' )
		);

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

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

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

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

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

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

		$data['group'] = (
			(
				function_exists( 'bbp_is_forum_group_forum' )
				&& bbp_is_forum_group_forum( $forum->ID )
				&& function_exists( 'groups_get_group' )
			)
			? $this->bp_rest_get_group( $forum->ID )
			: ''
		);

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

		// current user permission.
		$data['user_permission'] = $this->get_forum_current_user_permissions( $forum->ID );

		$data['sub_forums'] = $this->get_sub_forums(
			array(
				'post_parent'    => $forum->ID,
				'posts_per_page' => - 1,
			)
		);

		$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( $forum ) );

		/**
		 * 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, $forum );
	}

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.