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

MslsAdmin::rewrites_section()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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