Completed
Push — master ( 386570...f59c88 )
by Dennis
02:15
created

MslsOptions::get_available_languages()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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