Completed
Push — issues/1556 ( 619bdc )
by Ravinder
16:53
created

Give_CMB2_Settings_Loader::en_translation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
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 19 and the first side effect is on line 670.

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 CMB2 settings backward compatibility.
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_CMB2_Settings_Loader
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 ( ! class_exists( 'Give_CMB2_Settings_Loader' ) ) :
13
14
	/**
15
	 * This class loads the cmb2 settings.
16
	 *
17
	 * @since 1.8
18
	 */
19
	class Give_CMB2_Settings_Loader {
20
21
		/**
22
		 * @since 1.8
23
		 * @var   Give_Plugin_Settings $prev_settings Previous setting class object.
24
		 */
25
		private $id;
26
27
		/**
28
		 * @since 1.8
29
		 * @var   Give_Plugin_Settings $prev_settings Previous setting class object.
30
		 */
31
		private $prev_settings;
32
33
		/**
34
		 * @since 1.8
35
		 * @var   string $current_tab Current setting section.
36
		 */
37
		protected $current_tab;
38
39
		/**
40
		 * @since 1.8
41
		 * @var   string $current_tab Current setting section.
42
		 */
43
		private $current_section;
44
45
46
		/**
47
		 * Give_CMB2_Settings_Loader constructor.
48
		 */
49
		function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
			// Get previous setting class object.
51
			$this->prev_settings = new Give_Plugin_Settings();
52
53
			// Get current tab.
54
			$this->current_tab     = give_get_current_setting_tab();
55
			$this->current_section = empty( $_REQUEST['section'] ) ? ( current( array_keys( $this->get_sections() ) ) ) : sanitize_title( $_REQUEST['section'] );
56
57
			// Tab ID.
58
			$this->id = $this->current_tab;
59
60
			// Add addon tabs.
61
			add_filter( 'give-settings_tabs_array', array( $this, 'add_addon_settings_page' ), 999999 );
62
63
			// Add save hook to addons.
64
			add_action( 'give-settings_get_settings_pages', array( $this, 'setup_addon_save_hook' ), 999999 );
65
66
			// Add backward compatibility filters plugin settings.
67
			$setting_tabs = array( 'general', 'gateways', 'display', 'emails', 'addons', 'licenses' );
68
69
			// Filter Payment Gateways settings.
70
			if ( in_array( $this->current_tab, $setting_tabs ) ) {
71
				add_filter( "give_get_settings_{$this->current_tab}", array(
72
					$this,
73
					'get_filtered_addon_settings',
74
				), 999999, 1 );
75
				add_filter( "give_get_sections_{$this->current_tab}", array(
76
					$this,
77
					'get_filtered_addon_sections',
78
				), 999999, 1 );
79
			}
80
		}
81
82
		/**
83
		 * Default setting tab.
84
		 *
85
		 * @since  1.8
86
		 *
87
		 * @param  $setting_tab
88
		 *
89
		 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string|false?

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...
90
		 */
91
		function set_default_setting_tab( $setting_tab ) {
0 ignored issues
show
Unused Code introduced by
The parameter $setting_tab is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
92
			$default_tab = '';
93
94
			// Set default tab to first setting tab.
95
			if ( $sections = array_keys( $this->get_sections() ) ) {
96
				$default_tab = current( $sections );
97
			}
98
99
			return $default_tab;
100
		}
101
102
		/**
103
		 * Add addon setting pages.
104
		 *
105
		 * @since  1.8
106
		 *
107
		 * @param  $pages
108
		 *
109
		 * @return mixed
110
		 */
111
		function add_addon_settings_page( $pages ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
112
			// Previous setting page.
113
			$previous_pages = $this->prev_settings->give_get_settings_tabs();
114
115
			// API and System Info setting tab merge to Tools setting tab, so remove them from tabs.
116
			unset( $previous_pages['api'] );
117
			unset( $previous_pages['system_info'] );
118
119
			// Tab is not register.
120
			$pages_diff = array_keys( array_diff( $previous_pages, $pages ) );
121
122
			// Merge old settings with new settings.
123
			$pages = array_merge( $pages, $previous_pages );
124
125
			if ( in_array( $this->current_tab, $pages_diff ) ) {
126
				// Filter & actions.
127
				add_filter( "give_default_setting_tab_section_{$this->current_tab}", array(
128
					$this,
129
					'set_default_setting_tab',
130
				), 10 );
131
				add_action( "give-settings_sections_{$this->current_tab}_page", array( $this, 'output_sections' ) );
132
				add_action( "give-settings_settings_{$this->current_tab}_page", array( $this, 'output' ), 10 );
133
				add_action( "give-settings_save_{$this->current_tab}", array( $this, 'save' ) );
134
			}
135
136
			return $pages;
137
		}
