Completed
Push — master ( 70c768...281d7e )
by Dennis
02:10
created

MslsAdmin::content_priority()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
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
	 * Init
16
	 * @return MslsAdmin
17
	 */
18
	public static function init() {
19
		$obj = new self();
20
21
		if ( current_user_can( 'manage_options' ) ) {
22
			$title = __( 'Multisite Language Switcher', 'multisite-language-switcher' );
23
			add_options_page( $title, $title, 'manage_options', __CLASS__, array( $obj, 'render' ) );
24
25
			add_action( 'admin_init',    array( $obj, 'register' ) );
26
			add_action( 'admin_notices', array( $obj, 'has_problems' ) );
27
28
			add_filter( 'msls_admin_validate', array( $obj, 'set_blog_language' ) );
29
		}
30
31
		return $obj;
32
	}
33
34
	/**
35
	 * There is something wrong? Here comes the message...
36
	 * @return bool
37
	 */
38
	public function has_problems() {
39
		$message = '';
40
		$options = MslsOptions::instance();
41
42
		if ( 1 == count( $options->get_available_languages() ) ) {
43
			$message = sprintf(
44
				__( '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.' ),
45
				esc_url( 'http://codex.wordpress.org/Installing_WordPress_in_Your_Language#Manually_Installing_Language_Files' ),
46
				esc_url( 'http://wordpress.org/plugins/wp-native-dashboard/' )
47
			);
48
		}
49
		elseif ( $options->is_empty() ) {
50
			$message = sprintf(
51
				__( 'Multisite Language Switcher is almost ready. You must <a href="%s">complete the configuration process</a>.' ),
52
				esc_url( admin_url( '/options-general.php?page=MslsAdmin' ) )
53
			);
54
		}
55
56
		return MslsPlugin::message_handler( $message, 'updated fade' );
57
	}
58
59
	/**
60
	 * Render the options-page
61
	 * @codeCoverageIgnore
62
	 */
63
	public function render() {
64
		printf(
65
			'<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>',
66
			__( 'Multisite Language Switcher Options', 'multisite-language-switcher' ),
67
			$this->subsubsub(),
68
			__( 'To achieve maximum flexibility, you have to configure each blog separately.', 'multisite-language-switcher' )
69
		);
70
71
		settings_fields( 'msls' );
72
		do_settings_sections( __CLASS__ );
73
74
		printf(
75
			'<p class="submit"><input name="Submit" type="submit" class="button-primary" value="%s" /></p></form></div>',
76
			( MslsOptions::instance()->is_empty() ? __( 'Configure', 'multisite-language-switcher' ) : __( 'Update', 'multisite-language-switcher' ) )
77
		);
78
	}
79
80
	/**
81
	 * Create a submenu which contains links to all blogs of the current user
82
	 * @return string
83
	 */
84
	public function subsubsub() {
85
		$arr = array();
86
87
		$blogs = MslsBlogCollection::instance();
88
		foreach ( $blogs->get_plugin_active_blogs() as $blog ) {
89
			$arr[] = sprintf(
90
				'<a href="%s"%s>%s / %s</a>',
91
				get_admin_url( $blog->userblog_id, '/options-general.php?page=MslsAdmin' ),
92
				( $blog->userblog_id == $blogs->get_current_blog_id() ? ' class="current"' : '' ),
93
				$blog->blogname,
94
				$blog->get_description()
95
			);
96
		}
97
98
		return(
99
			empty( $arr ) ?
100
			'' :
101
			sprintf(
102
				'<ul class="subsubsub"><li>%s</li></ul>',
103
				implode( ' | </li><li>', $arr )
104
			)
105
		);
106
	}
107
108
	/**
109
	 * Register the form-elements
110
	 * @codeCoverageIgnore
111
	 */
112
	public function register() {
113
		register_setting( 'msls', 'msls', array( $this, 'validate' ) );
114
115
		add_settings_section( 'language_section', __( 'Language Settings', 'multisite-language-switcher' ), array( $this, 'language_section' ), __CLASS__ );
116
		add_settings_section( 'main_section', __( 'Main Settings', 'multisite-language-switcher' ), array( $this, 'main_section' ), __CLASS__ );
117
		add_settings_section( 'advanced_section', __( 'Advanced Settings', 'multisite-language-switcher' ), array( $this, 'advanced_section' ), __CLASS__ );
118
119
		/**
120
		 * Lets you add your own settings section
121
		 * @since 1.0
122
		 * @param string $page
123
		 */
124
		do_action( 'msls_admin_register', __CLASS__ );
125
	}
