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

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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.