Completed
Branch master (136588)
by
unknown
02:26
created

ALNP_Admin_Settings::save_fields()   F

Complexity

Conditions 31
Paths 4493

Size

Total Lines 116

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
nc 4493
nop 1
dl 0
loc 116
rs 0
c 0
b 0
f 0

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
 * @version  1.6.0
7
 * @author   Sébastien Dumont
8
 * @category Admin
9
 * @package  Auto Load Next Post/Admin/Settings
10
 * @license  GPL-2.0+
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( 'ALNP_Admin_Settings' ) ) {
19
20
	class ALNP_Admin_Settings {
21
22
		/**
23
		 * Setting pages.
24
		 *
25
		 * @access private
26
		 * @static
27
		 * @var array
28
		 */
29
		private static $settings = array();
30
31
		/**
32
		 * Error messages.
33
		 *
34
		 * @access private
35
		 * @static
36
		 * @var array
37
		 */
38
		private static $errors = array();
39
40
		/**
41
		 * Update messages.
42
		 *
43
		 * @access private
44
		 * @static
45
		 * @var array
46
		 */
47
		private static $messages = array();
48
49
		/**
50
		 * Include the settings page classes.
51
		 *
52
		 * @access  public
53
		 * @static
54
		 * @since   1.0.0
55
		 * @version 1.6.0
56
		 * @return  $settings
57
		 */
58
		public static function get_settings_pages() {
59
			if ( empty( self::$settings ) ) {
60
				$settings = array();
61
62
				include_once( dirname( __FILE__ ) . '/settings/class-alnp-settings-page.php' );
63
64
				$settings[] = include( dirname( __FILE__ ) . '/settings/class-alnp-settings-theme-selectors.php' );
65
				$settings[] = include( dirname( __FILE__ ) . '/settings/class-alnp-settings-templates.php' );
66
				$settings[] = include( dirname( __FILE__ ) . '/settings/class-alnp-settings-events.php' );
67
				$settings[] = include( dirname( __FILE__ ) . '/settings/class-alnp-settings-misc.php' );
68
69
				self::$settings = apply_filters( 'alnp_get_settings_pages', $settings );
70
			}
71
72
			return self::$settings;
73
		} // END get_settings_page()
74
75
		/**
76
		 * Save the settings.
77
		 *
78
		 * @access  public
79
		 * @static
80
		 * @since   1.0.0
81
		 * @version 1.6.0
82
		 * @global  $current_view
83
		 */
84
		public static function save() {
85
			global $current_view;
86
87
			check_admin_referer( 'auto-load-next-post-settings' );
88
89
			// Trigger actions
90
			do_action( 'auto_load_next_post_settings_save_' . $current_view );
91
			do_action( 'auto_load_next_post_update_options_' . $current_view );
92
			do_action( 'auto_load_next_post_update_options' );
93
94
			self::add_message( __( 'Your settings have been saved.', 'auto-load-next-post' ) );
95
96
			do_action( 'auto_load_next_post_settings_saved' );
97
		} // END save()
98
99
		/**
100
		 * Add a message
101
		 *
102
		 * @access public
103
		 * @static
104
		 * @since  1.0.0
105
		 * @param  string $text Message
106
		 */
107
		public static function add_message( $text ) {
108
			self::$messages[] = $text;
109
		} // END add_message()
110
111
		/**
112
		 * Add an error
113
		 *
114
		 * @access public
115
		 * @static
116
		 * @since  1.0.0
117
		 * @param  string $text Error
118
		 */
119
		public static function add_error( $text ) {
120
			self::$errors[] = $text;
121
		} // END add_error()
122
123
		/**
124
		 * Output messages and errors.
125
		 *
126
		 * @access  public
127
		 * @static
128
		 * @since   1.0.0
129
		 * @version 1.6.0
130
		 * @return  string
131
		 */
132
		public static function show_messages() {
133
			if ( count( self::$errors ) > 0 ) {
134
				foreach ( self::$errors as $error ) {
135
					echo '<div class="notice notice-error"><p><strong>' . esc_html( $error ) . '</strong></p></div>';
136
				}
137
			} elseif ( count( self::$messages ) > 0 ) {
138
				foreach ( self::$messages as $message ) {
139
					echo '<div class="notice notice-success"><p><strong>' . esc_html( $message ) . '</strong></p></div>';
140
				}
141
			}
142
		} // END show_messages()
143
144
		/**
145
		 * Settings Page.
146
		 *
147
		 * Handles the display of the main settings page in admin.
148
		 *
149
		 * @access  public
150
		 * @static
151
		 * @since   1.0.0
152
		 * @version 1.6.0
153
		 * @global  $current_view
154
		 * @return  void
155
		 */
156
		public static function output() {
157
			global $current_view;
158
159
			do_action( 'auto_load_next_post_settings_start' );
160
161
			// Get tabs for the settings page.
162
			$tabs = apply_filters( 'alnp_settings_tabs_array', array() );
163
164
			// These tabs do not require a settings output.
165
			$no_settings_req = array( 'getting-started', 'setup-wizard', 'extensions', 'videos' );
166
167
			// Only include settings output if the current tab requires it.
168
			if ( ! in_array( $current_view, $no_settings_req ) ) {
169
				include( dirname( __FILE__ ) . '/views/html-admin-settings.php' );
170
			}
171
172
			do_action( 'auto_load_next_post_settings_end', $current_view, $tabs );
173
		} // END output()
174
175
		/**
176
		 * Get a setting from the settings API.
177
		 *
178
		 * @access public
179
		 * @static
180
		 * @since  1.0.0
181
		 * @param  mixed $option_name
182
		 * @return string
183
		 */
184
		public static function get_option( $option_name, $default = '' ) {
185
			// Array value
186
			if ( strstr( $option_name, '[' ) ) {
187
				parse_str( $option_name, $option_array );
188
189
				// Option name is first key
190
				$option_name = current( array_keys( $option_array ) );
191
192
				// Get value
193
				$option_values = get_option( $option_name, '' );
194
195
				$key = key( $option_array[$option_name] );
196
197
				if ( isset( $option_values[$key] ) ) {
198
					$option_value = $option_values[$key];
199
				} else {
200
					$option_value = null;
201
				}
202
			} else {
203
				// Single value
204
				$option_value = get_option( $option_name, null );
205
			}
206
207
			if ( is_array( $option_value ) ) {
208
				$option_value = array_map( 'stripslashes', $option_value );
209
			} elseif ( ! is_null( $option_value ) ) {
210
				$option_value = stripslashes( $option_value );
211
			}
212
213
			return $option_value === null ? $default : $option_value;
214
		} // END get_option()
215
216
		/**
217
		 * Output admin fields.
218
		 *
219
		 * Loops though the plugin options array and outputs each field.
220
		 *
221
		 * @access  public
222
		 * @static
223
		 * @since   1.0.0
224
		 * @version 1.6.0
225
		 * @param   array $options Opens array to output
226
		 */
227
		public static function output_fields( $options ) {
228
			foreach ( $options as $key => $value ) {
229
230
			if ( ! isset( $value['type'] ) ) {
231
					continue;
232
				}
233
				if ( ! isset( $value['id'] ) ) {
234
					$value['id'] = '';
235
				}
236
				if ( ! isset( $value['title'] ) ) {
237
					$value['title'] = isset( $value['name'] ) ? $value['name'] : '';
238
				}
239
				if ( ! isset( $value['class'] ) ) {
240
					$value['class'] = '';
241
				}
242
				if ( ! isset( $value['css'] ) ) {
243
					$value['css'] = '';
244
				}
245
				if ( ! isset( $value['default'] ) ) {
246
					$value['default'] = '';
247
				}
248
				if ( ! isset( $value['desc'] ) ) {
249
					$value['desc'] = '';
250
				}
251
				if ( ! isset( $value['placeholder'] ) ) {
252
					$value['placeholder'] = '';
253
				}
254
255
				$value['readonly'] = isset( $value['readonly'] ) && $value['readonly'] == 'yes' ? 'readonly' : '';
256
257
				// Custom attribute handling
258
				$custom_attributes = array();
259
260
				if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) {
261
					foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) {
262
						$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
263
					}
264
				}
265
266
				// Description handling
267
				$description = '';
268
269
				if ( ! empty( $value['desc'] ) ) {
270
					$description = $value['desc'];
271
				}
272
273
				if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ), true ) ) {
274
					$description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
275
				} elseif ( $description && in_array( $value['type'], array( 'checkbox' ), true ) ) {
276
					$description = wp_kses_post( $description );
277
				} elseif ( $description ) {
278
					$description = '<p class="description">' . wp_kses_post( $description ) . '</p>';
279
				}
