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 ( |
285
|
|
|
isset( $this->sort_by_description ) ? |
286
|
|
|
'description' : |
287
|
|
|
'language' |
288
|
|
|
); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get url |
293
|
|
|
* |
294
|
|
|
* @param string $dir |
295
|
|
|
* |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
|
|
public function get_url( $dir ) { |
299
|
|
|
return esc_url( plugins_url( $dir, MSLS_PLUGIN__FILE__ ) ); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns slug for a post type |
304
|
|
|
* @param string $post_type |
305
|
|
|
* |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
public function get_slug( $post_type ) { |
309
|
|
|
$key = "rewrite_{$post_type}"; |
310
|
|
|
|
311
|
|
|
error_log( $key ); |
312
|
|
|
|
313
|
|
|
return isset( $this->$key ) ? $this->$key : ''; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param string $language |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public function get_icon( $language ) { |
322
|
|
|
return ( new IconPng() )->get( $language ); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Get flag url |
327
|
|
|
* |
328
|
|
|
* @param string $language |
329
|
|
|
* |
330
|
|
|
* @return string |
331
|
|
|
*/ |
332
|
|
|
public function get_flag_url( $language ) { |
333
|
|
|
if ( ! is_admin() && isset( $this->image_url ) ) { |
334
|
|
|
$url = $this->__get( 'image_url' ); |
335
|
|
|
} else { |
336
|
|
|
$url = $this->get_url( 'flags' ); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Override the path to the flag-icons |
341
|
|
|
* @since 0.9.9 |
342
|
|
|
* |
343
|
|
|
* @param string $url |
344
|
|
|
*/ |
345
|
|
|
$url = (string) apply_filters( 'msls_options_get_flag_url', $url ); |
346
|
|
|
|
347
|
|
|
$icon = $this->get_icon( $language ); |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Use your own filename for the flag-icon |
351
|
|
|
* @since 1.0.3 |
352
|
|
|
* |
353
|
|
|
* @param string $icon |
354
|
|
|
* @param string $language |
355
|
|
|
*/ |
356
|
|
|
$icon = (string) apply_filters( 'msls_options_get_flag_icon', $icon, $language ); |
357
|
|
|
|
358
|
|
|
return sprintf( '%s/%s', $url, $icon ); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Get all available languages |
363
|
|
|
* |
364
|
|
|
* @uses get_available_languages |
365
|
|
|
* @uses format_code_lang |
366
|
|
|
* @return array |
367
|
|
|
*/ |
368
|
|
|
public function get_available_languages() { |
369
|
|
|
if ( empty( $this->available_languages ) ) { |
370
|
|
|
$this->available_languages = [ |
371
|
|
|
'en_US' => __( 'American English', 'multisite-language-switcher' ), |
372
|
|
|
]; |
373
|
|
|
|
374
|
|
|
foreach ( get_available_languages() as $code ) { |
375
|
|
|
$this->available_languages[ esc_attr( $code ) ] = format_code_lang( $code ); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Returns custom filtered available languages |
380
|
|
|
* @since 1.0 |
381
|
|
|
* |
382
|
|
|
* @param array $available_languages |
383
|
|
|
*/ |
384
|
|
|
$this->available_languages = (array) apply_filters( |
385
|
|
|
'msls_options_get_available_languages', |
386
|
|
|
$this->available_languages |
387
|
|
|
); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return $this->available_languages; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* The 'blog'-slug-problem :/ |
395
|
|
|
* |
396
|
|
|
* @param string $url |
397
|
|
|
* @param MslsOptions $options |
398
|
|
|
* |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
public static function check_for_blog_slug( $url, $options ) { |
402
|
|
|
if ( empty( $url ) || ! is_string( $url ) ) { |
403
|
|
|
return ''; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
global $wp_rewrite; |
407
|
|
|
if ( ! is_subdomain_install() || ! $wp_rewrite->using_permalinks() ) { |
408
|
|
|
return $url; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
$count = 1; |
412
|
|
|
$url = str_replace( home_url(), '', $url, $count ); |
413
|
|
|
|
414
|
|
|
global $current_site; |
415
|
|
|
$permalink_structure = get_blog_option( $current_site->blog_id, 'permalink_structure' ); |
416
|
|
|
if ( $permalink_structure ) { |
417
|
|
|
list( $needle, ) = explode( '/%', $permalink_structure, 2 ); |
418
|
|
|
|
419
|
|
|
$url = str_replace( $needle, '', $url ); |
420
|
|
|
if ( is_main_site() && $options->with_front ) { |
421
|
|
|
$url = "{$needle}{$url}"; |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return home_url( $url ); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
} |
429
|
|
|
|