-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass-polylang.php
executable file
·315 lines (270 loc) · 6.93 KB
/
class-polylang.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* Class BPPL_Polylang
*
* @since 1.0.0
*
* This class managaes all needed Polylang functionality for translating BuddyPress
*/
if( ! defined( 'ABSPATH' ) ) {
exit;
}
class BPPL_Polylang {
/**
* All languages added by Polylang
*
* @since 1.0.0
*
* @var array
*/
protected $languages = array();
/**
* Language of user
*
* @since 1.0.0
*
* @var string|null
*/
protected $current_user_lang = null;
/**
* User data
*
* @var BPPL_User
*/
protected $user;
protected $switched;
/**
* Adding Actionhooks & Co.
*
* @since 1.0.0
*/
public function __construct() {
$this->user = new BPPL_User();
$this->init_languages();
$this->set_language_cookie();
// Todo: Maybe change this, because it changes the user language on opening sites of other languages
add_action( 'pll_language_defined', array( $this, 'define_language' ), 10, 2 );
add_filter( 'option_polylang', array( $this , 'option_polylang' ), 10, 1 );
add_filter( 'pll_model', array( $this, 'pll_model') );
add_action( 'pll_init', array( $this, 'pll_init') );
}
public function option_polylang() {
remove_filter( 'option_polylang', array( $this , 'option_polylang' ), 10, 1 );
$option = get_blog_option( bp_get_root_blog_id(), 'polylang' );
return $option;
}
public function pll_model( $model ) {
if( ! bp_is_root_blog() ) {
switch_to_blog( bp_get_root_blog_id() );
$this->switched = true;
}
return $model;
}
public function pll_init() {
if( $this->switched ) {
$this->init_languages();
restore_current_blog();
}
}
/**
* Init Polylang languages
*
* Initializing an array for the languages for later use.
*
* @since 1.0.0
*/
public function init_languages(){
global $wpdb;
/**
* We have to work with our own SQL statements, because Polylang loads everything
* at the plugins_loaded hook with priority 1. No chance to get in after taxonomies
* loaded and to do anything like setting cookie and anything else on the Polylang start.
*/
$languages = $wpdb->get_results( "SELECT * FROM {$wpdb->terms} AS t, {$wpdb->term_taxonomy} AS tt WHERE t.term_id = tt.term_id AND taxonomy = 'language'" );
// Stopping if no languages existing
if( null === $languages ) {
bppl_messages()->add( $languages->get_error_message() );
return;
}
foreach( $languages AS $language ) {
$description = maybe_unserialize( $language->description );
if( is_array( $description ) && array_key_exists( 'locale', $description )) {
$locale = $description[ 'locale' ];
$this->languages[ $language->slug ] = array(
'term_id' => $language->term_id,
'name' => $language->name,
'lang' => $language->slug,
'locale' => $locale,
);
}
}
}
/**
* Setting up language cookie of Polylang
*
* @since 1.0.0
*/
private function set_language_cookie() {
$locale = $this->user->get_locale();
if( empty( $locale ) ) {
return;
}
if( ! is_wp_error( $locale ) ) {
$lang = $this->get_lang_slug_by_locale( $locale );
} else {
$lang = $this->get_default_lang();
}
if( is_wp_error( $lang ) ) {
return $lang;
}
if( ! setcookie( 'pll_language', $lang ) ) {
bppl_messages()->add( __( 'Could not set language cookie for Polylang', 'buddypress-polylang' ) );
}
}
/**
* Getting global polylang options
*
* @since 1.0.0
*
* @param string $option_name Name of the Polylang option
*
* @return bool|string
*/
public function get_option( $option_name ) {
$options = maybe_unserialize( get_option( 'polylang' ) );
if( ! array_key_exists( $option_name, $options ) ) {
return false;
}
return $options[ $option_name ];
}
/**
* Getting all available languages
*
* @since 1.0.0
*
* @return array|WP_Error $languages All language information in an array
*/
public function get_languages() {
return $this->languages;
}
/**
* Flexible getting values from array
*
* @since 1.0.0
*
* @param string $key
* @param string $value
* @param string $return_key
*
* @return array|WP_Error
*/
public function get_value_by( $key, $value, $return_key ) {
$languages = $this->get_languages();
if( is_wp_error( $languages ) ) {
return $languages;
}
if( count( $languages ) === 0 ) {
return new WP_Error( 'languages_not_existing', __( 'There are no languages added in Polylang yet.', 'buddypress-polylang' ) );
}
foreach( $languages AS $language ) {
if( array_key_exists( $key, $language ) && $language[ $key ] === $value ) {
return $language[ $return_key ];
}
}
return new WP_Error( 'not_found', __( 'Value Not found.', 'buddypress-polylang' ) );
}
/**
* Getting a locale by language slug
*
* @since 1.0.0
*
* @param string $locale Lang slug
*
* @return string|WP_Error
*/
public function get_lang_slug_by_locale( $locale ) {
$lang = $this->get_value_by( 'locale', $locale, 'lang' );
return $lang;
}
/**
* Getting a language slug by locale
*
* @since 1.0.0
*
* @param string $locale Name of the locale
*
* @return string|WP_Error
*/
public function get_locale_by_lang_slug( $lang ) {
$locale = $this->get_value_by( 'lang', $lang, 'locale' );
return $locale;
}
/**
* Getting default language of Polylang
*
* @since 1.0.0
*
* @return string
*/
public function get_default_lang() {
$default_lang = $this->get_option( 'default_lang' );
/**
* Filter for default language
*
* @since 1.0.0
*
* @param string $default_language The requested field for the default language
*/
return apply_filters( 'bppl_default_language', $default_lang );
}
/**
* Getting user locale
*
* @since 1.0.0
*
* @param $user_id int User ID
*
* @return array|mixed|WP_Error
*/
public function get_user_locale( $user_id ) {
$locale = get_user_meta( $user_id, 'locale', true );
if( empty( $locale ) ) {
$lang = bppl()->polylang()->get_default_lang();
$locale = bppl()->polylang()->get_value_by('lang', $lang, 'locale');
}
return $locale;
}
/**
* Saving the locale for a user in WordPress user locale
*
* @since 1.0.0
*
* @param $user_id
* @param $locale
*
* @return int|WP_Error
*/
public function save_user_locale( $user_id, $locale ) {
return wp_update_user( array( 'ID' => $user_id, 'locale' => $locale ) );
}
/**
* Setting the user locale
*
* Saves the user locale to the user settings
*
* @since 1.0.0
*
* @param string $lang_slug Language slug
* @param PLL_Language $current_lang Language object
*
* @return bool|WP_Error $saved True if everything is saved fine or WP_Error on failure.
*/
public function define_language( $lang_slug, $current_lang ) {
$user = wp_get_current_user();
// We do not save if the user has not logged in or he is in wp-admin
if( 0 === $user->ID || is_admin() ) {
return;
}
$this->save_user_locale( $user->ID, $current_lang->locale );
}
}