280
281
				// Switch based on type
282
				switch( $value['type'] ) {
283
284
					// Section Titles
285
					case 'title':
286
						if ( ! empty( $value['title'] ) ) {
287
							echo '<h2>' . esc_html( $value['title'] ) . '</h2>';
288
						}
289
						if ( ! empty( $value['desc'] ) ) {
290
							echo '<div id="' . esc_attr( sanitize_title( $value['id'] ) ) . '-description">';
291
							echo wp_kses_post( wpautop( wptexturize( $value['desc'] ) ) );
292
							echo '</div>';
293
						}
294
						echo '<table class="alnp-table">'."\n\n";
295
						if ( ! empty( $value['id'] ) ) {
296
							do_action( 'auto_load_next_post_settings_' . sanitize_title( $value['id'] ) );
297
						}
298
299
						break;
300
301
					// Section Ends
302
					case 'sectionend':
303 View Code Duplication
						if ( ! empty( $value['id'] ) ) {
304
							do_action( 'auto_load_next_post_settings_' . sanitize_title( $value['id'] ) . '_end' );
305
						}
306
						echo '</table>';
307 View Code Duplication
						if ( ! empty( $value['id'] ) ) {
308
							do_action( 'auto_load_next_post_settings_' . sanitize_title( $value['id'] ) . '_after' );
309
						}
310
						break;
311
312
					// Standard text inputs and subtypes like 'number'
313
					case 'text':
314
					case 'number':
315
					case 'password':
316
					case 'date':
317
					case 'month':
318
					case 'time':
319
					case 'week':
320
					case 'email':
321
						$option_value = self::get_option( $value['id'], $value['default'] );
322
323
						?><tr valign="top">
324
							<th scope="row" class="titledesc">
325
								<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
326
							</th>
327
							<td>
328
								<input
329
									name="<?php echo esc_attr( $value['id'] ); ?>"
330
									id="<?php echo esc_attr( $value['id'] ); ?>"
331
									type="<?php echo esc_attr( $value['type'] ); ?>"
332
									style="<?php echo esc_attr( $value['css'] ); ?>"
333
									value="<?php echo esc_attr( $option_value ); ?>"
334
									class="<?php echo esc_attr( $value['class'] ); ?>"
335
									placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
336
									<?php echo esc_attr( $value['readonly'] ); ?>
337
									<?php echo implode(' ', $custom_attributes ); ?>
338
								/><?php echo $description; ?>
339
							</td>
340
						</tr><?php
341
						break;
342
343
					// Textarea.
344
					case 'textarea':
345
						$option_value = self::get_option( $value['id'], $value['default'] );
346
						?>
347
						<tr valign="top">
348
							<th scope="row" class="titledesc">
349
								<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
350
							</th>
351
							<td>
352
								<?php echo $description; ?>
353
354
								<textarea
355
									name="<?php echo esc_attr( $value['id'] ); ?>"
356
									id="<?php echo esc_attr( $value['id'] ); ?>"
357
									style="<?php echo esc_attr( $value['css'] ); ?>"
358
									class="<?php echo esc_attr( $value['class'] ); ?>"
359
									placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
360
									<?php echo implode( ' ', $custom_attributes ); ?>
361
									><?php echo esc_textarea( $option_value ); ?></textarea>
362
							</td>
363
						</tr>
364
						<?php
365
						break;
366
367
					// Select boxes.
368
					case 'select':
369
					case 'multiselect':
370
						$option_value = self::get_option( $value['id'], $value['default'] );
371
						?>
372
						<tr valign="top">
373
							<th scope="row" class="titledesc">
374
								<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
375
							</th>
376
							<td>
377
								<select
378
									name="<?php echo esc_attr( $value['id'] ); ?><?php echo ( 'multiselect' === $value['type'] ) ? '[]' : ''; ?>"
379
									id="<?php echo esc_attr( $value['id'] ); ?>"
380
									style="<?php echo esc_attr( $value['css'] ); ?>"
381
									class="<?php echo esc_attr( $value['class'] ); ?>"
382
									data-placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"
383
									<?php echo implode( ' ', $custom_attributes ); ?>
384
									<?php echo 'multiselect' === $value['type'] ? 'multiple="multiple"' : ''; ?>
385
									>
386
									<?php
387
									foreach ( $value['options'] as $key => $val ) {
388
										?>
389
										<option value="<?php echo esc_attr( $key ); ?>"
390
											<?php
391
											if ( is_array( $option_value ) ) {
392
												selected( in_array( (string) $key, $option_value, true ), true );
393
											} else {
394
												selected( $option_value, (string) $key );
395
											}
396
											?>
397
											>
398
											<?php echo esc_html( $val ); ?></option>
399
											<?php
400
									}
401
									?>
402
								</select> <?php echo $description; ?>
403
							</td>
404
						</tr>
405
						<?php
406
						break;
407
408
					// Radio inputs.
409
					case 'radio':
410
						$option_value = self::get_option( $value['id'], $value['default'] );
411
						?>
412
						<tr valign="top">
413
							<th scope="row" class="titledesc">
414
								<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
415
							</th>
416
							<td>
417
								<fieldset>
418
									<?php echo $description; ?>
419
									<ul>
420
									<?php
421
									foreach ( $value['options'] as $key => $val ) {
422
										?>
423
										<li>
424
											<label><input
425
													name="<?php echo esc_attr( $value['id'] ); ?>"
426
													value="<?php echo esc_attr( $key ); ?>"
427
													type="radio"
428
													style="<?php echo esc_attr( $value['css'] ); ?>"
429
													class="<?php echo esc_attr( $value['class'] ); ?>"
430
													<?php echo implode( ' ', $custom_attributes ); // WPCS: XSS ok. ?>
431
													<?php checked( $key, $option_value ); ?>
432
													/> <?php echo esc_html( $val ); ?></label>
433
										</li>
434
										<?php
435
									}
436
									?>
437
									</ul>
438
								</fieldset>
439
							</td>
440
						</tr>
441
						<?php
442
						break;
443
444
					// Checkbox input.
445
					case 'checkbox':
446
						$option_value     = self::get_option( $value['id'], $value['default'] );
447
						$visibility_class = array();
448
449
						if ( ! isset( $value['hide_if_checked'] ) ) {
450
							$value['hide_if_checked'] = false;
451
						}
452
						if ( ! isset( $value['show_if_checked'] ) ) {
453
							$value['show_if_checked'] = false;
454
						}
455
						if ( 'yes' === $value['hide_if_checked'] || 'yes' === $value['show_if_checked'] ) {
456
							$visibility_class[] = 'hidden_option';
457
						}
458
						if ( 'option' === $value['hide_if_checked'] ) {
459
							$visibility_class[] = 'hide_options_if_checked';
460
						}
461
						if ( 'option' === $value['show_if_checked'] ) {
462
							$visibility_class[] = 'show_options_if_checked';
463
						}
464
465
						if ( ! isset( $value['checkboxgroup'] ) || 'start' === $value['checkboxgroup'] ) {
466
							?>
467
								<tr valign="top" class="<?php echo esc_attr( implode( ' ', $visibility_class ) ); ?>">
468
									<th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ); ?></th>
469
									<td>
470
										<fieldset>
471
							<?php
472
						} else {
473
							?>
474
								<fieldset class="<?php echo esc_attr( implode( ' ', $visibility_class ) ); ?>">
475
							<?php
476
						}
