$e->getMessage() ) ); } } /** * Seed a starting Brand profile from the free-text `aipt_brand_context` * Settings field, once. Idempotent via `BrandProfileStore::findByOrigin()` * — a second call never creates a duplicate profile. Never silently * drops a merchant's brand voice: when the text is longer than the * profile schema allows, the stored profile is shortened to fit (the * same limit a manual save through the Brand Voice editor would also * enforce), not rejected outright. * * @return void */ private static function upgradeBrandProfile(): void { $raw = (string) get_option( 'aipt_brand_context', '' ); if ( '' === trim( $raw ) ) { return; } $brand_profiles = new BrandProfileStore(); if ( null !== $brand_profiles->findByOrigin( self::BRAND_ORIGIN ) ) { return; } $candidate = BrandProfileSchema::emptyProfile(); $candidate['description'] = $raw; $normalized = BrandProfileSchema::normalize( $candidate ); $full_sanitized = (string) $normalized['profile']['description']; if ( mb_strlen( $full_sanitized, 'UTF-8' ) > BrandProfileSchema::DESCRIPTION_MAX_LEN ) { $candidate['description'] = mb_substr( $full_sanitized, 0, BrandProfileSchema::DESCRIPTION_MAX_LEN, 'UTF-8' ); $normalized = BrandProfileSchema::normalize( $candidate ); } $profile_id = $brand_profiles->create( array( 'name' => __( 'Store brand voice', 'ai-product-tools' ), 'profile' => $normalized['profile'], 'origin' => self::BRAND_ORIGIN, ) ); if ( $profile_id <= 0 ) { throw new RuntimeException( 'Could not create the starting brand profile.' ); } } }