Completed
Push — issues/3312 ( 6b1a83 )
by Ravinder
1313:55 queued 1307:48
created

Give_Admin_Settings::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
1
<?php
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
		 * Verify admin setting nonce
82
		 *
83
		 * @since  1.8.14
84
		 * @access public
85
		 *
86
		 * @return bool
87
		 */
88
		public static function verify_nonce() {
89
			if ( empty( $_REQUEST['_give-save-settings'] ) || ! wp_verify_nonce( $_REQUEST['_give-save-settings'], 'give-save-settings' ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
90
				return false;
91
			}
92
93
			return true;
94
		}
95
96
		/**
97
		 * Save the settings.
98
		 *
99
		 * @since  1.8
100
		 * @return void
101
		 */
102
		public static function save() {
103
			$current_tab = give_get_current_setting_tab();
104
105
			if ( ! self::verify_nonce() ) {
106
				echo '<div class="notice error"><p>' . esc_attr__( 'Action failed. Please refresh the page and retry.', 'give' ) . '</p></div>';
107
				die();
108
			}
109
110
			/**
111
			 * Trigger Action.
112
			 *
113
			 * Note: action dynamically fire on basis of setting page slug and current tab.
114
			 * For example: if you register a setting page with give-settings menu slug and general current tab name
115
			 *              then action will be give-settings_save_general
116
			 *
117
			 * @since 1.8
118
			 */
119
			do_action( self::$setting_filter_prefix . '_save_' . $current_tab );
120
121
			self::add_message( 'give-setting-updated', __( 'Your settings have been saved.', 'give' ) );
122
123
			/**
124
			 * Trigger Action.
125
			 *
126
			 * Note: action dynamically fire on basis of setting page slug.
127
			 * For example: if you register a setting page with give-settings menu slug
128
			 *              then action will be give-settings_saved
129
			 *
130
			 * @since 1.8
131
			 */
132
			do_action( self::$setting_filter_prefix . '_saved' );
133
		}
134
135
		/**
136
		 * Add a message.
137
		 *
138
		 * @since  1.8
139
		 *
140
		 * @param  string $code    Message code (Note: This should be unique).
141
		 * @param  string $message Message text.
142
		 *
143
		 * @return void
144
		 */
145
		public static function add_message( $code, $message ) {
146
			self::$messages[ $code ] = $message;
147
		}
148
149
		/**
150
		 * Add an error.
151
		 *
152
		 * @since  1.8
153
		 *
154
		 * @param  string $code    Message code (Note: This should be unique).
155
		 * @param  string $message Message text.
156
		 *
157
		 * @return void
158
		 */
159
		public static function add_error( $code, $message ) {
160
			self::$errors[ $code ] = $message;
161
		}
162
163
		/**
164
		 * Output messages + errors.
165
		 *
166
		 * @since  1.8
167
		 * @return void
168
		 */
169
		public static function show_messages() {
170
			$notice_html = '';
171
			$classes     = 'give-notice settings-error notice is-dismissible';
172
173
			self::$errors   = apply_filters( self::$setting_filter_prefix . '_error_notices', self::$errors );
174
			self::$messages = apply_filters( self::$setting_filter_prefix . '_update_notices', self::$messages );
175
176
			if ( 0 < count( self::$errors ) ) {
177
				foreach ( self::$errors as $code => $message ) {
178
					$notice_html .= sprintf(
179
						'<div id="setting-error-%1$s" class="%2$s error" style="display: none"><p><strong>%3$s</strong></p></div>',
180
						$code,
181
						$classes,
182
						$message
183
					);
184
				}
185
			}
186
187
			if ( 0 < count( self::$messages ) ) {
188
				foreach ( self::$messages as $code => $message ) {
189
					$notice_html .= sprintf(
190
						'<div id="setting-error-%1$s" class="%2$s updated" style="display: none"><p><strong>%3$s</strong></p></div>',
191
						$code,
192
						$classes,
193
						$message
194
					);
195
				}
196
			}
197
198
			echo $notice_html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$notice_html'
Loading history...
199
		}
200
201
		/**
202
		 * Settings page.
203
		 *
204
		 * Handles the display of the main give settings page in admin.
205
		 *
206
		 * @since  1.8
207
		 * @return bool
208
		 */
209
		public static function output() {
210
			// Get current setting page.
211
			self::$setting_filter_prefix = give_get_current_setting_page();
212
213
			// Bailout: Exit if setting page is not defined.
214
			if ( empty( self::$setting_filter_prefix ) ) {
215
				return false;
216
			}
217
218
			/**
219
			 * Trigger Action.
220
			 *
221
			 * Note: action dynamically fire on basis of setting page slug
222
			 * For example: if you register a setting page with give-settings menu slug
223
			 *              then action will be give-settings_start
224
			 *
225
			 * @since 1.8
226
			 */
227
			do_action( self::$setting_filter_prefix . '_start' );
228
229
			$current_tab = give_get_current_setting_tab();
230
231
			// Include settings pages.
232
			$all_setting = self::get_settings_pages();
233
234
			/* @var object $current_setting_obj */
235
			$current_setting_obj = new StdClass;
236
237
			foreach ( $all_setting as $setting ) {
238
				if (
239
					method_exists( $setting, 'get_id' ) &&
240
					$current_tab === $setting->get_id()
241
				) {
242
					$current_setting_obj = $setting;
243
					break;
244
				}
245
			}
246
247
			// Save settings if data has been posted.
248
			if ( isset( $_POST['_give-save-settings'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
249
				self::save();
250
			}
251
252
			/**
253
			 * Filter the tabs for current setting page.
254
			 *
255
			 * Note: filter dynamically fire on basis of setting page slug.
256
			 * For example: if you register a setting page with give-settings menu slug and general current tab name
257
			 *              then action will be give-settings_tabs_array
258
			 *
259
			 * @since 1.8
260
			 */
261
			$tabs = apply_filters( self::$setting_filter_prefix . '_tabs_array', array() );
262
263
			include 'views/html-admin-settings.php';
264
265
			return true;
266
		}
267
268
		/**
269
		 * Get a setting from the settings API.
270
		 *
271
		 * @since  1.8
272
		 *
273
		 * @param string $option_name Option Name.
274
		 * @param string $field_id    Field ID.
275
		 * @param mixed  $default     Default.
276
		 *
277
		 * @return string|bool
278
		 */
279
		public static function get_option( $option_name = '', $field_id = '', $default = false ) {
280
			// Bailout.
281
			if ( empty( $option_name ) && empty( $field_id ) ) {
282
				return false;
283
			}
284
285
			if ( ! empty( $field_id ) && ! empty( $option_name ) ) {
286
				// Get field value if any.
287
				$option_value = get_option( $option_name );
288
289
				$option_value = ( is_array( $option_value ) && array_key_exists( $field_id, $option_value ) )
290
					? $option_value[ $field_id ]
291
					: $default;
292
			} else {
293
				// If option name is empty but not field name then this means, setting is direct store to option table under there field name.
294
				$option_name = ! $option_name ? $field_id : $option_name;
295
296
				// Get option value if any.
297
				$option_value = get_option( $option_name, $default );
298
			}
299
300
			return $option_value;
301
		}
302
303
		/**
304
		 * Output admin fields.
305
		 *
306
		 * Loops though the give options array and outputs each field.
307
		 *
308
		 * @todo   : Refactor this function
309
		 * @since  1.8
310
		 *
311
		 * @param  array  $options     Opens array to output.
312
		 * @param  string $option_name Opens array to output.
313
		 *
314
		 * @return void
315
		 */
316
		public static function output_fields( $options, $option_name = '' ) {
317
			$current_tab = give_get_current_setting_tab();
318
319
			// Field Default values.
320
			$defaults = array(
321
				'id'                  => '',
322
				'class'               => '',
323
				'css'                 => '',
324
				'default'             => '',
325
				'desc'                => '',
326
				'table_html'          => true,
327
				'repeat'              => false,
328
				'repeat_btn_title'    => __( 'Add Field', 'give' ),
329
			);
330
331
			foreach ( $options as $value ) {
332
				if ( ! isset( $value['type'] ) ) {
333
					continue;
334
				}
335
336
				// Set title.
337
				$defaults['title'] = isset( $value['name'] ) ? $value['name'] : '';
338
339
				// Set default setting.
340
				$value = wp_parse_args( $value, $defaults );
341
342
				// Colorpicker field.
343
				$value['class'] = ( 'colorpicker' === $value['type'] ? trim( $value['class'] ) . ' give-colorpicker' : $value['class'] );
344
				$value['type']  = ( 'colorpicker' === $value['type'] ? 'text' : $value['type'] );
345
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
346
347
				// Custom attribute handling.
348
				$custom_attributes = array();
349
350 View Code Duplication
				if ( ! empty( $value['attributes'] ) && is_array( $value['attributes'] ) ) {
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...
351
					foreach ( $value['attributes'] as $attribute => $attribute_value ) {
352
						$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
353
					}
354
				}
355
356
				// Description handling.
357
				$description = self::get_field_description( $value );
358
359
				// Switch based on type.
360
				switch ( $value['type'] ) {
361
362
					// Section Titles.
363
					case 'title':
364
						if ( ! empty( $value['title'] ) || ! empty( $value['desc'] ) ) {
365
							?>
366
							<div class="give-setting-tab-header give-setting-tab-header-<?php echo $current_tab; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$current_tab'
Loading history...
367
								<?php if ( ! empty( $value['title'] ) ) : ?>
368
									<h2><?php echo self::get_field_title( $value ); ?></h2>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
369
									<hr>
370
								<?php endif; ?>
371
372
								<?php if ( ! empty( $value['desc'] ) ) : ?>
373
									<?php echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'wpautop'
Loading history...
374
								<?php endif; ?>
375
							</div>
376
							<?php
377
						}
378
379
						if ( $value['table_html'] ) {
380
							echo '<table class="form-table give-setting-tab-body give-setting-tab-body-' . $current_tab . '">' . "\n\n";
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$current_tab'
Loading history...
381
						}
382
383
						if ( ! empty( $value['id'] ) ) {
384
385
							/**
386
							 * Trigger Action.
387
							 *
388
							 * Note: action dynamically fire on basis of field id.
389
							 *
390
							 * @since 1.8
391
							 */
392
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) );
393
						}
394
395
						break;
396
397
					// Section Ends.
398
					case 'sectionend':
399 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...
400
401
							/**
402
							 * Trigger Action.
403
							 *
404
							 * Note: action dynamically fire on basis of field id.
405
							 *
406
							 * @since 1.8
407
							 */
408
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_end' );
409
						}
410
411
						if ( $value['table_html'] ) {
412
							echo '</table>';
413
						}
414
415 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...
416
417
							/**
418
							 * Trigger Action.
419
							 *
420
							 * Note: action dynamically fire on basis of field id.
421
							 *
422
							 * @since 1.8
423
							 */
424
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_after' );
425
						}
426
427
						break;
428
429
					// Standard text inputs and subtypes like 'number'.
430
					case 'colorpicker':
431
					case 'hidden' :
432
						$value['wrapper_class'] = empty( $value['wrapper_class'] ) ? 'give-hidden' : trim( $value['wrapper_class'] ) . ' give-hidden';
433
					case 'text':
434
					case 'email':
435
					case 'number':
436
					case 'password' :
437
						$type = $value['type'];
438
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
439
440
						// Set default value for repeater field if not any value set yet.
441
						if ( $value['repeat'] && is_string( $option_value ) ) {
442
							$option_value = array( $value['default'] );
443
						}
444
						?>
445
					<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
446
						<th scope="row" class="titledesc">
447
							<label
448
									for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
449
						</th>
450
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
451
							<?php if ( $value['repeat'] ) : ?>
452
								<?php foreach ( $option_value as $index => $field_value ) : ?>
0 ignored issues
show
Bug introduced by
The expression $option_value of type string|boolean|array<integer,?,{"0":"?"}> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
453
									<p>
454
										<input
455
												name="<?php echo esc_attr( $value['id'] ); ?>[]"
456
												type="<?php echo esc_attr( $type ); ?>"
457
												style="<?php echo esc_attr( $value['css'] ); ?>"
458
												value="<?php echo esc_attr( $field_value ); ?>"
459
												class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?> <?php echo esc_attr( $value['id'] ); ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
460
											<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
461
										/>
462
										<span class="give-remove-setting-field"
463
												title="<?php esc_html_e( 'Remove setting field', 'give' ); ?>">-</span>
464
									</p>
465
								<?php endforeach; ?>
466
								<a href="#" data-id="<?php echo $value['id']; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
467
										class="give-repeat-setting-field button-secondary"><?php echo $value['repeat_btn_title']; ?></a>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
468
							<?php else : ?>
469
								<input
470
										name="<?php echo esc_attr( $value['id'] ); ?>"
471
										id="<?php echo esc_attr( $value['id'] ); ?>"
472
										type="<?php echo esc_attr( $type ); ?>"
473
										style="<?php echo esc_attr( $value['css'] ); ?>"
474
										value="<?php echo esc_attr( $option_value ); ?>"
475
										class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
476
									<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
477
								/>
478
							<?php endif; ?>
479
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
480
						</td>
481
						</tr><?php
482
						break;
483
484
					// Textarea.
485
					case 'textarea':
486
487
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
488
489
						?>
490
					<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
491
						<th scope="row" class="titledesc">
492
							<label
493
									for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
494
						</th>
495
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
496
									<textarea
497
											name="<?php echo esc_attr( $value['id'] ); ?>"
498
											id="<?php echo esc_attr( $value['id'] ); ?>"
499
											style="<?php echo esc_attr( $value['css'] ); ?>"
500
											class="<?php echo esc_attr( $value['class'] ); ?>"
501
											rows="10"
502
											cols="60"
503
										<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
504
									><?php echo esc_textarea( $option_value ); ?></textarea>
505
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
506
						</td>
507
						</tr><?php
508
						break;
509
510
					// Select boxes.
511
					case 'select' :
512
					case 'multiselect' :
513
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
514
515
						/**
516
						 * Insert page in option if missing.
517
						 *
518
						 * Check success_page setting in general settings.
519
						 */
520
						if (
521
							isset( $value['attributes'] ) &&
522
							false !== strpos( $value['class'], 'give-select-chosen' ) &&
523
							in_array( 'data-search-type', array_keys( $value['attributes' ] ) ) &&
0 ignored issues
show
introduced by
Array keys should NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
524
							'pages' === $value['attributes' ]['data-search-type'] &&
0 ignored issues
show
introduced by
Array keys should NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
525
							! in_array( $option_value, array_keys( $value['options'] ) )
526
						) {
527
							$value['options'][ $option_value ] = get_the_title( $option_value );
528
						}
529
						?>
530
					<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
531
						<th scope="row" class="titledesc">
532
							<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
533
						</th>
534
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
535
							<select
536
									name="<?php echo esc_attr( $value['id'] ); ?><?php if ( 'multiselect' === $value['type'] ) echo '[]'; ?>"
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
537
									id="<?php echo esc_attr( $value['id'] ); ?>"
538
									style="<?php echo esc_attr( $value['css'] ); ?>"
539
									class="<?php echo esc_attr( $value['class'] ); ?>"
540
								<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
541
								<?php echo ( 'multiselect' === $value['type'] ) ? 'multiple="multiple"' : ''; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
542
							>
543
544
								<?php
545
								if ( ! empty( $value['options'] ) ) {
546 View Code Duplication
									foreach ( $value['options'] as $key => $val ) {
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...
547
										?>
548
										<option value="<?php echo esc_attr( $key ); ?>" <?php
549
550
										if ( is_array( $option_value ) ) {
551
											selected( in_array( $key, $option_value ), true );
552
										} else {
553
											selected( $option_value, $key );
554
										}
555
556
										?>><?php echo $val ?></option>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
557
										<?php
558
									}
559
								}
560
								?>
561
562
							</select> <?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
563
						</td>
564
						</tr><?php
565
						break;
566
567
					// Radio inputs.
568
					case 'radio_inline' :
569
						$value['class'] = empty( $value['class'] ) ? 'give-radio-inline' : $value['class'] . ' give-radio-inline';
570
					case 'radio' :
571
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
572
						?>
573
					<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
574
						<th scope="row" class="titledesc">
575
							<label
576
									for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
577
						</th>
578
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
579
							<fieldset>
580
								<ul>
581
									<?php
582 View Code Duplication
									foreach ( $value['options'] as $key => $val ) {
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...
583
										?>
584
										<li>
585
											<label><input
586
														name="<?php echo esc_attr( $value['id'] ); ?>"
587
														value="<?php echo $key; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$key'
Loading history...
588
														type="radio"
589
														style="<?php echo esc_attr( $value['css'] ); ?>"
590
													<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
591
													<?php checked( $key, $option_value ); ?>
592
												/> <?php echo $val ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
593
										</li>
594
										<?php
595
									}
596
									?>
597
									<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
598
							</fieldset>
599
						</td>
600
						</tr><?php
601
						break;
602
603
					// Checkbox input.
604
					case 'checkbox' :
605
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
606
						?>
607
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
608
							<th scope="row" class="titledesc">
609
								<label
610
										for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
611
							</th>
612
							<td class="give-forminp">
613
								<input
614
										name="<?php echo esc_attr( $value['id'] ); ?>"
615
										id="<?php echo esc_attr( $value['id'] ); ?>"
616
										type="checkbox"
617
										class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>"
618
										value="1"
619
									<?php checked( $option_value, 'on' ); ?>
620
									<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
621
								/>
622
								<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
623
							</td>
624
						</tr>
625
						<?php
626
						break;
627
628
					// Multi Checkbox input.
629
					case 'multicheck' :
630
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
631
						$option_value = is_array( $option_value ) ? $option_value : array();
632
						?>
633
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
634
							<th scope="row" class="titledesc">
635
								<label
636
										for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
637
							</th>
638
							<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?> <?php echo( ! empty( $value['class'] ) ? $value['class'] : '' ); ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
639
								<fieldset>
640
									<ul>
641
										<?php
642 View Code Duplication
										foreach ( $value['options'] as $key => $val ) {
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...
643
											?>
644
											<li>
645
												<label>
646
													<input
647
															name="<?php echo esc_attr( $value['id'] ); ?>[]"
648
															value="<?php echo $key; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$key'
Loading history...
649
															type="checkbox"
650
															style="<?php echo esc_attr( $value['css'] ); ?>"
651
														<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
652
														<?php if ( in_array( $key, $option_value ) ) {
653
															echo 'checked="checked"';
654
														} ?>
655
													/> <?php echo $val ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
656
												</label>
657
											</li>
658
											<?php
659
										}
660
										?>
661
										<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
662
								</fieldset>
663
							</td>
664
						</tr>
665
						<?php
666
						break;
667
668
					// File input field.
669
					case 'file' :
670
					case 'media' :
671
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
672
						$button_label = sprintf( __( 'Add or Upload %s', 'give' ), ( 'file' === $value['type'] ? __( 'File', 'give' ) : __( 'Image', 'give' ) ) );
673
						$fvalue       = empty( $value['fvalue'] ) ? 'url' : $value['fvalue'];
674
675
						$allow_media_preview_tags = array( 'jpg', 'jpeg', 'png', 'gif', 'ico' );
676
						$preview_image_src        = $option_value ? ( 'id' === $fvalue ? wp_get_attachment_url( $option_value ) : $option_value ) : '';
677
						$preview_image_extension  = $preview_image_src ? pathinfo( $preview_image_src, PATHINFO_EXTENSION ) : '';
678
						$is_show_preview          = in_array( $preview_image_extension, $allow_media_preview_tags );
679
						?>
680
						<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
681
							<th scope="row" class="titledesc">
682
								<label
683
										for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
684
							</th>
685
							<td class="give-forminp">
686
								<div class="give-field-wrap">
687
									<label for="<?php echo $value['id'] ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
688
										<input
689
												name="<?php echo esc_attr( $value['id'] ); ?>"
690
												id="<?php echo esc_attr( $value['id'] ); ?>"
691
												type="text"
692
												class="give-input-field<?php echo esc_attr( isset( $value['class'] ) ? ' ' . $value['class'] : '' ); ?>"
693
												value="<?php echo $option_value; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$option_value'
Loading history...
694
												style="<?php echo esc_attr( $value['css'] ); ?>"
695
											<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
696
										/>&nbsp;&nbsp;&nbsp;&nbsp;<input class="give-upload-button button" type="button"
697
												data-fvalue="<?php echo $fvalue; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$fvalue'
Loading history...
698
												data-field-type="<?php echo $value['type']; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
699
												value="<?php echo $button_label; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$button_label'
Loading history...
700
										<?php echo $description ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
701
										<div
702
												class="give-image-thumb<?php echo ! $option_value || ! $is_show_preview ? ' give-hidden' : ''; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
703
											<span class="give-delete-image-thumb dashicons dashicons-no-alt"></span>
704
											<img src="<?php echo $preview_image_src; ?>" alt="">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$preview_image_src'
Loading history...
705
										</div>
706
									</label>
707
								</div>
708
							</td>
709
						</tr>
710
						<?php
711
						break;
712
713
					// WordPress Editor.
714
					case 'wysiwyg' :
715
						// Get option value.
716
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
717
718
						// Get editor settings.
719
						$editor_settings = ! empty( $value['options'] ) ? $value['options'] : array();
720
						?>
721
					<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
722
						<th scope="row" class="titledesc">
723
							<label
724
									for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
725
						</th>
726
						<td class="give-forminp">
727
							<?php wp_editor( $option_value, $value['id'], $editor_settings ); ?>
728
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
729
						</td>
730
						</tr><?php
731
						break;
732
733
					// Custom: Default gateways setting field.
734
					case 'default_gateway' :
735
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
736
						?>
737
					<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
738
						<th scope="row" class="titledesc">
739
							<label
740
									for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
741
						</th>
742
						<td class="give-forminp">
743
							<?php give_default_gateway_callback( $value, $option_value ); ?>
0 ignored issues
show
Documentation introduced by
$option_value is of type string|boolean, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
744
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
745
						</td>
746
						</tr><?php
747
						break;
748
749
					// Custom: Email preview buttons field.
750
					case 'email_preview_buttons' :
751
						?>
752
					<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
753
						<th scope="row" class="titledesc">
754
							<label
755
									for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
756
						</th>
757
						<td class="give-forminp">
758
							<?php give_email_preview_buttons_callback( $value ); ?>
759
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
760
						</td>
761
						</tr><?php
762
						break;
763
764
					// Custom: API field.
765
					case 'api' :
766
						give_api_callback();
767
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
768
						break;
769
770
					// Custom: Gateway API key.
771
					case 'api_key' :
772
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
773
						$type         = ! empty( $option_value ) ? 'password' : 'text';
774
						?>
775
					<tr valign="top" <?php echo ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '!'
Loading history...
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
776
						<th scope="row" class="titledesc">
777
							<label
778
									for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo self::get_field_title( $value ); ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
779
						</th>
780
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
781
							<input
782
									name="<?php echo esc_attr( $value['id'] ); ?>"
783
									id="<?php echo esc_attr( $value['id'] ); ?>"
784
									type="<?php echo esc_attr( $type ); ?>"
785
									style="<?php echo esc_attr( $value['css'] ); ?>"
786
									value="<?php echo esc_attr( trim( $option_value ) ); ?>"
787
									class="give-input-field<?php echo( empty( $value['class'] ) ? '' : ' ' . esc_attr( $value['class'] ) ); ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
788
								<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
789
							/> <?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
790
						</td>
791
						</tr><?php
792
						break;
793
794
					case 'chosen' :
795
796
						// Get option value.
797
						$option_value     = self::get_option( $option_name, $value['id'], $value['default'] );
798
						$wrapper_class    = ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '';
799
						$type             = '';
800
						$allow_new_values = '';
801
						$name             = give_get_field_name( $value );
802
803
						// Set attributes based on multiselect datatype.
804
						if ( 'multiselect' === $value['data_type'] ) {
805
							$type = 'multiple';
806
							$allow_new_values = 'data-allows-new-values="true"';
807
							$name             = $name . '[]';
808
809
							$option_value = empty( $option_value ) ? array() : $option_value;
810
						}
811
812
						$value['options'] = array_merge( $value['options'], array_combine( array_values( $option_value ), array_values( $option_value ) ) );
813
						?>
814
						<tr valign="top" <?php echo $wrapper_class; ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$wrapper_class'
Loading history...
815
							<th scope="row" class="titledesc">
816
								<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_attr( self::get_field_title( $value ) ); ?></label>
817
							</th>
818
							<td class="give-forminp give-forminp-<?php echo esc_attr( $value['type'] ); ?>">
819
								<select
820
									class="give-select-chosen give-chosen-settings"
821
									style="<?php echo esc_attr( $value['style'] ); ?>"
822
									name="<?php echo esc_attr( $name ); ?>"
823
									id="<?php echo esc_attr( $value['id'] ); ?>"
824
									<?php echo esc_attr( $type ) . ' ' . esc_attr( $allow_new_values ); ?>
825
									<?php echo implode( ' ', $custom_attributes ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'implode'
Loading history...
826
								>
827 View Code Duplication
									<?php foreach ( $value['options'] as $key => $item_value ) : ?>
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...
828
										<option
829
											value="<?php echo esc_attr( $key ); ?>"
830
											<?php
831
											if ( is_array( $option_value ) ) {
832
												selected( in_array( $key, $option_value, true ) );
833
											} else {
834
												selected( $option_value, $key );
835
											}
836
											?>
837
										>
838
											<?php echo esc_html( $item_value ); ?>
839
										</option>
840
									<?php endforeach; ?>
841
842
								</select>
843
								<?php echo wp_kses_post( $description ); ?>
844
							</td>
845
						</tr>
846
						<?php
847
						break;
848
849
					// Custom: Log field.
850
					case 'logs' :
851
852
						// Get current section.
853
						$current_section = $_GET['section'] = give_get_current_setting_section();
854
855
						/**
856
						 * Fires for each tab of logs view.
857
						 *
858
						 * @since 1.0
859
						 */
860
						do_action( "give_logs_view_{$current_section}" );
861
862
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
863
						break;
864
865
					// Custom: Data field.
866
					case 'data' :
867
868
						include GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-data.php';
869
870
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
871
						break;
872
873
					// Custom: Give Docs Link field type.
874
					case 'give_docs_link' :
875
						$wrapper_class = ! empty( $value['wrapper_class'] ) ? 'class="' . $value['wrapper_class'] . '"' : '';
876
						?>
877
					<tr valign="top" <?php echo esc_html( $wrapper_class ); ?>>
878
						<td class="give-docs-link" colspan="2">
879
							<p class="give-docs-link">
880
								<a href="<?php echo esc_url( $value['url'] ); ?>" target="_blank">
881
									<?php
882
									echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
883
										/* translators: %s Title */
884
										esc_html__( 'Need Help? See docs on "%s"', 'give' ),
885
										esc_html( $value['title'] )
886
									);
887
									?>
888
									<span class="dashicons dashicons-editor-help"></span>
889
								</a>
890
							</p>
891
						</td>
892
						</tr><?php
893
						break;
894
895
					// Default: run an action
896
					// You can add or handle your custom field action.
897
					default:
898
						// Get option value.
899
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
900
						do_action( 'give_admin_field_' . $value['type'], $value, $option_value );
901
						break;
902
				}
903
			}
