Completed
Pull Request — master (#131)
by
unknown
01:57
created

Auto_Load_Next_Post_Admin_Settings::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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