Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion includes/RestApi/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ final class RestApi {
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\SiteClassificationController',
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\SiteGenController',
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\PreviewsController',
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\LanguagesController',
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\DesignController',
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\GlobalStylesController',
);
Expand Down
20 changes: 6 additions & 14 deletions includes/RestApi/SiteGenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService as LegacySiteGenService;
use NewfoldLabs\WP\Module\Onboarding\Data\SiteGen as SiteGenData;
use NewfoldLabs\WP\Module\Onboarding\Services\Ai\ContentGeneration\SitekitsContentGeneration;
use NewfoldLabs\WP\Module\Onboarding\Services\LanguageService;
use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenService;
use NewfoldLabs\WP\Module\Onboarding\Services\SiteNavigationService;

Expand Down Expand Up @@ -121,10 +122,6 @@ public function sitegen_meta_args() {
'required' => true,
'type' => 'string',
),
'locale' => array(
'required' => true,
'type' => 'string',
),
'skip_cache' => array(
'required' => false,
'type' => 'boolean',
Expand All @@ -148,10 +145,6 @@ public function get_homepages_args() {
'required' => true,
'type' => 'string',
),
'locale' => array(
'required' => true,
'type' => 'string',
),
);
}

Expand Down Expand Up @@ -235,9 +228,9 @@ public function generate_sitegen_meta( \WP_REST_Request $request ) {
}

$site_info = $request->get_param( 'site_info' );
$identifier = $request->get_param( 'identifier' );
$identifier = $request->get_param( 'identifier' );
$site_type = $request->get_param( 'site_type' );
$locale = $request->get_param( 'locale' );
$locale = LanguageService::get_site_locale();
$skip_cache = $request->get_param( 'skip_cache' );

