Completed
Pull Request — master (#1412)
by Ravinder
17:25
created

Give_Admin_Settings::get_option()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 12
nc 7
nop 3
dl 0
loc 23
rs 6.1403
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Give Admin Settings Class
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Admin_Settings
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.8
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
if ( ! class_exists( 'Give_Admin_Settings' ) ) :
17
18
	/**
19
	 * Give_Admin_Settings Class.
20
	 *
21
	 * @since 1.8
22
	 */
23
	class Give_Admin_Settings {
24
25
		/**
26
		 * Setting pages.
27
		 *
28
		 * @since 1.8
29
		 * @var   array List of settings.
30
		 */
31
		private static $settings = array();
32
33
		/**
34
		 * Setting filter and action prefix.
35
		 *
36
		 * @since 1.8
37
		 * @var   string setting fileter and action anme prefix.
38
		 */
39
		private static $setting_filter_prefix = '';
40
41
		/**
42
		 * Error messages.
43
		 *
44
		 * @since 1.8
45
		 * @var   array List of errors.
46
		 */
47
		private static $errors = array();
48
49
		/**
50
		 * Update messages.
51
		 *
52
		 * @since 1.8
53
		 * @var   array List of messages.
54
		 */
55
		private static $messages = array();
56
57
		/**
58
		 * Include the settings page classes.
59
		 *
60
		 * @since  1.8
61
		 * @return array
62
		 */
63
		public static function get_settings_pages() {
64
			/**
65
			 * Filter the setting page.
66
			 *
67
			 * Note: filter dynamically fire on basis of setting page slug.
68
			 * For example: if you register a setting page with give-settings menu slug
69
			 *              then filter will be give-settings_get_settings_pages
70
			 *
71
			 * @since 1.8
72
			 *
73
			 * @param array $settings Array of settings class object.
74
			 */
75
			self::$settings = apply_filters( self::$setting_filter_prefix . '_get_settings_pages', array() );
76
77
			return self::$settings;
78
		}
79
80
		/**
81
		 * Save the settings.
82
		 *
83
		 * @since  1.8
84
		 * @return void
85
		 */
86
		public static function save() {
87
			$current_tab = give_get_current_setting_tab();
88
89
			if ( empty( $_REQUEST['_give-save-settings'] ) || ! wp_verify_nonce( $_REQUEST['_give-save-settings'], 'give-save-settings' ) ) {
90
				die( __( 'Action failed. Please refresh the page and retry.', 'give' ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method save() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
91
			}
92
93
			/**
94
			 * Trigger Action.
95
			 *
96
			 * Note: action dynamically fire on basis of setting page slug and current tab.
97
			 * For example: if you register a setting page with give-settings menu slug and general current tab name
98
			 *              then action will be give-settings_save_general
99
			 *
100
			 * @since 1.8
101
			 */
102
			do_action( self::$setting_filter_prefix . '_save_' . $current_tab );
103
104
			self::add_message( 'give-setting-updated', __( 'Your settings have been saved.', 'give' ) );
105
106
			/**
107
			 * Trigger Action.
108
			 *
109
			 * Note: action dynamically fire on basis of setting page slug.
110
			 * For example: if you register a setting page with give-settings menu slug
111
			 *              then action will be give-settings_saved
112
			 *
113
			 * @since 1.8
114
			 */
115
			do_action( self::$setting_filter_prefix . '_saved' );
116
		}
117
118
		/**
119
		 * Add a message.
120
		 *
121
		 * @since  1.8
122
		 *
123
		 * @param  string $code    Message code (Note: This should be unique).
124
		 * @param  string $message Message text.
125
		 *
126
		 * @return void
127
		 */
128
		public static function add_message( $code, $message ) {
129
			self::$messages[ $code ] = $message;
130
		}
131
132
		/**
133
		 * Add an error.
134
		 *
135
		 * @since  1.8
136
		 *
137
		 * @param  string $code    Message code (Note: This should be unique).
138
		 * @param  string $message Message text.
139
		 *
140
		 * @return void
141
		 */
142
		public static function add_error( $code, $message ) {
143
			self::$errors[ $code ] = $message;
144
		}
145
146
		/**
147
		 * Output messages + errors.
148
		 *
149
		 * @since  1.8
150
		 * @return void
151
		 */
152
		public static function show_messages() {
153
			$notice_html = '';
154
			$classes     = 'give-notice settings-error notice is-dismissible';
155
156
			self::$errors   = apply_filters( self::$setting_filter_prefix . '_error_notices', self::$errors );
157
			self::$messages = apply_filters( self::$setting_filter_prefix . '_update_notices', self::$messages );
158
159
			if ( 0 < count( self::$errors ) ) {
160
				foreach ( self::$errors as $code => $message ) {
161
					$notice_html .= '<div id="setting-error-' . $code . '" class="' . $classes . ' error"><p><strong>' . $message . '</strong></p></div>';
162
				}
163
			}
164
165
			if ( 0 < count( self::$messages ) ) {
166
				foreach ( self::$messages as $code => $message ) {
167
					$notice_html .= '<div id="setting-error-' . $code . '" class="' . $classes . ' updated"><p><strong>' . $message . '</strong></p></div>';
168
				}
169
			}
170
171
			echo $notice_html;
172
		}
173
174
		/**
175
		 * Settings page.
176
		 *
177
		 * Handles the display of the main give settings page in admin.
178
		 *
179
		 * @since  1.8
180
		 * @return void|bool
181
		 */
182
		public static function output() {
183
			// Get current setting page.
184
			self::$setting_filter_prefix = give_get_current_setting_page();
185
186
			// Bailout: Exit if setting page is not defined.
187
			if ( empty( self::$setting_filter_prefix ) ) {
188
				return false;
189
			}
190
191
			/**
192
			 * Trigger Action.
193
			 *
194
			 * Note: action dynamically fire on basis of setting page slug
195
			 * For example: if you register a setting page with give-settings menu slug
196
			 *              then action will be give-settings_start
197
			 *
198
			 * @since 1.8
199
			 */
200
			do_action( self::$setting_filter_prefix . '_start' );
201
202
			$current_tab = give_get_current_setting_tab();
203
204
			// Include settings pages.
205
			self::get_settings_pages();
206
207
			// Save settings if data has been posted.
208
			if ( ! empty( $_POST ) ) {
209
				self::save();
210
			}
211
212
			/**
213
			 * Filter the tabs for current setting page.
214
			 *
215
			 * Note: filter dynamically fire on basis of setting page slug.
216
			 * For example: if you register a setting page with give-settings menu slug and general current tab name
217
			 *              then action will be give-settings_tabs_array
218
			 *
219
			 * @since 1.8
220
			 */
221
			$tabs = apply_filters( self::$setting_filter_prefix . '_tabs_array', array() );
222
223
			include 'views/html-admin-settings.php';
224
225
			return true;
226
		}
227
228
		/**
229
		 * Get a setting from the settings API.
230
		 *
231
		 * @since  1.8
232
		 *
233
		 * @param  string $option_name
234
		 * @param  string $field_id
235
		 * @param  mixed  $default
236
		 *
237
		 * @return string|bool
238
		 */
239
		public static function get_option( $option_name = '', $field_id = '', $default = false ) {
240
			// Bailout.
241
			if ( empty( $option_name ) && empty( $field_id ) ) {
242
				return false;
243
			}
244
245
			if ( ! empty( $field_id ) && ! empty( $option_name ) ) {
246
				// Get field value if any.
247
				$option_value = get_option( $option_name );
248
249
				$option_value = ( is_array( $option_value ) && array_key_exists( $field_id, $option_value ) )
250
					? $option_value[ $field_id ]
251
					: $default;
252
			} else {
253
				// If option name is empty but not field name then this means, setting is direct store to option table under there field name.
254
				$option_name = ! $option_name ? $field_id : $option_name;
255
256
				// Get option value if any.
257
				$option_value = get_option( $option_name, $default );
258
			}
259
260
			return $option_value;
261
		}
262
263
		/**
264
		 * Output admin fields.
265
		 *
266
		 * Loops though the give options array and outputs each field.
267
		 *
268
		 * @since  1.8
269
		 *
270
		 * @param  array  $options     Opens array to output
271
		 * @param  string $option_name Opens array to output
272
		 *
273
		 * @return void
274
		 */
275
		public static function output_fields( $options, $option_name = '' ) {
276
			$current_tab = give_get_current_setting_tab();
277
278
			// Field Default values.
279
			$defaults = array(
280
				'id'         => '',
281
				'class'      => '',
282
				'css'        => '',
283
				'default'    => '',
284
				'desc'       => '',
285
				'table_html' => true,
286
			);
287
288
			foreach ( $options as $value ) {
289
				if ( ! isset( $value['type'] ) ) {
290
					continue;
291
				}
292
293
				// Set title.
294
				$defaults['title'] = isset( $value['name'] ) ? $value['name'] : '';
295
296
				$value = wp_parse_args( $value, $defaults );
297
298
				// Custom attribute handling.
299
				$custom_attributes = array();
300
301
				if ( ! empty( $value['attributes'] ) && is_array( $value['attributes'] ) ) {
302
					foreach ( $value['attributes'] as $attribute => $attribute_value ) {
303
						$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
304
					}
305
				}
306
307
				// Description handling.
308
				$description          = self::get_field_description( $value );
309
310
				// Switch based on type.
311
				switch ( $value['type'] ) {
312
313
					// Section Titles
314
					case 'title':
315
						if ( ! empty( $value['title'] ) ) {
316
							echo '<div class="give-setting-tab-header give-setting-tab-header-' . $current_tab . '"><h2>' . self::get_field_title( $value ) . '</h2><hr></div>';
317
						}
318
319
						if ( ! empty( $value['desc'] ) ) {
320
							echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) );
321
						}
322
323
						if ( $value['table_html'] ) {
324
							echo '<table class="form-table give-setting-tab-body give-setting-tab-body-' . $current_tab . '">' . "\n\n";
325
						}
326
327
						if ( ! empty( $value['id'] ) ) {
328
329
							/**
330
							 * Trigger Action.
331
							 *
332
							 * Note: action dynamically fire on basis of field id.
333
							 *
334
							 * @since 1.8
335
							 */
336
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) );
337
						}
338
339
						break;
340
341
					// Section Ends.
342
					case 'sectionend':
343
						if ( ! empty( $value['id'] ) ) {
344
345
							/**
346
							 * Trigger Action.
347
							 *
348
							 * Note: action dynamically fire on basis of field id.
349
							 *
350
							 * @since 1.8
351
							 */
352
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_end' );
353
						}
354
355
						if ( $value['table_html'] ) {
356
							echo '</table>';
357
						}
358
359
						if ( ! empty( $value['id'] ) ) {
360
361
							/**
362
							 * Trigger Action.
363
							 *
364
							 * Note: action dynamically fire on basis of field id.
365
							 *
366
							 * @since 1.8
367
							 */
368
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_after' );
369
						}
370
371
						break;
372
373
					// Standard text inputs and subtypes like 'number'.
374
					case 'text':
375
					case 'email':
376
					case 'number':
377
					case 'password' :
378
379
						$type = $value['type'];
380
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
381
382
						?>
383
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
384
						<th scope="row" class="titledesc">
385
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
386
						</th>
387
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
388
							<input
389
								name="<?php echo esc_attr( $value['id'] ); ?>"
390
								id="<?php echo esc_attr( $value['id'] ); ?>"
391
								type="<?php echo esc_attr( $type ); ?>"
392
								style="<?php echo esc_attr( $value['css'] ); ?>"
393
								value="<?php echo esc_attr( $option_value ); ?>"
394
								class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?>"
395
								<?php echo implode( ' ', $custom_attributes ); ?>
396
							/> <?php echo $description; ?>
397
						</td>
398
						</tr><?php
399
						break;
400
401
					// Textarea.
402
					case 'textarea':
403
404
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
405
406
						?>
407
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
408
						<th scope="row" class="titledesc">
409
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
410
						</th>
411
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
412
								<textarea
413
									name="<?php echo esc_attr( $value['id'] ); ?>"
414
									id="<?php echo esc_attr( $value['id'] ); ?>"
415
									style="<?php echo esc_attr( $value['css'] ); ?>"
416
									class="<?php echo esc_attr( $value['class'] ); ?>"
417
									rows="10"
418
									cols="60"
419
									<?php echo implode( ' ', $custom_attributes ); ?>
420
								><?php echo esc_textarea( $option_value ); ?></textarea>
421
							<?php echo $description; ?>
422
						</td>
423
						</tr><?php
424
						break;
425
426
					// Select boxes.
427
					case 'select' :
428
					case 'multiselect' :
429
430
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
431
432
						?>
433
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
434
						<th scope="row" class="titledesc">
435
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
436
						</th>
437
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
438
							<select
439
								name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) {
440
									echo '[]';
441
								} ?>"
442
								id="<?php echo esc_attr( $value['id'] ); ?>"
443
								style="<?php echo esc_attr( $value['css'] ); ?>"
444
								class="<?php echo esc_attr( $value['class'] ); ?>"
445
								<?php echo implode( ' ', $custom_attributes ); ?>
446
								<?php echo ( 'multiselect' == $value['type'] ) ? 'multiple="multiple"' : ''; ?>
447
							>
448
449
								<?php
450
								if ( ! empty( $value['options'] ) ) {
451
									foreach ( $value['options'] as $key => $val ) {
452
										?>
453
										<option value="<?php echo esc_attr( $key ); ?>" <?php
454
455
										if ( is_array( $option_value ) ) {
456
											selected( in_array( $key, $option_value ), true );
457
										} else {
458
											selected( $option_value, $key );
459
										}
460
461
										?>><?php echo $val ?></option>
462
										<?php
463
									}
464
								}
465
								?>
466
467
							</select> <?php echo $description; ?>
468
						</td>
469
						</tr><?php
470
						break;
471
472
					// Radio inputs.
473
					case 'radio_inline' :
474
						$value['class'] = empty( $value['class'] ) ? 'give-radio-inline' : $value['class'] . ' give-radio-inline';
475
					case 'radio' :
476
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
477
						?>
478
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
479
						<th scope="row" class="titledesc">
480
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
481
						</th>
482
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>">
483
							<fieldset>
484
								<ul>
485
									<?php
486
									foreach ( $value['options'] as $key => $val ) {
487
										?>
488
										<li>
489
											<label><input
490
													name="<?php echo esc_attr( $value['id'] ); ?>"
491
													value="<?php echo $key; ?>"
492
													type="radio"
493
													style="<?php echo esc_attr( $value['css'] ); ?>"
494
													<?php echo implode( ' ', $custom_attributes ); ?>
495
													<?php checked( $key, $option_value ); ?>
496
												/> <?php echo $val ?></label>
497
										</li>
498
										<?php
499
									}
500
									?>
501
									<?php echo $description; ?>
502
							</fieldset>
503
						</td>
504
						</tr><?php
505
						break;
506
507
					// Checkbox input.
508
					case 'checkbox' :
509
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
510
						?>
511
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
512
							<th scope="row" class="titledesc">
513
								<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
514
							</th>
515
							<td class="give-forminp">
516
								<input
517
									name="<?php echo esc_attr( $value['id'] ); ?>"
518
									id="<?php echo esc_attr( $value['id'] ); ?>"
519
									type="checkbox"
520
									class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>"
521
									value="1"
522
									<?php checked( $option_value, 'on' ); ?>
523
									<?php echo implode( ' ', $custom_attributes ); ?>
524
								/>
525
								<?php echo $description; ?>
526
							</td>
527
						</tr>
528
						<?php
529
						break;
530
531
					// Multi Checkbox input.
532
					case 'multicheck' :
533
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
534
						$option_value = is_array( $option_value ) ? $option_value : array();
535
						?>
536
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
537
							<th scope="row" class="titledesc">
538
								<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
539
							</th>
540
							<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>">
541
								<fieldset>
542
									<ul>
543
										<?php
544
										foreach ( $value['options'] as $key => $val ) {
545
											?>
546
											<li>
547
												<label>
548
													<input
549
														name="<?php echo esc_attr( $value['id'] ); ?>[]"
550
														value="<?php echo $key; ?>"
551
														type="checkbox"
552
														style="<?php echo esc_attr( $value['css'] ); ?>"
553
														<?php echo implode( ' ', $custom_attributes ); ?>
554
														<?php if ( in_array( $key, $option_value ) ) {
555
															echo 'checked="checked"';
556
														} ?>
557
													/> <?php echo $val ?>
558
												</label>
559
											</li>
560
											<?php
561
										}
562
										?>
563
										<?php echo $description; ?>
564
								</fieldset>
565
							</td>
566
						</tr>
567
						<?php
568
						break;
569
570
					// File input field.
571
					case 'file' :
572
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
573
						?>
574
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
575
						<th scope="row" class="titledesc">
576
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
577
						</th>
578
						<td class="give-forminp">
579
							<div class="give-field-wrap">
580
								<label for="<?php echo $value['id'] ?>">
581
									<input
582
										name="<?php echo esc_attr( $value['id'] ); ?>"
583
										id="<?php echo esc_attr( $value['id'] ); ?>"
584
										type="text"
585
										class="give-input-field<?php echo esc_attr( isset( $value['class'] ) ? ' ' . $value['class'] : '' ); ?>"
586
										value="<?php echo $option_value; ?>"
587
										style="<?php echo esc_attr( $value['css'] ); ?>"
588
										<?php echo implode( ' ', $custom_attributes ); ?>
589
									/>&nbsp;&nbsp;&nbsp;&nbsp;<input class="give-upload-button button" type="button" value="<?php echo esc_html__( 'Add or Upload File', 'give' ); ?>">
590
									<?php echo $description ?>
591
									<div class="give-image-thumb<?php echo ! $option_value ? ' give-hidden' : ''; ?>">
592
										<span class="give-delete-image-thumb dashicons dashicons-no-alt"></span>
593
										<img src="<?php echo $option_value; ?>" alt="">
594
									</div>
595
								</label>
596
							</div>
597
						</td>
598
						</tr><?php
599
						break;
600
601
					// WordPress Editor.
602
					case 'wysiwyg' :
603
						// Get option value.
604
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
605
606
						// Get editor settings.
607
						$editor_settings = ! empty( $value['options'] ) ? $value['options'] : array();
608
						?>
609
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
610
						<th scope="row" class="titledesc">
611
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
612
						</th>
613
						<td class="give-forminp">
614
							<?php wp_editor( $option_value, $value['id'], $editor_settings ); ?>
615
							<?php echo $description; ?>
616
						</td>
617
						</tr><?php
618
						break;
619
620
					// Custom: System setting field.
621
					case 'system_info' :
622
						?>
623
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
624
						<th scope="row" class="titledesc">
625
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
626
						</th>
627
						<td class="give-forminp">
628
							<?php give_system_info_callback(); ?>
629
							<?php echo $description; ?>
630
						</td>
631
						</tr><?php
632
						break;
633
634
					// Custom: Default gateways setting field.
635
					case 'default_gateway' :
636
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
637
						?>
638
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
639
						<th scope="row" class="titledesc">
640
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
641
						</th>
642
						<td class="give-forminp">
643
							<?php give_default_gateway_callback( $value, $option_value ); ?>
644
							<?php echo $description; ?>
645
						</td>
646
						</tr><?php
647
						break;
648
649
					// Custom: Enable gateways setting field.
650
					case 'enabled_gateways' :
651
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
652
						?>
653
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
654
						<th scope="row" class="titledesc">
655
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
656
						</th>
657
						<td class="give-forminp">
658
							<?php give_enabled_gateways_callback( $value, $option_value ); ?>
659
							<?php echo $description; ?>
660
						</td>
661
						</tr><?php
662
						break;
663
664
					// Custom: Email preview buttons field.
665
					case 'email_preview_buttons' :
666
						?>
667
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
668
						<th scope="row" class="titledesc">
669
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
670
						</th>
671
						<td class="give-forminp">
672
							<?php give_email_preview_buttons_callback(); ?>
673
							<?php echo $description; ?>
674
						</td>
675
						</tr><?php
676
						break;
677
678
					// Custom: API field.
679
					case 'api' :
680
						give_api_callback();
681
						echo $description;
682
						break;
683
684
					// Custom: Log field.
685
					case 'logs' :
686
						// Note: there are no need to check for html field param because we want custom html to this field.
687
						give_reports_tab_logs();
688
						echo $description;
689
						break;
690
691
					// Custom: API field.
692
					case 'data' :
693
						give_tools_recount_stats_display();
694
						echo $description;
695
						break;
696
697
					// Custom: Give Docs Link field type.
698
					case 'give_docs_link' :
699
						?>
700
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
701
						<td class="give-docs-link" colspan="2">
702
						<?php
703
							echo '<p class="give-docs-link"><a href="' . esc_url( $value['url'] )
704
							. '" target="_blank">'
705
							. sprintf( esc_html__( 'Need Help? See docs on "%s"' ), $value['title'] )
706
							. '<span class="dashicons dashicons-editor-help"></span></a></p>';
707
						?>
708
						</td>
709
						</tr><?php
710
						break;
711
712
					// Default: run an action
713
					// You can add or handle your custom field action.
714
					default:
715
						// Get option value.
716
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
717
						do_action( 'give_admin_field_' . $value['type'], $value, $option_value );
718
						break;
719
				}
720
			}
