Test Failed
Push — issues-2650 ( f6e8fb )
by Ravinder
50:58 queued 44:44
created

Give_Admin_Settings::is_setting_page()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 15
rs 9.7666
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
		 * Varify 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>' . __( 'Action failed. Please refresh the page and retry.', 'give' ) . '</p></div>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
107
				die();
108
			}
109
110
			// Show error message if Akismet not configured and Admin try to save 'enabled' option.
111
			if ( isset( $_POST['akismet_spam_protection'] )
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
112
			     && give_is_setting_enabled( $_POST['akismet_spam_protection'] )
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...
113
			     && ! give_check_akismet_key()
114
			) {
115
				self::add_error( 'give-akismet-protection', __( 'Please properly configure Akismet to enable SPAM protection.', 'give' ) );
116
117
				return;
118
			}
119
120
			/**
121
			 * Trigger Action.
122
			 *
123
			 * Note: action dynamically fire on basis of setting page slug and current tab.
124
			 * For example: if you register a setting page with give-settings menu slug and general current tab name
125
			 *              then action will be give-settings_save_general
126
			 *
127
			 * @since 1.8
128
			 */
129
			do_action( self::$setting_filter_prefix . '_save_' . $current_tab );
130
131
			self::add_message( 'give-setting-updated', __( 'Your settings have been saved.', 'give' ) );
132
133
			/**
134
			 * Trigger Action.
135
			 *
136
			 * Note: action dynamically fire on basis of setting page slug.
137
			 * For example: if you register a setting page with give-settings menu slug
138
			 *              then action will be give-settings_saved
139
			 *
140
			 * @since 1.8
141
			 */
142
			do_action( self::$setting_filter_prefix . '_saved' );
143
		}
144
145
		/**
146
		 * Add a message.
147
		 *
148
		 * @since  1.8
149
		 *
150
		 * @param  string $code    Message code (Note: This should be unique).
151
		 * @param  string $message Message text.
152
		 *
153
		 * @return void
154
		 */
155
		public static function add_message( $code, $message ) {
156
			self::$messages[ $code ] = $message;
157
		}
158
159
		/**
160
		 * Add an error.
161
		 *
162
		 * @since  1.8
163
		 *
164
		 * @param  string $code    Message code (Note: This should be unique).
165
		 * @param  string $message Message text.
166
		 *
167
		 * @return void
168
		 */
169
		public static function add_error( $code, $message ) {
170
			self::$errors[ $code ] = $message;
171
		}
172
173
		/**
174
		 * Output messages + errors.
175
		 *
176
		 * @since  1.8
177
		 * @return void
178
		 */
179
		public static function show_messages() {
180
			$notice_html = '';
181
			$classes     = 'give-notice settings-error notice is-dismissible';
182
183
			self::$errors   = apply_filters( self::$setting_filter_prefix . '_error_notices', self::$errors );
184
			self::$messages = apply_filters( self::$setting_filter_prefix . '_update_notices', self::$messages );
185
186 View Code Duplication
			if ( 0 < count( self::$errors ) ) {
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...
187
				foreach ( self::$errors as $code => $message ) {
188
					$notice_html .= '<div id="setting-error-' . $code . '" class="' . $classes . ' error"><p><strong>' . $message . '</strong></p></div>';
189
				}
190
			}
191
192 View Code Duplication
			if ( 0 < count( self::$messages ) ) {
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...
193
				foreach ( self::$messages as $code => $message ) {
194
					$notice_html .= '<div id="setting-error-' . $code . '" class="' . $classes . ' updated"><p><strong>' . $message . '</strong></p></div>';
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 ( ! empty( $_POST ) ) {
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
274
		 * @param  string $field_id
275
		 * @param  mixed  $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><hr>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'self'
Loading history...
369
								<?php endif; ?>
370
371
								<?php if ( ! empty( $value['desc'] ) ) : ?>
372
									<?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...
373
								<?php endif; ?>
374
							</div>
375
							<?php
376
						}
377
378
						if ( $value['table_html'] ) {
379
							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...
380
						}
381
382
						if ( ! empty( $value['id'] ) ) {
383
384
							/**
385
							 * Trigger Action.
386
							 *
387
							 * Note: action dynamically fire on basis of field id.
388
							 *
389
							 * @since 1.8
390
							 */
391
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) );
392
						}
393
394
						break;
395
396
					// Section Ends.
397
					case 'sectionend':
398 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...
399
400
							/**
401
							 * Trigger Action.
402
							 *
403
							 * Note: action dynamically fire on basis of field id.
404
							 *
405
							 * @since 1.8
406
							 */
407
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_end' );
408
						}
