Completed
Push — issues/1038 ( 82778e...ccd3e1 )
by Ravinder
19:16
created

Give_Admin_Settings::backward_compatibility_1_8()   D

Complexity

Conditions 10
Paths 129

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 129
nop 1
dl 0
loc 32
rs 4.606
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
						$value = give_backward_compatibility_setting_api_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
						$value = give_backward_compatibility_setting_api_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
						$value = give_backward_compatibility_setting_api_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
						$value = give_backward_compatibility_setting_api_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
						$value = give_backward_compatibility_setting_api_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
						$value = give_backward_compatibility_setting_api_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
						$value = give_backward_compatibility_setting_api_1_8( $value );
503
504
						// Set field value.
505
						$value['value'] = self::get_option( $option_name, $value['id'], $value['default'] );
506
507
						// Set layout.
508
						$value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) );
509
510
						echo Give_Fields_API::render_tag( $value );
511
						break;
512
513
					// WordPress Editor.
514
					case 'wysiwyg' :
515
						$value = give_backward_compatibility_setting_api_1_8( $value );
516
517
						// Set field value.
518
						$value['value'] = self::get_option( $option_name, $value['id'], $value['default'] );
519
520
						// Set layout.
521
						$value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) );
522
						
523
						echo Give_Fields_API::render_tag( $value );
524
						break;
525
526
					// Custom: Give Docs Link field type.
527
					case 'give_docs_link' :
528
						$value = give_backward_compatibility_setting_api_1_8( $value );
529
530
						// Set layout.
531
						$value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) );
532
						$value['before_field'] = '<td class="give-forminp give-forminp-' . sanitize_title( $value['type'] ) . ' give-docs-link" colspan="2">';
533
534
						echo Give_Fields_API::render_tag( $value );
535
						break;
536
537
					case 'group':
538
						// Set field value.
539
						$value['value'] = give_clean( self::get_option( $option_name, $value['name'], $value['default'] ) );
540
541
						// Set layout.
542
						$value = array_merge( $value, self::get_field_wrapper( $value, $option_name ) );
543
544
						echo Give_Fields_API::render_tag( $value );
545
						break;
546
547
					// Default: run an action
548
					// You can add or handle your custom field action.
549
					default:
550
						// Get option value.
551
						$option_value = self::get_option( $option_name, $value['id'], $value['default'] );
552
						do_action( 'give_admin_field_' . $value['type'], $value, $option_value );
553
						break;
554
				}
555
			}
556
		}
557
558
		/**
559
		 * Helper function to get the formatted description for a given form field.
560
		 * Plugins can call this when implementing their own custom settings types.
561
		 *
562
		 * @since  1.8
563
		 *
564
		 * @param  array $value The form field value array
565
		 *
566
		 * @return string The HTML description of the field.
567
		 */
568
		public static function get_field_description( $value ) {
569
			$description = '';
570
571
			// Support for both 'description' and 'desc' args.
572
			$description_key = isset( $value['description'] ) ? 'description' : 'desc';
573
			$value           = ( isset( $value[ $description_key ] ) && ! empty( $value[ $description_key ] ) ) ? $value[ $description_key ] : '';
574
575
			if ( ! empty( $value ) ) {
576
				$description = '<p class="give-field-description">' . wp_kses_post( $value ) . '</p>';
577
			}
578
579
			return $description;
580
		}
581
582
583
		/**
584
		 * Helper function to get the formated title.
585
		 * Plugins can call this when implementing their own custom settings types.
586
		 *
587
		 * @since  1.8
588
		 *
589
		 * @param  array $value The form field value array
590
		 *
591
		 * @return array The description and tip as a 2 element array
592
		 */
593
		public static function get_field_title( $value ) {
594
			// Backward compatibility: version 1.8
595
			$title = ! empty( $value['id'] )
596
				? ( ! empty( $value['title'] ) ? $value['title'] : $value['name'] )
597
				: ( ! empty( $value['label'] ) ? $value['label'] : '' );
598
599
			// If html tag detected then allow them to print.
600
			if ( ! strip_tags( $title ) ) {
601
				$title = esc_html( $title );
602
			}
603
604
			return $title;
605
		}
606
607
		/**
608
		 * Save admin fields.
609
		 *
610
		 * Loops though the give options array and outputs each field.
611
		 *
612
		 * @since  1.8
613
		 *
614
		 * @param  array  $options     Options array to output
615
		 * @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.
616
		 *
617
		 * @return bool
618
		 */