477
478
						if ( ! empty( $value['title'] ) ) {
479
							?>
480
								<legend class="screen-reader-text"><span><?php echo esc_html( $value['title'] ); ?></span></legend>
481
							<?php
482
						}
483
						?>
484
								<label for="<?php echo esc_attr( $value['id'] ); ?>">
485
									<input
486
										name="<?php echo esc_attr( $value['id'] ); ?>"
487
										id="<?php echo esc_attr( $value['id'] ); ?>"
488
										type="checkbox"
489
										class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>"
490
										value="1"
491
										<?php checked( $option_value, 'yes' ); ?>
492
										<?php echo implode( ' ', $custom_attributes ); ?>
493
									/> <?php echo $description; ?>
494
								</label>
495
								<?php
496
497
								if ( ! isset( $value['checkboxgroup'] ) || 'end' === $value['checkboxgroup'] ) {
498
									?>
499
										</fieldset>
500
									</td>
501
								</tr>
502
							<?php
503
						} else {
504
							?>
505
								</fieldset>
506
							<?php
507
						}
508
						break;
509
510
					case 'button':
511
						if ( isset( $value['url'] ) && ! empty( $value['url'] ) ) {
512
					?>
513
						<tr valign="top">
514
							<th scope="row" class="titledesc"><?php echo $value['title'];?></th>
515
							<td>
516
								<a href="<?php echo $value['url']; ?>" class="button-secondary <?php echo esc_attr( $value['class'] ); ?>">
517
									<?php echo $value['value']; ?>
518
								</a>
519
								<?php echo $description; ?>
520
							</td>
521
						</tr>
522
						<?php
523
						}
