-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathScriptAsset.php
149 lines (129 loc) · 3.95 KB
/
ScriptAsset.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
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Assets;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidAsset;
/**
* Class ScriptAsset
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Assets
*/
class ScriptAsset extends BaseAsset {
/**
* Whether the script should be printed in the footer.
*
* @var bool
*/
protected $in_footer = false;
/**
* Array of localizations to add to the script.
*
* @var array
*/
protected $localizations = [];
/**
* Array of inline scripts to pass generic data from PHP to JavaScript with JSON format.
*
* @var array
*/
protected $inline_scripts = [];
/**
* ScriptAsset constructor.
*
* @param string $handle The asset handle.
* @param string $uri The URI for the asset.
* @param array $dependencies (Optional) Any dependencies the asset has.
* @param string $version (Optional) A version string for the asset. Will default to
* the plugin version if not set.
* @param callable|null $enqueue_condition_callback (Optional) The asset is always enqueued if this callback
* returns true or isn't set.
* @param bool $in_footer (Optional) Whether the script should be printed in the
* footer. Defaults to false.
*/
public function __construct(
string $handle,
string $uri,
array $dependencies = [],
string $version = '',
callable $enqueue_condition_callback = null,
bool $in_footer = false
) {
$this->in_footer = $in_footer;
parent::__construct( 'js', $handle, $uri, $dependencies, $version, $enqueue_condition_callback );
}
/**
* Add a localization to the script.
*
* @param string $object_name The object name.
* @param array $data Array of data for the object.
*
* @return $this
*/
public function add_localization( string $object_name, array $data ): ScriptAsset {
$this->localizations[ $object_name ] = $data;
return $this;
}
/**
* Add a inline script to pass generic data from PHP to JavaScript.
*
* @param string $variable_name The global JavaScript variable name.
* @param array $data Array of data to be encoded to JSON format.
*
* @return $this
*/
public function add_inline_script( string $variable_name, array $data ): ScriptAsset {
$this->inline_scripts[ $variable_name ] = $data;
return $this;
}
/**
* Get the register callback to use.
*
* @return callable
*/
protected function get_register_callback(): callable {
return function () {
if ( wp_script_is( $this->handle, 'registered' ) ) {
return;
}
wp_register_script(
$this->handle,
$this->uri,
$this->dependencies,
$this->version,
$this->in_footer
);
};
}
/**
* Get the enqueue callback to use.
*
* @return callable
*/
protected function get_enqueue_callback(): callable {
return function () {
if ( ! wp_script_is( $this->handle, 'registered' ) ) {
throw InvalidAsset::asset_not_registered( $this->handle );
}
foreach ( $this->localizations as $object_name => $data_array ) {
wp_localize_script( $this->handle, $object_name, $data_array );
}
foreach ( $this->inline_scripts as $variable_name => $data_array ) {
$inline_script = "var $variable_name = " . json_encode( $data_array );
wp_add_inline_script( $this->handle, $inline_script, 'before' );
}
wp_enqueue_script( $this->handle );
if ( in_array( 'wp-i18n', $this->dependencies, true ) ) {
wp_set_script_translations( $this->handle, 'google-listings-and-ads' );
}
};
}
/**
* Get the dequeue callback to use.
*
* @return callable
*/
protected function get_dequeue_callback(): callable {
return function () {
wp_dequeue_script( $this->handle );
};
}
}