BP_REST_Forums_Endpoint::get_items( WP_REST_Request $request )

Retrieve Forums.

Description

Parameters

$request

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

Return

(WP_REST_Response) | WP_Error

Source

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

	public function get_items( $request ) {

		$args = array(
			'post_parent'    => ( ! empty( $request['parent'] ) ? $request['parent'] : 0 ),
			'post_status'    => ( ! empty( $request['status'] ) ? $request['status'] : bbp_get_public_status_id() ),
			'orderby'        => ( ! empty( $request['orderby'] ) ? $request['orderby'] : 'menu_order title' ),
			'order'          => ( ! empty( $request['order'] ) ? $request['order'] : 'asc' ),
			'paged'          => ( ! empty( $request['page'] ) ? $request['page'] : '' ),
			'posts_per_page' => ( ! empty( $request['per_page'] ) ? $request['per_page'] : bbp_get_forums_per_page() ),
		);

		if ( ! empty( $request['search'] ) ) {
			$args['s'] = $request['search'];
		}

		if ( ! empty( $request['author'] ) ) {
			$args['author'] = $request['author'];
		}

		if ( ! empty( $request['author_exclude'] ) ) {
			$args['author__not_in'] = $request['author_exclude'];
		}

		if ( ! empty( $request['exclude'] ) ) {
			$args['post__not_in'] = $request['exclude'];
		}

		if ( ! empty( $request['include'] ) ) {
			$args['post__in'] = $request['include'];
		}

		if ( ! empty( $request['offset'] ) ) {
			$args['offset'] = $request['offset'];
		}

		if (
			! empty( $args['orderby'] )
			&& is_array( $args['orderby'] )
		) {
			if ( in_array( 'popular', $args['orderby'], true ) ) {
				$args['orderby']  = 'meta_value_num';
				$args['meta_key'] = '_bbp_total_topic_count'; // phpcs:ignore
			} elseif ( in_array( 'activity', $args['orderby'], true ) ) {
				$args['orderby']  = 'meta_value';
				$args['meta_key'] = '_bbp_last_active_time'; // phpcs:ignore
			}
		}

		if ( is_array( $args['orderby'] ) ) {
			$args['orderby'] = implode( ' ', $args['orderby'] );
		}

		/**
		 * Filter the query arguments for the request.
		 *
		 * @param array           $args    Key value array of query var to query value.
		 * @param WP_REST_Request $request The request sent to the API.
		 *
		 * @since 0.1.0
		 */
		$args = apply_filters( 'bp_rest_forums_get_items_query_args', $args, $request );

		$bbp_f = bbp_parse_args(
			$args,
			array(
				'post_type'           => bbp_get_forum_post_type(),
				'ignore_sticky_posts' => true,
			),
			'has_forums'
		);

		if ( isset( $request['subscriptions'] ) && ! empty( $request['subscriptions'] ) ) {
			$user_id = (int) ( isset( $args['author'] ) && ! empty( $args['author'] ) ) ? $args['author'] : bbp_get_current_user_id();

			$subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );
			if ( ! empty( $subscriptions ) ) {
				$bbp_f['post__in'] = $subscriptions;
				if ( isset( $args['author'] ) ) {
					unset( $bbp_f['author'] );
				}
			} else {
				$bbp_f = array();
			}
		}

		// Run the query.
		$forums_query = new WP_Query( $bbp_f );

		$forums = ( ! empty( $forums_query->posts ) ? $forums_query->posts : array() );

		$retval = array();
		foreach ( $forums as $forum ) {
			$retval[] = $this->prepare_response_for_collection(
				$this->prepare_item_for_response( $forum, $request )
			);
		}

		$response = rest_ensure_response( $retval );
		$response = bp_rest_response_add_total_headers( $response, $forums_query->found_posts, $args['posts_per_page'] );

		/**
		 * Fires after a list of forums is fetched via the REST API.
		 *
		 * @param array            $forums   Fetched forums.
		 * @param WP_REST_Response $response The response data.
		 * @param WP_REST_Request  $request  The request sent to the API.
		 *
		 * @since 0.1.0
		 */
		do_action( 'bp_rest_forums_get_items', $forums, $response, $request );

		return $response;
	}

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.