$attachment_ids * @return array{queued: int, jobIds: list, skipped: list, missing: list, operationId: string, message: string} */ public function queue( array $attachment_ids, string $format, int $quality ): array { $format = sanitize_key( $format ); if ( ! in_array( $format, self::FORMATS, true ) ) { throw new \InvalidArgumentException( esc_html__( 'Choose JPG, PNG or WebP.', 'foldrocket' ) ); } $quality = max( ImageConvertService::QUALITY_MIN, min( ImageConvertService::QUALITY_MAX, $quality ) ); $jobs = new JobRepository(); $runner = new JobScheduler(); $queued = array(); $skipped = array(); $missing = array(); $operation_id = wp_generate_uuid4(); foreach ( $attachment_ids as $attachment_id ) { $attachment_id = (int) $attachment_id; $preview = ( new ImageConvertService() )->preview( $attachment_id ); $file = get_attached_file( $attachment_id ); if ( ! is_string( $file ) || $file === '' || ! is_readable( $file ) ) { update_post_meta( $attachment_id, AssetIndexer::MISSING_FILE_META, '1' ); $missing[] = $attachment_id; $skipped[] = $attachment_id; continue; } if ( ! $preview['canConvert'] || $format === (string) $preview['format'] || $jobs->has_open_type_for_attachment( 'convert_asset', $attachment_id ) ) { $skipped[] = $attachment_id; continue; } $job_id = $jobs->create( 'convert_asset', array( 'attachment_id' => $attachment_id, 'requested_by' => get_current_user_id(), 'format' => $format, 'quality' => $quality, 'backup' => true, 'operation_id' => $operation_id, ), 'convert:' . $attachment_id ); $runner->dispatch( $job_id ); $queued[] = $job_id; } return array( 'queued' => count( $queued ), 'jobIds' => $queued, 'skipped' => $skipped, 'missing' => $missing, 'operationId' => $operation_id, 'message' => sprintf( /* translators: 1: queued media count, 2: skipped media count */ __( 'Queued format conversion for %1$d item(s). %2$d skipped.', 'foldrocket' ), count( $queued ), count( $skipped ) ), ); } }