'advanced', 'manualBlueprintId' => null, 'overwrite' => $overwrite, ); return $settings; } if ( ! empty( $incoming['trustExistingPin'] ) ) { $trusted = self::trustedPin( $generator, $incoming, $overwrite ); if ( null !== $trusted ) { $settings['blueprint'] = $trusted; return $settings; } } $manual_id = isset( $incoming['manualBlueprintId'] ) && null !== $incoming['manualBlueprintId'] && (int) $incoming['manualBlueprintId'] > 0 ? (int) $incoming['manualBlueprintId'] : null; if ( null !== $manual_id ) { $mismatch = self::validateManualBlueprint( $generator, $manual_id ); if ( is_wp_error( $mismatch ) ) { return $mismatch; } } if ( self::choiceUnchanged( $previous_settings, $manual_id ) ) { $settings['blueprint'] = $previous_settings['blueprint']; $settings['blueprint']['overwrite'] = $overwrite; return $settings; } $pinned = self::pin( $generator, $manual_id, $overwrite, $user_id ); if ( is_wp_error( $pinned ) ) { return $pinned; } $settings['blueprint'] = $pinned; return $settings; } /** * Always mint a fresh pin for the job's CURRENT saved structure choice, * for a recurring cycle start. A no-op (returns the settings unchanged, * with an empty `blueprints` list) for the advanced path or a job that * carries no structure choice at all. * * @param string $tool_type Job tool_type. * @param array $settings Job settings. * @param int $user_id Acting user id — the job's own creator, since * a recurring cycle starts from a cron tick with * no "current user". * @return array{settings:array,blueprints:array,pinFailed:bool} `pinFailed` is true only when a repin was actually attempted and the mint failed ({@see \AIPT\Core\ContentStructure\Execution\RunPinService::pinFailed()}); `settings` then comes back UNCHANGED (the job's existing valid pin), never a broken runPinId of 0, so the caller can warn instead of persisting it or logging a false "This cycle uses ..." line. */ public static function forceRepin( string $tool_type, array $settings, int $user_id ): array { $blueprint = isset( $settings['blueprint'] ) && is_array( $settings['blueprint'] ) ? $settings['blueprint'] : null; if ( null === $blueprint || 'advanced' === ( $blueprint['mode'] ?? '' ) ) { return array( 'settings' => $settings, 'blueprints' => array(), 'pinFailed' => false, ); } $generator = Generators::fromAutomationToolType( $tool_type ); if ( null === $generator ) { return array( 'settings' => $settings, 'blueprints' => array(), 'pinFailed' => false, ); } $manual_id = isset( $blueprint['manualBlueprintId'] ) && null !== $blueprint['manualBlueprintId'] && (int) $blueprint['manualBlueprintId'] > 0 ? (int) $blueprint['manualBlueprintId'] : null; $overwrite = 'empty_only' === ( $blueprint['overwrite'] ?? 'replace' ) ? 'empty_only' : 'replace'; $pinned = self::pin( $generator, $manual_id, $overwrite, $user_id ); if ( is_wp_error( $pinned ) ) { return array( 'settings' => $settings, 'blueprints' => array(), 'pinFailed' => true, ); } $settings['blueprint'] = $pinned; return array( 'settings' => $settings, 'blueprints' => $pinned['blueprints'], 'pinFailed' => false, ); } /** * Whether a submitted manual pick is identical to the job's previously * saved choice AND that choice already carries a usable pin — the only * case where create/update should keep the existing pin rather than * minting a new one. * * @param array $previous_settings The job's settings before this write. * @param int|null $manual_id Submitted manual Blueprint id, or null. * @return bool */ private static function choiceUnchanged( array $previous_settings, ?int $manual_id ): bool { $previous = isset( $previous_settings['blueprint'] ) && is_array( $previous_settings['blueprint'] ) ? $previous_settings['blueprint'] : null; if ( null === $previous || 'advanced' === ( $previous['mode'] ?? '' ) ) { return false; } $previous_manual_id = isset( $previous['manualBlueprintId'] ) && null !== $previous['manualBlueprintId'] ? (int) $previous['manualBlueprintId'] : null; $previous_pin_id = (int) ( $previous['runPinId'] ?? 0 ); return $previous_pin_id > 0 && $previous_manual_id === $manual_id; } /** * Mint a fresh run pin and build the stored `settings.blueprint` shape. * * @param string $generator Blueprint generator key. * @param int|null $manual_id Manually picked Blueprint id, or null. * @param string $overwrite 'replace' | 'empty_only'. * @param int $user_id Acting user id. * @return array{mode:string,manualBlueprintId:int|null,runPinId:int,runPinHash:string,pinnedAt:string,blueprints:array,overwrite:string}|WP_Error `aipt_blueprint_pin_failed` (500) when the underlying run-pin insert failed ({@see RunPinService::pinFailed()}). */ private static function pin( string $generator, ?int $manual_id, string $overwrite, int $user_id ) { $pin = self::pinService()->createRunPin( $generator, $manual_id, $user_id ); if ( RunPinService::pinFailed( $pin ) ) { return new WP_Error( 'aipt_blueprint_pin_failed', __( 'This run could not be pinned. Please try again.', 'ai-product-tools' ), array( 'status' => 500 ) ); } return array( 'mode' => 'blueprint', 'manualBlueprintId' => $manual_id, 'runPinId' => $pin['id'], 'runPinHash' => $pin['hash'], 'pinnedAt' => $pin['createdAt'], 'blueprints' => $pin['blueprints'], 'overwrite' => $overwrite, ); } /** * Build the stored `settings.blueprint` shape straight from an EXISTING * run pin the caller claims to already hold, instead of minting a new * one from current live data. * * The claim is independently verified, never trusted at face value: the * pin must actually load by id and its own recorded generator must * match this job's. Every stored field comes from the LOADED pin, not * from $incoming — including manualBlueprintId and blueprints — so a * caller cannot claim a display shape the pin itself does not have. * * @param string $generator Resolved generator key. * @param array $incoming Submitted settings.blueprint (carries `runPinId` and the `trustExistingPin` marker). * @param string $overwrite Already-normalized 'replace' | 'empty_only' — a sibling run option, not part of the pinned choice itself. * @return array{mode:string,manualBlueprintId:int|null,runPinId:int,runPinHash:string,pinnedAt:string,blueprints:array,overwrite:string}|null The stored shape, or null when the claimed pin cannot be verified (the caller then falls back to minting a fresh one). */ private static function trustedPin( string $generator, array $incoming, string $overwrite ): ?array { $pin_id = isset( $incoming['runPinId'] ) ? (int) $incoming['runPinId'] : 0; if ( $pin_id <= 0 ) { return null; } $pin = self::pinService()->loadRunPin( $pin_id ); if ( null === $pin || ! is_array( $pin ) || (string) ( $pin['generator'] ?? '' ) !== $generator ) { return null; } return array( 'mode' => 'blueprint', 'manualBlueprintId' => $pin['manualBlueprintId'] ?? null, 'runPinId' => $pin['id'], 'runPinHash' => $pin['hash'], 'pinnedAt' => $pin['createdAt'], 'blueprints' => $pin['blueprints'], 'overwrite' => $overwrite, ); } /** * Reject a manually picked Blueprint that does not exist or belongs to a * different generator. * * @param string $generator Expected generator key. * @param int $blueprint_id Manually picked Blueprint id. * @return WP_Error|null */ private static function validateManualBlueprint( string $generator, int $blueprint_id ): ?WP_Error { $blueprint = self::blueprintStore()->get( $blueprint_id ); if ( null === $blueprint ) { return new WP_Error( 'aipt_blueprint_not_found', __( 'The selected template no longer exists.', 'ai-product-tools' ), array( 'status' => 400 ) ); } if ( (string) $blueprint['generator'] !== $generator ) { return new WP_Error( 'aipt_blueprint_generator_mismatch', __( 'The selected template is for a different content type.', 'ai-product-tools' ), array( 'status' => 400 ) ); } return null; } /** * @return RunPinService */ private static function pinService() { if ( null !== self::$pin_service_factory ) { return call_user_func( self::$pin_service_factory ); } return new RunPinService(); } /** * @return BlueprintStore */ private static function blueprintStore() { if ( null !== self::$blueprint_store_factory ) { return call_user_func( self::$blueprint_store_factory ); } return new BlueprintStore(); } }