|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\values\assets; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use EventEspresso\core\domain\DomainInterface; |
|
7
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class JavascriptAsset |
|
11
|
|
|
* Details for a Javascript asset |
|
12
|
|
|
* |
|
13
|
|
|
* @package EventEspresso\core\domain\values\assets |
|
14
|
|
|
* @author Brent Christensen |
|
15
|
|
|
* @since 4.9.62.p |
|
16
|
|
|
*/ |
|
17
|
|
|
class JavascriptAsset extends BrowserAsset |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var boolean $load_in_footer |
|
22
|
|
|
*/ |
|
23
|
|
|
private $load_in_footer = false; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var boolean $requires_translation |
|
27
|
|
|
*/ |
|
28
|
|
|
private $requires_translation = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var boolean $has_inline_data |
|
32
|
|
|
*/ |
|
33
|
|
|
private $has_inline_data = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Closure $inline_data_callback |
|
37
|
|
|
*/ |
|
38
|
|
|
private $inline_data_callback; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Asset constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $handle |
|
45
|
|
|
* @param string $source |
|
46
|
|
|
* @param array $dependencies |
|
47
|
|
|
* @param bool $load_in_footer |
|
48
|
|
|
* @param DomainInterface $domain |
|
49
|
|
|
* @throws InvalidDataTypeException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( |
|
52
|
|
|
$handle, |
|
53
|
|
|
$source, |
|
54
|
|
|
array $dependencies, |
|
55
|
|
|
$load_in_footer, |
|
56
|
|
|
DomainInterface $domain |
|
57
|
|
|
) { |
|
58
|
|
|
parent::__construct(Asset::TYPE_JS, $handle, $source, $dependencies, $domain); |
|
59
|
|
|
$this->setLoadInFooter($load_in_footer); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function loadInFooter() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->load_in_footer; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param bool $load_in_footer |
|
74
|
|
|
*/ |
|
75
|
|
|
private function setLoadInFooter($load_in_footer = true) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->load_in_footer = filter_var($load_in_footer, FILTER_VALIDATE_BOOLEAN); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function requiresTranslation() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->requires_translation; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param bool $requires_translation |
|
92
|
|
|
* @return JavascriptAsset |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setRequiresTranslation($requires_translation = true) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->requires_translation = filter_var($requires_translation, FILTER_VALIDATE_BOOLEAN); |
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function hasInlineData() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->has_inline_data; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param bool $has_inline_data |
|
112
|
|
|
* @return JavascriptAsset |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setHasInlineData($has_inline_data = true) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->has_inline_data = filter_var($has_inline_data, FILTER_VALIDATE_BOOLEAN); |
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return Closure |
|
123
|
|
|
*/ |
|
124
|
|
|
public function inlineDataCallback() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->inline_data_callback; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
|
|
public function hasInlineDataCallback() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->inline_data_callback instanceof Closure; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param Closure $inline_data_callback |
|
141
|
|
|
* @return JavascriptAsset |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setInlineDataCallback(Closure $inline_data_callback) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->inline_data_callback = $inline_data_callback; |
|
146
|
|
|
$this->setHasInlineData(); |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @since 4.9.62.p |
|
153
|
|
|
*/ |
|
154
|
|
|
public function enqueueAsset() |
|
155
|
|
|
{ |
|
156
|
|
|
wp_enqueue_script($this->handle()); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|