clampTitlePremiumSources( $definition, $applied ); $this->clampTitleKeywords( $definition, $applied ); } /** * The data sources a Title run withholds on this plan. * * @return string[] */ public function lockedSourceRefs(): array { return $this->titleAdvancedSourcesAllowed() ? array() : self::TITLE_PREMIUM_SOURCE_REFS; } /** * Whether this site may use a title data source beyond the legacy free * defaults ('product_name', 'product_description'). Replays the EXACT filter * `TitleGeneratorController::applyPremiumControls()` applies * (`aipt_title_validate_options`, registered by * `Premium\BulkGenerator\Title\PremiumTitleController::validate_premium_options()`) * over a synthetic request asking for every legacy source, so a licensed * site is restored exactly the way the legacy controller restores it — * this never duplicates the entitlement decision, only the request * shape it is made from. Public so the Blueprint editor locks the same * sources this clamp withholds. * * @return bool */ public function titleAdvancedSourcesAllowed(): bool { $all_sources = array( 'existing_title', 'product_name', 'product_description', 'product_tags', 'product_categories', 'product_attributes' ); $free_default = TitleGeneratorController::FREE_TITLE_SOURCES; $requested = array( 'titleSources' => $all_sources ); $free_defaults = array( 'titleSources' => $free_default ); $controlled = apply_filters( 'aipt_title_validate_options', array_merge( $requested, $free_defaults ), $requested ); $restored = ( is_array( $controlled ) && is_array( $controlled['titleSources'] ?? null ) ) ? $controlled['titleSources'] : $free_default; return ! empty( array_diff( $restored, $free_default ) ); } /** * Drop the title data sources the legacy generator kept for Business * (everything except the product name and description) when this site is not * entitled, by stripping their FieldRef equivalents from the shared * `sources.productFields` allowlist: the context an `ai`-mode TitlePart * reads from. The legacy gate decided what the AI reads; it never had * `data`-mode parts, which copy one field's value into the title as is * (the built-in templates' brand and product type parts), so those keep * working on every plan. Default fields are withheld without an entry * ({@see anyChosenRef()}). * * @param array &$definition Clamped definition (mutated in place). * @param array &$applied Accumulated appliedFreeDefaults entries. * @return void */ private function clampTitlePremiumSources( array &$definition, array &$applied ): void { if ( $this->titleAdvancedSourcesAllowed() ) { return; } $removed = $this->stripProductFieldRefs( $definition, function ( string $ref ): bool { return $this->isTitlePremiumSourceRef( $ref ); } ); if ( $this->anyChosenRef( $removed ) ) { $applied[] = $this->freeDefault( 'title:sources', 'advancedTitleOptions', __( 'This template used a title data source that was not used in this run, so the product name and description were used instead.', 'ai-product-tools' ), __( 'more title data sources', 'ai-product-tools' ) ); } } /** * Whether this site may have the AI work a structure's keywords into a * title (`structure.keywords`). Replays the EXACT filter * `TitleGeneratorController::applyPremiumControls()` applies * (`aipt_title_validate_options`, which restores the legacy paid * `keywords` option) over a synthetic request carrying a keyword, the * same way {@see titleImproveExistingAllowed()} replays the mode. Public * so the Blueprint editor shows the field only where a run reads it. * * @return bool */ public function titleKeywordsAllowed(): bool { $requested = array( 'keywords' => 'keyword' ); $free_defaults = array( 'keywords' => '' ); $controlled = apply_filters( 'aipt_title_validate_options', array_merge( $requested, $free_defaults ), $requested ); return is_array( $controlled ) && is_string( $controlled['keywords'] ?? null ) && '' !== trim( $controlled['keywords'] ); } /** * Drop a Title structure's keywords on a site that may not use them, and * say so: only a merchant ever adds keywords, so the entry always * follows a choice of theirs, never a default. * * @param array &$definition Clamped definition (mutated in place). * @param array &$applied Accumulated appliedFreeDefaults entries. * @return void */ private function clampTitleKeywords( array &$definition, array &$applied ): void { if ( empty( $definition['structure']['keywords'] ) || $this->titleKeywordsAllowed() ) { return; } $definition['structure']['keywords'] = array(); $applied[] = $this->freeDefault( 'title:keywords', 'advancedTitleOptions', __( 'This template lists keywords to work into the title, but the title was written without them.', 'ai-product-tools' ), __( 'title keywords', 'ai-product-tools' ) ); } /** * @param string $ref Candidate FieldRef. * @return bool Whether $ref is one of the title sources kept for Business. */ private function isTitlePremiumSourceRef( string $ref ): bool { return in_array( $ref, self::TITLE_PREMIUM_SOURCE_REFS, true ) || 0 === strpos( $ref, 'attr:' ); } /** * Whether this site may use Title's "Improve existing text" run option * (`runOptions.improveExisting`). Replays the EXACT filter * `TitleGeneratorController::applyPremiumControls()` applies * (`aipt_title_validate_options` - the legacy mode gate, the same filter * {@see titleAdvancedSourcesAllowed()} replays for the source list) * asking for `mode: 'improve'`, so a licensed site is restored exactly * the way the legacy controller restores it. * * Public: `runOptions.improveExisting` is a run option, not a definition * field, so {@see EntitlementPolicy::apply()} never sees it - the same reason * {@see SeoMetaPolicy::seoMetaMaxVariants()} is public and read directly by * `ExecutionService` where it clamps `$run_options`. * * @return bool */ public function titleImproveExistingAllowed(): bool { $requested = array( 'mode' => 'improve' ); $free_defaults = array( 'mode' => 'rewrite' ); $controlled = apply_filters( 'aipt_title_validate_options', array_merge( $requested, $free_defaults ), $requested ); $mode = ( is_array( $controlled ) && is_string( $controlled['mode'] ?? null ) ) ? $controlled['mode'] : $free_defaults['mode']; return 'improve' === $mode; } /** * The `appliedFreeDefaults` entry for `runOptions.improveExisting` being * turned off for a run because this site is not entitled. Added to the * result only by a caller that already knows the run actually requested * it - `ExecutionService` is where that comparison is made (never this * class, which has no run option to compare against), the same pattern * {@see SeoMetaPolicy::seoMetaVariantsFreeDefault()} already uses. * * @return array{key:string,reason:string} */ public function titleImproveExistingFreeDefault(): array { return $this->freeDefault( 'title:improveExisting', 'advancedTitleOptions', __( 'This template asked to improve the existing title, so a new title was written from product data instead.', 'ai-product-tools' ), __( 'improving the existing title', 'ai-product-tools' ) ); } }