524
						break;
525
526
					// Default: run an action
527
					default:
528
						do_action( 'auto_load_next_post_admin_field_' . $value['type'], $value );
529
530
						break;
531
				} // end switch()
532
			} // END foreach()
533
		} // END output_fields()
534
535
		/**
536
		 * Save admin fields.
537
		 *
538
		 * Loops though the plugin options array and outputs each field.
539
		 *
540
		 * @access  public
541
		 * @static
542
		 * @since   1.0.0
543
		 * @version 1.5.0
544
		 * @param   array $options Opens array to output
545
		 * @return  bool
546
		 */
547
		public static function save_fields( $options ) {
548
			if ( empty( $_POST ) ) {
549
				return false;
550
			}
551
552
			// Options to update will be stored here
553
			$update_options   = array();
554
			$autoload_options = array();
555
556
			// Loop options and get values to save
557
			foreach ( $options as $option ) {
558
				if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
559
					continue;
560
				}
561
562
				// Get posted value.
563
				if ( strstr( $option['id'], '[' ) ) {
564
					parse_str( $option['id'], $option_name_array );
565
					$option_name  = current( array_keys( $option_name_array ) );
566
					$setting_name = key( $option_name_array[ $option_name ] );
567
					$raw_value    = isset( $_POST[ $option_name ][ $setting_name ] ) ? wp_unslash( $_POST[ $option_name ][ $setting_name ] ) : null;
568
				} else {
569
					$option_name  = $option['id'];
570
					$setting_name = '';
571
					$raw_value    = isset( $_POST[ $option['id'] ] ) ? wp_unslash( $_POST[ $option['id'] ] ) : null;
572
				}
573
574
				switch ( $option['type'] ) {
575
					case "checkbox" :
576
						$value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no';
577
						break;
578
579
					case "textarea" :
580
						$value = wp_kses_post( trim( stripslashes( $_POST[$option['id']] ) ) );
581
						break;
582
583
					case "multiselect" :
584
						$value = array_filter( array_map( 'auto_load_next_post_clean', (array) $raw_value ) );
585
						break;
586
587
					case 'select' :
588
						$allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) );
