forked from lloc/Multisite-Language-Switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMslsOptions.php
More file actions
444 lines (384 loc) · 8.92 KB
/
Copy pathMslsOptions.php
File metadata and controls
444 lines (384 loc) · 8.92 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php declare( strict_types=1 );
namespace lloc\Msls;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use lloc\Msls\Component\Icon\IconPng;
/**
* General options class
*
* @package Msls
* @property bool $activate_autocomplete
* @property bool $activate_content_import
* @property bool $activate_quick_create
* @property bool $output_current_blog
* @property bool $only_with_translation
* @property int $content_priority
* @property int $display
* @property int $reference_user
* @property string $admin_display
* @property string $admin_language
* @property string $after_item
* @property string $after_output
* @property string $before_item
* @property string $before_output
* @property string $content_filter
* @property string $description
* @property string $exclude_current_blog
* @property string $sort_by_description
*/
class MslsOptions extends MslsGetSet implements OptionsInterface {
const MSLS_GET_POSTLINK_HOOK = 'msls_get_postlink';
public const PREFIX = 'msls';
public const SEPARATOR = '';
/**
* The name of the option in the database
*
* @var string
*/
protected string $name;
/**
* Indicates whether the option exists in the database
*
* @var bool
*/
protected bool $exists = false;
/**
* Autoload option
*
* @var bool
*/
protected bool $autoload = true;
/**
* @var array<int, mixed>
*/
protected array $args;
/**
* @var array<string, string>
*/
private array $available_languages;
/**
* Rewrite with front
*
* @var bool
*/
public ?bool $with_front = null;
/**
* Factory method
*
* @codeCoverageIgnore
*
* @param int $id
*
* @return OptionsInterface
*/
public static function create( $id = 0 ) {
if ( is_admin() ) {
$id = (int) $id;
if ( msls_content_types()->is_taxonomy() ) {
return MslsOptionsTax::create( $id );
}
return new MslsOptionsPost( $id );
}
if ( self::is_main_page() ) {
$options = new MslsOptions();
} elseif ( self::is_tax_page() ) {
$options = MslsOptionsTax::create();
} elseif ( self::is_query_page() ) {
$options = MslsOptionsQuery::create();
} else {
$options = new MslsOptionsPost( get_queried_object_id() );
}
add_filter( self::MSLS_GET_POSTLINK_HOOK, array( $options, 'check_for_blog_slug' ), 10, 2 );
return $options;
}
/**
* Determines if the current page is the main page (front page, search, 404).
*
* @return boolean
*/
public static function is_main_page() {
return is_front_page() || is_search() || is_404();
}
/**
* Determines if the current page is a category, tag or taxonomy page.
*
* @return boolean
*/
public static function is_tax_page() {
return is_category() || is_tag() || is_tax();
}
/**
* Determines if the current page is an archive page for a date, author, or any other post type.
*
* @return boolean
*/
public static function is_query_page() {
return is_date() || is_author() || is_post_type_archive();
}
/**
* Constructor
*/
public function __construct() {
$this->args = func_get_args();
$this->name = $this->get_option_name();
$this->exists = $this->set( get_option( $this->name ) );
}
public function get_option_name(): string {
return self::PREFIX . static::SEPARATOR . implode( static::SEPARATOR, $this->args );
}
/**
* Gets an element of arg by index
*
* The returned value will either be cast to the type of `$default` or, if nothing is set at this index, it will be the value of `$default`.
*
* @param int $index
* @param mixed $preset
*
* @return mixed
*/
public function get_arg( int $index, $preset = null ) {
$arg = $this->args[ $index ] ?? $preset;
settype( $arg, gettype( $preset ) );
return $arg;
}
/**
* @param mixed $arr
*/
public function save( $arr ): void {
$this->delete();
if ( $this->set( $arr ) ) {
$arr = $this->get_arr();
if ( ! empty( $arr ) ) {
add_option( $this->name, $arr, '', $this->autoload );
}
}
}
public function delete(): void {
$this->reset();
if ( $this->exists ) {
delete_option( $this->name );
}
}
/**
* @param mixed $arr
*
* @return bool
*/
public function set( $arr ): bool {
if ( ! is_array( $arr ) ) {
return false;
}
/**
* Mapping for us language code
*/
$map = array(
'us' => 'en_US',
'en' => 'en_US',
);
foreach ( $map as $old => $new ) {
if ( isset( $arr[ $old ] ) ) {
$arr[ $new ] = $arr[ $old ];
}
}
foreach ( $arr as $key => $value ) {
$this->__set( $key, $value );
}
return true;
}
public function get_permalink( string $language ): string {
/**
* Filters the url by language
*
* @param string $postlink
* @param string $language
*
* @since 0.9.8
*/
$postlink = (string) apply_filters(
'msls_options_get_permalink',
$this->get_postlink( $language ),
$language
);
return '' !== $postlink ? $postlink : home_url( '/' );
}
/**
* Get postlink
*
* @param string $language
*
* @return string
*/
public function get_postlink( $language ) {
return '';
}
/**
* Get the queried taxonomy
*
* @return string
*/
public function get_tax_query() {
return '';
}
/**
* Get current link
*/
public function get_current_link(): string {
return home_url( '/' );
}
/**
* Is excluded
*
* @return bool
*/
public function is_excluded() {
return isset( $this->exclude_current_blog );
}
/**
* Is content
*
* @return bool
*/
public function is_content_filter(): bool {
return isset( $this->content_filter );
}
/**
* Get order
*
* @return string
*/
public function get_order() {
return isset( $this->sort_by_description ) ? 'description' : 'language';
}
/**
* Get url
*
* @param string $dir
*
* @return string
*/
public function get_url( $dir ) {
return esc_url( MslsPlugin::plugins_url( $dir ) );
}
/**
* Returns slug for a post type
*
* @todo This method is not used anywhere in the codebase. Should it be removed?
*
* @param string $post_type
*
* @return string
*/
public function get_slug( string $post_type ): string {
$key = "rewrite_{$post_type}";
return $this->$key ?? '';
}
/**
* @param string $language
*
* @return string
*/
public function get_icon( $language ) {
return ( new IconPng() )->get( $language );
}
/**
* Get flag url
*
* @param string $language
*
* @return string
*/
public function get_flag_url( $language ) {
$url = ! is_admin() && isset( $this->image_url ) ? $this->__get( 'image_url' ) : $this->get_url( 'assets/flags' );
/**
* Override the path to the flag-icons
*
* @param string $url
*
* @since 0.9.9
*/
$url = (string) apply_filters( 'msls_options_get_flag_url', $url );
$icon = $this->get_icon( $language );
/**
* Use your own filename for the flag-icon
*
* @param string $icon
* @param string $language
*
* @since 1.0.3
*/
$icon = (string) apply_filters( 'msls_options_get_flag_icon', $icon, $language );
return sprintf( '%s/%s', $url, $icon );
}
/**
* Get all available languages
*
* @return array<string, string>
*/
public function get_available_languages(): array {
if ( empty( $this->available_languages ) ) {
$this->available_languages = array(
'en_US' => __( 'American English', 'multisite-language-switcher' ),
);
foreach ( get_available_languages() as $code ) {
$this->available_languages[ esc_attr( $code ) ] = format_code_lang( $code );
}
/**
* Returns custom filtered available languages
*
* @param array $available_languages
*
* @since 1.0
*/
$this->available_languages = (array) apply_filters(
'msls_options_get_available_languages',
$this->available_languages
);
}
return $this->available_languages;
}
/**
* The 'blog'-slug-problem :/
*
* @param mixed $url
* @param MslsOptions $options
*
* @return string
*/
public static function check_for_blog_slug( $url, $options ) {
if ( empty( $url ) || ! is_string( $url ) ) {
return '';
}
global $wp_rewrite;
if ( ! is_subdomain_install() || ! $wp_rewrite->using_permalinks() ) {
return $url;
}
$count = 1;
$url = str_replace( home_url(), '', $url, $count );
global $current_site;
$permalink_structure = get_blog_option( $current_site->blog_id, 'permalink_structure' );
if ( $permalink_structure ) {
list( $needle, ) = explode( '/%', $permalink_structure, 2 );
$stripped = false;
if ( '' !== $needle && str_starts_with( $url, $needle ) ) {
$rest = substr( $url, strlen( $needle ) );
if ( '' === $rest || '/' === $rest[0] ) {
$url = '' === $rest ? '/' : $rest;
$stripped = true;
}
}
if ( is_main_site() && $options->with_front && $stripped ) {
$url = "{$needle}{$url}";
}
}
return home_url( $url );
}
/**
* Get the icon type by the settings saved in admin_display
*
* @return string
*/
public function get_icon_type(): string {
return MslsAdminIcon::TYPE_LABEL === $this->admin_display ? MslsAdminIcon::TYPE_LABEL : MslsAdminIcon::TYPE_FLAG;
}
}