Test Failed
Push — issues/370 ( da4d1b...967cb5 )
by Ravinder
05:08
created

Give_Admin_Settings::verify_nonce()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
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()  ) {
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...
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 void|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
			self::get_settings_pages();
233
234
			// Save settings if data has been posted.
235
			if ( ! empty( $_POST ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
236
				self::save();
237
			}
238
239
			/**
240
			 * Filter the tabs for current setting page.
241
			 *
242
			 * Note: filter dynamically fire on basis of setting page slug.
243
			 * For example: if you register a setting page with give-settings menu slug and general current tab name
244
			 *              then action will be give-settings_tabs_array
245
			 *
246
			 * @since 1.8
247
			 */
248
			$tabs = apply_filters( self::$setting_filter_prefix . '_tabs_array', array() );
249
250
			include 'views/html-admin-settings.php';
251
252
			return true;
253
		}
254
255
		/**
256
		 * Get a setting from the settings API.
257
		 *
258
		 * @since  1.8
259
		 *
260
		 * @param  string $option_name
261
		 * @param  string $field_id
262
		 * @param  mixed  $default
263
		 *
264
		 * @return string|bool
265
		 */
266
		public static function get_option( $option_name = '', $field_id = '', $default = false ) {
267
			// Bailout.
268
			if ( empty( $option_name ) && empty( $field_id ) ) {
269
				return false;
270
			}
271
272
			if ( ! empty( $field_id ) && ! empty( $option_name ) ) {
273
				// Get field value if any.
274
				$option_value = get_option( $option_name );
275
276
				$option_value = ( is_array( $option_value ) && array_key_exists( $field_id, $option_value ) )
277
					? $option_value[ $field_id ]
278
					: $default;
279
			} else {
280
				// If option name is empty but not field name then this means, setting is direct store to option table under there field name.
281
				$option_name = ! $option_name ? $field_id : $option_name;
282
283
				// Get option value if any.
284
				$option_value = get_option( $option_name, $default );
285
			}
286
287
			return $option_value;
288
		}
289
290
		/**
291
		 * Output admin fields.
292
		 *
293
		 * Loops though the give options array and outputs each field.
294
		 *
295
		 * @todo: Refactor this function
296
		 * @since  1.8
297
		 *
298
		 * @param  array  $options     Opens array to output
299
		 * @param  string $option_name Opens array to output
300
		 *
301
		 * @return void
302
		 */
303
		public static function output_fields( $options, $option_name = '' ) {
304
			$current_tab = give_get_current_setting_tab();
305
306
			// Field Default values.
307
			$defaults = array(
308
				'id'               => '',
309
				'class'            => '',
310
				'css'              => '',
311
				'default'          => '',
312
				'desc'             => '',
313
				'table_html'       => true,
314
				'repeat'           => false,
315
				'repeat_btn_title' => __( 'Add Field', 'give' ),
316
			);
317
318
			foreach ( $options as $value ) {
319
				if ( ! isset( $value['type'] ) ) {
320
					continue;
321
				}
322
323
				// Set title.
324
				$defaults['title'] = isset( $value['name'] ) ? $value['name'] : '';
325
326
				// Set default setting.
327
				$value = wp_parse_args( $value, $defaults );
328
329
				// Colorpicker field.
330
				$value['class'] = ( 'colorpicker' === $value['type'] ? trim( $value['class'] ) . ' give-colorpicker' : $value['class'] );
331
				$value['type']  = ( 'colorpicker' === $value['type'] ? 'text' : $value['type'] );
332
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
333
334
				// Custom attribute handling.
335
				$custom_attributes = array();
336
337 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...
338
					foreach ( $value['attributes'] as $attribute => $attribute_value ) {
339
						$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
340
					}
341
				}
342
343
				// Description handling.
344
				$description          = self::get_field_description( $value );
345
346
				// Switch based on type.
347
				switch ( $value['type'] ) {
348
349
					// Section Titles
350
					case 'title':
351
						if ( ! empty( $value['title'] ) || ! empty( $value['desc'] ) ) {
352
							?>
353
							<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...
354
								<?php if ( ! empty( $value['title'] ) ) : ?>
355
									<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...
356
								<?php endif; ?>
357
358
								<?php if ( ! empty( $value['desc'] ) ) : ?>
359
									<?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...
360
								<?php endif; ?>
361
							</div>
362
							<?php
363
						}
364
365
						if ( $value['table_html'] ) {
366
							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...
367
						}
368
369
						if ( ! empty( $value['id'] ) ) {
370
371
							/**
372
							 * Trigger Action.
373
							 *
374
							 * Note: action dynamically fire on basis of field id.
375
							 *
376
							 * @since 1.8
377
							 */
378
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) );
379
						}
380
381
						break;
382
383
					// Section Ends.
384
					case 'sectionend':
385 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...
386
387
							/**
388
							 * Trigger Action.
389
							 *
390
							 * Note: action dynamically fire on basis of field id.
391
							 *
392
							 * @since 1.8
393
							 */
394
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_end' );
395
						}
