Constants
5 days ago
Contracts
1 month ago
Hooks
5 days ago
Models
1 month ago
BaseHook.php
1 month ago
HookServiceProvider.php
5 days ago
Menu.php
1 month ago
User.php
1 month ago
UserMeta.php
1 month ago
HookServiceProvider.php
89 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\VersionUpdate; |
| 17 | use Kirki\Framework\Wordpress\Hooks\Actions\RegisterRestApi; |
| 18 | use Kirki\Framework\Wordpress\Hooks\Actions\RegisterSiteRoutes; |
| 19 | class HookServiceProvider extends ServiceProvider |
| 20 | { |
| 21 | /** |
| 22 | * The defaults. |
| 23 | * |
| 24 | * @var array<string, array<string>> |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | protected static $defaults = ['actions' => [RegisterRestApi::class, RegisterSiteRoutes::class, VersionUpdate::class], 'filters' => []]; |
| 29 | /** |
| 30 | * Register the hooks to the application. |
| 31 | * |
| 32 | * @return void |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | public function register() |
| 37 | { |
| 38 | $hooks = $this->hooks(); |
| 39 | $this->app->tag($hooks['actions'], 'hook.actions'); |
| 40 | $this->app->tag($hooks['filters'], 'hook.filters'); |
| 41 | } |
| 42 | /** |
| 43 | * Get the hooks from the configuration file. |
| 44 | * |
| 45 | * @return array |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | */ |
| 49 | protected function hooks() |
| 50 | { |
| 51 | $hooks_path = $this->app->config_path('hooks.php'); |
| 52 | if (!\file_exists($hooks_path)) { |
| 53 | return static::$defaults; |
| 54 | } |
| 55 | $hooks = (include $hooks_path); |
| 56 | if (empty($hooks)) { |
| 57 | return static::$defaults; |
| 58 | } |
| 59 | $default_action_hooks = []; |
| 60 | foreach (static::$defaults['actions'] as $action) { |
| 61 | if (!\in_array($action, $hooks['actions'], \true)) { |
| 62 | $default_action_hooks[] = $action; |
| 63 | } |
| 64 | } |
| 65 | if (!empty($default_action_hooks)) { |
| 66 | $hooks['actions'] = \array_merge($default_action_hooks, $hooks['actions']); |
| 67 | } |
| 68 | return ['actions' => $hooks['actions'] ?? [], 'filters' => $hooks['filters'] ?? []]; |
| 69 | } |
| 70 | /** |
| 71 | * Add the action hooks on after the application booted. |
| 72 | * |
| 73 | * @return void |
| 74 | * |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | public function boot() |
| 78 | { |
| 79 | $actions = $this->app->tagged('hook.actions'); |
| 80 | foreach ($actions as $action) { |
| 81 | add_action($action->get_name(), [$action, 'handle'], $action->get_priority(), $action->get_args_count()); |
| 82 | } |
| 83 | $filters = $this->app->tagged('hook.filters'); |
| 84 | foreach ($filters as $filter) { |
| 85 | add_filter($filter->get_name(), [$filter, 'handle'], $filter->get_priority(), $filter->get_args_count()); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 |