Completed
Push — master ( 0af146...28843d )
by Ravinder
21:08
created

Give_Settings_Page   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 169
rs 10
c 0
b 0
f 0
wmc 14
lcom 2
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A set_default_setting_tab() 0 3 1
A add_settings_page() 0 5 1
A get_settings() 0 12 1
A get_sections() 0 3 1
C output_sections() 0 28 7
A output() 0 5 1
A save() 0 13 1
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 Settings Page/Tab
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Settings_Page
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; // Exit if accessed directly
14
}
15
16
if ( ! class_exists( 'Give_Settings_Page' ) ) :
17
18
	/**
19
	 * Give_Settings_Page.
20
	 *
21
	 * @sine 1.8
22
	 */
23
	class Give_Settings_Page {
24
25
		/**
26
		 * Setting page id.
27
		 *
28
		 * @since 1.8
29
		 * @var   string
30
		 */
31
		protected $id = '';
32
33
		/**
34
		 * Setting page label.
35
		 *
36
		 * @since 1.8
37
		 * @var   string
38
		 */
39
		protected $label = '';
40
41
42
		/**
43
		 * Default tab.
44
		 *
45
		 * @since 1.8
46
		 * @var   string
47
		 */
48
		protected $default_tab = '';
49
50
		/**
51
		 * Current setting page.
52
		 *
53
		 * @since 1.8
54
		 * @var   string|null
55
		 */
56
		private $current_setting_page = null;
57
58
		/**
59
		 * Constructor.
60
		 */
61
		public function __construct() {
62
			// Get current setting page.
63
			$this->current_setting_page = give_get_current_setting_page();
64
65
			add_filter( "give_default_setting_tab_section_{$this->id}", array( $this, 'set_default_setting_tab' ), 10 );
66
			add_filter( "{$this->current_setting_page}_tabs_array", array( $this, 'add_settings_page' ), 20 );
67
			add_action( "{$this->current_setting_page}_sections_{$this->id}_page", array( $this, 'output_sections' ) );
68
			add_action( "{$this->current_setting_page}_settings_{$this->id}_page", array( $this, 'output' ) );
69
			add_action( "{$this->current_setting_page}_save_{$this->id}", array( $this, 'save' ) );
70
		}
71
72
		/**
73
		 * Default setting tab.
74
		 *
75
		 * @since  1.8
76
		 * @param  $setting_tab
77
		 * @return string
78
		 */
79
		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...
80
			return $this->default_tab;
81
		}
82
83
		/**
84
		 * Add this page to settings.
85
		 *
86
		 * @since  1.8
87
		 * @param  array $pages Lst of pages.
88
		 * @return array
89
		 */
90
		public function add_settings_page( $pages ) {
91
			$pages[ $this->id ] = $this->label;
92
93
			return $pages;
94
		}
95
96
		/**
97
		 * Get settings array.
98
		 *
99
		 * @since  1.8
100
		 * @return array
101
		 */
102
		public function get_settings() {
103
			/**
104
			 * Filter the settings.
105
			 *
106
			 * @since  1.8
107
			 * @param  array $settings
108
			 */
109
			$settings = apply_filters( 'give_get_settings_' . $this->id, array() );
110
111
			// Output.
112
			return $settings;
113
		}
114
115
		/**
116
		 * Get sections.
117
		 *
118
		 * @since 1.8
119
		 * @return array
120
		 */
121
		public function get_sections() {
122
			return apply_filters( 'give_get_sections_' . $this->id, array() );
123
		}
124
125
		/**
126
		 * Output sections.
127
		 *
128
		 * @since  1.8
129
		 * @return void
130
		 */
131
		public function output_sections() {
132
			// Get current section.
133
			$current_section = give_get_current_setting_section();
134
135
			// Get all sections.
136
			$sections = $this->get_sections();
137
138
139
			// Bailout.
140
			if ( empty( $sections ) || 1 === sizeof( $sections ) ) {
141
				return;
142
			}
143
144
			if( is_null( $this->current_setting_page ) ) {
145
				$this->current_setting_page = give_get_current_setting_page();
146
			}
147
148
			echo '<ul class="subsubsub">';
149
150
			// Get section keys.
151
			$array_keys = array_keys( $sections );
152
153
			foreach ( $sections as $id => $label ) {
154
				echo '<li><a href="' . admin_url( 'edit.php?post_type=give_forms&page=' . $this->current_setting_page . '&tab=' . $this->id . '&section=' . sanitize_title( $id ) ) . '" class="' . ( $current_section == $id ? 'current' : '' ) . '">' . $label . '</a> ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
155
			}
156
157
			echo '</ul><br class="clear" />';
158
		}
159
160
		/**
161
		 * Output the settings.
162
		 *
163
		 * @since  1.8
164
		 * @return void
165
		 */
166
		public function output() {
167
			$settings = $this->get_settings();
168
169
			Give_Admin_Settings::output_fields( $settings, 'give_settings' );
170
		}
171
172
		/**
173
		 * Save settings.
174
		 *
175
		 * @since  1.8
176
		 * @return void
177
		 */
178
		public function save() {
179
			$settings = $this->get_settings();
180
			$current_section = give_get_current_setting_section();
181
182
			Give_Admin_Settings::save_fields( $settings, 'give_settings' );
183
184
			/**
185
			 * Trigger Action
186
			 *
187
			 * @since 1.8
188
			 */
189
			do_action( 'give_update_options_' . $this->id . '_' . $current_section );
190
		}
191
	}
192
193
endif;
194