return LegacySiteGenService::instantiate_site_meta(
Expand All @@ -264,15 +257,14 @@ public function get_homepages( \WP_REST_Request $request ) {
$site_description = $request->get_param( 'site_description' );
$site_info = array( 'site_description' => $site_description );
$site_type = $request->get_param( 'site_type' );
$locale = $request->get_param( 'locale' );
$locale = LanguageService::get_site_locale();

$homepages = [];
if ( SitekitsContentGeneration::site_type_supported( $site_type ) ) {
$siteGenService = new SiteGenService();
$homepages = $siteGenService->get_sitekits(
$site_description,
$site_type,
$locale,
true
);

Expand Down Expand Up @@ -315,7 +307,7 @@ public function regenerate_homepage( \WP_REST_Request $request ) {
$color_palette = $request->get_param( 'palette' );
$is_favorite = $request->get_param( 'isFavorite' );
$site_info = array( 'site_description' => $site_description );
$locale = $request->get_param( 'locale' );
$locale = LanguageService::get_site_locale();
$skip_cache = $request->get_param( 'skip_cache' );

$target_audience = LegacySiteGenService::instantiate_site_meta( $site_info, 'target_audience', $locale, $skip_cache );
Expand Down Expand Up @@ -356,7 +348,7 @@ public function publish_sitemap_pages( \WP_REST_Request $request ) {
$site_description = $request->get_param( 'site_description' );
$site_info = array( 'site_description' => $site_description );
$site_type = $request->get_param( 'site_type' );
$locale = $request->get_param( 'locale' );
$locale = LanguageService::get_site_locale();
$skip_cache = $request->get_param( 'skip_cache' );

$target_audience = LegacySiteGenService::instantiate_site_meta( $site_info, 'target_audience', $site_type, $locale, $skip_cache );
Expand Down
23 changes: 5 additions & 18 deletions includes/Services/Ai/ContentGeneration/ContentGenerationPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use NewfoldLabs\WP\Module\AI\SiteGen\SiteGen;
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService as LegacySiteGenService;
use NewfoldLabs\WP\Module\Onboarding\Services\LanguageService;

/**
* Content Generation Prompt Class
Expand All @@ -36,13 +37,6 @@ class ContentGenerationPrompt {
*/
private $site_type;

/**
* The locale/language code for the site.
*
* @var string
*/
private $locale;

/**
* The required site meta dependencies.
*
Expand All @@ -59,37 +53,30 @@ class ContentGenerationPrompt {
*
* @param string $site_description The user-provided site description.
* @param string $site_type The type of site (e.g., 'business', 'personal', 'ecommerce', 'linkinbio').
* @param string $locale The locale/language code for the site.
*/
public function __construct( $site_description, $site_type, $locale ) {
$this->validate_input( $site_description, $site_type, $locale );
public function __construct( $site_description, $site_type ) {
$this->validate_input( $site_description, $site_type );

$this->site_description = trim( $site_description );
$this->site_type = trim( $site_type );
$this->locale = trim( $locale );
}

/**
* Validates the input parameters.
*
* @param string $site_description The site description.
* @param string $site_type The site type.
* @param string $locale The locale.
*
* @throws \Exception If any parameter is empty.
*/
private function validate_input( $site_description, $site_type, $locale ) {
private function validate_input( $site_description, $site_type ) {
if ( empty( $site_description ) ) {
throw new \Exception( 'Site description cannot be empty' );
}

if ( empty( $site_type ) ) {
throw new \Exception( 'Site type cannot be empty' );
}

if ( empty( $locale ) ) {
throw new \Exception( 'Locale cannot be empty' );
}
}

/**
Expand All @@ -113,7 +100,7 @@ private function get_meta_response( $site_meta_dependency ): array {
$this->site_description,
$site_meta_dependency,
$this->site_type,
$this->locale
LanguageService::get_site_locale()
);

if ( is_wp_error( $response ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NewfoldLabs\WP\Module\Onboarding\Services\Ai\ContentGeneration;

use NewfoldLabs\WP\Module\Onboarding\Services\LanguageService;
use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenService;
use NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes\EcommerceSiteTypeService;
use NewfoldLabs\WP\Module\Onboarding\Services\SiteTypes\CommonSiteTypeService;
Expand All @@ -23,13 +24,6 @@ class SitekitsContentGeneration {
*/
private $site_type;

/**
* The locale.
*
* @var string
*/
private $locale;

/**
* The prompt.
*
Expand All @@ -48,13 +42,11 @@ class SitekitsContentGeneration {
* Constructor.
*
* @param string $site_type The site type.
* @param string $locale The locale.
* @param ContentGenerationPrompt $prompt The prompt.
* @param SiteClassification $site_classification The site classification.
*/
public function __construct( string $site_type, string $locale, ContentGenerationPrompt $prompt, SiteClassification $site_classification ) {
public function __construct( string $site_type, ContentGenerationPrompt $prompt, SiteClassification $site_classification ) {
$this->site_type = $site_type;
$this->locale = $locale;
$this->prompt = $prompt;
$this->site_classification = $site_classification;
}
Expand All @@ -71,7 +63,7 @@ public function generate_sitekits( int $count = 3 ) {
'siteType' => $this->site_type,
'count' => $count,
'prompt' => $prompt,
'locale' => $this->locale,
'locale' => LanguageService::get_site_locale(),
'primaryType' => $this->site_classification->get_primary_type(),
'secondaryType' => $this->site_classification->get_secondary_type(),
);
Expand Down
94 changes: 10 additions & 84 deletions includes/Services/LanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace NewfoldLabs\WP\Module\Onboarding\Services;

use NewfoldLabs\WP\Module\Onboarding\Data\Languages;

/**
* Class LanguageService
*
Expand All @@ -10,92 +12,16 @@
class LanguageService {

/**
* Get all available languages including the default English.
* Get the site locale.
*
* @return array List of language data
* @return string The site locale.
*/
public static function get_all_languages() {
// Load translation-install.php if the function doesn't exist
if ( ! function_exists( 'wp_get_available_translations' ) ) {
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
}

// Get all available translations from WordPress API
$translations = \wp_get_available_translations();

// If translations API fails, provide basic fallback
if ( empty( $translations ) ) {
return self::get_fallback_languages();
public static function get_site_locale(): string {
$locale = Languages::get_default_language();
if ( ! $locale ) {
// Fallback to en_US if no default language is found.
$locale = 'en_US';
}

// Format data for response - include English as default
$formatted_languages = array(
array(
'code' => 'en_US',
'name' => 'English (United States)',
'native_name' => 'English (United States)',
),
);

// Add translated languages with proper format
foreach ( $translations as $locale => $translation ) {
$formatted_languages[] = array(
'code' => $locale,
'name' => $translation['english_name'],
'native_name' => $translation['native_name'],
);
}

return $formatted_languages;
}

/**
* Get language data array formatted for the language selection component.
* Returns array of [language_name, language_code] pairs.
*
* @return array Array of language name and code pairs
*/
public static function get_languages_for_selection() {
$languages = self::get_all_languages();
$language_list = array();

foreach ( $languages as $language ) {
$language_list[] = array(
$language['name'],
$language['code'],
);
}

return $language_list;
}

/**
* Get fallback languages in case the translations API fails.
*
* @return array Basic list of language data
*/
private static function get_fallback_languages() {
return array(
array(
'code' => 'en_US',
'name' => 'English (United States)',
'native_name' => 'English (United States)',
),
array(
'code' => 'es_ES',
'name' => 'Spanish (Spain)',
'native_name' => 'Español',
),
array(
'code' => 'fr_FR',
'name' => 'French (France)',
'native_name' => 'Français',
),
array(
'code' => 'de_DE',
'name' => 'German',
'native_name' => 'Deutsch',
),
);
return $locale;
}
}
22 changes: 6 additions & 16 deletions includes/Services/SiteGenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ public static function get_instance(): SiteGenService {
*
* @param string $site_description The site description.
* @param string $site_type The site type.
* @param string $locale The locale.
* @param bool $for_onboarding_preview Whether to return the sitekits as array for onboarding preview.
* @return array|\WP_Error Array of Sitekit objects, array of sitekits for onboarding preview, or WP_Error if there is an error.
*/
public function get_sitekits( string $site_description, string $site_type, string $locale = 'en_US', bool $for_onboarding_preview = false ) {
$prompt = new ContentGenerationPrompt( $site_description, $site_type, $locale );
public function get_sitekits( string $site_description, string $site_type, bool $for_onboarding_preview = false ) {
$prompt = new ContentGenerationPrompt( $site_description, $site_type );
$site_classification = $this->get_site_classification();

$sitekits = new SitekitsContentGeneration( $site_type, $locale, $prompt, $site_classification );
$sitekits = new SitekitsContentGeneration( $site_type, $prompt, $site_classification );
$sitekits = $sitekits->generate_sitekits();

// If there is an error, return it.
Expand Down Expand Up @@ -159,7 +158,7 @@ public function publish_homepage( string $selected_sitegen_homepage ) {
*/
public function get_sitemap_page_title( string $slug ) {
$prompt = $this->get_prompt();
$locale = $this->get_locale();
$locale = LanguageService::get_site_locale();
$site_type = $this->get_site_type();
if ( ! $prompt || ! $locale || ! $site_type ) {
return false;
Expand Down Expand Up @@ -190,7 +189,7 @@ public function get_site_classification(): SiteClassification {
$this->get_prompt(),
'site_classification',
$this->get_site_type(),
$this->get_locale()
LanguageService::get_site_locale()
);

if ( is_array( $site_classification ) ) {
Expand All @@ -211,7 +210,7 @@ public function get_color_palette() {
$this->get_prompt(),
'color_palette',
$this->get_site_type(),
$this->get_locale()
LanguageService::get_site_locale()
);

if ( ! is_array( $color_palettes ) ) {
Expand Down Expand Up @@ -254,13 +253,4 @@ public function get_prompt() {
public function get_site_type() {
return ! empty( $this->input_data['siteType'] ) ? $this->input_data['siteType'] : 'business';
}

/**
* Get the locale entered during Onboarding.
*
* @return string
*/
public function get_locale() {
return ! empty( $this->input_data['locale'] ) ? $this->input_data['locale'] : 'en_US';
}
}
2 changes: 1 addition & 1 deletion includes/Services/SiteNavigationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function get_site_navigation_items( $site_type = null ): array {
$this->get_prompt(),
'sitemap',
$site_type,
$this->get_locale()
LanguageService::get_site_locale()
);

$nav_items = array();
Expand Down
Loading
Loading