|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Theme_Options_Datastore; |
|
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Theme options container class. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Theme_Options_Container extends Container { |
|
12
|
|
|
static protected $registered_pages = array(); |
|
13
|
|
|
|
|
14
|
|
|
public $settings = array( |
|
15
|
|
|
'parent' => 'self', |
|
16
|
|
|
'file' => '', |
|
17
|
|
|
'permissions' => 'manage_options', |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
public $icon = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Create a new theme options fields container |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $title Unique title of the container |
|
26
|
|
|
**/ |
|
27
|
|
|
public function __construct( $title ) { |
|
28
|
|
|
parent::__construct( $title ); |
|
29
|
|
|
|
|
30
|
|
|
if ( ! $this->get_datastore() ) { |
|
31
|
|
|
$this->set_datastore( new Theme_Options_Datastore(), $this->has_default_datastore() ); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Perform save operation after successful is_valid_save() check. |
|
37
|
|
|
* The call is propagated to all fields in the container. |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $user_data |
|
40
|
|
|
**/ |
|
41
|
|
|
public function save( $user_data = null ) { |
|
42
|
|
|
try { |
|
43
|
|
|
parent::save( $user_data ); |
|
44
|
|
|
} catch ( Incorrect_Syntax_Exception $e ) { |
|
45
|
|
|
$this->errors[] = $e->getMessage(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
do_action( 'carbon_after_save_theme_options', $user_data ); |
|
49
|
|
|
|
|
50
|
|
|
if ( ! headers_sent() ) { |
|
51
|
|
|
wp_redirect( add_query_arg( array( 'settings-updated' => 'true' ) ) ); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Sanitize a title to a filename |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $title |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function title_to_filename( $title, $extension ) { |
|
63
|
|
|
$title = sanitize_file_name( $title ); |
|
64
|
|
|
$title = strtolower( $title ); |
|
65
|
|
|
$title = remove_accents( $title ); |
|
66
|
|
|
$title = preg_replace( array( |
|
67
|
|
|
'~\s+~', |
|
68
|
|
|
'~[^\w\d-]+~u', |
|
69
|
|
|
'~-+~' |
|
|
|
|
|
|
70
|
|
|
), array( |
|
|
|
|
|
|
71
|
|
|
'-', |
|
72
|
|
|
'-', |
|
73
|
|
|
'-' |
|
|
|
|
|
|
74
|
|
|
), $title ); |
|
75
|
|
|
return $title . $extension; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Attach container as a theme options page/subpage. |
|
80
|
|
|
**/ |
|
81
|
|
|
public function init() { |
|
82
|
|
|
if ( ! $this->settings['parent'] || $this->settings['parent'] == 'self' ) { |
|
83
|
|
|
$this->settings['parent'] = ''; |
|
84
|
|
|
} else if ( strpos( $this->settings['parent'], '.php' ) === false ) { |
|
85
|
|
|
$this->settings['parent'] = $this->title_to_filename( 'crbn-' . $this->settings['parent'], '.php' ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ( ! $this->settings['file'] ) { |
|
89
|
|
|
$this->settings['file'] = $this->title_to_filename( 'crbn-' . $this->title, '.php' ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->verify_unique_page(); |
|
93
|
|
|
|
|
94
|
|
|
add_action( 'admin_menu', array( $this, '_attach' ) ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Perform checks whether the current save() request is valid. |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool |
|
101
|
|
|
**/ |
|
102
|
|
View Code Duplication |
public function is_valid_save() { |
|
|
|
|
|
|
103
|
|
|
if ( ! isset( $_POST[ $this->get_nonce_name() ] ) || ! wp_verify_nonce( $_POST[ $this->get_nonce_name() ], $this->get_nonce_name() ) ) { // Input var okay. |
|
|
|
|
|
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Add theme options container pages. |
|
112
|
|
|
* Hook the container saving action. |
|
113
|
|
|
**/ |
|
114
|
|
|
public function attach() { |
|
115
|
|
|
|
|
116
|
|
|
// Add menu page |
|
117
|
|
|
if ( ! $this->settings['parent'] ) { |
|
118
|
|
|
add_menu_page( |
|
119
|
|
|
$this->title, |
|
120
|
|
|
$this->title, |
|
121
|
|
|
$this->settings['permissions'], |
|
122
|
|
|
$this->settings['file'], |
|
123
|
|
|
array( $this, 'render' ), |
|
124
|
|
|
$this->icon |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
add_submenu_page( |
|
129
|
|
|
$this->settings['parent'], |
|
130
|
|
|
$this->title, |
|
131
|
|
|
$this->title, |
|
132
|
|
|
$this->settings['permissions'], |
|
133
|
|
|
$this->settings['file'], |
|
134
|
|
|
array( $this, 'render' ), |
|
135
|
|
|
$this->icon |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
$page_hook = get_plugin_page_hookname( $this->settings['file'], '' ); |
|
139
|
|
|
add_action( 'load-' . $page_hook, array( $this, '_save' ) ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Whether this container is currently viewed. |
|
144
|
|
|
**/ |
|
145
|
|
|
public function is_active() { |
|
146
|
|
|
if ( isset( $_GET['page'] ) && $_GET['page'] === $this->settings['file'] ) { |
|
|
|
|
|
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Revert the result of attach() |
|
155
|
|
|
**/ |
|
156
|
|
|
public function detach() { |
|
157
|
|
|
parent::detach(); |
|
158
|
|
|
|
|
159
|
|
|
$this->drop_unique_page(); |
|
160
|
|
|
|
|
161
|
|
|
$page_hook = get_plugin_page_hookname( $this->settings['file'], '' ); |
|
162
|
|
|
remove_action( 'load-' . $page_hook, array( $this, '_save' ) ); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Output the container markup |
|
167
|
|
|
**/ |
|
168
|
|
|
public function render() { |
|
169
|
|
|
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == 'true' ) { |
|
170
|
|
|
$this->notifications[] = __( 'Settings saved.', 'carbon-fields' ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/theme_options.php'; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Make sure that there are no duplicate containers with the same name. |
|
178
|
|
|
**/ |
|
179
|
|
|
public function verify_unique_page() { |
|
180
|
|
|
$file = $this->settings['file']; |
|
181
|
|
|
$parent = $this->settings['parent']; |
|
182
|
|
|
|
|
183
|
|
|
// Register top level page |
|
184
|
|
|
if ( ! $parent ) { |
|
185
|
|
|
if ( isset( self::$registered_pages[ $file ] ) ) { |
|
186
|
|
|
Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" already registered' ); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
self::$registered_pages[ $file ] = array(); |
|
190
|
|
|
return; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// Register sub-page |
|
194
|
|
|
if ( ! isset( self::$registered_pages[ $parent ] ) ) { |
|
195
|
|
|
self::$registered_pages[ $parent ] = array( $file ); |
|
196
|
|
|
} elseif ( in_array( $file, self::$registered_pages[ $parent ] ) ) { |
|
197
|
|
|
Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" with parent "' . $parent . '" is already registered. Please set a different file name using setup()' ); |
|
198
|
|
|
} else { |
|
199
|
|
|
self::$registered_pages[ $parent ][] = $file; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Unregister the container parent and child pages. |
|
205
|
|
|
**/ |
|
206
|
|
|
public function drop_unique_page() { |
|
207
|
|
|
$file = $this->settings['file']; |
|
208
|
|
|
$parent = $this->settings['parent']; |
|
209
|
|
|
|
|
210
|
|
|
// Register top level page |
|
211
|
|
|
if ( ! $parent ) { |
|
212
|
|
|
if ( isset( self::$registered_pages[ $file ] ) && empty( self::$registered_pages[ $file ] ) ) { |
|
213
|
|
|
unset( self::$registered_pages[ $file ] ); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
// Register sub-page |
|
220
|
|
|
if ( isset( self::$registered_pages[ $parent ] ) && in_array( $file, self::$registered_pages[ $parent ] ) ) { |
|
221
|
|
|
|
|
222
|
|
|
$index = array_search( $file, self::$registered_pages[ $parent ] ); |
|
223
|
|
|
if ( $index !== false ) { |
|
224
|
|
|
unset( self::$registered_pages[ $parent ][ $index ] ); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Append array of fields to the current fields set. All items of the array |
|
231
|
|
|
* must be instances of Field and their names should be unique for all |
|
232
|
|
|
* Carbon containers. |
|
233
|
|
|
* If a field does not have DataStore already, the container datastore is |
|
234
|
|
|
* assigned to them instead. |
|
235
|
|
|
* |
|
236
|
|
|
* @param array $fields |
|
237
|
|
|
**/ |
|
238
|
|
|
public function add_fields( $fields ) { |
|
239
|
|
|
parent::add_fields( $fields ); |
|
240
|
|
|
|
|
241
|
|
|
foreach ( $this->fields as $field ) { |
|
242
|
|
|
$field->set_prefix( '' ); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $this; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Change the parent theme options page of this container |
|
250
|
|
|
**/ |
|
251
|
|
|
public function set_page_parent( $parent ) { |
|
252
|
|
|
if ( is_a( $parent, 'Carbon_Container' ) ) { |
|
253
|
|
|
$parent = $parent->title; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$this->settings['parent'] = $parent; |
|
257
|
|
|
return $this; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Set the icon of this theme options page. |
|
262
|
|
|
* Applicable only for parent theme option pages. |
|
263
|
|
|
**/ |
|
264
|
|
|
public function set_icon( $icon ) { |
|
265
|
|
|
$this->icon = $icon; |
|
266
|
|
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Set the theme options file name of this container. |
|
271
|
|
|
**/ |
|
272
|
|
|
public function set_page_file( $file ) { |
|
273
|
|
|
$this->settings['file'] = $file; |
|
274
|
|
|
return $this; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Set the permissions necessary to view |
|
279
|
|
|
* the corresponding theme options page |
|
280
|
|
|
**/ |
|
281
|
|
|
public function set_page_permissions( $permissions ) { |
|
282
|
|
|
$this->settings['permissions'] = $permissions; |
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: