Script.php
893 Bytes
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
<?php
namespace AC\Asset;
class Script extends Enqueueable {
public function register() {
if ( null === $this->location ) {
return;
}
wp_register_script(
$this->get_handle(),
$this->location->get_url(),
$this->dependencies,
$this->get_version()
);
}
public function add_inline_variable( $name, $variable, $before = true ) {
if ( is_array( $variable ) ) {
$variable = json_encode( $variable );
}
if ( is_bool( $variable ) ) {
$variable = $variable ? 'true' : 'false';
}
wp_add_inline_script(
$this->get_handle(),
sprintf( "var %s = %s;", $name, $variable ),
$before
? 'before'
: 'after'
);
}
public function enqueue() {
if ( wp_script_is( $this->get_handle() ) ) {
return;
}
if ( ! wp_script_is( $this->get_handle(), 'registered' ) ) {
$this->register();
}
wp_enqueue_script( $this->get_handle() );
}
}