index|references|duplicates)', array( 'methods' => 'GET', 'callback' => array( $this, 'scan_status' ), 'permission_callback' => array( $this, 'can_view' ), 'args' => array( 'kind' => array( 'type' => 'string', 'enum' => array( 'index', 'references', 'duplicates' ), 'required' => true ) ), ) ); register_rest_route( Router::NAMESPACE, '/bulk-operations/active', array( array( 'methods' => 'GET', 'callback' => array( $this, 'active_bulk_operation' ), 'permission_callback' => array( $this, 'can_run_jobs' ), ), ) ); register_rest_route( Router::NAMESPACE, '/jobs/(?P\d+)', array( array( 'methods' => 'GET', 'callback' => array( $this, 'show' ), 'permission_callback' => array( $this, 'can_access_requested_job' ), ), ) ); register_rest_route( Router::NAMESPACE, '/jobs', array( array( 'methods' => 'GET', 'callback' => array( $this, 'index' ), 'permission_callback' => array( $this, 'can_manage_jobs' ), ), ) ); register_rest_route( Router::NAMESPACE, '/jobs/run', array( array( 'methods' => 'POST', 'callback' => array( $this, 'run' ), 'permission_callback' => array( $this, 'can_run_jobs' ), ), ) ); register_rest_route( Router::NAMESPACE, '/jobs/(?P\d+)/retry', array( array( 'methods' => 'POST', 'callback' => array( $this, 'retry' ), 'permission_callback' => array( $this, 'can_access_requested_job' ), ), ) ); register_rest_route( Router::NAMESPACE, '/jobs/(?P\d+)/cancel', array( array( 'methods' => 'POST', 'callback' => array( $this, 'cancel' ), 'permission_callback' => array( $this, 'can_access_requested_job' ), ), ) ); } public function can_access_requested_job( WP_REST_Request $request ): bool { if ( $this->can_manage_jobs() ) { return true; } $job = ( new JobRepository() )->find( $this->int_param( $request, 'id' ) ); if ( ! is_array( $job ) || (int) ( $job['requestedBy'] ?? 0 ) !== get_current_user_id() ) { return false; } $attachment_id = (int) ( $job['attachmentId'] ?? 0 ); if ( 'build_library_zip' === (string) ( $job['type'] ?? '' ) ) { return $this->can_view(); } return $attachment_id > 0 && current_user_can( 'edit_post', $attachment_id ); } public function can_run_jobs(): bool { return $this->can_manage_jobs() || $this->can_run_ai() || $this->can_manage(); } public function scan_status( WP_REST_Request $request ): WP_REST_Response { return new WP_REST_Response( ( new \FoldRocket\Application\Services\LibraryScanService() )->status( (string) $request->get_param( 'kind' ) ) ); } public function active_bulk_operation(): WP_REST_Response { return new WP_REST_Response( array( 'operation' => ( new JobRepository() )->active_operation( get_current_user_id() ) ) ); } public function index( WP_REST_Request $request ): WP_REST_Response { $page = max( 1, (int) $request->get_param( 'page' ) ?: 1 ); $data = ( new JobRepository() )->list( $page, 20 ); return new WP_REST_Response( $data ); } public function show( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $job = ( new JobRepository() )->find( $this->int_param( $request, 'id' ) ); return $job ? new WP_REST_Response( $job ) : $this->error( 'job_not_found', __( 'Job not found.', 'foldrocket' ), 404 ); } /** Nudge the durable queue without executing heavy work in the REST request. */ public function run(): WP_REST_Response { $actor = $this->can_manage_jobs() ? null : get_current_user_id(); $repo = new JobRepository(); $scheduler = new JobScheduler(); $scheduled = 0; foreach ( $repo->pending_ids( 10, $actor ) as $job_id ) { $scheduler->dispatch( $job_id ); ++$scheduled; } return new WP_REST_Response( array( 'ran' => false, 'locked' => false, 'scheduled' => $scheduled, 'pending' => $repo->count_open( $actor ), 'job' => null, ) ); } public function retry( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $id = $this->int_param( $request, 'id' ); $repo = new JobRepository(); $job = $repo->find( $id ); $kind = $job['payload']['kind'] ?? null; if ( $kind && $id !== (int) get_option( 'frkt_scan_' . $kind, 0 ) ) { return $this->error( 'scan_superseded', __( 'A newer scan exists. Use its progress controls.', 'foldrocket' ), 409 ); } if ( ! $repo->reset_for_retry( $id ) ) { return $this->error( 'cannot_retry', __( 'Job cannot be retried.', 'foldrocket' ) ); } ( new JobScheduler() )->dispatch( $id ); return new WP_REST_Response( array( 'retried' => true ) ); } public function cancel( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $id = $this->int_param( $request, 'id' ); $repo = new JobRepository(); $job = $repo->find( $id ); $ok = $repo->cancel( $id ); if ( ! $ok ) { return $this->error( 'cannot_cancel', __( 'Job cannot be cancelled.', 'foldrocket' ) ); } // Free the inspector overlay if this was an AI job mid-flight. $type = is_array( $job ) ? (string) ( $job['type'] ?? '' ) : ''; $payload = is_array( $job['payload'] ?? null ) ? $job['payload'] : array(); if ( in_array( $type, array( 'analyze_asset', 'generate_metadata' ), true ) ) { $attachment_id = (int) ( $payload['attachment_id'] ?? 0 ); if ( $attachment_id > 0 ) { ( new \FoldRocket\Domain\Assets\AssetRepository() )->upsert( $attachment_id, array( 'ai_status' => 'pending' ) ); } } if ( 'build_library_zip' === $type ) { ( new \FoldRocket\Application\Services\LibraryZipService() )->cleanup_job( $payload ); } return new WP_REST_Response( array( 'cancelled' => true ) ); } }