applier = $applier ?? new ResultApplier(); $this->outcomeBuilder = $outcomeBuilder ?? new ConfirmOutcomeBuilder(); $this->tokens = $tokens ?? new ConfirmTokenStore(); } /** * Run each remaining product synchronously, in order, updating the * stored outcome after every product so a timeout leaves a truthful * 'partial' outcome behind for a retry to read (nothing is re-run). * * @param string $token Raw confirm token. * @param string $generator Generator key. * @param int[] $remaining Product ids to run, in order. * @param array $pin Loaded run pin. * @param string $destination 'apply' | 'review'. * @param string $overwrite 'replace' | 'empty_only'. * @param int $user_id Acting user id. * @param array $skipped Already-decided skipped entries (for progress snapshots only). * @param array $run_pin_echo Response `run_pin` shape (for progress snapshots only). * @param array $blueprints_echo Response `blueprints` shape (for progress snapshots only). * @return array{applied:array,staged:array,held_back:array,failed:array,plan_limits:array} */ public function run( string $token, string $generator, array $remaining, array $pin, string $destination, string $overwrite, int $user_id, array $skipped, array $run_pin_echo, array $blueprints_echo ): array { if ( function_exists( 'wc_set_time_limit' ) ) { wc_set_time_limit( 0 ); } $applied = array(); $staged = array(); $held_back = array(); $failed = array(); $plan_limits = array(); foreach ( $remaining as $product_id ) { $result = BlueprintIntegration::run( $generator, array( 'type' => 'product', 'id' => $product_id ), array( 'blueprintRun' => (int) $pin['id'], 'overwrite' => $overwrite ), array(), 'ability_run', array( 'userId' => $user_id ) ); if ( $result instanceof WP_Error ) { $failed[] = array( 'product_id' => $product_id, 'code' => $result->get_error_code(), 'message' => $result->get_error_message() ); $this->recordProgress( $token, $generator, $run_pin_echo, $blueprints_echo, $applied, $staged, $held_back, $skipped, $failed, $plan_limits ); continue; } foreach ( (array) ( $result['appliedFreeDefaults'] ?? array() ) as $entry ) { if ( is_array( $entry ) && isset( $entry['key'] ) ) { $plan_limits[ $entry['key'] ] = $entry; } } $result_key = (string) ( $result['resultKey'] ?? '' ); $blueprint = is_array( $result['blueprint'] ?? null ) ? $result['blueprint'] : array(); if ( BlueprintIntegration::isHeldBack( $result, $destination ) ) { $held_back[] = array( 'product_id' => $product_id, 'result_key' => $result_key, 'reason' => empty( $result['eligibility']['canApply'] ) ? 'blocked' : 'needs_review', 'message' => BlueprintIntegration::heldBackMessage( $result ), ); $this->recordProgress( $token, $generator, $run_pin_echo, $blueprints_echo, $applied, $staged, $held_back, $skipped, $failed, $plan_limits ); continue; } $this->applyOne( $product_id, $result_key, $blueprint, $overwrite, $destination, $user_id, $applied, $staged, $failed ); $this->recordProgress( $token, $generator, $run_pin_echo, $blueprints_echo, $applied, $staged, $held_back, $skipped, $failed, $plan_limits ); } return array( 'applied' => $applied, 'staged' => $staged, 'held_back' => $held_back, 'failed' => $failed, 'plan_limits' => array_values( $plan_limits ) ); } /** * Apply (or stage) one already-generated result, appending to the * relevant by-reference bucket. * * @param int $product_id Product id. * @param string $result_key Stored result's result_key. * @param array $blueprint Result's `blueprint` meta. * @param string $overwrite 'replace' | 'empty_only'. * @param string $destination 'apply' | 'review'. * @param int $user_id Acting user id. * @param array &$applied Applied entries (mutated). * @param array &$staged Staged entries (mutated). * @param array &$failed Failed entries (mutated). * @return void */ private function applyOne( int $product_id, string $result_key, array $blueprint, string $overwrite, string $destination, int $user_id, array &$applied, array &$staged, array &$failed ): void { $outcome = $this->applier->apply( $result_key, array( 'expectedDefinitionHash' => (string) ( $blueprint['definitionHash'] ?? '' ), 'overwrite' => $overwrite, 'destination' => $destination, 'confirmReview' => false, ), $user_id ); if ( $outcome instanceof WP_Error ) { $failed[] = array( 'product_id' => $product_id, 'code' => $outcome->get_error_code(), 'message' => $outcome->get_error_message() ); return; } if ( 'staged' === $outcome['status'] ) { $staged[] = array( 'product_id' => $product_id, 'result_key' => $result_key, 'fields' => $outcome['fields'] ); return; } if ( 'applied' === $outcome['status'] ) { $applied[] = array( 'product_id' => $product_id, 'result_key' => $result_key, 'blueprint' => array( 'id' => $blueprint['id'] ?? null, 'name' => (string) ( $blueprint['name'] ?? '' ), 'revision' => $blueprint['revision'] ?? null ), 'fields' => $outcome['fields'], 'snapshot_taken' => (bool) $outcome['snapshotTaken'], ); return; } $failed[] = array( 'product_id' => $product_id, 'code' => 'aipt_ability_apply_skipped', 'message' => (string) $outcome['message'] ); } /** * Write the current (in-progress) outcome to the token, marked NOT * finished: computeStatus() has no notion of "more products remain" and * can report a terminal-looking status (e.g. 'completed') for a * checkpoint taken mid-loop. ConfirmContentRunService::finish() records * the run's real, final outcome once this whole loop returns. * * @return void */ private function recordProgress( string $token, string $generator, array $run_pin_echo, array $blueprints_echo, array $applied, array $staged, array $held_back, array $skipped, array $failed, array $plan_limits ): void { $status = $this->outcomeBuilder->computeStatus( $applied, $staged, $held_back, $failed ); $this->tokens->recordOutcome( $token, $this->outcomeBuilder->responseShape( $status, $generator, $run_pin_echo, $blueprints_echo, $applied, $staged, $held_back, $skipped, $failed, array_values( $plan_limits ) ), false ); } }