409
410
						if ( $value['table_html'] ) {
411
							echo '</table>';
412
						}
413
414 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...
415
416
							/**
417
							 * Trigger Action.
418
							 *
419
							 * Note: action dynamically fire on basis of field id.
420
							 *
421
							 * @since 1.8
422
							 */
423
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_after' );
424
						}
425
426
						break;
427
428
					// Standard text inputs and subtypes like 'number'.
429
					case 'colorpicker':
430
					case 'hidden' :
431
						$value['wrapper_class'] = empty( $value['wrapper_class'] ) ? 'give-hidden' : trim( $value['wrapper_class'] ) . ' give-hidden';
432
					case 'text':
433
					case 'email':
434
					case 'number':
435
					case 'password' :
436
						$type = $value['type'];
437
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
438
439
						// Set default value for repeater field if not any value set yet.
440
						if( $value['repeat'] && is_string( $option_value ) ) {
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...
441
							$option_value = array( $value['default'] );
442
						}
443
						?>
444
						<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...
445
							<th scope="row" class="titledesc">
446
								<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...
447
							</th>
448
							<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
449
								<?php if ( $value['repeat'] ) : ?>
450
									<?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...
451
										<p>
452
											<input
453
													name="<?php echo esc_attr( $value['id'] ); ?>[]"
454
													type="<?php echo esc_attr( $type ); ?>"
455
													style="<?php echo esc_attr( $value['css'] ); ?>"
456
													value="<?php echo esc_attr( $field_value ); ?>"
457
													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...
458
												<?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...
459
											/>
460
											<span class="give-remove-setting-field" title="<?php esc_html_e( 'Remove setting field', 'give' ); ?>">-</span>
461
										</p>
462
									<?php endforeach; ?>
463
									<a href="#" data-id="<?php echo $value['id']; ?>" 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...
464
								<?php else : ?>
465
									<input
466
											name="<?php echo esc_attr( $value['id'] ); ?>"
467
											id="<?php echo esc_attr( $value['id'] ); ?>"
468
											type="<?php echo esc_attr( $type ); ?>"
469
											style="<?php echo esc_attr( $value['css'] ); ?>"
470
											value="<?php echo esc_attr( $option_value ); ?>"
471
											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...
472
										<?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...
473
									/>
474
								<?php endif; ?>
475
								<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
476
							</td>
477
							</tr><?php
478
							break;
479
480
					// Textarea.
481
					case 'textarea':
482
483
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
484
485
						?>
486
					<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...
487
						<th scope="row" class="titledesc">
488
							<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...
489
						</th>
490
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
491
								<textarea
492
										name="<?php echo esc_attr( $value['id'] ); ?>"
493
										id="<?php echo esc_attr( $value['id'] ); ?>"
494
										style="<?php echo esc_attr( $value['css'] ); ?>"
495
										class="<?php echo esc_attr( $value['class'] ); ?>"
496
										rows="10"
497
										cols="60"
498
									<?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...
499
								><?php echo esc_textarea( $option_value ); ?></textarea>
500
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
501
						</td>
502
						</tr><?php
503
						break;
504
505
					// Select boxes.
506
					case 'select' :
507
					case 'multiselect' :
508
509
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
510
511
						?>
512
					<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...
513
						<th scope="row" class="titledesc">
514
							<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...
515
						</th>
516
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
517
							<select
518
									name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
519
										echo '[]';
520
									} ?>"
521
									id="<?php echo esc_attr( $value['id'] ); ?>"
522
									style="<?php echo esc_attr( $value['css'] ); ?>"
523
									class="<?php echo esc_attr( $value['class'] ); ?>"
