Completed
Pull Request — master (#1412)
by Ravinder
17:25
created

Give_Settings_Page::add_settings_page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
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 ( ! in_array( $current_section, array_keys( $sections ) ) ) {
145
				echo '<div class="error"><p>' . __( 'Oops, this settings page does not exist.', 'give' ) . '</p></div>';
146
147
				return;
148
			}
149
150
			// Bailout.
151
			if ( empty( $sections ) ) {
152
				return;
153
			}
154
155
			if ( is_null( $this->current_setting_page ) ) {
156
				$this->current_setting_page = give_get_current_setting_page();
157
			}
158
159
			echo '<ul class="subsubsub">';
160
161
			// Get section keys.
162
			$array_keys = array_keys( $sections );
163
164
			foreach ( $sections as $id => $label ) {
165
				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>';
166
			}
167
168
			echo '</ul><br class="clear" /><hr>';
169
		}
170
171
		/**
172
		 * Output the settings.
173
		 *
174
		 * @since  1.8
175
		 * @return void
176
		 */
177
		public function output() {
178
			$settings = $this->get_settings();
179
180
			Give_Admin_Settings::output_fields( $settings, 'give_settings' );
181
		}
182
183
		/**
184
		 * Save settings.
185
		 *
186
		 * @since  1.8
187
		 * @return void
188
		 */
189
		public function save() {
190
			$settings        = $this->get_settings();
191
			$current_section = give_get_current_setting_section();
192
193
			Give_Admin_Settings::save_fields( $settings, 'give_settings' );
194
195
			/**
196
			 * Trigger Action
197
			 *
198
			 * @since 1.8
199
			 */
200
			do_action( 'give_update_options_' . $this->id . '_' . $current_section );
201
		}
202
	}
203
204
endif;
205