pins = $pins ?? new RunPinService(); $this->resolver = $resolver ?? new AssignmentResolver(); } /** * The kill switch, custom-prompts mode and review-gate re-checks, plus * loading the pin — the same errors the preview already returns for the * first two. * * @param string $generator Generator key. * @param string $destination 'apply' | 'review'. * @param int $pin_id Stored pin id. * @return array|WP_Error The loaded pin, or an error. */ public function recheckBeforeRun( string $generator, string $destination, int $pin_id ) { $gate = AiSupportGate::guard(); if ( $gate instanceof WP_Error ) { return $gate; } $path = BlueprintIntegration::executionPath( $generator, array() ); if ( BlueprintIntegration::PATH_BLUEPRINT !== $path['path'] ) { return new WP_Error( 'aipt_ability_custom_prompts', __( 'This content type runs on custom prompts. Preview and confirm work only with its content template.', 'ai-product-tools' ), array( 'status' => 409 ) ); } if ( 'review' === $destination && ! apply_filters( 'aipt_content_review_enabled', false ) ) { return new WP_Error( 'aipt_ability_review_unavailable', __( 'The review queue is not available on this plan. Choose apply instead.', 'ai-product-tools' ), array( 'status' => 403 ) ); } $pin = $this->pins->loadRunPin( $pin_id ); if ( null === $pin ) { return new WP_Error( 'aipt_ability_pin_gone', __( 'The pinned run no longer exists. Run preview-content-run again.', 'ai-product-tools' ), array( 'status' => 410 ) ); } return $pin; } /** * Re-check existence/type/status, `edit_post`, and each product's group * under the pin. The existence/type/status check mirrors * {@see \AIPT\Api\ContentStructure\Ability\PreviewContentRunService::permissionSkipReason()}'s * own preview-time check: a product trashed, deleted, or turned into a * variation in the window between preview and confirm must not reach * generation again, the same way it never would have reached preview in * the first place. * * @param string $generator Generator key. * @param array $product_ids Stored product ids. * @param array $pin Loaded run pin. * @param array $groups_by_product Preview-time group per product id. * @return array{0:int[],1:array} [$remaining, $skipped]. */ public function reResolveProducts( string $generator, array $product_ids, array $pin, array $groups_by_product ): array { $skipped = array(); $remaining = array(); foreach ( $product_ids as $product_id ) { $product_id = (int) $product_id; $product = function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : false; if ( ! ProductEligibility::isEligible( $product ) ) { $skipped[] = array( 'product_id' => $product_id, 'reason' => 'not_found' ); continue; } if ( ! current_user_can( 'edit_post', $product_id ) ) { $skipped[] = array( 'product_id' => $product_id, 'reason' => 'forbidden' ); continue; } $resolution = $this->resolver->resolve( $generator, array( 'type' => 'product', 'id' => $product_id ), null, $pin['payload'] ); if ( $resolution instanceof WP_Error || null !== $resolution['conflict'] ) { $skipped[] = array( 'product_id' => $product_id, 'reason' => 'conflict' ); continue; } $current_key = null === $resolution['blueprintId'] ? 'builtin' : (string) $resolution['blueprintId']; $stored_key = (string) ( $groups_by_product[ $product_id ] ?? '' ); if ( $current_key !== $stored_key ) { $skipped[] = array( 'product_id' => $product_id, 'reason' => 'changed_since_preview' ); continue; } $remaining[] = $product_id; } return array( $remaining, $skipped ); } }