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

	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.