138
139
140
		/**
141
		 * Setup save addon data hook.
142
		 *
143
		 * @since  1.8
144
		 *
145
		 * @param  $pages
146
		 *
147
		 * @return mixed
148
		 */
149
		function setup_addon_save_hook( $pages ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
150
			$page_ids = array();
151
152
			foreach ( $pages as $page ) {
153
				$page_ids = $page->add_settings_page( $page_ids );
154
			}
155
156
			// Previous setting page.
157
			$previous_pages = $this->prev_settings->give_get_settings_tabs();
158
159
			// API and System Info setting tab merge to Tools setting tab, so remove them from tabs.
160
			unset( $previous_pages['api'] );
161
			unset( $previous_pages['system_info'] );
162
163
			// Tab is not register.
164
			$pages_diff = array_keys( array_diff( $previous_pages, $page_ids ) );
165
166
			// Merge old settings with new settings.
167
			$pages = array_merge( $page_ids, $previous_pages );
168
169
			if ( in_array( $this->current_tab, $pages_diff ) ) {
170
				// Filter & actions.
171
				add_action( "give-settings_save_{$this->current_tab}", array( $this, 'save' ) );
172
			}
173
174
			return $pages;
175
		}
176
177
		/**
178
		 * Get section name from section title
179
		 *
180
		 * @since  1.8
181
		 *
182
		 * @param  $field_name
183
		 *
184
		 * @return string
185
		 */
186
		function get_section_name( $field_name ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
187
			// Bailout.
188
			if ( empty( $field_name ) ) {
189
				return $field_name;
190
			}
191
192
			$section_name = explode( ' ', $field_name );
193
194
			// Output.
195
			return strip_tags( implode( ' ', $section_name ) );
196
		}
197
198
199
		/**
200
		 * Do not translate string
201
		 *
202
		 * @since  1.0
203
		 * @access public
204
		 *
205
		 * @param $translation
206
		 * @param $text
207
		 *
208
		 * @return mixed
209
		 */
210
		public function en_translation( $translation, $text ) {
211
			return $text;
212
		}
213
214
215
		/**
216
		 * Get addon sections.
217
		 *
218
		 * @since  1.8
219
		 *
220
		 * @param  array $sections Array of setting fields (Optional).
221
		 *
222
		 * @return mixed
223
		 */
224
		function get_filtered_addon_sections( $sections = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
225
			// New sections.
226
			$new_sections = array();
227
			$sections_ID  = array_keys( $sections );
228
			$setting_fields = $this->prev_settings->give_settings( $this->current_tab );
229
230
			// We need untranslated settings for backward compatibility.
231
			add_filter( 'gettext', array( $this, 'en_translation' ), 10, 2 );
232
			$en_setting_fields = $this->prev_settings->give_settings( $this->current_tab );
233
			remove_filter( 'gettext', array( $this, 'en_translation' ), 10, 2 );
234
			
235
			if ( ! empty( $setting_fields ) && ! empty( $setting_fields['fields'] ) ) {
236
237
				foreach ( $setting_fields['fields'] as $index => $field ) {
238
					// Collect new sections from addons.
239
					if ( 'give_title' !== $field['type'] ) {
240
						continue;
241
					}
242
243
					// Untranslated setting name.
244
					$en_setting_field_name = isset( $en_setting_fields['fields'][ $index ]['name'] ) ? $en_setting_fields['fields'][ $index ]['name'] : '';
245
246
					// Section name.
247
					$field['name'] = isset( $field['name'] ) ? $field['name'] : '';
248
					$section_name  = $this->get_section_name( $field['name'] );
249
250
					// Check if section name exit and section title array is not empty.
251
					if ( ! empty( $sections ) && ! empty( $en_setting_field_name ) ) {
252
253
						// Bailout: Do not load section if it is already exist.
254
						if (
255
							in_array( sanitize_title( $en_setting_field_name ), $sections_ID ) // Check section id.
256
							|| in_array( $section_name, $sections )                            // Check section name.
257
						) {
258
							continue;
259
						}
260
					}
261
262
					// Collect new sections from addons.
263
					$new_sections[ sanitize_title( $field['name'] ) ] = $section_name;
264
				}
265
			}
266
267
			// Add new section.
268
			if ( ! empty( $new_sections ) ) {
269
				$sections = array_merge( $sections, $new_sections );
270
			}
271
272
			// Output.
273
			return $sections;
274
		}
