@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | * |
49 | 49 | * @var array; |
50 | 50 | */ |
51 | - protected $dependencies = [ ]; |
|
51 | + protected $dependencies = []; |
|
52 | 52 | |
53 | 53 | /** |
54 | 54 | * Hold the handlers. |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | * |
58 | 58 | * @var DependencyHandlerInterface[] |
59 | 59 | */ |
60 | - protected $handlers = [ ]; |
|
60 | + protected $handlers = []; |
|
61 | 61 | |
62 | 62 | /** |
63 | 63 | * Whether to enqueue immediately upon registration. |
@@ -81,8 +81,8 @@ discard block |
||
81 | 81 | * @throws InvalidArgumentException If no dependency handlers were |
82 | 82 | * specified. |
83 | 83 | */ |
84 | - public function __construct( ConfigInterface $config, $enqueue = true ) { |
|
85 | - $this->processConfig( $config ); |
|
84 | + public function __construct(ConfigInterface $config, $enqueue = true) { |
|
85 | + $this->processConfig($config); |
|
86 | 86 | $this->enqueue_immediately = $enqueue; |
87 | 87 | $this->init_handlers(); |
88 | 88 | $this->init_dependencies(); |
@@ -94,10 +94,10 @@ discard block |
||
94 | 94 | * @since 0.1.0 |
95 | 95 | */ |
96 | 96 | protected function init_handlers() { |
97 | - $keys = [ self::KEY_SCRIPTS, self::KEY_STYLES ]; |
|
98 | - foreach ( $keys as $key ) { |
|
99 | - if ( $this->hasConfigKey( $key ) ) { |
|
100 | - $this->add_handler( $key ); |
|
97 | + $keys = [self::KEY_SCRIPTS, self::KEY_STYLES]; |
|
98 | + foreach ($keys as $key) { |
|
99 | + if ($this->hasConfigKey($key)) { |
|
100 | + $this->add_handler($key); |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 | } |
@@ -109,13 +109,13 @@ discard block |
||
109 | 109 | * |
110 | 110 | * @param string $dependency The dependency type for which to add a handler. |
111 | 111 | */ |
112 | - protected function add_handler( $dependency ) { |
|
113 | - if ( $this->hasConfigKey( $dependency ) ) { |
|
114 | - $handler = $this->hasConfigKey( self::KEY_HANDLERS, $dependency ) |
|
115 | - ? $this->getConfigKey( self::KEY_HANDLERS, $dependency ) |
|
116 | - : $this->get_default_handler( $dependency ); |
|
117 | - if ( $handler ) { |
|
118 | - $this->handlers[ $dependency ] = new $handler; |
|
112 | + protected function add_handler($dependency) { |
|
113 | + if ($this->hasConfigKey($dependency)) { |
|
114 | + $handler = $this->hasConfigKey(self::KEY_HANDLERS, $dependency) |
|
115 | + ? $this->getConfigKey(self::KEY_HANDLERS, $dependency) |
|
116 | + : $this->get_default_handler($dependency); |
|
117 | + if ($handler) { |
|
118 | + $this->handlers[$dependency] = new $handler; |
|
119 | 119 | } |
120 | 120 | } |
121 | 121 | } |
@@ -128,8 +128,8 @@ discard block |
||
128 | 128 | * @param string $dependency The dependency that needs a handler. |
129 | 129 | * @return string|null Class name of the handler. Null if none. |
130 | 130 | */ |
131 | - protected function get_default_handler( $dependency ) { |
|
132 | - switch ( $dependency ) { |
|
131 | + protected function get_default_handler($dependency) { |
|
132 | + switch ($dependency) { |
|
133 | 133 | case self::KEY_STYLES: |
134 | 134 | return self::DEFAULT_STYLE_HANDLER; |
135 | 135 | case self::KEY_SCRIPTS: |
@@ -145,10 +145,10 @@ discard block |
||
145 | 145 | * @since 0.1.0 |
146 | 146 | */ |
147 | 147 | protected function init_dependencies() { |
148 | - array_walk( $this->handlers, |
|
149 | - function ( $handler, $dependency_type ) { |
|
150 | - if ( $this->hasConfigKey( $dependency_type ) ) { |
|
151 | - $this->dependencies[ $dependency_type ] = $this->init_dependency_type( $dependency_type ); |
|
148 | + array_walk($this->handlers, |
|
149 | + function($handler, $dependency_type) { |
|
150 | + if ($this->hasConfigKey($dependency_type)) { |
|
151 | + $this->dependencies[$dependency_type] = $this->init_dependency_type($dependency_type); |
|
152 | 152 | } |
153 | 153 | } ); |
154 | 154 | } |
@@ -161,13 +161,13 @@ discard block |
||
161 | 161 | * @param string $type The type of dependency to initialize. |
162 | 162 | * @return array Array of dependency configurations. |
163 | 163 | */ |
164 | - protected function init_dependency_type( $type ) { |
|
165 | - $array = [ ]; |
|
166 | - $data = $this->getConfigKey( $type ); |
|
167 | - foreach ( $data as $dependency ) { |
|
168 | - $handle = array_key_exists( 'handle', |
|
169 | - $dependency ) ? $dependency['handle'] : ''; |
|
170 | - $array[ $handle ] = $dependency; |
|
164 | + protected function init_dependency_type($type) { |
|
165 | + $array = []; |
|
166 | + $data = $this->getConfigKey($type); |
|
167 | + foreach ($data as $dependency) { |
|
168 | + $handle = array_key_exists('handle', |
|
169 | + $dependency) ? $dependency['handle'] : ''; |
|
170 | + $array[$handle] = $dependency; |
|
171 | 171 | } |
172 | 172 | return $array; |
173 | 173 | } |
@@ -179,10 +179,10 @@ discard block |
||
179 | 179 | * |
180 | 180 | * @param mixed $context Optional. The context to pass to the dependencies. |
181 | 181 | */ |
182 | - public function register( $context = null ) { |
|
183 | - $context = $this->validate_context( $context ); |
|
184 | - array_walk( $this->dependencies, |
|
185 | - [ $this, 'register_dependency_type' ], $context ); |
|
182 | + public function register($context = null) { |
|
183 | + $context = $this->validate_context($context); |
|
184 | + array_walk($this->dependencies, |
|
185 | + [$this, 'register_dependency_type'], $context); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | /** |
@@ -193,9 +193,9 @@ discard block |
||
193 | 193 | * @param mixed $context The context as passed in by WordPress. |
194 | 194 | * @return array Validated context. |
195 | 195 | */ |
196 | - protected function validate_context( $context ) { |
|
197 | - if ( is_string( $context ) ) { |
|
198 | - return [ 'wp_context' => $context ]; |
|
196 | + protected function validate_context($context) { |
|
197 | + if (is_string($context)) { |
|
198 | + return ['wp_context' => $context]; |
|
199 | 199 | } |
200 | 200 | return (array) $context; |
201 | 201 | } |
@@ -208,11 +208,11 @@ discard block |
||
208 | 208 | * @param mixed $context Optional. The context to pass to the |
209 | 209 | * dependencies. |
210 | 210 | */ |
211 | - public function enqueue( $context = null ) { |
|
212 | - $context = $this->validate_context( $context ); |
|
211 | + public function enqueue($context = null) { |
|
212 | + $context = $this->validate_context($context); |
|
213 | 213 | |
214 | - array_walk( $this->dependencies, |
|
215 | - [ $this, 'enqueue_dependency_type' ], $context ); |
|
214 | + array_walk($this->dependencies, |
|
215 | + [$this, 'enqueue_dependency_type'], $context); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
@@ -227,10 +227,10 @@ discard block |
||
227 | 227 | * outside of DependencyManager. Defaults to false. |
228 | 228 | * @return bool Returns whether the handle was found or not. |
229 | 229 | */ |
230 | - public function enqueue_handle( $handle, $context = null, $fallback = false ) { |
|
231 | - list( $dependency_type, $dependency ) = $this->get_dependency_array( $handle ); |
|
230 | + public function enqueue_handle($handle, $context = null, $fallback = false) { |
|
231 | + list($dependency_type, $dependency) = $this->get_dependency_array($handle); |
|
232 | 232 | $context['dependency_type'] = $dependency_type; |
233 | - if ( $dependency ) { |
|
233 | + if ($dependency) { |
|
234 | 234 | |
235 | 235 | $this->enqueue_dependency( |
236 | 236 | $dependency, |
@@ -238,16 +238,16 @@ discard block |
||
238 | 238 | $context |
239 | 239 | ); |
240 | 240 | |
241 | - $this->maybe_localize( $dependency, $context ); |
|
241 | + $this->maybe_localize($dependency, $context); |
|
242 | 242 | |
243 | 243 | return true; |
244 | 244 | } |
245 | 245 | |
246 | 246 | $result = false; |
247 | 247 | |
248 | - if ( $fallback ) { |
|
249 | - foreach ( $this->handlers as $handler ) { |
|
250 | - $result = $result || $handler->maybe_enqueue( $handle ); |
|
248 | + if ($fallback) { |
|
249 | + foreach ($this->handlers as $handler) { |
|
250 | + $result = $result || $handler->maybe_enqueue($handle); |
|
251 | 251 | } |
252 | 252 | } |
253 | 253 | return $result; |
@@ -262,14 +262,14 @@ discard block |
||
262 | 262 | * @return array Array containing the dependency key as well as the |
263 | 263 | * dependency array itself. |
264 | 264 | */ |
265 | - protected function get_dependency_array( $handle ) { |
|
266 | - foreach ( $this->dependencies as $type => $dependencies ) { |
|
267 | - if ( array_key_exists( $handle, $dependencies ) ) { |
|
268 | - return [ $type, $dependencies[ $handle ] ]; |
|
265 | + protected function get_dependency_array($handle) { |
|
266 | + foreach ($this->dependencies as $type => $dependencies) { |
|
267 | + if (array_key_exists($handle, $dependencies)) { |
|
268 | + return [$type, $dependencies[$handle]]; |
|
269 | 269 | } |
270 | 270 | } |
271 | 271 | // Handle not found, return an empty array. |
272 | - return [ '', null ]; |
|
272 | + return ['', null]; |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | /** |
@@ -284,12 +284,12 @@ discard block |
||
284 | 284 | * dependency at key |
285 | 285 | * 'dependency_type'. |
286 | 286 | */ |
287 | - protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) { |
|
288 | - if ( ! $this->is_needed( $dependency, $context ) ) { |
|
287 | + protected function enqueue_dependency($dependency, $dependency_key, $context = null) { |
|
288 | + if ( ! $this->is_needed($dependency, $context)) { |
|
289 | 289 | return; |
290 | 290 | } |
291 | - $handler = $this->handlers[ $context['dependency_type'] ]; |
|
292 | - $handler->enqueue( $dependency ); |
|
291 | + $handler = $this->handlers[$context['dependency_type']]; |
|
292 | + $handler->enqueue($dependency); |
|
293 | 293 | } |
294 | 294 | |
295 | 295 | /** |
@@ -303,16 +303,16 @@ discard block |
||
303 | 303 | * 'dependency_type'. |
304 | 304 | * @return bool Whether it is needed or not. |
305 | 305 | */ |
306 | - protected function is_needed( $dependency, $context ) { |
|
307 | - $is_needed = array_key_exists( 'is_needed', $dependency ) |
|
306 | + protected function is_needed($dependency, $context) { |
|
307 | + $is_needed = array_key_exists('is_needed', $dependency) |
|
308 | 308 | ? $dependency['is_needed'] |
309 | 309 | : null; |
310 | 310 | |
311 | - if ( null === $is_needed ) { |
|
311 | + if (null === $is_needed) { |
|
312 | 312 | return true; |
313 | 313 | } |
314 | 314 | |
315 | - return is_callable( $is_needed ) && $is_needed( $context ); |
|
315 | + return is_callable($is_needed) && $is_needed($context); |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | /** |
@@ -325,18 +325,18 @@ discard block |
||
325 | 325 | * Contains the type of the dependency at key |
326 | 326 | * 'dependency_type'. |
327 | 327 | */ |
328 | - protected function maybe_localize( $dependency, $context ) { |
|
329 | - if ( ! array_key_exists( 'localize', $dependency ) ) { |
|
328 | + protected function maybe_localize($dependency, $context) { |
|
329 | + if ( ! array_key_exists('localize', $dependency)) { |
|
330 | 330 | return; |
331 | 331 | } |
332 | 332 | |
333 | 333 | $localize = $dependency['localize']; |
334 | 334 | $data = $localize['data']; |
335 | - if ( is_callable( $data ) ) { |
|
336 | - $data = $data( $context ); |
|
335 | + if (is_callable($data)) { |
|
336 | + $data = $data($context); |
|
337 | 337 | } |
338 | 338 | |
339 | - \wp_localize_script( $dependency['handle'], $localize['name'], $data ); |
|
339 | + \wp_localize_script($dependency['handle'], $localize['name'], $data); |
|
340 | 340 | } |
341 | 341 | |
342 | 342 | /** |
@@ -349,9 +349,9 @@ discard block |
||
349 | 349 | * @param mixed $context Optional. The context to pass to the |
350 | 350 | * dependencies. |
351 | 351 | */ |
352 | - protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
352 | + protected function enqueue_dependency_type($dependencies, $dependency_type, $context = null) { |
|
353 | 353 | $context['dependency_type'] = $dependency_type; |
354 | - array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context ); |
|
354 | + array_walk($dependencies, [$this, 'enqueue_dependency'], $context); |
|
355 | 355 | } |
356 | 356 | |
357 | 357 | /** |
@@ -364,9 +364,9 @@ discard block |
||
364 | 364 | * @param mixed $context Optional. The context to pass to the |
365 | 365 | * dependencies. |
366 | 366 | */ |
367 | - protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
367 | + protected function register_dependency_type($dependencies, $dependency_type, $context = null) { |
|
368 | 368 | $context['dependency_type'] = $dependency_type; |
369 | - array_walk( $dependencies, [ $this, 'register_dependency' ], $context ); |
|
369 | + array_walk($dependencies, [$this, 'register_dependency'], $context); |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | /** |
@@ -381,12 +381,12 @@ discard block |
||
381 | 381 | * dependency at key |
382 | 382 | * 'dependency_type'. |
383 | 383 | */ |
384 | - protected function register_dependency( $dependency, $dependency_key, $context = null ) { |
|
385 | - $handler = $this->handlers[ $context['dependency_type'] ]; |
|
386 | - $handler->register( $dependency ); |
|
384 | + protected function register_dependency($dependency, $dependency_key, $context = null) { |
|
385 | + $handler = $this->handlers[$context['dependency_type']]; |
|
386 | + $handler->register($dependency); |
|
387 | 387 | |
388 | - if ( $this->enqueue_immediately ) { |
|
389 | - $this->register_enqueue_hooks( $dependency, $context ); |
|
388 | + if ($this->enqueue_immediately) { |
|
389 | + $this->register_enqueue_hooks($dependency, $context); |
|
390 | 390 | } |
391 | 391 | } |
392 | 392 | |
@@ -400,14 +400,14 @@ discard block |
||
400 | 400 | * Contains the type of the dependency at key |
401 | 401 | * 'dependency_type'. |
402 | 402 | */ |
403 | - protected function register_enqueue_hooks( $dependency, $context = null ) { |
|
404 | - $priority = $this->get_priority( $dependency ); |
|
403 | + protected function register_enqueue_hooks($dependency, $context = null) { |
|
404 | + $priority = $this->get_priority($dependency); |
|
405 | 405 | |
406 | - foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) { |
|
407 | - \add_action( $hook, [ $this, 'enqueue' ], $priority, 1 ); |
|
406 | + foreach (['wp_enqueue_scripts', 'admin_enqueue_scripts'] as $hook) { |
|
407 | + \add_action($hook, [$this, 'enqueue'], $priority, 1); |
|
408 | 408 | } |
409 | 409 | |
410 | - $this->maybe_localize( $dependency, $context ); |
|
410 | + $this->maybe_localize($dependency, $context); |
|
411 | 411 | } |
412 | 412 | |
413 | 413 | /** |
@@ -418,9 +418,9 @@ discard block |
||
418 | 418 | * @param array $dependency Configuration data of the dependency. |
419 | 419 | * @return int Priority to use. |
420 | 420 | */ |
421 | - protected function get_priority( $dependency ) { |
|
422 | - if ( array_key_exists( 'priority', $dependency ) ) { |
|
423 | - return intval( $dependency['priority'] ); |
|
421 | + protected function get_priority($dependency) { |
|
422 | + if (array_key_exists('priority', $dependency)) { |
|
423 | + return intval($dependency['priority']); |
|
424 | 424 | } |
425 | 425 | return 10; |
426 | 426 | } |