-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathBaseAsset.php
312 lines (272 loc) · 7.03 KB
/
BaseAsset.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Assets;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidAsset;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
/**
* Class BaseAsset
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Assets
*/
abstract class BaseAsset implements Asset {
use PluginHelper;
/**
* The file extension for the source.
*
* @var string
*/
protected $file_extension;
/**
* Priority for registering an asset.
*
* @var int
*/
protected $registration_priority = 1;
/**
* Priority for enqueuing an asset.
*
* @var int
*/
protected $enqueue_priority = 10;
/**
* Priority for dequeuing an asset.
*
* @var int
*/
protected $dequeue_priority = 50;
/**
* The asset handle.
*
* @var string
*/
protected $handle;
/**
* The full URI to the asset.
*
* @var string
*/
protected $uri;
/**
* Array of dependencies for the asset.
*
* @var array
*/
protected $dependencies = [];
/**
* The version string for the asset.
*
* @var string
*/
protected $version;
/**
* @var callable
*/
protected $enqueue_condition_callback;
/**
* BaseAsset constructor.
*
* @param string $file_extension The asset file extension.
* @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.
*/
public function __construct(
string $file_extension,
string $handle,
string $uri,
array $dependencies = [],
string $version = '',
callable $enqueue_condition_callback = null
) {
$this->file_extension = $file_extension;
$this->handle = $handle;
$this->uri = $this->get_uri_from_path( $uri );
$this->dependencies = $dependencies;
$this->version = $version ?: $this->get_version();
$this->enqueue_condition_callback = $enqueue_condition_callback;
}
/**
* Get the handle of the asset. The handle serves as the ID within WordPress.
*
* @return string
*/
public function get_handle(): string {
return $this->handle;
}
/**
* Get the URI for the asset.
*
* @return string
*/
public function get_uri(): string {
return $this->uri;
}
/**
* Get the condition callback to run when enqueuing the asset.
*
* The asset will only be enqueued if the callback returns true.
*
* @return bool
*/
public function can_enqueue(): bool {
return (bool) call_user_func( $this->enqueue_condition_callback, $this );
}
/**
* Enqueue the asset within WordPress.
*/
public function enqueue(): void {
if ( ! $this->can_enqueue() ) {
return;
}
$this->defer_action(
$this->get_enqueue_action(),
$this->get_enqueue_callback(),
$this->enqueue_priority
);
}
/**
* Dequeue the asset within WordPress.
*/
public function dequeue(): void {
$this->defer_action(
$this->get_dequeue_action(),
$this->get_dequeue_callback(),
$this->dequeue_priority
);
}
/**
* Register a service.
*/
public function register(): void {
$this->defer_action(
$this->get_register_action(),
$this->get_register_callback(),
$this->registration_priority
);
}
/**
* Get the register action to use.
*
* @since 0.1.0
*
* @return string Register action to use.
*/
protected function get_register_action(): string {
return $this->get_enqueue_action();
}
/**
* Get the enqueue action to use.
*
* @return string
*/
protected function get_enqueue_action(): string {
return 'wp_enqueue_scripts';
}
/**
* Get the dequeue action to use.
*
* @return string
*/
protected function get_dequeue_action(): string {
return 'wp_print_scripts';
}
/**
* Add a callable to an action, or run it immediately if the action has already fired.
*
* @param string $action
* @param callable $callback
* @param int $priority
*/
protected function defer_action( string $action, callable $callback, int $priority = 10 ): void {
if ( did_action( $action ) ) {
$callback();
return;
}
add_action( $action, $callback, $priority );
}
/**
* Convert a file path to a URI for a source.
*
* @param string $path The source file path.
*
* @return string
*/
protected function get_uri_from_path( string $path ): string {
$path = $this->normalize_source_path( $path );
$path = str_replace( $this->get_root_dir(), '', $path );
return $this->get_plugin_url( $path );
}
/**
* Normalize a source path with a given file extension.
*
* @param string $path The path to normalize.
*
* @return string
*/
protected function normalize_source_path( string $path ): string {
$path = ltrim( $path, '/' );
$path = $this->maybe_add_extension( $path );
$path = "{$this->get_root_dir()}/{$path}";
return $this->maybe_add_minimized_extension( $path );
}
/**
* Possibly add an extension to a path.
*
* @param string $path Path where an extension may be needed.
*
* @return string
*/
protected function maybe_add_extension( string $path ): string {
$detected_extension = pathinfo( $path, PATHINFO_EXTENSION );
if ( $this->file_extension !== $detected_extension ) {
$path .= ".{$this->file_extension}";
}
return $path;
}
/**
* Possibly add a minimized extension to a path.
*
* @param string $path Path where a minimized extension may be needed.
*
* @return string
* @throws InvalidAsset When no asset can be found.
*/
protected function maybe_add_minimized_extension( string $path ): string {
$minimized_path = str_replace( ".{$this->file_extension}", ".min.{$this->file_extension}", $path );
// Validate that at least one version of the file exists.
$path_readable = is_readable( $path );
$minimized_readable = is_readable( $minimized_path );
if ( ! $path_readable && ! $minimized_readable ) {
throw InvalidAsset::invalid_path( $path );
}
// If we only have one available, return the available one no matter what.
if ( ! $minimized_readable ) {
return $path;
} elseif ( ! $path_readable ) {
return $minimized_path;
}
return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? $path : $minimized_path;
}
/**
* Get the register callback to use.
*
* @return callable
*/
abstract protected function get_register_callback(): callable;
/**
* Get the enqueue callback to use.
*
* @return callable
*/
abstract protected function get_enqueue_callback(): callable;
/**
* Get the dequeue callback to use.
*
* @return callable
*/
abstract protected function get_dequeue_callback(): callable;
}