396
397
						if ( $value['table_html'] ) {
398
							echo '</table>';
399
						}
400
401 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...
402
403
							/**
404
							 * Trigger Action.
405
							 *
406
							 * Note: action dynamically fire on basis of field id.
407
							 *
408
							 * @since 1.8
409
							 */
410
							do_action( 'give_settings_' . sanitize_title( $value['id'] ) . '_after' );
411
						}
412
413
						break;
414
415
					// Standard text inputs and subtypes like 'number'.
416
					case 'colorpicker':
417
					case 'hidden' :
418
						$value['wrapper_class'] = empty( $value['wrapper_class'] ) ? 'give-hidden' : trim( $value['wrapper_class'] ) . ' give-hidden';
419
					case 'text':
420
					case 'email':
421
					case 'number':
422
					case 'password' :
423
						$type = $value['type'];
424
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
425
426
						// Set default value for repeater field if not any value set yet.
427
						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...
428
							$option_value = array( $value['default'] );
429
						}
430
						?>
431
						<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...
432
							<th scope="row" class="titledesc">
433
								<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...
434
							</th>
435
							<td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
436
								<?php if ( $value['repeat'] ) : ?>
437
									<?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...
438
										<p>
439
											<input
440
													name="<?php echo esc_attr( $value['id'] ); ?>[]"
441
													type="<?php echo esc_attr( $type ); ?>"
442
													style="<?php echo esc_attr( $value['css'] ); ?>"
443
													value="<?php echo esc_attr( $field_value ); ?>"
444
													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...
445
												<?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...
446
											/>
447
											<span class="give-remove-setting-field" title="<?php esc_html_e( 'Remove setting field', 'give' ); ?>">-</span>
448
										</p>
449
									<?php endforeach; ?>
450
									<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...
451
								<?php else : ?>
452
									<input
453
											name="<?php echo esc_attr( $value['id'] ); ?>"
454
											id="<?php echo esc_attr( $value['id'] ); ?>"
455
											type="<?php echo esc_attr( $type ); ?>"
456
											style="<?php echo esc_attr( $value['css'] ); ?>"
457
											value="<?php echo esc_attr( $option_value ); ?>"
458
											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...
459
										<?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...
460
									/>
461
								<?php endif; ?>
462
								<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
463
							</td>
464
							</tr><?php
465
							break;
466
467
					// Textarea.
468
					case 'textarea':
469
470
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
471
472
						?>
473
                    <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...
474
                        <th scope="row" class="titledesc">
475
                            <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...
476
                        </th>
477
                        <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
478
								<textarea
479
                                        name="<?php echo esc_attr( $value['id'] ); ?>"
480
                                        id="<?php echo esc_attr( $value['id'] ); ?>"
481
                                        style="<?php echo esc_attr( $value['css'] ); ?>"
482
                                        class="<?php echo esc_attr( $value['class'] ); ?>"
483
                                        rows="10"
484
                                        cols="60"
485
									<?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...