524
								<?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...
525
								<?php echo ( 'multiselect' == $value['type'] ) ? 'multiple="multiple"' : ''; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
526
							>
527
528
								<?php
529
								if ( ! empty( $value['options'] ) ) {
530
									foreach ( $value['options'] as $key => $val ) {
531
										?>
532
										<option value="<?php echo esc_attr( $key ); ?>" <?php
533
534
										if ( is_array( $option_value ) ) {
535
											selected( in_array( $key, $option_value ), true );
536
										} else {
537
											selected( $option_value, $key );
538
										}
539
540
										?>><?php echo $val ?></option>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
541
										<?php
542
									}
543
								}
544
								?>
545
546
							</select> <?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
547
						</td>
548
						</tr><?php
549
						break;
550
551
					// Radio inputs.
552
					case 'radio_inline' :
553
						$value['class'] = empty( $value['class'] ) ? 'give-radio-inline' : $value['class'] . ' give-radio-inline';
554
					case 'radio' :
555
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
556
						?>
557
					<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...
558
						<th scope="row" class="titledesc">
559
							<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...
560
						</th>
561
						<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...
562
							<fieldset>
563
								<ul>
564
									<?php
565 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...
566
										?>
567
										<li>
568
											<label><input
569
														name="<?php echo esc_attr( $value['id'] ); ?>"
570
														value="<?php echo $key; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$key'
Loading history...
571
														type="radio"
572
														style="<?php echo esc_attr( $value['css'] ); ?>"
573
													<?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...
574
													<?php checked( $key, $option_value ); ?>
575
												/> <?php echo $val ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
576
										</li>
577
										<?php
578
									}
579
									?>
580
									<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
581
							</fieldset>
582
						</td>
583
						</tr><?php
584
						break;
585
586
					// Checkbox input.
587
					case 'checkbox' :
588
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
589
						?>
590
						<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...
591
							<th scope="row" class="titledesc">
592
								<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...
593
							</th>
594
							<td class="give-forminp">
595
								<input
596
										name="<?php echo esc_attr( $value['id'] ); ?>"
597
										id="<?php echo esc_attr( $value['id'] ); ?>"
598
										type="checkbox"
599
										class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>"
600
										value="1"
601
									<?php checked( $option_value, 'on' ); ?>
602
									<?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...
603
								/>
604
								<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
605
							</td>
606
						</tr>
607
						<?php
608
						break;
609
610
					// Multi Checkbox input.
611
					case 'multicheck' :
612
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
613
						$option_value = is_array( $option_value ) ? $option_value : array();
614
						?>
615
						<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...
616
							<th scope="row" class="titledesc">
617
								<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...
618
							</th>
619
							<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...
620
								<fieldset>
621
									<ul>
622
										<?php
623 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...
624
											?>
625
											<li>
626
												<label>
627
													<input
628
															name="<?php echo esc_attr( $value['id'] ); ?>[]"
629
															value="<?php echo $key; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$key'
Loading history...
630
															type="checkbox"
631
															style="<?php echo esc_attr( $value['css'] ); ?>"
632
														<?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...
633
														<?php if ( in_array( $key, $option_value ) ) {
634
															echo 'checked="checked"';
635
														} ?>
636
													/> <?php echo $val ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
637
												</label>
638
											</li>
639
											<?php
640
										}
641
										?>
642
										<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
643
								</fieldset>
644
							</td>
645
						</tr>
646
						<?php
647
						break;
648
649
					// File input field.
650
					case 'file' :
651
					case 'media' :
652
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
653
						$button_label = esc_html__( sprintf( 'Add or Upload %s', ( 'file' === $value['type'] ? 'File' : 'Image' ) ), 'give' );
654
						$fvalue       = empty( $value['fvalue'] ) ? 'url' : $value['fvalue'];
655
656
						$allow_media_preview_tags = array( 'jpg', 'jpeg', 'png', 'gif', 'ico' );
657
						$preview_image_src        = $option_value ? ( 'id' === $fvalue ? wp_get_attachment_url( $option_value ) : $option_value ) : '';
