Completed
Push — master ( 551434...e0a666 )
by Dennis
03:35
created

MslsOptions::get_available_languages()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 10

Duplication

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