619
		public static function save_fields( $options, $option_name = '' ) {
620
			if ( empty( $_POST ) ) {
621
				return false;
622
			}
623
624
			// Options to update will be stored here and saved later.
625
			$update_options = array();
626
627
			// Loop options and get values to save.
628
			foreach ( $options as $option ) {
629
				// Backward compatibility ( 1.8=<version>1.9)
630
				$option['name'] = ! empty( $option['id'] ) ? $option['id'] : $option['name'];
631
632
				if (
633
					! isset( $option['name'] )
634
					|| ! isset( $option['type'] )
635
					|| empty( $option['name'] )
636
				) {
637
					continue;
638
				}
639
640
				// Backward compatibility: version >= 1.8, version < 1.9
641
				$option['name'] = ! empty( $option['id'] ) ?  $option['id'] : $option['name'];
642
643
				// Get posted value.
644
				if ( strstr( $option['name'], '[' ) ) {
645
					parse_str( $option['name'], $option_name_array );
646
					$field_option_name = current( array_keys( $option_name_array ) );
647
					$setting_name      = key( $option_name_array[ $field_option_name ] );
648
					$raw_value         = isset( $_POST[ $field_option_name ][ $setting_name ] ) ? wp_unslash( $_POST[ $field_option_name ][ $setting_name ] ) : null;
649
				} else {
650
					$field_option_name = $option['name'];
651
					$setting_name      = '';
652
					$raw_value         = isset( $_POST[ $option['name'] ] ) ? wp_unslash( $_POST[ $option['name'] ] ) : null;
653
				}
654
655
				// Format the value based on option type.
656
				switch ( $option['type'] ) {
657
					case 'checkbox' :
658
						$value = is_null( $raw_value ) ? '' : 'on';
659
						break;
660
					case 'wysiwyg'  :
661
					case 'textarea' :
662
						$value = wp_kses_post( trim( $raw_value ) );
663
						break;
664
					case 'multiselect' :
665
						$value = array_filter( array_map( 'give_clean', (array) $raw_value ) );
666
						break;
667
					default :
668
						$value = give_clean( $raw_value );
669
						break;
670
				}
671
672
				/**
673
				 * Sanitize the value of an option.
674
				 *
675
				 * @since 1.8
676
				 */
677
				$value = apply_filters( 'give_admin_settings_sanitize_option', $value, $option, $raw_value );
678
679
				/**
680
				 * Sanitize the value of an option by option name.
681
				 *
682
				 * @since 1.8
683
				 */
684
				$value = apply_filters( "give_admin_settings_sanitize_option_{$field_option_name}", $value, $option, $raw_value );
685
686
				if ( is_null( $value ) ) {
687
					continue;
688
				}
689
690
				// Check if option is an array and handle that differently to single values.
691
				if ( $field_option_name && $setting_name ) {
692
					if ( ! isset( $update_options[ $field_option_name ] ) ) {
693
						$update_options[ $field_option_name ] = get_option( $field_option_name, array() );
694
					}
695
					if ( ! is_array( $update_options[ $field_option_name ] ) ) {
696
						$update_options[ $field_option_name ] = array();
697
					}
698
					$update_options[ $field_option_name ][ $setting_name ] = $value;
699
				} else {
700
					$update_options[ $field_option_name ] = $value;
701
				}
702
			}
703
704
			// Save all options in our array or there own option name i.e. option id.
705
			if ( empty( $option_name ) ) {
706
				foreach ( $update_options as $name => $value ) {
707
					update_option( $name, $value );
708
709
					/**
710
					 * Trigger action.
711
					 *
712
					 * Note: This is dynamically fire on basis of option name.
713
					 *
714
					 * @since 1.8
715
					 */
716
					do_action( "give_save_option_{$name}", $value, $name );
717
				}
718
			} else {
719
				$old_options    = ( $old_options = get_option( $option_name ) ) ? $old_options : array();
720
				$update_options = array_merge( $old_options, $update_options );
721
722
				update_option( $option_name, $update_options );
723
724
				/**
725
				 * Trigger action.
726
				 *
727
				 * Note: This is dynamically fire on basis of setting name.
728
				 *
729
				 * @since 1.8
730
				 */
731
				do_action( "give_save_settings_{$option_name}", $update_options, $option_name );
732
			}
733
734
			return true;
735
		}
736
737
738
		/**
739
		 * Get field wrapper.
740
		 *
741
		 * @since  1.9
742
		 * @access private
743
		 *
744
		 * @param array  $field
745
		 * @param string $option_name
746
		 *
747
		 * @return array
748
		 */
749
		private function get_field_wrapper( $field, $option_name ) {
750
			$field_args = array(
751
				'before_label' => '<th scope="row" class="titledesc">',
752
				'after_label'  => '</th>',
753
				'value'        => ! empty( $field['value'] ) ? $field['value'] : give_clean( self::get_option( $option_name, $field['name'], $field['default'] ) ),
754
				'wrapper_type' => 'tr',
755
				'before_field' => '<td class="give-forminp give-forminp-' . sanitize_title( $field['type'] ) . '">',
756
				'after_field'  => self::get_field_description( $field ) . '</td>',
757
			);
758
759
			return $field_args;
760
		}
761
	}
762
763
endif;
764