option => Free value). */ private const FREE_DEFAULTS = [ 'title' => [ 'keywords' => '', 'mode' => 'rewrite', 'titleSources' => ['product_description'], ], 'faq' => [ 'faqMode' => 'both', 'writeMode' => 'append', 'faqSources' => ['product_description'], ], 'seo_meta' => [ 'titleMaxLength' => 60, 'descMaxLength' => 160, 'generateFocusKeyword' => false, 'optimizeForFocusKeyword' => false, 'variantCount' => 2, ], 'image_alt' => [ 'includeGallery' => false, 'enrichWithAttributes' => false, ], ]; /** * Paid option keys a translation replaced with Free defaults. * * A key counts only when the job asked for a non-default value and the * translated option ended on the Free default, so a restored (entitled) * value is never reported. FAQ writeMode is excluded: replace mode is * gated separately at apply time through aipt_faq_replace_enabled. * * @param string $tool_type Job tool type. * @param array $settings Stored job settings. * @param array $translated translate() output for the same settings. * @return string[] Dropped option keys. */ public static function appliedFreeDefaults(string $tool_type, array $settings, array $translated): array { $defaults = self::FREE_DEFAULTS[$tool_type] ?? []; unset($defaults['writeMode']); $dropped = []; foreach ($defaults as $key => $free_value) { if (!array_key_exists($key, $settings)) { continue; } if ('title' === $tool_type && 'titleSources' === $key) { $free_value = TitleGeneratorController::freeTitleSources($settings[$key]); } $requested = self::comparableOption($settings[$key]); $free = self::comparableOption($free_value); if (null === $requested || $requested === $free) { continue; } if (self::comparableOption($translated[$key] ?? null) === $free) { $dropped[] = $key; } } return $dropped; } /** * Canonical form for comparing an option value with its Free default. * * @param mixed $value Option value. * @return mixed Null for empty values. */ private static function comparableOption($value) { if (is_string($value)) { $value = trim($value); } if (null === $value || '' === $value || [] === $value) { return null; } if (is_bool($value)) { return $value; } if (is_numeric($value)) { return (float) $value > 0 ? (float) $value : null; } if (is_array($value)) { $items = array_map([self::class, 'comparableOption'], array_values($value)); sort($items); return wp_json_encode($items); } return trim((string) $value); } /** * Blueprint run options, derived from the job's own * settings.blueprint (never re-resolved — JobExecutor only reads the * pin a job already has). * * `jobMode` and `legacyPrompts` are the server-side facts * `BlueprintIntegration::executionPath()` expects as `$trusted`. A * generator fork never reads these two keys off $options itself * for security: $options is also the shape bulk generate REST * requests, Product Editor and * the metabox send, so a key living there could be set by any of * them too. Instead JobExecutor reads them from here and hands them to * `BlueprintIntegration::setJobTrustedFacts()` for the duration of one * generator call only (cleared immediately after), which no REST * request can reach — that ambient channel, not $options, is what * every Automation tool's generator fork actually reads. * * @param array $settings Raw job settings. * @return array{blueprintRun:int,blueprintId:int,jobMode:string,overwrite:string,legacyPrompts:bool} */ private static function blueprintOptions(array $settings): array { $blueprint = isset($settings['blueprint']) && is_array($settings['blueprint']) ? $settings['blueprint'] : []; $mode = isset($blueprint['mode']) ? (string) $blueprint['mode'] : ''; return [ 'blueprintRun' => isset($blueprint['runPinId']) ? max(0, (int) $blueprint['runPinId']) : 0, 'blueprintId' => isset($blueprint['manualBlueprintId']) ? max(0, (int) $blueprint['manualBlueprintId']) : 0, 'jobMode' => in_array($mode, ['blueprint', 'advanced'], true) ? $mode : '', 'overwrite' => self::overwriteMode($settings), 'legacyPrompts' => self::hasLegacyPrompts($settings), ]; } /** * This job's configured overwrite mode, read from the same * settings.blueprint.overwrite key blueprintOptions() folds into every * tool's translated run options for generation. The automation apply * path (JobExecutor::processResults()) reads it through here too, so * the apply step honors the identical job setting the generation step * already does instead of keeping its own, possibly drifting, copy. * * @param array $settings Raw job settings. * @return string 'replace' | 'empty_only'. */ public static function overwriteMode(array $settings): string { $blueprint = isset($settings['blueprint']) && is_array($settings['blueprint']) ? $settings['blueprint'] : []; return 'empty_only' === ($blueprint['overwrite'] ?? 'replace') ? 'empty_only' : 'replace'; } /** * Whether the job carries a non-empty per-job raw prompt override * (Automation's StepCustomPrompts, 'advanced' tool type only in * practice). This is the per-JOB legacy-prompt signal, distinct from * the per-GENERATOR execution mode: a job can carry its own custom * prompts even while the generator's default mode is 'blueprint'. * * True when either description length yields a prompt under * CustomPrompts::runOverride() semantics: the job's own * `prompts.{short,full}.custom_prompt` (2.8.1), or a carried-over * `system_prompt`/`user_prompt` pair merged on the fly. A job whose * ONLY saved prompt text is in the even-older flat * `{short,full}_{system,user}_prompt` settings keys (no `prompts` map * at all) is not detected here either, matching this method's * pre-2.8.1 behavior for that same shape. * * @param array $settings Raw job settings. * @return bool */ private static function hasLegacyPrompts(array $settings): bool { $prompts = isset($settings['prompts']) && is_array($settings['prompts']) ? $settings['prompts'] : null; foreach (CustomPrompts::types() as $type) { if (null !== CustomPrompts::runOverride($prompts, $type)) { return true; } } return false; } /** * This job's Description custom prompts, normalized to * `prompts.{short,full}.custom_prompt` (2.8.1) regardless of which era * saved the job: an existing non-empty `custom_prompt` for a length is * kept as-is; otherwise its `system_prompt`/`user_prompt` pair — from * the nested `settings.prompts.{short,full}` shape, or the even older * flat `{short,full}_{system,user}_prompt` settings keys — is merged * with CustomPrompts::merge(). A length with nothing on either shape is * left out of the result entirely. * * @param array $settings Raw job settings. * @return array */ private static function descriptionPrompts(array $settings): array { $raw = isset($settings['prompts']) && is_array($settings['prompts']) ? $settings['prompts'] : []; $prompts = []; foreach (CustomPrompts::types() as $type) { $pair = isset($raw[$type]) && is_array($raw[$type]) ? $raw[$type] : []; $custom = isset($pair['custom_prompt']) && is_string($pair['custom_prompt']) ? trim($pair['custom_prompt']) : ''; if ('' === $custom) { $system = isset($pair['system_prompt']) && is_string($pair['system_prompt']) ? $pair['system_prompt'] : (string) ($settings["{$type}_system_prompt"] ?? ''); $user = isset($pair['user_prompt']) && is_string($pair['user_prompt']) ? $pair['user_prompt'] : (string) ($settings["{$type}_user_prompt"] ?? ''); $custom = CustomPrompts::merge($system, $user); } if ('' !== $custom) { $prompts[$type] = ['custom_prompt' => $custom]; } } return $prompts; } public function translate(array $settings, string $tool_type): array { $tool_type = sanitize_text_field($tool_type); $provider = isset($settings['provider']) ? sanitize_text_field($settings['provider']) : 'openai'; $model = isset($settings['model']) ? sanitize_text_field($settings['model']) : \AIPT\Api\ModelCatalog::get_default_model('openai'); $model = \AIPT\Api\ModelCatalog::normalize_model($provider, $model); $writing_style = isset($settings['writingStyle']) ? sanitize_text_field($settings['writingStyle']) : (isset($settings['writing_style']) ? sanitize_text_field($settings['writing_style']) : 'Professional'); $contentSettings = isset($settings['contentSettings']) && is_array($settings['contentSettings']) ? $settings['contentSettings'] : []; $base = [ 'provider' => $provider, 'model' => $model, 'writingStyle' => $writing_style, 'writing_style' => $writing_style, 'contentSettings' => $contentSettings, ]; if (isset($settings['language']) && '' !== trim((string) $settings['language'])) { $base['language'] = sanitize_text_field($settings['language']); } $base = array_merge($base, self::blueprintOptions($settings)); if ($tool_type === 'title') { $max_length_old = isset($settings['maxTitleLength']) ? (int) $settings['maxTitleLength'] : null; $max_length = isset($settings['maxLength']) ? (int) $settings['maxLength'] : ($max_length_old ?? 80); $base['maxTitleLength'] = $max_length; $base['maxLength'] = (string) $max_length; if (isset($settings['variantCount'])) { $base['variantCount'] = (int) $settings['variantCount']; } if (isset($settings['improveExisting'])) { $base['improveExisting'] = (bool) $settings['improveExisting']; } return $this->applyTitlePremiumControls($base, $settings); } if ($tool_type === 'faq') { if (isset($settings['faqCount'])) { $base['faqCount'] = FAQGeneratorService::normalize_faq_count($settings['faqCount']); } if (isset($settings['faqFormat'])) { $base['faqFormat'] = sanitize_text_field((string) $settings['faqFormat']); } if (isset($settings['faqMode'])) { $base['faqMode'] = sanitize_text_field((string) $settings['faqMode']); } if (isset($settings['writeMode'])) { $base['writeMode'] = sanitize_text_field((string) $settings['writeMode']); } if (isset($settings['faqSources']) && is_array($settings['faqSources'])) { $base['faqSources'] = array_values(array_map('sanitize_text_field', $settings['faqSources'])); } return $this->applyFaqPremiumControls($base); } if ($tool_type === 'seo_meta') { foreach (['generateTitle', 'generateDescription', 'generateFocusKeyword', 'optimizeForFocusKeyword'] as $key) { if (isset($settings[$key])) { $base[$key] = (bool) $settings[$key]; } } foreach (['titleMaxLength', 'descMaxLength', 'variantCount'] as $key) { if (isset($settings[$key])) { $base[$key] = (int) $settings[$key]; } } if (isset($settings['tone'])) { $base['tone'] = sanitize_text_field((string) $settings['tone']); } if (isset($settings['sources']) && is_array($settings['sources'])) { $base['sources'] = array_values(array_filter(array_map('sanitize_key', $settings['sources']))); } return $this->applySeoMetaPremiumControls($base); } if ($tool_type === 'image_alt') { if (isset($settings['includeGallery'])) { $base['includeGallery'] = (bool) $settings['includeGallery']; } if (isset($settings['maxLength'])) { $base['maxLength'] = (int) $settings['maxLength']; } if (isset($settings['tone'])) { $base['tone'] = sanitize_text_field((string) $settings['tone']); } if (isset($settings['enrichWithAttributes'])) { $base['enrichWithAttributes'] = (bool) $settings['enrichWithAttributes']; } return $this->applyImageAltPremiumControls($base); } $base['generateDescription'] = !empty($settings['generateDescription']); $base['generateShortDescription'] = !empty($settings['generateShortDescription']); $base['generateTags'] = !empty($settings['generateTags']); if (array_key_exists('preserve_content_images', $settings)) { $base['preserve_content_images'] = (bool) $settings['preserve_content_images']; } elseif (array_key_exists('preserveContentImages', $settings)) { $base['preserve_content_images'] = (bool) $settings['preserveContentImages']; } if (isset($settings['content_length_preset'])) { $base['content_length_preset'] = sanitize_text_field($settings['content_length_preset']); } else { $base['content_length_preset'] = 'long'; } if (isset($settings['short_length_preset'])) { $base['short_length_preset'] = sanitize_text_field($settings['short_length_preset']); } else { $base['short_length_preset'] = 'standard'; } if (isset($settings['max_tags_count'])) { $base['max_tags_count'] = (string) $settings['max_tags_count']; } if (isset($settings['improveExistingDescription'])) { $base['improveExistingDescription'] = (bool) $settings['improveExistingDescription']; } if (isset($settings['keepExistingDescription'])) { $base['keepExistingDescription'] = (bool) $settings['keepExistingDescription']; } if (isset($settings['additionalTagsCount'])) { $base['additionalTagsCount'] = (int) $settings['additionalTagsCount']; } if (isset($settings['existingDescriptionPosition'])) { $pos = in_array($settings['existingDescriptionPosition'], ['before','after'], true) ? $settings['existingDescriptionPosition'] : 'after'; $base['existingDescriptionPosition'] = $pos; } if (isset($settings['beforeContentText'])) { $base['beforeContentText'] = sanitize_text_field((string) $settings['beforeContentText']); } if (isset($settings['afterContentText'])) { $base['afterContentText'] = sanitize_text_field((string) $settings['afterContentText']); } if (isset($settings['seo'])) { $base['seo'] = is_array($settings['seo']) ? $settings['seo'] : []; } $prompts = self::descriptionPrompts($settings); if (!empty($prompts)) { $base['prompts'] = $prompts; } return $base; } private function applyTitlePremiumControls(array $options, array $settings): array { $free_defaults = self::FREE_DEFAULTS['title']; $free_defaults['titleSources'] = TitleGeneratorController::freeTitleSources($settings['titleSources'] ?? null); if (isset($settings['tone'])) { $options['tone'] = sanitize_text_field((string) $settings['tone']); } $controlled = array_merge($options, $free_defaults); $controlled = apply_filters('aipt_title_validate_options', $controlled, $settings); $controlled['tone'] = TitleGeneratorController::normalizeTone($controlled['tone'] ?? ''); return $controlled; } private function applyFaqPremiumControls(array $options): array { $free_defaults = self::FREE_DEFAULTS['faq']; $controlled = array_merge($options, $free_defaults); return apply_filters('aipt_faq_validate_options', $controlled, $options); } private function applySeoMetaPremiumControls(array $options): array { $free_defaults = self::FREE_DEFAULTS['seo_meta']; $controlled = array_merge($options, $free_defaults); $controlled = apply_filters('aipt_seo_meta_validate_options', $controlled, $options); $controlled['tone'] = SeoMetaGeneratorController::normalizeTone($controlled['tone'] ?? ''); $allowed_sources = [ 'product_name', 'product_brand', 'product_description', 'short_description', 'product_categories', 'product_tags', 'product_attributes', 'product_sku', 'product_price', ]; if (isset($controlled['sources']) && is_array($controlled['sources'])) { $controlled['sources'] = array_values(array_intersect($controlled['sources'], $allowed_sources)); if (empty($controlled['sources'])) { $controlled['sources'] = ['product_name', 'product_description', 'product_categories']; } } else { $controlled['sources'] = ['product_name', 'product_description', 'product_categories']; } return $controlled; } private function applyImageAltPremiumControls(array $options): array { $free_defaults = self::FREE_DEFAULTS['image_alt']; $controlled = array_merge($options, $free_defaults); $controlled = apply_filters('aipt_image_alt_validate_options', $controlled, $options); $controlled['tone'] = ImageAltController::normalizeTone($controlled['tone'] ?? ''); return $controlled; } }