|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* MslsOptions |
|
4
|
|
|
* @author Dennis Ploetner <[email protected]> |
|
5
|
|
|
* @since 0.9.8 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace lloc\Msls; |
|
9
|
|
|
|
|
10
|
|
|
use lloc\Msls\Component\Icon\IconPng; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* General options class |
|
14
|
|
|
* @package Msls |
|
15
|
|
|
* @property bool $activate_autocomplete |
|
16
|
|
|
* @property int $display |
|
17
|
|
|
* @property int $reference_user |
|
18
|
|
|
* @property int $content_priority |
|
19
|
|
|
* @property string $admin_language |
|
20
|
|
|
* @property string $description |
|
21
|
|
|
* @property string $before_item |
|
22
|
|
|
* @property string $after_item |
|
23
|
|
|
* @property string $before_output |
|
24
|
|
|
* @property string $after_output |
|
25
|
|
|
*/ |
|
26
|
|
|
class MslsOptions extends MslsGetSet { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Args |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $args; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Name |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $name; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Exists |
|
42
|
|
|
* @var bool |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $exists = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Separator |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $sep = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Autoload |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $autoload = 'yes'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Available languages |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
private $available_languages; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Rewrite with front |
|
66
|
|
|
* @var bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public $with_front; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Factory method |
|
72
|
|
|
* |
|
73
|
|
|
* @codeCoverageIgnore |
|
74
|
|
|
* |
|
75
|
|
|
* @param int $id |
|
76
|
|
|
* |
|
77
|
|
|
* @return MslsOptions |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function create( $id = 0 ) { |
|
80
|
|
|
if ( is_admin() ) { |
|
81
|
|
|
$id = (int) $id; |
|
82
|
|
|
|
|
83
|
|
|
if ( MslsContentTypes::create()->is_taxonomy() ) { |
|
84
|
|
|
return MslsOptionsTax::create( $id ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return new MslsOptionsPost( $id ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ( self::is_main_page() ) { |
|
91
|
|
|
$options = new MslsOptions(); |
|
92
|
|
|
} elseif ( self::is_tax_page() ) { |
|
93
|
|
|
$options = MslsOptionsTax::create(); |
|
94
|
|
|
} elseif ( self::is_query_page() ) { |
|
95
|
|
|
$options = MslsOptionsQuery::create(); |
|
96
|
|
|
} else { |
|
97
|
|
|
$options = new MslsOptionsPost( get_queried_object_id() ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
add_filter( 'check_url', [ $options, 'check_for_blog_slug' ], 10, 2 ); |
|
101
|
|
|
|
|
102
|
|
|
return $options; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Checks if the current page is a home, front or 404 page |
|
107
|
|
|
* @return boolean |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function is_main_page() { |
|
110
|
|
|
return is_front_page() || is_search() || is_404(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Checks if the current page is a category, tag or any other tax archive |
|
115
|
|
|
* @return boolean |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function is_tax_page() { |
|
118
|
|
|
return is_category() || is_tag() || is_tax(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Checks if the current page is a date, author any other post_type archive |
|
123
|
|
|
* @return boolean |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function is_query_page() { |
|
126
|
|
|
return is_date() || is_author() || is_post_type_archive(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Constructor |
|
131
|
|
|
*/ |
|
132
|
|
|
public function __construct() { |
|
133
|
|
|
$this->args = func_get_args(); |
|
134
|
|
|
$this->name = 'msls' . $this->sep . implode( $this->sep, $this->args ); |
|
135
|
|
|
$this->exists = $this->set( get_option( $this->name ) ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Gets an element of arg by index |
|
140
|
|
|
* The returning value is casted to the type of $retval or will be the |
|
141
|
|
|
* value of $retval if nothing is set at this index. |
|
142
|
|
|
* |
|
143
|
|
|
* @param int $idx |
|
144
|
|
|
* @param mixed $val |
|
145
|
|
|
* |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
*/ |
|
148
|
|
|
public function get_arg( $idx, $val = null ) { |
|
149
|
|
|
$arg = isset( $this->args[ $idx ] ) ? $this->args[ $idx ] : $val; |
|
150
|
|
|
settype( $arg, gettype( $val ) ); |
|
151
|
|
|
|
|
152
|
|
|
return $arg; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Save |
|
157
|
|
|
* |
|
158
|
|
|
* @param mixed $arr |
|
159
|
|
|
* |
|
160
|
|
|
* @codeCoverageIgnore |
|
161
|
|
|
*/ |
|
162
|
|
|
public function save( $arr ) { |
|
163
|
|
|
$this->delete(); |
|
164
|
|
|
if ( $this->set( $arr ) ) { |
|
165
|
|
|
$arr = $this->get_arr(); |
|
166
|
|
|
if ( ! empty( $arr ) ) { |
|
167
|
|
|
add_option( $this->name, $arr, '', $this->autoload ); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Delete |
|
174
|
|
|
* @codeCoverageIgnore |
|
175
|
|
|
*/ |
|
176
|
|
|
public function delete() { |
|
177
|
|
|
$this->reset(); |
|
178
|
|
|
if ( $this->exists ) { |
|
179
|
|
|
delete_option( $this->name ); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Set |
|
185
|
|
|
* |
|
186
|
|
|
* @param mixed $arr |
|
187
|
|
|
* |
|
188
|
|
|
* @return bool |
|
189
|
|
|
*/ |
|
190
|
|
|
public function set( $arr ) { |
|
191
|
|
|
if ( ! is_array( $arr ) ) { |
|
192
|
|
|
return false; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Mapping for us language code |
|
197
|
|
|
*/ |
|
198
|
|
|
$map = [ 'us' => 'en_US', 'en' => 'en_US' ]; |
|
199
|
|
|
foreach ( $map as $old => $new ) { |
|
200
|
|
|
if ( isset( $arr[ $old ] ) ) { |
|
201
|
|
|
$arr[ $new ] = $arr[ $old ]; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
foreach ( $arr as $key => $value ) { |
|
206
|
|
|
$this->__set( $key, $value ); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return true; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Get permalink |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $language |
|
216
|
|
|
* |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
|
|
public function get_permalink( $language ) { |
|
220
|
|
|
/** |
|
221
|
|
|
* Filters the url by language |
|
222
|
|
|
* @since 0.9.8 |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $postlink |
|
225
|
|
|
* @param string $language |
|
226
|
|
|
*/ |
|
227
|
|
|
$postlink = (string) apply_filters( |
|
228
|
|
|
'msls_options_get_permalink', |
|
229
|
|
|
$this->get_postlink( $language ), |
|
230
|
|
|
$language |
|
231
|
|
|
); |
|
232
|
|
|
|
|
233
|
|
|
return '' != $postlink ? $postlink : home_url( '/' ); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get postlink |
|
238
|
|
|
* |
|
239
|
|
|
* @param string $language |
|
240
|
|
|
* |
|
241
|
|
|
* @return string |
|
242
|
|
|
*/ |
|
243
|
|
|
public function get_postlink( $language ) { |
|
244
|
|
|
return ''; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get the queried taxonomy |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
|
|
public function get_tax_query() { |
|
252
|
|
|
return ''; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Get current link |
|
257
|
|
|
* @return string |
|
258
|
|
|
*/ |
|
259
|
|
|
public function get_current_link() { |
|
260
|
|
|
return home_url( '/' ); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Is excluded |
|
265
|
|
|
* @return bool |
|
266
|
|
|
*/ |
|
267
|
|
|
public function is_excluded() { |
|
268
|
|
|
return isset( $this->exclude_current_blog ); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Is content |
|
273
|
|
|
* @return bool |
|
274
|
|
|
*/ |
|
275
|
|
|
public function is_content_filter() { |
|
276
|
|
|
return isset( $this->content_filter ); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Get order |
|
281
|
|
|
* @return string |
|
282
|
|
|
*/ |
|
283
|
|
|
public function get_order() { |
|
284
|
|
|
return isset( $this->sort_by_description ) ? 'description' : 'language'; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Get url |
|
289
|
|
|
* |
|
290
|
|
|
* @param string $dir |
|
291
|
|
|
* |
|
292
|
|
|
* @return string |
|
293
|
|
|
*/ |
|
294
|
|
|
public function get_url( $dir ) { |
|
295
|
|
|
return esc_url( MslsPlugin::plugins_url( $dir ) ); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Returns slug for a post type |
|
300
|
|
|
* @param string $post_type |
|
301
|
|
|
* |
|
302
|
|
|
* @return string |
|
303
|
|
|
*/ |
|
304
|
|
|
public function get_slug( $post_type ) { |
|
305
|
|
|
$key = "rewrite_{$post_type}"; |
|
306
|
|
|
|
|
307
|
|
|
error_log( $key ); |
|
308
|
|
|
|
|
309
|
|
|
return isset( $this->$key ) ? $this->$key : ''; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @param string $language |
|
314
|
|
|
* |
|
315
|
|
|
* @return string |
|
316
|
|
|
*/ |
|
317
|
|
|
public function get_icon( $language ) { |
|
318
|
|
|
return ( new IconPng() )->get( $language ); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Get flag url |
|
323
|
|
|
* |
|
324
|
|
|
* @param string $language |
|
325
|
|
|
* |
|
326
|
|
|
* @return string |
|
327
|
|
|
*/ |
|
328
|
|
|
public function get_flag_url( $language ) { |
|
329
|
|
|
if ( ! is_admin() && isset( $this->image_url ) ) { |
|
330
|
|
|
$url = $this->__get( 'image_url' ); |
|
331
|
|
|
} else { |
|
332
|
|
|
$url = $this->get_url( 'flags' ); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Override the path to the flag-icons |
|
337
|
|
|
* @since 0.9.9 |
|
338
|
|
|
* |
|
339
|
|
|
* @param string $url |
|
340
|
|
|
*/ |
|
341
|
|
|
$url = (string) apply_filters( 'msls_options_get_flag_url', $url ); |
|
342
|
|
|
|
|
343
|
|
|
$icon = $this->get_icon( $language ); |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Use your own filename for the flag-icon |
|
347
|
|
|
* @since 1.0.3 |
|
348
|
|
|
* |
|
349
|
|
|
* @param string $icon |
|
350
|
|
|
* @param string $language |
|
351
|
|
|
*/ |
|
352
|
|
|
$icon = (string) apply_filters( 'msls_options_get_flag_icon', $icon, $language ); |
|
353
|
|
|
|
|
354
|
|
|
return sprintf( '%s/%s', $url, $icon ); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Get all available languages |
|
359
|
|
|
* |
|
360
|
|
|
* @uses get_available_languages |
|
361
|
|
|
* @uses format_code_lang |
|
362
|
|
|
* @return array |
|
363
|
|
|
*/ |
|
364
|
|
|
public function get_available_languages() { |
|
365
|
|
|
if ( empty( $this->available_languages ) ) { |
|
366
|
|
|
$this->available_languages = [ |
|
367
|
|
|
'en_US' => __( 'American English', 'multisite-language-switcher' ), |
|
368
|
|
|
]; |
|
369
|
|
|
|
|
370
|
|
|
foreach ( get_available_languages() as $code ) { |
|
371
|
|
|
$this->available_languages[ esc_attr( $code ) ] = format_code_lang( $code ); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Returns custom filtered available languages |
|
376
|
|
|
* @since 1.0 |
|
377
|
|
|
* |
|
378
|
|
|
* @param array $available_languages |
|
379
|
|
|
*/ |
|
380
|
|
|
$this->available_languages = (array) apply_filters( |
|
381
|
|
|
'msls_options_get_available_languages', |
|
382
|
|
|
$this->available_languages |
|
383
|
|
|
); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
return $this->available_languages; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* The 'blog'-slug-problem :/ |
|
391
|
|
|
* |
|
392
|
|
|
* @param string $url |
|
393
|
|
|
* @param MslsOptions $options |
|
394
|
|
|
* |
|
395
|
|
|
* @return string |
|
396
|
|
|
*/ |
|
397
|
|
|
public static function check_for_blog_slug( $url, $options ) { |
|
398
|
|
|
if ( empty( $url ) || ! is_string( $url ) ) { |
|
399
|
|
|
return ''; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
global $wp_rewrite; |
|
403
|
|
|
if ( ! is_subdomain_install() || ! $wp_rewrite->using_permalinks() ) { |
|
404
|
|
|
return $url; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
$count = 1; |
|
408
|
|
|
$url = str_replace( home_url(), '', $url, $count ); |
|
409
|
|
|
|
|
410
|
|
|
global $current_site; |
|
411
|
|
|
$permalink_structure = get_blog_option( $current_site->blog_id, 'permalink_structure' ); |
|
412
|
|
|
if ( $permalink_structure ) { |
|
413
|
|
|
list( $needle, ) = explode( '/%', $permalink_structure, 2 ); |
|
414
|
|
|
|
|
415
|
|
|
$url = str_replace( $needle, '', $url ); |
|
416
|
|
|
if ( is_main_site() && $options->with_front ) { |
|
417
|
|
|
$url = "{$needle}{$url}"; |
|
418
|
|
|
} |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
return home_url( $url ); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
} |
|
425
|
|
|
|