1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Settings Pages |
4
|
|
|
* |
5
|
|
|
* @package SimpleCalendar/Admin |
6
|
|
|
*/ |
7
|
|
|
namespace SimpleCalendar\Admin; |
8
|
|
|
|
9
|
|
|
use SimpleCalendar\Abstracts\Field; |
10
|
|
|
use SimpleCalendar\Abstracts\Admin_Page; |
11
|
|
|
|
12
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
13
|
|
|
exit; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Admin pages class. |
18
|
|
|
* |
19
|
|
|
* Handles settings pages and settings UI in admin dashboard. |
20
|
|
|
* |
21
|
|
|
* @since 3.0.0 |
22
|
|
|
*/ |
23
|
|
|
class Pages { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Current settings page. |
27
|
|
|
* |
28
|
|
|
* @access private |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $page = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Default tab. |
35
|
|
|
* |
36
|
|
|
* @access private |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $tab = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Settings pages. |
43
|
|
|
* |
44
|
|
|
* @access private |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $settings = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor. |
51
|
|
|
* |
52
|
|
|
* @since 3.0.0 |
53
|
|
|
* |
54
|
|
|
* @param string $page |
55
|
|
|
*/ |
56
|
|
|
public function __construct( $page = 'settings' ) { |
57
|
|
|
|
58
|
|
|
$this->page = $page; |
59
|
|
|
$settings_pages = ! is_null( \SimpleCalendar\plugin()->objects ) ? simcal_get_admin_pages() : ''; |
60
|
|
|
$settings_page_tabs = array(); |
61
|
|
|
$tabs = isset( $settings_pages[ $page ] ) ? $settings_pages[ $page ] : false; |
62
|
|
|
|
63
|
|
|
if ( $tabs && is_array( $tabs ) ) { |
64
|
|
|
foreach ( $tabs as $tab ) { |
65
|
|
|
|
66
|
|
|
$settings_page = simcal_get_admin_page( $tab ); |
67
|
|
|
|
68
|
|
|
if ( $settings_page instanceof Admin_Page ) { |
69
|
|
|
$settings_page_tabs[ $settings_page->id ] = $settings_page; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->settings = $settings_page_tabs; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// The first tab is the default tab when opening a page. |
77
|
|
|
$this->tab = isset( $tabs[0] ) ? $tabs[0] : ''; |
78
|
|
|
|
79
|
|
|
do_action( 'simcal_admin_pages', $page ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get settings pages. |
84
|
|
|
* |
85
|
|
|
* @since 3.0.0 |
86
|
|
|
* @access private |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function get_settings() { |
91
|
|
|
|
92
|
|
|
$settings = array(); |
93
|
|
|
|
94
|
|
|
if ( ! empty( $this->settings ) && is_array( $this->settings ) ) { |
95
|
|
|
foreach ( $this->settings as $id => $object ) { |
96
|
|
|
|
97
|
|
|
if ( $object instanceof Admin_Page ) { |
98
|
|
|
|
99
|
|
|
$settings_page = $object->get_settings(); |
100
|
|
|
|
101
|
|
|
if ( isset( $settings_page[ $id ] ) ) { |
102
|
|
|
$settings[ $id ] = $settings_page[ $id ]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $settings; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Register settings. |
114
|
|
|
* |
115
|
|
|
* Adds settings sections and fields to settings pages. |
116
|
|
|
* |
117
|
|
|
* @since 3.0.0 |
118
|
|
|
* |
119
|
|
|
* @param array $settings |
120
|
|
|
*/ |
121
|
|
|
public function register_settings( $settings = array() ) { |
122
|
|
|
|
123
|
|
|
$settings = $settings ? $settings : $this->get_settings(); |
124
|
|
|
|
125
|
|
|
if ( ! empty( $settings ) && is_array( $settings ) ) { |
126
|
|
|
|
127
|
|
|
foreach ( $settings as $tab_id => $settings_page ) { |
128
|
|
|
|
129
|
|
|
if ( isset( $settings_page['sections'] ) ) { |
130
|
|
|
|
131
|
|
|
$sections = $settings_page['sections']; |
132
|
|
|
|
133
|
|
|
if ( ! empty( $sections ) && is_array( $sections ) ) { |
134
|
|
|
|
135
|
|
|
foreach ( $sections as $section_id => $section ) { |
136
|
|
|
|
137
|
|
|
add_settings_section( |
138
|
|
|
$section_id, |
139
|
|
|
isset( $section['title'] ) ? $section['title'] : '', |
140
|
|
|
isset( $section['callback'] ) ? $section['callback'] : '', |
141
|
|
|
'simple-calendar_' . $this->page . '_' . $tab_id |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
if ( isset( $section['fields'] ) ) { |
145
|
|
|
|
146
|
|
|
$fields = $section['fields']; |
147
|
|
|
|
148
|
|
|
if ( ! empty( $fields ) && is_array( $fields ) ) { |
149
|
|
|
|
150
|
|
|
foreach ( $fields as $field ) { |
151
|
|
|
|
152
|
|
|
if ( isset( $field['id'] ) && isset( $field['type'] ) ) { |
153
|
|
|
|
154
|
|
|
$field_object = simcal_get_field( $field, $field['type'] ); |
155
|
|
|
|
156
|
|
|
if ( $field_object instanceof Field ) { |
157
|
|
|
|
158
|
|
|
add_settings_field( |
159
|
|
|
$field['id'], |
160
|
|
|
isset( $field['title'] ) ? $field['title'] : '', |
161
|
|
|
array( $field_object, 'html' ), |
162
|
|
|
'simple-calendar_' . $this->page . '_' . $tab_id, |
163
|
|
|
$section_id |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
} // add field |
167
|
|
|
|
168
|
|
|
} // is field valid? |
169
|
|
|
|
170
|
|
|
} // loop fields |
171
|
|
|
|
172
|
|
|
} // are fields non empty? |
173
|
|
|
|
174
|
|
|
} // are there fields? |
175
|
|
|
|
176
|
|
|
$page = simcal_get_admin_page( $tab_id ); |
177
|
|
|
|
178
|
|
|
register_setting( |
179
|
|
|
'simple-calendar_' . $this->page . '_' . $tab_id, |
180
|
|
|
'simple-calendar_' . $this->page . '_' . $tab_id, |
181
|
|
|
$page instanceof Admin_Page ? array( $page, 'validate' ) : '' |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
} // loop sections |
185
|
|
|
|
186
|
|
|
} // are sections non empty? |
187
|
|
|
|
188
|
|
|
} // are there sections? |
189
|
|
|
|
190
|
|
|
} // loop settings |
191
|
|
|
|
192
|
|
|
} // are there settings? |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Print Settings Pages. |
198
|
|
|
* |
199
|
|
|
* @since 3.0.0 |
200
|
|
|
*/ |
201
|
|
|
public function html() { |
|
|
|
|
202
|
|
|
|
203
|
|
|
global $current_tab; |
|
|
|
|
204
|
|
|
|
205
|
|
|
// Get current tab/section |
206
|
|
|
$current_tab = empty( $_GET['tab'] ) ? $this->tab : sanitize_title( $_GET['tab'] ); |
207
|
|
|
$this->tab = $current_tab; |
208
|
|
|
|
209
|
|
|
?> |
210
|
|
|
<div class="wrap" id="simcal-settings-page"> |
211
|
|
|
<form id="simcal-settings-page-form" |
212
|
|
|
method="post" |
213
|
|
|
action="options.php"> |
214
|
|
|
<?php |
215
|
|
|
|
216
|
|
|
// Include settings pages |
217
|
|
|
$settings_pages = self::get_settings(); |
218
|
|
|
if ( ! empty( $settings_pages ) && is_array( $settings_pages ) ) { |
219
|
|
|
|
220
|
|
|
echo '<h2 class="nav-tab-wrapper simcal-nav-tab-wrapper">'; |
221
|
|
|
|
222
|
|
|
// Get tabs for the settings page |
223
|
|
|
if ( ! empty( $settings_pages ) && is_array( $settings_pages ) ) { |
224
|
|
|
|
225
|
|
|
foreach ( $settings_pages as $id => $settings ) { |
226
|
|
|
|
227
|
|
|
$tab_id = isset( $id ) ? $id : ''; |
228
|
|
|
$tab_label = isset( $settings['label'] ) ? $settings['label'] : ''; |
229
|
|
|
$tab_link = admin_url( 'edit.php?post_type=calendar&page=simple-calendar_' . $this->page . '&tab=' . $tab_id ); |
230
|
|
|
|
231
|
|
|
echo '<a href="' . $tab_link . '" class="nav-tab ' . ( $current_tab == $tab_id ? 'nav-tab-active' : '' ) . '">' . $tab_label . '</a>'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
do_action( 'simcal_admin_page_' . $this->page . '_tabs' ); |
237
|
|
|
|
238
|
|
|
echo '</h2>'; |
239
|
|
|
|
240
|
|
|
settings_errors(); |
241
|
|
|
|
242
|
|
|
foreach ( $settings_pages as $tab_id => $contents ) { |
243
|
|
|
|
244
|
|
|
if ( $tab_id === $current_tab ) { |
245
|
|
|
|
246
|
|
|
echo isset( $contents['description'] ) ? '<p>' . $contents['description'] . '</p>' : ''; |
247
|
|
|
|
248
|
|
|
do_action( 'simcal_admin_page_' . $this->page . '_' . $current_tab . '_start' ); |
249
|
|
|
|
250
|
|
|
settings_fields( 'simple-calendar_' . $this->page . '_' . $tab_id ); |
251
|
|
|
do_settings_sections( 'simple-calendar_' . $this->page . '_' . $tab_id ); |
252
|
|
|
|
253
|
|
|
do_action( 'simcal_admin_page_' . $this->page . '_' . $current_tab . '_end' ); |
254
|
|
|
|
255
|
|
|
$submit = apply_filters( 'simcal_admin_page_' . $this->page . '_' . $current_tab . '_submit', true ); |
256
|
|
|
if ( true === $submit ) { |
257
|
|
|
submit_button(); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
?> |
264
|
|
|
</form> |
265
|
|
|
</div> |
266
|
|
|
<?php |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: