Skip to content

Commit 23c2dfc

Browse files
committed
adding languages endpoint
1 parent 0a96b27 commit 23c2dfc

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace NewfoldLabs\WP\Module\Onboarding\RestApi;
4+
5+
use NewfoldLabs\WP\Module\Onboarding\Permissions;
6+
use NewfoldLabs\WP\Module\Onboarding\Services\LanguageService;
7+
8+
/**
9+
* Controller for handling language-related endpoints.
10+
*/
11+
class LanguagesController {
12+
13+
/**
14+
* The namespace of this controller's route.
15+
*
16+
* @var string
17+
*/
18+
protected $namespace = 'newfold-onboarding/v1';
19+
20+
/**
21+
* The base of this controller's route.
22+
*
23+
* @var string
24+
*/
25+
protected $rest_base = '/languages';
26+
27+
/**
28+
* Register routes for LanguagesController.
29+
*/
30+
public function register_routes() {
31+
register_rest_route(
32+
$this->namespace,
33+
$this->rest_base,
34+
array(
35+
array(
36+
'methods' => \WP_REST_Server::READABLE,
37+
'callback' => array( $this, 'get_languages' ),
38+
'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ),
39+
),
40+
)
41+
);
42+
}
43+
44+
/**
45+
* Get available languages in WordPress.
46+
*
47+
* @return WP_REST_Response
48+
*/
49+
public function get_languages() {
50+
// Use LanguageService to get languages
51+
$languages = LanguageService::get_all_languages();
52+
53+
return new \WP_REST_Response(
54+
array(
55+
'languages' => $languages,
56+
),
57+
200
58+
);
59+
}
60+
}

includes/RestApi/RestApi.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ final class RestApi {
3030
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\SiteClassificationController',
3131
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\SiteGenController',
3232
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\BlockRenderController',
33+
'NewfoldLabs\\WP\\Module\\Onboarding\\RestApi\\LanguagesController',
3334
);
3435

3536
/**

includes/Services/LanguageService.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace NewfoldLabs\WP\Module\Onboarding\Services;
4+
5+
/**
6+
* Class LanguageService
7+
*
8+
* Handles language-related operations for the onboarding module.
9+
*/
10+
class LanguageService {
11+
12+
/**
13+
* Get all available languages including the default English.
14+
*
15+
* @return array List of language data
16+
*/
17+
public static function get_all_languages() {
18+
// Get available languages
19+
$languages = \get_available_languages();
20+
21+
// Load translation-install.php if the function doesn't exist
22+
if ( ! function_exists( 'wp_get_available_translations' ) ) {
23+
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
24+
}
25+
26+
// Get all available translations from WordPress API
27+
$translations = \wp_get_available_translations();
28+
29+
// If translations API fails, provide basic fallback
30+
if ( empty( $translations ) ) {
31+
return self::get_fallback_languages();
32+
}
33+
34+
// Format data for response - include English as default
35+
$formatted_languages = array(
36+
array(
37+
'code' => 'en_US',
38+
'name' => 'English (United States)',
39+
'native_name' => 'English (United States)',
40+
)
41+
);
42+
43+
// Add translated languages with proper format
44+
foreach ( $languages as $locale ) {
45+
if ( isset( $translations[ $locale ] ) ) {
46+
$translation = $translations[ $locale ];
47+
$formatted_languages[] = array(
48+
'code' => $locale,
49+
'name' => $translation['english_name'],
50+
'native_name' => $translation['native_name'],
51+
);
52+
}
53+
}
54+
55+
return $formatted_languages;
56+
}
57+
58+
/**
59+
* Get language data array formatted for the language selection component.
60+
* Returns array of [language_name, language_code] pairs.
61+
*
62+
* @return array Array of language name and code pairs
63+
*/
64+
public static function get_languages_for_selection() {
65+
$languages = self::get_all_languages();
66+
$language_list = array();
67+
68+
foreach ( $languages as $language ) {
69+
$language_list[] = array(
70+
$language['name'],
71+
$language['code']
72+
);
73+
}
74+
75+
return $language_list;
76+
}
77+
78+
/**
79+
* Get fallback languages in case the translations API fails.
80+
*
81+
* @return array Basic list of language data
82+
*/
83+
private static function get_fallback_languages() {
84+
return array(
85+
array(
86+
'code' => 'en_US',
87+
'name' => 'English (United States)',
88+
'native_name' => 'English (United States)',
89+
),
90+
array(
91+
'code' => 'es_ES',
92+
'name' => 'Spanish (Spain)',
93+
'native_name' => 'Español',
94+
),
95+
array(
96+
'code' => 'fr_FR',
97+
'name' => 'French (France)',
98+
'native_name' => 'Français',
99+
),
100+
array(
101+
'code' => 'de_DE',
102+
'name' => 'German',
103+
'native_name' => 'Deutsch',
104+
),
105+
);
106+
}
107+
}

0 commit comments

Comments
 (0)