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