PluginProbe
Bogo / 3.9
Bogo v3.9
3.9.3 trunk 1.0 1.1 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.8 2.8.1 2.9 3.0 3.0.1 All 52 releases
bogo / includes / user.php

user.php in Bogo 3.9, at includes/user.php

181 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /* Toolbar (Admin Bar) */
4
5 add_action( 'admin_bar_menu', 'bogo_admin_bar_init', 0, 1 );
6
7 function bogo_admin_bar_init( $wp_admin_bar ) {
8 switch_to_locale( bogo_get_user_locale() );
9 }
10
11
12 add_action( 'wp_after_admin_bar_render', 'bogo_after_admin_bar_render', 10, 0 );
13
14 function bogo_after_admin_bar_render() {
15 if ( is_locale_switched() ) {
16 restore_current_locale();
17 }
18 }
19
20
21 add_action( 'admin_bar_menu', 'bogo_admin_bar_menu', 10, 1 );
22
23 function bogo_admin_bar_menu( $wp_admin_bar ) {
24 $current_locale = bogo_get_user_locale();
25
26 $available_languages = bogo_available_languages( array(
27 'current_user_can_access' => true,
28 ) );
29
30 if ( isset( $available_languages[$current_locale] ) ) {
31 $current_language = $available_languages[$current_locale];
32 unset( $available_languages[$current_locale] );
33 } else {
34 $current_language = $current_locale;
35 }
36
37 $wp_admin_bar->add_menu( array(
38 'parent' => 'top-secondary',
39 'id' => 'bogo-user-locale',
40 'title' => sprintf(
41 '<span class="ab-icon"></span><span class="ab-label">%s</span>',
42 esc_html( $current_language )
43 ),
44 ) );
45
46 foreach ( $available_languages as $locale => $lang ) {
47 $url = add_query_arg(
48 array(
49 'action' => 'bogo-switch-locale',
50 'locale' => $locale,
51 'redirect_to' => urlencode( $_SERVER['REQUEST_URI'] ),
52 ),
53 admin_url( 'profile.php' )
54 );
55
56 $url = wp_nonce_url( $url, 'bogo-switch-locale' );
57
58 $wp_admin_bar->add_menu( array(
59 'parent' => 'bogo-user-locale',
60 'id' => 'bogo-user-locale-' . $locale,
61 'title' => $lang,
62 'href' => $url,
63 ) );
64 }
65 }
66
67
68 add_action( 'admin_init', 'bogo_switch_user_locale', 10, 0 );
69
70 function bogo_switch_user_locale() {
71 if (
72 empty( $_REQUEST['action'] ) or
73 'bogo-switch-locale' !== $_REQUEST['action']
74 ) {
75 return;
76 }
77
78 check_admin_referer( 'bogo-switch-locale' );
79
80 $locale = $_REQUEST['locale'] ?? '';
81
82 if (
83 ! bogo_is_available_locale( $locale ) or
84 $locale === bogo_get_user_locale()
85 ) {
86 return;
87 }
88
89 update_user_option( get_current_user_id(), 'locale', $locale, true );
90
91 if ( ! empty( $_REQUEST['redirect_to'] ) ) {
92 wp_safe_redirect( $_REQUEST['redirect_to'] );
93 exit();
94 }
95 }
96
97
98 function bogo_get_user_locale( $user_id = 0 ) {
99 $default_locale = bogo_get_default_locale();
100
101 if ( ! $user_id = absint( $user_id ) ) {
102 if ( function_exists( 'wp_get_current_user' ) ) {
103 $current_user = wp_get_current_user();
104
105 if ( ! empty( $current_user->locale ) ) {
106 return $current_user->locale;
107 }
108 }
109
110 if ( ! $user_id = get_current_user_id() ) {
111 return $default_locale;
112 }
113 }
114
115 $locale = get_user_option( 'locale', $user_id );
116
117 if ( bogo_is_available_locale( $locale ) ) {
118 return $locale;
119 }
120
121 return $default_locale;
122 }
123
124
125 function bogo_get_user_accessible_locales( $user_id ) {
126 global $wpdb;
127
128 $user_id = absint( $user_id );
129
130 if ( user_can( $user_id, 'bogo_access_all_locales' ) ) {
131 $locales = bogo_available_locales();
132
133 return $locales;
134 }
135
136 $meta_key = $wpdb->get_blog_prefix() . 'accessible_locale';
137
138 $locales = (array) get_user_meta( $user_id, $meta_key );
139
140 if ( bogo_is_enus_deactivated() ) {
141 $locales = array_diff( $locales, array( 'en_US' ) );
142 }
143
144 $locales = bogo_filter_locales( $locales );
145
146 if ( empty( $locales ) ) {
147 $locales = array( bogo_get_default_locale() );
148 }
149
150 return $locales;
151 }
152
153
154 add_filter( 'insert_user_meta', 'bogo_user_meta_filter', 10, 3 );
155
156 function bogo_user_meta_filter( $meta, $user, $update ) {
157 if ( user_can( $user, 'bogo_access_all_locales' ) ) {
158 return $meta;
159 }
160
161 $locale = $meta['locale'];
162
163 if ( empty( $locale ) ) {
164 $locale = bogo_get_default_locale();
165 }
166
167 $accessible_locales = bogo_filter_locales(
168 bogo_get_user_accessible_locales( $user->ID )
169 );
170
171 if ( empty( $accessible_locales ) ) {
172 $locale = '';
173 } elseif ( ! in_array( $locale, $accessible_locales, true ) ) {
174 $locale = $accessible_locales[0];
175 }
176
177 $meta['locale'] = $locale;
178
179 return $meta;
180 }
181