904
		}
905
906
		/**
907
		 * Helper function to get the formatted description for a given form field.
908
		 * Plugins can call this when implementing their own custom settings types.
909
		 *
910
		 * @since  1.8
911
		 *
912
		 * @param  array $value The form field value array
913
		 *
914
		 * @return string The HTML description of the field.
915
		 */
916
		public static function get_field_description( $value ) {
917
			$description = '';
918
919
			// Support for both 'description' and 'desc' args.
920
			$description_key = isset( $value['description'] ) ? 'description' : 'desc';
921
			$value           = ( isset( $value[ $description_key ] ) && ! empty( $value[ $description_key ] ) ) ? $value[ $description_key ] : '';
922
923
			if ( ! empty( $value ) ) {
924
				$description = '<div class="give-field-description">' . wp_kses_post( $value ) . '</div>';
925
			}
926
927
			return $description;
928
		}
929
930
931
		/**
932
		 * Helper function to get the formated title.
933
		 * Plugins can call this when implementing their own custom settings types.
934
		 *
935
		 * @since  1.8
936
		 *
937
		 * @param  array $value The form field value array
938
		 *
939
		 * @return array The description and tip as a 2 element array
940
		 */
941
		public static function get_field_title( $value ) {
942
			$title = esc_html( $value['title'] );
943
944
			// If html tag detected then allow them to print.
945
			if ( strip_tags( $title ) ) {
946
				$title = $value['title'];
947
			}
948
949
			return $title;
950
		}
