Constants
1 week ago
Contracts
1 month ago
Hooks
1 week ago
Models
1 month ago
BaseHook.php
1 month ago
HookServiceProvider.php
1 week ago
Menu.php
1 month ago
User.php
1 month ago
UserMeta.php
1 month ago
HookServiceProvider.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Loads action and filter hook classes from config/hooks.php and tags them for container discovery. |
| 5 | * Ensures RegisterRestApi is always registered even when config is empty. |
| 6 | * Boots hook instances onto WordPress during application boot. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Wordpress |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Wordpress; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Kirki\Framework\ServiceProvider; |
| 16 | use Kirki\Framework\Wordpress\Hooks\Actions\FlushQueuedCookies; |
| 17 | use Kirki\Framework\Wordpress\Hooks\Actions\VersionUpdate; |
| 18 | use Kirki\Framework\Wordpress\Hooks\Actions\RegisterRestApi; |
| 19 | use Kirki\Framework\Wordpress\Hooks\Actions\RegisterSiteRoutes; |
| 20 | use Kirki\Framework\Wordpress\Hooks\Filters\FlushRestCookies; |
| 21 | class HookServiceProvider extends ServiceProvider |
| 22 | { |
| 23 | /** |
| 24 | * The defaults. |
| 25 | * |
| 26 | * @var array<string, array<string>> |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | protected static $defaults = ['actions' => [RegisterRestApi::class, RegisterSiteRoutes::class, VersionUpdate::class, FlushQueuedCookies::class], 'filters' => [FlushRestCookies::class]]; |
| 31 | /** |
| 32 | * Register the hooks to the application. |
| 33 | * |
| 34 | * @return void |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public function register() |
| 39 | { |
| 40 | $hooks = $this->hooks(); |
| 41 | $this->app->tag($hooks['actions'], 'hook.actions'); |
| 42 | $this->app->tag($hooks['filters'], 'hook.filters'); |
| 43 | } |
| 44 | /** |
| 45 | * Get the hooks from the configuration file. |
| 46 | * |
| 47 | * @return array |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | protected function hooks() |
| 52 | { |
| 53 | $hooks_path = $this->app->config_path('hooks.php'); |
| 54 | if (!\file_exists($hooks_path)) { |
| 55 | return static::$defaults; |
| 56 | } |
| 57 | $hooks = (include $hooks_path); |
| 58 | if (empty($hooks)) { |
| 59 | return static::$defaults; |
| 60 | } |
| 61 | return ['actions' => $this->merge_defaults($hooks['actions'] ?? [], 'actions'), 'filters' => $this->merge_defaults($hooks['filters'] ?? [], 'filters')]; |
| 62 | } |
| 63 | /** |
| 64 | * Merge the framework default hooks into the configured ones. |
| 65 | * |
| 66 | * Defaults are prepended so the framework hooks register before application hooks, |
| 67 | * and are skipped when the application already lists them. |
| 68 | * |
| 69 | * @param array $configured The hooks defined by the application. |
| 70 | * @param string $type The hook group, either actions or filters. |
| 71 | * |
| 72 | * @return array |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | protected function merge_defaults(array $configured, string $type) |
| 77 | { |
| 78 | $missing = []; |
| 79 | foreach (static::$defaults[$type] as $hook) { |
| 80 | if (!\in_array($hook, $configured, \true)) { |
| 81 | $missing[] = $hook; |
| 82 | } |
| 83 | } |
| 84 | if (empty($missing)) { |
| 85 | return $configured; |
| 86 | } |
| 87 | return \array_merge($missing, $configured); |
| 88 | } |
| 89 | /** |
| 90 | * Add the action hooks on after the application booted. |
| 91 | * |
| 92 | * @return void |
| 93 | * |
| 94 | * @since 1.0.0 |
| 95 | */ |
| 96 | public function boot() |
| 97 | { |
| 98 | $actions = $this->app->tagged('hook.actions'); |
| 99 | foreach ($actions as $action) { |
| 100 | add_action($action->get_name(), [$action, 'handle'], $action->get_priority(), $action->get_args_count()); |
| 101 | } |
| 102 | $filters = $this->app->tagged('hook.filters'); |
| 103 | foreach ($filters as $filter) { |
| 104 | add_filter($filter->get_name(), [$filter, 'handle'], $filter->get_priority(), $filter->get_args_count()); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 |