'GET', 'callback' => array( $this, 'index' ), 'permission_callback' => array( $this, 'can_view' ), 'args' => array( 'page' => array( 'type' => 'integer', 'default' => 1, 'sanitize_callback' => 'absint', ), 'per_page' => array( 'type' => 'integer', 'default' => 20, 'sanitize_callback' => 'absint', ), 'cursor' => array( 'type' => 'integer', 'minimum' => 0, 'sanitize_callback' => 'absint', ), 'include_total' => array( 'type' => 'boolean', 'default' => true, 'sanitize_callback' => 'rest_sanitize_boolean', ), 'search' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), 'folder_id' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'mime' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'ai_status' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'usage_min' => array( 'type' => 'integer', 'minimum' => 0, 'sanitize_callback' => 'absint' ), 'usage_max' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'min_size' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'missing_alt' => array( 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ), 'missing_file' => array( 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ), 'indexed_only' => array( 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ), 'orientation' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'author_id' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'date_from' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'date_to' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'sort' => array( 'type' => 'string', 'default' => 'date', 'sanitize_callback' => 'sanitize_key', ), 'direction' => array( 'type' => 'string', 'default' => 'DESC', 'sanitize_callback' => 'sanitize_text_field', ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/assets/authors', array( array( 'methods' => 'GET', 'callback' => array( $this, 'authors' ), 'permission_callback' => array( $this, 'can_view' ), ), ) ); // Register static paths before /assets/(?P\d+) so they never collide. register_rest_route( Router::NAMESPACE, '/assets/upload', array( array( 'methods' => 'POST', 'callback' => array( $this, 'upload' ), 'permission_callback' => array( $this, 'can_upload' ), 'args' => array( 'folder_id' => array( 'type' => 'integer', 'required' => false, 'sanitize_callback' => 'absint', ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/assets/download-zip', array( array( 'methods' => 'POST', 'callback' => array( $this, 'download_zip' ), 'permission_callback' => array( $this, 'can_download_zip' ), 'args' => array( 'attachmentIds' => array( 'type' => 'array', 'minItems' => 1, 'maxItems' => \FoldRocket\Application\Services\LibraryZipService::MAX_FILES, 'uniqueItems' => true, 'items' => array( 'type' => 'integer', 'minimum' => 1 ), ), 'sort' => array( 'type' => 'string', 'enum' => array( 'default', 'date', 'title', 'size', 'usage' ), 'sanitize_callback' => 'sanitize_key' ), 'direction' => array( 'type' => 'string', 'enum' => array( 'ASC', 'DESC' ), 'sanitize_callback' => 'sanitize_text_field' ), 'search' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'folder_id' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'mime' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'ai_status' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'usage_min' => array( 'type' => 'integer', 'minimum' => 0, 'sanitize_callback' => 'absint' ), 'usage_max' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'min_size' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'missing_alt' => array( 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', ), 'missing_file' => array( 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', ), 'indexed_only' => array( 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', ), 'orientation' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'author_id' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'date_from' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'date_to' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'name' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/assets/index', array( array( 'methods' => 'POST', 'callback' => array( $this, 'index_library' ), 'permission_callback' => array( $this, 'can_settings' ), 'args' => array( 'mode' => array( 'type' => 'string', 'default' => 'differential', 'enum' => array( 'differential', 'full' ), 'sanitize_callback' => 'sanitize_key', ), ), ), ) ); register_rest_route( Router::NAMESPACE, '/assets/(?P\d+)', array( array( 'methods' => 'GET', 'callback' => array( $this, 'show' ), 'permission_callback' => array( $this, 'can_edit_requested_attachment' ), ), array( 'methods' => 'PATCH', 'callback' => array( $this, 'update' ), 'permission_callback' => array( $this, 'can_edit_requested_attachment' ), ), ) ); } public function upload( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'upload' ) ) { return $denied; } $file = $this->extract_uploaded_file( $request ); if ( ! is_array( $file ) || empty( $file['tmp_name'] ) ) { $keys = array_keys( (array) $request->get_file_params() ); return $this->error( 'missing_file', sprintf( /* translators: %s: comma-separated list of received file field names, or "none". */ __( 'No file was uploaded (received fields: %s). Check PHP upload_max_filesize / post_max_size.', 'foldrocket' ), $keys ? implode( ', ', $keys ) : 'none' ), 400 ); } $folder_raw = $request->get_param( 'folder_id' ); if ( null === $folder_raw || '' === $folder_raw ) { $body_params = $request->get_body_params(); $folder_raw = is_array( $body_params ) ? ( $body_params['folder_id'] ?? null ) : null; } $folder_id = $folder_raw ? (int) $folder_raw : null; if ( $folder_id !== null && $folder_id <= 0 ) { $folder_id = null; } try { $pipeline = $this->extract_upload_pipeline( $request ); $asset = ( new MediaUploadService() )->upload( $file, $folder_id, $pipeline ); return new WP_REST_Response( array( 'asset' => $asset ), 201 ); } catch ( \Throwable $e ) { return $this->exception_error( 'upload_failed', $e, __( 'Upload failed. Please try again.', 'foldrocket' ), 400, true ); } } /** * Per-upload pipeline flags from the library modal (FormData). * * @return array{analyze: bool, aiRename: bool} */ private function extract_upload_pipeline( WP_REST_Request $request ): array { $body = $request->get_body_params(); if ( ! is_array( $body ) ) { $body = array(); } $flag = static function ( string $key ) use ( $request, $body ): bool { $raw = $request->get_param( $key ); if ( null === $raw || '' === $raw ) { $raw = $body[ $key ] ?? '0'; } return in_array( (string) $raw, array( '1', 'true', 'yes' ), true ); }; return array( 'analyze' => $flag( 'analyze' ), 'aiRename' => $flag( 'ai_rename' ), ); } public function download_zip( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'download_zip' ) ) { return $denied; } $json = $request->get_json_params(); $usage = is_array( $json ) && array_key_exists( 'usage_max', $json ) ? $json['usage_max'] : null; $args = array( 'search' => (string) $request->get_param( 'search' ), 'folder_id' => $request->get_param( 'folder_id' ) ? $this->int_param( $request, 'folder_id' ) : null, 'mime' => (string) $request->get_param( 'mime' ), 'ai_status' => (string) $request->get_param( 'ai_status' ), 'usage_min' => $request->get_param( 'usage_min' ) !== null ? $this->int_param( $request, 'usage_min' ) : null, 'usage_max' => ( null === $usage || '' === $usage ) ? null : (int) $usage, 'min_size' => $request->get_param( 'min_size' ) ? $this->int_param( $request, 'min_size' ) : null, 'missing_alt' => rest_sanitize_boolean( $request->get_param( 'missing_alt' ) ), 'missing_file' => rest_sanitize_boolean( $request->get_param( 'missing_file' ) ), 'indexed_only' => rest_sanitize_boolean( $request->get_param( 'indexed_only' ) ), 'orientation' => (string) $request->get_param( 'orientation' ), 'author_id' => (string) $request->get_param( 'author_id' ), 'date_from' => (string) $request->get_param( 'date_from' ), 'date_to' => (string) $request->get_param( 'date_to' ), 'sort' => 'date', 'direction' => 'DESC', ); $selected_ids = $request->get_param( 'attachmentIds' ); $selected_ids = is_array( $selected_ids ) ? array_values( array_unique( array_filter( array_map( 'absint', $selected_ids ) ) ) ) : array(); $user_id = get_current_user_id(); $payload = array( 'args' => $args, 'label' => (string) $request->get_param( 'name' ), 'user_id' => $user_id, 'requested_by' => $user_id, 'phase' => 'queued', ); if ( $selected_ids ) { $payload['selected_attachment_ids'] = $selected_ids; } $key = 'build_library_zip:' . $user_id . ':' . hash( 'sha256', (string) wp_json_encode( array( $args, $payload['label'], $selected_ids ) ) ); $repo = new JobRepository(); $job_id = $repo->create( 'build_library_zip', $payload, $key ); ( new JobScheduler() )->dispatch( $job_id ); return new WP_REST_Response( array( 'queued' => true, 'jobId' => $job_id, ), 202 ); } public function can_download_zip( WP_REST_Request $request ): bool { if ( ! $this->can_view() ) { return false; } $ids = $request->get_param( 'attachmentIds' ); if ( null === $ids ) { return true; } if ( ! is_array( $ids ) || ! $ids || count( $ids ) > \FoldRocket\Application\Services\LibraryZipService::MAX_FILES ) { return false; } foreach ( $ids as $attachment_id ) { $attachment_id = absint( $attachment_id ); if ( $attachment_id < 1 || 'attachment' !== get_post_type( $attachment_id ) || ! current_user_can( 'read_post', $attachment_id ) ) { return false; } } return true; } public function index( WP_REST_Request $request ): WP_REST_Response { $missing_file = filter_var( $request->get_param( 'missing_file' ), FILTER_VALIDATE_BOOLEAN ); $page = $this->int_param( $request, 'page', 1 ); $cursor = $this->int_param( $request, 'cursor', 0 ); // Refresh once at the start of a Missing file result set. This catches // files removed from uploads after their last FoldRocket index pass. if ( $missing_file && $page <= 1 && $cursor <= 0 ) { ( new \FoldRocket\Application\Services\AssetIndexer() )->refresh_file_statuses(); } $result = ( new AssetRepository() )->list( array( 'page' => $page, 'per_page' => $this->int_param( $request, 'per_page', 20 ), 'cursor' => $cursor, '_include_total' => filter_var( $request->get_param( 'include_total' ), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) ?? true, 'search' => (string) $request->get_param( 'search' ), 'folder_id' => $request->get_param( 'folder_id' ) ? $this->int_param( $request, 'folder_id' ) : null, 'mime' => (string) $request->get_param( 'mime' ), 'ai_status' => (string) $request->get_param( 'ai_status' ), 'duplicate_status' => (string) $request->get_param( 'duplicate_status' ), 'usage_min' => $request->get_param( 'usage_min' ) !== null ? $this->int_param( $request, 'usage_min' ) : null, 'usage_max' => $request->get_param( 'usage_max' ) !== null ? $this->int_param( $request, 'usage_max' ) : null, 'min_size' => $request->get_param( 'min_size' ) ? $this->int_param( $request, 'min_size' ) : null, 'missing_alt' => filter_var( $request->get_param( 'missing_alt' ), FILTER_VALIDATE_BOOLEAN ), 'missing_file' => $missing_file, 'indexed_only' => filter_var( $request->get_param( 'indexed_only' ), FILTER_VALIDATE_BOOLEAN ), 'orientation' => (string) ( $request->get_param( 'orientation' ) ?: '' ), 'author_id' => (string) ( $request->get_param( 'author_id' ) ?: '' ), 'date_from' => (string) ( $request->get_param( 'date_from' ) ?: '' ), 'date_to' => (string) ( $request->get_param( 'date_to' ) ?: '' ), 'sort' => (string) $request->get_param( 'sort' ), 'direction' => (string) $request->get_param( 'direction' ), ) ); return new WP_REST_Response( $result ); } public function authors(): WP_REST_Response { return new WP_REST_Response( array( 'items' => ( new AssetRepository() )->list_attachment_authors(), ) ); } public function show( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $attachment_id = $this->int_param( $request, 'id' ); $asset = ( new AssetRepository() )->find_by_attachment( $attachment_id ); if ( ! $asset ) { return $this->error( 'not_found', __( 'Asset not found.', 'foldrocket' ), 404 ); } if ( ! empty( $asset['id'] ) ) { $asset['references'] = ( new ReferenceRepository() )->list_for_asset( (int) $asset['id'] ); $asset['usageCount'] = count( $asset['references'] ); } else { $asset['references'] = array(); $asset['usageCount'] = 0; } return new WP_REST_Response( $asset ); } public function update( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $attachment_id = $this->int_param( $request, 'id' ); $data = array(); foreach ( array( 'title', 'altText', 'caption', 'description', 'locale' ) as $key ) { if ( null !== $request->get_param( $key ) ) { $data[ $key ] = $request->get_param( $key ); } } $asset = ( new AssetUpdater() )->update( $attachment_id, $data ); if ( ! $asset ) { return $this->error( 'not_found', __( 'Asset not found.', 'foldrocket' ), 404 ); } return new WP_REST_Response( $asset ); } public function index_library( WP_REST_Request $request ): WP_REST_Response { $mode = 'full' === (string) $request->get_param( 'mode' ) ? 'full' : 'differential'; $status = ( new \FoldRocket\Application\Services\LibraryScanService() )->start( 'index', $mode ); return new WP_REST_Response( array_merge( $status, array( 'queued' => $status['total'], 'scan' => $status ) ), 202 ); } }