Skip to content

Commit e402665

Browse files
committed
feat(Hook): Improved and standardized interfaces
1 parent ece4d6c commit e402665

12 files changed

+347
-124
lines changed

Accessible_Hook_Methods.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Accessible_Hook_Methods trait file.
44
*
5-
* @package XWP
5+
* @package eXtended WordPress
66
* @subpackage Contracts\Hook
77
*/
88

Context.php

+20
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ public function display_name(): string {
6464
};
6565
}
6666

67+
/**
68+
* Create a context from a slug.
69+
*
70+
* @param string $ctx_slug The context slug.
71+
* @return Context
72+
*/
73+
public static function fromSlug( string $ctx_slug ): Context {
74+
return match ( $ctx_slug ) {
75+
'front', 'frontend',
76+
'public', 'website' => self::Frontend,
77+
'admin', 'dashboard',
78+
'administration' => self::Admin,
79+
'ajax' => self::Ajax,
80+
'cron' => self::Cron,
81+
'rest' => self::REST,
82+
'cli' => self::CLI,
83+
'global' => self::Global,
84+
};
85+
}
86+
6787
/**
6888
* Check if the current context is valid.
6989
*

Context_Interface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Context_Interface interface file.
44
*
5-
* @package XWP
5+
* @package eXtended WordPress
66
* @subpackage Contracts\Hook
77
*/
88

Hook_Interface.php

-104
This file was deleted.

Hookable.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Hookable interface file.
4+
*
5+
* @package eXtended WordPress
6+
* @subpackage Contracts\Hook
7+
*/
8+
9+
namespace XWP\Contracts\Hook;
10+
11+
/**
12+
* Hook decorator functionality.
13+
*
14+
* @template T
15+
*
16+
* ! Global properties shared among hooks and handlers
17+
*
18+
* @property-read string $tag Hook name. Can use the `vsprinf` format in combination with `$modifiers`.
19+
* @property-read array|int|string|callable $priority Hook priority. Can be a number, callable, or a string. Strings are treated as filters, which will be applied to the default priority.
20+
* @property-read int $context Context bitmask determining where the hook can be invoked.
21+
* @property-read string|false $requires Prerequisite hook that must be invoked before this hook. Handler classname, or Classname, method array.
22+
* @property-read string|array|false $modifiers Replacement pairs for the tag name.
23+
* @property-read int $real_priority Actual priority of the hook.
24+
* @property-read array|callable|false $conditional Hook conditional. Callable which will be invoked to determine if the hook should be invoked.
25+
*/
26+
interface Hookable {
27+
/**
28+
* Indicates that a hook can be invoked in user-facing pages.
29+
*
30+
* @var int
31+
*/
32+
public const CTX_FRONTEND = 1; // 0000001
33+
34+
/**
35+
* Indicates that a hook can be invoked in the admin area.
36+
*
37+
* @var int
38+
*/
39+
public const CTX_ADMIN = 2; // 0000010
40+
41+
/**
42+
* Indicates that a hook can be invoked on AJAX requests.
43+
*
44+
* @var int
45+
*/
46+
public const CTX_AJAX = 4; // 0000100
47+
48+
/**
49+
* Indicates that a hook can be invoked when a cron job is running.
50+
*
51+
* @var int
52+
*/
53+
public const CTX_CRON = 8; // 0001000
54+
55+
/**
56+
* Indicates that a hook can be invoked on REST API requests.
57+
*
58+
* @var int
59+
*/
60+
public const CTX_REST = 16; // 0010000
61+
62+
/**
63+
* Indicates that a hook can be invoked when WP CLI is running.
64+
*
65+
* @var int
66+
*/
67+
public const CTX_CLI = 32; // 0100000
68+
69+
/**
70+
* Indicates that a hook can be invoked in any context.
71+
*
72+
* @var int
73+
*/
74+
public const CTX_GLOBAL = 63; // 0111111
75+
76+
/**
77+
* Hook type. Can be `handler`, `action` or `filter`.
78+
*
79+
* @var string
80+
*/
81+
public const HOOK_TYPE = self::HOOK_TYPE;
82+
83+
/**
84+
* Set the reflector
85+
*
86+
* @param \Reflector $reflector Reflector instance.
87+
* @return static
88+
*/
89+
public function set_reflector( \Reflector $reflector ): static;
90+
91+
/**
92+
* Set the hook target.
93+
*
94+
* @param array|T $target Hook target.
95+
* @return static
96+
*/
97+
public function set_target( array|object $target ): static;
98+
}

