PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.0.13
Admin Columns v7.0.13
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Asset / Script.php
codepress-admin-columns / classes / Asset Last commit date
Location 3 months ago Script 3 months ago Assets.php 3 months ago Enqueueable.php 3 months ago Enqueueables.php 3 months ago Location.php 3 months ago Script.php 3 months ago Style.php 3 months ago
Script.php
114 lines
1 <?php
2
3 namespace AC\Asset;
4
5 use AC\Asset\Script\Inline\Position;
6
7 class Script extends Enqueueable
8 {
9
10 protected bool $in_footer;
11
12 protected array $templates = [];
13
14 public function __construct(
15 string $handle,
16 ?Location $location = null,
17 array $dependencies = [],
18 bool $in_footer = false
19 ) {
20 parent::__construct($handle, $location, $dependencies);
21
22 $this->in_footer = $in_footer;
23 }
24
25 protected function is_registered(): bool
26 {
27 return wp_script_is($this->get_handle(), 'registered');
28 }
29
30 public function is_in_footer(): bool
31 {
32 return $this->in_footer;
33 }
34
35 public function register(): void
36 {
37 if ( ! $this->location instanceof Location) {
38 return;
39 }
40
41 $version = $this->get_version();
42
43 wp_register_script(
44 $this->get_handle(),
45 $this->location->get_url(),
46 $this->dependencies,
47 $version !== null
48 ? (string)$version
49 : null,
50 $this->is_in_footer()
51 );
52 }
53
54 public function enqueue(): void
55 {
56 if (wp_script_is($this->get_handle())) {
57 return;
58 }
59
60 if ( ! $this->is_registered()) {
61 $this->register();
62 }
63
64 wp_enqueue_script($this->get_handle());
65
66 if ($this->templates) {
67 // Allows JS frameworks to use PHP templates
68 $this->add_inline(
69 sprintf(
70 'window.addEventListener("DOMContentLoaded",function(){document.body.insertAdjacentHTML("beforeend", %s);});',
71 json_encode(implode('', $this->templates))
72 )
73 );
74 }
75 }
76
77 public function localize(string $name, Script\Localize\Translation $translation): self
78 {
79 if ( ! $this->is_registered()) {
80 $this->register();
81 }
82
83 wp_localize_script($this->handle, $name, $translation->get_translation());
84
85 return $this;
86 }
87
88 public function add_inline(string $data, ?Position $position = null): self
89 {
90 if ( ! $this->is_registered()) {
91 $this->register();
92 }
93
94 wp_add_inline_script($this->handle, $data, (string)($position ?? Position::after()));
95
96 return $this;
97 }
98
99 public function add_inline_variable(string $name, $data): self
100 {
101 return $this->add_inline(
102 sprintf('var %s = %s;', $name, json_encode($data)),
103 Position::before()
104 );
105 }
106
107 public function add_template(string $id, string $html): self
108 {
109 $this->templates[] = sprintf('<template id="%s">%s</template>', esc_attr($id), $html);
110
111 return $this;
112 }
113
114 }