| 1 |
<?php |
| 2 |
|
| 3 |
require_once BOGO_PLUGIN_DIR . '/admin/includes/user.php'; |
| 4 |
require_once BOGO_PLUGIN_DIR . '/admin/includes/post.php'; |
| 5 |
require_once BOGO_PLUGIN_DIR . '/admin/includes/nav-menu.php'; |
| 6 |
require_once BOGO_PLUGIN_DIR . '/admin/includes/widgets.php'; |
| 7 |
require_once BOGO_PLUGIN_DIR . '/admin/includes/language-packs.php'; |
| 8 |
require_once BOGO_PLUGIN_DIR . '/admin/includes/terms-translation.php'; |
| 9 |
|
| 10 |
add_action( 'admin_init', 'bogo_upgrade', 10, 0 ); |
| 11 |
|
| 12 |
function bogo_upgrade() { |
| 13 |
$old_ver = bogo_get_prop( 'version' ); |
| 14 |
$new_ver = BOGO_VERSION; |
| 15 |
|
| 16 |
if ( $old_ver !== $new_ver ) { |
| 17 |
require_once BOGO_PLUGIN_DIR . '/admin/includes/upgrade.php'; |
| 18 |
do_action( 'bogo_upgrade', $new_ver, $old_ver ); |
| 19 |
bogo_set_prop( 'version', $new_ver ); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
add_action( 'admin_enqueue_scripts', 'bogo_admin_enqueue_scripts', 10, 1 ); |
| 24 |
|
| 25 |
function bogo_admin_enqueue_scripts( $hook_suffix ) { |
| 26 |
wp_enqueue_style( 'bogo-admin', |
| 27 |
plugins_url( 'admin/includes/css/admin.css', BOGO_PLUGIN_BASENAME ), |
| 28 |
array(), BOGO_VERSION, 'all' |
| 29 |
); |
| 30 |
|
| 31 |
if ( is_rtl() ) { |
| 32 |
wp_enqueue_style( 'bogo-admin-rtl', |
| 33 |
plugins_url( 'admin/includes/css/admin-rtl.css', BOGO_PLUGIN_BASENAME ), |
| 34 |
array(), BOGO_VERSION, 'all' |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
$assets = include BOGO_PLUGIN_DIR . '/admin/includes/js/index.asset.php'; |
| 39 |
|
| 40 |
wp_enqueue_script( 'bogo-admin', |
| 41 |
plugins_url( 'admin/includes/js/index.js', BOGO_PLUGIN_BASENAME ), |
| 42 |
$assets['dependencies'], |
| 43 |
$assets['version'], |
| 44 |
array( 'in_footer' => true ) |
| 45 |
); |
| 46 |
|
| 47 |
wp_set_script_translations( 'bogo-admin', 'bogo' ); |
| 48 |
|
| 49 |
$available_languages = array(); |
| 50 |
|
| 51 |
foreach ( bogo_available_languages() as $locale => $language ) { |
| 52 |
$native_name = bogo_get_language_native_name( $locale ); |
| 53 |
|
| 54 |
if ( bogo_locale_is_alone( $locale ) ) { |
| 55 |
$native_name = bogo_get_short_name( $native_name ); |
| 56 |
} |
| 57 |
|
| 58 |
$tags = array( |
| 59 |
bogo_language_tag( $locale ), |
| 60 |
bogo_lang_slug( $locale ), |
| 61 |
); |
| 62 |
|
| 63 |
$available_languages[$locale] = array( |
| 64 |
'name' => $language, |
| 65 |
'nativename' => trim( $native_name ), |
| 66 |
'country' => bogo_get_country_code( $locale ), |
| 67 |
'tags' => array_unique( array_filter( $tags ) ), |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
$bogo_obj = array( |
| 72 |
'availableLanguages' => $available_languages, |
| 73 |
'defaultLocale' => bogo_get_default_locale(), |
| 74 |
'currentPost' => array(), |
| 75 |
'localizablePostTypes' => bogo_localizable_post_types(), |
| 76 |
'showFlags' => apply_filters( 'bogo_use_flags', true ), |
| 77 |
); |
| 78 |
|
| 79 |
if ( in_array( $hook_suffix, array( 'post.php', 'post-new.php' ) ) ) { |
| 80 |
if ( $screen = get_current_screen() and $screen->is_block_editor() ) { |
| 81 |
wp_enqueue_script( 'bogo-block-editor' ); |
| 82 |
} |
| 83 |
|
| 84 |
$user_locale = bogo_get_user_locale(); |
| 85 |
|
| 86 |
$current_post = array( |
| 87 |
'locale' => $user_locale, |
| 88 |
'lang' => bogo_lang_slug( $user_locale ), |
| 89 |
'translations' => array(), |
| 90 |
); |
| 91 |
|
| 92 |
if ( $post = get_post() ) { |
| 93 |
$current_post['postId'] = $post->ID; |
| 94 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 95 |
$edit_post_cap = $post_type_object->cap->edit_post; |
| 96 |
|
| 97 |
if ( $locale = get_post_meta( $post->ID, '_locale', true ) ) { |
| 98 |
$current_post['locale'] = $locale; |
| 99 |
$current_post['lang'] = bogo_lang_slug( $locale ); |
| 100 |
} |
| 101 |
|
| 102 |
$available_locales = bogo_available_locales( array( |
| 103 |
'exclude' => array( $current_post['locale'] ), |
| 104 |
) ); |
| 105 |
|
| 106 |
foreach ( $available_locales as $locale ) { |
| 107 |
$current_post['translations'][$locale] = array(); |
| 108 |
|
| 109 |
$translation = bogo_get_post_translation( $post->ID, $locale ); |
| 110 |
|
| 111 |
if ( $translation ) { |
| 112 |
$current_post['translations'][$locale] = array( |
| 113 |
'postId' => $translation->ID, |
| 114 |
'postTitle' => $translation->post_title, |
| 115 |
'editLink' => current_user_can( $edit_post_cap, $translation->ID ) |
| 116 |
? get_edit_post_link( $translation, 'raw' ) |
| 117 |
: '', |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$bogo_obj['currentPost'] = $current_post; |
| 124 |
} |
| 125 |
|
| 126 |
wp_add_inline_script( 'bogo-admin', |
| 127 |
sprintf( |
| 128 |
'var bogo = %s;', |
| 129 |
wp_json_encode( $bogo_obj, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) |
| 130 |
), |
| 131 |
'before' |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
add_action( 'admin_menu', 'bogo_admin_menu', 10, 0 ); |
| 136 |
|
| 137 |
function bogo_admin_menu() { |
| 138 |
add_menu_page( |
| 139 |
__( 'Languages', 'bogo' ), |
| 140 |
__( 'Languages', 'bogo' ), |
| 141 |
'bogo_manage_language_packs', |
| 142 |
'bogo', |
| 143 |
'bogo_tools_page', |
| 144 |
'dashicons-translation', |
| 145 |
73 // between Users (70) and Tools (75) |
| 146 |
); |
| 147 |
|
| 148 |
$tools = add_submenu_page( |
| 149 |
'bogo', |
| 150 |
__( 'Language Packs', 'bogo' ), |
| 151 |
__( 'Language Packs', 'bogo' ), |
| 152 |
'bogo_manage_language_packs', |
| 153 |
'bogo', |
| 154 |
'bogo_tools_page' |
| 155 |
); |
| 156 |
|
| 157 |
add_action( 'load-' . $tools, 'bogo_load_tools_page', 10, 0 ); |
| 158 |
|
| 159 |
$available_locales = bogo_available_locales( array( |
| 160 |
'current_user_can_access' => true, |
| 161 |
'exclude' => array( bogo_get_default_locale() ), |
| 162 |
) ); |
| 163 |
|
| 164 |
if ( 0 < count( $available_locales ) ) { |
| 165 |
$texts = add_submenu_page( |
| 166 |
'bogo', |
| 167 |
__( 'Terms Translation', 'bogo' ), |
| 168 |
__( 'Terms Translation', 'bogo' ), |
| 169 |
'bogo_edit_terms_translation', |
| 170 |
'bogo-texts', |
| 171 |
'bogo_texts_page' |
| 172 |
); |
| 173 |
|
| 174 |
add_action( 'load-' . $texts, 'bogo_load_texts_page', 10, 0 ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
add_filter( |
| 179 |
'set_screen_option_bogo_texts_per_page', |
| 180 |
'bogo_set_screen_options', |
| 181 |
10, 3 |
| 182 |
); |
| 183 |
|
| 184 |
function bogo_set_screen_options( $result, $option, $value ) { |
| 185 |
$bogo_screens = array( |
| 186 |
'bogo_texts_per_page', |
| 187 |
); |
| 188 |
|
| 189 |
if ( in_array( $option, $bogo_screens ) ) { |
| 190 |
$result = $value; |
| 191 |
} |
| 192 |
|
| 193 |
return $result; |
| 194 |
} |
| 195 |
|
| 196 |
function bogo_load_tools_page() { |
| 197 |
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); |
| 198 |
|
| 199 |
$action = $_GET['action'] ?? ''; |
| 200 |
$locale = $_GET['locale'] ?? null; |
| 201 |
|
| 202 |
if ( 'activate' === $action ) { |
| 203 |
check_admin_referer( 'bogo-tools' ); |
| 204 |
|
| 205 |
if ( ! current_user_can( 'bogo_manage_language_packs' ) ) { |
| 206 |
wp_die( wp_kses_data( __( 'You are not allowed to manage translations.', 'bogo' ) ) ); |
| 207 |
} |
| 208 |
|
| 209 |
if ( 'en_US' === $locale ) { |
| 210 |
bogo_set_prop( 'enus_deactivated', false ); |
| 211 |
|
| 212 |
$redirect_to = add_query_arg( |
| 213 |
array( 'message' => 'enus_activated' ), |
| 214 |
menu_page_url( 'bogo', false ) |
| 215 |
); |
| 216 |
} else { |
| 217 |
if ( wp_download_language_pack( $locale ) ) { |
| 218 |
$redirect_to = add_query_arg( |
| 219 |
array( 'locale' => $locale, 'message' => 'install_success' ), |
| 220 |
menu_page_url( 'bogo', false ) |
| 221 |
); |
| 222 |
} else { |
| 223 |
$redirect_to = add_query_arg( |
| 224 |
array( 'locale' => $locale, 'message' => 'install_failed' ), |
| 225 |
menu_page_url( 'bogo', false ) |
| 226 |
); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
wp_safe_redirect( $redirect_to ); |
| 231 |
exit(); |
| 232 |
} |
| 233 |
|
| 234 |
if ( 'deactivate' === $action ) { |
| 235 |
check_admin_referer( 'bogo-tools' ); |
| 236 |
|
| 237 |
if ( ! current_user_can( 'bogo_manage_language_packs' ) ) { |
| 238 |
wp_die( wp_kses_data( __( 'You are not allowed to manage translations.', 'bogo' ) ) ); |
| 239 |
} |
| 240 |
|
| 241 |
if ( 'en_US' === $locale ) { |
| 242 |
bogo_set_prop( 'enus_deactivated', true ); |
| 243 |
|
| 244 |
$redirect_to = add_query_arg( |
| 245 |
array( 'message' => 'enus_deactivated' ), |
| 246 |
menu_page_url( 'bogo', false ) |
| 247 |
); |
| 248 |
} else { |
| 249 |
if ( bogo_delete_language_pack( $locale ) ) { |
| 250 |
$redirect_to = add_query_arg( |
| 251 |
array( 'locale' => $locale, 'message' => 'delete_success' ), |
| 252 |
menu_page_url( 'bogo', false ) |
| 253 |
); |
| 254 |
} else { |
| 255 |
$redirect_to = add_query_arg( |
| 256 |
array( 'locale' => $locale, 'message' => 'delete_failed' ), |
| 257 |
menu_page_url( 'bogo', false ) |
| 258 |
); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
wp_safe_redirect( $redirect_to ); |
| 263 |
exit(); |
| 264 |
} |
| 265 |
|
| 266 |
if ( 'promote' === $action ) { |
| 267 |
check_admin_referer( 'bogo-tools' ); |
| 268 |
|
| 269 |
if ( ! current_user_can( 'bogo_manage_language_packs' ) ) { |
| 270 |
wp_die( wp_kses_data( __( 'You are not allowed to manage translations.', 'bogo' ) ) ); |
| 271 |
} |
| 272 |
|
| 273 |
if ( 'en_US' === $locale or ! bogo_is_available_locale( $locale ) ) { |
| 274 |
$locale = ''; |
| 275 |
} |
| 276 |
|
| 277 |
if ( update_option( 'WPLANG', $locale ) ) { |
| 278 |
$redirect_to = add_query_arg( |
| 279 |
array( 'locale' => $locale, 'message' => 'promote_success' ), |
| 280 |
menu_page_url( 'bogo', false ) |
| 281 |
); |
| 282 |
} else { |
| 283 |
$redirect_to = add_query_arg( |
| 284 |
array( 'locale' => $locale, 'message' => 'promote_failed' ), |
| 285 |
menu_page_url( 'bogo', false ) |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
wp_safe_redirect( $redirect_to ); |
| 290 |
exit(); |
| 291 |
} |
| 292 |
|
| 293 |
if ( 'translate' === $action ) { |
| 294 |
check_admin_referer( 'bogo-tools' ); |
| 295 |
|
| 296 |
if ( ! current_user_can( 'bogo_edit_terms_translation', $locale ) ) { |
| 297 |
wp_die( wp_kses_data( __( 'You are not allowed to edit translations.', 'bogo' ) ) ); |
| 298 |
} |
| 299 |
|
| 300 |
$is_active = ( 'en_US' === $locale ) |
| 301 |
? ! bogo_is_enus_deactivated() |
| 302 |
: bogo_is_available_locale( $locale ); |
| 303 |
|
| 304 |
if ( ! bogo_is_default_locale( $locale ) and $is_active ) { |
| 305 |
$redirect_to = add_query_arg( |
| 306 |
array( 'locale' => $locale ), |
| 307 |
menu_page_url( 'bogo-texts', false ) ); |
| 308 |
|
| 309 |
wp_safe_redirect( $redirect_to ); |
| 310 |
exit(); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
$current_screen = get_current_screen(); |
| 315 |
|
| 316 |
add_filter( 'manage_' . $current_screen->id . '_columns', |
| 317 |
array( 'Bogo_Language_Packs_List_Table', 'define_columns' ), |
| 318 |
10, 1 |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
function bogo_tools_page() { |
| 323 |
$list_table = new Bogo_Language_Packs_List_Table(); |
| 324 |
$list_table->prepare_items(); |
| 325 |
|
| 326 |
?> |
| 327 |
<div class="wrap"> |
| 328 |
|
| 329 |
<h1 class="wp-heading-inline"><?php |
| 330 |
echo esc_html( __( 'Language Packs', 'bogo' ) ); |
| 331 |
?></h1> |
| 332 |
|
| 333 |
<?php |
| 334 |
if ( ! empty( $_REQUEST['s'] ) ) { |
| 335 |
echo sprintf( |
| 336 |
'<span class="subtitle">%s</span>', |
| 337 |
wp_kses_data( sprintf( |
| 338 |
/* translators: %s: search query */ |
| 339 |
__( 'Search results for: <strong>%s</strong>', 'bogo' ), |
| 340 |
esc_html( $_REQUEST['s'] ) |
| 341 |
) ) |
| 342 |
); |
| 343 |
} |
| 344 |
?> |
| 345 |
|
| 346 |
<hr class="wp-header-end"> |
| 347 |
|
| 348 |
<?php bogo_admin_notice(); ?> |
| 349 |
|
| 350 |
<?php $list_table->views(); ?> |
| 351 |
|
| 352 |
<form method="get" action="" id="bogo-language-packs"> |
| 353 |
<input type="hidden" name="page" value="<?php echo esc_attr( $_REQUEST['page'] ); ?>" /> |
| 354 |
<?php $list_table->search_box( __( 'Search Language', 'bogo' ), 'bogo-language-packs' ); ?> |
| 355 |
<?php $list_table->display(); ?> |
| 356 |
</form> |
| 357 |
|
| 358 |
</div> |
| 359 |
<?php |
| 360 |
} |
| 361 |
|
| 362 |
function bogo_load_texts_page() { |
| 363 |
$action = $_POST['action'] ?? ''; |
| 364 |
|
| 365 |
if ( 'save' === $action ) { |
| 366 |
check_admin_referer( 'bogo-edit-text-translation' ); |
| 367 |
|
| 368 |
if ( ! current_user_can( 'bogo_edit_terms_translation' ) ) { |
| 369 |
wp_die( wp_kses_data( __( 'You are not allowed to edit translations.', 'bogo' ) ) ); |
| 370 |
} |
| 371 |
|
| 372 |
$locale = $_POST['locale'] ?? null; |
| 373 |
|
| 374 |
if ( ! bogo_is_available_locale( $locale ) ) { |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
if ( ! current_user_can( 'bogo_access_locale', $locale ) ) { |
| 379 |
wp_die( wp_kses_data( __( 'You are not allowed to edit terms in this locale.', 'bogo' ) ) ); |
| 380 |
} |
| 381 |
|
| 382 |
$entries = array(); |
| 383 |
|
| 384 |
foreach ( (array) bogo_terms_translation( $locale ) as $item ) { |
| 385 |
$translation = $item['translated']; |
| 386 |
|
| 387 |
$cap = $item['cap'] ?? 'bogo_edit_terms_translation'; |
| 388 |
|
| 389 |
if ( isset( $_POST[$item['name']] ) and current_user_can( $cap ) ) { |
| 390 |
$translation = $_POST[$item['name']]; |
| 391 |
} |
| 392 |
|
| 393 |
$entries[] = array( |
| 394 |
'singular' => $item['name'], |
| 395 |
'translations' => array( $translation ), |
| 396 |
'context' => preg_replace( '/:.*$/', '', $item['name'] ), |
| 397 |
); |
| 398 |
} |
| 399 |
|
| 400 |
if ( Bogo_POMO::export( $locale, $entries ) ) { |
| 401 |
$message = 'translation_saved'; |
| 402 |
} else { |
| 403 |
$message = 'translation_failed'; |
| 404 |
} |
| 405 |
|
| 406 |
$redirect_to = add_query_arg( |
| 407 |
array( |
| 408 |
'locale' => $locale, |
| 409 |
'message' => $message, |
| 410 |
'paged' => absint( $_POST['paged'] ?? 1 ), |
| 411 |
), |
| 412 |
menu_page_url( 'bogo-texts', false ) |
| 413 |
); |
| 414 |
|
| 415 |
wp_safe_redirect( $redirect_to ); |
| 416 |
exit(); |
| 417 |
|
| 418 |
} else { |
| 419 |
$current_screen = get_current_screen(); |
| 420 |
|
| 421 |
add_filter( 'manage_' . $current_screen->id . '_columns', |
| 422 |
array( 'Bogo_Terms_Translation_List_Table', 'define_columns' ), |
| 423 |
10, 1 |
| 424 |
); |
| 425 |
|
| 426 |
add_screen_option( 'per_page', array( |
| 427 |
'default' => 20, |
| 428 |
'option' => 'bogo_texts_per_page', |
| 429 |
) ); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
function bogo_texts_page() { |
| 434 |
$list_table = new Bogo_Terms_Translation_List_Table(); |
| 435 |
$list_table->prepare_items(); |
| 436 |
|
| 437 |
?> |
| 438 |
<div class="wrap"> |
| 439 |
|
| 440 |
<h1 class="wp-heading-inline"><?php |
| 441 |
echo esc_html( __( 'Terms Translation', 'bogo' ) ); |
| 442 |
?></h1> |
| 443 |
|
| 444 |
<?php |
| 445 |
if ( ! empty( $_REQUEST['s'] ) ) { |
| 446 |
echo sprintf( |
| 447 |
'<span class="subtitle">%s</span>', |
| 448 |
wp_kses_data( sprintf( |
| 449 |
/* translators: %s: search query */ |
| 450 |
__( 'Search results for: <strong>%s</strong>', 'bogo' ), |
| 451 |
esc_html( $_REQUEST['s'] ) |
| 452 |
) ) |
| 453 |
); |
| 454 |
} |
| 455 |
?> |
| 456 |
|
| 457 |
<hr class="wp-header-end"> |
| 458 |
|
| 459 |
<?php bogo_admin_notice(); ?> |
| 460 |
|
| 461 |
<form action="" method="get"> |
| 462 |
<input type="hidden" name="page" value="<?php echo esc_attr( $_REQUEST['page'] ?? '' ); ?>" /> |
| 463 |
<input type="hidden" name="locale" value="<?php echo esc_attr( $_REQUEST['locale'] ?? '' ); ?>" /> |
| 464 |
<?php |
| 465 |
$list_table->search_box( |
| 466 |
__( 'Search Translation', 'bogo' ), 'bogo-terms-translation' |
| 467 |
); |
| 468 |
?> |
| 469 |
</form> |
| 470 |
|
| 471 |
<form action="" method="post" id="bogo-terms-translation"> |
| 472 |
<input type="hidden" name="action" value="save" /> |
| 473 |
<input type="hidden" name="paged" value="<?php echo absint( $_GET['paged'] ?? '' ); ?>" /> |
| 474 |
<?php |
| 475 |
wp_nonce_field( 'bogo-edit-text-translation' ); |
| 476 |
$list_table->display(); |
| 477 |
?> |
| 478 |
</form> |
| 479 |
</div> |
| 480 |
<?php |
| 481 |
} |
| 482 |
|
| 483 |
function bogo_admin_notice( $reason = '' ) { |
| 484 |
if ( empty( $reason ) and isset( $_GET['message'] ) ) { |
| 485 |
$reason = $_GET['message']; |
| 486 |
} |
| 487 |
|
| 488 |
if ( 'install_success' === $reason ) { |
| 489 |
$message = __( 'Translation installed successfully.', 'bogo' ); |
| 490 |
} elseif ( 'install_failed' === $reason ) { |
| 491 |
$message = __( 'Translation install failed.', 'bogo' ); |
| 492 |
} elseif ( 'promote_success' === $reason ) { |
| 493 |
$message = __( 'Site language set successfully.', 'bogo' ); |
| 494 |
} elseif ( 'promote_failed' === $reason ) { |
| 495 |
$message = __( 'Setting site language failed.', 'bogo' ); |
| 496 |
} elseif ( 'delete_success' === $reason ) { |
| 497 |
$message = __( 'Translation uninstalled successfully.', 'bogo' ); |
| 498 |
} elseif ( 'delete_failed' === $reason ) { |
| 499 |
$message = __( 'Translation uninstall failed.', 'bogo' ); |
| 500 |
} elseif ( 'enus_deactivated' === $reason ) { |
| 501 |
$message = __( 'English (United States) deactivated.', 'bogo' ); |
| 502 |
} elseif ( 'enus_activated' === $reason ) { |
| 503 |
$message = __( 'English (United States) activated.', 'bogo' ); |
| 504 |
} elseif ( 'translation_saved' === $reason ) { |
| 505 |
$message = __( 'Translation saved.', 'bogo' ); |
| 506 |
} elseif ( 'translation_failed' === $reason ) { |
| 507 |
$message = __( 'Saving translation failed.', 'bogo' ); |
| 508 |
} else { |
| 509 |
return false; |
| 510 |
} |
| 511 |
|
| 512 |
wp_admin_notice( $message, array( |
| 513 |
'type' => str_ends_with( $reason, '_failed' ) ? 'error' : 'success', |
| 514 |
) ); |
| 515 |
} |
| 516 |
|