658
						$preview_image_extension  = $preview_image_src ? pathinfo( $preview_image_src, PATHINFO_EXTENSION ) : '';
659
						$is_show_preview          = in_array( $preview_image_extension, $allow_media_preview_tags );
660
						?>
661
						<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...
662
							<th scope="row" class="titledesc">
663
								<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...
664
							</th>
665
							<td class="give-forminp">
666
								<div class="give-field-wrap">
667
									<label for="<?php echo $value['id'] ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
668
										<input
669
												name="<?php echo esc_attr( $value['id'] ); ?>"
670
												id="<?php echo esc_attr( $value['id'] ); ?>"
671
												type="text"
672
												class="give-input-field<?php echo esc_attr( isset( $value['class'] ) ? ' ' . $value['class'] : '' ); ?>"
673
												value="<?php echo $option_value; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$option_value'
Loading history...
674
												style="<?php echo esc_attr( $value['css'] ); ?>"
675
											<?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...
676
										/>&nbsp;&nbsp;&nbsp;&nbsp;<input class="give-upload-button button" type="button" data-fvalue="<?php echo $fvalue; ?>" data-field-type="<?php echo $value['type']; ?>" value="<?php echo $button_label; ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$fvalue'
Loading history...
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 '$button_label'
Loading history...
677
										<?php echo $description ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
678
										<div 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...
679
											<span class="give-delete-image-thumb dashicons dashicons-no-alt"></span>
680
											<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...
681
										</div>
682
									</label>
683
								</div>
684
							</td>
685
						</tr>
686
						<?php
687
						break;
688
689
					// WordPress Editor.
690
					case 'wysiwyg' :
691
						// Get option value.
692
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
693
694
						// Get editor settings.
695
						$editor_settings = ! empty( $value['options'] ) ? $value['options'] : array();
696
						?>
697
					<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...
698
						<th scope="row" class="titledesc">
699
							<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...
700
						</th>
701
						<td class="give-forminp">
702
							<?php wp_editor( $option_value, $value['id'], $editor_settings ); ?>
703
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
704
						</td>
705
						</tr><?php
706
						break;
707
708
					// Custom: System setting field.
709 View Code Duplication
					case 'system_info' :
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...
710
						?>
711
					<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...
712
						<th scope="row" class="titledesc">
713
							<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...
714
						</th>
715
						<td class="give-forminp">
716
							<?php give_system_info_callback(); ?>
717
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
718
						</td>
719
						</tr><?php
720
						break;
721
722
					// Custom: Default gateways setting field.
723 View Code Duplication
					case 'default_gateway' :
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...
724
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
725
						?>
726
					<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...
727
						<th scope="row" class="titledesc">
728
							<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...
729
						</th>
730
						<td class="give-forminp">
731
							<?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...
732
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
733
						</td>
734
						</tr><?php
735
						break;
736
737
					// Custom: Enable gateways setting field.
738 View Code Duplication
					case 'enabled_gateways' :
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...
739
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
740
						?>
741
					<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...
742
						<th scope="row" class="titledesc">
743
							<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...
744
						</th>
745
						<td class="give-forminp">
746
							<?php give_enabled_gateways_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...
747
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
748
						</td>
749
						</tr><?php
750
						break;
751
752
					// Custom: Email preview buttons field.
753 View Code Duplication
					case 'email_preview_buttons' :
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...
754
						?>
755
						<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...
756
						<th scope="row" class="titledesc">
757
							<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...
758
						</th>
759
						<td class="give-forminp">
760
							<?php give_email_preview_buttons_callback( $value ); ?>
761
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
762
						</td>
763
						</tr><?php
764
						break;
765
766
					// Custom: API field.
767
					case 'api' :
768
						give_api_callback();
769
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
770
						break;
771
772
					// Custom: Gateway API key.
773
					case 'api_key' :
774
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
775
						$type         = ! empty( $option_value ) ? 'password' : 'text';
776
						?>
777
					<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...
778
						<th scope="row" class="titledesc">
779
							<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...
780
						</th>
781
						<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
782
							<input
783
									name="<?php echo esc_attr( $value['id'] ); ?>"
784
									id="<?php echo esc_attr( $value['id'] ); ?>"
