| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPGlobus / Admin / Language Edit |
| 4 |
* |
| 5 |
* @package WPGlobus\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly |
| 10 |
} |
| 11 |
|
| 12 |
// Load the Request class. |
| 13 |
require_once dirname( __FILE__ ) . '/class-wpglobus-language-edit-request.php'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WPGlobus_Language_Edit |
| 17 |
*/ |
| 18 |
class WPGlobus_Language_Edit { |
| 19 |
|
| 20 |
/** |
| 21 |
* All flag files. |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $all_flags = array(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Current action |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $action = WPGlobus_Language_Edit_Request::ACTION_ADD; |
| 33 |
|
| 34 |
/** |
| 35 |
* Language code |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $language_code = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* Language name |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
protected $language_name = ''; |
| 47 |
|
| 48 |
/** |
| 49 |
* Language name in English |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
protected $en_language_name = ''; |
| 54 |
|
| 55 |
/** |
| 56 |
* Locale |
| 57 |
* |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
protected $locale = ''; |
| 61 |
|
| 62 |
/** |
| 63 |
* Flag for the current language |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
protected $flag = ''; |
| 68 |
|
| 69 |
/** |
| 70 |
* Set to true when the form is submitted |
| 71 |
* |
| 72 |
* @var bool |
| 73 |
*/ |
| 74 |
protected $submit = false; |
| 75 |
|
| 76 |
/** |
| 77 |
* Diagnostic messages |
| 78 |
* |
| 79 |
* @var string[][] |
| 80 |
*/ |
| 81 |
protected $submit_messages = array(); |
| 82 |
|
| 83 |
/** |
| 84 |
* The Request object |
| 85 |
* |
| 86 |
* @var WPGlobus_Language_Edit_Request |
| 87 |
*/ |
| 88 |
protected $request; |
| 89 |
|
| 90 |
/** |
| 91 |
* Constructor |
| 92 |
*/ |
| 93 |
public function __construct() { |
| 94 |
|
| 95 |
$this->request = new WPGlobus_Language_Edit_Request(); |
| 96 |
|
| 97 |
if ( WPGlobus_Language_Edit_Request::ACTION_DELETE === $this->request->get_action() ) { |
| 98 |
$this->action = WPGlobus_Language_Edit_Request::ACTION_DELETE; |
| 99 |
} elseif ( WPGlobus_Language_Edit_Request::ACTION_EDIT === $this->request->get_action() ) { |
| 100 |
$this->action = WPGlobus_Language_Edit_Request::ACTION_EDIT; |
| 101 |
} |
| 102 |
|
| 103 |
$this->language_code = $this->request->get_lang(); |
| 104 |
|
| 105 |
if ( $this->request->is_submit() ) { |
| 106 |
$this->submit = true; |
| 107 |
$this->process_submit(); |
| 108 |
} elseif ( $this->request->is_delete() ) { |
| 109 |
$this->process_delete(); |
| 110 |
$this->action = WPGlobus_Language_Edit_Request::ACTION_DONE; |
| 111 |
} else { |
| 112 |
$this->get_data(); |
| 113 |
} |
| 114 |
|
| 115 |
if ( WPGlobus_Language_Edit_Request::ACTION_DONE !== $this->action ) { |
| 116 |
$this->display_table(); |
| 117 |
} |
| 118 |
|
| 119 |
add_action( 'admin_footer', array( $this, 'on_print_scripts' ), 99 ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Add script in admin footer |
| 124 |
*/ |
| 125 |
public function on_print_scripts() { |
| 126 |
|
| 127 |
if ( WPGlobus_Language_Edit_Request::ACTION_DONE === $this->action ) { |
| 128 |
$location = '?page=' . WPGlobus::OPTIONS_PAGE_SLUG; |
| 129 |
// @formatter:off |
| 130 |
?> |
| 131 |
<script>jQuery(function(){window.location = window.location.protocol + '//' + window.location.host + window.location.pathname + '<?php echo esc_js($location ); ?>'});</script> |
| 132 |
<?php |
| 133 |
// @formatter:on |
| 134 |
} |
| 135 |
|
| 136 |
wp_enqueue_script( |
| 137 |
'wpglobus-form', |
| 138 |
WPGlobus::plugin_dir_url() . 'includes/js/wpglobus-form' . WPGlobus::SCRIPT_SUFFIX() . '.js', |
| 139 |
array( 'jquery' ), |
| 140 |
WPGLOBUS_VERSION, |
| 141 |
true |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Process delete language action |
| 147 |
*/ |
| 148 |
protected function process_delete() { |
| 149 |
|
| 150 |
$config = WPGlobus::Config(); |
| 151 |
|
| 152 |
/** |
| 153 |
* Get options |
| 154 |
* |
| 155 |
* @var array |
| 156 |
*/ |
| 157 |
$opts = get_option( $config->option ); |
| 158 |
|
| 159 |
if ( isset( $opts['enabled_languages'][ $this->language_code ] ) ) { |
| 160 |
|
| 161 |
unset( $opts['enabled_languages'][ $this->language_code ] ); |
| 162 |
|
| 163 |
/** FIX: reset $opts['more_languages'] */ |
| 164 |
if ( array_key_exists( 'more_languages', $opts ) ) { |
| 165 |
$opts['more_languages'] = ''; |
| 166 |
} |
| 167 |
update_option( $config->option, $opts ); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
unset( $config->language_name[ $this->language_code ] ); |
| 172 |
update_option( $config->option_language_names, $config->language_name ); |
| 173 |
|
| 174 |
unset( $config->flag[ $this->language_code ] ); |
| 175 |
update_option( $config->option_flags, $config->flag ); |
| 176 |
|
| 177 |
unset( $config->en_language_name[ $this->language_code ] ); |
| 178 |
update_option( $config->option_en_language_names, $config->en_language_name ); |
| 179 |
|
| 180 |
unset( $config->locale[ $this->language_code ] ); |
| 181 |
update_option( $config->option_locale, $config->locale ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Process submit action |
| 186 |
*/ |
| 187 |
protected function process_submit() { |
| 188 |
|
| 189 |
$code = $this->request->get_wpglobus_language_code(); |
| 190 |
if ( $code && $this->language_code === $code ) { |
| 191 |
if ( $this->check_fields( $code, false ) ) { |
| 192 |
$this->save(); |
| 193 |
$this->submit_messages['success'][] = __( 'Options updated', 'wpglobus' ); |
| 194 |
} |
| 195 |
} else { |
| 196 |
if ( $this->check_fields( $code ) ) { |
| 197 |
$this->save( true ); |
| 198 |
$this->submit_messages['success'][] = __( 'Options updated', 'wpglobus' ); |
| 199 |
} |
| 200 |
} |
| 201 |
$this->get_flags(); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Save data language to DB |
| 206 |
* |
| 207 |
* @param bool $update_code If need to change language code. |
| 208 |
*/ |
| 209 |
protected function save( $update_code = false ) { |
| 210 |
|
| 211 |
$config = WPGlobus::Config(); |
| 212 |
|
| 213 |
$old_code = ''; |
| 214 |
if ( $update_code && WPGlobus_Language_Edit_Request::ACTION_EDIT === $this->action ) { |
| 215 |
$old_code = $this->language_code ? $this->language_code : $old_code; |
| 216 |
if ( isset( $config->language_name[ $old_code ] ) ) { |
| 217 |
unset( $config->language_name[ $old_code ] ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Get options |
| 222 |
* |
| 223 |
* @var array |
| 224 |
*/ |
| 225 |
$opts = get_option( $config->option ); |
| 226 |
if ( isset( $opts['enabled_languages'][ $old_code ] ) ) { |
| 227 |
unset( $opts['enabled_languages'][ $old_code ] ); |
| 228 |
update_option( $config->option, $opts ); |
| 229 |
} |
| 230 |
if ( isset( $opts['more_languages'] ) && $old_code === $opts['more_languages'] ) { |
| 231 |
unset( $opts['more_languages'] ); |
| 232 |
update_option( $config->option, $opts ); |
| 233 |
} |
| 234 |
} |
| 235 |
$config->language_name[ $this->language_code ] = $this->language_name; |
| 236 |
update_option( $config->option_language_names, $config->language_name ); |
| 237 |
|
| 238 |
if ( $update_code && isset( $config->flag[ $old_code ] ) ) { |
| 239 |
unset( $config->flag[ $old_code ] ); |
| 240 |
} |
| 241 |
$config->flag[ $this->language_code ] = $this->flag; |
| 242 |
update_option( $config->option_flags, $config->flag ); |
| 243 |
|
| 244 |
if ( $update_code && isset( $config->en_language_name[ $old_code ] ) ) { |
| 245 |
unset( $config->en_language_name[ $old_code ] ); |
| 246 |
} |
| 247 |
$config->en_language_name[ $this->language_code ] = $this->en_language_name; |
| 248 |
update_option( $config->option_en_language_names, $config->en_language_name ); |
| 249 |
|
| 250 |
if ( $update_code && isset( $config->locale[ $old_code ] ) ) { |
| 251 |
unset( $config->locale[ $old_code ] ); |
| 252 |
} |
| 253 |
$config->locale[ $this->language_code ] = $this->locale; |
| 254 |
update_option( $config->option_locale, $config->locale ); |
| 255 |
|
| 256 |
if ( $update_code ) { |
| 257 |
$this->action = WPGlobus_Language_Edit_Request::ACTION_DONE; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Check form fields |
| 263 |
* |
| 264 |
* @param string $lang_code Language code. |
| 265 |
* @param bool $check_code Use for language code existence check. |
| 266 |
* |
| 267 |
* @return bool True if no errors, false otherwise. |
| 268 |
*/ |
| 269 |
protected function check_fields( $lang_code, $check_code = true ) { |
| 270 |
$this->submit_messages['errors'] = array(); |
| 271 |
if ( $check_code && empty( $lang_code ) ) { |
| 272 |
$this->submit_messages['errors'][] = __( 'Please enter a language code!', 'wpglobus' ); |
| 273 |
} |
| 274 |
|
| 275 |
if ( $check_code && $this->language_exists( $lang_code ) ) { |
| 276 |
$this->submit_messages['errors'][] = __( 'Language code already exists!', 'wpglobus' ); |
| 277 |
} |
| 278 |
|
| 279 |
if ( ! $this->request->get_wpglobus_flags() ) { |
| 280 |
$this->submit_messages['errors'][] = __( 'Please specify the language flag!', 'wpglobus' ); |
| 281 |
} |
| 282 |
|
| 283 |
if ( ! $this->request->get_wpglobus_language_name() ) { |
| 284 |
$this->submit_messages['errors'][] = __( 'Please enter the language name!', 'wpglobus' ); |
| 285 |
} |
| 286 |
|
| 287 |
if ( ! $this->request->get_wpglobus_en_language_name() ) { |
| 288 |
$this->submit_messages['errors'][] = __( 'Please enter the language name in English!', 'wpglobus' ); |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! $this->request->get_wpglobus_locale() ) { |
| 292 |
$this->submit_messages['errors'][] = __( 'Please enter the locale!', 'wpglobus' ); |
| 293 |
} |
| 294 |
|
| 295 |
$this->language_code = $lang_code; |
| 296 |
$this->flag = $this->request->get_wpglobus_flags(); |
| 297 |
$this->language_name = $this->request->get_wpglobus_language_name(); |
| 298 |
$this->en_language_name = $this->request->get_wpglobus_en_language_name(); |
| 299 |
$this->locale = $this->request->get_wpglobus_locale(); |
| 300 |
|
| 301 |
if ( empty( $this->submit_messages['errors'] ) ) { |
| 302 |
return true; |
| 303 |
} |
| 304 |
|
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Check existing language code in global $WPGlobus_Config |
| 310 |
* |
| 311 |
* @param string $code Language code. |
| 312 |
* |
| 313 |
* @return bool true if language code exists |
| 314 |
*/ |
| 315 |
protected function language_exists( $code ) { |
| 316 |
|
| 317 |
if ( array_key_exists( $code, WPGlobus::Config()->language_name ) ) { |
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Get data for form fields |
| 326 |
*/ |
| 327 |
protected function get_data() { |
| 328 |
|
| 329 |
if ( WPGlobus_Language_Edit_Request::ACTION_EDIT === $this->action || WPGlobus_Language_Edit_Request::ACTION_DELETE === $this->action ) { |
| 330 |
|
| 331 |
$config = WPGlobus::Config(); |
| 332 |
|
| 333 |
$this->language_name = $config->language_name[ $this->language_code ]; |
| 334 |
$this->en_language_name = $config->en_language_name[ $this->language_code ]; |
| 335 |
$this->locale = $config->locale[ $this->language_code ]; |
| 336 |
$this->flag = $config->flag[ $this->language_code ]; |
| 337 |
} |
| 338 |
$this->get_flags(); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Display language form |
| 343 |
*/ |
| 344 |
protected function display_table() { |
| 345 |
|
| 346 |
$disabled = ''; |
| 347 |
if ( WPGlobus_Language_Edit_Request::ACTION_EDIT === $this->action ) { |
| 348 |
$header = __( 'Edit Language', 'wpglobus' ); |
| 349 |
} elseif ( WPGlobus_Language_Edit_Request::ACTION_DELETE === $this->action ) { |
| 350 |
$header = __( 'Delete Language', 'wpglobus' ); |
| 351 |
$disabled = 'disabled'; |
| 352 |
} else { |
| 353 |
$header = __( 'Add Language', 'wpglobus' ); |
| 354 |
} |
| 355 |
?> |
| 356 |
<div class="wrap"> |
| 357 |
<h1>WPGlobus: <?php echo esc_html( $header ); ?></h1> |
| 358 |
<?php |
| 359 |
if ( $this->submit ) { |
| 360 |
if ( ! empty( $this->submit_messages['errors'] ) ) { |
| 361 |
$mess = ''; |
| 362 |
foreach ( $this->submit_messages['errors'] as $message ) { |
| 363 |
$mess .= $message . '<br />'; |
| 364 |
} |
| 365 |
?> |
| 366 |
<div class="error"><p><?php echo wp_kses( $mess, array( 'br' => array() ) ); ?></p></div> |
| 367 |
<?php |
| 368 |
} elseif ( ! empty( $this->submit_messages['success'] ) ) { |
| 369 |
$mess = ''; |
| 370 |
foreach ( $this->submit_messages['success'] as $message ) { |
| 371 |
$mess .= $message . '<br />'; |
| 372 |
} |
| 373 |
?> |
| 374 |
<div class="updated"><p><?php echo wp_kses( $mess, array( 'br' => array() ) ); ?></p></div> |
| 375 |
<?php |
| 376 |
} |
| 377 |
} |
| 378 |
?> |
| 379 |
<form id="wpglobus_edit_form" method="post" action=""> |
| 380 |
<table class="form-table"> |
| 381 |
<tr> |
| 382 |
<th scope="row"><label |
| 383 |
for="wpglobus_language_code"><?php esc_html_e( 'Language Code', 'wpglobus' ); ?></label> |
| 384 |
</th> |
| 385 |
<td> |
| 386 |
<input name="wpglobus_language_code" <?php echo esc_attr( $disabled ); ?> type="text" |
| 387 |
id="wpglobus_language_code" |
| 388 |
value="<?php echo esc_attr( $this->language_code ); ?>" class="regular-text"/> |
| 389 |
|
| 390 |
<p class="description"><?php esc_html_e( '2-Letter ISO Language Code for the Language you want to insert. (Example: en)', 'wpglobus' ); ?></p> |
| 391 |
</td> |
| 392 |
</tr> |
| 393 |
<tr> |
| 394 |
<th scope="row"><label |
| 395 |
for="wpglobus_flags"><?php esc_html_e( 'Language flag', 'wpglobus' ); ?></label> |
| 396 |
</th> |
| 397 |
<td> |
| 398 |
<select id="wpglobus_flags" name="wpglobus_flags" style="width:300px;" |
| 399 |
class="populate"> |
| 400 |
<?php |
| 401 |
foreach ( $this->all_flags as $file_name ) : |
| 402 |
?> |
| 403 |
<option <?php selected( $this->flag === $file_name ); ?> |
| 404 |
value="<?php echo esc_attr( $file_name ); ?>"><?php echo esc_html( $file_name ); ?></option> |
| 405 |
<?php endforeach; ?> |
| 406 |
</select> |
| 407 |
</td> |
| 408 |
</tr> |
| 409 |
<tr> |
| 410 |
<th scope="row"><label |
| 411 |
for="wpglobus_language_name"><?php esc_html_e( 'Name', 'wpglobus' ); ?></label> |
| 412 |
</th> |
| 413 |
<td><input name="wpglobus_language_name" type="text" id="wpglobus_language_name" |
| 414 |
value="<?php echo esc_attr( $this->language_name ); ?>" class="regular-text"/> |
| 415 |
|
| 416 |
<p class="description"><?php esc_html_e( 'The name of the language in its native alphabet. (Examples: English, Русский)', 'wpglobus' ); ?></p> |
| 417 |
</td> |
| 418 |
</tr> |
| 419 |
<tr> |
| 420 |
<th scope="row"><label |
| 421 |
for="wpglobus_en_language_name"><?php esc_html_e( 'Name in English', 'wpglobus' ); ?></label> |
| 422 |
</th> |
| 423 |
<td><input name="wpglobus_en_language_name" type="text" id="wpglobus_en_language_name" |
| 424 |
value="<?php echo esc_attr( $this->en_language_name ); ?>" class="regular-text"/> |
| 425 |
|
| 426 |
<p class="description"><?php esc_html_e( 'The name of the language in English', 'wpglobus' ); ?></p> |
| 427 |
</td> |
| 428 |
</tr> |
| 429 |
<tr> |
| 430 |
<th scope="row"><label |
| 431 |
for="wpglobus_locale"><?php esc_html_e( 'Locale', 'wpglobus' ); ?></label></th> |
| 432 |
<td><input name="wpglobus_locale" type="text" id="wpglobus_locale" |
| 433 |
value="<?php echo esc_attr( $this->locale ); ?>" |
| 434 |
class="regular-text"/> |
| 435 |
|
| 436 |
<p class="description"><?php esc_html_e( 'PHP/WordPress Locale of the language. (Examples: en_US, ru_RU)', 'wpglobus' ); ?></p> |
| 437 |
</td> |
| 438 |
</tr> |
| 439 |
</table> |
| 440 |
<?php |
| 441 |
|
| 442 |
if ( WPGlobus_Language_Edit_Request::ACTION_EDIT === $this->action || WPGlobus_Language_Edit_Request::ACTION_ADD === $this->action ) { |
| 443 |
?> |
| 444 |
<input class="button button-primary" type="submit" name="submit" |
| 445 |
value="<?php esc_attr_e( 'Save Changes', 'wpglobus' ); ?>"> |
| 446 |
<?php |
| 447 |
|
| 448 |
if ( WPGlobus_Language_Edit_Request::ACTION_EDIT === $this->action ) { |
| 449 |
?> |
| 450 |
|
| 451 |
<a class="button button-link-delete" style="margin-left: 1em" |
| 452 |
href="<?php echo esc_url( WPGlobus_Language_Edit_Request::url_language_delete( $this->language_code ) ); ?>"> |
| 453 |
<i class="dashicons dashicons-trash" style="margin-top: 4px;"></i> |
| 454 |
<?php esc_html_e( 'Delete Language', 'wpglobus' ); ?>…</a> |
| 455 |
<?php |
| 456 |
} |
| 457 |
} elseif ( WPGlobus_Language_Edit_Request::ACTION_DELETE === $this->action ) { |
| 458 |
?> |
| 459 |
<div class="notice-large wp-ui-notification"><?php esc_html_e( 'Are you sure you want to delete?', 'wpglobus' ); ?></div> |
| 460 |
<p class="submit"><input class="button button-primary" type="submit" name="delete" |
| 461 |
value="<?php esc_attr_e( 'Delete Language', 'wpglobus' ); ?>"></p> |
| 462 |
<?php } ?> |
| 463 |
|
| 464 |
</form> |
| 465 |
|
| 466 |
<hr/> |
| 467 |
<span class="dashicons dashicons-admin-site"></span> |
| 468 |
<a href="<?php echo esc_url( WPGlobus_Admin_Page::url_settings() ); ?>"> |
| 469 |
<?php esc_html_e( 'Back to the WPGlobus Settings', 'wpglobus' ); ?> |
| 470 |
</a> |
| 471 |
</div> |
| 472 |
<?php |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Get flag files from directory |
| 477 |
*/ |
| 478 |
protected function get_flags() { |
| 479 |
|
| 480 |
$dir = new DirectoryIterator( WPGlobus::plugin_dir_path() . 'flags/' ); |
| 481 |
|
| 482 |
foreach ( $dir as $file ) { |
| 483 |
/** |
| 484 |
* File object |
| 485 |
* |
| 486 |
* @var DirectoryIterator $file |
| 487 |
*/ |
| 488 |
if ( $file->isFile() ) { |
| 489 |
$this->all_flags[] = $file->getFilename(); |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
|