Skip to content

Commit 82d1355

Browse files
committed
fix(Hook): Fixed tag parsing
1 parent 36e0f39 commit 82d1355

File tree

5 files changed

+156
-132
lines changed

5 files changed

+156
-132
lines changed

src/App_Factory.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace XWP\DI;
1010

1111
use DI\Container;
12+
use Psr\Container\ContainerInterface;
1213
use XWP\Helper\Traits\Singleton;
1314

1415
/**
@@ -58,7 +59,15 @@ protected function call_create( array $config ): Container {
5859

5960
return $this->containers[ $config['id'] ] ??= App_Builder::configure( $config )
6061
->addDefinitions( $config['module'] )
61-
->addDefinitions( array( 'xwp.app.config' => $config ) )
62+
->addDefinitions(
63+
array(
64+
'xwp.app.config' => $config,
65+
'xwp.app.tag' => \DI\factory(
66+
static fn( string $tag, ContainerInterface $ctr ) =>
67+
\DI\string( $tag )->resolve( $ctr ),
68+
),
69+
),
70+
)
6271
->build();
6372
}
6473

@@ -73,7 +82,7 @@ protected function call_create( array $config ): Container {
7382
protected function call_extend( string $container, array $module, string $position = 'after', ?string $target = null ): void {
7483
\add_filter(
7584
"xwp_extend_import_{$container}",
76-
static function ( array $imports, string $classname ) use( $module, $position, $target ): array {
85+
static function ( array $imports, string $classname ) use ( $module, $position, $target ): array {
7786
if ( $target && $target !== $classname ) {
7887
return $imports;
7988
}

src/Decorators/Ajax_Action.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,24 @@ protected function get_cb_args( array $args ): array {
209209
/**
210210
* Parse the nonce parameter.
211211
*
212-
* @param bool|string|array{0:string,1:string} $nonce Nonce parameter.
212+
* @param bool|string|array{0:string,1:string}|array<string,string> $nonce Nonce parameter.
213213
* @return array{0?:string,1?:string}
214214
*/
215215
private function parse_nonce( bool|string|array $nonce ): array {
216-
if ( false === $nonce ) {
216+
if ( ! $nonce ) {
217217
return array();
218218
}
219219

220-
if ( \is_array( $nonce ) ) {
221-
return $nonce;
220+
if ( ! \is_array( $nonce ) ) {
221+
return array(
222+
"{$this->prefix}_{$this->action}",
223+
\is_string( $nonce ) ? $nonce : false,
224+
);
222225
}
223226

224-
return array(
225-
"{$this->prefix}_{$this->action}",
226-
\is_string( $nonce ) ? $nonce : false,
227-
);
227+
return ! \array_is_list( $nonce )
228+
? array( \current( $nonce ), \key( $nonce ) )
229+
: $nonce;
228230
}
229231

230232
private function fire_guard_cb( string $type ): void {

src/Decorators/Filter.php

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,6 @@ public function __construct(
7979
parent::__construct( $tag, $priority, $context, $conditional, $modifiers );
8080
}
8181

82-
protected function get_container(): Container {
83-
return $this->handler->container;
84-
}
85-
86-
/**
87-
* Get the type of hook.
88-
*
89-
* @return string
90-
*/
91-
protected function get_type(): string {
92-
return 'filter';
93-
}
94-
95-
/**
96-
* Get the current hook.
97-
*
98-
* @return string
99-
*/
100-
protected function current(): string {
101-
$cb = "current_{$this->get_type()}";
102-
103-
return $cb();
104-
}
105-
10682
public function with_reflector( Reflector $r ): static {
10783
$this->args ??= $r->getNumberOfParameters();
10884
$this->method ??= $r->getName();
@@ -136,6 +112,64 @@ public function can_load(): bool {
136112
return parent::can_load() && ( $this->handler->is_lazy() || $this->handler->loaded );
137113
}
138114

115+
public function load(): bool {
116+
if ( $this->loaded ) {
117+
return true;
118+
}
119+
120+
if ( ! $this->init_handler( $this->handler::INIT_ON_DEMAND ) ) {
121+
return false;
122+
}
123+
124+
$this->loaded = $this->load_hook();
125+
126+
return $this->loaded;
127+
}
128+
129+
public function invoke( mixed ...$args ): mixed {
130+
if (
131+
! $this->init_handler( $this->handler::INIT_JUST_IN_TIME ) ||
132+
! parent::can_load() ||
133+
( $this->cb_valid( self::INV_ONCE ) && $this->fired ) ||
134+
( $this->cb_valid( self::INV_LOOPED ) && $this->firing )
135+
) {
136+
return $args[0] ?? null;
137+
}
138+
139+
try {
140+
return $this->fire_hook( ...$args );
141+
} catch ( \Throwable $e ) {
142+
return $this->handle_exception( $e, $args[0] ?? null );
143+
} finally {
144+
$this->firing = false;
145+
++$this->fired;
146+
}
147+
}
148+
149+
protected function get_container(): Container {
150+
return $this->handler->container;
151+
}
152+
153+
/**
154+
* Get the type of hook.
155+
*
156+
* @return string
157+
*/
158+
protected function get_type(): string {
159+
return 'filter';
160+
}
161+
162+
/**
163+
* Get the current hook.
164+
*
165+
* @return string
166+
*/
167+
protected function current(): string {
168+
$cb = "current_{$this->get_type()}";
169+
170+
return $cb();
171+
}
172+
139173
protected function init_handler( string $strategy ): bool {
140174
if ( $this->handler->loaded ) {
141175
return true;
@@ -165,20 +199,6 @@ protected function get_target(): array {
165199
: array( $this, 'invoke' );
166200
}
167201

168-
public function load(): bool {
169-
if ( $this->loaded ) {
170-
return true;
171-
}
172-
173-
if ( ! $this->init_handler( $this->handler::INIT_ON_DEMAND ) ) {
174-
return false;
175-
}
176-
177-
$this->loaded = $this->load_hook();
178-
179-
return $this->loaded;
180-
}
181-
182202
/**
183203
* Loads the hook.
184204
*
@@ -187,33 +207,13 @@ public function load(): bool {
187207
*/
188208
protected function load_hook( ?string $tag = null ): bool {
189209
return ( "add_{$this->get_type()}" )(
190-
$tag ?? $this->tag,
210+
$tag ?? $this->get_tag(),
191211
$this->target,
192212
$this->priority,
193213
$this->args,
194214
);
195215
}
196216

197-
public function invoke( mixed ...$args ): mixed {
198-
if (
199-
! $this->init_handler( $this->handler::INIT_JUST_IN_TIME ) ||
200-
! parent::can_load() ||
201-
( $this->cb_valid( self::INV_ONCE ) && $this->fired ) ||
202-
( $this->cb_valid( self::INV_LOOPED ) && $this->firing )
203-
) {
204-
return $args[0] ?? null;
205-
}
206-
207-
try {
208-
return $this->fire_hook( ...$args );
209-
} catch ( \Throwable $e ) {
210-
return $this->handle_exception( $e, $args[0] ?? null );
211-
} finally {
212-
$this->firing = false;
213-
++$this->fired;
214-
}
215-
}
216-
217217
/**
218218
* Fire the hook.
219219
*
@@ -276,7 +276,7 @@ protected function handle_exception( \Throwable $e, mixed $v ): mixed {
276276
\sprintf(
277277
'Error during %s %s for handler %s. %s',
278278
\esc_html( $this->get_type() ),
279-
\esc_html( $this->tag ),
279+
\esc_html( $this->get_tag() ),
280280
\esc_html( $this->handler->classname ),
281281
\esc_html( $e->getMessage() ),
282282
),

src/Decorators/Handler.php

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ public function __construct(
100100
parent::__construct( $tag, $tag ? $priority : null, $context, $conditional, $modifiers );
101101
}
102102

103+
/**
104+
* Mark the handler as loaded, and call the on_initialize method.
105+
*
106+
* @return bool
107+
*/
108+
protected function on_initialize(): bool {
109+
if ( ! $this->did_init && $this->method_exists( __FUNCTION__ ) ) {
110+
$this->container->call(
111+
array( $this->instance, __FUNCTION__ ),
112+
$this->resolve_params( __FUNCTION__ ),
113+
);
114+
}
115+
116+
$this->did_init = true;
117+
118+
return true;
119+
}
120+
103121
/**
104122
* Set the handler classname.
105123
*
@@ -159,6 +177,35 @@ public function load(): bool {
159177
$this->initialize()->configure_async()->on_initialize();
160178
}
161179

180+
/**
181+
* Get the handler target.
182+
*
183+
* @return T|null
184+
*/
185+
public function get_target(): ?object {
186+
return $this->instance ?? null;
187+
}
188+
189+
public function can_load(): bool {
190+
return parent::can_load() &&
191+
$this->check_method(
192+
array( $this->classname, 'can_initialize' ),
193+
$this->resolve_params( 'can_initialize' ),
194+
);
195+
}
196+
197+
public function is_lazy(): bool {
198+
return self::INIT_ON_DEMAND === $this->strategy || self::INIT_JUST_IN_TIME === $this->strategy;
199+
}
200+
201+
public function is_hookable(): bool {
202+
if ( ! $this->check_context() ) {
203+
return false;
204+
}
205+
206+
return $this->hookable ?? true;
207+
}
208+
162209
/**
163210
* Instantiate the handler.
164211
*
@@ -200,24 +247,6 @@ protected function configure_async(): static {
200247
return $this;
201248
}
202249

203-
/**
204-
* Mark the handler as loaded, and call the on_initialize method.
205-
*
206-
* @return bool
207-
*/
208-
protected function on_initialize(): bool {
209-
if ( ! $this->did_init && $this->method_exists( __FUNCTION__ ) ) {
210-
$this->container->call(
211-
array( $this->instance, __FUNCTION__ ),
212-
$this->resolve_params( __FUNCTION__ ),
213-
);
214-
}
215-
216-
$this->did_init = true;
217-
218-
return true;
219-
}
220-
221250
/**
222251
* Check if the method exists.
223252
*
@@ -244,29 +273,12 @@ protected function resolve_params( string $method ): array {
244273
return $injector ? $injector->resolve( $this ) : array();
245274
}
246275

247-
/**
248-
* Get the handler target.
249-
*
250-
* @return T|null
251-
*/
252-
public function get_target(): ?object {
253-
return $this->instance ?? null;
254-
}
255-
256-
public function can_load(): bool {
257-
return parent::can_load() &&
258-
$this->check_method(
259-
array( $this->classname, 'can_initialize' ),
260-
$this->resolve_params( 'can_initialize' ),
261-
);
262-
}
263-
264276
protected function get_id(): string {
265277
return \strtolower( \str_replace( '\\', '_', $this->classname ) );
266278
}
267279

268280
protected function get_tag(): string {
269-
return $this->tag ?: \current_action();
281+
return parent::get_tag() ?: \current_action();
270282
}
271283

272284
protected function get_priority(): int {
@@ -286,16 +298,4 @@ protected function get_container(): Container {
286298
protected function get_lazy_hook(): string {
287299
return "{$this->id}_{$this->strategy}_init";
288300
}
289-
290-
public function is_lazy(): bool {
291-
return self::INIT_ON_DEMAND === $this->strategy || self::INIT_JUST_IN_TIME === $this->strategy;
292-
}
293-
294-
public function is_hookable(): bool {
295-
if ( ! $this->check_context() ) {
296-
return false;
297-
}
298-
299-
return $this->hookable ?? true;
300-
}
301301
}

0 commit comments

Comments
 (0)