'POST', 'callback' => array( $this, 'analyze' ), 'permission_callback' => array( $this, 'can_run_ai_on_requested_attachments' ), 'args' => array( 'fields' => array( 'type' => 'array', 'minItems' => 1, 'maxItems' => 5, 'uniqueItems' => true, 'items' => array( 'type' => 'string', 'enum' => AssetAnalyzer::SELECTABLE_FIELDS ) ) ), ), ) ); register_rest_route( Router::NAMESPACE, '/bulk/alt/preview', array( array( 'methods' => 'POST', 'callback' => array( $this, 'alt_preview' ), 'permission_callback' => array( $this, 'can_run_ai_on_requested_attachments' ), ), ) ); register_rest_route( Router::NAMESPACE, '/bulk/alt/apply', array( array( 'methods' => 'POST', 'callback' => array( $this, 'alt_apply' ), 'permission_callback' => array( $this, 'can_apply_bulk_alt' ), ), ) ); register_rest_route( Router::NAMESPACE, '/bulk/scan-references', array( array( 'methods' => 'POST', 'callback' => array( $this, 'scan_references' ), 'permission_callback' => array( $this, 'can_edit_requested_media' ), ), ) ); } register_rest_route( Router::NAMESPACE, '/bulk/delete', array( array( 'methods' => 'POST', 'callback' => array( $this, 'delete' ), 'permission_callback' => array( $this, 'can_delete_requested_attachments' ), ), ) ); } public function alt_preview( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'bulk_alt' ) ) { return $denied; } $ids = $this->attachment_ids( $request ); if ( is_wp_error( $ids ) ) { return $ids; } if ( count( $ids ) > BulkAltService::MAX_ITEMS ) { return $this->error( 'too_many', sprintf( /* translators: %d: max items */ __( 'Maximum %d items for bulk ALT preview.', 'foldrocket' ), BulkAltService::MAX_ITEMS ) ); } $locale = $request->get_param( 'locale' ); $locale = is_string( $locale ) ? $locale : null; try { $result = ( new BulkAltService() )->preview( $ids, $locale ); return new WP_REST_Response( $result ); } catch ( \Throwable $e ) { return $this->exception_error( 'bulk_alt_preview_failed', $e, __( 'Could not generate ALT previews. Check your AI provider and try again.', 'foldrocket' ), 502, true ); } } public function alt_apply( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'bulk_alt' ) ) { return $denied; } $confirmed = filter_var( $request->get_param( 'confirmed' ), FILTER_VALIDATE_BOOLEAN ); if ( ! $confirmed ) { return $this->error( 'confirmation_required', __( 'Applying ALT text requires explicit confirmation.', 'foldrocket' ) ); } $items = $request->get_param( 'items' ); if ( ! is_array( $items ) || ! $items ) { return $this->error( 'invalid_items', __( 'No ALT proposals to apply.', 'foldrocket' ) ); } if ( count( $items ) > BulkAltService::MAX_ITEMS ) { return $this->error( 'too_many', sprintf( /* translators: %d: max items */ __( 'Maximum %d items for bulk ALT apply.', 'foldrocket' ), BulkAltService::MAX_ITEMS ) ); } $locale = $request->get_param( 'locale' ); $locale = is_string( $locale ) ? $locale : null; $result = ( new BulkAltService() )->apply( $items, $locale ); return new WP_REST_Response( $result ); } public function analyze( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'ai_analysis' ) ) { return $denied; } $ids = $this->attachment_ids( $request ); if ( is_wp_error( $ids ) ) { return $ids; } $analyzer = new AssetAnalyzer(); $queued = array(); $operation_id = wp_generate_uuid4(); try { $fields = $request->get_param( 'fields' ); if ( $fields !== null ) { if ( ! is_array( $fields ) ) { return $this->error( 'invalid_fields', __( 'Select at least one field to update.', 'foldrocket' ), 400 ); } $fields = AssetAnalyzer::validate_selected_fields( $fields ); if ( array_diff( $fields, array( 'filename' ) ) && ( $denied = $this->require_feature( 'ai_metadata' ) ) ) return $denied; if ( in_array( 'filename', $fields, true ) && ( $denied = $this->require_feature( 'rename_file' ) ) ) return $denied; } $jobs = new JobRepository(); foreach ( $ids as $id ) { if ( $jobs->has_open_attachment_work( $id ) ) { return $this->error( 'media_busy', __( 'AI work is already running for a selected file. Wait for it to finish.', 'foldrocket' ), 409 ); } } foreach ( $ids as $id ) { $queued[] = $fields !== null ? $analyzer->queue_selected_fields( $id, $fields, $operation_id ) : $analyzer->queue( $id, $operation_id ); } } catch ( \InvalidArgumentException $e ) { $code = str_contains( $e->getMessage(), 'API key' ) ? 'missing_api_key' : 'invalid_request'; return $this->error( $code, $e->getMessage(), 400 ); } return new WP_REST_Response( array( 'queued' => count( $queued ), 'jobIds' => $queued, 'operationId' => $operation_id, 'message' => sprintf( /* translators: %d: number of jobs */ __( 'Queued AI analysis for %d item(s).', 'foldrocket' ), count( $queued ) ), ), 202 ); } public function scan_references( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $ids = $this->attachment_ids( $request ); if ( is_wp_error( $ids ) ) { return $ids; } $scanner = new ReferenceScannerService(); $job_ids = array(); foreach ( $ids as $id ) { $job_ids[] = $scanner->queue( $id ); } return new WP_REST_Response( array( 'queued' => count( $job_ids ), 'jobIds' => $job_ids, 'message' => sprintf( /* translators: Placeholder in user-facing message. */ __( 'Queued usage scan for %d item(s).', 'foldrocket' ), count( $job_ids ) ), ), 202 ); } public function delete( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $ids = $this->attachment_ids( $request, false ); if ( is_wp_error( $ids ) ) { return $ids; } $parts = $this->partition_attachment_ids( $ids ); $live = $parts['live']; $missing = $parts['missing']; $confirmed = filter_var( $request->get_param( 'confirmed' ), FILTER_VALIDATE_BOOLEAN ); if ( ! $confirmed ) { if ( ! $live && $missing ) { return new WP_REST_Response( array( 'trashed' => array(), 'deleted' => array(), 'skipped' => $missing, 'blocked' => array(), 'message' => __( 'Those files are already gone. Stale index entries were cleared.', 'foldrocket' ), ) ); } return $this->error( 'confirmation_required', __( 'Bulk delete requires explicit confirmation.', 'foldrocket' ), 400, array( 'attachmentIds' => $live, 'count' => count( $live ), 'preview' => $this->delete_preview( $live ), 'alreadyGone' => $missing, ) ); } // All requested IDs already missing from Media Library — treat as success after prune. if ( ! $live ) { return new WP_REST_Response( array( 'trashed' => array(), 'deleted' => array(), 'skipped' => $missing, 'blocked' => array(), 'message' => __( 'Those files are already gone. Stale index entries were cleared.', 'foldrocket' ), ) ); } // Permanent delete only — no WordPress trash step. $repo = new AssetRepository(); $deleted = array(); $skipped = $missing; $blocked = array(); $handled = array(); $block_used = filter_var( $request->get_param( 'skipUsed' ), FILTER_VALIDATE_BOOLEAN ); foreach ( $live as $id ) { if ( isset( $handled[ $id ] ) ) { if ( ! $repo->attachment_is_live( $id ) ) { $deleted[] = $id; } continue; } $family = $repo->wpml_attachment_family_ids( $id ); $live_family = array_values( array_filter( $family, static fn( int $family_id ): bool => $repo->attachment_is_live( $family_id ) ) ); $can_delete_family = ! array_filter( $live_family, static fn( int $family_id ): bool => ! current_user_can( 'delete_post', $family_id ) ); if ( ! $can_delete_family ) { $skipped[] = $id; continue; } if ( $block_used ) { $used = array_filter( $live_family, static function ( int $family_id ) use ( $repo ): bool { $asset = $repo->find_by_attachment( $family_id ); return $asset && (int) ( $asset['usageCount'] ?? 0 ) > 0; } ); if ( $used ) { $blocked[] = $id; continue; } } foreach ( $live_family as $family_id ) { $handled[ $family_id ] = true; if ( ! $repo->attachment_is_live( $family_id ) ) { continue; } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- frkt is the plugin prefix. do_action( 'frkt_before_delete', $family_id, true ); $result = wp_delete_attachment( $family_id, true ); if ( $result ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- frkt is the plugin prefix. do_action( 'frkt_after_delete', $family_id, true ); } } foreach ( $family as $family_id ) { if ( ! $repo->attachment_is_live( $family_id ) ) { $repo->delete_by_attachment( $family_id ); } } if ( ! $repo->attachment_is_live( $id ) ) { $deleted[] = $id; } else { $skipped[] = $id; } } return new WP_REST_Response( array( 'trashed' => array(), 'deleted' => $deleted, 'skipped' => $skipped, 'blocked' => $blocked, 'message' => sprintf( /* translators: %d: number of deleted items */ __( 'Permanently deleted %d item(s).', 'foldrocket' ), count( $deleted ) ), ) ); } /** * @return list|\WP_Error */ private function attachment_ids( WP_REST_Request $request, bool $require_live = true ) { $ids = $request->get_param( 'attachmentIds' ); if ( ! is_array( $ids ) || ! $ids ) { return $this->error( 'invalid_ids', __( 'attachmentIds must be a non-empty array.', 'foldrocket' ) ); } $ids = array_values( array_unique( array_filter( array_map( 'intval', $ids ) ) ) ); $ids = array_values( array_filter( $ids, static function ( int $id ): bool { return $id > 0; } ) ); if ( ! $ids ) { return $this->error( 'invalid_ids', __( 'No valid attachments selected.', 'foldrocket' ) ); } if ( count( $ids ) > 100 ) { return $this->error( 'too_many', __( 'Maximum 100 items per bulk operation.', 'foldrocket' ) ); } if ( $require_live ) { $repo = new AssetRepository(); $ids = array_values( array_filter( $ids, static function ( int $id ) use ( $repo ): bool { return $repo->attachment_is_live( $id ); } ) ); if ( ! $ids ) { return $this->error( 'invalid_ids', __( 'No valid attachments selected.', 'foldrocket' ) ); } } return $ids; } /** * Split requested IDs into live attachments vs already-gone (prune index for the latter). * * @param list $ids * @return array{live: list, missing: list} */ private function partition_attachment_ids( array $ids ): array { $repo = new AssetRepository(); $live = array(); $missing = array(); foreach ( $ids as $id ) { if ( $repo->attachment_is_live( $id ) ) { $live[] = $id; continue; } $missing[] = $id; $repo->delete_by_attachment( $id ); } return array( 'live' => $live, 'missing' => $missing, ); } /** * @param list $ids * @return list> */ private function delete_preview( array $ids ): array { $repo = new AssetRepository(); $out = array(); foreach ( array_slice( $ids, 0, 20 ) as $id ) { $asset = $repo->find_by_attachment( $id ); $family = array_values( array_filter( $repo->wpml_attachment_family_ids( $id ), static fn( int $family_id ): bool => $repo->attachment_is_live( $family_id ) ) ); $out[] = array( 'attachmentId' => $id, 'filename' => $asset['filename'] ?? (string) $id, 'usageCount' => (int) ( $asset['usageCount'] ?? 0 ), 'linkedAttachmentIds' => array_values( array_diff( $family, array( $id ) ) ), 'linkedCount' => max( 0, count( $family ) - 1 ), ); } return $out; } }