@@ -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 array |
| 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 ] = $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] = $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 | /** |
@@ -206,9 +206,9 @@ discard block |
||
| 206 | 206 | * dependencies. |
| 207 | 207 | * @return bool Returns whether the handle was found or not. |
| 208 | 208 | */ |
| 209 | - public function enqueue_handle( $handle, $context = null ) { |
|
| 210 | - list( $dependency_key, $dependency ) = $this->get_dependency_array( $handle ); |
|
| 211 | - if ( $dependency ) { |
|
| 209 | + public function enqueue_handle($handle, $context = null) { |
|
| 210 | + list($dependency_key, $dependency) = $this->get_dependency_array($handle); |
|
| 211 | + if ($dependency) { |
|
| 212 | 212 | |
| 213 | 213 | $this->enqueue_dependency( |
| 214 | 214 | $dependency, |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | $context |
| 217 | 217 | ); |
| 218 | 218 | |
| 219 | - $this->maybe_localize( $dependency, $context ); |
|
| 219 | + $this->maybe_localize($dependency, $context); |
|
| 220 | 220 | |
| 221 | 221 | return true; |
| 222 | 222 | } |
@@ -232,15 +232,15 @@ discard block |
||
| 232 | 232 | * @return array Array containing the dependency key as well as the |
| 233 | 233 | * dependency array itself. |
| 234 | 234 | */ |
| 235 | - protected function get_dependency_array( $handle ) { |
|
| 236 | - foreach ( $this->dependencies as $dependency_type ) { |
|
| 237 | - $dependency_key = array_search( $handle, $dependency_type, true ); |
|
| 238 | - if ( $dependency_key ) { |
|
| 239 | - return [ $dependency_key, $dependency_type[ $dependency_key ] ]; |
|
| 235 | + protected function get_dependency_array($handle) { |
|
| 236 | + foreach ($this->dependencies as $dependency_type) { |
|
| 237 | + $dependency_key = array_search($handle, $dependency_type, true); |
|
| 238 | + if ($dependency_key) { |
|
| 239 | + return [$dependency_key, $dependency_type[$dependency_key]]; |
|
| 240 | 240 | } |
| 241 | 241 | } |
| 242 | 242 | // Handle not found, return an empty array. |
| 243 | - return [ '', null ]; |
|
| 243 | + return ['', null]; |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -255,13 +255,13 @@ discard block |
||
| 255 | 255 | * dependency at key |
| 256 | 256 | * 'dependency_type'. |
| 257 | 257 | */ |
| 258 | - protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) { |
|
| 259 | - if ( ! $this->is_needed( $dependency, $context ) ) { |
|
| 258 | + protected function enqueue_dependency($dependency, $dependency_key, $context = null) { |
|
| 259 | + if ( ! $this->is_needed($dependency, $context)) { |
|
| 260 | 260 | return; |
| 261 | 261 | } |
| 262 | 262 | /** @var \BrightNucleus\Contract\Enqueueable $handler */ |
| 263 | 263 | $handler = new $this->handlers[$context['dependency_type']]; |
| 264 | - $handler->enqueue( $dependency ); |
|
| 264 | + $handler->enqueue($dependency); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -275,16 +275,16 @@ discard block |
||
| 275 | 275 | * 'dependency_type'. |
| 276 | 276 | * @return bool Whether it is needed or not. |
| 277 | 277 | */ |
| 278 | - protected function is_needed( $dependency, $context ) { |
|
| 279 | - $is_needed = array_key_exists( 'is_needed', $dependency ) |
|
| 278 | + protected function is_needed($dependency, $context) { |
|
| 279 | + $is_needed = array_key_exists('is_needed', $dependency) |
|
| 280 | 280 | ? $dependency['is_needed'] |
| 281 | 281 | : null; |
| 282 | 282 | |
| 283 | - if ( null === $is_needed ) { |
|
| 283 | + if (null === $is_needed) { |
|
| 284 | 284 | return true; |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | - return is_callable( $is_needed ) && $is_needed( $context ); |
|
| 287 | + return is_callable($is_needed) && $is_needed($context); |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | /** |
@@ -297,18 +297,18 @@ discard block |
||
| 297 | 297 | * Contains the type of the dependency at key |
| 298 | 298 | * 'dependency_type'. |
| 299 | 299 | */ |
| 300 | - protected function maybe_localize( $dependency, $context ) { |
|
| 301 | - if ( ! array_key_exists( 'localize', $dependency ) ) { |
|
| 300 | + protected function maybe_localize($dependency, $context) { |
|
| 301 | + if ( ! array_key_exists('localize', $dependency)) { |
|
| 302 | 302 | return; |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | $localize = $dependency['localize']; |
| 306 | 306 | $data = $localize['data']; |
| 307 | - if ( is_callable( $data ) ) { |
|
| 308 | - $data = $data( $context ); |
|
| 307 | + if (is_callable($data)) { |
|
| 308 | + $data = $data($context); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | - \wp_localize_script( $dependency['handle'], $localize['name'], $data ); |
|
| 311 | + \wp_localize_script($dependency['handle'], $localize['name'], $data); |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | /** |
@@ -321,9 +321,9 @@ discard block |
||
| 321 | 321 | * @param mixed $context Optional. The context to pass to the |
| 322 | 322 | * dependencies. |
| 323 | 323 | */ |
| 324 | - protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
| 324 | + protected function enqueue_dependency_type($dependencies, $dependency_type, $context = null) { |
|
| 325 | 325 | $context['dependency_type'] = $dependency_type; |
| 326 | - array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context ); |
|
| 326 | + array_walk($dependencies, [$this, 'enqueue_dependency'], $context); |
|
| 327 | 327 | } |
| 328 | 328 | |
| 329 | 329 | /** |
@@ -336,9 +336,9 @@ discard block |
||
| 336 | 336 | * @param mixed $context Optional. The context to pass to the |
| 337 | 337 | * dependencies. |
| 338 | 338 | */ |
| 339 | - protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) { |
|
| 339 | + protected function register_dependency_type($dependencies, $dependency_type, $context = null) { |
|
| 340 | 340 | $context['dependency_type'] = $dependency_type; |
| 341 | - array_walk( $dependencies, [ $this, 'register_dependency' ], $context ); |
|
| 341 | + array_walk($dependencies, [$this, 'register_dependency'], $context); |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | /** |
@@ -353,13 +353,13 @@ discard block |
||
| 353 | 353 | * dependency at key |
| 354 | 354 | * 'dependency_type'. |
| 355 | 355 | */ |
| 356 | - protected function register_dependency( $dependency, $dependency_key, $context = null ) { |
|
| 356 | + protected function register_dependency($dependency, $dependency_key, $context = null) { |
|
| 357 | 357 | /** @var \BrightNucleus\Contract\Registerable $handler */ |
| 358 | 358 | $handler = new $this->handlers[$context['dependency_type']]; |
| 359 | - $handler->register( $dependency ); |
|
| 359 | + $handler->register($dependency); |
|
| 360 | 360 | |
| 361 | - if ( $this->enqueue_immediately ) { |
|
| 362 | - $this->register_enqueue_hooks( $dependency, $context ); |
|
| 361 | + if ($this->enqueue_immediately) { |
|
| 362 | + $this->register_enqueue_hooks($dependency, $context); |
|
| 363 | 363 | } |
| 364 | 364 | } |
| 365 | 365 | |
@@ -373,13 +373,13 @@ discard block |
||
| 373 | 373 | * Contains the type of the dependency at key |
| 374 | 374 | * 'dependency_type'. |
| 375 | 375 | */ |
| 376 | - protected function register_enqueue_hooks( $dependency, $context = null ) { |
|
| 377 | - $priority = $this->get_priority( $dependency ); |
|
| 376 | + protected function register_enqueue_hooks($dependency, $context = null) { |
|
| 377 | + $priority = $this->get_priority($dependency); |
|
| 378 | 378 | |
| 379 | - foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) { |
|
| 380 | - \add_action( $hook, [ $this, 'enqueue' ], $priority, 1 ); |
|
| 379 | + foreach (['wp_enqueue_scripts', 'admin_enqueue_scripts'] as $hook) { |
|
| 380 | + \add_action($hook, [$this, 'enqueue'], $priority, 1); |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | - $this->maybe_localize( $dependency, $context ); |
|
| 383 | + $this->maybe_localize($dependency, $context); |
|
| 384 | 384 | } |
| 385 | 385 | } |