486
                                ><?php echo esc_textarea( $option_value ); ?></textarea>
487
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
488
                        </td>
489
                        </tr><?php
490
						break;
491
492
					// Select boxes.
493
					case 'select' :
494
					case 'multiselect' :
495
496
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
497
498
						?>
499
                    <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...
500
                        <th scope="row" class="titledesc">
501
                            <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...
502
                        </th>
503
                        <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
504
                            <select
505
                                    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...
506
										echo '[]';
507
									} ?>"
508
                                    id="<?php echo esc_attr( $value['id'] ); ?>"
509
                                    style="<?php echo esc_attr( $value['css'] ); ?>"
510
                                    class="<?php echo esc_attr( $value['class'] ); ?>"
511
								<?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...
512
								<?php echo ( 'multiselect' == $value['type'] ) ? 'multiple="multiple"' : ''; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
513
                            >
514
515
								<?php
516
								if ( ! empty( $value['options'] ) ) {
517
									foreach ( $value['options'] as $key => $val ) {
518
										?>
519
                                        <option value="<?php echo esc_attr( $key ); ?>" <?php
520
521
										if ( is_array( $option_value ) ) {
522
											selected( in_array( $key, $option_value ), true );
523
										} else {
524
											selected( $option_value, $key );
525
										}
526
527
										?>><?php echo $val ?></option>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
528
										<?php
529
									}
530
								}
531
								?>
532
533
                            </select> <?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
534
                        </td>
535
                        </tr><?php
536
						break;
537
538
					// Radio inputs.
539
					case 'radio_inline' :
540
						$value['class'] = empty( $value['class'] ) ? 'give-radio-inline' : $value['class'] . ' give-radio-inline';
541
					case 'radio' :
542
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
543
						?>
544
                    <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...
545
                        <th scope="row" class="titledesc">
546
                            <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...
547
                        </th>
548
                        <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...
549
                            <fieldset>
550
                                <ul>
551
									<?php
552 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...
553
										?>
554
                                        <li>
555
                                            <label><input
556
                                                        name="<?php echo esc_attr( $value['id'] ); ?>"
557
                                                        value="<?php echo $key; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$key'
Loading history...
558
                                                        type="radio"
559
                                                        style="<?php echo esc_attr( $value['css'] ); ?>"
560
													<?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...
561
													<?php checked( $key, $option_value ); ?>
562
                                                /> <?php echo $val ?></label>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
563
                                        </li>
564
										<?php
565
									}
566
									?>
567
									<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
568
                            </fieldset>
569
                        </td>
570
                        </tr><?php
571
						break;
572
573
					// Checkbox input.
574
					case 'checkbox' :
575
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
576
						?>
577
                        <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...
578
                            <th scope="row" class="titledesc">
579
                                <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...
580
                            </th>
581
                            <td class="give-forminp">
582
                                <input
583
                                        name="<?php echo esc_attr( $value['id'] ); ?>"
584
                                        id="<?php echo esc_attr( $value['id'] ); ?>"
585
                                        type="checkbox"
586
                                        class="<?php echo esc_attr( isset( $value['class'] ) ? $value['class'] : '' ); ?>"
587
                                        value="1"
588
									<?php checked( $option_value, 'on' ); ?>
589
									<?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...
590
                                />
591
								<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
592
                            </td>
593
                        </tr>
594
						<?php
595
						break;
596
597
					// Multi Checkbox input.
598
					case 'multicheck' :
599
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
600
						$option_value = is_array( $option_value ) ? $option_value : array();
601
						?>
602
                        <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...
603
                            <th scope="row" class="titledesc">
604
                                <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...
605
                            </th>
606
                            <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...
607
                                <fieldset>
608
                                    <ul>
609
										<?php
610 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...
611
											?>
612
                                            <li>
613
                                                <label>
614
                                                    <input
615
                                                            name="<?php echo esc_attr( $value['id'] ); ?>[]"
616
                                                            value="<?php echo $key; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$key'
Loading history...
617
                                                            type="checkbox"
