This repository has been archived by the owner on Aug 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbon-fields-custom-options-container.php
339 lines (298 loc) · 8.11 KB
/
carbon-fields-custom-options-container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Carbon fields theme options container modified to support custom files for rendering pages.
*
* @package carbon-fields-custom-options-container
*/
namespace Carbon_Fields\Container;
use Carbon_Fields\Datastore\Datastore;
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
/**
* Theme options container class.
*/
class Custom_Options_Container extends Container {
/**
* Array of registered page slugs to verify uniqueness with.
*
* @var array
*/
protected static $registered_pages = array();
/**
* Array of container settings.
*
* @var array
*/
public $settings = array(
'parent' => '',
'file' => '',
'icon' => '',
'menu_position' => null,
'menu_title' => null,
);
/**
* {@inheritDoc}
*/
public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) {
parent::__construct( $id, $title, $type, $condition_collection, $condition_translator );
$this->type = 'theme_options';
if ( ! $this->get_datastore() ) {
$this->set_datastore( Datastore::make( 'theme_options' ), $this->has_default_datastore() );
}
if ( apply_filters( 'carbon_fields_' . $type . '_container_admin_only_access', true, $title, $this ) ) {
$this->where( 'current_user_capability', '=', 'manage_options' );
}
}
/**
* Sanitize a title to a filename.
*
* @param string $title
* @param string $extension
* @return string
*/
protected function title_to_filename( $title, $extension ) {
$title = sanitize_file_name( $title );
$title = strtolower( $title );
$title = remove_accents( $title );
$title = preg_replace(
array(
'~\s+~',
'~[^\w\d-]+~u',
'~-+~',
),
array(
'-',
'-',
'-',
),
$title
);
return $title . $extension;
}
/**
* Attach container as a theme options page/subpage.
*/
public function init() {
$registered = $this->register_page();
if ( $registered ) {
add_action( 'admin_menu', array( $this, '_attach' ) );
}
}
/**
* Checks whether the current save request is valid.
*
* @return bool
*/
public function is_valid_save() {
if ( ! $this->verified_nonce_in_request() ) {
return false;
}
return $this->is_valid_attach_for_object( null );
}
/**
* Perform save operation after successful is_valid_save() check.
* The call is propagated to all fields in the container.
*
* @param mixed $user_data
*/
public function save( $user_data = null ) {
try {
parent::save( $user_data );
} catch ( Incorrect_Syntax_Exception $e ) {
$this->errors[] = $e->getMessage();
}
do_action( 'carbon_fields_' . $this->type . '_container_saved', $user_data, $this );
if ( ! headers_sent() ) {
wp_redirect( add_query_arg( array( 'settings-updated' => 'true' ) ) );
}
}
/**
* Get environment array for page request (in admin).
*
* @return array
*/
protected function get_environment_for_request() {
return array();
}
/**
* Perform checks whether the container should be attached during the current request.
*
* @return bool True if the container is allowed to be attached.
*/
public function is_valid_attach_for_request() {
return $this->static_conditions_pass();
}
/**
* Get environment array for object id.
*
* @return array
*/
protected function get_environment_for_object( $object_id ) {
return array();
}
/**
* Check container attachment rules against object id.
*
* @param int $object_id
* @return bool
*/
public function is_valid_attach_for_object( $object_id = null ) {
return $this->all_conditions_pass( intval( $object_id ) );
}
/**
* Add theme options container pages.
* Hook the container saving action.
*/
public function attach() {
// use the "read" capability because conditions will handle actual access and save capability checking
// before the attach() method is called
// Add menu page
if ( ! $this->settings['parent'] ) {
add_menu_page(
$this->title,
$this->settings['menu_title'] ? $this->settings['menu_title'] : $this->title,
'read',
$this->get_page_file(),
array( $this, 'render' ),
$this->settings['icon'],
$this->settings['menu_position']
);
}
add_submenu_page(
$this->settings['parent'],
$this->title,
$this->settings['menu_title'] ? $this->settings['menu_title'] : $this->title,
'read',
$this->get_page_file(),
array( $this, 'render' )
);
$page_hook = get_plugin_page_hookname( $this->get_page_file(), '' );
add_action( 'load-' . $page_hook, array( $this, '_save' ) );
}
/**
* Whether this container is currently viewed.
*
* @return boolean
*/
public function should_activate() {
$input = stripslashes_deep( $_GET );
$request_page = isset( $input['page'] ) ? $input['page'] : '';
if ( ! empty( $request_page ) && $request_page === $this->get_page_file() ) {
return true;
}
return false;
}
/**
* Output the container markup.
*/
public function render() {
$input = stripslashes_deep( $_GET );
$request_settings_updated = isset( $input['settings-updated'] ) ? $input['settings-updated'] : '';
if ( $request_settings_updated === 'true' ) {
$this->notifications[] = __( 'Settings saved.', 'carbon-fields' );
}
include apply_filters( "cb_theme_options_{$this->get_page_file()}_container_file", \Carbon_Fields\DIR . '/templates/Container/' . $this->type . '.php' );
}
/**
* Register the page while making sure it is unique.
*
* @return boolean
*/
protected function register_page() {
$file = $this->get_page_file();
$parent = $this->settings['parent'];
if ( ! $parent ) {
// Register top level page.
if ( isset( static::$registered_pages[ $file ] ) ) {
Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" already registered' );
return false;
}
static::$registered_pages[ $file ] = array();
return true;
}
// Register sub-page.
if ( ! isset( static::$registered_pages[ $parent ] ) ) {
static::$registered_pages[ $parent ] = array();
}
if ( in_array( $file, static::$registered_pages[ $parent ] ) ) {
Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" with parent "' . $parent . '" is already registered. Please set a name for the container.' );
return false;
}
static::$registered_pages[ $parent ][] = $file;
return true;
}
/**
* Change the parent theme options page of this container.
*
* @param string|Theme_Options_Container $parent
* @return Container $this
*/
public function set_page_parent( $parent ) {
if ( is_a( $parent, get_class() ) ) {
$this->settings['parent'] = $parent->get_page_file();
return $this;
}
$this->settings['parent'] = $parent;
return $this;
}
/**
* Get the theme options file name of this container.
*
* @return string
*/
public function get_page_file() {
if ( ! empty( $this->settings['file'] ) ) {
return $this->settings['file'];
}
return $this->title_to_filename( 'crb_' . $this->get_id(), '.php' );
}
/**
* Set the theme options file name of this container.
*
* @param string $file
* @return Container $this
*/
public function set_page_file( $file ) {
$this->settings['file'] = $file;
return $this;
}
/**
* Set the title of this container in the administration menu.
*
* @param string $title
* @return Container $this
*/
public function set_page_menu_title( $title ) {
$this->settings['menu_title'] = $title;
return $this;
}
/**
* Alias of the set_page_menu_position() method for backwards compatibility.
*
* @param integer $position
* @return Container $this
*/
public function set_page_position( $position ) {
return $this->set_page_menu_position( $position );
}
/**
* Set the page position of this container in the administration menu.
*
* @param integer $position
* @return Container $this
*/
public function set_page_menu_position( $position ) {
$this->settings['menu_position'] = $position;
return $this;
}
/**
* Set the icon of this theme options page.
* Applicable only for parent theme option pages.
*
* @param string $icon
* @return Container $this
*/
public function set_icon( $icon ) {
$this->settings['icon'] = $icon;
return $this;
}
}