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 / Script.php

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

319 lines 7.4 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\Handler\ScriptHandler;
18
19 class Script extends BaseAsset implements Asset
20 {
21 /**
22 * @var array<string, mixed>
23 */
24 protected $localize = [];
25
26 /**
27 * @var array{after:string[], before:string[]}
28 */
29 protected $inlineScripts = [
30 'after' => [],
31 'before' => [],
32 ];
33
34 /**
35 * @var bool
36 */
37 protected $inFooter = true;
38
39 /**
40 * @var array{domain:string, path:string|null}
41 */
42 protected $translation = [
43 'domain' => '',
44 'path' => null,
45 ];
46
47 /**
48 * @var bool
49 */
50 protected $resolvedDependencyExtractionPlugin = false;
51
52 /**
53 * @return array<string, mixed>
54 */
55 public function localize(): array
56 {
57 $output = [];
58 foreach ($this->localize as $objectName => $data) {
59 $output[$objectName] = is_callable($data)
60 ? $data()
61 : $data;
62 }
63
64 return $output;
65 }
66
67 /**
68 * @param string $objectName
69 * @param string|int|array|callable $data
70 *
71 * @return static
72 *
73 * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
74 */
75 public function withLocalize(string $objectName, $data): Script
76 {
77 // phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
78
79 $this->localize[$objectName] = $data;
80
81 return $this;
82 }
83
84 /**
85 * @return bool
86 */
87 public function inFooter(): bool
88 {
89 return $this->inFooter;
90 }
91
92 /**
93 * @return static
94 */
95 public function isInFooter(): Script
96 {
97 $this->inFooter = true;
98
99 return $this;
100 }
101
102 /**
103 * @return static
104 */
105 public function isInHeader(): Script
106 {
107 $this->inFooter = false;
108
109 return $this;
110 }
111
112 /**
113 * @return array{before:string[], after:string[]}
114 */
115 public function inlineScripts(): array
116 {
117 return $this->inlineScripts;
118 }
119
120 /**
121 * @param string $jsCode
122 *
123 * @return static
124 */
125 public function prependInlineScript(string $jsCode): Script
126 {
127 $this->inlineScripts['before'][] = $jsCode;
128
129 return $this;
130 }
131
132 /**
133 * @param string $jsCode
134 *
135 * @return static
136 */
137 public function appendInlineScript(string $jsCode): Script
138 {
139 $this->inlineScripts['after'][] = $jsCode;
140
141 return $this;
142 }
143
144 /**
145 * @return array{domain:string, path:string|null}
146 */
147 public function translation(): array
148 {
149 return $this->translation;
150 }
151
152 /**
153 * @param string $domain
154 * @param string|null $path
155 *
156 * @return static
157 */
158 public function withTranslation(string $domain = 'default', string $path = null): Script
159 {
160 $this->translation = ['domain' => $domain, 'path' => $path];
161
162 return $this;
163 }
164
165 /**
166 * Wrapper function to set AsyncScriptOutputFilter as filter.
167 *
168 * @return static
169 * @deprecated use Script::withAttributes(['async' => true]);
170 */
171 public function useAsyncFilter(): Script
172 {
173 $this->withAttributes(['async' => true]);
174
175 return $this;
176 }
177
178 /**
179 * Wrapper function to set DeferScriptOutputFilter as filter.
180 *
181 * @return static
182 * @deprecated use Script::withAttributes(['defer' => true]);
183 */
184 public function useDeferFilter(): Script
185 {
186 $this->withAttributes(['defer' => true]);
187
188 return $this;
189 }
190
191 /**
192 * {@inheritDoc}
193 */
194 protected function defaultHandler(): string
195 {
196 return ScriptHandler::class;
197 }
198
199 /**
200 * @deprecated when calling Script::version() or Script::dependencies(),
201 * we will automatically resolve the dependency extraction plugin files.
202 * This method will be removed in future.
203 *
204 * @see https://github.com/WordPress/gutenberg/tree/master/packages/dependency-extraction-webpack-plugin
205 */
206 public function useDependencyExtractionPlugin(): Script
207 {
208 return $this;
209 }
210
211 /**
212 * {@inheritDoc}
213 */
214 public function version(): ?string
215 {
216 $this->resolveDependencyExtractionPlugin();
217
218 return parent::version();
219 }
220
221 /**
222 * {@inheritDoc}
223 */
224 public function dependencies(): array
225 {
226 $this->resolveDependencyExtractionPlugin();
227
228 return parent::dependencies();
229 }
230
231 /**
232 * @return bool
233 *
234 * phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
235 * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
236 * @psalm-suppress MixedArrayAccess
237 * @psalm-suppress PossiblyFalseArgument
238 * @psalm-suppress UnresolvableInclude
239 */
240 protected function resolveDependencyExtractionPlugin(): bool
241 {
242 if ($this->resolvedDependencyExtractionPlugin) {
243 return false;
244 }
245 $this->resolvedDependencyExtractionPlugin = true;
246
247 $depsFile = $this->findDepdendencyFile();
248 if (!$depsFile) {
249 return false;
250 }
251
252 $depsFilePath = $depsFile->getPathname();
253 $data = $depsFile->getExtension() === 'json'
254 ? @json_decode(@file_get_contents($depsFilePath), true)
255 : @require $depsFilePath;
256
257 /** @var string[] $dependencies */
258 $dependencies = $data['dependencies'] ?? [];
259 /** @var string|null $version */
260 $version = $data['version'] ?? null;
261
262 $this->withDependencies(...$dependencies);
263 if (!$this->version && $version) {
264 $this->withVersion($version);
265 }
266
267 return true;
268 }
269
270 /**
271 * Searching for in directory of the Script:
272 *
273 * - {fileName}.asset.json
274 * - {fileName}.{hash}.asset.json
275 * - {fileName}.asset.php
276 * - {fileName}.{hash}.asset.php
277 *
278 * @return \DirectoryIterator|null
279 */
280 protected function findDepdendencyFile(): ?\DirectoryIterator
281 {
282 try {
283 $filePath = $this->filePath();
284 if ($filePath === '') {
285 return null;
286 }
287
288 $path = dirname($filePath) . '/';
289
290 $fileName = str_replace([$path, '.js'], '', $filePath);
291 // It might be possible that the script file contains a version hash as well.
292 // So we need to split it apart and just use the first part of the file.
293 $fileNamePieces = explode('.', $fileName);
294 $fileName = $fileNamePieces[0];
295
296 $regex = '/' . $fileName . '(?:\.[a-zA-Z0-9]+)?\.asset\.(json|php)/';
297
298 $depsFile = null;
299 foreach (new \DirectoryIterator($path) as $fileInfo) {
300 if (
301 $fileInfo->isDot()
302 || $fileInfo->isDir()
303 || !in_array($fileInfo->getExtension(), ['json', 'php'], true)
304 ) {
305 continue;
306 }
307 if (preg_match($regex, $fileInfo->getFilename())) {
308 $depsFile = $fileInfo;
309 break;
310 }
311 }
312
313 return $depsFile;
314 } catch (\Throwable $exception) {
315 return null;
316 }
317 }
318 }
319