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
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.