785
									type="<?php echo esc_attr( $type ); ?>"
786
									style="<?php echo esc_attr( $value['css'] ); ?>"
787
									value="<?php echo esc_attr( trim( $option_value ) ); ?>"
788
									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...
789
								<?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...
790
							/> <?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
791
						</td>
792
						</tr><?php
793
						break;
794
795
					// Custom: Log field.
796
					case 'logs' :
797
798
						// Get current section.
799
						$current_section = $_GET['section'] = give_get_current_setting_section();
800
801
						/**
802
						 * Fires for each tab of logs view.
803
						 *
804
						 * @since 1.0
805
						 */
806
						do_action( "give_logs_view_{$current_section}" );
807
808
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
809
						break;
810
811
					// Custom: Data field.
812
					case 'data' :
813
814
						include GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-data.php';
815
816
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
817
						break;
818
819
					// Custom: Give Docs Link field type.
820
					case 'give_docs_link' :
821
						?>
822
					<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...
823
						<td class="give-docs-link" colspan="2">
824
							<?php
825
							echo '<p class="give-docs-link"><a href="' . esc_url( $value['url'] )
826
							     . '" target="_blank">'
827
							     . sprintf( esc_html__( 'Need Help? See docs on "%s"', 'give' ), $value['title'] )
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
828
							     . '<span class="dashicons dashicons-editor-help"></span></a></p>';
829
							?>
830
						</td>
831
						</tr><?php
832
						break;
833
834
					// Default: run an action
835
					// You can add or handle your custom field action.
836
					default:
837
						// Get option value.
838
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
839
						do_action( 'give_admin_field_' . $value['type'], $value, $option_value );
840
						break;
841
				}
842
			}
843
		}
844
845
		/**
846
		 * Helper function to get the formatted description for a given form field.
847
		 * Plugins can call this when implementing their own custom settings types.
848
		 *
849
		 * @since  1.8
850
		 *
851
		 * @param  array $value The form field value array
852
		 *
853
		 * @return string The HTML description of the field.
854
		 */
855
		public static function get_field_description( $value ) {
856
			$description = '';
857
858
			// Support for both 'description' and 'desc' args.
859
			$description_key = isset( $value['description'] ) ? 'description' : 'desc';
860
			$value           = ( isset( $value[ $description_key ] ) && ! empty( $value[ $description_key ] ) ) ? $value[ $description_key ] : '';
861
862
			if ( ! empty( $value ) ) {
863
				$description = '<p class="give-field-description">' . wp_kses_post( $value ) . '</p>';
864
			}
865
866
			return $description;
867
		}
868
869
870
		/**
871
		 * Helper function to get the formated title.
872
		 * Plugins can call this when implementing their own custom settings types.
873
		 *
874
		 * @since  1.8
875
		 *
876
		 * @param  array $value The form field value array
877
		 *
878
		 * @return array The description and tip as a 2 element array
879
		 */
880
		public static function get_field_title( $value ) {
881
			$title = esc_html( $value['title'] );
882
883
			// If html tag detected then allow them to print.
884
			if ( strip_tags( $title ) ) {
885
				$title = $value['title'];
886
			}
887
888
			return $title;
889
		}
890
891
		/**
892
		 * Save admin fields.
893
		 *
894
		 * Loops though the give options array and outputs each field.
895
		 *
896
		 * @since  1.8
897
		 *
898
		 * @param  array  $options     Options array to output
899
		 * @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.
900
		 *
901
		 * @return bool
902
		 */
