@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * |
51 | 51 | * @var array; |
52 | 52 | */ |
53 | - protected $dependencies = [ ]; |
|
53 | + protected $dependencies = []; |
|
54 | 54 | |
55 | 55 | /** |
56 | 56 | * Hold the handlers. |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | * |
60 | 60 | * @var DependencyHandlerInterface[] |
61 | 61 | */ |
62 | - protected $handlers = [ ]; |
|
62 | + protected $handlers = []; |
|
63 | 63 | |
64 | 64 | /** |
65 | 65 | * Whether to enqueue immediately upon registration. |
@@ -83,8 +83,8 @@ discard block |
||
83 | 83 | * @throws InvalidArgumentException If no dependency handlers were |
84 | 84 | * specified. |
85 | 85 | */ |
86 | - public function __construct( ConfigInterface $config, $enqueue = true ) { |
|
87 | - $this->processConfig( $config ); |
|
86 | + public function __construct(ConfigInterface $config, $enqueue = true) { |
|
87 | + $this->processConfig($config); |
|
88 | 88 | $this->enqueue_immediately = $enqueue; |
89 | 89 | $this->init_handlers(); |
90 | 90 | $this->init_dependencies(); |
@@ -96,10 +96,10 @@ discard block |
||
96 | 96 | * @since 0.1.0 |
97 | 97 | */ |
98 | 98 | protected function init_handlers() { |
99 | - $keys = [ self::KEY_SCRIPTS, self::KEY_STYLES ]; |
|
100 | - foreach ( $keys as $key ) { |
|
101 | - if ( $this->hasConfigKey( $key ) ) { |
|
102 | - $this->add_handler( $key ); |
|
99 | + $keys = [self::KEY_SCRIPTS, self::KEY_STYLES]; |
|
100 | + foreach ($keys as $key) { |
|
101 | + if ($this->hasConfigKey($key)) { |
|
102 | + $this->add_handler($key); |
|
103 | 103 | } |
104 | 104 | } |
105 | 105 | } |
@@ -111,13 +111,13 @@ discard block |
||
111 | 111 | * |
112 | 112 | * @param string $dependency The dependency type for which to add a handler. |
113 | 113 | */ |
114 | - protected function add_handler( $dependency ) { |
|
115 | - if ( $this->hasConfigKey( $dependency ) ) { |
|
116 | - $handler = $this->hasConfigKey( self::KEY_HANDLERS, $dependency ) |
|
117 | - ? $this->getConfigKey( self::KEY_HANDLERS, $dependency ) |
|
118 | - : $this->get_default_handler( $dependency ); |
|
119 | - if ( $handler ) { |
|
120 | - $this->handlers[ $dependency ] = new $handler; |
|
114 | + protected function add_handler($dependency) { |
|
115 | + if ($this->hasConfigKey($dependency)) { |
|
116 | + $handler = $this->hasConfigKey(self::KEY_HANDLERS, $dependency) |
|
117 | + ? $this->getConfigKey(self::KEY_HANDLERS, $dependency) |
|
118 | + : $this->get_default_handler($dependency); |
|
119 | + if ($handler) { |
|
120 | + $this->handlers[$dependency] = new $handler; |
|
121 | 121 | } |
122 | 122 | } |
123 | 123 | } |
@@ -130,8 +130,8 @@ discard block |
||
130 | 130 | * @param string $dependency The dependency that needs a handler. |
131 | 131 | * @return string|null Class name of the handler. Null if none. |
132 | 132 | */ |
133 | - protected function get_default_handler( $dependency ) { |
|
134 | - switch ( $dependency ) { |
|
133 | + protected function get_default_handler($dependency) { |
|
134 | + switch ($dependency) { |
|
135 | 135 | case self::KEY_STYLES: |
136 | 136 | return self::DEFAULT_STYLE_HANDLER; |
137 | 137 | case self::KEY_SCRIPTS: |
@@ -147,10 +147,10 @@ discard block |
||
147 | 147 | * @since 0.1.0 |
148 | 148 | */ |
149 | 149 | protected function init_dependencies() { |
150 | - array_walk( $this->handlers, |
|
151 | - function ( $handler, $dependency_type ) { |
|
152 | - if ( $this->hasConfigKey( $dependency_type ) ) { |
|
153 | - $this->dependencies[ $dependency_type ] = $this->init_dependency_type( $dependency_type ); |
|
150 | + array_walk($this->handlers, |
|
151 | + function($handler, $dependency_type) { |
|
152 | + if ($this->hasConfigKey($dependency_type)) { |
|
153 | + $this->dependencies[$dependency_type] = $this->init_dependency_type($dependency_type); |
|
154 | 154 | } |
155 | 155 | } ); |
156 | 156 | } |
@@ -163,13 +163,13 @@ discard block |
||
163 | 163 | * @param string $type The type of dependency to initialize. |
164 | 164 | * @return array Array of dependency configurations. |
165 | 165 | */ |
166 | - protected function init_dependency_type( $type ) { |
|
167 | - $array = [ ]; |
|
168 | - $data = $this->getConfigKey( $type ); |
|
169 | - foreach ( $data as $dependency ) { |
|
170 | - $handle = array_key_exists( 'handle', |
|
171 | - $dependency ) ? $dependency['handle'] : ''; |
|
172 | - $array[ $handle ] = $dependency; |
|
166 | + protected function init_dependency_type($type) { |
|
167 | + $array = []; |
|
168 | + $data = $this->getConfigKey($type); |
|
169 | + foreach ($data as $dependency) { |
|
170 | + $handle = array_key_exists('handle', |
|
171 | + $dependency) ? $dependency['handle'] : ''; |
|
172 | + $array[$handle] = $dependency; |
|
173 | 173 | } |
174 | 174 | return $array; |
175 | 175 | } |
@@ -181,10 +181,10 @@ discard block |
||
181 | 181 | * |
182 | 182 | * @param mixed $context Optional. The context to pass to the dependencies. |
183 | 183 | */ |
184 | - public function register( $context = null ) { |
|
185 | - $context = $this->validate_context( $context ); |
|
186 | - array_walk( $this->dependencies, |
|
187 | - [ $this, 'register_dependency_type' ], $context ); |
|
184 | + public function register($context = null) { |
|
185 | + $context = $this->validate_context($context); |
|
186 | + array_walk($this->dependencies, |
|
187 | + [$this, 'register_dependency_type'], $context); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | /** |
@@ -195,9 +195,9 @@ discard block |
||
195 | 195 | * @param mixed $context The context as passed in by WordPress. |
196 | 196 | * @return array Validated context. |
197 | 197 | */ |
198 | - protected function validate_context( $context ) { |
|
199 | - if ( is_string( $context ) ) { |
|
200 | - return [ 'wp_context' => $context ]; |
|
198 | + protected function validate_context($context) { |
|
199 | + if (is_string($context)) { |
|
200 | + return ['wp_context' => $context]; |
|
201 | 201 | } |
202 | 202 | return (array) $context; |
203 | 203 | } |
@@ -210,11 +210,11 @@ discard block |
||
210 | 210 | * @param mixed $context Optional. The context to pass to the |
211 | 211 | * dependencies. |
212 | 212 | */ |
213 | - public function enqueue( $context = null ) { |
|
214 | - $context = $this->validate_context( $context ); |
|
213 | + public function enqueue($context = null) { |
|
214 | + $context = $this->validate_context($context); |
|
215 | 215 | |
216 | - array_walk( $this->dependencies, |
|
217 | - [ $this, 'enqueue_dependency_type' ], $context ); |
|
216 | + array_walk($this->dependencies, |
|
217 | + [$this, 'enqueue_dependency_type'], $context); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -229,9 +229,9 @@ discard block |
||
229 | 229 | * outside of DependencyManager. Defaults to false. |
230 | 230 | * @return bool Returns whether the handle was found or not. |
231 | 231 | */ |
232 | - public function enqueue_handle( $handle, $context = null, $fallback = false ) { |
|
233 | - if ( ! $this->enqueue_internal_handle( $handle, $context ) ) { |
|
234 | - return $this->enqueue_fallback_handle( $handle ); |
|
232 | + public function enqueue_handle($handle, $context = null, $fallback = false) { |
|
233 | + if ( ! $this->enqueue_internal_handle($handle, $context)) { |
|
234 | + return $this->enqueue_fallback_handle($handle); |
|
235 | 235 | } |
236 | 236 | return true; |
237 | 237 | } |
@@ -247,11 +247,11 @@ discard block |
||
247 | 247 | * dependencies. |
248 | 248 | * @return bool Returns whether the handle was found or not. |
249 | 249 | */ |
250 | - protected function enqueue_internal_handle( $handle, $context = null ) { |
|
251 | - list( $dependency_type, $dependency ) = $this->get_dependency_array( $handle ); |
|
250 | + protected function enqueue_internal_handle($handle, $context = null) { |
|
251 | + list($dependency_type, $dependency) = $this->get_dependency_array($handle); |
|
252 | 252 | $context['dependency_type'] = $dependency_type; |
253 | 253 | |
254 | - if ( ! $dependency ) { |
|
254 | + if ( ! $dependency) { |
|
255 | 255 | return false; |
256 | 256 | } |
257 | 257 | |
@@ -261,8 +261,8 @@ discard block |
||
261 | 261 | $context |
262 | 262 | ); |
263 | 263 | |
264 | - $this->maybe_localize( $dependency, $context ); |
|
265 | - $this->maybe_add_inline_script( $dependency, $context ); |
|
264 | + $this->maybe_localize($dependency, $context); |
|
265 | + $this->maybe_add_inline_script($dependency, $context); |
|
266 | 266 | |
267 | 267 | return true; |
268 | 268 | } |
@@ -276,14 +276,14 @@ discard block |
||
276 | 276 | * @return array Array containing the dependency key as well as the |
277 | 277 | * dependency array itself. |
278 | 278 | */ |
279 | - protected function get_dependency_array( $handle ) { |
|
280 | - foreach ( $this->dependencies as $type => $dependencies ) { |
|
281 | - if ( array_key_exists( $handle, $dependencies ) ) { |
|
282 | - return [ $type, $dependencies[ $handle ] ]; |
|
279 | + protected function get_dependency_array($handle) { |
|
280 | + foreach ($this->dependencies as $type => $dependencies) { |
|
281 | + if (array_key_exists($handle, $dependencies)) { |
|
282 | + return [$type, $dependencies[$handle]]; |
|
283 | 283 | } |
284 | 284 | } |
285 | 285 | // Handle not found, return an empty array. |
286 | - return [ '', null ]; |
|
286 | + return ['', null]; |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | /** |
@@ -298,12 +298,12 @@ discard block |
||
298 | 298 | * dependency at key |
299 | 299 | * 'dependency_type'. |
300 | 300 | */ |
301 | - protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) { |
|
302 | - if ( ! $this->is_needed( $dependency, $context ) ) { |
|
301 | + protected function enqueue_dependency($dependency, $dependency_key, $context = null) { |
|
302 | + if ( ! $this->is_needed($dependency, $context)) { |
|
303 | 303 | return; |
304 | 304 | } |
305 | - $handler = $this->handlers[ $context['dependency_type'] ]; |
|
306 | - $handler->enqueue( $dependency ); |
|
305 | + $handler = $this->handlers[$context['dependency_type']]; |
|
306 | + $handler->enqueue($dependency); |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | /** |
@@ -317,16 +317,16 @@ discard block |
||
317 | 317 | * 'dependency_type'. |
318 | 318 | * @return bool Whether it is needed or not. |
319 | 319 | */ |
320 | - protected function is_needed( $dependency, $context ) { |
|
321 | - $is_needed = array_key_exists( 'is_needed', $dependency ) |
|
320 | + protected function is_needed($dependency, $context) { |
|
321 | + $is_needed = array_key_exists('is_needed', $dependency) |
|
322 | 322 | ? $dependency['is_needed'] |
323 | 323 | : null; |
324 | 324 | |
325 | - if ( null === $is_needed ) { |
|
325 | + if (null === $is_needed) { |
|
326 | 326 | return true; |
327 | 327 | } |
328 | 328 | |
329 | - return is_callable( $is_needed ) && $is_needed( $context ); |
|
329 | + return is_callable($is_needed) && $is_needed($context); |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | /** |
@@ -339,18 +339,18 @@ discard block |
||
339 | 339 | * Contains the type of the dependency at key |
340 | 340 | * 'dependency_type'. |
341 | 341 | */ |
342 | - protected function maybe_localize( $dependency, $context ) { |
|
343 | - if ( ! array_key_exists( 'localize', $dependency ) ) { |
|
342 | + protected function maybe_localize($dependency, $context) { |
|
343 | + if ( ! array_key_exists('localize', $dependency)) { |
|
344 | 344 | return; |
345 | 345 | } |
346 | 346 | |
347 | 347 | $localize = $dependency['localize']; |
348 | 348 | $data = $localize['data']; |
349 | - if ( is_callable( $data ) ) { |
|
350 | - $data = $data( $context ); |
|
349 | + if (is_callable($data)) { |
|
350 | + $data = $data($context); |
|
351 | 351 | } |
352 | 352 | |
353 | - wp_localize_script( $dependency['handle'], $localize['name'], $data ); |
|
353 | + wp_localize_script($dependency['handle'], $localize['name'], $data); |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | /** |
@@ -363,18 +363,18 @@ discard block |
||
363 | 363 | * Contains the type of the dependency at key |
364 | 364 | * 'dependency_type'. |
365 | 365 | */ |
366 | - protected function maybe_add_inline_script( $dependency, $context ) { |
|
367 | - if ( ! array_key_exists( 'add_inline', $dependency ) ) { |
|
366 | + protected function maybe_add_inline_script($dependency, $context) { |
|
367 | + if ( ! array_key_exists('add_inline', $dependency)) { |
|
368 | 368 | return; |
369 | 369 | } |
370 | 370 | |
371 | 371 | $inline_script = $dependency['add_inline']; |
372 | 372 | |
373 | - if ( is_callable( $inline_script ) ) { |
|
374 | - $inline_script = $inline_script( $context ); |
|
373 | + if (is_callable($inline_script)) { |
|
374 | + $inline_script = $inline_script($context); |
|
375 | 375 | } |
376 | 376 | |
377 | - wp_add_inline_script( $dependency['handle'], $inline_script ); |
|
377 | + wp_add_inline_script($dependency['handle'], $inline_script); |
|
378 | 378 | } |
379 | 379 | |
380 | 380 | /** |
@@ -386,10 +386,10 @@ discard block |
||
386 | 386 | * @param string $handle The dependency handle to enqueue. |
387 | 387 | * @return bool Returns whether the handle was found or not. |
388 | 388 | */ |
389 | - protected function enqueue_fallback_handle( $handle ) { |
|
389 | + protected function enqueue_fallback_handle($handle) { |
|
390 | 390 | $result = false; |
391 | - foreach ( $this->handlers as $handler ) { |
|
392 | - $result = $result || $handler->maybe_enqueue( $handle ); |
|
391 | + foreach ($this->handlers as $handler) { |
|
392 | + $result = $result || $handler->maybe_enqueue($handle); |
|
393 | 393 | } |
394 | 394 | return $result; |
395 | 395 | } |
@@ -404,9 +404,9 @@ discard block |
||
404 | 404 | * @param mixed $context Optional. The context to pass to the |
405 | 405 | * dependencies. |
406 | 406 | */ |
407 | - protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
407 | + protected function enqueue_dependency_type($dependencies, $dependency_type, $context = null) { |
|
408 | 408 | $context['dependency_type'] = $dependency_type; |
409 | - array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context ); |
|
409 | + array_walk($dependencies, [$this, 'enqueue_dependency'], $context); |
|
410 | 410 | } |
411 | 411 | |
412 | 412 | /** |
@@ -419,9 +419,9 @@ discard block |
||
419 | 419 | * @param mixed $context Optional. The context to pass to the |
420 | 420 | * dependencies. |
421 | 421 | */ |
422 | - protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
422 | + protected function register_dependency_type($dependencies, $dependency_type, $context = null) { |
|
423 | 423 | $context['dependency_type'] = $dependency_type; |
424 | - array_walk( $dependencies, [ $this, 'register_dependency' ], $context ); |
|
424 | + array_walk($dependencies, [$this, 'register_dependency'], $context); |
|
425 | 425 | } |
426 | 426 | |
427 | 427 | /** |
@@ -436,12 +436,12 @@ discard block |
||
436 | 436 | * dependency at key |
437 | 437 | * 'dependency_type'. |
438 | 438 | */ |
439 | - protected function register_dependency( $dependency, $dependency_key, $context = null ) { |
|
440 | - $handler = $this->handlers[ $context['dependency_type'] ]; |
|
441 | - $handler->register( $dependency ); |
|
439 | + protected function register_dependency($dependency, $dependency_key, $context = null) { |
|
440 | + $handler = $this->handlers[$context['dependency_type']]; |
|
441 | + $handler->register($dependency); |
|
442 | 442 | |
443 | - if ( $this->enqueue_immediately ) { |
|
444 | - $this->register_enqueue_hooks( $dependency, $context ); |
|
443 | + if ($this->enqueue_immediately) { |
|
444 | + $this->register_enqueue_hooks($dependency, $context); |
|
445 | 445 | } |
446 | 446 | } |
447 | 447 | |
@@ -455,15 +455,15 @@ discard block |
||
455 | 455 | * Contains the type of the dependency at key |
456 | 456 | * 'dependency_type'. |
457 | 457 | */ |
458 | - protected function register_enqueue_hooks( $dependency, $context = null ) { |
|
459 | - $priority = $this->get_priority( $dependency ); |
|
458 | + protected function register_enqueue_hooks($dependency, $context = null) { |
|
459 | + $priority = $this->get_priority($dependency); |
|
460 | 460 | |
461 | - foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) { |
|
462 | - add_action( $hook, [ $this, 'enqueue' ], $priority, 1 ); |
|
461 | + foreach (['wp_enqueue_scripts', 'admin_enqueue_scripts'] as $hook) { |
|
462 | + add_action($hook, [$this, 'enqueue'], $priority, 1); |
|
463 | 463 | } |
464 | 464 | |
465 | - $this->maybe_localize( $dependency, $context ); |
|
466 | - $this->maybe_add_inline_script( $dependency, $context ); |
|
465 | + $this->maybe_localize($dependency, $context); |
|
466 | + $this->maybe_add_inline_script($dependency, $context); |
|
467 | 467 | } |
468 | 468 | |
469 | 469 | /** |
@@ -474,9 +474,9 @@ discard block |
||
474 | 474 | * @param array $dependency Configuration data of the dependency. |
475 | 475 | * @return int Priority to use. |
476 | 476 | */ |
477 | - protected function get_priority( $dependency ) { |
|
478 | - if ( array_key_exists( 'priority', $dependency ) ) { |
|
479 | - return intval( $dependency['priority'] ); |
|
477 | + protected function get_priority($dependency) { |
|
478 | + if (array_key_exists('priority', $dependency)) { |
|
479 | + return intval($dependency['priority']); |
|
480 | 480 | } |
481 | 481 | return 10; |
482 | 482 | } |