Script.php
1.71 KB
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
<?php
namespace AC\Asset;
use AC\Asset\Script\Inline\Position;
class Script extends Enqueueable {
protected $in_footer;
public function __construct(
string $handle,
Location $location = null,
array $dependencies = [],
bool $in_footer = false
) {
parent::__construct( $handle, $location, $dependencies );
$this->in_footer = $in_footer;
}
protected function is_registered(): bool {
return wp_script_is( $this->get_handle(), 'registered' );
}
public function is_in_footer(): bool {
return $this->in_footer;
}
public function register(): void {
if ( ! $this->location instanceof Location ) {
return;
}
wp_register_script(
$this->get_handle(),
$this->location->get_url(),
$this->dependencies,
$this->get_version(),
$this->is_in_footer()
);
}
public function enqueue(): void {
if ( wp_script_is( $this->get_handle() ) ) {
return;
}
if ( ! $this->is_registered() ) {
$this->register();
}
wp_enqueue_script( $this->get_handle() );
}
public function localize( string $name, Script\Localize\Translation $translation ): self {
if ( ! $this->is_registered() ) {
$this->register();
}
wp_localize_script( $this->handle, $name, $translation->get_translation() );
return $this;
}
public function add_inline( string $data, Position $position = null ): self {
if ( null === $position ) {
$position = Position::after();
}
if ( ! $this->is_registered() ) {
$this->register();
}
wp_add_inline_script( $this->handle, $data, (string) $position );
return $this;
}
public function add_inline_variable( string $name, $data ): self {
return $this->add_inline(
sprintf( 'var %s = %s;', $name, json_encode( $data ) ),
Position::before()
);
}
}