BP_REST_Groups_Types_Endpoint
Groups Type endpoints.
Description
Use /groups/types
Source
File: bp-groups/classes/class-bp-rest-groups-types-endpoint.php
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | class BP_REST_Groups_Types_Endpoint extends WP_REST_Controller { /** * Constructor. * * @since 0.1.0 */ public function __construct() { $this -> namespace = bp_rest_namespace() . '/' . bp_rest_version(); $this ->rest_base = buddypress()->groups->id . '/types' ; } /** * Register the component routes. * * @since 0.1.0 */ public function register_routes() { register_rest_route( $this -> namespace , '/' . $this ->rest_base, array ( array ( 'methods' => WP_REST_Server::READABLE, 'callback' => array ( $this , 'get_items' ), 'permission_callback' => array ( $this , 'get_items_permissions_check' ), 'args' => $this ->get_collection_params(), ), 'schema' => array ( $this , 'get_item_schema' ), ) ); } /** * Retrieve Groups Types. * * @param WP_REST_Request $request Full data about the request. * * @return WP_REST_Response * @since 0.1.0 * * @api {GET} /wp-json/buddyboss/v1/groups/types Groups Types * @apiName GetBBGroupsTypes * @apiGroup Groups * @apiDescription Retrieve Groups Types. * @apiVersion 1.0.0 * @apiPermission LoggedInUser if the site is in Private Network. */ public function get_items( $request ) { $registered_types = bp_groups_get_group_types( array (), 'objects' ); $retval = array (); if ( ! empty ( $registered_types ) ) { foreach ( $registered_types as $type ) { $retval [] = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $type , $request ) ); } } $response = rest_ensure_response( $retval ); /** * Fires after a list of types are fetched via the REST API. * * @param array $registered_types Fetched groups types * @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_types_get_items' , $registered_types , $response , $request ); return $response ; } /** * Check if a given request has access to Groups Types. * * @param WP_REST_Request $request Full data about the request. * * @return bool * @since 0.1.0 */ public function get_items_permissions_check( $request ) { $retval = true; if ( function_exists( 'bp_enable_private_network' ) && true !== bp_enable_private_network() && ! is_user_logged_in() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, Restrict access to only logged-in members.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( function_exists( 'bp_disable_group_type_creation' ) && false === bp_disable_group_type_creation() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, Group Type is disabled from setting.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the Groups types `get_items` permissions check. * * @param bool $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_groups_types_get_items_permissions_check' , $retval , $request ); } /** * Prepares single Groups type to return as an object. * * @param array $type Groups Type. * @param WP_REST_Request $request Full data about the request. * * @return WP_REST_Response * @since 0.1.0 */ public function prepare_item_for_response( $type , $request ) { $data = array ( 'labels' => array ( 'name' => ( isset( $type ->labels[ 'name' ] ) && ! empty ( $type ->labels[ 'name' ] ) ) ? $type ->labels[ 'name' ] : '' , 'singular_name' => ( isset( $type ->labels[ 'singular_name' ] ) && ! empty ( $type ->labels[ 'singular_name' ] ) ) ? $type ->labels[ 'singular_name' ] : '' , ), 'name' => ( isset( $type ->name ) ? $type ->name : '' ), 'description' => ( isset( $type ->description ) ? $type ->description : '' ), 'directory_slug' => ( isset( $type ->directory_slug ) ? $type ->directory_slug : '' ), 'has_directory' => ( isset( $type ->has_directory ) ? $type ->has_directory : false ), 'show_in_create_screen' => ( isset( $type ->show_in_create_screen ) ? $type ->show_in_create_screen : '' ), 'show_in_list' => ( isset( $type ->show_in_list ) ? $type ->show_in_list : '' ), 'create_screen_checked' => ( isset( $type ->create_screen_checked ) ? $type ->create_screen_checked : '' ), ); // Get the edit schema. $schema = $this ->get_item_schema(); $schema = $schema [ 'properties' ]; $post_id = $this ->bp_rest_group_type_post_by_type( $data [ 'name' ] ); // Define default visibility property. if ( isset( $schema [ 'enable_filter' ] ) ) { if ( ! empty ( $post_id ) ) { $data [ 'enable_filter' ] = ( ! empty ( get_post_meta( $post_id , '_bp_group_type_enable_filter' , true ) ) ? get_post_meta( $post_id , '_bp_group_type_enable_filter' , true ) : '' ); } else { $data [ 'enable_filter' ] = false; } } if ( isset( $schema [ 'enable_remove' ] ) ) { if ( ! empty ( $post_id ) ) { $data [ 'enable_remove' ] = ( ! empty ( get_post_meta( $post_id , '_bp_group_type_enable_remove' , true ) ) ? get_post_meta( $post_id , '_bp_group_type_enable_remove' , true ) : '' ); } else { $data [ 'enable_remove' ] = false; } } if ( isset( $schema [ 'restrict-invites-user-same-group-type' ] ) ) { if ( ! empty ( $post_id ) ) { $data [ 'restrict-invites-user-same-group-type' ] = ( ! empty ( get_post_meta( $post_id , '_bp_group_type_restrict_invites_user_same_group_type' , true ) ) ? get_post_meta( $post_id , '_bp_group_type_restrict_invites_user_same_group_type' , true ) : '' ); } else { $data [ 'restrict-invites-user-same-group-type' ] = false; } } if ( isset( $schema [ 'member_type_group_invites' ] ) ) { $member_types = bp_get_member_types( array (), 'names' ); $get_selected_member_types = ( ! empty ( $post_id ) && ! empty ( get_post_meta( $post_id , '_bp_group_type_enabled_member_type_group_invites' , true ) ) ) ? get_post_meta( $post_id , '_bp_group_type_enabled_member_type_group_invites' , true ) : array (); $member_types_invite = array (); if ( ! empty ( $member_types ) ) { foreach ( $member_types as $member_type ) { $member_types_invite [] = array ( 'name' => $member_type , 'selected' => in_array( $member_type , $get_selected_member_types , true ), ); } } $data [ 'member_type_group_invites' ] = $member_types_invite ; } if ( isset( $schema [ 'member_type_join' ] ) ) { $member_types = bp_get_member_types( array (), 'names' ); $get_selected_member_types = ( ! empty ( $post_id ) && ! empty ( get_post_meta( $post_id , '_bp_group_type_enabled_member_type_join' , true ) ) ) ? get_post_meta( $post_id , '_bp_group_type_enabled_member_type_join' , true ) : array (); $member_types_join = array (); if ( ! empty ( $member_types ) ) { foreach ( $member_types as $member_type ) { $member_types_join [] = array ( 'name' => $member_type , 'selected' => in_array( $member_type , $get_selected_member_types , true ), ); } } $data [ 'member_type_join' ] = $member_types_join ; } if ( isset( $schema [ 'group_type_role_labels' ] ) ) { if ( ! empty ( $post_id ) ) { $data [ 'group_type_role_labels' ] = ( ! empty ( get_post_meta( $post_id , '_bp_group_type_role_labels' , true ) ) ? get_post_meta( $post_id , '_bp_group_type_role_labels' , true ) : '' ); } else { $data [ 'group_type_role_labels' ] = array (); } } $response = rest_ensure_response( $data ); /** * Filter the Groups type field 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 $type Xprofile Type * * @since 0.1.0 */ return apply_filters( 'bp_rest_groups_types_prepare_value' , $response , $request , $type ); } /** * Get the Groups types schema, conforming to JSON Schema. * * @return array * @since 0.1.0 */ public function get_item_schema() { $schema = array ( 'title' => 'bp_groups_types' , 'type' => 'object' , 'properties' => array ( 'labels' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'Labels to use in various parts of the interface.' , 'buddyboss' ), 'type' => 'object' , 'arg_options' => array ( 'sanitize_callback' => null, 'validate_callback' => null, ), 'properties' => array ( 'name' => array ( 'description' => __( 'Default name. Should typically be plural.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'singular_name' => array ( 'description' => __( 'Singular name.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), ), ), 'name' => array ( 'description' => __( 'Slug of the group type.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'description' => array ( 'description' => __( 'Description of the group type.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'directory_slug' => array ( 'description' => __( 'Directory slug of the group type.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'has_directory' => array ( 'description' => __( 'Whether the group type should have its own type-specific directory.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'show_in_create_screen' => array ( 'description' => __( 'Whether this group type is allowed to be selected on the group creation page.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'show_in_list' => array ( 'description' => __( 'Whether this group type should be shown in lists.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), 'create_screen_checked' => array ( 'description' => __( 'Whether we should have our group type checkbox checked by default on group create.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ), ), ); $post_types = get_post_types(); if ( ! empty ( $post_types ) && in_array( 'bp-group-type' , $post_types , true ) ) { $schema [ 'properties' ][ 'enable_filter' ] = array ( 'description' => __( 'Display this group type in "Types" filter in Groups Directory.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ); $schema [ 'properties' ][ 'enable_remove' ] = array ( 'description' => __( 'Hide all groups of this type from Groups Directory.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ); $schema [ 'properties' ][ 'restrict-invites-user-same-group-type' ] = array ( 'description' => __( 'If a member is already in a group of this type, they cannot be sent an invite to join another group of this type.' , 'buddyboss' ), 'type' => 'boolean' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ); $schema [ 'properties' ][ 'member_type_group_invites' ] = array ( 'description' => __( 'Only members of the selected profile types may be sent requests to join this group.' , 'buddyboss' ), 'type' => 'object' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ); $schema [ 'properties' ][ 'member_type_join' ] = array ( 'description' => __( 'Members of the selected profile types can always join groups of this type, even if the group is private.' , 'buddyboss' ), 'type' => 'object' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ); $schema [ 'properties' ][ 'group_type_role_labels' ] = array ( 'description' => __( 'Rename the group member roles for groups of this type.' , 'buddyboss' ), 'type' => 'object' , 'context' => array ( 'embed' , 'view' , 'edit' ), 'readonly' => true, ); } /** * Filters the groups types field schema. * * @param array $schema The endpoint schema. */ return apply_filters( 'bp_rest_groups_types_schema' , $schema ); } /** * Get the query params for the Groups types. * * @return array * @since 0.1.0 */ public function get_collection_params() { $params = parent::get_collection_params(); $params [ 'context' ][ 'default' ] = 'view' ; unset( $params [ 'page' ] ); unset( $params [ 'per_page' ] ); unset( $params [ 'search' ] ); /** * Filters the collection query params. * * @param array $params Query params. */ return apply_filters( 'bp_rest_groups_types_collection_params' , $params ); } /** * Get group type post by Group type slug. * * @param string $group_type Group type name. * * @return array */ protected function bp_rest_group_type_post_by_type( $group_type ) { if ( empty ( $group_type ) ) { return ; } $post_id = '' ; $group_type_posts = get_posts( array ( 'name' => $group_type , 'post_type' => 'bp-group-type' , 'post_status' => 'publish' , 'numberposts' => 1, 'fields' => 'ids' , ) ); if ( ! empty ( $group_type_posts ) ) { $post_id = $group_type_posts [0]; } return apply_filters( 'bp_member_type_post_by_type' , $post_id ); } } |
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Methods
- __construct — Constructor.
- bp_rest_group_type_post_by_type — Get group type post by Group type slug.
- get_collection_params — Get the query params for the Groups types.
- get_item_schema — Get the Groups types schema, conforming to JSON Schema.
- get_items — Retrieve Groups Types.
- get_items_permissions_check — Check if a given request has access to Groups Types.
- prepare_item_for_response — Prepares single Groups type to return as an object.
- register_routes — Register the component routes.
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.