721
		}
722
723
		/**
724
		 * Helper function to get the formated description for a given form field.
725
		 * Plugins can call this when implementing their own custom settings types.
726
		 *
727
		 * @since  1.8
728
		 *
729
		 * @param  array $value The form field value array
730
		 *
731
		 * @return array The description and tip as a 2 element array
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
732
		 */
733
		public static function get_field_description( $value ) {
734
			$description = '';
735
736
			if ( ! empty( $value['desc'] ) ) {
737
				$description = '<p class="give-field-description">' . wp_kses_post( $value['desc'] ) . '</p>';
738
			}
739
740
			return $description;
741
		}
742
743
744
		/**
745
		 * Helper function to get the formated title.
746
		 * Plugins can call this when implementing their own custom settings types.
747
		 *
748
		 * @since  1.8
749
		 *
750
		 * @param  array $value The form field value array
751
		 *
752
		 * @return array The description and tip as a 2 element array
753
		 */
754
		public static function get_field_title( $value ) {
755
			$title = esc_html( $value['title'] );
756
757
			// If html tag detected then allow them to print.
758
			if ( strip_tags( $title ) ) {
759
				$title = $value['title'];
760
			}
761
762
			return $title;
763
		}
764
765
		/**
766
		 * Save admin fields.
767
		 *
768
		 * Loops though the give options array and outputs each field.
769
		 *
770
		 * @since  1.8
771
		 *
772
		 * @param  array  $options     Options array to output
773
		 * @param  string $option_name Option name to save output. If empty then option will be store in there own option name i.e option id.
774
		 *
775
		 * @return bool
776
		 */
