-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-select-locale.php
More file actions
315 lines (269 loc) · 8.05 KB
/
Copy pathclass-select-locale.php
File metadata and controls
315 lines (269 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* Add task to select the site locale.
*
* @package Progress_Planner
*/
namespace Progress_Planner\Suggested_Tasks\Providers;
/**
* Add task to select the site locale.
*/
class Select_Locale extends Tasks_Interactive {
/**
* The capability required to perform the task.
*
* @var string
*/
protected const CAPABILITY = 'install_languages';
/**
* Whether the task is an onboarding task.
*
* @var bool
*/
protected const IS_ONBOARDING_TASK = false;
/**
* The provider ID.
*
* @var string
*/
protected const PROVIDER_ID = 'select-locale';
/**
* The popover ID.
*
* @var string
*/
const POPOVER_ID = 'select-locale';
/**
* The external link URL.
*
* @var string
*/
protected const EXTERNAL_LINK_URL = 'https://prpl.fyi/set-locale';
/**
* Whether the task is dismissable.
*
* @var bool
*/
protected $is_dismissable = true;
/**
* The task priority.
*
* @var int
*/
protected $priority = 8;
/**
* Initialize the task.
*
* @return void
*/
public function init() {
\add_action( 'wp_ajax_prpl_interactive_task_submit_select-locale', [ $this, 'handle_interactive_task_specific_submit' ] );
}
/**
* Get the task URL.
*
* @return string
*/
protected function get_url() {
return \admin_url( 'options-general.php' );
}
/**
* Get the link setting.
*
* @return array
*/
public function get_link_setting() {
return [
'hook' => 'options-general.php',
'iconEl' => 'label[for="WPLANG"]',
];
}
/**
* Get the task title.
*
* @return string
*/
protected function get_title() {
return \esc_html__( 'Select your site locale', 'progress-planner' );
}
/**
* Check if the task should be added.
*
* @return bool
*/
public function should_add_task() {
$user_lang = $this->get_browser_locale();
$wp_lang = \get_locale();
return $user_lang && ! \str_starts_with( $wp_lang, $user_lang );
}
/**
* Get the browser locale.
*
* @return string
*/
protected function get_browser_locale() {
$lang = \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '' ) );
if ( ! $lang ) {
return '';
}
$lang = \strtolower( \substr( $lang, 0, 2 ) );
$lang = \explode( '-', $lang )[0];
$lang = \explode( '_', $lang )[0];
return $lang;
}
/**
* Get all locales from the WP API.
*
* Not currently used, but could be useful in the future.
*
* @return array
*/
protected function get_locales() {
$cache_key = 'all_locales';
$cached = \progress_planner()->get_utils__cache()->get( $cache_key );
if ( $cached ) {
return $cached;
}
$response = \wp_remote_get( 'https://api.wordpress.org/translations/core/1.0/' );
if ( \is_wp_error( $response ) ) {
\progress_planner()->get_utils__cache()->set( $cache_key, [], 5 * MINUTE_IN_SECONDS );
return [];
}
$body = \wp_remote_retrieve_body( $response );
$locales = \json_decode( $body, true );
if ( ! \is_array( $locales ) || ! isset( $locales['translations'] ) ) {
\progress_planner()->get_utils__cache()->set( $cache_key, [], 5 * MINUTE_IN_SECONDS );
return [];
}
// Get the locales.
$locales = \array_map(
fn( $locale ) => [
'code' => $locale['language'],
'name' => $locale['native_name'],
],
$locales['translations']
);
\progress_planner()->get_utils__cache()->set( $cache_key, $locales, MONTH_IN_SECONDS );
// Return the locales.
return $locales;
}
/**
* Check if the task is completed.
*
* @param string $task_id Optional task ID to check completion for.
* @return bool
*/
public function is_task_completed( $task_id = '' ) {
$activity = \progress_planner()->get_activities__query()->query_activities(
[
'category' => 'suggested_task',
'data_id' => static::PROVIDER_ID,
]
);
return ! empty( $activity );
}
/**
* Get the popover instructions.
*
* @return void
*/
public function print_popover_instructions() {
echo '<p>' . \esc_html__( 'Select your site locale to ensure your site is displayed correctly in the correct language', 'progress-planner' ) . '</p>';
}
/**
* Print the popover input field for the form.
*
* @return void
*/
public function print_popover_form_contents() {
if ( ! \function_exists( 'wp_get_available_translations' ) ) {
require_once ABSPATH . 'wp-admin/includes/translation-install.php'; // @phpstan-ignore requireOnce.fileNotFound
}
$languages = \get_available_languages();
$translations = \wp_get_available_translations();
$locale = \get_locale();
if ( ! \in_array( $locale, $languages, true ) ) {
$locale = '';
}
\wp_dropdown_languages(
[
'name' => 'language',
'id' => 'language',
'selected' => $locale,
'languages' => $languages,
'translations' => $translations,
'show_available_translations' => \current_user_can( 'install_languages' ) && \wp_can_install_language_pack(),
]
);
$this->print_submit_button( \__( 'Select locale', 'progress-planner' ), 'prpl-steps-nav-wrapper-align-left' );
}
/**
* Handle the interactive task submit.
*
* This is only for interactive tasks that change non-core settings.
* The $_POST data is expected to be:
* - setting: (string) The setting to update.
* - value: (mixed) The value to update the setting to.
* - setting_path: (array) The path to the setting to update.
* Use an empty array if the setting is not nested.
* If the value is nested, use an array of keys.
* Example: [ 'a', 'b', 'c' ] will update the value of $option['a']['b']['c'].
* - nonce: (string) The nonce.
*
* @return void
*/
public function handle_interactive_task_specific_submit() {
// Check if the user has the necessary capabilities.
if ( ! \current_user_can( 'manage_options' ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'You do not have permission to update settings.', 'progress-planner' ) ] );
}
// Check the nonce.
if ( ! \check_ajax_referer( 'progress_planner', 'nonce', false ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid nonce.', 'progress-planner' ) ] );
}
if ( ! isset( $_POST['setting'] ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'Missing setting.', 'progress-planner' ) ] );
}
if ( ! isset( $_POST['value'] ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'Missing value.', 'progress-planner' ) ] );
}
if ( ! isset( $_POST['setting_path'] ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'Missing setting path.', 'progress-planner' ) ] );
}
$option_updated = false;
$language_for_update = \sanitize_text_field( \wp_unslash( $_POST['value'] ) );
if ( empty( $language_for_update ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid language.', 'progress-planner' ) ] );
}
// Handle translation installation.
if ( \current_user_can( 'install_languages' ) ) {
require_once ABSPATH . 'wp-admin/includes/translation-install.php'; // @phpstan-ignore requireOnce.fileNotFound
if ( \wp_can_install_language_pack() ) {
$language = \wp_download_language_pack( $language_for_update );
if ( $language ) {
$language_for_update = $language;
$option_updated = \update_option( 'WPLANG', $language_for_update );
}
}
}
if ( $option_updated ) {
\wp_send_json_success( [ 'message' => \esc_html__( 'Setting updated.', 'progress-planner' ) ] );
}
\wp_send_json_error( [ 'message' => \esc_html__( 'Failed to update setting.', 'progress-planner' ) ] );
}
/**
* Add task actions specific to this task.
*
* @param array $data The task data.
* @param array $actions The existing actions.
*
* @return array
*/
public function add_task_actions( $data = [], $actions = [] ) {
$actions[] = [
'priority' => 10,
'html' => '<a href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover()">' . \esc_html__( 'Select locale', 'progress-planner' ) . '</a>',
];
return $actions;
}
}