275
276
277
		/**
278
		 * Get setting fields.
279
		 *
280
		 * @since  1.8
281
		 *
282
		 * @param  array $settings       List of settings.
283
		 * @param  array $setting_fields Main tab settings data.
284
		 *
285
		 * @return array
286
		 */
287
		function get_filtered_addon_settings( $settings, $setting_fields = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
288
			global $wp_filter;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
289
290
			$new_setting_fields = array();
291
292
			if ( ! empty( $settings ) ) {
293
				// Bailout: If setting array contain first element of type title then it means it is already created with new setting api (skip this section ).
294
				if ( isset( $settings[0]['type'] ) && 'title' == $settings[0]['type'] ) {
295
					foreach ( $settings as $setting ) {
296
						$new_setting_fields[] = $setting;
297
298
						// We need setting only till first section end.
299
						if ( 'sectionend' === $setting['type'] ) {
300
							break;
301
						}
302
					}
303
304
					return $new_setting_fields;
305
				}
306
307
				// Store title field id.
308
				$prev_title_field_id = '';
309
310
				// Create new setting fields.
311
				foreach ( $settings as $index => $field ) {
312
313
					// Bailout: Must need field type to process.
314
					if ( ! isset( $field['type'] ) ) {
315
						continue;
316
					}
317
318
					// Set wrapper class if any.
319
					if ( ! empty( $field['row_classes'] ) ) {
320
						$field['wrapper_class'] = $field['row_classes'];
321
						unset( $field['row_classes'] );
322
					}
323
324
					$field['name'] = ! isset( $field['name'] ) ? '' : $field['name'];
325
					$field['desc'] = ! isset( $field['desc'] ) ? '' : $field['desc'];
326
327
					// Modify cmb2 setting fields.
328
					switch ( $field['type'] ) {
329
						case 'text' :
330
						case 'file' :
331
							$field['css'] = 'width:25em;';
332
							break;
333
334
						case 'text_small' :
335
							$field['type'] = 'text';
336
							break;
337
338
						case 'text_email' :
339
							$field['type'] = 'email';
340
							$field['css']  = 'width:25em;';
341
							break;
342
343
						case 'radio_inline' :
344
							$field['type']  = 'radio';
345
							$field['class'] = 'give-radio-inline';
346
							break;
347
348
						case 'give_title' :
349
							$field['type'] = 'title';
350
351
							// Since we are showing sections, so there now ned to show horizontal rules.
352
							if ( '<hr>' === $field['desc'] ) {
353
								$field['desc'] = '';
354
							}
355
356
							break;
357
					}
358
359
					if ( 'title' === $field['type'] ) {
360
361
						// If we do not have first element as title then these field will be skip from frontend
362
						// because there are not belong to any section, so put all abandon fields under first section.
363
						if ( $index && empty( $prev_title_field_id ) ) {
364
							array_unshift(
365
								$new_setting_fields,
366
								array(
367
									'title' => $field['name'],
368
									'type'  => $field['type'],
369
									'desc'  => $field['desc'],
370
									'id'    => $field['id'],
371
								)
372
							);
373
374
							$prev_title_field_id = $field['id'];
375
376
							continue;
377
						} elseif ( $index ) {
378
							// Section end.
379
							$new_setting_fields[] = array(
380
								'type' => 'sectionend',
381
								'id'   => $prev_title_field_id,
382
							);
383
						}
384
385
						// Section start.
386
						$new_setting_fields[] = array(
387
							'title' => $field['name'],
388
							'type'  => $field['type'],
389
							'desc'  => $field['desc'],
390
							'id'    => $field['id'],
391
						);
392
393
						$prev_title_field_id = $field['id'];
394
					} else {
395
396
						// setting fields
397
						$new_setting_fields[] = $field;
398
					}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
399
				}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
400
401
				// Section end.
402
				$new_setting_fields[] = array(
403
					'type' => 'sectionend',
404
					'id'   => $prev_title_field_id,
405
				);
406
407
				// Check if setting page has title section or not.
408
				// If setting page does not have title section  then add title section to it and fix section end array id.
409
				if ( 'title' !== $new_setting_fields[0]['type'] ) {
410
					array_unshift(
411
						$new_setting_fields,
412
						array(
413
							'title' => ( isset( $settings['give_title'] ) ? $settings['give_title'] : '' ),
414
							'type'  => 'title',
415
							'desc'  => ! empty( $setting_fields['desc'] ) ? $setting_fields['desc'] : '',
416
							'id'    => ( isset( $settings['id'] ) ? $settings['id'] : '' ),
417
						)
418
					);
419
420
					// Update id in section end array if does not contain.
421
					if ( empty( $new_setting_fields[ count( $new_setting_fields ) - 1 ]['id'] ) ) {
422
						$new_setting_fields[ count( $new_setting_fields ) - 1 ]['id'] = ( isset( $settings['id'] ) ? $settings['id'] : '' );
423
					}
424
				}
425
426
				// Return only section related settings.
427
				if ( $sections = $this->get_filtered_addon_sections() ) {
428
					$new_setting_fields = $this->get_section_settings( $new_setting_fields );
429
				}
430
431
				// Third party plugin backward compatibility.
432
				$wp_filter_keys = array_keys( $wp_filter );
433
				foreach ( $new_setting_fields as $index => $field ) {
434
					if ( ! isset( $field['type'] ) || in_array( $field['type'], array( 'title', 'sectionend' ) ) ) {
435
						continue;
436
					}
437
438
					$cmb2_filter_name = "cmb2_render_{$field['type']}";
439
440
					if ( in_array( $cmb2_filter_name, $wp_filter_keys ) ) {
441
442
						if ( 0 >= version_compare( 4.7, get_bloginfo( 'version' ) ) && ! empty( $wp_filter[ $cmb2_filter_name ]->callbacks ) ) {
443
							$cmb2_filter_arr = current( $wp_filter[ $cmb2_filter_name ]->callbacks );
444
						} else {
445
							$cmb2_filter_arr = current( $wp_filter[ $cmb2_filter_name ] );
446
						}
447
448
						if ( ! empty( $cmb2_filter_arr ) ) {
449
							// Note: function can be called either globally or with class object, it depends on how developer invoke it.
450
							$new_setting_fields[ $index ]['func'] = current( $cmb2_filter_arr );
451
							add_action( "give_admin_field_{$field['type']}", array(
452
								$this,
453
								'addon_setting_field',
454
							), 10, 2 );
455
						}
456
					}
457
				}
458
459
				return $new_setting_fields;
460
			}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
461
462
			return $settings;
463
		}
