PluginProbe
Bogo / 2.9
Bogo v2.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
← All changes | includes/user.php +46 -106 trunk2.9 View file →
@@ -1,37 +1,15 @@
1 1 <?php
2 2
3 -/* Toolbar (Admin Bar) */
3 +/* Admin Bar */
4 4
5 -add_action( 'admin_bar_menu', 'bogo_admin_bar_init', 0, 1 );
5 +add_action( 'admin_bar_menu', 'bogo_admin_bar_menu' );
6 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 7 function bogo_admin_bar_menu( $wp_admin_bar ) {
24 8 $current_locale = bogo_get_user_locale();
9 + $current_language = bogo_get_language( $current_locale );
25 10
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 {
11 + if ( ! $current_language ) {
34 12 $current_language = $current_locale;
35 13 }
36 14
37 15 $wp_admin_bar->add_menu( array(
@@ -38,21 +16,21 @@
38 16 'parent' => 'top-secondary',
39 17 'id' => 'bogo-user-locale',
40 18 'title' => sprintf(
41 19 '<span class="ab-icon"></span><span class="ab-label">%s</span>',
42 - esc_html( $current_language )
43 - ),
44 - ) );
20 + esc_html( $current_language ) ) ) );
45 21
22 + $available_languages = bogo_available_languages( array(
23 + 'exclude' => array( $current_locale ),
24 + 'exclude_enus_if_inactive' => true,
25 + 'current_user_can_access' => true ) );
26 +
46 27 foreach ( $available_languages as $locale => $lang ) {
28 + $url = admin_url( 'profile.php?action=bogo-switch-locale&locale=' . $locale );
29 +
47 30 $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 - );
31 + array( 'redirect_to' => urlencode( $_SERVER['REQUEST_URI'] ) ),
32 + $url );
55 33
56 34 $url = wp_nonce_url( $url, 'bogo-switch-locale' );
57 35
58 36 $wp_admin_bar->add_menu( array(
@@ -58,32 +36,26 @@
58 36 $wp_admin_bar->add_menu( array(
59 37 'parent' => 'bogo-user-locale',
60 38 'id' => 'bogo-user-locale-' . $locale,
61 39 'title' => $lang,
62 - 'href' => $url,
63 - ) );
40 + 'href' => $url ) );
64 41 }
65 42 }
66 43
44 +add_action( 'admin_init', 'bogo_switch_user_locale' );
67 45
68 -add_action( 'admin_init', 'bogo_switch_user_locale', 10, 0 );
69 -
70 46 function bogo_switch_user_locale() {
71 - if (
72 - empty( $_REQUEST['action'] ) or
73 - 'bogo-switch-locale' !== $_REQUEST['action']
74 - ) {
47 + if ( empty( $_REQUEST['action'] )
48 + || 'bogo-switch-locale' != $_REQUEST['action'] ) {
75 49 return;
76 50 }
77 51
78 52 check_admin_referer( 'bogo-switch-locale' );
79 53
80 - $locale = $_REQUEST['locale'] ?? '';
54 + $locale = isset( $_REQUEST['locale'] ) ? $_REQUEST['locale'] : '';
81 55
82 - if (
83 - ! bogo_is_available_locale( $locale ) or
84 - $locale === bogo_get_user_locale()
85 - ) {
56 + if ( ! bogo_is_available_locale( $locale )
57 + || $locale == bogo_get_user_locale() ) {
86 58 return;
87 59 }
88 60
89 61 update_user_option( get_current_user_id(), 'locale', $locale, true );
@@ -93,22 +65,18 @@
93 65 exit();
94 66 }
95 67 }
96 68
69 +function bogo_get_user_locale( $user_id = 0 ) {
70 + global $current_user;
97 71
98 -function bogo_get_user_locale( $user_id = 0 ) {
99 72 $default_locale = bogo_get_default_locale();
73 + $user_id = absint( $user_id );
100 74
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() ) {
75 + if ( ! $user_id ) {
76 + if ( function_exists( 'wp_get_current_user' ) && ! empty( $current_user ) ) {
77 + $user_id = get_current_user_id();
78 + } elseif ( ! $user_id = apply_filters( 'determine_current_user', false ) ) {
111 79 return $default_locale;
112 80 }
113 81 }
114 82
@@ -113,68 +81,40 @@
113 81 }
114 82
115 83 $locale = get_user_option( 'locale', $user_id );
116 84
117 - if ( bogo_is_available_locale( $locale ) ) {
85 + if ( bogo_is_available_locale( $locale )
86 + && ( 'en_US' != $locale || ! bogo_get_prop( 'enus_deactivated' ) )
87 + && user_can( $user_id, 'bogo_access_locale', $locale ) ) {
118 88 return $locale;
119 89 }
120 90
91 + if ( user_can( $user_id, 'bogo_access_locale', $default_locale ) ) {
92 + return $default_locale;
93 + }
94 +
95 + $available_locales = bogo_available_locales( array(
96 + 'exclude_enus_if_inactive' => true ) );
97 +
98 + foreach ( $available_locales as $locale ) {
99 + if ( user_can( $user_id, 'bogo_access_locale', $locale ) ) {
100 + return $locale;
101 + }
102 + }
103 +
121 104 return $default_locale;
122 105 }
123 106
124 -
125 107 function bogo_get_user_accessible_locales( $user_id ) {
126 108 global $wpdb;
127 109
128 110 $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 111 $meta_key = $wpdb->get_blog_prefix() . 'accessible_locale';
137 112
138 - $locales = (array) get_user_meta( $user_id, $meta_key );
113 + $locales = get_user_meta( $user_id, $meta_key );
139 114
140 - if ( bogo_is_enus_deactivated() ) {
115 + if ( bogo_get_prop( 'enus_deactivated' ) ) {
141 116 $locales = array_diff( $locales, array( 'en_US' ) );
142 117 }
143 118
144 - $locales = bogo_filter_locales( $locales );
145 -
146 - if ( empty( $locales ) ) {
147 - $locales = array( bogo_get_default_locale() );
148 - }
149 -
150 119 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 120 }