PluginProbe
Simple Admin Language Change / 2.0.0
Simple Admin Language Change v2.0.0
trunk 1.0.0 1.0.1 1.0.2 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1.0 2.1.1
simple-admin-language-change / simple-admin-language-change.php

simple-admin-language-change.php in Simple Admin Language Change 2.0.0, at simple-admin-language-change.php

121 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.0.0
20 * Author: Karolína Vyskočilová
21 * Author URI: https://www.kybernaut.cz
22 * Text Domain: kbnt-sacl
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.0.0');
34
35 /**
36 * Localize the plugin
37 *
38 * @return void
39 */
40 function localize_plugin()
41 {
42 load_plugin_textdomain('kbnt-sacl', 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 // Check for permissions matching the user_locale.
59 if (! current_user_can('edit_posts') || ! current_user_can('edit_pages')) {
60 return;
61 }
62
63 $languages = parse_wp_dropdown_languages();
64
65 $admin_bar->add_menu([
66 'id' => 'salc-current-language',
67 'parent' => 'top-secondary',
68 'group' => null,
69 'title' => '<span class="ab-icon"></span>' . $languages['active']['title'],
70 'href' => '#',
71 'meta' => [
72 'title' => __('Current dashboard language', 'kbnt-sacl'),
73 'onclick' => "return false;"
74 ]
75 ]);
76
77 foreach ($languages['available'] as $la) {
78 $admin_bar->add_menu([
79 'id' => 'salc-current-' . sanitize_title($la['title']),
80 'parent' => 'salc-current-language',
81 'group' => null,
82 'title' => $la['title'],
83 'href' => '#' . ($la['value'] ? $la['value'] : 'en_US'),
84 ]);
85 }
86 }
87 add_action('admin_bar_menu', __NAMESPACE__ . '\admin_menu', 500);
88
89
90 /**
91 * Change user locale
92 *
93 * @return void
94 */
95 function change_user_locale_ajax()
96 {
97
98 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
99 if (isset($_REQUEST['nonce']) && !wp_verify_nonce($_REQUEST['nonce'], "salc_change_user_locale")) {
100 wp_die("Something went wrong, try again.");
101 }
102
103 $user_id = isset($_REQUEST['user_id']) ? intval(wp_unslash($_REQUEST['user_id'])) : false;
104 $lang = isset($_REQUEST['lang']) ? \sanitize_text_field(wp_unslash($_REQUEST['lang'])) : false;
105
106 if (! $user_id || ! $lang) {
107 \wp_send_json_error('updated', 403);
108 wp_die();
109 }
110
111 if ($lang === 'site-default') {
112 $lang = null;
113 }
114
115 wp_update_user(['ID' => $user_id, 'locale' => $lang]);
116
117 \wp_send_json_success('updated', 200);
118 wp_die();
119 }
120 add_action("wp_ajax_change_user_locale", __NAMESPACE__ . "\change_user_locale_ajax");
121