|
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
|
|
|
* Field container designed to extend WordPress custom fields functionality, |
|
10
|
|
|
* providing easier user interface to add, edit and delete text, media files, |
|
11
|
|
|
* location information and more. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Post_Meta_Container extends Container { |
|
14
|
|
|
/** |
|
15
|
|
|
* ID of the post the container is working with |
|
16
|
|
|
* |
|
17
|
|
|
* @see init() |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $post_id; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* List of default container settings |
|
24
|
|
|
* |
|
25
|
|
|
* @see init() |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
public $settings = array( |
|
29
|
|
|
'panel_context' => 'normal', |
|
30
|
|
|
'panel_priority' => 'high', |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create a new container |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $unique_id Unique id of the container |
|
37
|
|
|
* @param string $title title of the container |
|
38
|
|
|
* @param string $type Type of the container |
|
39
|
|
|
**/ |
|
40
|
|
View Code Duplication |
public function __construct( $unique_id, $title, $type ) { |
|
41
|
|
|
parent::__construct( $unique_id, $title, $type ); |
|
42
|
|
|
|
|
43
|
|
|
if ( ! $this->get_datastore() ) { |
|
44
|
|
|
$this->set_datastore( Datastore::make( 'post_meta' ), $this->has_default_datastore() ); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create DataStore instance, set post ID to operate with (if such exists). |
|
50
|
|
|
* Bind attach() and save() to the appropriate WordPress actions. |
|
51
|
|
|
**/ |
|
52
|
|
|
public function init() { |
|
53
|
|
|
$input = stripslashes_deep( $_GET ); |
|
|
|
|
|
|
54
|
|
|
$request_post_id = isset( $input['post'] ) ? intval( $input['post'] ) : 0; |
|
55
|
|
|
if ( $request_post_id > 0 ) { |
|
56
|
|
|
$this->set_post_id( $request_post_id ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
add_action( 'admin_init', array( $this, '_attach' ) ); |
|
60
|
|
|
add_action( 'save_post', array( $this, '_save' ) ); |
|
61
|
|
|
|
|
62
|
|
|
// support for attachments |
|
63
|
|
|
add_action( 'add_attachment', array( $this, '_save' ) ); |
|
64
|
|
|
add_action( 'edit_attachment', array( $this, '_save' ) ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Checks whether the current save request is valid |
|
69
|
|
|
* Possible errors are triggering save() for autosave requests |
|
70
|
|
|
* or performing post save outside of the post edit page (like Quick Edit) |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool |
|
73
|
|
|
**/ |
|
74
|
|
|
public function is_valid_save() { |
|
75
|
|
|
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ( ! $this->verified_nonce_in_request() ) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$params = func_get_args(); |
|
84
|
|
|
$post_id = $params[0]; |
|
85
|
|
|
$post_type = get_post_type( $post_id ); |
|
86
|
|
|
if ( $post_type === 'revision' ) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->is_valid_attach_for_object( $post_id ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Perform save operation after successful is_valid_save() check. |
|
95
|
|
|
* The call is propagated to all fields in the container. |
|
96
|
|
|
* |
|
97
|
|
|
* @param int $post_id ID of the post against which save() is ran |
|
98
|
|
|
**/ |
|
99
|
|
View Code Duplication |
public function save( $post_id = null ) { |
|
100
|
|
|
// Unhook action to garantee single save |
|
101
|
|
|
remove_action( 'save_post', array( $this, '_save' ) ); |
|
102
|
|
|
|
|
103
|
|
|
$this->set_post_id( $post_id ); |
|
104
|
|
|
|
|
105
|
|
|
foreach ( $this->fields as $field ) { |
|
106
|
|
|
$field->set_value_from_input( stripslashes_deep( $_POST ) ); |
|
|
|
|
|
|
107
|
|
|
$field->save(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
do_action( 'carbon_after_save_custom_fields', $post_id ); |
|
111
|
|
|
do_action( 'carbon_after_save_post_meta', $post_id ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get environment array for page request (in admin) |
|
116
|
|
|
* |
|
117
|
|
|
* @return array |
|
118
|
|
|
**/ |
|
119
|
|
|
protected function get_environment_for_request() { |
|
120
|
|
|
global $pagenow; |
|
121
|
|
|
|
|
122
|
|
|
$input = stripslashes_deep( $_GET ); |
|
|
|
|
|
|
123
|
|
|
$request_post_type = isset( $input['post_type'] ) ? $input['post_type'] : ''; |
|
124
|
|
|
$post_type = ''; |
|
125
|
|
|
|
|
126
|
|
|
if ( $this->post_id ) { |
|
127
|
|
|
$post_type = get_post_type( $this->post_id ); |
|
128
|
|
|
} elseif ( ! empty( $request_post_type ) ) { |
|
129
|
|
|
$post_type = $request_post_type; |
|
130
|
|
|
} elseif ( $pagenow === 'post-new.php' ) { |
|
131
|
|
|
$post_type = 'post'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$post = get_post( $this->post_id ); |
|
135
|
|
|
$post = $post ? $post : null; |
|
136
|
|
|
$environment = array( |
|
137
|
|
|
'post_id' => $post ? $post->ID : 0, |
|
138
|
|
|
'post_type' => $post ? $post->post_type : $post_type, |
|
139
|
|
|
'post' => $post, |
|
140
|
|
|
); |
|
141
|
|
|
return $environment; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Perform checks whether the container should be attached during the current request |
|
146
|
|
|
* |
|
147
|
|
|
* @return bool True if the container is allowed to be attached |
|
148
|
|
|
**/ |
|
149
|
|
|
public function is_valid_attach_for_request() { |
|
150
|
|
|
global $pagenow; |
|
151
|
|
|
|
|
152
|
|
|
if ( $pagenow !== 'post.php' && $pagenow !== 'post-new.php' ) { |
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$environment = $this->get_environment_for_request(); |
|
157
|
|
|
if ( ! $environment['post_type'] ) { |
|
158
|
|
|
return false; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this->static_conditions_pass(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get environment array for object id |
|
166
|
|
|
* |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function get_environment_for_object( $object_id ) { |
|
170
|
|
|
$post = get_post( intval( $object_id ) ); |
|
171
|
|
|
|
|
172
|
|
|
$environment = array( |
|
173
|
|
|
'post_id' => $post->ID, |
|
174
|
|
|
'post' => $post, |
|
175
|
|
|
'post_type' => get_post_type( $post->ID ), |
|
176
|
|
|
); |
|
177
|
|
|
return $environment; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Check container attachment rules against object id |
|
182
|
|
|
* |
|
183
|
|
|
* @param int $object_id |
|
184
|
|
|
* @return bool |
|
185
|
|
|
**/ |
|
186
|
|
View Code Duplication |
public function is_valid_attach_for_object( $object_id = null ) { |
|
|
|
|
|
|
187
|
|
|
$post = get_post( intval( $object_id ) ); |
|
188
|
|
|
|
|
189
|
|
|
if ( ! $post ) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $this->all_conditions_pass( intval( $post->ID ) ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Add meta box for each of the container post types |
|
198
|
|
|
**/ |
|
199
|
|
|
public function attach() { |
|
200
|
|
|
$this->post_types = $this->get_post_type_visibility(); |
|
201
|
|
|
|
|
202
|
|
|
foreach ( $this->post_types as $post_type ) { |
|
203
|
|
|
add_meta_box( |
|
204
|
|
|
$this->id, |
|
205
|
|
|
$this->title, |
|
206
|
|
|
array( $this, 'render' ), |
|
207
|
|
|
$post_type, |
|
208
|
|
|
$this->settings['panel_context'], |
|
209
|
|
|
$this->settings['panel_priority'] |
|
210
|
|
|
); |
|
211
|
|
|
|
|
212
|
|
|
add_filter( "postbox_classes_{$post_type}_{$this->id}", array( $this, 'add_postbox_classes' ) ); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Classes to add to the post meta box |
|
218
|
|
|
*/ |
|
219
|
|
|
public function add_postbox_classes( $classes ) { |
|
220
|
|
|
$classes[] = 'carbon-box'; |
|
221
|
|
|
return $classes; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Output the container markup |
|
226
|
|
|
**/ |
|
227
|
|
|
public function render() { |
|
228
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/post_meta.php'; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Set the post ID the container will operate with. |
|
233
|
|
|
* |
|
234
|
|
|
* @param int $post_id |
|
235
|
|
|
**/ |
|
236
|
|
|
public function set_post_id( $post_id ) { |
|
237
|
|
|
$this->post_id = $post_id; |
|
238
|
|
|
$this->get_datastore()->set_id( $post_id ); |
|
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Get array of post types this container can appear on conditionally |
|
243
|
|
|
* |
|
244
|
|
|
* @return array<string> |
|
245
|
|
|
*/ |
|
246
|
|
View Code Duplication |
public function get_post_type_visibility() { |
|
|
|
|
|
|
247
|
|
|
$all_post_types = get_post_types(); |
|
248
|
|
|
$filtered_collection = $this->condition_collection->filter( array( 'post_type' ) ); |
|
249
|
|
|
|
|
250
|
|
|
$shown_on = array(); |
|
251
|
|
|
foreach ( $all_post_types as $post_type ) { |
|
252
|
|
|
$environment = array( |
|
253
|
|
|
'post_type' => $post_type, |
|
254
|
|
|
); |
|
255
|
|
|
if ( $filtered_collection->is_fulfilled( $environment ) ) { |
|
256
|
|
|
$shown_on[] = $post_type; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
return $shown_on; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* COMMON USAGE METHODS |
|
264
|
|
|
*/ |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Show the container only on particular page referenced by it's path. |
|
268
|
|
|
* |
|
269
|
|
|
* @deprecated |
|
270
|
|
|
* @param int|string $page page ID or page path |
|
271
|
|
|
* @return object $this |
|
272
|
|
|
**/ |
|
273
|
|
|
public function show_on_page( $page ) { |
|
274
|
|
|
$page_id = absint( $page ); |
|
275
|
|
|
|
|
276
|
|
|
if ( $page_id && $page_id == $page ) { |
|
277
|
|
|
$page_obj = get_post( $page_id ); |
|
278
|
|
|
} else { |
|
279
|
|
|
$page_obj = get_page_by_path( $page ); |
|
280
|
|
|
} |
|
281
|
|
|
$page_id = ( $page_obj ) ? $page_obj->ID : -1; |
|
282
|
|
|
|
|
283
|
|
|
$this->and_when( 'post_id', '=', $page_id ); |
|
284
|
|
|
|
|
285
|
|
|
return $this; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Show the container only on pages whose parent is referenced by $parent_page_path. |
|
290
|
|
|
* |
|
291
|
|
|
* @deprecated |
|
292
|
|
|
* @param string $parent_page_path |
|
293
|
|
|
* @return object $this |
|
294
|
|
|
**/ |
|
295
|
|
|
public function show_on_page_children( $parent_page_path ) { |
|
296
|
|
|
$page = get_page_by_path( $parent_page_path ); |
|
297
|
|
|
$page_id = ( $page ) ? $page->ID : -1; |
|
298
|
|
|
$this->and_when( 'post_parent_id', '=', $page_id ); |
|
299
|
|
|
return $this; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Show the container only on pages whose template has filename $template_path. |
|
304
|
|
|
* |
|
305
|
|
|
* @deprecated |
|
306
|
|
|
* @param string|array $template_path |
|
307
|
|
|
* @return object $this |
|
308
|
|
|
**/ |
|
309
|
|
|
public function show_on_template( $template_path ) { |
|
310
|
|
|
// Backwards compatibility where only pages support templates |
|
311
|
|
|
if ( version_compare( get_bloginfo( 'version' ), '4.7', '<' ) ) { |
|
312
|
|
|
$this->show_on_post_type( 'page' ); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$template_paths = is_array( $template_path ) ? $template_path : array( $template_path ); |
|
316
|
|
|
$this->and_when( 'post_template', 'IN', $template_paths ); |
|
317
|
|
|
return $this; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Hide the container from pages whose template has filename $template_path. |
|
322
|
|
|
* |
|
323
|
|
|
* @deprecated |
|
324
|
|
|
* @param string|array $template_path |
|
325
|
|
|
* @return object $this |
|
326
|
|
|
**/ |
|
327
|
|
|
public function hide_on_template( $template_path ) { |
|
328
|
|
|
$template_paths = is_array( $template_path ) ? $template_path : array( $template_path ); |
|
329
|
|
|
$this->and_when( 'post_template', 'NOT IN', $template_paths ); |
|
330
|
|
|
return $this; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Show the container only on hierarchical posts of level $level. |
|
335
|
|
|
* Levels start from 1 (top level post) |
|
336
|
|
|
* |
|
337
|
|
|
* @deprecated |
|
338
|
|
|
* @param int $level |
|
339
|
|
|
* @return object $this |
|
340
|
|
|
**/ |
|
341
|
|
|
public function show_on_level( $level ) { |
|
342
|
|
|
$this->and_when( 'post_level', '=', intval( $level ) ); |
|
343
|
|
|
return $this; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Show the container only on posts from the specified format. |
|
348
|
|
|
* Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
|
349
|
|
|
* |
|
350
|
|
|
* @deprecated |
|
351
|
|
|
* @param string|array $post_format Name of the format as listed on Codex |
|
352
|
|
|
* @return object $this |
|
353
|
|
|
**/ |
|
354
|
|
|
public function show_on_post_format( $post_format ) { |
|
355
|
|
|
$post_formats = is_array( $post_format ) ? $post_format : array( $post_format ); |
|
356
|
|
|
$this->and_when( 'post_format', 'IN', $post_formats ); |
|
357
|
|
|
return $this; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* Show the container only on posts from the specified type(s). |
|
362
|
|
|
* |
|
363
|
|
|
* @deprecated |
|
364
|
|
|
* @param string|array $post_types |
|
365
|
|
|
* @return object $this |
|
366
|
|
|
**/ |
|
367
|
|
|
public function show_on_post_type( $post_types ) { |
|
368
|
|
|
$post_types = is_array( $post_types ) ? $post_types : array( $post_types ); |
|
369
|
|
|
$this->and_when( 'post_type', 'IN', $post_types ); |
|
370
|
|
|
return $this; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* Show the container only on posts from the specified category. |
|
375
|
|
|
* |
|
376
|
|
|
* @see show_on_taxonomy_term() |
|
377
|
|
|
* |
|
378
|
|
|
* @deprecated |
|
379
|
|
|
* @param string $category_slug |
|
380
|
|
|
* @return object $this |
|
381
|
|
|
**/ |
|
382
|
|
View Code Duplication |
public function show_on_category( $category_slug ) { |
|
|
|
|
|
|
383
|
|
|
$this->and_when( 'post_term', '=', array( |
|
384
|
|
|
'value' => $category_slug, |
|
385
|
|
|
'field' => 'slug', |
|
386
|
|
|
'taxonomy' => 'category', |
|
387
|
|
|
) ); |
|
388
|
|
|
return $this; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
|
393
|
|
|
* |
|
394
|
|
|
* @deprecated |
|
395
|
|
|
* @param string $taxonomy_slug |
|
396
|
|
|
* @param string $term_slug |
|
397
|
|
|
* @return object $this |
|
398
|
|
|
**/ |
|
399
|
|
View Code Duplication |
public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
|
|
|
|
|
|
400
|
|
|
$this->and_when( 'post_term', '=', array( |
|
401
|
|
|
'value' => $term_slug, |
|
402
|
|
|
'field' => 'slug', |
|
403
|
|
|
'taxonomy' => $taxonomy_slug, |
|
404
|
|
|
) ); |
|
405
|
|
|
return $this; |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Sets the meta box container context |
|
410
|
|
|
* |
|
411
|
|
|
* @see https://codex.wordpress.org/Function_Reference/add_meta_box |
|
412
|
|
|
* @param string $context ('normal', 'advanced' or 'side') |
|
413
|
|
|
*/ |
|
414
|
|
|
public function set_context( $context ) { |
|
415
|
|
|
$this->settings['panel_context'] = $context; |
|
416
|
|
|
|
|
417
|
|
|
return $this; |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
/** |
|
421
|
|
|
* Sets the meta box container priority |
|
422
|
|
|
* |
|
423
|
|
|
* @see https://codex.wordpress.org/Function_Reference/add_meta_box |
|
424
|
|
|
* @param string $priority ('high', 'core', 'default' or 'low') |
|
425
|
|
|
*/ |
|
426
|
|
|
public function set_priority( $priority ) { |
|
427
|
|
|
$this->settings['panel_priority'] = $priority; |
|
428
|
|
|
|
|
429
|
|
|
return $this; |
|
430
|
|
|
} |
|
431
|
|
|
} |
|
432
|
|
|
|