Completed
Push — master ( ee928d...1fc1bc )
by Dennis
02:34
created

MslsAdmin::exclude_current_blog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsAdmin
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Administration of the options
12
 * @package Msls
13
 */
14
class MslsAdmin extends MslsMain {
15
16
	/**
17
	 * Factory
18
	 *
19
	 * @codeCoverageIgnore
20
	 *
21
	 * @return MslsAdmin
22
	 */
23
	public static function init() {
24
		if ( ! ( $obj = MslsRegistry::get_object( __CLASS__ ) ) ) {
25
			$options    = MslsOptions::instance();
26
			$collection = MslsBlogCollection::instance();
27
28
			$obj = new static( $options, $collection );
29
30
			MslsRegistry::set_object( __CLASS__, $obj );
31
32
			/**
33
			 * Override the capabilities needed for the plugin's settings
34
			 *
35
			 * @since 2.0
36
			 *
37
			 * @param string $capability
38
			 */
39
			$caps = apply_filters( 'msls_admin_caps', 'manage_options' );
40
			if ( current_user_can( $caps ) ) {
41
				$title = __( 'Multisite Language Switcher', 'multisite-language-switcher' );
42
				add_options_page( $title, $title, 'manage_options', $obj->get_menu_slug(), [ $obj, 'render' ] );
43
44
				add_action( 'admin_init', [ $obj, 'register' ] );
45
				add_action( 'admin_notices', [ $obj, 'has_problems' ] );
46
47
				add_filter( 'msls_admin_validate', [ $obj, 'set_blog_language' ] );
48
			}
49
		}
50
51
		return $obj;
52
	}
53
54
	/**
55
	 * Let's do this simple
56
	 *
57
	 * @return string
58
	 */
59
	public function get_menu_slug() {
60
		return 'MslsAdmin';
61
	}
62
63
	/**
64
	 * Get's the link for the switcher-settings in the wp-admin
65
	 *
66
	 * @return string
67
	 */
68
	public function get_options_page_link() {
69
		return sprintf( '/options-general.php?page=%s', $this->get_menu_slug() );
70
	}
71
72
	/**
73
	 * You can use every method of the decorated object
74
	 *
75
	 * @param string $method
76
	 * @param mixed $args
77
	 *
78
	 * @return mixed
79
	 */
80
	public function __call( $method, $args ) {
81
		$parts = explode( '_', $method, 2 );
82
		if ( 2 == count( $parts ) ) {
83
			switch ( $parts[0] ) {
84
				case 'rewrite':
85
					return $this->render_rewrite( $parts[1] );
86
					break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
87
				case 'text':
88
					echo $this->render_input( $parts[1] );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
89
					break;
90
			}
91
		}
92
	}
93
94
	/**
95
	 * There is something wrong? Here comes the message...
96
	 * @return bool
97
	 */
98
	public function has_problems() {
99
		$message = '';
100
101
		if ( $this->options->is_empty() ) {
102
			$message = sprintf(
103
				__( 'Multisite Language Switcher is almost ready. You must complete the configuration process</a>.' ),
104
				esc_url( admin_url( $this->get_options_page_link() ) )
105
			);
106
		} elseif ( 1 == count( $this->options->get_available_languages() ) ) {
107
			$message = sprintf(
108
				__( 'There are no language files installed. You can <a href="%s">manually install some language files</a> or you could use a <a href="%s">plugin</a> to download these files automatically.' ),
109
				esc_url( 'http://codex.wordpress.org/Installing_WordPress_in_Your_Language#Manually_Installing_Language_Files' ),
110
				esc_url( 'http://wordpress.org/plugins/wp-native-dashboard/' )
111
			);
112
		}
113
114
		return MslsPlugin::message_handler( $message, 'updated fade' );
115
	}
116
117
	/**
118
	 * Render the options-page
119
	 * @codeCoverageIgnore
120
	 */
121
	public function render() {
122
		printf(
123
			'<div class="wrap"><div class="icon32" id="icon-options-general"><br/></div><h1>%s</h1>%s<br class="clear"/><form action="options.php" method="post"><p>%s</p>',
124
			__( 'Multisite Language Switcher Options', 'multisite-language-switcher' ),
125
			$this->subsubsub(),
126
			__( 'To achieve maximum flexibility, you have to configure each blog separately.', 'multisite-language-switcher' )
127
		);
128
129
		settings_fields( 'msls' );
130
		do_settings_sections( __CLASS__ );
131
132
		printf(
133
			'<p class="submit"><input name="Submit" type="submit" class="button-primary" value="%s" /></p></form></div>',
134
			( $this->options->is_empty() ? __( 'Configure', 'multisite-language-switcher' ) : __( 'Update', 'multisite-language-switcher' ) )
135
		);
136
	}
137
138
139
	/**
140
	 * Create a submenu which contains links to all blogs of the current user
141
	 * @return string
142
	 */
143
	public function subsubsub() {
144
		$arr = [];
145
146
		foreach ( $this->collection->get_plugin_active_blogs() as $blog ) {
147
			$arr[] = sprintf(
148
				'<a href="%s"%s>%s / %s</a>',
149
				get_admin_url( $blog->userblog_id, $this->get_options_page_link() ),
150
				( $blog->userblog_id == $this->collection->get_current_blog_id() ? ' class="current"' : '' ),
151
				$blog->blogname,
152
				$blog->get_description()
153
			);
154
		}
155
156
		return (
157
			empty( $arr ) ?
158
			'' :
159
			sprintf(
160
				'<ul class="subsubsub"><li>%s</li></ul>',
161
				implode( ' | </li><li>', $arr )
162
			)
163
		);
164
	}
165
166
	/**
167
	 * Register the form-elements
168
	 * @codeCoverageIgnore
169
	 */
170
	public function register() {
171
		register_setting( 'msls', 'msls', [ $this, 'validate' ] );
172
173
		add_settings_section( 'language_section', __( 'Language Settings', 'multisite-language-switcher' ), array(
174
			$this,
175
			'language_section'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
176
		), __CLASS__ );
177
		add_settings_section( 'main_section', __( 'Main Settings', 'multisite-language-switcher' ), array(
178
			$this,
179
			'main_section'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
180
		), __CLASS__ );
181
		add_settings_section( 'advanced_section', __( 'Advanced Settings', 'multisite-language-switcher' ), array(
182
			$this,
183
			'advanced_section'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
184
		), __CLASS__ );
185
186
		global $wp_rewrite;
187
		if ( $wp_rewrite->using_permalinks() ) {
188
			add_settings_section( 'rewrites_section', __( 'Rewrites Settings', 'multisite-language-switcher' ), array(
189
				$this,
190
				'rewrites_section'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
191
			), __CLASS__ );
192
		}
193
194
		/**
195
		 * Lets you add your own settings section
196
		 * @since 1.0
197
		 *
198
		 * @param string $page
199
		 */
200
		do_action( 'msls_admin_register', __CLASS__ );
201
	}
202
203
	/**
204
	 * Register the fields in the language_section
205
	 * @codeCoverageIgnore
206
	 */
207
	public function language_section() {
208
		add_settings_field( 'blog_language', __( 'Blog Language', 'multisite-language-switcher' ), array(
209
			$this,
210
			'blog_language'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
211
		), __CLASS__, 'language_section' );
212
213
		/**
214
		 * Lets you add your own field to the language section
215
		 * @since 1.0
216
		 *
217
		 * @param string $page
218
		 * @param string $section
219
		 */
220
		do_action( 'msls_admin_language_section', __CLASS__, 'language_section' );
221
	}
222
223
	/**
224
	 * Register the fields in the main_section
225
	 * @codeCoverageIgnore
226
	 */
227
	public function main_section() {
228
		add_settings_field( 'display', __( 'Display', 'multisite-language-switcher' ), array(
229
			$this,
230
			'display'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
231
		), __CLASS__, 'main_section' );
232
		add_settings_field( 'sort_by_description', __( 'Sort output by description', 'multisite-language-switcher' ), array(
233
			$this,
234
			'sort_by_description'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
235
		), __CLASS__, 'main_section' );
236
		add_settings_field( 'output_current_blog', __( 'Display link to the current language', 'multisite-language-switcher' ), array(
237
			$this,
238
			'output_current_blog'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
239
		), __CLASS__, 'main_section' );
240
		add_settings_field( 'only_with_translation', __( 'Show only links with a translation', 'multisite-language-switcher' ), array(
241
			$this,
242
			'only_with_translation'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
243
		), __CLASS__, 'main_section' );
244
		add_settings_field( 'description', __( 'Description', 'multisite-language-switcher' ), array(
245
			$this,
246
			'description'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
247
		), __CLASS__, 'main_section' );
248
		add_settings_field( 'before_output', __( 'Text/HTML before the list', 'multisite-language-switcher' ), array(
249
			$this,
250
			'text_before_output'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
251
		), __CLASS__, 'main_section' );
252
		add_settings_field( 'after_output', __( 'Text/HTML after the list', 'multisite-language-switcher' ), array(
253
			$this,
254
			'text_after_output'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
255
		), __CLASS__, 'main_section' );
256
		add_settings_field( 'before_item', __( 'Text/HTML before each item', 'multisite-language-switcher' ), array(
257
			$this,
258
			'text_before_item'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
259
		), __CLASS__, 'main_section' );
260
		add_settings_field( 'after_item', __( 'Text/HTML after each item', 'multisite-language-switcher' ), array(
261
			$this,
262
			'text_after_item'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
263
		), __CLASS__, 'main_section' );
264
		add_settings_field( 'content_filter', __( 'Add hint for available translations', 'multisite-language-switcher' ), array(
265
			$this,
266
			'content_filter'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
267
		), __CLASS__, 'main_section' );
268
		add_settings_field( 'content_priority', __( 'Hint priority', 'multisite-language-switcher' ), array(
269
			$this,
270
			'content_priority'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
271
		), __CLASS__, 'main_section' );
272
273
		/**
274
		 * Lets you add your own field to the main section
275
		 * @since 1.0
276
		 *
277
		 * @param string $page
278
		 * @param string $section
279
		 */
280
		do_action( 'msls_admin_main_section', __CLASS__, 'main_section' );
281
	}
282
283
	/**
284
	 * Register the fields in the advanced_section
285
	 * @codeCoverageIgnore
286
	 */
287
	public function advanced_section() {
288
		add_settings_field( 'activate_autocomplete', __( 'Activate experimental autocomplete inputs', 'multisite-language-switcher' ), array(
289
			$this,
290
			'activate_autocomplete'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
291
		), __CLASS__, 'advanced_section' );
292
		add_settings_field( 'image_url', __( 'Custom URL for flag-images', 'multisite-language-switcher' ), array(
293
			$this,
294
			'text_image_url'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
295
		), __CLASS__, 'advanced_section' );
296
		add_settings_field( 'reference_user', __( 'Reference user', 'multisite-language-switcher' ), array(
297
			$this,
298
			'reference_user'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
299
		), __CLASS__, 'advanced_section' );
300
		add_settings_field( 'exclude_current_blog', __( 'Exclude this blog from output', 'multisite-language-switcher' ), array(
301
			$this,
302
			'exclude_current_blog'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
303
		), __CLASS__, 'advanced_section' );
304
		add_settings_field( 'activate_content_import', __( 'Activate the content import functionality', 'multisite-language-switcher' ), array(
305
			$this,
306
			'activate_content_import'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
307
		), __CLASS__, 'advanced_section' );
308
309
		/**
310
		 * Lets you add your own field to the advanced section
311
		 * @since 1.0
312
		 *
313
		 * @param string $page
314
		 * @param string $section
315
		 */
316
		do_action( 'msls_admin_advanced_section', __CLASS__, 'advanced_section' );
317
	}
318
319
	/**
320
	 * Register the fields in the rewrites_section
321
	 * @since 1.1
322
	 * @codeCoverageIgnore
323
	 */
324
	public function rewrites_section() {
325
		foreach ( get_post_types( [ 'public' => true ], 'objects' ) as $key => $object ) {
326
			$title = sprintf( __( '%s Slug', 'multisite-language-switcher' ), $object->label );
327
			add_settings_field( "rewrite_{$key}", $title, [ $this, "rewrite_{$key}" ], __CLASS__, 'rewrites_section' );
328
		}
329
330
		/**
331
		 * Lets you add your own field to the rewrites section
332
		 *
333
		 * @param string $page
334
		 * @param string $section
335
		 */
336
		do_action( 'msls_admin_rewrites_section', __CLASS__, 'rewrites_section' );
337
	}
338
339
	/**
340
	 * Shows the select-form-field 'blog_language'
341
	 */
342
	public function blog_language() {
343
		echo $this->render_select(
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
344
			'blog_language',
345
			$this->options->get_available_languages(),
346
			get_option( 'WPLANG', 'en_US' )
347
		);
348
	}
349
350
	/**
351
	 * Shows the select-form-field 'display'
352
	 */
353
	public function display() {
354
		echo $this->render_select(
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
355
			'display',
356
			MslsLink::get_types_description(),
357
			$this->options->display
358
		);
359
	}
360
361
	/**
362
	 * Shows the select-form-field 'reference_user'
363
	 */
364
	public function reference_user() {
365
		$users = array();
366
367
		foreach ( $this->collection->get_users() as $user ) {
368
			$users[ $user->ID ] = $user->user_nicename;
369
		}
370
371
		echo $this->render_select( 'reference_user', $users, $this->options->reference_user );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
372
	}
373
374
	/**
375
	 * render
376
	 *
377
	 * You can decide if you want to activate the experimental autocomplete
378
	 * input fields in the backend instead of the traditional select-menus.
379
	 */
380
	public function activate_autocomplete() {
381
		echo $this->render_checkbox( 'activate_autocomplete' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
382
	}
383
384
	/**
385
	 * render
386
	 *
387
	 * You can decide if you want to activate the content import functionality
388
	 * in the backend instead of the traditional select-menus.
389
	 */
390
	public function activate_content_import() {
391
		echo $this->render_checkbox( 'activate_content_import' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
392
	}
393
394
	/**
395
	 * Show sort_by_description-field
396
	 *
397
	 * You can decide that the ouput will be sorted by the description. If not
398
	 * the output will be sorted by the language-code.
399
	 */
400
	public function sort_by_description() {
401
		echo $this->render_checkbox( 'sort_by_description' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
402
	}
403
404
	/**
405
	 * Exclude the current blog
406
	 *
407
	 * You can exclude a blog explicitly. All your settings will be safe but the
408
	 * plugin will ignore this blog while this option is active.
409
	 */
410
	public function exclude_current_blog() {
411
		echo $this->render_checkbox( 'exclude_current_blog' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
412
	}
413
414
	/**
415
	 * Show only a link  if a translation is available
416
	 *
417
	 * Some user requested this feature. Shows only links to available
418
	 * translations.
419
	 */
420
	public function only_with_translation() {
421
		echo $this->render_checkbox( 'only_with_translation' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
422
	}
423
424
	/**
425
	 * Show a link to the current blog
426
	 *
427
	 * Some user requested this feature. If active the plugin will place also a
428
	 * link to the current blog.
429
	 */
430
	public function output_current_blog() {
431
		echo $this->render_checkbox( 'output_current_blog' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
432
	}
433
434
	/**
435
	 * The description for the current blog
436
	 *
437
	 * The language will be used ff there is no description.
438
	 */
439
	public function description() {
440
		echo $this->render_input( 'description', '40' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
441
	}
442
443
	/**
444
	 * The output can be placed after the_content
445
	 */
446
	public function content_filter() {
447
		echo $this->render_checkbox( 'content_filter' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
448
	}
449
450
	/**
451
	 * If the output in the_content is active you can set the priority too
452
	 *
453
	 * Default is 10. But may be there are other plugins active and you run into
454
	 * trouble. So you can decide a higher (from 1) or a lower (to 100) priority
455
	 * for the output
456
	 */
457
	public function content_priority() {
458
		$temp     = array_merge( range( 1, 10 ), [ 20, 50, 100 ] );
459
		$arr      = array_combine( $temp, $temp );
460
		$selected = (
461
		empty( $this->options->content_priority ) ?
462
			10 :
463
			$this->options->content_priority
464
		);
465
466
		echo $this->render_select( 'content_priority', $arr, $selected );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
467
	}
468
469
	/**
470
	 * Rewrites slugs for registered post types
471
	 *
472
	 * @param string $key
473
	 */
474
	public function render_rewrite( $key ) {
475
		$rewrite = get_post_type_object( $key )->rewrite;
476
477
		$value = '';
478
		if ( true === $rewrite ) {
479
			$value = $key;
480
		} elseif ( ! empty( $rewrite['slug'] ) ) {
481
			$value = $rewrite['slug'];
482
		}
483
484
		echo $this->render_input( "rewrite_{$key}", 30, $value, true );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
485
	}
486
487
	/**
488
	 * Render form-element (checkbox)
489
	 *
490
	 * @param string $key Name and ID of the form-element
491
	 *
492
	 * @return string
493
	 */
494
	public function render_checkbox( $key ) {
495
		return sprintf(
496
			'<input type="checkbox" id="%1$s" name="msls[%1$s]" value="1" %2$s/>',
497
			$key,
498
			checked( 1, $this->options->$key, false )
499
		);
500
	}
501
502
	/**
503
	 * Render form-element (text-input)
504
	 *
505
	 * @param string $key Name and ID of the form-element
506
	 * @param string $size Size-attribute of the input-field
507
	 * @param string $default
508
	 * @param bool $readonly
509
	 *
510
	 * @return string
511
	 */
512
	public function render_input( $key, $size = '30', $default = '', $readonly = false ) {
513
		return sprintf(
514
			'<input id="%1$s" name="msls[%1$s]" value="%2$s" size="%3$s"%4$s/>',
515
			$key,
516
			esc_attr( ! empty( $this->options->$key ) ? $this->options->$key : $default ),
517
			$size,
518
			$readonly ? ' readonly="readonly"' : ''
519
		);
520
	}
521
522
	/**
523
	 * Render form-element (select)
524
	 * @uses selected
525
	 *
526
	 * @param string $key Name and ID of the form-element
527
	 * @param array $arr Options as associative array
528
	 * @param string $selected Values which should be selected
529
	 *
530
	 * @return string
531
	 */
532
	public function render_select( $key, array $arr, $selected = '' ) {
533
		$options = [];
534
535
		foreach ( $arr as $value => $description ) {
536
			$options[] = sprintf(
537
				'<option value="%s" %s>%s</option>',
538
				$value,
539
				selected( $value, $selected, false ),
540
				$description
541
			);
542
		}
543
544
		return sprintf(
545
			'<select id="%1$s" name="msls[%1$s]">%2$s</select>',
546
			$key,
547
			implode( '', $options )
548
		);
549
	}
550
551
	/**
552
	 * Validates input before saving it
553
	 *
554
	 * @param array $arr Values of the submitted form
555
	 *
556
	 * @return array Validated input
557
	 */
558
	public function validate( array $arr ) {
559
		/**
560
		 * Returns custom filtered input array
561
		 * @since 1.0
562
		 *
563
		 * @param array $arr
564
		 */
565
		$arr = apply_filters( 'msls_admin_validate', $arr );
566
567
		$arr['display'] = (
568
		isset( $arr['display'] ) ?
569
			(int) $arr['display'] :
570
			0
571
		);
572
573
		if ( isset( $arr['image_url'] ) ) {
574
			$arr['image_url'] = rtrim( esc_attr( $arr['image_url'] ), '/' );
575
		}
576
577
		return $arr;
578
	}
579
580
	/**
581
	 * Filter which sets the global blog language
582
	 *
583
	 * @param array $arr
584
	 *
585
	 * @return array
586
	 */
587
	public function set_blog_language( array $arr ) {
588
		if ( isset( $arr['blog_language'] ) ) {
589
			$blog_language = ( 'en_US' === $arr['blog_language'] ) ? '' : $arr['blog_language'];
590
			update_option( 'WPLANG', $blog_language );
591
			unset( $arr['blog_language'] );
592
		}
593
594
		return $arr;
595
	}
596
597
}
598