Completed
Push — master ( ebc6d0...69cc4e )
by Dennis
02:37
created

MslsOptions::get_permalink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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