777
		public static function save_fields( $options, $option_name = '' ) {
778
			if ( empty( $_POST ) ) {
779
				return false;
780
			}
781
782
			// Options to update will be stored here and saved later.
783
			$update_options = array();
784
785
			// Loop options and get values to save.
786
			foreach ( $options as $option ) {
787
				if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
788
					continue;
789
				}
790
791
				// Get posted value.
792
				if ( strstr( $option['id'], '[' ) ) {
793
					parse_str( $option['id'], $option_name_array );
794
					$field_option_name = current( array_keys( $option_name_array ) );
795
					$setting_name      = key( $option_name_array[ $field_option_name ] );
796
					$raw_value         = isset( $_POST[ $field_option_name ][ $setting_name ] ) ? wp_unslash( $_POST[ $field_option_name ][ $setting_name ] ) : null;
797
				} else {
798
					$field_option_name = $option['id'];
799
					$setting_name      = '';
800
					$raw_value         = isset( $_POST[ $option['id'] ] ) ? wp_unslash( $_POST[ $option['id'] ] ) : null;
801
				}
802
803
				// Format the value based on option type.
804
				switch ( $option['type'] ) {
805
					case 'checkbox' :
806
						$value = is_null( $raw_value ) ? '' : 'on';
807
						break;
808
					case 'wysiwyg'  :
809
					case 'textarea' :
810
						$value = wp_kses_post( trim( $raw_value ) );
811
						break;
812
					case 'multiselect' :
813
						$value = array_filter( array_map( 'give_clean', (array) $raw_value ) );
814
						break;
815
					default :
816
						$value = give_clean( $raw_value );
817
						break;
818
				}
819
820
				/**
821
				 * Sanitize the value of an option.
822
				 *
823
				 * @since 1.8
824
				 */
825
				$value = apply_filters( 'give_admin_settings_sanitize_option', $value, $option, $raw_value );
826
827
				/**
828
				 * Sanitize the value of an option by option name.
829
				 *
830
				 * @since 1.8
831
				 */
832
				$value = apply_filters( "give_admin_settings_sanitize_option_{$field_option_name}", $value, $option, $raw_value );
833
834
				if ( is_null( $value ) ) {
835
					continue;
836
				}
837
838
				// Check if option is an array and handle that differently to single values.
839
				if ( $field_option_name && $setting_name ) {
840
					if ( ! isset( $update_options[ $field_option_name ] ) ) {
841
						$update_options[ $field_option_name ] = get_option( $field_option_name, array() );
842
					}
843
					if ( ! is_array( $update_options[ $field_option_name ] ) ) {
844
						$update_options[ $field_option_name ] = array();
845
					}
846
					$update_options[ $field_option_name ][ $setting_name ] = $value;
847
				} else {
848
					$update_options[ $field_option_name ] = $value;
849
				}
850
			}
851
852
			// Save all options in our array or there own option name i.e. option id.
853
			if ( empty( $option_name ) ) {
854
				foreach ( $update_options as $name => $value ) {
855
					update_option( $name, $value );
856
857
					/**
858
					 * Trigger action.
859
					 *
860
					 * Note: This is dynamically fire on basis of option name.
861
					 *
862
					 * @since 1.8
863
					 */
864
					do_action( "give_save_option_{$name}", $value, $name );
865
				}
866
			} else {
867
				$old_options    = ( $old_options = get_option( $option_name ) ) ? $old_options : array();
868
				$update_options = array_merge( $old_options, $update_options );
869
870
				update_option( $option_name, $update_options );
871
872
				/**
873
				 * Trigger action.
874
				 *
875
				 * Note: This is dynamically fire on basis of setting name.
876
				 *
877
				 * @since 1.8
878
				 */
879
				do_action( "give_save_settings_{$option_name}", $update_options, $option_name );
880
			}
881
882
			return true;
883
		}
884
	}
885
886
endif;
887