Completed
Push — master ( d24e06...1436e2 )
by Dennis
02:14
created

MslsOptions::get_arg()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 9.4285
c 2
b 0
f 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(
211
			'msls_options_get_permalink',
212
			$this->get_postlink( $language ),
213
			$language
214
		);
215
216
		return ( '' != $postlink ? $postlink : home_url( '/' ) );
217
	}
218
219
	/**
220
	 * Get postlink
221
	 *
222
	 * @param string $language
223
	 *
224
	 * @return string
225
	 */
226
	public function get_postlink( $language ) {
227
		return '';
228
	}
229
230
	/**
231
	 * Get current link
232
	 * @return string
233
	 */
234
	public function get_current_link() {
235
		return home_url( '/' );
236
	}
237
238
	/**
239
	 * Is excluded
240
	 * @return bool
241
	 */
242
	public function is_excluded() {
243
		return isset( $this->exclude_current_blog );
244
	}
245
246
	/**
247
	 * Is content
248
	 * @return bool
249
	 */
250
	public function is_content_filter() {
251
		return isset( $this->content_filter );
252
	}
253
254
	/**
255
	 * Get order
256
	 * @return string
257
	 */
258
	public function get_order() {
259
		return (
260
		isset( $this->sort_by_description ) ?
261
			'description' :
262
			'language'
263
		);
264
	}
265
266
	/**
267
	 * Get url
268
	 *
269
	 * @param string $dir
270
	 *
271
	 * @return string
272
	 */
273
	public function get_url( $dir ) {
274
		return esc_url( plugins_url( $dir, MSLS_PLUGIN__FILE__ ) );
275
	}
276
277
	/**
278
	 * Returns slug for a post type
279
	 * @param string $post_type
280
	 *
281
	 * @return string
282
	 */
283
	public function get_slug( $post_type ) {
284
		$key = "rewrite_{$post_type}";
285
286
		error_log( $key );
287
		
288
		return isset( $this->$key ) ? $this->$key : '';
289
	}
290
291
	/**
292
	 * Get flag url
293
	 *
294
	 * @param string $language
295
	 *
296
	 * @return string
297
	 */
298
	public function get_flag_url( $language ) {
299
		if ( ! is_admin() && isset( $this->image_url ) ) {
300
			$url = $this->__get( 'image_url' );
301
		} else {
302
			$url = $this->get_url( 'flags' );
303
		}
304
305
		/**
306
		 * Override the path to the flag-icons
307
		 * @since 0.9.9
308
		 *
309
		 * @param string $url
310
		 */
311
		$url = (string) apply_filters( 'msls_options_get_flag_url', $url );
312
313
		if ( 5 == strlen( $language ) ) {
314
			$icon = strtolower( substr( $language, - 2 ) );
315
		} else {
316
			$icon = $language;
317
		}
318
		$icon .= '.png';
319
320
		/**
321
		 * Use your own filename for the flag-icon
322
		 * @since 1.0.3
323
		 *
324
		 * @param string $icon
325
		 * @param string $language
326
		 */
327
		$icon = (string) apply_filters( 'msls_options_get_flag_icon', $icon, $language );
328
329
		return sprintf( '%s/%s', $url, $icon );
330
	}
331
332
	/**
333
	 * Get all available languages
334
	 * @uses get_available_languages
335
	 * @uses format_code_lang
336
	 * @return array
337
	 */
338
	public function get_available_languages() {
339
		if ( empty( $this->available_languages ) ) {
340
			$this->available_languages = array(
341
				'en_US' => __( 'American English', 'multisite-language-switcher' ),
342
			);
343
			foreach ( get_available_languages() as $code ) {
344
				$this->available_languages[ esc_attr( $code ) ] = format_code_lang( $code );
345
			}
346
347
			/**
348
			 * Returns custom filtered available languages
349
			 * @since 1.0
350
			 *
351
			 * @param array $available_languages
352
			 */
353
			$this->available_languages = (array) apply_filters(
354
				'msls_options_get_available_languages',
355
				$this->available_languages
356
			);
357
		}
358
359
		return $this->available_languages;
360
	}
361
362
	/**
363
	 * The 'blog'-slug-problem :/
364
	 *
365
	 * @param string $url
366
	 * @param MslsOptions $options
367
	 *
368
	 * @return string
369
	 */
370
	public static function check_for_blog_slug( $url, $options ) {
371
		if ( empty( $url ) || ! is_string( $url ) ) {
372
			return '';
373
		}
374
375
		global $wp_rewrite;
376
		if ( ! is_subdomain_install() || ! $wp_rewrite->using_permalinks() ) {
377
			return $url;
378
		}
379
380
		$count = 1;
381
		$url   = str_replace( home_url(), '', $url, $count );
382
383
		global $current_site;
384
		$permalink_structure = get_blog_option( $current_site->blog_id, 'permalink_structure' );
385
		if ( $permalink_structure ) {
386
			list( $needle, ) = explode( '/%', $permalink_structure, 2 );
387
388
			$url = str_replace( $needle, '', $url );
389
			if ( is_main_site() && $options->with_front ) {
390
				$url = "{$needle}{$url}";
391
			}
392
		}
393
394
		return home_url( $url );
395
	}
396
397
	/**
398
	 * Get or create an instance of MslsOptions
399
	 * @todo Until PHP 5.2 is not longer the minimum for WordPress ...
400
	 * @return MslsOptions
401
	 */
402
	public static function instance() {
403
		if ( ! ( $obj = MslsRegistry::get_object( 'MslsOptions' ) ) ) {
404
			$obj = new self();
405
			MslsRegistry::set_object( 'MslsOptions', $obj );
406
		}
407
408
		return $obj;
409
	}
410
411
}
412