| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Assets package. |
| 5 |
* |
| 6 |
* (c) Inpsyde GmbH |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace Inpsyde\Assets\Util; |
| 15 |
|
| 16 |
use Inpsyde\WpContext; |
| 17 |
use Inpsyde\Assets\Asset; |
| 18 |
|
| 19 |
class AssetHookResolver |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var WpContext |
| 23 |
*/ |
| 24 |
protected $context; |
| 25 |
|
| 26 |
/** |
| 27 |
* @param WpContext|null $context |
| 28 |
*/ |
| 29 |
public function __construct(?WpContext $context = null) |
| 30 |
{ |
| 31 |
$this->context = $context ?? WpContext::determine(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Resolving to the current location/page in WordPress all current hooks. |
| 36 |
* |
| 37 |
* @return string[] |
| 38 |
*/ |
| 39 |
public function resolve(): array |
| 40 |
{ |
| 41 |
$isLogin = $this->context->isLogin(); |
| 42 |
$isFront = $this->context->isFrontoffice(); |
| 43 |
$isActivate = $this->context->isWpActivate(); |
| 44 |
|
| 45 |
if (!$isActivate && !$isLogin && !$isFront && !$this->context->isBackoffice()) { |
| 46 |
return []; |
| 47 |
} |
| 48 |
|
| 49 |
if ($isLogin) { |
| 50 |
return [Asset::HOOK_LOGIN]; |
| 51 |
} |
| 52 |
|
| 53 |
if ($isActivate) { |
| 54 |
return [Asset::HOOK_ACTIVATE]; |
| 55 |
} |
| 56 |
|
| 57 |
// These hooks might be fired in both front and back office. |
| 58 |
$assets = [Asset::HOOK_BLOCK_ASSETS]; |
| 59 |
|
| 60 |
if ($isFront) { |
| 61 |
$assets[] = Asset::HOOK_FRONTEND; |
| 62 |
$assets[] = Asset::HOOK_CUSTOMIZER_PREVIEW; |
| 63 |
|
| 64 |
return $assets; |
| 65 |
} |
| 66 |
|
| 67 |
$assets[] = Asset::HOOK_BLOCK_EDITOR_ASSETS; |
| 68 |
$assets[] = Asset::HOOK_CUSTOMIZER; |
| 69 |
$assets[] = Asset::HOOK_BACKEND; |
| 70 |
|
| 71 |
return $assets; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @return string|null |
| 76 |
*/ |
| 77 |
public function lastHook(): ?string |
| 78 |
{ |
| 79 |
switch (true) { |
| 80 |
case $this->context->isLogin(): |
| 81 |
return Asset::HOOK_LOGIN; |
| 82 |
case $this->context->isFrontoffice(): |
| 83 |
return Asset::HOOK_FRONTEND; |
| 84 |
case $this->context->isBackoffice(): |
| 85 |
return Asset::HOOK_BACKEND; |
| 86 |
case $this->context->isWpActivate(): |
| 87 |
return Asset::HOOK_ACTIVATE; |
| 88 |
} |
| 89 |
|
| 90 |
return null; |
| 91 |
} |
| 92 |
} |
| 93 |
|