464
465
466
		/**
467
		 * Get section related setting.
468
		 *
469
		 * @since 1.8
470
		 *
471
		 * @param $tab_settings
472
		 *
473
		 * @return array
474
		 */
475
		function get_section_settings( $tab_settings ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
476
			$current_section = give_get_current_setting_section();
477
478
			// Note: If we are opening default tab for addon setting then it is possible that we will get empty string as current section
479
			// because default section filter added after save hook fire, so we will always get problem to save first section [default] or if there are only on section
480
			// This is hack to fix this.
481
			if ( empty( $current_section ) ) {
482
				$current_section = $this->set_default_setting_tab( $current_section );
483
			}
484
485
			$section_start               = false;
486
			$section_end                 = false;
487
			$section_only_setting_fields = array();
488
489
			foreach ( $tab_settings as $field ) {
490
				if ( 'title' == $field['type'] && $current_section == sanitize_title( $field['title'] ) ) {
491
					$section_start = true;
492
				}
493
494
				if ( ! $section_start || $section_end ) {
495
					continue;
496
				}
497
498
				if ( $section_start && ! $section_end ) {
499
					if ( 'sectionend' == $field['type'] ) {
500
						$section_end = true;
501
					}
502
					$section_only_setting_fields[] = $field;
503
				}
504
			}
505
506
			// Remove title from setting, prevent it from render in setting tab.
507
			$section_only_setting_fields[0]['title'] = '';
508
509
			return apply_filters( "give_get_settings_{$this->current_tab}_{$current_section}", $section_only_setting_fields, $tab_settings );
510
		}
511
512
513
		/**
514
		 * CMB2 addon setting fields backward compatibility.
515
		 *
516
		 * @since  1.8
517
		 *
518
		 * @param  array $field
519
		 * @param  mixed $saved_value
520
		 *
521
		 * @return void
522
		 */
