|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YIKES Inc. Easy Mailchimp Forms Plugin. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Yikes\EasyForms |
|
6
|
|
|
* @author Freddie Mixell |
|
7
|
|
|
* @license GPL2 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace YIKES\EasyForms\Assets; |
|
11
|
|
|
|
|
12
|
|
|
use Closure; |
|
13
|
|
|
use YIKES\EasyForms\Exception\InvalidURI; |
|
14
|
|
|
use YIKES\EasyForms\PluginHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Abstract class BaseAsset. |
|
18
|
|
|
* |
|
19
|
|
|
* @since %VERSION% |
|
20
|
|
|
* |
|
21
|
|
|
* @package Yikes\EasyForms\Assets |
|
22
|
|
|
* @author Freddie Mixell |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class BaseAsset implements Asset { |
|
25
|
|
|
|
|
26
|
|
|
use PluginHelper; |
|
27
|
|
|
|
|
28
|
|
|
const REGISTER_PRIORITY = 1; |
|
29
|
|
|
const ENQUEUE_PRIORITY = 10; |
|
30
|
|
|
const DEQUEUE_PRIORITY = 20; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Handle of the asset. |
|
34
|
|
|
* |
|
35
|
|
|
* @since %VERSION% |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $handle; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the handle of the asset. |
|
43
|
|
|
* |
|
44
|
|
|
* @since %VERSION% |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function get_handle() { |
|
49
|
|
|
return $this->handle; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Register the current Registerable. |
|
54
|
|
|
* |
|
55
|
|
|
* @since %VERSION% |
|
56
|
|
|
*/ |
|
57
|
|
|
public function register() { |
|
58
|
|
|
$this->deferred_action( $this->get_register_action(), $this->get_register_closure(), static::REGISTER_PRIORITY ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Enqueue the asset. |
|
63
|
|
|
* |
|
64
|
|
|
* @since %VERSION% |
|
65
|
|
|
*/ |
|
66
|
|
|
public function enqueue() { |
|
67
|
|
|
$this->deferred_action( $this->get_enqueue_action(), $this->get_enqueue_closure(), static::ENQUEUE_PRIORITY ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Dequeue the asset. |
|
72
|
|
|
* |
|
73
|
|
|
* @since %VERSION% |
|
74
|
|
|
*/ |
|
75
|
|
|
public function dequeue() { |
|
76
|
|
|
$this->deferred_action( $this->get_dequeue_action(), $this->get_dequeue_closure(), static::DEQUEUE_PRIORITY ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Add a deferred action hook. |
|
81
|
|
|
* |
|
82
|
|
|
* If the action has already passed, the closure will be called directly. |
|
83
|
|
|
* |
|
84
|
|
|
* @since %VERSION% |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $action Deferred action to hook to. |
|
87
|
|
|
* @param Closure $closure Closure to attach to the action. |
|
88
|
|
|
* @param int $priority Optional. Priority to use. Defaults to 10. |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function deferred_action( $action, $closure, $priority = 10 ) { |
|
91
|
|
|
if ( did_action( $action ) ) { |
|
92
|
|
|
$closure(); |
|
93
|
|
|
|
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
add_action( $action, $closure, $priority ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get the register action to use. |
|
102
|
|
|
* |
|
103
|
|
|
* @since %VERSION% |
|
104
|
|
|
* |
|
105
|
|
|
* @return string Register action to use. |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function get_register_action() { |
|
108
|
|
|
return $this->get_enqueue_action(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get the enqueue action to use. |
|
113
|
|
|
* |
|
114
|
|
|
* @since %VERSION% |
|
115
|
|
|
* |
|
116
|
|
|
* @return string Enqueue action name. |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function get_enqueue_action() { |
|
119
|
|
|
return is_admin() ? 'admin_enqueue_scripts' : 'wp_enqueue_scripts'; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get the dequeue action to use. |
|
124
|
|
|
* |
|
125
|
|
|
* @since %VERSION% |
|
126
|
|
|
* |
|
127
|
|
|
* @return string Enqueue action name. |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function get_dequeue_action() { |
|
130
|
|
|
return is_admin() ? 'admin_print_scripts' : 'wp_print_scripts'; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Normalize the source URI. |
|
135
|
|
|
* |
|
136
|
|
|
* @since %VERSION% |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $uri Source URI to normalize. |
|
139
|
|
|
* @param string $extension Default extension to use. |
|
140
|
|
|
* |
|
141
|
|
|
* @return string Normalized source URI. |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function normalize_source( $uri, $extension ) { |
|
144
|
|
|
$uri = $this->check_extension( $uri, $extension ); |
|
145
|
|
|
$path = trailingslashit( $this->get_root_dir() ) . $uri; |
|
146
|
|
|
$uri = $this->get_plugin_url( $uri ); |
|
147
|
|
|
|
|
148
|
|
|
return $this->check_for_minified_asset( $uri, $path, $extension ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Return the URI of the minified asset if it is readable and |
|
153
|
|
|
* `SCRIPT_DEBUG` is not set. |
|
154
|
|
|
* |
|
155
|
|
|
* @since %VERSION% |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $uri Source URI. |
|
158
|
|
|
* @param string $path Source path. |
|
159
|
|
|
* @param string $extension Default extension to use. |
|
160
|
|
|
* |
|
161
|
|
|
* @return string URI of the asset to use. |
|
162
|
|
|
* @throws InvalidURI When the file specified by $path isn't readable. |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function check_for_minified_asset( $uri, $path, $extension ) { |
|
165
|
|
|
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; |
|
166
|
|
|
$minified_uri = str_replace( ".$extension", ".min.{$extension}", $uri ); |
|
167
|
|
|
$minified_path = str_replace( ".$extension", ".min.{$extension}", $path ); |
|
168
|
|
|
|
|
169
|
|
|
// If both the regular and minified path aren't readable, that might mean that build scripts need to run. |
|
170
|
|
|
if ( ! is_readable( $path ) && ! is_readable( $minified_path ) ) { |
|
171
|
|
|
throw InvalidURI::from_asset_path( $path ); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
// If we're not in debug mode and we have a minified asset, or we're in a debug mode and we don't have an unminified asset but we have a minified asset, return the minified. |
|
175
|
|
|
return ! $debug && is_readable( $minified_path ) || $debug && ! is_readable( $path ) && is_readable( $minified_path ) ? $minified_uri : $uri; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Check that the URI has the correct extension. |
|
180
|
|
|
* |
|
181
|
|
|
* Optionally adds the extension if none was detected. |
|
182
|
|
|
* |
|
183
|
|
|
* @since %VERSION% |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $uri URI to check the extension of. |
|
186
|
|
|
* @param string $extension Extension to use. |
|
187
|
|
|
* |
|
188
|
|
|
* @return string URI with correct extension. |
|
189
|
|
|
*/ |
|
190
|
|
|
public function check_extension( $uri, $extension ) { |
|
191
|
|
|
$detected_extension = pathinfo( $uri, PATHINFO_EXTENSION ); |
|
192
|
|
|
|
|
193
|
|
|
if ( $extension !== $detected_extension ) { |
|
194
|
|
|
$uri .= '.' . $extension; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $uri; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Get the enqueue closure to use. |
|
202
|
|
|
* |
|
203
|
|
|
* @since %VERSION% |
|
204
|
|
|
* |
|
205
|
|
|
* @return Closure |
|
206
|
|
|
*/ |
|
207
|
|
|
abstract protected function get_register_closure(); |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Get the enqueue closure to use. |
|
211
|
|
|
* |
|
212
|
|
|
* @since %VERSION% |
|
213
|
|
|
* |
|
214
|
|
|
* @return Closure |
|
215
|
|
|
*/ |
|
216
|
|
|
abstract protected function get_enqueue_closure(); |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Get the dequeue closure to use. |
|
220
|
|
|
* |
|
221
|
|
|
* @since %VERSION% |
|
222
|
|
|
* |
|
223
|
|
|
* @return Closure |
|
224
|
|
|
*/ |
|
225
|
|
|
abstract protected function get_dequeue_closure(); |
|
226
|
|
|
} |
|
227
|
|
|
|