618
                                                            style="<?php echo esc_attr( $value['css'] ); ?>"
619
														<?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...
620
														<?php if ( in_array( $key, $option_value ) ) {
621
															echo 'checked="checked"';
622
														} ?>
623
                                                    /> <?php echo $val ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$val'
Loading history...
624
                                                </label>
625
                                            </li>
626
											<?php
627
										}
628
										?>
629
										<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
630
                                </fieldset>
631
                            </td>
632
                        </tr>
633
						<?php
634
						break;
635
636
					// File input field.
637
					case 'file' :
638
					case 'media' :
639
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
640
						$button_label = esc_html__( sprintf( 'Add or Upload %s', ( 'file' === $value['type'] ? 'File' : 'Image' ) ), 'give' );
641
						$fvalue       = empty( $value['fvalue'] ) ? 'url' : $value['fvalue'];
642
643
						$allow_media_preview_tags = array( 'jpg', 'jpeg', 'png', 'gif', 'ico' );
644
						$preview_image_src        = $option_value ? ( 'id' === $fvalue ? wp_get_attachment_url( $option_value ) : $option_value ) : '#';
645
						$preview_image_extension  = $preview_image_src ? pathinfo( $preview_image_src, PATHINFO_EXTENSION ) : '';
646
						$is_show_preview = in_array( $preview_image_extension, $allow_media_preview_tags );
647
						?>
648
						<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...
649
							<th scope="row" class="titledesc">
650
								<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...
651
							</th>
652
							<td class="give-forminp">
653
								<div class="give-field-wrap">
654
									<label for="<?php echo $value['id'] ?>">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$value'
Loading history...
655
										<input
656
												name="<?php echo esc_attr( $value['id'] ); ?>"
657
												id="<?php echo esc_attr( $value['id'] ); ?>"
658
												type="text"
659
												class="give-input-field<?php echo esc_attr( isset( $value['class'] ) ? ' ' . $value['class'] : '' ); ?>"
660
												value="<?php echo $option_value; ?>"
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$option_value'
Loading history...
661
												style="<?php echo esc_attr( $value['css'] ); ?>"
662
											<?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...
663
										/>&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...
664
										<?php echo $description ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
665
										<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...
666
											<span class="give-delete-image-thumb dashicons dashicons-no-alt"></span>
667
											<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...
668
										</div>
669
									</label>
670
								</div>
671
							</td>
672
							</tr>
673
						<?php
674
						break;
675
676
					// WordPress Editor.
677
					case 'wysiwyg' :
678
						// Get option value.
679
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
680
681
						// Get editor settings.
682
						$editor_settings = ! empty( $value['options'] ) ? $value['options'] : array();
683
						?>
684
                    <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...
685
                        <th scope="row" class="titledesc">
686
                            <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...
687
                        </th>
688
                        <td class="give-forminp">
689
							<?php wp_editor( $option_value, $value['id'], $editor_settings ); ?>
690
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
691
                        </td>
692
                        </tr><?php
693
						break;
694
695
					// Custom: System setting field.
696 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...
697
						?>
698
                    <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...
699
                        <th scope="row" class="titledesc">
700
                            <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...
701
                        </th>
702
                        <td class="give-forminp">
703
							<?php give_system_info_callback(); ?>
704
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
705
                        </td>
706
                        </tr><?php
707
						break;
708
709
					// Custom: Default gateways setting field.
710 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...
711
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
712
						?>
713
                    <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...
714
                        <th scope="row" class="titledesc">
715
                            <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...
716
                        </th>
717
                        <td class="give-forminp">
718
							<?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...
719
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
720
                        </td>
721
                        </tr><?php
722
						break;
723
724
					// Custom: Enable gateways setting field.
725 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...
726
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
727
						?>
728
                    <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...
729
                        <th scope="row" class="titledesc">
730
                            <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...
731
                        </th>
732
                        <td class="give-forminp">
733
							<?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...
734
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
735
                        </td>
736
                        </tr><?php
737
						break;
738
739
					// Custom: Email preview buttons field.
740 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...
741
						?>
