Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsOptions::check_for_blog_slug()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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