BP_REST_Document_Folder_Endpoint::update_item( WP_REST_Request $request )
Update a folder.
Description
Parameters
- $request
-
(Required) Full details about the request.
Return
(WP_REST_Response) | WP_Error
Source
File: bp-document/classes/class-bp-rest-document-folder-endpoint.php
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 | public function update_item( $request ) { $id = $request [ 'id' ]; $folders = $this ->assemble_response_data( array ( 'folder_ids' => array ( $id ) ) ); if ( empty ( $folders [ 'folders' ] ) ) { return new WP_Error( 'bp_rest_folder_invalid_id' , __( 'Invalid Folder ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } $folder = end ( $folders [ 'folders' ] ); $args = array ( 'id' => $folder ->id, 'user_id' => $folder ->user_id, 'title' => $folder ->title, 'group_id' => $folder ->group_id, 'parent' => $folder ->parent, 'privacy' => $folder ->privacy, ); if ( isset( $request [ 'title' ] ) && ! empty ( $request [ 'title' ] ) ) { $args [ 'title' ] = wp_strip_all_tags( $request [ 'title' ] ); } if ( isset( $request [ 'privacy' ] ) && ! empty ( $request [ 'privacy' ] ) ) { $args [ 'privacy' ] = $request [ 'privacy' ]; } if ( isset( $request [ 'group_id' ] ) && ! empty ( $request [ 'group_id' ] ) ) { $args [ 'group_id' ] = $request [ 'group_id' ]; $args [ 'privacy' ] = 'grouponly' ; } if ( isset( $request [ 'parent' ] ) && ! empty ( $request [ 'parent' ] ) ) { $args [ 'parent' ] = $request [ 'parent' ]; $parent_folder = new BP_Document_Folder( $args [ 'parent' ] ); $args [ 'privacy' ] = $parent_folder ->privacy; } /** * 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_document_folder_update_items_query_args' , $args , $request ); if ( isset( $request [ 'privacy' ] ) && ! empty ( $request [ 'privacy' ] ) ) { bp_document_update_privacy( $folder ->id, $request [ 'privacy' ], 'folder' ); } // Move folders. if ( (int) $args [ 'parent' ] !== (int) $folder ->parent ) { $folder_id = $folder ->id; $destination_folder_id = $args [ 'parent' ]; $group_id = $args [ 'group_id' ]; if ( (int) $folder_id > 0 ) { if ( ! bp_folder_user_can_edit( $folder_id ) ) { return new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, You don\'t have permission to move this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } if ( (int) $destination_folder_id > 0 ) { if ( ! bp_folder_user_can_edit( $destination_folder_id ) ) { return new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, You don\'t have permission to move this folder.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } } $fetch_children = bp_document_get_folder_children( $folder_id ); if ( ! empty ( $fetch_children ) ) { if ( in_array( $destination_folder_id , $fetch_children , true ) ) { return new WP_Error( 'bp_rest_invalid_move_folder' , __( 'Couldn’t move item because it\'s parent folder.' , 'buddyboss' ), array ( 'status' => 400, ) ); } } bp_document_move_folder_to_folder( $folder_id , $destination_folder_id , $group_id ); } $updated_folder_id = bp_folder_add( $args ); $status = true; if ( is_wp_error( $updated_folder_id ) ) { return $updated_folder_id ; } if ( empty ( $updated_folder_id ) ) { $status = false; } $folders = $this ->assemble_response_data( array ( 'folder_ids' => array ( $updated_folder_id ) ) ); $folder = current( $folders [ 'folders' ] ); $fields_update = $this ->update_additional_fields_for_object( $folder , $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update ; } $retval = $this ->prepare_response_for_collection( $this ->document_endpoint->prepare_item_for_response( $folder , $request ) ); $response = new WP_REST_Response(); $response ->set_data( array ( 'updated' => $status , 'data' => $retval , ) ); /** * Fires after an document folder is updated 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_document_folder_update_item' , $response , $request ); return $response ; } |
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.