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