| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Main plugin file |
| 5 |
* |
| 6 |
* @package WordPress |
| 7 |
* @subpackage SALC |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace SALC; |
| 12 |
|
| 13 |
use WP_Admin_Bar; |
| 14 |
|
| 15 |
/** |
| 16 |
* Plugin Name: Simple Admin Language Change |
| 17 |
* Plugin URI: http://kybernaut.cz/pluginy/simple-admin-language-change |
| 18 |
* Description: Change your dashboard language quickly and easily in the admin bar. |
| 19 |
* Version: 2.1.0 |
| 20 |
* Author: Karolína Vyskočilová |
| 21 |
* Author URI: https://www.kybernaut.cz |
| 22 |
* Text Domain: simple-admin-language-change |
| 23 |
* License: GPL-2.0+ |
| 24 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 25 |
* Domain Path: /languages |
| 26 |
*/ |
| 27 |
|
| 28 |
// If this file is called directly, abort. |
| 29 |
if (! defined('WPINC')) { |
| 30 |
die; |
| 31 |
} |
| 32 |
|
| 33 |
define('SALC_VERSION', '2.1.0'); |
| 34 |
|
| 35 |
/** |
| 36 |
* Localize the plugin |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
function localize_plugin() |
| 41 |
{ |
| 42 |
load_plugin_textdomain('simple-admin-language-change', false, plugin_dir_path(__FILE__) . 'languages/'); |
| 43 |
} |
| 44 |
add_action('init', __NAMESPACE__ . '\localize_plugin'); |
| 45 |
|
| 46 |
require_once 'inc/upgrade.php'; |
| 47 |
require_once 'inc/helpers.php'; |
| 48 |
require_once 'inc/scripts-and-styles.php'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Add menu to admin bar |
| 52 |
* |
| 53 |
* @param WP_Admin_Bar $admin_bar Admin bar instance. |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
function admin_menu($admin_bar) |
| 57 |
{ |
| 58 |
|
| 59 |
// Check for permissions and if it is admin. |
| 60 |
if (! current_user_can('read') || ! is_admin() ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$languages = parse_wp_dropdown_languages(); |
| 65 |
|
| 66 |
$admin_bar->add_menu([ |
| 67 |
'id' => 'salc-current-language', |
| 68 |
'parent' => 'top-secondary', |
| 69 |
'group' => null, |
| 70 |
'title' => '<span class="ab-icon"></span>' . $languages['active']['title'], |
| 71 |
'href' => '#', |
| 72 |
'meta' => [ |
| 73 |
'title' => __('Current dashboard language', 'simple-admin-language-change'), |
| 74 |
'onclick' => "return false;" |
| 75 |
] |
| 76 |
]); |
| 77 |
|
| 78 |
foreach ($languages['available'] as $la) { |
| 79 |
$admin_bar->add_menu([ |
| 80 |
'id' => 'salc-current-' . sanitize_title($la['title']), |
| 81 |
'parent' => 'salc-current-language', |
| 82 |
'group' => null, |
| 83 |
'title' => $la['title'], |
| 84 |
'href' => '#' . ($la['value'] ? $la['value'] : 'en_US'), |
| 85 |
]); |
| 86 |
} |
| 87 |
} |
| 88 |
add_action('admin_bar_menu', __NAMESPACE__ . '\admin_menu', 500); |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Change user locale |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
function change_user_locale_ajax() |
| 97 |
{ |
| 98 |
|
| 99 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 100 |
if (!isset($_REQUEST['nonce']) || !wp_verify_nonce($_REQUEST['nonce'], "salc_change_user_locale")) { |
| 101 |
wp_die(esc_html(__('Something went wrong, try again.', 'simple-admin-language-change'))); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
// Check for permissions and if it is admin. |
| 106 |
if (! current_user_can('read') || ! is_admin()) { |
| 107 |
|
| 108 |
wp_die(esc_html(__('You don\'t have the correct permissions for language change.', 'simple-admin-language-change'))); |
| 109 |
} |
| 110 |
|
| 111 |
$user_id = \get_current_user_id(); |
| 112 |
|
| 113 |
$lang = isset($_REQUEST['lang']) ? \sanitize_text_field(wp_unslash($_REQUEST['lang'])) : false; |
| 114 |
|
| 115 |
if (! $user_id || ! $lang) { |
| 116 |
\wp_send_json_error('updated', 403); |
| 117 |
wp_die(); |
| 118 |
} |
| 119 |
|
| 120 |
if ($lang === 'site-default') { |
| 121 |
$lang = null; |
| 122 |
} |
| 123 |
|
| 124 |
wp_update_user(['ID' => $user_id, 'locale' => $lang]); |
| 125 |
|
| 126 |
\wp_send_json_success('updated', 200); |
| 127 |
wp_die(); |
| 128 |
} |
| 129 |
add_action("wp_ajax_change_user_locale", __NAMESPACE__ . "\change_user_locale_ajax"); |
| 130 |
|