PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / vendor / inpsyde / assets / src / BaseAsset.php

BaseAsset.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at vendor/inpsyde/assets/src/BaseAsset.php

401 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Assets package.
5 *
6 * (c) Inpsyde GmbH
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 declare(strict_types=1);
13
14 namespace Inpsyde\Assets;
15
16 use Inpsyde\Assets\Handler\AssetHandler;
17 use Inpsyde\Assets\Util\AssetPathResolver;
18 use Inpsyde\Assets\OutputFilter\AssetOutputFilter;
19 use Inpsyde\Assets\OutputFilter\AttributesOutputFilter;
20 use Inpsyde\Assets\OutputFilter\InlineAssetOutputFilter;
21
22 /**
23 * phpcs:disable Inpsyde.CodeQuality.PropertyPerClassLimit.TooManyProperties
24 */
25 abstract class BaseAsset implements Asset
26 {
27 use ConfigureAutodiscoverVersionTrait;
28
29 /**
30 * @var string
31 */
32 protected $url = '';
33
34 /**
35 * Full filePath to an Asset which can
36 * be used to auto-discover version or
37 * load Asset content inline.
38 *
39 * @var string
40 */
41 protected $filePath = '';
42
43 /**
44 * @var string
45 */
46 protected $handle = '';
47
48 /**
49 * Dependencies to other Asset handles.
50 *
51 * @var string[]
52 */
53 protected $dependencies = [];
54
55 /**
56 * Location where the Asset will be enqueued.
57 *
58 * @var int
59 */
60 protected $location = self::FRONTEND;
61
62 /**
63 * Version can be auto-discovered if null.
64 *
65 * @see BaseAsset::enableAutodiscoverVersion().
66 *
67 * @var null|string
68 */
69 protected $version = null;
70
71 /**
72 * @var bool|callable(): bool
73 */
74 protected $enqueue = true;
75
76 /**
77 * @var callable[]|AssetOutputFilter[]|class-string<AssetOutputFilter>[]
78 */
79 protected $filters = [];
80
81 /**
82 * @var class-string<AssetHandler>|null
83 */
84 protected $handler = null;
85
86 /**
87 * Data which will be added via ...
88 * - WP_Script::add_data()
89 * - WP_Style::add_data()
90 *
91 * @var array<string, mixed>
92 */
93 protected $data = [];
94
95 /**
96 * Additional attributes to "link"- or "script"-tag.
97 *
98 * @var array<string, mixed>
99 */
100 protected $attributes = [];
101
102 /**
103 * @param string $handle
104 * @param string $url
105 * @param int $location
106 */
107 public function __construct(
108 string $handle,
109 string $url,
110 int $location = Asset::FRONTEND | Asset::ACTIVATE
111 ) {
112
113 $this->handle = $handle;
114 $this->url = $url;
115 $this->location = $location;
116 }
117
118 /**
119 * @return string
120 */
121 public function url(): string
122 {
123 return $this->url;
124 }
125
126 /**
127 * @return string
128 */
129 public function handle(): string
130 {
131 return $this->handle;
132 }
133
134 /**
135 * @return string
136 */
137 public function filePath(): string
138 {
139 $filePath = $this->filePath;
140
141 if ($filePath !== '') {
142 return $filePath;
143 }
144
145 try {
146 $filePath = AssetPathResolver::resolve($this->url());
147 } catch (\Throwable $throwable) {
148 $filePath = null;
149 }
150
151 // if replacement fails, don't set the url as path.
152 if ($filePath === null || !file_exists($filePath)) {
153 return '';
154 }
155
156 $this->withFilePath($filePath);
157
158 return $filePath;
159 }
160
161 /**
162 * @param string $filePath
163 *
164 * @return static
165 */
166 public function withFilePath(string $filePath): Asset
167 {
168 $this->filePath = $filePath;
169
170 return $this;
171 }
172
173 /**
174 * Returns a version which will be automatically generated based on file time by default.
175 *
176 * @return string|null
177 */
178 public function version(): ?string
179 {
180 $version = $this->version;
181
182 if ($version === null && $this->autodiscoverVersion) {
183 $filePath = $this->filePath();
184 $version = (string) filemtime($filePath);
185 $this->withVersion($version);
186
187 return $version;
188 }
189
190 return $version === null
191 ? null
192 : (string) $version;
193 }
194
195 /**
196 * @param string $version
197 *
198 * @return static
199 */
200 public function withVersion(string $version): Asset
201 {
202 $this->version = $version;
203
204 return $this;
205 }
206
207 /**
208 * @return string[]
209 */
210 public function dependencies(): array
211 {
212 return array_values(array_unique($this->dependencies));
213 }
214
215 /**
216 * @param string ...$dependencies
217 *
218 * @return static
219 */
220 public function withDependencies(string ...$dependencies): Asset
221 {
222 $this->dependencies = array_merge(
223 $this->dependencies,
224 $dependencies
225 );
226
227 return $this;
228 }
229
230 /**
231 * @return int
232 */
233 public function location(): int
234 {
235 return (int) $this->location;
236 }
237
238 /**
239 * @param int $location
240 *
241 * @return static
242 */
243 public function forLocation(int $location): Asset
244 {
245 $this->location = $location;
246
247 return $this;
248 }
249
250 /**
251 * @return callable[]|AssetOutputFilter[]|class-string<AssetOutputFilter>[]
252 */
253 public function filters(): array
254 {
255 return $this->filters;
256 }
257
258 /**
259 * @param callable|class-string<AssetOutputFilter> ...$filters
260 *
261 * @return static
262 *
263 * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
264 */
265 public function withFilters(...$filters): Asset
266 {
267 $this->filters = array_merge($this->filters, $filters);
268
269 return $this;
270 }
271
272 /**
273 * Shortcut to use the InlineFilter.
274 *
275 * @return static
276 */
277 public function useInlineFilter(): Asset
278 {
279 $this->withFilters(InlineAssetOutputFilter::class);
280
281 return $this;
282 }
283
284 /**
285 * @return bool
286 */
287 public function enqueue(): bool
288 {
289 $enqueue = $this->enqueue;
290 is_callable($enqueue) and $enqueue = $enqueue();
291
292 return (bool) $enqueue;
293 }
294
295 /**
296 * @param bool|callable(): bool $enqueue
297 *
298 * @return static
299 *
300 * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
301 * @psalm-suppress MoreSpecificImplementedParamType
302 */
303 public function canEnqueue($enqueue): Asset
304 {
305 // phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
306
307 $this->enqueue = $enqueue;
308
309 return $this;
310 }
311
312 /**
313 * @param class-string<AssetHandler> $handler
314 *
315 * @return static
316 */
317 public function useHandler(string $handler): Asset
318 {
319 $this->handler = $handler;
320
321 return $this;
322 }
323
324 /**
325 * @return class-string<AssetHandler>
326 */
327 public function handler(): string
328 {
329 if (!$this->handler) {
330 $this->handler = $this->defaultHandler();
331 }
332
333 return $this->handler;
334 }
335
336 /**
337 * @return class-string<AssetHandler> className of the default handler
338 */
339 abstract protected function defaultHandler(): string;
340
341 /**
342 * @return array<string, mixed>
343 */
344 public function data(): array
345 {
346 return $this->data;
347 }
348
349 /**
350 * Allows to set additional data via WP_Script::add_data() or WP_Style::add_data().
351 *
352 * @param array<string, mixed> $data
353 *
354 * @return static
355 */
356 public function withData(array $data): Asset
357 {
358 $this->data = array_merge($this->data, $data);
359
360 return $this;
361 }
362
363 /**
364 * Shortcut for Asset::withData(['conditional' => $condition]);
365 *
366 * @param string $condition
367 *
368 * @return static
369 */
370 public function withCondition(string $condition): Asset
371 {
372 $this->withData(['conditional' => $condition]);
373
374 return $this;
375 }
376
377 /**
378 * @return array<string, mixed>
379 */
380 public function attributes(): array
381 {
382 return $this->attributes;
383 }
384
385 /**
386 * Allows you to set additional attributes to your "link"- or "script"-tag.
387 * Existing attributes like "src" or "id" will not be overwrite.
388 *
389 * @param array<string, mixed> $attributes
390 *
391 * @return static
392 */
393 public function withAttributes(array $attributes): Asset
394 {
395 $this->attributes = array_merge($this->attributes, $attributes);
396 $this->withFilters(AttributesOutputFilter::class);
397
398 return $this;
399 }
400 }
401