Test Failed
Pull Request — master (#2482)
by Devin
05:37
created

Give_Settings_Page::get_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
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
		 * Flag to check if enable saving option for setting page or not
60
		 *
61
		 * @since 1.8.17
62
		 * @var bool
63
		 */
64
		protected $enable_save = true;
65
66
		/**
67
		 * Constructor.
68
		 */
69
		public function __construct() {
70
			// Get current setting page.
71
			$this->current_setting_page = give_get_current_setting_page();
72
73
			add_filter( "give_default_setting_tab_section_{$this->id}", array( $this, 'set_default_setting_tab' ), 10 );
74
			add_filter( "{$this->current_setting_page}_tabs_array", array( $this, 'add_settings_page' ), 20 );
75
			add_action( "{$this->current_setting_page}_settings_{$this->id}_page", array( $this, 'output' ) );
76
77
			// Output section only if exist.
78
			$sections = $this->get_sections();
79
			if ( ! empty( $sections ) ) {
80
				add_action( "{$this->current_setting_page}_sections_{$this->id}_page", array(
81
					$this,
82
					'output_sections',
83
				) );
84
			}
85
86
			// Save hide button by default.
87
			$GLOBALS['give_hide_save_button'] = true;
88
89
			// Enable saving feature.
90
			if ( $this->enable_save ) {
91
				add_action( "{$this->current_setting_page}_save_{$this->id}", array( $this, 'save' ) );
92
			}
93
		}
94
95
96
		/**
97
		 * Get setting id
98
		 *
99
		 * @since  1.8.17
100
		 * @access public
101
		 * @return string
102
		 */
103
		public function get_id() {
104
			return $this->id;
105
		}
106
107
		/**
108
		 * Default setting tab.
109
		 *
110
		 * @since  1.8
111
		 *
112
		 * @param  $setting_tab
113
		 *
114
		 * @return string
115
		 */
116
		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...
117
			return $this->default_tab;
118
		}
119
120
		/**
121
		 * Add this page to settings.
122
		 *
123
		 * @since  1.8
124
		 *
125
		 * @param  array $pages Lst of pages.
126
		 *
127
		 * @return array
128
		 */
129
		public function add_settings_page( $pages ) {
130
			$pages[ $this->id ] = $this->label;
131
132
			return $pages;
133
		}
134
135
		/**
136
		 * Get settings array.
137
		 *
138
		 * @since  1.8
139
		 * @return array
140
		 */
141
		public function get_settings() {
142
			/**
143
			 * Filter the settings.
144
			 *
145
			 * @since  1.8
146
			 *
147
			 * @param  array $settings
148
			 */
149
			$settings = apply_filters( 'give_get_settings_' . $this->id, array() );
150
151
			// Output.
152
			return $settings;
153
		}
154
155
		/**
156
		 * Get sections.
157
		 *
158
		 * @since 1.8
159
		 * @return array
160
		 */
161
		public function get_sections() {
162
			return apply_filters( 'give_get_sections_' . $this->id, array() );
163
		}
164
165
		/**
166
		 * Output sections.
167
		 *
168
		 * @since  1.8
169
		 * @return void
170
		 */
171
		public function output_sections() {
172
			// Get current section.
173
			$current_section = give_get_current_setting_section();
174
175
			// Get all sections.
176
			$sections = $this->get_sections();
177
178
			// Show section settings only if setting section exist.
179
			if ( $current_section && ! in_array( $current_section, array_keys( $sections ) ) ) {
180
				echo '<div class="error"><p>' . __( 'Oops, this settings page does not exist.', 'give' ) . '</p></div>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
181
				$GLOBALS['give_hide_save_button'] = true;
182
183
				return;
184
			}
185
186
			// Bailout.
187
			if ( empty( $sections ) ) {
188
				return;
189
			}
190
191
			if ( is_null( $this->current_setting_page ) ) {
192
				$this->current_setting_page = give_get_current_setting_page();
193
			}
194
195
			echo '<ul class="subsubsub">';
196
197
			// Get section keys.
198
			$array_keys = array_keys( $sections );
199
200 View Code Duplication
			foreach ( $sections as $id => $label ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
				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>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'admin_url'
Loading history...
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
introduced by
Expected next thing to be a escaping function, not '$label'
Loading history...
202
			}
203
204
			echo '</ul><br class="clear" /><hr>';
205
		}
206
207
		/**
208
		 * Output the settings.
209
		 *
210
		 * Note: if you want to overwrite this function then manage show/hide save button in your class.
211
		 *
212
		 * @since  1.8
213
		 * @return void
214
		 */
215
		public function output() {
216
			if ( $this->enable_save ) {
217
				$GLOBALS['give_hide_save_button'] = false;
218
			}
219
220
			$settings = $this->get_settings();
221
222
			Give_Admin_Settings::output_fields( $settings, 'give_settings' );
223
		}
224
225
		/**
226
		 * Save settings.
227
		 *
228
		 * @since  1.8
229
		 * @return void
230
		 */
231
		public function save() {
232
			$settings        = $this->get_settings();
233
			$current_section = give_get_current_setting_section();
234
235
			Give_Admin_Settings::save_fields( $settings, 'give_settings' );
236
237
			/**
238
			 * Trigger Action
239
			 *
240
			 * @since 1.8
241
			 */
242
			do_action( 'give_update_options_' . $this->id . '_' . $current_section );
243
		}
244
245
		/**
246
		 * Get heading labels
247
		 *
248
		 * @since  1.8.7
249
		 * @access private
250
		 *
251
		 * @return array
252
		 */
253
		private function get_heading() {
254
			$heading[]       = give_get_admin_page_menu_title();
255
			$heading[]       = $this->label;
256
			$section         = $this->get_sections();
257
			$current_section = give_get_current_setting_section();
258
259
			if ( array_key_exists( $current_section, $section ) ) {
260
				$heading[] = $section[ $current_section ];
261
			}
262
263
			return array_unique( $heading );
264
		}
265
266
		/**
267
		 * Get heading html
268
		 *
269
		 * @since  1.8.7
270
		 * @access private
271
		 *
272
		 * @return string
273
		 */
274
		public function get_heading_html() {
275
			return sprintf(
276
				'<h1 class="wp-heading-inline">%s</h1><hr class="wp-header-end">',
277
				implode( ' <span class="give-settings-heading-sep dashicons dashicons-arrow-right-alt2"></span> ', $this->get_heading() )
278
			);
279
		}
280
	}
281
282
endif;
283