paid feature key ('pro' tier, any paid plan). */ private const FILTER_FEATURES = array( 'categories' => 'job_filter_categories', 'tags' => 'job_filter_tags', 'stockStatus' => 'job_filter_stock_status', 'status' => 'job_filter_product_status', ); /** * Product status a job selects without a product status filter (the Free * scope). */ public const DEFAULT_PRODUCT_STATUS = 'publish'; /** * Product statuses a paid product status filter may restrict a job to. */ public const PAID_PRODUCT_STATUSES = array( 'draft', 'pending', 'private' ); /** * Tool type => paid generation-options feature key ('pro' tier). */ private const OPTION_FEATURES = array( 'title' => 'advancedTitleOptions', 'faq' => 'advancedFaqOptions', 'seo_meta' => 'advancedSeoOptions', 'image_alt' => 'imageAltEnrichment', ); /** * Normalized paid filter values a saved product filter asks for. * * @param array $filter Saved or requested product filter. * @return array{categories:int[], tags:int[], stockStatus:string, status:string} */ public static function paidFilterValues( array $filter ): array { $stock = isset( $filter['stockStatus'] ) && is_string( $filter['stockStatus'] ) ? $filter['stockStatus'] : 'all'; return array( 'categories' => ProductIdListFilter::parse( $filter['categories'] ?? null ), 'tags' => ProductIdListFilter::parse( $filter['tags'] ?? null ), 'stockStatus' => in_array( $stock, array( 'instock', 'outofstock', 'onbackorder' ), true ) ? $stock : 'all', 'status' => self::productStatus( $filter ), ); } /** * Product status a saved product filter selects. Anything but a paid * status (including the legacy 'all' the job form used to send) means * published products only. * * @param array $filter Saved or requested product filter. * @return string DEFAULT_PRODUCT_STATUS or one of PAID_PRODUCT_STATUSES. */ public static function productStatus( array $filter ): string { $status = $filter['status'] ?? ''; return is_string( $status ) && in_array( $status, self::PAID_PRODUCT_STATUSES, true ) ? $status : self::DEFAULT_PRODUCT_STATUS; } /** * Saved restricting filter keys the current plan cannot honor. * * @param array $filter Saved or requested product filter. * @return string[] Subset of categories, tags, stockStatus, status. */ public static function unhonoredFilterKeys( array $filter ): array { $requested = self::paidFilterValues( $filter ); if ( empty( $requested['categories'] ) && empty( $requested['tags'] ) && 'all' === $requested['stockStatus'] && self::DEFAULT_PRODUCT_STATUS === $requested['status'] ) { return array(); } $granted = self::grantedFilterOptions( $requested ); $keys = array(); if ( ! empty( $requested['categories'] ) && empty( $granted['categories'] ) ) { $keys[] = 'categories'; } if ( ! empty( $requested['tags'] ) && empty( $granted['tags'] ) ) { $keys[] = 'tags'; } if ( 'all' !== $requested['stockStatus'] && ( $granted['stock_status'] ?? 'all' ) !== $requested['stockStatus'] ) { $keys[] = 'stockStatus'; } if ( self::DEFAULT_PRODUCT_STATUS !== $requested['status'] && ( $granted['product_status'] ?? self::DEFAULT_PRODUCT_STATUS ) !== $requested['status'] ) { $keys[] = 'status'; } return $keys; } /** * The paid filter values this plan grants for a request, through the * legacy `aipt_automation_filter_options` provider: PremiumFilterProvider * restores the requested values only when the paid filter entitlement is * granted, so the Free defaults mean it was not. * * @param array{categories:int[], tags:int[], stockStatus:string, status:string} $requested paidFilterValues() output. * @return array Granted options: categories, tags, stock_status, product_status. */ public static function grantedFilterOptions( array $requested ): array { $granted = apply_filters( 'aipt_automation_filter_options', array( 'categories' => array(), 'tags' => array(), 'stock_status' => 'all', 'product_status' => self::DEFAULT_PRODUCT_STATUS, ), $requested['categories'], $requested['tags'], $requested['stockStatus'], $requested['status'] ); return is_array( $granted ) ? $granted : array(); } /** * Why a saved product filter blocks building a pool, or null. * * @param array $filter Saved product filter. * @param string $context CONTEXT_POOL for background work. * @return array|null Denial descriptor. */ public static function filterBlock( array $filter, string $context = self::CONTEXT_POOL ): ?array { $keys = self::unhonoredFilterKeys( $filter ); if ( empty( $keys ) ) { return null; } return self::describe( self::KIND_FILTERS, self::FILTER_FEATURES[ $keys[0] ], PlanDenial::PLAN_PAID, $keys, $context ); } /** * Why a newly selected paid filter value may not be stored, or null. * * @param array $requested Requested product filter. * @param array|null $stored Stored product filter on update, null on create. * @return array|null Denial descriptor. */ public static function filterWriteBlock( array $requested, ?array $stored ): ?array { if ( null !== $stored && self::paidFilterValues( $requested ) == self::paidFilterValues( $stored ) ) { return null; } return self::filterBlock( $requested, self::CONTEXT_RUN ); } /** * Whether a schedule config uses a recurring cadence. * * @param mixed $schedule_config Schedule config. * @return bool */ public static function isRecurring( $schedule_config ): bool { return is_array( $schedule_config ) && in_array( strtolower( (string) ( $schedule_config['type'] ?? '' ) ), self::RECURRING_TYPES, true ); } /** * Why recurring scheduling is not available, or null when it is. * * @param string $context CONTEXT_REARM for background work, CONTEXT_RUN for writes. * @return array|null Denial descriptor. */ public static function recurringBlock( string $context = self::CONTEXT_REARM ): ?array { if ( apply_filters( 'aipt_recurring_schedule_enabled', false ) ) { return null; } return self::describe( self::KIND_RECURRING, 'recurringSchedule', PlanDenial::PLAN_BUSINESS, array( 'schedule_config.type' ), $context ); } /** * Why a newly selected recurring cadence may not be stored, or null. * * @param array $requested Requested schedule config. * @param array|null $stored Stored schedule config on update, null on create. * @return array|null Denial descriptor. */ public static function recurringWriteBlock( array $requested, ?array $stored ): ?array { if ( ! self::isRecurring( $requested ) ) { return null; } if ( null !== $stored && self::cadenceSignature( $requested ) === self::cadenceSignature( $stored ) ) { return null; } return self::recurringBlock( self::CONTEXT_RUN ); } /** * Job content routing status exactly as JobExecutor::processResults() reads it. * * @param mixed $settings Job settings. * @return string 'draft' or 'published'. */ public static function contentStatus( $settings ): string { $settings = is_array( $settings ) ? $settings : array(); $content_settings = array(); if ( ! empty( $settings['contentSettings'] ) && is_array( $settings['contentSettings'] ) ) { $content_settings = $settings['contentSettings']; } elseif ( ! empty( $settings['content_settings'] ) && is_array( $settings['content_settings'] ) ) { $content_settings = $settings['content_settings']; } $status = $content_settings['status'] ?? 'draft'; return 'draft' === strtolower( is_string( $status ) ? $status : 'draft' ) ? 'draft' : 'published'; } /** * Job settings with the content routing status set under the key * contentStatus() reads, so JobExecutor routes the results by it. * * @param mixed $settings Job settings. * @param string $status 'draft' or 'published'. * @return array The whole settings array, every other key kept. */ public static function withContentStatus( $settings, string $status ): array { $settings = is_array( $settings ) ? $settings : array(); if ( ! empty( $settings['contentSettings'] ) && is_array( $settings['contentSettings'] ) ) { $settings['contentSettings']['status'] = $status; } elseif ( ! empty( $settings['content_settings'] ) && is_array( $settings['content_settings'] ) ) { $settings['content_settings']['status'] = $status; } else { $settings['contentSettings'] = array( 'status' => $status ); } return $settings; } /** * Resolve the review-routing part of a content settings write. * * @param array $content_settings Requested content settings. * @param string|null $stored_status On update the routing the job runs with (contentStatus()), null on create. * @return array|WP_Error Content settings to store, or the rejection. */ public static function resolveDraftStatusWrite( array $content_settings, ?string $stored_status ) { $has_status = isset( $content_settings['status'] ) && '' !== (string) $content_settings['status']; if ( ! $has_status && null !== $stored_status && '' !== $stored_status ) { $content_settings['status'] = $stored_status; return $content_settings; } $status = $has_status ? strtolower( sanitize_text_field( (string) $content_settings['status'] ) ) : 'draft'; if ( ! $has_status ) { $content_settings['status'] = 'draft'; } if ( 'draft' !== $status || apply_filters( 'aipt_content_review_enabled', false ) ) { return $content_settings; } if ( $has_status && null !== $stored_status && 'draft' === strtolower( $stored_status ) ) { return $content_settings; } $block = self::describe( self::KIND_DRAFT_REVIEW, 'reviewPolicy', PlanDenial::PLAN_BUSINESS, array( 'contentSettings.status' ), self::CONTEXT_RUN ); if ( $has_status || PlanDenial::isVerificationUnavailable( $block['reason'] ) ) { return self::restError( $block ); } $content_settings['status'] = 'published'; $content_settings['_draft_coerced'] = true; return $content_settings; } /** * Assess a job run: what must block before any provider call, and which * definitive denials fall back to Free behavior for this run. * * @param AutomationJob $job Job about to run. * @param array $translated_options OptionsTranslator output for the job. * @return array{block: array|null, clamps: array} */ public static function assessRun( AutomationJob $job, array $translated_options ): array { $settings = is_array( $job->settings ) ? $job->settings : array(); $tool = (string) $job->tool_type; $clamps = array(); if ( 'draft' === self::contentStatus( $settings ) ) { if ( ! apply_filters( 'aipt_content_review_enabled', false ) ) { return array( 'block' => self::describe( self::KIND_DRAFT_REVIEW, 'reviewPolicy', PlanDenial::PLAN_BUSINESS, array( 'contentSettings.status' ), self::CONTEXT_RUN ), 'clamps' => array(), ); } } elseif ( ! empty( get_option( 'aipt_review_policy_enabled', false ) ) && ! apply_filters( 'aipt_automation_force_review', false, (int) $job->id ) ) { $clamps[] = array( self::KIND_REVIEW_POLICY, 'reviewPolicy', PlanDenial::PLAN_BUSINESS, array( 'reviewPolicy' ) ); } if ( 'faq' === $tool && 'replace' === ( $settings['writeMode'] ?? '' ) && ! apply_filters( 'aipt_faq_replace_enabled', false ) ) { $clamps[] = array( self::KIND_FAQ_REPLACE, 'faqReplaceMode', PlanDenial::PLAN_PAID, array( 'writeMode' ) ); } $dropped = OptionsTranslator::appliedFreeDefaults( $tool, $settings, $translated_options ); if ( ! empty( $dropped ) && isset( self::OPTION_FEATURES[ $tool ] ) ) { $clamps[] = array( self::KIND_PAID_OPTIONS, self::OPTION_FEATURES[ $tool ], PlanDenial::PLAN_PAID, $dropped ); } $capped_types = self::cappedContentTypes( $tool, $translated_options ); if ( ! empty( $capped_types ) ) { $clamps[] = array( self::KIND_CONTENT_TYPES, 'multiTypeJobs', PlanDenial::PLAN_BUSINESS, $capped_types ); } $described = array(); foreach ( $clamps as $clamp ) { $descriptor = self::describe( $clamp[0], $clamp[1], $clamp[2], $clamp[3], self::CONTEXT_RUN ); if ( PlanDenial::isVerificationUnavailable( $descriptor['reason'] ) ) { return array( 'block' => $descriptor, 'clamps' => array(), ); } $descriptor['message'] = self::subjectMessage( $descriptor['kind'] ) . ' ' . self::clampOutcome( $descriptor['reason'] ); $described[] = $descriptor; } return array( 'block' => null, 'clamps' => $described, ); } /** * Whether this job's results are guaranteed to pass through a human * review step before anything is written: its own content routing is * 'draft' (To review is itself the review step for a review-routed * job), or this site's review policy is enabled AND * this job is forced through it. The second half reads the exact same * option/filter {@see assessRun()} already reads for * `KIND_REVIEW_POLICY`, and matches * `ResultProcessor::reviewPolicyForcesDraft()`, which already routes a * forced job's WHOLE output to review when it returns true — this * helper registers no new filter. * * Job-uniform: `aipt_automation_force_review` takes only a job id, never * a product id, so one boolean applies identically to every product a * run processes. Used only to clamp Description's `customer_reviews` * section (the "What customers say" summary) when false, never to block * or pause a run — unlike `assessRun()`'s own review-routing check above, * which still blocks the whole run outright when To review is requested * without the entitlement for it. * * @param AutomationJob $job Job about to run (or already running). * @return bool */ public static function reviewGuaranteed( AutomationJob $job ): bool { $settings = is_array( $job->settings ) ? $job->settings : array(); if ( 'draft' === self::contentStatus( $settings ) ) { return true; } return (bool) get_option( 'aipt_review_policy_enabled', false ) && (bool) apply_filters( 'aipt_automation_force_review', false, (int) $job->id ); } /** * REST error for a denial descriptor. * * @param array $descriptor Denial descriptor. * @return WP_Error */ public static function restError( array $descriptor ): WP_Error { return PlanDenial::error( $descriptor['feature'], $descriptor['required_plan'], self::planMessage( $descriptor['kind'] ), 'premium_required', array( 'fields' => $descriptor['keys'] ) ); } /** * Build a denial descriptor with a reason-aware stored message. * * @param string $kind KIND_* constant. * @param string $feature Feature key. * @param string $required_plan PlanDenial::PLAN_*. * @param string[] $keys Affected setting keys. * @param string $context CONTEXT_* constant. * @return array{kind:string, feature:string, required_plan:string, reason:string, keys:string[], message:string} */ public static function describe( string $kind, string $feature, string $required_plan, array $keys, string $context ): array { $reason = PlanDenial::reason( $feature, $required_plan ); return array( 'kind' => $kind, 'feature' => $feature, 'required_plan' => $required_plan, 'reason' => $reason, 'keys' => array_values( $keys ), 'message' => trim( self::subjectMessage( $kind ) . ' ' . self::outcomeMessage( $reason, $context ) . ' ' . self::nextStepMessage( $kind, $reason, $context ) ), ); } /** * Advanced (description) content types the per-job ceiling drops. * * @param string $tool Tool type. * @param array $translated_options Translated options. * @return string[] Dropped option flags. */ private static function cappedContentTypes( string $tool, array $translated_options ): array { if ( 'advanced' !== $tool ) { return array(); } $requested = array_values( array_filter( array( 'generateDescription', 'generateShortDescription', 'generateTags' ), static function ( $flag ) use ( $translated_options ) { return ! empty( $translated_options[ $flag ] ); } ) ); $max = (int) apply_filters( 'aipt_max_job_content_types', 1 ); if ( $max <= 0 || count( $requested ) <= $max ) { return array(); } return array_slice( $requested, $max ); } /** * Comparable recurring cadence: type, time and timezone, plus the weekday * of a weekly cadence or the day of month of a monthly one. Fields the * type does not use, and defaults the edit form fills in for them, never * count as a change. * * @param array $schedule_config Schedule config. * @return string */ private static function cadenceSignature( array $schedule_config ): string { $type = strtolower( (string) ( $schedule_config['type'] ?? '' ) ); $parts = array( $type, (string) ( $schedule_config['time'] ?? '' ), (string) ( $schedule_config['timezone'] ?? 'UTC' ), ); if ( 'weekly' === $type ) { $parts[] = strtolower( (string) ( $schedule_config['day'] ?? 'monday' ) ); } elseif ( 'monthly' === $type ) { $parts[] = (string) absint( $schedule_config['date'] ?? 1 ); } return implode( '|', $parts ); } /** * Plan copy used for plan_required REST denials. * * @param string $kind KIND_* constant. * @return string */ private static function planMessage( string $kind ): string { switch ( $kind ) { case self::KIND_DRAFT_REVIEW: return __( 'Business plan adds To review for automation results.', 'ai-product-tools' ); case self::KIND_RECURRING: return __( 'Business plan adds recurring schedules.', 'ai-product-tools' ); case self::KIND_FILTERS: return __( 'Business plan adds category, tag, stock status and product status filters.', 'ai-product-tools' ); case self::KIND_REVIEW_POLICY: return __( 'Business plan adds the automation review policy.', 'ai-product-tools' ); case self::KIND_FAQ_REPLACE: return __( 'Business plan adds FAQ replacement.', 'ai-product-tools' ); case self::KIND_CONTENT_TYPES: return __( 'Business plan adds multiple content types per job.', 'ai-product-tools' ); default: return __( 'Business plan adds more generation options.', 'ai-product-tools' ); } } /** * First sentence of a stored message: what the job asks for. * * @param string $kind KIND_* constant. * @return string */ private static function subjectMessage( string $kind ): string { switch ( $kind ) { case self::KIND_DRAFT_REVIEW: return __( 'This job sends results to To review.', 'ai-product-tools' ); case self::KIND_REVIEW_POLICY: return __( 'The site review policy sends automation results to review before they go live.', 'ai-product-tools' ); case self::KIND_FAQ_REPLACE: return __( 'This job replaces existing FAQs.', 'ai-product-tools' ); case self::KIND_CONTENT_TYPES: return __( 'This job generates more than one content type.', 'ai-product-tools' ); case self::KIND_FILTERS: return __( 'This job uses category, tag, stock status or product status filters.', 'ai-product-tools' ); case self::KIND_RECURRING: return __( 'This job runs on a recurring schedule.', 'ai-product-tools' ); default: return __( 'This job uses paid generation options.', 'ai-product-tools' ); } } /** * Second sentence of a stored message: why, and what happened. * * @param string $reason PlanDenial reason. * @param string $context CONTEXT_* constant. * @return string */ private static function outcomeMessage( string $reason, string $context ): string { switch ( $context ) { case self::CONTEXT_POOL: return self::poolOutcome( $reason ); case self::CONTEXT_REARM: return self::rearmOutcome( $reason ); case self::CONTEXT_CLAMP: return self::clampOutcome( $reason ); default: return self::runOutcome( $reason ); } } /** * Third sentence of a stored message, only where the merchant can let the * job go on without another plan: a job that sends results to To review, * paused because the plan has no To review, runs once it replaces live content instead * (product owner decision, 2026-11-17). Switching stays the merchant's * choice, so the job is paused, never switched: a paused job cannot be * edited, and Resume offers the switch (AutomationJobsController::resume_job()). * * @param string $kind KIND_* constant. * @param string $reason PlanDenial reason. * @param string $context CONTEXT_* constant. * @return string Empty when there is no such step. */ private static function nextStepMessage( string $kind, string $reason, string $context ): string { if ( self::KIND_DRAFT_REVIEW !== $kind || self::CONTEXT_RUN !== $context || PlanDenial::REASON_PLAN_REQUIRED !== $reason ) { return ''; } /* translators: "Replace Live Content" is the name of the automation job option that writes results straight to the live products; use the same words as that option. */ return __( 'To run it on your current plan, resume it and choose Replace Live Content: generated content then replaces your live product content without a review step.', 'ai-product-tools' ); } /** * @param string $reason PlanDenial reason. * @return string */ private static function runOutcome( string $reason ): string { switch ( $reason ) { case PlanDenial::REASON_PREMIUM_CODE_MISSING: return __( 'Your plan includes this, but the premium version of AI Product Tools is not active on this site, so the job was paused before sending anything to the AI provider.', 'ai-product-tools' ); case PlanDenial::REASON_LICENSE_UNVERIFIED: return __( 'Your license could not be verified right now, so the job was paused before sending anything to the AI provider. Resume the job in a few minutes.', 'ai-product-tools' ); case PlanDenial::REASON_LICENSE_ATTENTION: return __( 'Your license needs attention on this site, so the job was paused before sending anything to the AI provider.', 'ai-product-tools' ); default: return __( 'Your current plan does not include this, so the job was paused before sending anything to the AI provider.', 'ai-product-tools' ); } } /** * @param string $reason PlanDenial reason. * @return string */ private static function poolOutcome( string $reason ): string { switch ( $reason ) { case PlanDenial::REASON_PREMIUM_CODE_MISSING: return __( 'Your plan includes this, but the premium version of AI Product Tools is not active on this site, so the job was paused before building its product list.', 'ai-product-tools' ); case PlanDenial::REASON_LICENSE_UNVERIFIED: return __( 'Your license could not be verified right now, so the job was paused before building its product list. Resume the job in a few minutes.', 'ai-product-tools' ); case PlanDenial::REASON_LICENSE_ATTENTION: return __( 'Your license needs attention on this site, so the job was paused before building its product list.', 'ai-product-tools' ); default: return __( 'Your current plan does not include this, so the job was paused before building its product list.', 'ai-product-tools' ); } } /** * @param string $reason PlanDenial reason. * @return string */ private static function rearmOutcome( string $reason ): string { switch ( $reason ) { case PlanDenial::REASON_PREMIUM_CODE_MISSING: return __( 'Your plan includes this, but the premium version of AI Product Tools is not active on this site, so the next run was not scheduled.', 'ai-product-tools' ); case PlanDenial::REASON_LICENSE_UNVERIFIED: return __( 'Your license could not be verified right now, so the job was paused instead of scheduling its next run. Resume the job in a few minutes.', 'ai-product-tools' ); case PlanDenial::REASON_LICENSE_ATTENTION: return __( 'Your license needs attention on this site, so the next run was not scheduled.', 'ai-product-tools' ); default: return __( 'Your current plan does not include this, so the next run was not scheduled.', 'ai-product-tools' ); } } /** * @param string $reason PlanDenial reason (never license_unverified: that blocks). * @return string */ private static function clampOutcome( string $reason ): string { switch ( $reason ) { case PlanDenial::REASON_PREMIUM_CODE_MISSING: return __( 'Your plan includes this, but the premium version of AI Product Tools is not active on this site, so Free defaults were used for this run.', 'ai-product-tools' ); case PlanDenial::REASON_LICENSE_ATTENTION: return __( 'Your license needs attention on this site, so Free defaults were used for this run.', 'ai-product-tools' ); default: return __( 'Your current plan does not include this, so Free defaults were used for this run.', 'ai-product-tools' ); } } }