BP_REST_Groups_Details_Endpoint::get_item( WP_REST_Request $request )

Retrieve groups detail.

Description

Parameters

$request

(Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error List of groups object data.

Source

File: bp-groups/classes/class-bp-rest-groups-details-endpoint.php

	public function get_item( $request ) {

		global $bp;

		$retval = array();
		$group  = $this->groups_endpoint->get_group_object( $request );

		if ( empty( $group->id ) ) {
			$retval = new WP_Error(
				'bp_rest_group_invalid_id',
				__( 'Invalid group ID.', 'buddyboss' ),
				array(
					'status' => 404,
				)
			);
		}

		/**
		 * Store temporary variable
		 */
		$url     = ( '/' . bp_get_groups_root_slug() . '/' . bp_get_group_slug( $group ) );
		$tempurl = ( ! empty( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '' );
		$tmp_bp  = $bp;

		/**
		 * Group navigation tabs creation start
		 *
		 * Groups Navigation tab only setup on group page request so for fetch group tabs we need set group url as `REQUEST_URI` forcefully and
		 * Once our job done switch back to original url.
		 * With below process BuddyPress state might be change so we need to rest it once our job done.
		 *
		 * After set Group url forcefully we need to re-execute core hook which load component and setup tabs for given group.
		 */
		$_SERVER['REQUEST_URI'] = $url;

		// Fixes for the phpunit.
		add_filter( 'bp_loggedin_user_id', array( $this, 'bp_rest_get_displayed_user' ), 999 );
		add_filter( 'bp_displayed_user_id', array( $this, 'bp_rest_get_displayed_user' ), 999 );

		remove_action( 'bp_init', 'bp_register_taxonomies', 2 );
		remove_action( 'bp_init', 'bp_register_post_types', 2 );
		remove_action( 'bp_init', 'bp_setup_title', 8 );
		remove_action( 'bp_init', 'bp_core_load_admin_bar_css', 12 );
		remove_action( 'bp_init', 'bp_add_rewrite_tags', 20 );
		remove_action( 'bp_init', 'bp_add_rewrite_rules', 30 );
		remove_action( 'bp_init', 'bp_add_permastructs', 40 );
		remove_action( 'bp_init', 'bp_init_background_updater', 50 );
		remove_all_actions( 'bp_actions' );

		do_action( 'bp_init' );
		// phpcs:ignore
		do_action( 'bp_ld_sync/init' ); // We should remove when platform load learndash extention on bp_init.
		do_action( 'bp_actions' );

		add_action( 'bp_init', 'bp_register_taxonomies', 2 );
		add_action( 'bp_init', 'bp_register_post_types', 2 );
		add_action( 'bp_init', 'bp_setup_title', 8 );
		add_action( 'bp_init', 'bp_core_load_admin_bar_css', 12 );
		add_action( 'bp_init', 'bp_add_rewrite_tags', 20 );
		add_action( 'bp_init', 'bp_add_rewrite_rules', 30 );
		add_action( 'bp_init', 'bp_add_permastructs', 40 );
		add_action( 'bp_init', 'bp_init_background_updater', 50 );

		$group_slug = $group->slug;

		$group_nav = buddypress()->groups->nav;
		bp_nouveau_set_nav_item_order( $group_nav, bp_nouveau_get_appearance_settings( 'group_nav_order' ), $group_slug );

		$navigation  = array();
		$default_tab = 'members';

		if ( function_exists( 'bp_nouveau_get_appearance_settings' ) ) {
			$default_tab = bp_nouveau_get_appearance_settings( 'group_default_tab' );
		}

		$nav_items = $group_nav->get_secondary(
			array(
				'parent_slug'     => $group_slug,
				'user_has_access' => true,
			)
		);

		if ( ! empty( $nav_items ) ) {
			foreach ( $nav_items as $nav ) {
				$nav = $nav->getArrayCopy();

				if ( 'public' !== $group->status && $nav['slug'] === 'courses' && ( ! groups_is_user_member( bp_loggedin_user_id(), $group->id ) && ! bp_current_user_can( 'bp_moderate' ) ) ) {
					continue;
				}

				$name = $nav['name'];
				$id   = $nav['slug'];

				// remove the count numbers.
				$name = preg_replace( '/^(.*)(<(.*)<\/(.*)>)/', '$1', $name );
				$name = trim( $name );

				$tab = array(
					'id'              => $id,
					'title'           => $name,
					'count'           => $this->bp_rest_get_nav_count( $group, $nav ),
					'position'        => $nav['position'],
					'default'         => false,
					'user_has_access' => $nav['user_has_access'],
					'link'            => $nav['link'],
					'children'        => '',
				);

				if ( $default_tab === $nav['slug'] ) {
					$tab['default'] = true;
				}

				$parent_slug = $group_slug;

				if ( 'admin' === $nav['slug'] ) {
					$parent_slug .= '_manage';
				} elseif ( 'invite' === $nav['slug'] ) {
					$parent_slug .= '_invite';
				} elseif ( 'photos' === $nav['slug'] ) {
					$parent_slug .= '_media';
				} elseif ( 'members' === $nav['slug'] ) {
					$parent_slug .= '_members';
				}

				$sub_navs = array();

				if ( $group_slug !== $parent_slug ) {
					$sub_items = $group_nav->get_secondary(
						array(
							'parent_slug'     => $parent_slug,
							'user_has_access' => true,
						)
					);

					if ( ! empty( $sub_items ) ) {
						foreach ( $sub_items as $sub_nav ) {
							$sub_nav = $sub_nav->getArrayCopy();

							$sub_name = $sub_nav['name'];
							$sub_id   = $sub_nav['slug'];

							// remove the count numbers.
							$sub_name = preg_replace( '/^(.*)(<(.*)<\/(.*)>)/', '$1', $sub_name );
							$sub_name = trim( $sub_name );

							$sub_navs[] = array(
								'id'              => $sub_id,
								'title'           => $sub_name,
								'count'           => $this->bp_rest_get_nav_count( $group, $sub_nav ),
								'position'        => $sub_nav['position'],
								'default'         => false,
								'user_has_access' => $sub_nav['user_has_access'],
								'link'            => $sub_nav['link'],
								'children'        => '',
							);
						}
					}
				}

				$tab['children'] = $sub_navs;
				$navigation[]    = apply_filters( 'bp_rest_group_tab_' . $id, $tab, $nav );
			}
		}

		$retval['tabs'] = $navigation;

		// Fixes for the phpunit.
		remove_filter( 'bp_displayed_user_id', array( $this, 'bp_rest_get_displayed_user' ), 999 );
		remove_filter( 'bp_loggedin_user_id', array( $this, 'bp_rest_get_displayed_user' ), 999 );

		/**
		 * Group navigation tabs creation End
		 *
		 * Switching back to original `REQUEST_URI` and BuddyPress stat.
		 */
		$_SERVER['REQUEST_URI'] = $tempurl;
		$bp                     = $tmp_bp;

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after a list of groups details is fetched via the REST API.
		 *
		 * @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_groups_get_item', $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.