126
127
	/**
128
	 * Register the fields in the language_section
129
	 * @codeCoverageIgnore
130
	 */
131
	public function language_section() {
132
		add_settings_field( 'blog_language', __( 'Blog Language', 'multisite-language-switcher' ), array( $this, 'blog_language' ), __CLASS__, 'language_section' );
133
		add_settings_field( 'admin_language', __( 'Admin Language', 'multisite-language-switcher' ), array( $this, 'admin_language' ), __CLASS__, 'language_section' );
134
135
		/**
136
		 * Lets you add your own field to the language section
137
		 * @since 1.0
138
		 * @param string $page
139
		 * @param string $section
140
		 */
141
		do_action( 'msls_admin_language_section', __CLASS__, 'language_section' );
142
	}
143
144
	/**
145
	 * Register the fields in the main_section
146
	 * @codeCoverageIgnore
147
	 */
148
	public function main_section() {
149
		add_settings_field( 'display', __( 'Display', 'multisite-language-switcher' ), array( $this, 'display' ), __CLASS__, 'main_section' );
150
		add_settings_field( 'sort_by_description', __( 'Sort output by description', 'multisite-language-switcher' ), array( $this, 'sort_by_description' ), __CLASS__, 'main_section' );
151
		add_settings_field( 'output_current_blog', __( 'Display link to the current language', 'multisite-language-switcher' ), array( $this, 'output_current_blog' ), __CLASS__, 'main_section' );
152
		add_settings_field( 'only_with_translation', __( 'Show only links with a translation', 'multisite-language-switcher' ), array( $this, 'only_with_translation' ), __CLASS__, 'main_section' );
153
		add_settings_field( 'x_default', __( 'x-default', 'multisite-language-switcher' ), array( $this, 'x_default' ), __CLASS__, 'main_section' );
154
		add_settings_field( 'description', __( 'Description', 'multisite-language-switcher' ), array( $this, 'description' ), __CLASS__, 'main_section' );
155
		add_settings_field( 'before_output', __( 'Text/HTML before the list', 'multisite-language-switcher' ), array( $this, 'before_output' ), __CLASS__, 'main_section' );
156
		add_settings_field( 'after_output', __( 'Text/HTML after the list', 'multisite-language-switcher' ), array( $this, 'after_output' ), __CLASS__, 'main_section' );
157
		add_settings_field( 'before_item', __( 'Text/HTML before each item', 'multisite-language-switcher' ), array( $this, 'before_item' ), __CLASS__, 'main_section' );
158
		add_settings_field( 'after_item', __( 'Text/HTML after each item', 'multisite-language-switcher' ), array( $this, 'after_item' ), __CLASS__, 'main_section' );
159
		add_settings_field( 'content_filter', __( 'Add hint for available translations', 'multisite-language-switcher' ), array( $this, 'content_filter' ), __CLASS__, 'main_section' );
160
		add_settings_field( 'content_priority', __( 'Hint priority', 'multisite-language-switcher' ), array( $this, 'content_priority' ), __CLASS__, 'main_section' );
161
162
		/**
163
		 * Lets you add your own field to the main section
164
		 * @since 1.0
165
		 * @param string $page
166
		 * @param string $section
167
		 */
168
		do_action( 'msls_admin_main_section', __CLASS__, 'main_section' );
169
	}
170
171
	/**
172
	 * Register the fields in the advanced_section
173
	 * @codeCoverageIgnore
174
	 */