523
		function addon_setting_field( $field, $saved_value ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
524
			// Create object for cmb2  function callback backward compatibility.
525
			// Note: Do not call any cmb2 function on these objects
526
			$field_obj      = (object) array( 'args' => $field );
527
			$field_type_obj = (object) array( 'field' => $field_obj );
528
529
			switch ( $this->current_tab ) :
530
				case 'licenses':
531
					?>
532
					<div class="give-settings-wrap give-settings-wrap-<?php echo $this->current_tab; ?>">
533
						<?php $field['func']['function']( $field_obj, $saved_value, '', '', $field_type_obj ); ?>
534
					</div>
535
					<?php break;
536
537
				default :
538
					$colspan = 'colspan="2"';
539
					?>
540
					<tr valign="top">
541
						<?php if ( ! empty( $field['name'] ) && ! in_array( $field['name'], array( '&nbsp;' ) ) ) : ?>
542
							<th scope="row" class="titledesc">
543
								<label
544
										for="<?php echo esc_attr( $field['name'] ); ?>"><?php echo $field['title']; ?></label>
545
							</th>
546
							<?php $colspan = ''; ?>
547
						<?php endif; ?>
548
						<td class="give-forminp" <?php echo $colspan; ?>>
549
							<?php
550
							if ( is_array( $field['func']['function'] ) ) {
551
								$classname = $field['func']['function'][0];
552
								$function_name = $field['func']['function'][1];
553
								$classname->$function_name( $field_obj, $saved_value, '', '', $field_type_obj );
554
							} else {
555
								$function_name = $field['func']['function'];
556
								$function_name( $field_obj, $saved_value, '', '', $field_type_obj );
557
							}
558
							?>
559
						</td>
560
					</tr>
561
					<?php
562
			endswitch;
563
		}
564
565
		/**
566
		 * Get sections.
567
		 *
568
		 * @since  1.8
569
		 * @return array
570
		 */
571
		public function get_sections() {
572
			$sections = array();
573
574
			if ( ( $setting_fields = $this->prev_settings->give_settings( $this->current_tab ) ) && ! empty( $setting_fields['fields'] ) ) {
575
				foreach ( $setting_fields['fields'] as $field ) {
576
					if ( 'give_title' == $field['type'] ) {
577
						$sections[ sanitize_title( $field['name'] ) ] = $this->get_section_name( $field['name'] );
578
					}
579
				}
580
			}
581
582
			return $sections;
583
		}
584
585
586
		/**
587
		 * Get setting fields.
588
		 *
589
		 * @since  1.8
590
		 * @return array
591
		 */
592
		function get_settings() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
593
			global $wp_filter;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
594
595
			$new_setting_fields = array();
596
597
			if ( $setting_fields = $this->prev_settings->give_settings( $this->current_tab ) ) {
598
				if ( isset( $setting_fields['fields'] ) ) {
599
600
					$tab_data = array(
601
						'id'         => $setting_fields['id'],
602
						'give_title' => $setting_fields['give_title'],
603
						'desc'       => ( isset( $setting_fields['desc'] ) ? $setting_fields['desc'] : '' ),
604
					);
605
606
					$new_setting_fields = $this->get_filtered_addon_settings( $setting_fields['fields'], $tab_data );
607
				}
608
			}
609
610
			return $new_setting_fields;
611
		}
612
613
		/**
614
		 * Output sections.
615
		 *
616
		 * @since  1.8
617
		 * @return void
618
		 */
619
		public function output_sections() {
620
			$sections = $this->get_sections();
621
622
			// Show section settings only if setting section exist.
623
			if ( $this->current_section && ! in_array( $this->current_section, array_keys( $sections ) ) ) {
624
				echo '<div class="error"><p>' . __( 'Oops, this settings page does not exist.', 'give' ) . '</p></div>';
625
				$GLOBALS['give_hide_save_button'] = true;
626
627
				return;
628
			}
629
630
			// Bailout.
631
			if ( empty( $sections ) ) {
632
				return;
633
			}
634
635
			echo '<ul class="subsubsub">';
636
637
			$array_keys = array_keys( $sections );
638
639
			foreach ( $sections as $id => $label ) {
640
				echo '<li><a href="' . admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=' . $this->current_tab . '&section=' . sanitize_title( $id ) ) . '" class="' . ( $this->current_section == $id ? 'current' : '' ) . '">' . strip_tags( $label ) . '</a> ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
641
			}
642
643
			echo '</ul><br class="clear" /><hr>';
644
		}
645
646
		/**
647
		 * Output the settings.
648
		 *
649
		 * @since  1.8
650
		 * @return void
651
		 */
652
		public function output() {
653
			$settings = $this->get_settings();
654
655
			Give_Admin_Settings::output_fields( $settings, 'give_settings' );
656
		}
657
658
		/**
659
		 * Save settings.
660
		 *
661
		 * @since  1.8
662
		 * @return void
663
		 */
664
		public function save() {
665
			$settings = $this->get_settings();
666
667
			Give_Admin_Settings::save_fields( $settings, 'give_settings' );
668
		}
669
	}
670
endif;
671
672
new Give_CMB2_Settings_Loader();
673