742
						<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...
743
						<th scope="row" class="titledesc">
744
							<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...
745
						</th>
746
						<td class="give-forminp">
747
							<?php give_email_preview_buttons_callback( $value ); ?>
748
							<?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
749
                        </td>
750
                        </tr><?php
751
						break;
752
753
					// Custom: API field.
754
					case 'api' :
755
						give_api_callback();
756
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
757
						break;
758
759
					// Custom: Gateway API key.
760
					case 'api_key' :
761
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
762
						$type         = ! empty( $option_value ) ? 'password' : 'text';
763
						?>
764
                    <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...
765
                        <th scope="row" class="titledesc">
766
                            <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...
767
                        </th>
768
                        <td class="give-forminp give-forminp-<?php echo sanitize_title( $value['type'] ) ?>">
769
                            <input
770
                                    name="<?php echo esc_attr( $value['id'] ); ?>"
771
                                    id="<?php echo esc_attr( $value['id'] ); ?>"
772
                                    type="<?php echo esc_attr( $type ); ?>"
773
                                    style="<?php echo esc_attr( $value['css'] ); ?>"
774
                                    value="<?php echo esc_attr( trim( $option_value ) ); ?>"
775
                                    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...
776
								<?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...
777
                            /> <?php echo $description; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
778
                        </td>
779
                        </tr><?php
780
						break;
781
782
					// Custom: Log field.
783
					case 'logs' :
784
785
						// Get current section.
786
						$current_section = $_GET['section'] = give_get_current_setting_section();
787
788
						/**
789
						 * Fires for each tab of logs view.
790
						 *
791
						 * @since 1.0
792
						 */
793
						do_action( "give_logs_view_{$current_section}" );
794
795
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
796
						break;
797
798
					// Custom: Data field.
799
					case 'data' :
800
801
						include  GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-data.php';
802
803
						echo $description;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$description'
Loading history...
804
						break;
805
806
					// Custom: Give Docs Link field type.
807
					case 'give_docs_link' :
808
						?>
809
                    <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...
810
                        <td class="give-docs-link" colspan="2">
811
							<?php
812
							echo '<p class="give-docs-link"><a href="' . esc_url( $value['url'] )
813
							     . '" target="_blank">'
814
							     . 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...
815
							     . '<span class="dashicons dashicons-editor-help"></span></a></p>';
816
							?>
817
                        </td>
818
                        </tr><?php
819
						break;
820
821
					// Default: run an action
822
					// You can add or handle your custom field action.
823
					default:
824
						// Get option value.
825
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
826
						do_action( 'give_admin_field_' . $value['type'], $value, $option_value );
827
						break;
828
				}
829
			}
830
		}
831
832
		/**
833
		 * Helper function to get the formatted description for a given form field.
834
		 * Plugins can call this when implementing their own custom settings types.
835
		 *
836
		 * @since  1.8
837
		 *
838
		 * @param  array $value The form field value array
839
		 *
840
		 * @return string The HTML description of the field.
841
		 */
842
		public static function get_field_description( $value ) {
843
			$description = '';
844
845
			// Support for both 'description' and 'desc' args.
846
			$description_key = isset( $value['description'] ) ? 'description' : 'desc';
847
			$value           = ( isset( $value[ $description_key ] ) && ! empty( $value[ $description_key ] ) ) ? $value[ $description_key ] : '';
848
849
			if ( ! empty( $value ) ) {
850
				$description = '<p class="give-field-description">' . wp_kses_post( $value ) . '</p>';
851
			}
852
853
			return $description;
854
		}
855
856
857
		/**
858
		 * Helper function to get the formated title.
859
		 * Plugins can call this when implementing their own custom settings types.
860
		 *
861
		 * @since  1.8
862
		 *
863
		 * @param  array $value The form field value array
864
		 *
865
		 * @return array The description and tip as a 2 element array
866
		 */
867
		public static function get_field_title( $value ) {
868
			$title = esc_html( $value['title'] );
869
870
			// If html tag detected then allow them to print.
871
			if ( strip_tags( $title ) ) {
872
				$title = $value['title'];
873
			}
874
875
			return $title;
876
		}