903
		public static function save_fields( $options, $option_name = '' ) {
904
			if ( empty( $_POST ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
905
				return false;
906
			}
907
908
			// Options to update will be stored here and saved later.
909
			$update_options = array();
910
911
			// Loop options and get values to save.
912
			foreach ( $options as $option ) {
913
				if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
914
					continue;
915
				}
916
917
				// Get posted value.
918
				if ( strstr( $option['id'], '[' ) ) {
919
					parse_str( $option['id'], $option_name_array );
920
					$field_option_name = current( array_keys( $option_name_array ) );
921
					$setting_name      = key( $option_name_array[ $field_option_name ] );
922
					$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...
923
				} else {
924
					$field_option_name = $option['id'];
925
					$setting_name      = '';
926
					$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...
927
				}
928
929
				// Format the value based on option type.
930
				switch ( $option['type'] ) {
931
					case 'checkbox' :
932
						$value = is_null( $raw_value ) ? '' : 'on';
933
						break;
934
					case 'wysiwyg'  :
935
					case 'textarea' :
936
						$value = wp_kses_post( trim( $raw_value ) );
937
						break;
938
					case 'multiselect' :
939
						$value = array_filter( array_map( 'give_clean', (array) $raw_value ) );
940
						break;
941
					default :
942
						$value = give_clean( $raw_value );
943
						break;
944
				}
945
946
				/**
947
				 * Sanitize the value of an option.
948
				 *
949
				 * @since 1.8
950
				 */
951
				$value = apply_filters( 'give_admin_settings_sanitize_option', $value, $option, $raw_value );
952
953
				/**
954
				 * Sanitize the value of an option by option name.
955
				 *
956
				 * @since 1.8
957
				 */
958
				$value = apply_filters( "give_admin_settings_sanitize_option_{$field_option_name}", $value, $option, $raw_value );
959
960
				if ( is_null( $value ) ) {
961
					continue;
962
				}
963
964
				// Check if option is an array and handle that differently to single values.
965
				if ( $field_option_name && $setting_name ) {
966
					if ( ! isset( $update_options[ $field_option_name ] ) ) {
967
						$update_options[ $field_option_name ] = get_option( $field_option_name, array() );
968
					}
969
					if ( ! is_array( $update_options[ $field_option_name ] ) ) {
970
						$update_options[ $field_option_name ] = array();
971
					}
972
					$update_options[ $field_option_name ][ $setting_name ] = $value;
973
				} else {
974
					$update_options[ $field_option_name ] = $value;
975
				}
976
			}
977
978
			// Save all options in our array or there own option name i.e. option id.
979
			if ( empty( $option_name ) ) {
980
				foreach ( $update_options as $name => $value ) {
981
					update_option( $name, $value );
982
983
					/**
984
					 * Trigger action.
985
					 *
986
					 * Note: This is dynamically fire on basis of option name.
987
					 *
988
					 * @since 1.8
989
					 */
990
					do_action( "give_save_option_{$name}", $value, $name );
991
				}
992
			} else {
993
				$old_options    = ( $old_options = get_option( $option_name ) ) ? $old_options : array();
994
				$update_options = array_merge( $old_options, $update_options );
995
996
				update_option( $option_name, $update_options );
997
998
				/**
999
				 * Trigger action.
1000
				 *
1001
				 * Note: This is dynamically fire on basis of setting name.
1002
				 *
1003
				 * @since 1.8
1004
				 */
1005
				do_action( "give_save_settings_{$option_name}", $update_options, $option_name );
1006
			}
1007
1008
			return true;
1009
		}
1010
1011
1012
		/**
1013
		 * Check if admin saving setting or not.
1014
		 *
1015
		 * @since 1.8.17
1016
		 *
1017
		 * @return bool
1018
		 */
1019
		public static function is_saving_settings() {
1020
			return self::verify_nonce();
1021
		}
1022
1023
		/**
1024
		 * Verify setting page
1025
		 *
1026
		 * @since  2.0
1027
		 * @access public
1028
		 *
1029
		 * @param string $tab
1030
		 * @param string $section
1031
		 *
1032
		 * @return bool
1033
		 */
1034
		public static function is_setting_page( $tab = '', $section = '' ) {
1035
			$is_setting_page = false;
1036
1037
			// Check fo setting tab.
1038
			if ( ! empty( $tab ) ) {
1039
				$is_setting_page = ( $tab === give_get_current_setting_tab() );
1040
			}
1041
1042
			// Check fo setting section.
1043
			if ( ! empty( $section ) ) {
1044
				$is_setting_page = ( $section === give_get_current_setting_section() );
1045
			}
1046
1047
			return $is_setting_page;
1048
		}
1049
	}
1050
1051
endif;
1052