@@ -30,6 +30,7 @@ |
||
| 30 | 30 | * @since 0.2.3 |
| 31 | 31 | * |
| 32 | 32 | * @param string $handle Handle of the dependency to enqueue. |
| 33 | + * @return void |
|
| 33 | 34 | */ |
| 34 | 35 | public function maybe_enqueue( $handle ); |
| 35 | 36 | } |
@@ -31,5 +31,5 @@ |
||
| 31 | 31 | * |
| 32 | 32 | * @param string $handle Handle of the dependency to enqueue. |
| 33 | 33 | */ |
| 34 | - public function maybe_enqueue( $handle ); |
|
| 34 | + public function maybe_enqueue($handle); |
|
| 35 | 35 | } |
@@ -35,8 +35,8 @@ discard block |
||
| 35 | 35 | * @throws InvalidArgumentException If the register function could not be |
| 36 | 36 | * called. |
| 37 | 37 | */ |
| 38 | - public function register( $args = null ) { |
|
| 39 | - $this->invokeFunction( $this->get_register_function(), $args ); |
|
| 38 | + public function register($args = null) { |
|
| 39 | + $this->invokeFunction($this->get_register_function(), $args); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -58,8 +58,8 @@ discard block |
||
| 58 | 58 | * @throws InvalidArgumentException If the register function could not be |
| 59 | 59 | * called. |
| 60 | 60 | */ |
| 61 | - public function enqueue( $args = null ) { |
|
| 62 | - $this->invokeFunction( $this->get_enqueue_function(), $args ); |
|
| 61 | + public function enqueue($args = null) { |
|
| 62 | + $this->invokeFunction($this->get_enqueue_function(), $args); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -79,10 +79,10 @@ discard block |
||
| 79 | 79 | * |
| 80 | 80 | * @param string $handle Handle of the dependency to enqueue. |
| 81 | 81 | */ |
| 82 | - public function maybe_enqueue( $handle ) { |
|
| 83 | - if ( $this->is_registered( $handle ) ) { |
|
| 82 | + public function maybe_enqueue($handle) { |
|
| 83 | + if ($this->is_registered($handle)) { |
|
| 84 | 84 | $enqueue = $this->get_enqueue_function(); |
| 85 | - $enqueue( $handle ); |
|
| 85 | + $enqueue($handle); |
|
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | |
@@ -94,5 +94,5 @@ discard block |
||
| 94 | 94 | * @param string $handle The handle to check |
| 95 | 95 | * @return bool Whether it is registered or not. |
| 96 | 96 | */ |
| 97 | - abstract protected function is_registered( $handle ); |
|
| 97 | + abstract protected function is_registered($handle); |
|
| 98 | 98 | } |
@@ -1,13 +1,13 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * DependencyManager Class. |
|
| 4 | - * |
|
| 5 | - * @package BrightNucleus\Dependency |
|
| 6 | - * @author Alain Schlesser <[email protected]> |
|
| 7 | - * @license GPL-2.0+ |
|
| 8 | - * @link http://www.brightnucleus.com/ |
|
| 9 | - * @copyright 2015 Alain Schlesser, Bright NucleusInterface |
|
| 10 | - */ |
|
| 3 | + * DependencyManager Class. |
|
| 4 | + * |
|
| 5 | + * @package BrightNucleus\Dependency |
|
| 6 | + * @author Alain Schlesser <[email protected]> |
|
| 7 | + * @license GPL-2.0+ |
|
| 8 | + * @link http://www.brightnucleus.com/ |
|
| 9 | + * @copyright 2015 Alain Schlesser, Bright NucleusInterface |
|
| 10 | + */ |
|
| 11 | 11 | |
| 12 | 12 | namespace BrightNucleus\Dependency; |
| 13 | 13 | |
@@ -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->getConfigKey( $dependency_type ); |
|
| 148 | + array_walk($this->handlers, |
|
| 149 | + function($handler, $dependency_type) { |
|
| 150 | + if ($this->hasConfigKey($dependency_type)) { |
|
| 151 | + $this->dependencies[$dependency_type] = $this->getConfigKey($dependency_type); |
|
| 152 | 152 | } |
| 153 | 153 | } ); |
| 154 | 154 | } |
@@ -160,10 +160,10 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @param mixed $context Optional. The context to pass to the dependencies. |
| 162 | 162 | */ |
| 163 | - public function register( $context = null ) { |
|
| 164 | - $context = $this->validate_context( $context ); |
|
| 165 | - array_walk( $this->dependencies, |
|
| 166 | - [ $this, 'register_dependency_type' ], $context ); |
|
| 163 | + public function register($context = null) { |
|
| 164 | + $context = $this->validate_context($context); |
|
| 165 | + array_walk($this->dependencies, |
|
| 166 | + [$this, 'register_dependency_type'], $context); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
@@ -174,9 +174,9 @@ discard block |
||
| 174 | 174 | * @param mixed $context The context as passed in by WordPress. |
| 175 | 175 | * @return array Validated context. |
| 176 | 176 | */ |
| 177 | - protected function validate_context( $context ) { |
|
| 178 | - if ( is_string( $context ) ) { |
|
| 179 | - return [ 'wp_context' => $context ]; |
|
| 177 | + protected function validate_context($context) { |
|
| 178 | + if (is_string($context)) { |
|
| 179 | + return ['wp_context' => $context]; |
|
| 180 | 180 | } |
| 181 | 181 | return (array) $context; |
| 182 | 182 | } |
@@ -189,11 +189,11 @@ discard block |
||
| 189 | 189 | * @param mixed $context Optional. The context to pass to the |
| 190 | 190 | * dependencies. |
| 191 | 191 | */ |
| 192 | - public function enqueue( $context = null ) { |
|
| 193 | - $context = $this->validate_context( $context ); |
|
| 192 | + public function enqueue($context = null) { |
|
| 193 | + $context = $this->validate_context($context); |
|
| 194 | 194 | |
| 195 | - array_walk( $this->dependencies, |
|
| 196 | - [ $this, 'enqueue_dependency_type' ], $context ); |
|
| 195 | + array_walk($this->dependencies, |
|
| 196 | + [$this, 'enqueue_dependency_type'], $context); |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | /** |
@@ -208,9 +208,9 @@ discard block |
||
| 208 | 208 | * outside of DependencyManager. Defaults to false. |
| 209 | 209 | * @return bool Returns whether the handle was found or not. |
| 210 | 210 | */ |
| 211 | - public function enqueue_handle( $handle, $context = null, $fallback = false ) { |
|
| 212 | - list( $dependency_key, $dependency ) = $this->get_dependency_array( $handle ); |
|
| 213 | - if ( $dependency ) { |
|
| 211 | + public function enqueue_handle($handle, $context = null, $fallback = false) { |
|
| 212 | + list($dependency_key, $dependency) = $this->get_dependency_array($handle); |
|
| 213 | + if ($dependency) { |
|
| 214 | 214 | |
| 215 | 215 | $this->enqueue_dependency( |
| 216 | 216 | $dependency, |
@@ -218,14 +218,14 @@ discard block |
||
| 218 | 218 | $context |
| 219 | 219 | ); |
| 220 | 220 | |
| 221 | - $this->maybe_localize( $dependency, $context ); |
|
| 221 | + $this->maybe_localize($dependency, $context); |
|
| 222 | 222 | |
| 223 | 223 | return true; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - if ( $fallback ) { |
|
| 227 | - foreach ( $this->handlers as $handler ) { |
|
| 228 | - $handler->maybe_enqueue( $handle ); |
|
| 226 | + if ($fallback) { |
|
| 227 | + foreach ($this->handlers as $handler) { |
|
| 228 | + $handler->maybe_enqueue($handle); |
|
| 229 | 229 | } |
| 230 | 230 | } |
| 231 | 231 | return false; |
@@ -240,15 +240,15 @@ discard block |
||
| 240 | 240 | * @return array Array containing the dependency key as well as the |
| 241 | 241 | * dependency array itself. |
| 242 | 242 | */ |
| 243 | - protected function get_dependency_array( $handle ) { |
|
| 244 | - foreach ( $this->dependencies as $dependency_type ) { |
|
| 245 | - $dependency_key = array_search( $handle, $dependency_type, true ); |
|
| 246 | - if ( $dependency_key ) { |
|
| 247 | - return [ $dependency_key, $dependency_type[ $dependency_key ] ]; |
|
| 243 | + protected function get_dependency_array($handle) { |
|
| 244 | + foreach ($this->dependencies as $dependency_type) { |
|
| 245 | + $dependency_key = array_search($handle, $dependency_type, true); |
|
| 246 | + if ($dependency_key) { |
|
| 247 | + return [$dependency_key, $dependency_type[$dependency_key]]; |
|
| 248 | 248 | } |
| 249 | 249 | } |
| 250 | 250 | // Handle not found, return an empty array. |
| 251 | - return [ '', null ]; |
|
| 251 | + return ['', null]; |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | /** |
@@ -263,12 +263,12 @@ discard block |
||
| 263 | 263 | * dependency at key |
| 264 | 264 | * 'dependency_type'. |
| 265 | 265 | */ |
| 266 | - protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) { |
|
| 267 | - if ( ! $this->is_needed( $dependency, $context ) ) { |
|
| 266 | + protected function enqueue_dependency($dependency, $dependency_key, $context = null) { |
|
| 267 | + if ( ! $this->is_needed($dependency, $context)) { |
|
| 268 | 268 | return; |
| 269 | 269 | } |
| 270 | - $handler = $this->handlers[ $context['dependency_type'] ]; |
|
| 271 | - $handler->enqueue( $dependency ); |
|
| 270 | + $handler = $this->handlers[$context['dependency_type']]; |
|
| 271 | + $handler->enqueue($dependency); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -282,16 +282,16 @@ discard block |
||
| 282 | 282 | * 'dependency_type'. |
| 283 | 283 | * @return bool Whether it is needed or not. |
| 284 | 284 | */ |
| 285 | - protected function is_needed( $dependency, $context ) { |
|
| 286 | - $is_needed = array_key_exists( 'is_needed', $dependency ) |
|
| 285 | + protected function is_needed($dependency, $context) { |
|
| 286 | + $is_needed = array_key_exists('is_needed', $dependency) |
|
| 287 | 287 | ? $dependency['is_needed'] |
| 288 | 288 | : null; |
| 289 | 289 | |
| 290 | - if ( null === $is_needed ) { |
|
| 290 | + if (null === $is_needed) { |
|
| 291 | 291 | return true; |
| 292 | 292 | } |
| 293 | 293 | |
| 294 | - return is_callable( $is_needed ) && $is_needed( $context ); |
|
| 294 | + return is_callable($is_needed) && $is_needed($context); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
@@ -304,18 +304,18 @@ discard block |
||
| 304 | 304 | * Contains the type of the dependency at key |
| 305 | 305 | * 'dependency_type'. |
| 306 | 306 | */ |
| 307 | - protected function maybe_localize( $dependency, $context ) { |
|
| 308 | - if ( ! array_key_exists( 'localize', $dependency ) ) { |
|
| 307 | + protected function maybe_localize($dependency, $context) { |
|
| 308 | + if ( ! array_key_exists('localize', $dependency)) { |
|
| 309 | 309 | return; |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | $localize = $dependency['localize']; |
| 313 | 313 | $data = $localize['data']; |
| 314 | - if ( is_callable( $data ) ) { |
|
| 315 | - $data = $data( $context ); |
|
| 314 | + if (is_callable($data)) { |
|
| 315 | + $data = $data($context); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | - \wp_localize_script( $dependency['handle'], $localize['name'], $data ); |
|
| 318 | + \wp_localize_script($dependency['handle'], $localize['name'], $data); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | /** |
@@ -328,9 +328,9 @@ discard block |
||
| 328 | 328 | * @param mixed $context Optional. The context to pass to the |
| 329 | 329 | * dependencies. |
| 330 | 330 | */ |
| 331 | - protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
| 331 | + protected function enqueue_dependency_type($dependencies, $dependency_type, $context = null) { |
|
| 332 | 332 | $context['dependency_type'] = $dependency_type; |
| 333 | - array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context ); |
|
| 333 | + array_walk($dependencies, [$this, 'enqueue_dependency'], $context); |
|
| 334 | 334 | } |
| 335 | 335 | |
| 336 | 336 | /** |
@@ -343,9 +343,9 @@ discard block |
||
| 343 | 343 | * @param mixed $context Optional. The context to pass to the |
| 344 | 344 | * dependencies. |
| 345 | 345 | */ |
| 346 | - protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
| 346 | + protected function register_dependency_type($dependencies, $dependency_type, $context = null) { |
|
| 347 | 347 | $context['dependency_type'] = $dependency_type; |
| 348 | - array_walk( $dependencies, [ $this, 'register_dependency' ], $context ); |
|
| 348 | + array_walk($dependencies, [$this, 'register_dependency'], $context); |
|
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | /** |
@@ -360,12 +360,12 @@ discard block |
||
| 360 | 360 | * dependency at key |
| 361 | 361 | * 'dependency_type'. |
| 362 | 362 | */ |
| 363 | - protected function register_dependency( $dependency, $dependency_key, $context = null ) { |
|
| 364 | - $handler = $this->handlers[ $context['dependency_type'] ]; |
|
| 365 | - $handler->register( $dependency ); |
|
| 363 | + protected function register_dependency($dependency, $dependency_key, $context = null) { |
|
| 364 | + $handler = $this->handlers[$context['dependency_type']]; |
|
| 365 | + $handler->register($dependency); |
|
| 366 | 366 | |
| 367 | - if ( $this->enqueue_immediately ) { |
|
| 368 | - $this->register_enqueue_hooks( $dependency, $context ); |
|
| 367 | + if ($this->enqueue_immediately) { |
|
| 368 | + $this->register_enqueue_hooks($dependency, $context); |
|
| 369 | 369 | } |
| 370 | 370 | } |
| 371 | 371 | |
@@ -379,14 +379,14 @@ discard block |
||
| 379 | 379 | * Contains the type of the dependency at key |
| 380 | 380 | * 'dependency_type'. |
| 381 | 381 | */ |
| 382 | - protected function register_enqueue_hooks( $dependency, $context = null ) { |
|
| 383 | - $priority = $this->get_priority( $dependency ); |
|
| 382 | + protected function register_enqueue_hooks($dependency, $context = null) { |
|
| 383 | + $priority = $this->get_priority($dependency); |
|
| 384 | 384 | |
| 385 | - foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) { |
|
| 386 | - \add_action( $hook, [ $this, 'enqueue' ], $priority, 1 ); |
|
| 385 | + foreach (['wp_enqueue_scripts', 'admin_enqueue_scripts'] as $hook) { |
|
| 386 | + \add_action($hook, [$this, 'enqueue'], $priority, 1); |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | - $this->maybe_localize( $dependency, $context ); |
|
| 389 | + $this->maybe_localize($dependency, $context); |
|
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | /** |
@@ -397,9 +397,9 @@ discard block |
||
| 397 | 397 | * @param array $dependency Configuration data of the dependency. |
| 398 | 398 | * @return int Priority to use. |
| 399 | 399 | */ |
| 400 | - protected function get_priority( $dependency ) { |
|
| 401 | - if ( array_key_exists( 'priority', $dependency ) ) { |
|
| 402 | - return intval( $dependency['priority'] ); |
|
| 400 | + protected function get_priority($dependency) { |
|
| 401 | + if (array_key_exists('priority', $dependency)) { |
|
| 402 | + return intval($dependency['priority']); |
|
| 403 | 403 | } |
| 404 | 404 | return 10; |
| 405 | 405 | } |
@@ -36,5 +36,5 @@ |
||
| 36 | 36 | * outside of DependencyManager. Defaults to false. |
| 37 | 37 | * @return bool Returns whether the handle was found or not. |
| 38 | 38 | */ |
| 39 | - public function enqueue_handle( $handle, $context = null, $fallback = false ); |
|
| 39 | + public function enqueue_handle($handle, $context = null, $fallback = false); |
|
| 40 | 40 | } |
@@ -50,7 +50,7 @@ |
||
| 50 | 50 | * @param string $handle The handle to check |
| 51 | 51 | * @return bool Whether it is registered or not. |
| 52 | 52 | */ |
| 53 | - protected function is_registered( $handle ) { |
|
| 54 | - return \wp_script_is( $handle ); |
|
| 53 | + protected function is_registered($handle) { |
|
| 54 | + return \wp_script_is($handle); |
|
| 55 | 55 | } |
| 56 | 56 | } |
@@ -39,8 +39,8 @@ |
||
| 39 | 39 | * @param string $handle The handle to check |
| 40 | 40 | * @return bool Whether it is registered or not. |
| 41 | 41 | */ |
| 42 | - protected function is_registered( $handle ) { |
|
| 43 | - return \wp_style_is( $handle ); |
|
| 42 | + protected function is_registered($handle) { |
|
| 43 | + return \wp_style_is($handle); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |