@@ -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,14 +238,14 @@ 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 | - if ( $fallback ) { |
|
| 247 | - foreach ( $this->handlers as $handler ) { |
|
| 248 | - $handler->maybe_enqueue( $handle ); |
|
| 246 | + if ($fallback) { |
|
| 247 | + foreach ($this->handlers as $handler) { |
|
| 248 | + $handler->maybe_enqueue($handle); |
|
| 249 | 249 | } |
| 250 | 250 | } |
| 251 | 251 | return false; |
@@ -260,14 +260,14 @@ discard block |
||
| 260 | 260 | * @return array Array containing the dependency key as well as the |
| 261 | 261 | * dependency array itself. |
| 262 | 262 | */ |
| 263 | - protected function get_dependency_array( $handle ) { |
|
| 264 | - foreach ( $this->dependencies as $type => $dependencies ) { |
|
| 265 | - if ( array_key_exists( $handle, $dependencies ) ) { |
|
| 266 | - return [ $type, $dependencies[ $handle ] ]; |
|
| 263 | + protected function get_dependency_array($handle) { |
|
| 264 | + foreach ($this->dependencies as $type => $dependencies) { |
|
| 265 | + if (array_key_exists($handle, $dependencies)) { |
|
| 266 | + return [$type, $dependencies[$handle]]; |
|
| 267 | 267 | } |
| 268 | 268 | } |
| 269 | 269 | // Handle not found, return an empty array. |
| 270 | - return [ '', null ]; |
|
| 270 | + return ['', null]; |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | /** |
@@ -282,12 +282,12 @@ discard block |
||
| 282 | 282 | * dependency at key |
| 283 | 283 | * 'dependency_type'. |
| 284 | 284 | */ |
| 285 | - protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) { |
|
| 286 | - if ( ! $this->is_needed( $dependency, $context ) ) { |
|
| 285 | + protected function enqueue_dependency($dependency, $dependency_key, $context = null) { |
|
| 286 | + if ( ! $this->is_needed($dependency, $context)) { |
|
| 287 | 287 | return; |
| 288 | 288 | } |
| 289 | - $handler = $this->handlers[ $context['dependency_type'] ]; |
|
| 290 | - $handler->enqueue( $dependency ); |
|
| 289 | + $handler = $this->handlers[$context['dependency_type']]; |
|
| 290 | + $handler->enqueue($dependency); |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | /** |
@@ -301,16 +301,16 @@ discard block |
||
| 301 | 301 | * 'dependency_type'. |
| 302 | 302 | * @return bool Whether it is needed or not. |
| 303 | 303 | */ |
| 304 | - protected function is_needed( $dependency, $context ) { |
|
| 305 | - $is_needed = array_key_exists( 'is_needed', $dependency ) |
|
| 304 | + protected function is_needed($dependency, $context) { |
|
| 305 | + $is_needed = array_key_exists('is_needed', $dependency) |
|
| 306 | 306 | ? $dependency['is_needed'] |
| 307 | 307 | : null; |
| 308 | 308 | |
| 309 | - if ( null === $is_needed ) { |
|
| 309 | + if (null === $is_needed) { |
|
| 310 | 310 | return true; |
| 311 | 311 | } |
| 312 | 312 | |
| 313 | - return is_callable( $is_needed ) && $is_needed( $context ); |
|
| 313 | + return is_callable($is_needed) && $is_needed($context); |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | /** |
@@ -323,18 +323,18 @@ discard block |
||
| 323 | 323 | * Contains the type of the dependency at key |
| 324 | 324 | * 'dependency_type'. |
| 325 | 325 | */ |
| 326 | - protected function maybe_localize( $dependency, $context ) { |
|
| 327 | - if ( ! array_key_exists( 'localize', $dependency ) ) { |
|
| 326 | + protected function maybe_localize($dependency, $context) { |
|
| 327 | + if ( ! array_key_exists('localize', $dependency)) { |
|
| 328 | 328 | return; |
| 329 | 329 | } |
| 330 | 330 | |
| 331 | 331 | $localize = $dependency['localize']; |
| 332 | 332 | $data = $localize['data']; |
| 333 | - if ( is_callable( $data ) ) { |
|
| 334 | - $data = $data( $context ); |
|
| 333 | + if (is_callable($data)) { |
|
| 334 | + $data = $data($context); |
|
| 335 | 335 | } |
| 336 | 336 | |
| 337 | - \wp_localize_script( $dependency['handle'], $localize['name'], $data ); |
|
| 337 | + \wp_localize_script($dependency['handle'], $localize['name'], $data); |
|
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | /** |
@@ -347,9 +347,9 @@ discard block |
||
| 347 | 347 | * @param mixed $context Optional. The context to pass to the |
| 348 | 348 | * dependencies. |
| 349 | 349 | */ |
| 350 | - protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
| 350 | + protected function enqueue_dependency_type($dependencies, $dependency_type, $context = null) { |
|
| 351 | 351 | $context['dependency_type'] = $dependency_type; |
| 352 | - array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context ); |
|
| 352 | + array_walk($dependencies, [$this, 'enqueue_dependency'], $context); |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -362,9 +362,9 @@ discard block |
||
| 362 | 362 | * @param mixed $context Optional. The context to pass to the |
| 363 | 363 | * dependencies. |
| 364 | 364 | */ |
| 365 | - protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
| 365 | + protected function register_dependency_type($dependencies, $dependency_type, $context = null) { |
|
| 366 | 366 | $context['dependency_type'] = $dependency_type; |
| 367 | - array_walk( $dependencies, [ $this, 'register_dependency' ], $context ); |
|
| 367 | + array_walk($dependencies, [$this, 'register_dependency'], $context); |
|
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | /** |
@@ -379,12 +379,12 @@ discard block |
||
| 379 | 379 | * dependency at key |
| 380 | 380 | * 'dependency_type'. |
| 381 | 381 | */ |
| 382 | - protected function register_dependency( $dependency, $dependency_key, $context = null ) { |
|
| 383 | - $handler = $this->handlers[ $context['dependency_type'] ]; |
|
| 384 | - $handler->register( $dependency ); |
|
| 382 | + protected function register_dependency($dependency, $dependency_key, $context = null) { |
|
| 383 | + $handler = $this->handlers[$context['dependency_type']]; |
|
| 384 | + $handler->register($dependency); |
|
| 385 | 385 | |
| 386 | - if ( $this->enqueue_immediately ) { |
|
| 387 | - $this->register_enqueue_hooks( $dependency, $context ); |
|
| 386 | + if ($this->enqueue_immediately) { |
|
| 387 | + $this->register_enqueue_hooks($dependency, $context); |
|
| 388 | 388 | } |
| 389 | 389 | } |
| 390 | 390 | |
@@ -398,14 +398,14 @@ discard block |
||
| 398 | 398 | * Contains the type of the dependency at key |
| 399 | 399 | * 'dependency_type'. |
| 400 | 400 | */ |
| 401 | - protected function register_enqueue_hooks( $dependency, $context = null ) { |
|
| 402 | - $priority = $this->get_priority( $dependency ); |
|
| 401 | + protected function register_enqueue_hooks($dependency, $context = null) { |
|
| 402 | + $priority = $this->get_priority($dependency); |
|
| 403 | 403 | |
| 404 | - foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) { |
|
| 405 | - \add_action( $hook, [ $this, 'enqueue' ], $priority, 1 ); |
|
| 404 | + foreach (['wp_enqueue_scripts', 'admin_enqueue_scripts'] as $hook) { |
|
| 405 | + \add_action($hook, [$this, 'enqueue'], $priority, 1); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | - $this->maybe_localize( $dependency, $context ); |
|
| 408 | + $this->maybe_localize($dependency, $context); |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | /** |
@@ -416,9 +416,9 @@ discard block |
||
| 416 | 416 | * @param array $dependency Configuration data of the dependency. |
| 417 | 417 | * @return int Priority to use. |
| 418 | 418 | */ |
| 419 | - protected function get_priority( $dependency ) { |
|
| 420 | - if ( array_key_exists( 'priority', $dependency ) ) { |
|
| 421 | - return intval( $dependency['priority'] ); |
|
| 419 | + protected function get_priority($dependency) { |
|
| 420 | + if (array_key_exists('priority', $dependency)) { |
|
| 421 | + return intval($dependency['priority']); |
|
| 422 | 422 | } |
| 423 | 423 | return 10; |
| 424 | 424 | } |