1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\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
|
|
|
|
13
|
|
|
protected static $registered_pages = array(); |
14
|
|
|
|
15
|
|
|
public $settings = array( |
16
|
|
|
'parent' => '', |
17
|
|
|
'file' => '', |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
public $icon = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new container |
24
|
|
|
* |
25
|
|
|
* @param string $unique_id Unique id of the container |
26
|
|
|
* @param string $title title of the container |
27
|
|
|
* @param string $type Type of the container |
28
|
|
|
**/ |
29
|
|
View Code Duplication |
public function __construct( $unique_id, $title, $type ) { |
30
|
|
|
parent::__construct( $unique_id, $title, $type ); |
31
|
|
|
|
32
|
|
|
if ( ! $this->get_datastore() ) { |
33
|
|
|
$this->set_datastore( Datastore::make( 'theme_options' ), $this->has_default_datastore() ); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Attach container as a theme options page/subpage. |
39
|
|
|
**/ |
40
|
|
|
public function init() { |
41
|
|
|
if ( $this->settings['parent'] !== '' && strpos( $this->settings['parent'], '.php' ) === false ) { |
42
|
|
|
$clear_title = $this->clear_string( $this->settings['parent'] ); |
43
|
|
|
$this->settings['parent'] = 'crbn-' . $clear_title . '.php'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ( ! $this->settings['file'] ) { |
47
|
|
|
$clear_title = $this->clear_string( $this->title ); |
48
|
|
|
$this->settings['file'] .= 'crbn-' . $clear_title . '.php'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->verify_unique_page(); |
52
|
|
|
|
53
|
|
|
add_action( 'admin_menu', array( $this, '_attach' ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Perform checks whether the current save() request is valid. |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
**/ |
61
|
|
|
public function is_valid_save() { |
62
|
|
|
if ( ! $this->verified_nonce_in_request() ) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->is_valid_attach_for_object(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Perform save operation after successful is_valid_save() check. |
71
|
|
|
* The call is propagated to all fields in the container. |
72
|
|
|
* |
73
|
|
|
* @param mixed $user_data |
74
|
|
|
**/ |
75
|
|
|
public function save( $user_data = null ) { |
76
|
|
|
try { |
77
|
|
|
parent::save( $user_data ); |
78
|
|
|
} catch ( Incorrect_Syntax_Exception $e ) { |
79
|
|
|
$this->errors[] = $e->getMessage(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
do_action( 'carbon_after_save_theme_options', $user_data ); |
83
|
|
|
|
84
|
|
|
if ( ! headers_sent() ) { |
85
|
|
|
wp_redirect( add_query_arg( array( 'settings-updated' => 'true' ) ) ); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get environment array for page request (in admin) |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
**/ |
94
|
|
|
protected function get_environment_for_request() { |
95
|
|
|
return array(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Perform checks whether the container should be attached during the current request |
100
|
|
|
* |
101
|
|
|
* @return bool True if the container is allowed to be attached |
102
|
|
|
**/ |
103
|
|
|
public function is_valid_attach_for_request() { |
104
|
|
|
return $this->static_conditions_pass(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get environment array for object id |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
protected function get_environment_for_object( $object_id ) { |
113
|
|
|
return array(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check container attachment rules against object id |
118
|
|
|
* |
119
|
|
|
* @param int $object_id |
120
|
|
|
* @return bool |
121
|
|
|
**/ |
122
|
|
|
public function is_valid_attach_for_object( $object_id = null ) { |
123
|
|
|
return $this->all_conditions_pass( intval( $object_id ) ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Add theme options container pages. |
128
|
|
|
* Hook the container saving action. |
129
|
|
|
**/ |
130
|
|
|
public function attach() { |
131
|
|
|
// use the "read" capability because conditions will handle actual access and save capability checking |
132
|
|
|
// before the attach() method is called |
133
|
|
|
|
134
|
|
|
// Add menu page |
135
|
|
|
if ( ! $this->settings['parent'] ) { |
136
|
|
|
add_menu_page( |
137
|
|
|
$this->title, |
138
|
|
|
$this->title, |
139
|
|
|
'read', |
140
|
|
|
$this->settings['file'], |
141
|
|
|
array( $this, 'render' ), |
142
|
|
|
$this->icon |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
add_submenu_page( |
147
|
|
|
$this->settings['parent'], |
148
|
|
|
$this->title, |
149
|
|
|
$this->title, |
150
|
|
|
'read', |
151
|
|
|
$this->settings['file'], |
152
|
|
|
array( $this, 'render' ), |
153
|
|
|
$this->icon |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$page_hook = get_plugin_page_hookname( $this->settings['file'], '' ); |
157
|
|
|
add_action( 'load-' . $page_hook, array( $this, '_save' ) ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Whether this container is currently viewed. |
162
|
|
|
**/ |
163
|
|
|
public function should_activate() { |
164
|
|
|
$request_page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
|
|
|
|
165
|
|
|
if ( ! empty( $request_page ) && $request_page === $this->settings['file'] ) { |
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Output the container markup |
174
|
|
|
**/ |
175
|
|
|
public function render() { |
176
|
|
|
$request_settings_updated = isset( $_GET['settings-updated'] ) ? $_GET['settings-updated'] : ''; |
|
|
|
|
177
|
|
|
if ( $request_settings_updated === 'true' ) { |
178
|
|
|
$this->notifications[] = __( 'Settings saved.', \Carbon_Fields\TEXT_DOMAIN ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/theme_options.php'; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Make sure that there are no duplicate containers with the same name. |
186
|
|
|
**/ |
187
|
|
|
public function verify_unique_page() { |
188
|
|
|
$file = $this->settings['file']; |
189
|
|
|
$parent = $this->settings['parent']; |
190
|
|
|
|
191
|
|
|
if ( ! $parent ) { |
192
|
|
|
// Register top level page |
193
|
|
|
if ( isset( static::$registered_pages[ $file ] ) ) { |
194
|
|
|
Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" already registered' ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
static::$registered_pages[ $file ] = array(); |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Register sub-page |
202
|
|
|
if ( ! isset( static::$registered_pages[ $parent ] ) ) { |
203
|
|
|
static::$registered_pages[ $parent ] = array(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ( in_array( $file, static::$registered_pages[ $parent ] ) ) { |
207
|
|
|
Incorrect_Syntax_Exception::raise( 'Page "' . $file . '" with parent "' . $parent . '" is already registered. Please set a name for the container.' ); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
static::$registered_pages[ $parent ][] = $file; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Sanitize the container title for use in |
215
|
|
|
* the theme options file name. |
216
|
|
|
**/ |
217
|
|
|
protected function clear_string( $string ) { |
218
|
|
|
return preg_replace( array( '~ +~', '~[^\w\d-]+~u', '~-+~' ), array( '-', '-', '-' ), strtolower( remove_accents( $string ) ) ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* COMMON USAGE METHODS |
223
|
|
|
*/ |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Change the parent theme options page of this container |
227
|
|
|
**/ |
228
|
|
|
public function set_page_parent( $parent ) { |
229
|
|
|
if ( is_a( $parent, 'Carbon_Container' ) ) { |
230
|
|
|
$parent = $parent->title; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$this->settings['parent'] = $parent; |
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Set the icon of this theme options page. |
239
|
|
|
* Applicable only for parent theme option pages. |
240
|
|
|
**/ |
241
|
|
|
public function set_icon( $icon ) { |
242
|
|
|
$this->icon = $icon; |
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Set the theme options file name of this container. |
248
|
|
|
**/ |
249
|
|
|
public function set_page_file( $file ) { |
250
|
|
|
$this->settings['file'] = $file; |
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
|