589
						if ( empty( $option['default'] ) && empty( $allowed_values ) ) {
590
							$value = null;
591
							break;
592
						}
593
						$default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] );
594
						$value   = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default;
595
						break;
596
597
					default :
598
						$value = auto_load_next_post_clean( $raw_value );
599
						break;
600
				} // END switch()
601
602
				/**
603
				 * Fire an action when a certain 'type' of field is being saved.
604
				 *
605
				 * @deprecated 1.5.0 - doesn't allow manipulation of values!
606
				 */
607
				if ( has_action( 'auto_load_next_post_update_option_' . sanitize_title( $option['type'] ) ) ) {
608
					if ( is_ajax() ) {
609
						error_log( 'auto_load_next_post_update_option_' . sanitize_title( $option['type'] ) . ' is deprecated since version 1.5.0' );
610
					} else {
611
						_deprecated_hook( 'auto_load_next_post_update_option_' . sanitize_title( $option['type'] ), '1.5.0' );
612
					}
613
614
					do_action( 'auto_load_next_post_update_option_' . sanitize_title( $option['type'] ), $option );
615
					continue;
616
				}
617
618
				if ( is_null( $value ) ) {
619
					continue;
620
				}
621
622
				// Check if option is an array and handle that differently to single values.
623
				if ( $option_name && $setting_name ) {
624
					if ( ! isset( $update_options[ $option_name ] ) ) {
625
						$update_options[ $option_name ] = get_option( $option_name, array() );
626
					}
627
					if ( ! is_array( $update_options[ $option_name ] ) ) {
628
						$update_options[ $option_name ] = array();
629
					}
630
					$update_options[ $option_name ][ $setting_name ] = $value;
631
				} else {
632
					$update_options[ $option_name ] = $value;
633
				}
634
635
				$autoload_options[ $option_name ] = isset( $option['autoload'] ) ? (bool) $option['autoload'] : true;
636
637
				/**
638
				 * Fire an action before saved.
639
				 *
640
				 * @deprecated 1.5.0 - doesn't allow manipulation of values!
641
				 */
642
				if ( has_action( 'auto_load_next_post_update_option' ) ) {
643
					if ( is_ajax() ) {
644
						error_log( 'auto_load_next_post_update_option is deprecated since version 1.5.0' );
645
					} else {
646
						_deprecated_hook( 'auto_load_next_post_update_option', '1.5.0' );
647
					}
648
649
					do_action( 'auto_load_next_post_update_option', $option );
650
				}
651
			}
652
653
			// Now save the options
654
			foreach ( $update_options as $name => $value ) {
655
				update_option( $name, $value, $autoload_options[ $name ] ? 'yes' : 'no' );
656
			}
657
658
			// Save all options as an array. Ready for export.
659
			update_option( 'auto_load_next_post_options', $update_options );
660
661
			return true;
662
		} // END save_fields()
663
664
	} // END class.
665
666
} // END if class exists.
667