$data { * @type string $generator * @type array $subject * @type string $mode * @type array|null $draft * @type int|null $blueprintId * @type int|string|null $runPin * @type array $runOptions * @type float|null $deadline Default now + 60s. * @type int|null $maxCalls Default 3. * @type string $origin Default 'test'. * @type int $userId Default 0. * @type int|null $clientVersion * @type string|null $clientRequestId * @type array|null $automationContext Default empty array. * } */ public function __construct( array $data ) { $this->generator = (string) ( $data['generator'] ?? '' ); $this->subject = $this->normalizeSubject( is_array( $data['subject'] ?? null ) ? $data['subject'] : array() ); $this->mode = 'run' === ( $data['mode'] ?? 'test' ) ? 'run' : 'test'; $this->draft = is_array( $data['draft'] ?? null ) ? $data['draft'] : null; $this->blueprintId = isset( $data['blueprintId'] ) && null !== $data['blueprintId'] ? (int) $data['blueprintId'] : null; $this->runPin = $this->normalizeRunPin( $data['runPin'] ?? null ); $this->runOptions = $this->normalizeRunOptions( is_array( $data['runOptions'] ?? null ) ? $data['runOptions'] : array() ); $this->deadline = isset( $data['deadline'] ) ? (float) $data['deadline'] : ( microtime( true ) + 60.0 ); $this->maxCalls = isset( $data['maxCalls'] ) ? max( 1, (int) $data['maxCalls'] ) : 3; $this->origin = (string) ( $data['origin'] ?? 'test' ); $this->userId = (int) ( $data['userId'] ?? 0 ); $this->clientVersion = isset( $data['clientVersion'] ) && null !== $data['clientVersion'] ? (int) $data['clientVersion'] : null; $this->clientRequestId = isset( $data['clientRequestId'] ) && '' !== $data['clientRequestId'] ? (string) $data['clientRequestId'] : null; $this->automationContext = is_array( $data['automationContext'] ?? null ) ? $data['automationContext'] : array(); } /** * @param array $data Same shape as the constructor. * @return self */ public static function fromArray( array $data ): self { return new self( $data ); } /** * @param array $subject Raw subject input. * @return array{type:string,id:int,productId?:int} */ private function normalizeSubject( array $subject ): array { $normalized = array( 'type' => (string) ( $subject['type'] ?? '' ), 'id' => (int) ( $subject['id'] ?? 0 ), ); if ( isset( $subject['productId'] ) ) { $normalized['productId'] = (int) $subject['productId']; } return $normalized; } /** * @param mixed $run_pin Raw runPin input. * @return int|string|null */ private function normalizeRunPin( $run_pin ) { if ( null === $run_pin || '' === $run_pin ) { return null; } return is_numeric( $run_pin ) ? (int) $run_pin : (string) $run_pin; } /** * @param array $run_options Raw runOptions input. * @return array{companions:array,wantSections:bool,language:string,provider:string,model:string,allowExistingDescription:bool,improveExisting:bool,preserveContentImages:bool,overwrite:string,additionalInstruction:string,excludeQuestions:string[]} */ private function normalizeRunOptions( array $run_options ): array { $companions = is_array( $run_options['companions'] ?? null ) ? $run_options['companions'] : array(); return array( 'companions' => array( 'shortDescription' => ! empty( $companions['shortDescription'] ), 'tags' => ! empty( $companions['tags'] ), ), 'wantSections' => ! isset( $run_options['wantSections'] ) || (bool) $run_options['wantSections'], 'language' => (string) ( $run_options['language'] ?? '' ), 'provider' => (string) ( $run_options['provider'] ?? '' ), 'model' => (string) ( $run_options['model'] ?? '' ), 'allowExistingDescription' => ! empty( $run_options['allowExistingDescription'] ), 'improveExisting' => ! empty( $run_options['improveExisting'] ), 'preserveContentImages' => $this->resolvePreserveContentImages( $run_options ), 'overwrite' => 'empty_only' === ( $run_options['overwrite'] ?? 'replace' ) ? 'empty_only' : 'replace', 'variants' => max( 1, min( self::MAX_VARIANTS, (int) ( $run_options['variants'] ?? 1 ) ) ), 'attachmentIds' => $this->normalizeAttachmentIds( $run_options['attachmentIds'] ?? array() ), 'additionalInstruction' => (string) ( $run_options['additionalInstruction'] ?? '' ), 'excludeQuestions' => $this->normalizeExcludeQuestions( $run_options['excludeQuestions'] ?? array() ), 'generateTitle' => ! array_key_exists( 'generateTitle', $run_options ) || ! empty( $run_options['generateTitle'] ), 'generateDescription' => ! array_key_exists( 'generateDescription', $run_options ) || ! empty( $run_options['generateDescription'] ), 'generateFocusKeyword' => ! array_key_exists( 'generateFocusKeyword', $run_options ) || ! empty( $run_options['generateFocusKeyword'] ), ); } /** * @param mixed $questions Raw question list. * @return string[] Trimmed, unique, non-empty questions, at most MAX_EXCLUDE_QUESTIONS. */ private function normalizeExcludeQuestions( $questions ): array { if ( ! is_array( $questions ) ) { return array(); } $clean = array(); foreach ( $questions as $question ) { $question = is_string( $question ) ? mb_substr( trim( sanitize_text_field( $question ) ), 0, 300 ) : ''; if ( '' !== $question && ! in_array( $question, $clean, true ) ) { $clean[] = $question; } if ( count( $clean ) >= self::MAX_EXCLUDE_QUESTIONS ) { break; } } return $clean; } /** * "Keep images in descriptions" for this run. * * The generation screens send camelCase, while Automation jobs still send * the legacy snake_case key. When neither is sent (Test and preview, or * any caller without the control), the store's own default applies, * exactly as it does on the custom-prompts path. * * @param array $run_options Raw runOptions input. * @return bool */ private function resolvePreserveContentImages( array $run_options ): bool { if ( array_key_exists( 'preserveContentImages', $run_options ) ) { return ! empty( $run_options['preserveContentImages'] ); } if ( array_key_exists( 'preserve_content_images', $run_options ) ) { return ! empty( $run_options['preserve_content_images'] ); } return (bool) get_option( 'aipt_preserve_content_images', true ); } /** * Optional image selection for image_alt runs on a product subject. * * @param mixed $ids Raw ids. * @return int[] Positive, unique ids, at most MAX_ATTACHMENTS. */ private function normalizeAttachmentIds( $ids ): array { if ( ! is_array( $ids ) ) { return array(); } $clean = array(); foreach ( $ids as $id ) { $id = (int) $id; if ( $id > 0 && ! in_array( $id, $clean, true ) ) { $clean[] = $id; } if ( count( $clean ) >= self::MAX_ATTACHMENTS ) { break; } } return $clean; } /** * @return string */ public function getGenerator(): string { return $this->generator; } /** * @return array{type:string,id:int,productId?:int} */ public function getSubject(): array { return $this->subject; } /** * @return string 'test' | 'run'. */ public function getMode(): string { return $this->mode; } /** * @return array{definition:array,brandProfileId:int,name:string}|null */ public function getDraft(): ?array { return $this->draft; } /** * @return int|null */ public function getBlueprintId(): ?int { return $this->blueprintId; } /** * @return int|string|null */ public function getRunPin() { return $this->runPin; } /** * @return array{companions:array,wantSections:bool,language:string,provider:string,model:string,allowExistingDescription:bool,improveExisting:bool,preserveContentImages:bool,overwrite:string} */ public function getRunOptions(): array { return $this->runOptions; } /** * @return float */ public function getDeadline(): float { return $this->deadline; } /** * @return int */ public function getMaxCalls(): int { return $this->maxCalls; } /** * @return string */ public function getOrigin(): string { return $this->origin; } /** * @return int */ public function getUserId(): int { return $this->userId; } /** * @return int|null */ public function getClientVersion(): ?int { return $this->clientVersion; } /** * @return string|null */ public function getClientRequestId(): ?string { return $this->clientRequestId; } /** * @return array{reviewGuaranteed?:bool} */ public function getAutomationContext(): array { return $this->automationContext; } }