Completed
Branch dev (58959f)
by
unknown
02:11
created

output_fields()   F

Complexity

Conditions 58
Paths > 20000

Size

Total Lines 294
Code Lines 199

Duplication

Lines 16
Ratio 5.44 %

Importance

Changes 7
Bugs 3 Features 0
Metric Value
cc 58
eloc 199
c 7
b 3
f 0
nc 2350082
nop 1
dl 16
loc 294
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Auto Load Next Post Admin Settings Class.
4
 *
5
 * @since    1.0.0
6
 * @author   Sébastien Dumont
7
 * @category Admin
8
 * @package  Auto Load Next Post
9
 * @license  GPL-2.0+
10
 */
11
12
if ( ! defined('ABSPATH')) {
13
	exit;
14
}
15
// Exit if accessed directly
16
17
if ( ! class_exists('Auto_Load_Next_Post_Admin_Settings')) {
18
19
/**
20
 * Auto_Load_Next_Post_Admin_Settings
21
 *
22
 * @since 1.0.0
23
 */
24
class Auto_Load_Next_Post_Admin_Settings {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
25
26
	private static $settings = array();
27
	private static $errors   = array();
28
	private static $messages = array();
29
30
	/**
31
	 * Include the settings page classes
32
	 *
33
	 * @since  1.0.0
34
	 * @access public static
35
	 * @return $settings
0 ignored issues
show
Documentation introduced by
The doc-type $settings could not be parsed: Unknown type name "$settings" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
36
	 */
37
	public static function get_settings_pages() {
38
		if (empty(self::$settings)) {
39
			$settings = array();
40
41
			include_once('settings/class-auto-load-next-post-settings-page.php');
42
			$settings[] = include('settings/class-auto-load-next-post-settings-general.php');
43
			$settings[] = include('settings/class-auto-load-next-post-settings-support.php');
44
		}
45
46
		return self::$settings;
47
	} // END get_settings_page()
48
49
	/**
50
	 * Save the settings
51
	 *
52
	 * @since  1.0.0
53
	 * @access public static
54
	 * @global $current_tab
55
	 */
56
	public static function save() {
57
		global $current_tab;
58
59
		if (empty($_REQUEST['_wpnonce']) || ! wp_verify_nonce($_REQUEST['_wpnonce'], 'auto-load-next-post-settings')) {
60
			wp_die(__('Action failed. Please refresh the page and retry.', 'auto-load-next-post'));
61
		}
62
63
		// Trigger actions
64
		do_action('auto_load_next_post_settings_save_'.$current_tab);
65
		do_action('auto_load_next_post_update_options_'.$current_tab);
66
		do_action('auto_load_next_post_update_options');
67
68
		self::add_message(__('Your settings have been saved.', 'auto-load-next-post'));
69
70
		do_action('auto_load_next_post_settings_saved');
71
	} // END save()
72
73
	/**
74
	 * Add a message
75
	 *
76
	 * @since  1.0.0
77
	 * @access public static
78
	 * @param  string $text
79
	 */
80
	public static function add_message($text) {
81
		self::$messages[] = $text;
82
	} // END add_message()
83
84
	/**
85
	 * Add an error
86
	 *
87
	 * @since  1.0.0
88
	 * @access public static
89
	 * @param  string $text
90
	 */
91
	public static function add_error($text) {
92
		self::$errors[] = $text;
93
	} // END add_error()
94
95
	/**
96
	 * Output messages and errors.
97
	 *
98
	 * @since  1.0.0
99
	 * @access public static
100
	 * @return string
101
	 */
102
	public static function show_messages() {
103
		if (sizeof(self::$errors) > 0) {
104
			foreach (self::$errors as $error) {
105
				echo '<div id="message" class="error auto-load-next-post fade"><p><strong>'.esc_html($error).'</strong></p></div>';
106
			}
107
		} elseif (sizeof(self::$messages) > 0) {
108
			foreach (self::$messages as $message) {
109
				echo '<div id="message" class="updated auto-load-next-post fade"><p><strong>'.esc_html($message).'</strong></p></div>';
110
			}
111
		}
112
	} // END show_messages()
113
114
	/**
115
	 * Settings Page.
116
	 *
117
	 * Handles the display of the main settings page in admin.
118
	 *
119
	 * @since  1.0.0
120
	 * @access public static
121
	 * @filter auto_load_next_post_settings_tabs_array
122
	 * @global $current_tab
123
	 * @return void
124
	 */
125
	public static function output() {
126
		global $current_tab;
127
128
		// Get current tab
129
		$current_tab = empty($_GET['tab']) ? 'general' : sanitize_text_field(urldecode($_GET['tab']));
130
131
		wp_enqueue_script('auto_load_next_post_settings', AUTO_LOAD_NEXT_POST_URL_PATH.'/assets/js/admin/settings'.AUTO_LOAD_NEXT_POST_SCRIPT_MODE.'.js', array('jquery'), AUTO_LOAD_NEXT_POST_VERSION, true);
132
133
		wp_localize_script('auto_load_next_post_settings', 'auto_load_next_post_settings_params', array(
134
			'i18n_nav_warning' => __('The changes you made will be lost if you navigate away from this page.', 'auto-load-next-post'),
135
		));
136
137
		// Include settings pages
138
		self::get_settings_pages();
139
140
		// Save settings if data has been posted
141
		if ( ! empty($_POST)) {
142
			self::save();
143
		}
144
145
		// Add any posted messages
146
		if ( ! empty($_GET['auto_load_next_post_error'])) {
147
			self::add_error(urldecode(stripslashes($_GET['auto_load_next_post_error'])));
148
		}
149
150
		if ( ! empty($_GET['auto_load_next_post_message'])) {
151
			self::add_message(urldecode(stripslashes($_GET['auto_load_next_post_message'])));
152
		}
153
154
		self::show_messages();
155
156
		// Get tabs for the settings page
157
		$tabs = apply_filters('auto_load_next_post_settings_tabs_array', array());
158
159
		include('views/html-admin-settings.php');
160
	} // END output()
161
162
	/**
163
	 * Get a setting from the settings API.
164
	 *
165
	 * @since  1.0.0
166
	 * @access public static
167
	 * @param  mixed $option_name
168
	 * @return string
169
	 */
170
	public static function get_option($option_name, $default = '') {
171
		// Array value
172
		if (strstr($option_name, '[')) {
173
			parse_str($option_name, $option_array);
174
175
			// Option name is first key
176
			$option_name = current(array_keys($option_array));
177
178
			// Get value
179
			$option_values = get_option($option_name, '');
180
181
			$key = key($option_array[$option_name]);
182
183
			if (isset($option_values[$key])) {
184
				$option_value = $option_values[$key];
185
			} else {
186
				$option_value = null;
187
			}
188
		// Single value
189
		} else {
190
			$option_value = get_option($option_name, null);
191
		}
192
193
		if (is_array($option_value)) {
194
			$option_value = array_map('stripslashes', $option_value);
195
		} elseif ( ! is_null($option_value)) {
196
			$option_value = stripslashes($option_value);
197
		}
198
199
		return $option_value === null ? $default : $option_value;
200
	} // END get_option()
201
202
	/**
203
	 * Output admin fields.
204
	 *
205
	 * Loops though the plugin name options array and outputs each field.
206
	 *
207
	 * @since  1.0.0
208
	 * @access public static
209
	 * @param  array $options Opens array to output
210
	 */
211
	public static function output_fields($options) {
212
		foreach ($options as $value) {
213
			if ( ! isset($value['type'])) {
214
				continue;
215
			}
216
			if ( ! isset($value['id'])) {
217
				$value['id'] = '';
218
			}
219
			if ( ! isset($value['title'])) {
220
				$value['title'] = isset($value['name']) ? $value['name'] : '';
221
			}
222
			if ( ! isset($value['class'])) {
223
				$value['class'] = '';
224
			}
225
			if ( ! isset($value['css'])) {
226
				$value['css'] = '';
227
			}
228
			if ( ! isset($value['default'])) {
229
				$value['default'] = '';
230
			}
231
			if ( ! isset($value['desc'])) {
232
				$value['desc'] = '';
233
			}
234
			if ( ! isset($value['desc_tip'])) {
235
				$value['desc_tip'] = false;
236
			}
237
238
			// Custom attribute handling
239
			$custom_attributes = array();
240
241
			if ( ! empty($value['custom_attributes']) && is_array($value['custom_attributes'])) {
242
				foreach ($value['custom_attributes'] as $attribute => $attribute_value) {
243
					$custom_attributes[] = esc_attr($attribute).'="'.esc_attr($attribute_value).'"';
244
				}
245
			}
246
247
			// Description handling
248
			if ($value['desc_tip'] === true) {
249
				$description = '';
250
				$tip = $value['desc'];
251
			} else if ( ! empty($value['desc_tip'])) {
252
				$description = $value['desc'];
253
				$tip = $value['desc_tip'];
254
			} else if ( ! empty($value['desc'])) {
255
				$description = $value['desc'];
256
				$tip = '';
257
			} else {
258
				$description = $tip = '';
259
			}
260
261 View Code Duplication
			if ($description && in_array($value['type'], array('textarea', 'radio'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
				$description = '<p style="margin-top:0">'.wp_kses_post($description).'</p>';
263
			} else if ($description) {
264
				$description = '<span class="description">'.wp_kses_post($description).'</span>';
265
			}
266
267 View Code Duplication
			if ($tip && in_array($value['type'], array('checkbox'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
268
				$tip = '<p class="description">'.$tip.'</p>';
269
			} else if ($tip) {
270
				$tip = '<img class="help_tip" data-tip="'.esc_attr($tip).'" src="'.AUTO_LOAD_NEXT_POST_URL_PATH.'/assets/images/help.png" height="16" width="16" />';
271
			}
272
273
			// Switch based on type
274
			switch ($value['type']) {
275
				// Section Titles
276
				case 'title':
277
					if ( ! empty($value['title'])) {
278
						echo '<h3>'.esc_html($value['title']).'</h3>';
279
					}
280
281
					if ( ! empty($value['desc'])) {
282
						echo wpautop(wptexturize(wp_kses_post($value['desc'])));
283
					}
284
285
					echo '<table class="form-table">'."\n\n";
286
287
					if ( ! empty($value['id'])) {
288
						do_action('auto_load_next_post_settings_'.sanitize_title($value['id']));
289
					}
290
291
					break;
292
293
				// Section Ends
294
				case 'sectionend':
295
296 View Code Duplication
					if ( ! empty($value['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
297
						do_action('auto_load_next_post_settings_'.sanitize_title($value['id']).'_end');
298
					}
299
300
					echo '</table>';
301
302 View Code Duplication
					if ( ! empty($value['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303
						do_action('auto_load_next_post_settings_'.sanitize_title($value['id']).'_after');
304
					}
305
306
					break;
307
308
				// Standard text inputs and subtypes like 'number'
309
				case 'text':
310
				case 'number':
311
					$type         = $value['type'];
312
					$class        = '';
313
					$option_value = self::get_option($value['id'], $value['default']);
314
					?><tr valign="top">
315
						<th scope="row" class="titledesc">
316
							<label for="<?php echo esc_attr($value['id']); ?>"><?php echo esc_html($value['title']); ?></label>
317
							<?php echo $tip; ?>
318
						</th>
319
						<td class="forminp forminp-<?php echo sanitize_title($value['type']); ?>">
320
							<input
321
								name="<?php echo esc_attr($value['id']); ?>"
322
								id="<?php echo esc_attr($value['id']); ?>"
323
								type="<?php echo esc_attr($type); ?>"
324
								style="<?php echo esc_attr($value['css']); ?>"
325
								value="<?php echo esc_attr($option_value); ?>"
326
								class="<?php echo esc_attr($value['class']); ?>"
327
								<?php echo implode(' ', $custom_attributes); ?>
328
							/> <?php echo $description; ?>
329
						</td>
330
					</tr><?php
331
332
					break;
333
334
				// Textarea
335
				case 'textarea':
336
					$option_value = self::get_option($value['id'], $value['default']);
337
					?><tr valign="top">
338
						<th scope="row" class="titledesc">
339
							<label for="<?php echo esc_attr($value['id']); ?>"><?php echo esc_html($value['title']); ?></label>
340
							<?php echo $tip; ?>
341
						</th>
342
						<td class="forminp forminp-<?php echo sanitize_title($value['type']); ?>">
343
						<?php echo $description; ?>
344
						<textarea
345
								name="<?php echo esc_attr($value['id']); ?>"
346
								id="<?php echo esc_attr($value['id']); ?>"
347
								style="<?php echo esc_attr($value['css']); ?>"
348
								class="<?php echo esc_attr($value['class']); ?>"
349
								<?php echo implode(' ', $custom_attributes); ?>
350
								><?php echo esc_textarea($option_value); ?></textarea>
351
						</td>
352
					</tr><?php
353
354
					break;
355
356
				// Select boxes
357
				case 'select':
358
				case 'multiselect':
359
					$option_value = self::get_option($value['id'], $value['default']);
360
					?><tr valign="top">
361
						<th scope="row" class="titledesc">
362
							<label for="<?php echo esc_attr($value['id']); ?>"><?php echo esc_html($value['title']); ?></label>
363
							<?php echo $tip; ?>
364
						</th>
365
						<td class="forminp forminp-<?php echo sanitize_title($value['type']); ?>">
366
						<select
367
								name="<?php echo esc_attr($value['id']); ?><?php if ($value['type'] == 'multiselect') {
368
	echo '[]';
369
}
370
?>"
371
								id="<?php echo esc_attr($value['id']); ?>"
372
								style="<?php echo esc_attr($value['css']); ?>"
373
								class="<?php echo esc_attr($value['class']); ?>"
374
								<?php echo implode(' ', $custom_attributes); ?>
375
								<?php if ($value['type'] == 'multiselect') {
376
	echo 'multiple="multiple"';
377
}
378
?>>
379
								<?php foreach ($value['options'] as $key => $val) { ?>
380
										<option value="<?php echo esc_attr($key); ?>" <?php
381
											if (is_array($option_value)) {
382
												selected(in_array($key, $option_value), true);
383
											} else {
384
												selected($option_value, $key);
385
											}
386
										?>><?php echo $val ?></option>
387
										<?php
388
									}
389
								?>
390
							</select> <?php echo $description; ?>
391
						</td>
392
						</tr><?php
393
394
						break;
395
396
				// Radio inputs
397
				case 'radio':
398
					$option_value = self::get_option($value['id'], $value['default']);
399
					?><tr valign="top">
400
						<th scope="row" class="titledesc">
401
							<label for="<?php echo esc_attr($value['id']); ?>"><?php echo esc_html($value['title']); ?></label>
402
							<?php echo $tip; ?>
403
						</th>
404
						<td class="forminp forminp-<?php echo sanitize_title($value['type']); ?>">
405
							<fieldset>
406
								<?php echo $description; ?>
407
							<ul>
408
							<?php foreach ($value['options'] as $key => $val) { ?>
409
								<li>
410
									<label><input
411
												name="<?php echo esc_attr($value['id']); ?>"
412
												value="<?php echo $key; ?>"
413
												type="radio"
414
												style="<?php echo esc_attr($value['css']); ?>"
415
												class="<?php echo esc_attr($value['class']); ?>"
416
												<?php echo implode(' ', $custom_attributes); ?>
417
												<?php checked($key, $option_value); ?>
418
												/> <?php echo $val ?></label>
419
								</li>
420
							<?php } ?>
421
							</ul>
422
							</fieldset>
423
						</td>
424
					</tr><?php
425
426
					break;
427
428
				// Checkbox input
429
				case 'checkbox':
430
					$option_value = self::get_option($value['id'], $value['default']);
431
					if ( ! isset($value['hide_if_checked'])) {
432
						$value['hide_if_checked'] = false;
433
					}
434
					if ( ! isset($value['show_if_checked'])) {
435
						$value['show_if_checked'] = false;
436
					}
437
					if ( ! isset($value['checkboxgroup']) || (isset($value['checkboxgroup']) && $value['checkboxgroup'] == 'start')) {
438
					?>
439
						<tr valign="top" class="<?php
440
							if ($value['hide_if_checked'] == 'yes' || $value['show_if_checked'] == 'yes') {
441
								echo 'hidden_option';
442
							}
443
							if ($value['hide_if_checked'] == 'option') {
444
								echo 'hide_options_if_checked';
445
							}
446
							if ($value['show_if_checked'] == 'option') {
447
								echo 'show_options_if_checked';
448
							}
449
						?>">
450
						<th scope="row" class="titledesc"><?php echo esc_html($value['title']); ?></th>
451
						<td class="forminp forminp-checkbox">
452
							<fieldset>
453
						<?php
454
					} else {
455
						?>
456
						<fieldset class="<?php
457
							if ($value['hide_if_checked'] == 'yes' || $value['show_if_checked'] == 'yes') {
458
								echo 'hidden_option';
459
							}
460
							if ($value['hide_if_checked'] == 'option') {
461
								echo 'hide_options_if_checked';
462
							}
463
							if ($value['show_if_checked'] == 'option') {
464
								echo 'show_options_if_checked';
465
							}
466
						?>">
467
					<?php
468
					}
469
					?>
470
						<legend class="screen-reader-text"><span><?php echo esc_html($value['title']); ?></span></legend>
471
						<label for="<?php echo $value['id'] ?>">
472
						<input
473
							name="<?php echo esc_attr($value['id']); ?>"
474
							id="<?php echo esc_attr($value['id']); ?>"
475
							type="checkbox"
476
							value="1"
477
							<?php checked($option_value, 'yes'); ?>
478
							<?php echo implode(' ', $custom_attributes); ?>
479
						/> <?php echo wp_kses_post($value['desc']) ?></label> <?php echo $tip; ?>
480
					<?php
481
					if ( ! isset($value['checkboxgroup']) || (isset($value['checkboxgroup']) && $value['checkboxgroup'] == 'end')) {
482
						?>
483
							</fieldset>
484
						</td>
485
						</tr>
486
						<?php
487
					} else {
488
						?>
489
						</fieldset>
490
						<?php
491
					}
492
493
					break;
494
495
				// Default: run an action
496
				default:
497
					do_action('auto_load_next_post_admin_field_'.$value['type'], $value);
498
499
					break;
500
			} // end switch
501
502
		}
503
504
	} // END output_fields()
505
506
	/**
507
	 * Save admin fields.
508
	 *
509
	 * Loops though the plugin name options array and outputs each field.
510
	 *
511
	 * @since  1.0.0
512
	 * @access public static
513
	 * @param  array $options Opens array to output
514
	 * @return bool
515
	 */
516
	public static function save_fields($options, $current_tab) {
517
		if (empty($_POST)) {
518
					return false;
519
		}
520
521
		// Options to update will be stored here
522
		$update_options = array();
523
524
		// Loop options and get values to save
525
		foreach ($options as $value) {
526
527
			if ( ! isset($value['id'])) {
528
							continue;
529
			}
530
531
			$type = isset($value['type']) ? sanitize_title($value['type']) : '';
532
533
			// Get the option name
534
			$option_value = null;
535
536
			switch ($type) {
537
				// Standard types
538
				case "checkbox" :
539
					if (isset($_POST[$value['id']])) {
540
						$option_value = 'yes';
541
					} else {
542
						$option_value = 'no';
543
					}
544
545
				break;
546
547 View Code Duplication
				case "textarea" :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
548
					if (isset($_POST[$value['id']])) {
549
						$option_value = wp_kses_post(trim(stripslashes($_POST[$value['id']])));
550
					} else {
551
						$option_value = '';
552
					}
553
554
				break;
555
556
				case "text" :
557
				case "number":
558
				case "select" :
559
				case "single_select_page" :
560 View Code Duplication
				case "radio" :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
561
					if (isset($_POST[$value['id']])) {
562
						$option_value = auto_load_next_post_clean(stripslashes($_POST[$value['id']]));
563
					} else {
564
						$option_value = '';
565
					}
566
567
				break;
568
569
				// Special types
570
				case "multiselect" :
571
					// Get array
572
					if (isset($_POST[$value['id']])) {
573
						$selected_values = array_map('auto_load_next_post_clean', array_map('stripslashes', (array) $_POST[$value['id']]));
574
					} else {
575
						$selected_values = array();
576
					}
577
					$option_value = $selected_values;
578
579
				break;
580
581
				// Custom handling
582
				default :
583
					do_action('auto_load_next_post_update_option_'.$type, $value);
584
585
				break;
586
587
			} // END switch()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
588
589
			if ( ! is_null($option_value)) {
590
591
				// Check if option is an array
592
				if (strstr($value['id'], '[')) {
593
					parse_str($value['id'], $option_array);
594
595
					// Option name is first key
596
					$option_name = current(array_keys($option_array));
597
598
					// Get old option value
599
					if ( ! isset($update_options[$option_name])) {
600
						$update_options[$option_name] = get_option($option_name, array());
601
					}
602
603
					if ( ! is_array($update_options[$option_name])) {
604
						$update_options[$option_name] = array();
605
					}
606
607
					// Set keys and value
608
					$key = key($option_array[$option_name]);
609
					$update_options[$option_name][$key] = $option_value;
610
611
				// Single value
612
				} else {
613
					$update_options[$value['id']] = $option_value;
614
				}
615
616
			}
617
618
			// Custom handling
619
			do_action('auto_load_next_post_update_option', $value);
620
		}
621
622
		// Now save the options
623
		foreach ($update_options as $name => $value) {
624
			update_option($name, $value);
625
		}
626
627
		// Save all options as an array. Ready for export.
628
		update_option('auto_load_next_post_options_'.$current_tab, $update_options);
629
630
		return true;
631
	} // END save_fields()
632
633
} // END class.
634
635
} // end if class exists.
636