Initializable.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Handler interface file.
4+
*
5+
* @package eXtended WordPress
6+
* @subpackage Contracts
7+
*/
8+
9+
namespace XWP\Contracts\Hook;
10+
11+
/**
12+
* Handler interface.
13+
*
14+
* @template<T>
15+
*
16+
* @property-read Initialize $strategy Invocation strategy.
17+
* @property-read class-string>T> $classname Handler classname.
18+
* @property-read bool $initialized Indicates if the handler has been initialized.
19+
* @property-read T|null $target Handler target.
20+
* @property-read \ReflectionClass<T> $reflector Reflector instance.
21+
*/
22+
interface Initializable extends Hookable {
23+
/**
24+
* Set the classname.
25+
*
26+
* @param class-string<T> $classname Handler classname.
27+
* @return static
28+
*/
29+
public function set_classname( string $classname ): static;
30+
31+
/**
32+
* Initializes the handler.
33+
*
34+
* @return static
35+
*/
36+
public function initialize(): static;
37+
38+
/**
39+
* Check if the handler can be initialized.
40+
*
41+
* @return bool
42+
*/
43+
public function can_initialize(): bool;
44+
}

Initialize.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php // phpcs:disable PHPCompatibility.Variables.ForbiddenThisUseContexts
2+
/**
3+
* Invoke enum file.
4+
*
5+
* @package eXtended WordPress
6+
* @subpackage Contracts\Hook
7+
*/
8+
9+
namespace XWP\Contracts\Hook;
10+
11+
/**
12+
* Determines initialization strategy for a Handler.
13+
*/
14+
enum Initialize: string {
15+
/**
16+
* Indicates that the handler should be initialized unconditionally.
17+
*/
18+
case Unconditionally = 'unconditional';
19+
20+
/**
21+
* Indicates that the handler should be initialized immediately.
22+
*/
23+
case Immediately = 'immediate';
24+
25+
/**
26+
* Indicates that the handler should be initialized on registration.
27+
*/
28+
case Early = 'early';
29+
30+
/**
31+
* Indicates that the handler should be initialized on demand.
32+
*
33+
* On demand will initialize the handler when the hook is registered.
34+
*/
35+
case OnDemand = 'on-demand';
36+
37+
/**
38+
* Indicates that the handler should be initialized on specified action.
39+
*/
40+
case Deferred = 'deferred';
41+
42+
/**
43+
* Special case for manually initialized handlers.
44+
*/
45+
case Dynamically = 'dynamic';
46+
47+
/**
48+
* Indicates that the handler should be initialized just in time.
49+
*
50+
* Just in time will initialize the handler just before the hook is invoked.
51+
*/
52+
case JustInTime = 'just-in-time';
53+
54+
/**
55+
* Check if the tag is valid for the initialization strategy.
56+
*
57+
* @param string|null $tag Tag to check.
58+
* @return bool
59+
*/
60+
public function is_tag_valid( ?string $tag ): bool {
61+
return match ( $this ) {
62+
self::OnDemand,
63+
self::JustInTime,
64+
self::Dynamically,
65+
self::Immediately => ! $tag,
66+
default => ! ! $tag,
67+
};
68+
}
69+
70+
/**
71+
* Check if the handler is initialized on demand.
72+
*
73+
* @return bool
74+
*/
75+
public function is_ondemand(): bool {
76+
return self::OnDemand === $this;
77+
}
78+
79+
/**
80+
* Check if the handler is initialized just in time.
81+
*
82+
* @return bool
83+
*/
84+
public function is_just_in_time(): bool {
85+
return self::JustInTime === $this;
86+
}
87+
}

0 commit comments

Comments
 (0)