1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leonidas\Library\Core\Asset; |
4
|
|
|
|
5
|
|
|
use Leonidas\Contracts\Ui\Asset\InlineScriptCollectionInterface; |
6
|
|
|
use Leonidas\Contracts\Ui\Asset\InlineScriptInterface; |
7
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptCollectionInterface; |
8
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptInterface; |
9
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptLoaderInterface; |
10
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptLocalizationCollectionInterface; |
11
|
|
|
use Leonidas\Contracts\Ui\Asset\ScriptLocalizationInterface; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
13
|
|
|
|
14
|
|
|
class ScriptLoader implements ScriptLoaderInterface |
15
|
|
|
{ |
16
|
|
|
public function load(ScriptCollectionInterface $scripts, ServerRequestInterface $request) |
17
|
|
|
{ |
18
|
|
|
foreach ($scripts->getScripts() as $script) { |
19
|
|
|
if ($script->shouldBeLoaded($request)) { |
20
|
|
|
if ($script->shouldBeEnqueued()) { |
21
|
|
|
$this->enqueueScript($script); |
22
|
|
|
} else { |
23
|
|
|
$this->registerScript($script); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function support(InlineScriptCollectionInterface $scripts, ServerRequestInterface $request) |
30
|
|
|
{ |
31
|
|
|
foreach ($scripts->getScripts() as $script) { |
32
|
|
|
if ($script->shouldBeLoaded($request)) { |
33
|
|
|
$this->addInlineScript($script); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function localize(ScriptLocalizationCollectionInterface $localizations, ServerRequestInterface $request) |
39
|
|
|
{ |
40
|
|
|
foreach ($localizations->getLocalizations() as $localization) { |
41
|
|
|
if ($localization->shouldBeLoaded($request)) { |
42
|
|
|
$this->localizeScript($localization); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function activate(string ...$scripts) |
48
|
|
|
{ |
49
|
|
|
foreach ($scripts as $script) { |
50
|
|
|
wp_enqueue_script($script); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function deactivate(string ...$scripts) |
55
|
|
|
{ |
56
|
|
|
foreach ($scripts as $script) { |
57
|
|
|
wp_dequeue_script($script); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function registerScript(ScriptInterface $script) |
62
|
|
|
{ |
63
|
|
|
wp_register_script( |
|
|
|
|
64
|
|
|
$script->getHandle(), |
65
|
|
|
$script->getSrc(), |
66
|
|
|
$script->getDependencies(), |
67
|
|
|
$script->getVersion(), |
68
|
|
|
$script->shouldLoadInFooter() |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
// $this->loadScriptAddons($script); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function enqueueScript(ScriptInterface $script) |
75
|
|
|
{ |
76
|
|
|
wp_enqueue_script( |
|
|
|
|
77
|
|
|
$script->getHandle(), |
78
|
|
|
$script->getSrc(), |
79
|
|
|
$script->getDependencies(), |
80
|
|
|
$script->getVersion(), |
81
|
|
|
$script->shouldLoadInFooter() |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
// $this->loadScriptAddons($script); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function addInlineScript(InlineScriptInterface $script) |
88
|
|
|
{ |
89
|
|
|
wp_add_inline_script( |
|
|
|
|
90
|
|
|
$script->getHandle(), |
91
|
|
|
$script->getCode(), |
92
|
|
|
$script->getPosition() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function localizeScript(ScriptLocalizationInterface $localization) |
97
|
|
|
{ |
98
|
|
|
wp_localize_script( |
|
|
|
|
99
|
|
|
$localization->getHandle(), |
100
|
|
|
$localization->getVariable(), |
101
|
|
|
$localization->getData() |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// protected function loadScriptAddons(ScriptInterface $script) |
106
|
|
|
// { |
107
|
|
|
// if ($script->hasLocalizations()) { |
108
|
|
|
// foreach ($script->getLocalizations()->getLocalizations() as $localization) { |
109
|
|
|
// $this->localizeScript($localization); |
110
|
|
|
// } |
111
|
|
|
// } |
112
|
|
|
|
113
|
|
|
// if ($script->hasInlineSupport()) { |
114
|
|
|
// foreach ($script->getInlineSupport()->getScripts() as $inlineScript) { |
115
|
|
|
// $this->addInlineScript($inlineScript); |
116
|
|
|
// } |
117
|
|
|
// } |
118
|
|
|
// } |
119
|
|
|
} |
120
|
|
|
|