-
Notifications
You must be signed in to change notification settings - Fork 0
/
enqueue.php
89 lines (69 loc) · 2.14 KB
/
enqueue.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
<?php
defined('ABSPATH') or die();
/**
* Store the $args of every registered field
*
* It will be passed to JS in order to init our react fields
*/
$fields->enqueued = [
'fields' => [],
'elements' => []
];
$fields->is_enqueued = false;
$fields->enqueue_item = function(
string $name,
string $type,
array $args
) use($fields) : void {
$args['context'] = $fields->current_context;
$fields->enqueued[ $type ][ $name ] = $args;
};
$fields->enqueue = function(array $config = []) use($fields) {
if( ! empty($config['context']) && is_array($config['context']) ) {
$fields->enqueued_contexts = [
...$fields->enqueued_contexts,
...$config['context']
];
}
$contexts = ! empty($fields->enqueued_contexts)
? $fields->enqueued_contexts
: ['default'];
foreach( $contexts as $context ) {
wp_enqueue_style(
'tangible-fields-' . $context,
plugins_url( '/assets', __FILE__ ) . '/build/' . $context . '/index.min.css',
[],
$fields->version
);
}
wp_enqueue_script(
'tangible-fields',
plugins_url( '/assets', __FILE__ ) . '/build/index.min.js',
[ 'wp-element' ],
$fields->version,
true
);
$data = [
'api' => [
'nonce' => wp_create_nonce( 'wp_rest' ),
'endpoint' => [
'media' => esc_url_raw( rest_url( '/wp/v2/media/' ) ),
],
],
'fields' => $fields->enqueued['fields'],
'elements' => $fields->enqueued['elements'],
'dynamics' => $fields->get_dynamic_value_data(),
'mimetypes' => get_allowed_mime_types()
];
wp_add_inline_script( 'tangible-fields', 'var TangibleFields = ' . json_encode($data) . ';', 'before' );
$fields->is_enqueued = true;
};
$fields->maybe_enqueue_scripts = function() use($fields) : void {
$has_registrations = empty($fields->enqueued['fields']) && empty($fields->enqueued['elements']);
if( $has_registrations || $fields->is_enqueued ) {
return;
}
$fields->enqueue();
};
add_action( 'wp_footer', $fields->maybe_enqueue_scripts );
add_action( 'admin_footer', $fields->maybe_enqueue_scripts );