175
	public function advanced_section() {
176
		add_settings_field( 'activate_autocomplete', __( 'Activate experimental autocomplete inputs', 'multisite-language-switcher' ), array( $this, 'activate_autocomplete' ), __CLASS__, 'advanced_section' );
177
		add_settings_field( 'image_url', __( 'Custom URL for flag-images', 'multisite-language-switcher' ), array( $this, 'image_url' ), __CLASS__, 'advanced_section' );
178
		add_settings_field( 'reference_user', __( 'Reference user', 'multisite-language-switcher' ), array( $this, 'reference_user' ), __CLASS__, 'advanced_section' );
179
		add_settings_field( 'exclude_current_blog', __( 'Exclude this blog from output', 'multisite-language-switcher' ), array( $this, 'exclude_current_blog' ), __CLASS__, 'advanced_section' );
180
181
		/**
182
		 * Lets you add your own field to the advanced section
183
		 * @since 1.0
184
		 * @param string $page
185
		 * @param string $section
186
		 */
187
		do_action( 'msls_admin_advanced_section', __CLASS__, 'advanced_section' );
188
	}
189
190
	/**
191
	 * Shows the select-form-field 'blog_language'
192
	 */
193
	public function blog_language() {
194
		echo $this->render_select(
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
195
			'blog_language',
196
			MslsOptions::instance()->get_available_languages(),
197
			get_option( 'WPLANG', 'en_US' )
198
		);
199
	}
200
201
	/**
202
	 * Shows the select-form-field 'admin_language'
203
	 */
204
	public function admin_language() {
205
		$options = MslsOptions::instance();
206
		echo $this->render_select(
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
207
			'admin_language',
208
			$options->get_available_languages(),
209
			$options->admin_language
210
		);
211
	}
212
213
	/**
214
	 * Shows the select-form-field 'display'
215
	 */
216
	public function display() {
217
		echo $this->render_select(
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
218
			'display',
219
			MslsLink::get_types_description(),
220
			MslsOptions::instance()->display
221
		);
222
	}
223
224
	/**
225
	 * Shows the select-form-field 'reference_user'
226
	 */
227
	public function reference_user() {
228
		$users = array();
229
230
		foreach ( MslsBlogCollection::instance()->get_users() as $user ) {
231
			$users[ $user->ID ] = $user->user_nicename;
232
		}
233
234
		echo $this->render_select( 'reference_user', $users, MslsOptions::instance()->reference_user );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
235
	}
236
237
	/**
238
	 * Activate autocomplete
239
	 *
240
	 * You can decide if you want to activate the experimental autocomplete
241
	 * input fields in the backend instead of the traditional select-menus.
242
	 */
