BP_REST_Document_Folder_Endpoint::create_item( WP_REST_Request $request )

Create document 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

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
public function create_item( $request ) {
 
    $args = array(
        'title'   => wp_strip_all_tags( $request['title'] ),
        '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_create_items_query_args', $args, $request );
 
    $folder_id = bp_folder_add( $args );
 
    if ( is_wp_error( $folder_id ) ) {
        return $folder_id;
    }
 
    $folders = $this->assemble_response_data( array( 'folder_ids' => array( $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 = rest_ensure_response( $retval );
 
    /**
     * Fires after a Document folder is created 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_create_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.