BP_REST_Attachments_Group_Avatar_Endpoint
Group Avatar endpoints.
Description
Source
File: bp-groups/classes/class-bp-rest-attachments-group-avatar-endpoint.php
16 17 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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | class BP_REST_Attachments_Group_Avatar_Endpoint extends WP_REST_Controller { use BP_REST_Attachments; /** * Reuse some parts of the BP_REST_Groups_Endpoint class. * * @since 0.1.0 * * @var BP_REST_Groups_Endpoint */ protected $groups_endpoint ; /** * BP_Attachment_Avatar Instance. * * @since 0.1.0 * * @var BP_Attachment_Avatar */ protected $avatar_instance ; /** * Hold the group object. * * @since 0.1.0 * * @var BP_Groups_Group */ protected $group ; /** * Group object type. * * @since 0.1.0 * * @var string */ protected $object = 'group' ; /** * Constructor. * * @since 0.1.0 */ public function __construct() { $this -> namespace = bp_rest_namespace() . '/' . bp_rest_version(); $this ->rest_base = buddypress()->groups->id; $this ->groups_endpoint = new BP_REST_Groups_Endpoint(); $this ->avatar_instance = new BP_Attachment_Avatar(); } /** * Register the component routes. * * @since 0.1.0 */ public function register_routes() { register_rest_route( $this -> namespace , '/' . $this ->rest_base . '/(?P<group_id>[\d]+)/avatar' , array ( 'args' => array ( 'group_id' => array ( 'description' => __( 'A unique numeric ID for the Group.' , 'buddyboss' ), 'type' => 'integer' , ), ), array ( 'methods' => WP_REST_Server::READABLE, 'callback' => array ( $this , 'get_item' ), 'permission_callback' => array ( $this , 'get_item_permissions_check' ), 'args' => $this ->get_item_collection_params(), ), array ( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array ( $this , 'create_item' ), 'permission_callback' => array ( $this , 'create_item_permissions_check' ), ), array ( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array ( $this , 'delete_item' ), 'permission_callback' => array ( $this , 'delete_item_permissions_check' ), ), 'schema' => array ( $this , 'get_item_schema' ), ) ); } /** * Fetch an existing group avatar. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {GET} /wp-json/buddyboss/v1/groups/:group_id/avatar Group Avatar * @apiName GetBBGroupAvatar * @apiGroup Groups * @apiDescription Retrieve group avatar * @apiVersion 1.0.0 * @apiPermission LoggedInUser if the site is in Private Network. * @apiParam {Number} group_id A unique numeric ID for the Group. * @apiParam {Boolean} [html=false] Whether to return an <img> HTML element, vs a raw URL to a group avatar. * @apiParam {String} [alt] The alt attribute for the <img> element. */ public function get_item( $request ) { $args = array (); foreach ( array ( 'full' , 'thumb' ) as $type ) { $args [ $type ] = bp_core_fetch_avatar( array ( 'object' => $this ->object, 'type' => $type , 'item_id' => (int) $this ->group->id, 'html' => (bool) $request [ 'html' ], 'alt' => $request [ 'alt' ], ) ); } // Get the avatar object. $avatar = $this ->get_avatar_object( $args ); if ( ! $avatar ->full && ! $avatar ->thumb ) { return new WP_Error( 'bp_rest_attachments_group_avatar_no_image' , __( 'Sorry, there was a problem fetching this group avatar.' , 'buddyboss' ), array ( 'status' => 500, ) ); } $retval = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $avatar , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a group avatar is fetched via the REST API. * * @param string $avatar The group avatar. * @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_attachments_group_avatar_get_item' , $avatar , $response , $request ); return $response ; } /** * Checks if a given request has access to get a group avatar. * * @param WP_REST_Request $request Full details about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function get_item_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(), ) ); } $this ->group = $this ->groups_endpoint->get_group_object( $request ); if ( true === $retval && ! $this ->group ) { $retval = new WP_Error( 'bp_rest_group_invalid_id' , __( 'Invalid group ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } /** * Filter the group avatar `get_item` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_attachments_group_avatar_get_item_permissions_check' , $retval , $request ); } /** * Upload a group avatar. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {POST} /wp-json/buddyboss/v1/groups/:group_id/avatar Create Group Avatar * @apiName CreateBBGroupAvatar * @apiGroup Groups * @apiDescription Create group avatar * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} group_id A unique numeric ID for the Group. * @apiParam {string=bp_avatar_upload} action Action name for upload the group avatar. */ public function create_item( $request ) { $request ->set_param( 'context' , 'edit' ); // Get the image file from $_FILES. $files = $request ->get_file_params(); if ( empty ( $files ) ) { return new WP_Error( 'bp_rest_attachments_group_avatar_no_image_file' , __( 'Sorry, you need an image file to upload.' , 'buddyboss' ), array ( 'status' => 500, ) ); } // Upload the avatar. $avatar = $this ->upload_avatar_from_file( $files ); if ( is_wp_error( $avatar ) ) { return $avatar ; } $retval = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $avatar , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a group avatar is uploaded via the REST API. * * @param stdClass $avatar The group avatar object. * @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_attachments_group_avatar_create_item' , $avatar , $response , $request ); return $response ; } /** * Checks if a given request has access to upload a group avatar. * * @param WP_REST_Request $request Full details about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function create_item_permissions_check( $request ) { $retval = $this ->get_item_permissions_check( $request ); if ( true === $retval && ( bp_disable_group_avatar_uploads() || ! buddypress()->avatar->show_avatars ) ) { $retval = new WP_Error( 'bp_rest_attachments_group_avatar_disabled' , __( 'Sorry, group avatar upload is disabled.' , 'buddyboss' ), array ( 'status' => 500, ) ); } if ( true === $retval && ! groups_is_user_admin( bp_loggedin_user_id(), $this ->group->id ) && ! current_user_can( 'bp_moderate' ) ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you are not authorized to perform this action.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the group avatar `create_item` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_attachments_group_avatar_create_item_permissions_check' , $retval , $request ); } /** * Delete an existing group avatar. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {DELETE} /wp-json/buddyboss/v1/groups/:group_id/avatar Delete Group Avatar * @apiName DeleteBBGroupAvatar * @apiGroup Groups * @apiDescription Delete group avatar * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} group_id A unique numeric ID for the Group. */ public function delete_item( $request ) { $request ->set_param( 'context' , 'edit' ); $group_id = (int) $this ->group->id; if ( ! bp_get_group_has_avatar( $group_id ) ) { return new WP_Error( 'bp_rest_attachments_group_avatar_no_uploaded_avatar' , __( 'Sorry, there are no uploaded avatars for this group on this site.' , 'buddyboss' ), array ( 'status' => 404, ) ); } $args = array (); foreach ( array ( 'full' , 'thumb' ) as $type ) { $args [ $type ] = bp_core_fetch_avatar( array ( 'object' => $this ->object, 'type' => $type , 'item_id' => $group_id , 'html' => false, ) ); } // Get the avatar object before deleting it. $avatar = $this ->get_avatar_object( $args ); $deleted = bp_core_delete_existing_avatar( array ( 'object' => $this ->object, 'item_id' => $group_id , ) ); if ( ! $deleted ) { return new WP_Error( 'bp_rest_attachments_group_avatar_delete_failed' , __( 'Sorry, there was a problem deleting this group avatar.' , 'buddyboss' ), array ( 'status' => 500, ) ); } // Build the response. $response = new WP_REST_Response(); $response ->set_data( array ( 'deleted' => true, 'previous' => $avatar , ) ); /** * Fires after a group avatar is deleted 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_attachments_group_avatar_delete_item' , $response , $request ); return $response ; } /** * Checks if a given request has access to delete a group avatar. * * @param WP_REST_Request $request Full details about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function delete_item_permissions_check( $request ) { $retval = $this ->create_item_permissions_check( $request ); /** * Filter the group avatar `delete_item` permissions check. * * @param bool|WP_Error $retval Returned value. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_attachments_group_avatar_delete_item_permissions_check' , $retval , $request ); } /** * Prepares avatar data to return as an object. * * @param stdClass|string $avatar Avatar object or string with url or image with html. * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response * @since 0.1.0 */ public function prepare_item_for_response( $avatar , $request ) { $data = array ( 'full' => $avatar ->full, 'thumb' => $avatar ->thumb, ); $context = ! empty ( $request [ 'context' ] ) ? $request [ 'context' ] : 'view' ; $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 ); /** * Filter a group avatar value returned from the API. * * @param WP_REST_Response $response Response. * @param WP_REST_Request $request Request used to generate the response. * @param stdClass|string $avatar Avatar object or string with url or image with html. * * @since 0.1.0 */ return apply_filters( 'bp_rest_attachments_group_avatar_prepare_value' , $response , $request , $avatar ); } /** * Get the plugin schema, conforming to JSON Schema. * * @return array * @since 0.1.0 */ public function get_item_schema() { $schema = array ( 'title' => 'bp_attachments_group_avatar' , 'type' => 'object' , 'properties' => array ( 'full' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'Full size of the image file.' , 'buddyboss' ), 'type' => 'string' , 'readonly' => true, ), 'thumb' => array ( 'context' => array ( 'embed' , 'view' , 'edit' ), 'description' => __( 'Thumb size of the image file.' , 'buddyboss' ), 'type' => 'string' , 'readonly' => true, ), ), ); /** * Filters the group avatar schema. * * @param string $schema The endpoint schema. */ return apply_filters( 'bp_rest_attachments_group_avatar_schema' , $this ->add_additional_fields_schema( $schema ) ); } /** * Get the query params for the `get_item`. * * @return array * @since 0.1.0 */ public function get_item_collection_params() { $params = parent::get_collection_params(); $params [ 'context' ][ 'default' ] = 'view' ; // Removing unused params. unset( $params [ 'search' ], $params [ 'page' ], $params [ 'per_page' ] ); $params [ 'html' ] = array ( 'description' => __( 'Whether to return an <img> HTML element, vs a raw URL to a group avatar.' , 'buddyboss' ), 'default' => false, 'type' => 'boolean' , 'sanitize_callback' => 'rest_sanitize_boolean' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'alt' ] = array ( 'description' => __( 'The alt attribute for the <img> element.' , 'buddyboss' ), 'default' => '' , 'type' => 'string' , 'sanitize_callback' => 'sanitize_text_field' , 'validate_callback' => 'rest_validate_request_arg' , ); /** * Filters the item collection query params. * * @param array $params Query params. */ return apply_filters( 'bp_rest_attachments_group_avatar_collection_params' , $params ); } } |
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Methods
- __construct — Constructor.
- create_item — Upload a group avatar.
- create_item_permissions_check — Checks if a given request has access to upload a group avatar.
- delete_item — Delete an existing group avatar.
- delete_item_permissions_check — Checks if a given request has access to delete a group avatar.
- get_item — Fetch an existing group avatar.
- get_item_collection_params — Get the query params for the `get_item`.
- get_item_permissions_check — Checks if a given request has access to get a group avatar.
- get_item_schema — Get the plugin schema, conforming to JSON Schema.
- prepare_item_for_response — Prepares avatar data 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.