243
	public function activate_autocomplete() {
244
		echo $this->render_checkbox( 'activate_autocomplete' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
245
	}
246
247
	/**
248
	 * Show sort_by_description-field
249
	 *
250
	 * You can decide that the ouput will be sorted by the description. If not
251
	 * the output will be sorted by the language-code.
252
	 */
253
	public function sort_by_description() {
254
		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...
255
	}
256
257
	/**
258
	 * Exclude the current blog
259
	 *
260
	 * You can exclude a blog explicitly. All your settings will be safe but the
261
	 * plugin will ignore this blog while this option is active.
262
	 */
263
	public function exclude_current_blog() {
264
		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...
265
	}
266
267
	/**
268
	 * Show only a link  if a translation is available
269
	 *
270
	 * Some user requested this feature. Shows only links to available
271
	 * translations.
272
	 */
273
	public function only_with_translation() {
274
		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...
275
	}
276
277
	/**
278
	 * Show a link to the current blog
279
	 *
280
	 * Some user requested this feature. If active the plugin will place also a
281
	 * link to the current blog.
282
	 */
283
	public function output_current_blog() {
284
		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...
285
	}
286
287
	/**
288
	 * Use this blog's hreflang as x-default
289
	 */
290
	public function x_default() {
291
		echo $this->render_checkbox( 'x_default' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
292
	}
293
294
	/**
295
	 * The description for the current blog
296
	 *
297
	 * The language will be used ff there is no description.
298
	 */
299
	public function description() {
300
		echo $this->render_input( 'description', '40' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
301
	}
302
303
	/**
304
	 * A String which will be placed before the output of the list
305
	 */
306
	public function before_output() {
307
		echo $this->render_input( 'before_output' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
308
	}
309
310
	/**
311
	 * A String which will be placed after the output of the list
312
	 */
313
	public function after_output() {
314
		echo $this->render_input( 'after_output' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
315
	}
316
317
	/**
318
	 * A String which will be placed before every item of the list
319
	 */
320
	public function before_item() {
321
		echo $this->render_input( 'before_item' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
322
	}
323
324
	/**
325
	 * A String which will be placed after every item of the list
326
	 */
327
	public function after_item() {
328
		echo $this->render_input( 'after_item' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
329
	}
330
331
	/**
332
	 * The output can be placed after the_content
333
	 */
334
	public function content_filter() {
335
		echo $this->render_checkbox( 'content_filter' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
336
	}
337
338
	/**
339
	 * If the output in the_content is active you can set the priority too
340
	 *
341
	 * Default is 10. But may be there are other plugins active and you run into
342
	 * trouble. So you can decide a higher (from 1) or a lower (to 100) priority
343
	 * for the output
344
	 */
345
	public function content_priority() {
346
		$temp     = array_merge( range( 1, 10 ), array( 20, 50, 100 ) );
347
		$arr      = array_combine( $temp, $temp );
348
		$options  = MslsOptions::instance();
349
		$selected = ( empty( $options->content_priority ) ? 10 : $options->content_priority );
350
351
		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...
352
	}
353
354
	/**
355
	 * Alternative image-url
356
	 * @todo This is a value of a directory-url which should be more clear
357
	 */
358
	public function image_url() {
359
		echo $this->render_input( 'image_url' );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
360
	}
361
362
	/**
363
	 * Render form-element (checkbox)
364
	 * @param string $key Name and ID of the form-element
365
	 * @return string
366
	 */
367
	public function render_checkbox( $key ) {
368
		return sprintf(
369
			'<input type="checkbox" id="%1$s" name="msls[%1$s]" value="1" %2$s/>',
370
			$key,
371
			checked( 1, MslsOptions::instance()->$key, false )
372
		);
373
	}
374
375
	/**
376
	 * Render form-element (text-input)
377
	 * @param string $key Name and ID of the form-element
378
	 * @param string $size Size-attribute of the input-field
379
	 * @return string
380
	 */
381
	public function render_input( $key, $size = '30' ) {
382
		return sprintf(
383
			'<input id="%1$s" name="msls[%1$s]" value="%2$s" size="%3$s"/>',
384
			$key,
385
			esc_attr( MslsOptions::instance()->$key ),
386
			$size
387
		);
388
	}
389
390
	/**
391
	 * Render form-element (select)
392
	 * @uses selected
393
	 * @param string $key Name and ID of the form-element
394
	 * @param array $arr Options as associative array
395
	 * @param string $selected Values which should be selected
396
	 * @return string
397
	 */
398
	public function render_select( $key, array $arr, $selected = '' ) {
399
		$options = array();
400
401
		foreach ( $arr as $value => $description ) {
402
			$options[] = sprintf(
403
				'<option value="%s" %s>%s</option>',
404
				$value,
405
				selected( $value, $selected, false ),
406
				$description
407
			);
408
		}
409
410
		return sprintf(
411
			'<select id="%1$s" name="msls[%1$s]">%2$s</select>',
412
			$key,
413
			implode( '', $options )
414
		);
415
	}
416
417
	/**
418
	 * Validates input before saving it
419
	 * @param array $arr Values of the submitted form
420
	 * @return array Validated input
421
	 */
422
	public function validate( array $arr ) {
423
		/**
424
		 * Returns custom filtered input array
425
		 * @since 1.0
426
		 *
427
		 * @param array $arr
428
		 */
429
		$arr = (array) apply_filters( 'msls_admin_validate', $arr );
430
431
		$arr['display'] = ( isset( $arr['display'] ) ? (int) $arr['display'] : 0 );
432
433
		if ( isset( $arr['image_url'] ) ) {
434
			$arr['image_url'] = rtrim( esc_attr( $arr['image_url'] ), '/' );
435
		}
436
437
		return $arr;
438
	}
439
440
	/**
441
	 * Filter which sets the global blog language
442
	 * @param array $arr
443
	 * @return array
444
	 */
445
	public function set_blog_language( array $arr ) {
446
		if ( isset( $arr['blog_language'] ) ) {
447
			$blog_language = ( 'en_US' === $arr['blog_language'] ) ? '' : $arr['blog_language'];
448
			update_option( 'WPLANG', $blog_language );
449
			unset( $arr['blog_language'] );
450
		}
451
		return $arr;
452
	}
453
454
}
455