Completed
Push — master ( 0af146...0e0d02 )
by Devin
13s
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
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
		 *
77
		 * @param  $setting_tab
78
		 *
79
		 * @return string
80
		 */
81
		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...
82
			return $this->default_tab;
83
		}
84
85
		/**
86
		 * Add this page to settings.
87
		 *
88
		 * @since  1.8
89
		 *
90
		 * @param  array $pages Lst of pages.
91
		 *
92
		 * @return array
93
		 */
94
		public function add_settings_page( $pages ) {
95
			$pages[ $this->id ] = $this->label;
96
97
			return $pages;
98
		}
99
100
		/**
101
		 * Get settings array.
102
		 *
103
		 * @since  1.8
104
		 * @return array
105
		 */
106
		public function get_settings() {
107
			/**
108
			 * Filter the settings.
109
			 *
110
			 * @since  1.8
111
			 *
112
			 * @param  array $settings
113
			 */
114
			$settings = apply_filters( 'give_get_settings_' . $this->id, array() );
115
116
			// Output.
117
			return $settings;
118
		}
119
120
		/**
121
		 * Get sections.
122
		 *
123
		 * @since 1.8
124
		 * @return array
125
		 */
126
		public function get_sections() {
127
			return apply_filters( 'give_get_sections_' . $this->id, array() );
128
		}
129
130
		/**
131
		 * Output sections.
132
		 *
133
		 * @since  1.8
134
		 * @return void
135
		 */
136
		public function output_sections() {
137
			// Get current section.
138
			$current_section = give_get_current_setting_section();
139
140
			// Get all sections.
141
			$sections = $this->get_sections();
142
143
			// Show section settings only if setting section exist.
144
			if ( $current_section && ! in_array( $current_section, array_keys( $sections ) ) ) {
145
				echo '<div class="error"><p>' . __( 'Oops, this settings page does not exist.', 'give' ) . '</p></div>';
146
				$GLOBALS['give_hide_save_button'] = true;
147
148
				return;
149
			}
150
151
			// Bailout.
152
			if ( empty( $sections ) ) {
153
				return;
154
			}
155
156
			if ( is_null( $this->current_setting_page ) ) {
157
				$this->current_setting_page = give_get_current_setting_page();
158
			}
159
160
			echo '<ul class="subsubsub">';
161
162
			// Get section keys.
163
			$array_keys = array_keys( $sections );
164
165
			foreach ( $sections as $id => $label ) {
166
				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>';
167
			}
168
169
			echo '</ul><br class="clear" /><hr>';
170
		}
171
172
		/**
173
		 * Output the settings.
174
		 *
175
		 * @since  1.8
176
		 * @return void
177
		 */
178
		public function output() {
179
			$settings = $this->get_settings();
180
181
			Give_Admin_Settings::output_fields( $settings, 'give_settings' );
182
		}
183
184
		/**
185
		 * Save settings.
186
		 *
187
		 * @since  1.8
188
		 * @return void
189
		 */
190
		public function save() {
191
			$settings        = $this->get_settings();
192
			$current_section = give_get_current_setting_section();
193
194
			Give_Admin_Settings::save_fields( $settings, 'give_settings' );
195
196
			/**
197
			 * Trigger Action
198
			 *
199
			 * @since 1.8
200
			 */
201
			do_action( 'give_update_options_' . $this->id . '_' . $current_section );
202
		}
203
	}
204
205
endif;
206