951
952
		/**
953
		 * Save admin fields.
954
		 *
955
		 * Loops though the give options array and outputs each field.
956
		 *
957
		 * @since  1.8
958
		 *
959
		 * @param  array  $options     Options array to output
960
		 * @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.
961
		 *
962
		 * @return bool
963
		 */
964
		public static function save_fields( $options, $option_name = '' ) {
965
			if ( empty( $_POST ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
966
				return false;
967
			}
968
969
			// Options to update will be stored here and saved later.
970
			$update_options = array();
971
972
			// Loop options and get values to save.
973
			foreach ( $options as $option ) {
974
				if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
975
					continue;
976
				}
977
978
				// Get posted value.
979
				if ( strstr( $option['id'], '[' ) ) {
980
					parse_str( $option['id'], $option_name_array );
981
					$field_option_name = current( array_keys( $option_name_array ) );
982
					$setting_name      = key( $option_name_array[ $field_option_name ] );
983
					$raw_value         = isset( $_POST[ $field_option_name ][ $setting_name ] ) ? wp_unslash( $_POST[ $field_option_name ][ $setting_name ] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
984
				} else {
985
					$field_option_name = $option['id'];
986
					$setting_name      = '';
987
					$raw_value         = isset( $_POST[ $option['id'] ] ) ? wp_unslash( $_POST[ $option['id'] ] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
988
				}
989
990
				// Format the value based on option type.
991
				switch ( $option['type'] ) {
992
					case 'checkbox' :
993
						$value = is_null( $raw_value ) ? '' : 'on';
994
						break;
995
					case 'wysiwyg'  :
996
					case 'textarea' :
997
						$value = wp_kses_post( trim( $raw_value ) );
998
						break;
999
					case 'multiselect' :
1000
					case 'chosen' :
1001
						$value = array_filter( array_map( 'give_clean', (array) $raw_value ) );
1002
						break;
1003
					default :
1004
						$value = give_clean( $raw_value );
1005
						break;
1006
				}
1007
1008
				/**
1009
				 * Sanitize the value of an option.
1010
				 *
1011
				 * @since 1.8
1012
				 */
1013
				$value = apply_filters( 'give_admin_settings_sanitize_option', $value, $option, $raw_value );
1014
1015
				/**
1016
				 * Sanitize the value of an option by option name.
1017
				 *
1018
				 * @since 1.8
1019
				 */
1020
				$value = apply_filters( "give_admin_settings_sanitize_option_{$field_option_name}", $value, $option, $raw_value );
1021
1022
				if ( is_null( $value ) ) {
1023
					continue;
1024
				}
1025
1026
				// Check if option is an array and handle that differently to single values.
1027
				if ( $field_option_name && $setting_name ) {
1028
					if ( ! isset( $update_options[ $field_option_name ] ) ) {
1029
						$update_options[ $field_option_name ] = get_option( $field_option_name, array() );
1030
					}
1031
					if ( ! is_array( $update_options[ $field_option_name ] ) ) {
1032
						$update_options[ $field_option_name ] = array();
1033
					}
1034
					$update_options[ $field_option_name ][ $setting_name ] = $value;
1035
				} else {
1036
					$update_options[ $field_option_name ] = $value;
1037
				}
1038
			}
1039
1040
			// Save all options in our array or there own option name i.e. option id.
1041
			if ( empty( $option_name ) ) {
1042
				foreach ( $update_options as $name => $value ) {
1043
					update_option( $name, $value );
1044
1045
					/**
1046
					 * Trigger action.
1047
					 *
1048
					 * Note: This is dynamically fire on basis of option name.
1049
					 *
1050
					 * @since 1.8
1051
					 */
1052
					do_action( "give_save_option_{$name}", $value, $name );
1053
				}
1054
			} else {
1055
				$old_options    = ( $old_options = get_option( $option_name ) ) ? $old_options : array();
1056
				$update_options = array_merge( $old_options, $update_options );
1057
1058
				update_option( $option_name, $update_options );
1059
1060
				/**
1061
				 * Trigger action.
1062
				 *
1063
				 * Note: This is dynamically fire on basis of setting name.
1064
				 *
1065
				 * @since 1.8
1066
				 */
1067
				do_action( "give_save_settings_{$option_name}", $update_options, $option_name, $old_options );
1068
			}
1069
1070
			return true;
1071
		}
1072
1073
1074
		/**
1075
		 * Check if admin saving setting or not.
1076
		 *
1077
		 * @since 1.8.17
1078
		 *
1079
		 * @return bool
1080
		 */
1081
		public static function is_saving_settings() {
1082
			return self::verify_nonce();
1083
		}
1084
1085
		/**
1086
		 * Verify setting page
1087
		 *
1088
		 * @since  2.0
1089
		 * @access public
1090
		 *
1091
		 * @param string $tab
1092
		 * @param string $section
1093
		 *
1094
		 * @return bool
1095
		 */
1096
		public static function is_setting_page( $tab = '', $section = '' ) {
1097
			$is_setting_page = false;
1098
1099
			if( ! is_admin() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1100
				return $is_setting_page;
1101
			}
1102
1103
			// Check fo setting tab.
1104
			if ( ! empty( $tab ) ) {
1105
				$is_setting_page = ( $tab === give_get_current_setting_tab() );
1106
			}
1107
1108
			// Check fo setting section.
1109
			if ( ! empty( $section ) ) {
1110
				$is_setting_page = ( $section === give_get_current_setting_section() );
1111
			}
1112
1113
			return $is_setting_page;
1114
		}
1115
	}
1116
1117
endif;
1118