Completed
Push — issues/1038 ( cbf4f0...f74a66 )
by Ravinder
22:12 queued 02:12
created

Give_Admin_Settings::backward_compatibility_1_8()   D

Complexity

Conditions 9
Paths 97

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

Loading history...
792
		 */
793
		private function backward_compatibility_1_8( &$field ) {
794
			if ( ! empty( $field['id'] ) ) {
795
				$field_args = array(
796
					'name'               => ( ! empty( $field['id'] ) ? $field['id'] : $field['name'] ),
797
					'label'              => self::get_field_title( $field ),
798
					'field_attributes'   => array(
799
						'class' => ( empty( $field['class'] ) ? '' : ' ' . esc_attr( $field['class'] ) ),
800
						'style' => esc_attr( $field['css'] ),
801
						'id'    => ( ! empty( $field['id'] ) ? $field['id'] : $field['name'] ),
802
803
					),
804
					'wrapper_attributes' => array(
805
						'class'  => ( ! empty( $field['wrapper_class'] ) ? $field['wrapper_class'] : '' ),
806
						'valign' => 'top',
807
					),
808
				);
809
810
				if( 'multiselect' === $field['type'] ) {
811
					$field_args['type'] = 'multi_select';
812
				}elseif ( 'multicheck' === $field['type'] ) {
813
					$field_args['type'] = 'multi_checkbox';
814
				}
815
816
				if ( ! empty( $field['attributes'] ) ) {
817
					$field_args['field_attributes'] = array_merge( $field_args['field_attributes'], $field['attributes'] );
818
				}
819
820
				$field = array_merge( $field, $field_args );
821
			}
822
		}
823
	}
824
825
endif;
826