877
878
		/**
879
		 * Save admin fields.
880
		 *
881
		 * Loops though the give options array and outputs each field.
882
		 *
883
		 * @since  1.8
884
		 *
885
		 * @param  array  $options     Options array to output
886
		 * @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.
887
		 *
888
		 * @return bool
889
		 */
890
		public static function save_fields( $options, $option_name = '' ) {
891
			if ( empty( $_POST ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
892
				return false;
893
			}
894
895
			// Options to update will be stored here and saved later.
896
			$update_options = array();
897
898
			// Loop options and get values to save.
899
			foreach ( $options as $option ) {
900
				if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
901
					continue;
902
				}
903
904
				// Get posted value.
905
				if ( strstr( $option['id'], '[' ) ) {
906
					parse_str( $option['id'], $option_name_array );
907
					$field_option_name = current( array_keys( $option_name_array ) );
908
					$setting_name      = key( $option_name_array[ $field_option_name ] );
909
					$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...
910
				} else {
911
					$field_option_name = $option['id'];
912
					$setting_name      = '';
913
					$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...
914
				}
915
916
				// Format the value based on option type.
917
				switch ( $option['type'] ) {
918
					case 'checkbox' :
919
						$value = is_null( $raw_value ) ? '' : 'on';
920
						break;
921
					case 'wysiwyg'  :
922
					case 'textarea' :
923
						$value = wp_kses_post( trim( $raw_value ) );
924
						break;
925
					case 'multiselect' :
926
						$value = array_filter( array_map( 'give_clean', (array) $raw_value ) );
927
						break;
928
					default :
929
						$value = give_clean( $raw_value );
930
						break;
931
				}
932
933
				/**
934
				 * Sanitize the value of an option.
935
				 *
936
				 * @since 1.8
937
				 */
938
				$value = apply_filters( 'give_admin_settings_sanitize_option', $value, $option, $raw_value );
939
940
				/**
941
				 * Sanitize the value of an option by option name.
942
				 *
943
				 * @since 1.8
944
				 */
945
				$value = apply_filters( "give_admin_settings_sanitize_option_{$field_option_name}", $value, $option, $raw_value );
946
947
				if ( is_null( $value ) ) {
948
					continue;
949
				}
950
951
				// Check if option is an array and handle that differently to single values.
952
				if ( $field_option_name && $setting_name ) {
953
					if ( ! isset( $update_options[ $field_option_name ] ) ) {
954
						$update_options[ $field_option_name ] = get_option( $field_option_name, array() );
955
					}
956
					if ( ! is_array( $update_options[ $field_option_name ] ) ) {
957
						$update_options[ $field_option_name ] = array();
958
					}
959
					$update_options[ $field_option_name ][ $setting_name ] = $value;
960
				} else {
961
					$update_options[ $field_option_name ] = $value;
962
				}
963
			}
964
965
			// Save all options in our array or there own option name i.e. option id.
966
			if ( empty( $option_name ) ) {
967
				foreach ( $update_options as $name => $value ) {
968
					update_option( $name, $value );
969
970
					/**
971
					 * Trigger action.
972
					 *
973
					 * Note: This is dynamically fire on basis of option name.
974
					 *
975
					 * @since 1.8
976
					 */
977
					do_action( "give_save_option_{$name}", $value, $name );
978
				}
979
			} else {
980
				$old_options    = ( $old_options = get_option( $option_name ) ) ? $old_options : array();
981
				$update_options = array_merge( $old_options, $update_options );
982
983
				update_option( $option_name, $update_options );
984
985
				/**
986
				 * Trigger action.
987
				 *
988
				 * Note: This is dynamically fire on basis of setting name.
989
				 *
990
				 * @since 1.8
991
				 */
992
				do_action( "give_save_settings_{$option_name}", $update_options, $option_name );
993
			}
994
995
			return true;
996
		}
997
	}
998
999
endif;
1000