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