'GET', 'callback' => array( $this, 'index' ), 'permission_callback' => array( $this, 'can_view' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'create' ), 'permission_callback' => array( $this, 'can_manage' ), 'args' => array( 'name' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), 'parentId' => array( 'type' => array( 'integer', 'null' ), 'required' => false, ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/folders/(?P\d+)', array( array( 'methods' => 'PATCH', 'callback' => array( $this, 'update' ), 'permission_callback' => array( $this, 'can_update_folder' ), ), array( 'methods' => 'DELETE', 'callback' => array( $this, 'delete' ), 'permission_callback' => array( $this, 'can_structure_folders' ), ), ) ); register_rest_route( Router::NAMESPACE, '/folders/(?P\d+)/order', array( array( 'methods' => 'POST', 'callback' => array( $this, 'reorder' ), 'permission_callback' => array( $this, 'can_edit_requested_media' ), 'args' => array( 'attachmentIds' => array( 'type' => 'array', 'required' => true, 'items' => array( 'type' => 'integer' ), ), 'targetAttachmentId' => array( 'type' => 'integer', 'required' => true, ), 'place' => array( 'type' => 'string', 'required' => true, 'enum' => array( 'before', 'after' ), ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/folders/(?P\d+)/assign', array( array( 'methods' => 'POST', 'callback' => array( $this, 'assign' ), 'permission_callback' => array( $this, 'can_edit_requested_media' ), 'args' => array( 'assetIds' => array( 'type' => 'array' ), 'attachmentIds' => array( 'type' => 'array' ), ), ), array( 'methods' => 'DELETE', 'callback' => array( $this, 'unassign' ), 'permission_callback' => array( $this, 'can_edit_requested_media' ), 'args' => array( 'attachmentIds' => array( 'type' => 'array', 'required' => true, 'items' => array( 'type' => 'integer' ), ), ), ), ) ); } public function index(): WP_REST_Response { return new WP_REST_Response( array( 'items' => ( new FolderRepository() )->all() ) ); } public function create( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $name = sanitize_text_field( (string) $request->get_param( 'name' ) ); if ( $name === '' ) { return $this->error( 'invalid_name', __( 'Folder name is required.', 'foldrocket' ) ); } $parent = $request->get_param( 'parentId' ); try { $folder = ( new FolderRepository() )->create( array( 'name' => $name, 'parentId' => null !== $parent && '' !== $parent ? (int) $parent : null, ) ); return new WP_REST_Response( $folder, 201 ); } catch ( \InvalidArgumentException $e ) { return $this->error( 'invalid_parent', $e->getMessage(), 400 ); } catch ( \Throwable $e ) { return $this->exception_error( 'create_failed', $e, __( 'Could not create the folder. Please try again.', 'foldrocket' ), 500, true ); } } public function update( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $data = array(); if ( null !== $request->get_param( 'name' ) ) { $data['name'] = $request->get_param( 'name' ); } if ( $request->has_param( 'parentId' ) ) { $data['parentId'] = $request->get_param( 'parentId' ); } if ( null !== $request->get_param( 'sortOrder' ) ) { $data['sortOrder'] = $request->get_param( 'sortOrder' ); } if ( $request->has_param( 'color' ) ) { if ( $denied = $this->require_feature( 'folder_colors' ) ) { return $denied; } $color = $request->get_param( 'color' ); $color = is_string( $color ) ? strtolower( trim( $color ) ) : ''; if ( '' !== $color && ! is_string( sanitize_hex_color( $color ) ) ) { return $this->error( 'invalid_color', __( 'Choose a hex color.', 'foldrocket' ), 400 ); } $data['color'] = $color; } if ( ! $data ) { return $this->error( 'nothing_to_update', __( 'No folder fields to update.', 'foldrocket' ) ); } try { $folder = ( new FolderRepository() )->update( $this->int_param( $request, 'id' ), $data ); } catch ( \InvalidArgumentException $e ) { return $this->error( 'invalid_parent', $e->getMessage(), 400 ); } catch ( \Throwable $e ) { return $this->exception_error( 'update_failed', $e, __( 'Could not update the folder. Please try again.', 'foldrocket' ), 500, true ); } if ( ! $folder ) { return $this->error( 'not_found', __( 'Folder not found.', 'foldrocket' ), 404 ); } return new WP_REST_Response( $folder ); } public function delete( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $result = ( new FolderRepository() )->delete( $this->int_param( $request, 'id' ) ); if ( ! $result ) { return $this->error( 'not_found', __( 'Folder not found.', 'foldrocket' ), 404 ); } return new WP_REST_Response( $result ); } public function assign( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'folders' ) ) { return $denied; } $folder_id = $this->int_param( $request, 'id' ); $attachment_ids = $request->get_param( 'attachmentIds' ); $asset_ids = $request->get_param( 'assetIds' ); $repo = new FolderRepository(); if ( is_array( $attachment_ids ) && $attachment_ids ) { $count = $repo->assign_attachments( $folder_id, array_map( 'intval', $attachment_ids ) ); return new WP_REST_Response( array( 'assigned' => $count ) ); } if ( ! is_array( $asset_ids ) ) { return $this->error( 'invalid_assets', __( 'attachmentIds or assetIds is required.', 'foldrocket' ) ); } $repo->assign_assets( $folder_id, array_map( 'intval', $asset_ids ) ); return new WP_REST_Response( array( 'assigned' => count( $asset_ids ) ) ); } public function reorder( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'folders' ) ) { return $denied; } $folder_id = $this->int_param( $request, 'id' ); $attachment_ids = $request->get_param( 'attachmentIds' ); $target_id = (int) $request->get_param( 'targetAttachmentId' ); $place = (string) $request->get_param( 'place' ); if ( ! is_array( $attachment_ids ) || ! $attachment_ids || $target_id <= 0 || ! in_array( $place, array( 'before', 'after' ), true ) ) { return $this->error( 'invalid_order', __( 'Choose a file and where it should go.', 'foldrocket' ), 400 ); } $repo = new FolderRepository(); if ( ! $repo->find( $folder_id ) ) { return $this->error( 'not_found', __( 'Folder not found.', 'foldrocket' ), 404 ); } $moved = $repo->reorder_attachments( $folder_id, array_map( 'intval', $attachment_ids ), $target_id, $place ); if ( ! $moved ) { return $this->error( 'invalid_order', __( 'That file is not in this folder.', 'foldrocket' ), 400 ); } return new WP_REST_Response( array( 'reordered' => true ) ); } public function unassign( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'folders' ) ) { return $denied; } $folder_id = $this->int_param( $request, 'id' ); $attachment_ids = $request->get_param( 'attachmentIds' ); if ( ! is_array( $attachment_ids ) || ! $attachment_ids ) { return $this->error( 'invalid_assets', __( 'attachmentIds is required.', 'foldrocket' ) ); } if ( ! ( new FolderRepository() )->find( $folder_id ) ) { return $this->error( 'not_found', __( 'Folder not found.', 'foldrocket' ), 404 ); } $count = ( new FolderRepository() )->unassign_attachments( $folder_id, array_map( 'intval', $attachment_ids ) ); return new WP_REST_Response( array( 'removed' => $count ) ); } }