Skip to content

Commit 73a3ff8

Browse files
committed
fix(Hook): Fixed tag parsing
1 parent 543ec27 commit 73a3ff8

File tree

6 files changed

+152
-142
lines changed

6 files changed

+152
-142
lines changed

src/App_Builder.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use DI\CompiledContainer as Compiled;
1212
use DI\ContainerBuilder;
1313
use DI\Definition\Source\DefinitionSource;
14+
use Psr\Container\ContainerInterface;
1415
use Psr\Log\LoggerInterface;
1516
use Psr\Log\NullLogger;
1617
use XWP\DI\Hook\Compiler;
@@ -120,8 +121,8 @@ public function enableHookCache( bool $enableCache, string $cacheDirectory ): st
120121
*/
121122
public function addBaseDefinition( array $config ): App_Builder {
122123
$definition = array(
123-
'app' => \DI\get( 'Hook-' . $config['app_module'] ),
124-
'app.cache' => \DI\value(
124+
'app' => \DI\get( 'Hook-' . $config['app_module'] ),
125+
'app.cache' => \DI\value(
125126
array(
126127
'app' => $config['cache_app'],
127128
'defs' => $config['cache_defs'],
@@ -130,14 +131,18 @@ public function addBaseDefinition( array $config ): App_Builder {
130131
'ns' => $config['app_id'],
131132
),
132133
),
133-
'app.debug' => \DI\value( $config['app_debug'] ),
134-
'app.env' => \DI\factory( 'wp_get_environment_type' ),
135-
'app.extend' => \DI\value( $config['extendable'] ),
136-
'app.id' => \DI\value( $config['app_id'] ),
137-
'app.module' => \DI\value( $config['app_module'] ),
138-
'app.type' => \DI\value( $config['app_type'] ),
139-
'app.uuid' => \DI\factory( 'wp_generate_uuid4' ),
140-
'app.ver' => \DI\value( $config['app_version'] ),
134+
'app.debug' => \DI\value( $config['app_debug'] ),
135+
'app.env' => \DI\factory( 'wp_get_environment_type' ),
136+
'app.extend' => \DI\value( $config['extendable'] ),
137+
'app.id' => \DI\value( $config['app_id'] ),
138+
'app.module' => \DI\value( $config['app_module'] ),
139+
'app.type' => \DI\value( $config['app_type'] ),
140+
'app.uuid' => \DI\factory( 'wp_generate_uuid4' ),
141+
'app.ver' => \DI\value( $config['app_version'] ),
142+
'xwp.app.tag' => \DI\factory(
143+
static fn( string $tag, ContainerInterface $ctr ) =>
144+
\DI\string( $tag )->resolve( $ctr ),
145+
),
141146
);
142147

143148
if ( $config['app_file'] && 'plugin' === $config['app_type'] ) {

src/Decorators/Ajax_Action.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class Ajax_Action extends Action {
4242
/**
4343
* Nonce query var.
4444
*
45-
* @var bool|string|array<string,string>
45+
* @var array{0?:string,1?:string}
4646
*/
47-
protected bool|string|array $nonce;
47+
protected array $nonce;
4848

4949
/**
5050
* Capability required to perform the action.
@@ -111,7 +111,7 @@ public function __construct(
111111
) {
112112
$this->action = $action;
113113
$this->prefix = $prefix;
114-
$this->nonce = $nonce;
114+
$this->nonce = $this->parse_nonce( $nonce );
115115
$this->cap = $cap;
116116
$this->vars = $vars;
117117
$this->hooks = $public ? array( 'wp_ajax_nopriv', 'wp_ajax' ) : array( 'wp_ajax' );
@@ -262,7 +262,7 @@ private function fire_guard_cb( string $type ): void {
262262
}
263263

264264
private function nonce_check(): bool {
265-
[ $arg, $action ] = $this->get_nonce_args();
265+
[ $action, $arg ] = $this->nonce;
266266

267267
return \check_ajax_referer( $action, $arg, false );
268268
}
@@ -282,20 +282,25 @@ private function cap_check(): bool {
282282
}
283283

284284
/**
285-
* Get the nonce arguments.
285+
* Parse the nonce parameter.
286286
*
287-
* @return array{0: string|false, 1: string}
287+
* @param bool|string|array{0:string,1:string}|array<string,string> $nonce Nonce parameter.
288+
* @return array{0?:string,1?:string}
288289
*/
289-
private function get_nonce_args(): array {
290-
$query_arg = match ( true ) {
291-
\is_array( $this->nonce ) => \key( $this->nonce ),
292-
\is_string( $this->nonce ) => $this->nonce,
293-
default => false,
294-
};
295-
$action = \is_array( $this->nonce )
296-
? \current( $this->nonce )
297-
: "{$this->prefix}_{$this->action}";
290+
private function parse_nonce( bool|string|array $nonce ): array {
291+
if ( ! $nonce ) {
292+
return array();
293+
}
294+
295+
if ( ! \is_array( $nonce ) ) {
296+
return array(
297+
"{$this->prefix}_{$this->action}",
298+
\is_string( $nonce ) ? $nonce : false,
299+
);
300+
}
298301

299-
return array( $query_arg, $action );
302+
return ! \array_is_list( $nonce )
303+
? array( \current( $nonce ), \key( $nonce ) )
304+
: $nonce;
300305
}
301306
}

src/Decorators/Dynamic_Filter.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,24 @@ public function get_data(): array {
8686
return $data;
8787
}
8888

89-
/**
90-
* Process variables.
91-
*
92-
* @param string|array<string>|callable():array<string> $vars Variables to mix into the tag.
93-
* @return array<int,string>|array<string,string>
94-
*/
95-
private function process_vars( string|callable|array $vars ): array {
96-
if ( \is_callable( $vars ) ) {
97-
return $vars();
98-
}
89+
public function with_reflector( Reflector $r ): static {
90+
$this->args ??= $r->getNumberOfParameters() - 1;
9991

100-
if ( \is_string( $vars ) && $this->container->has( $vars ) ) {
101-
return $this->container->get( $vars );
92+
return parent::with_reflector( $r );
93+
}
94+
95+
public function load_hook( ?string $tag = null ): bool {
96+
$res = true;
97+
98+
foreach ( $this->parse_vars( $this->raw_vars ) as $var => $param ) {
99+
$tag = $this->resolve_tag( $this->tag, array( $var ) );
100+
101+
$this->extra[ $tag ] = $param;
102+
103+
$res = $res && parent::load_hook( $tag );
102104
}
103105

104-
return $vars;
106+
return $res;
105107
}
106108

107109
/**
@@ -122,31 +124,29 @@ protected function parse_vars( string|callable|array $vars ): array {
122124
return $parsed;
123125
}
124126

125-
public function with_reflector( Reflector $r ): static {
126-
$this->args ??= $r->getNumberOfParameters() - 1;
127-
128-
return parent::with_reflector( $r );
129-
}
130-
131-
public function load_hook( ?string $tag = null ): bool {
132-
$res = true;
133-
134-
foreach ( $this->parse_vars( $this->raw_vars ) as $var => $param ) {
135-
$tag = $this->resolve_tag( $this->tag, array( $var ) );
136-
137-
$this->extra[ $tag ] = $param;
138-
139-
$res = $res && parent::load_hook( $tag );
140-
}
141-
142-
return $res;
143-
}
144-
145127
protected function get_cb_args( array $args ): array {
146128
$args = parent::get_cb_args( $args );
147129

148130
$args[] = $this->extra[ $this->current() ];
149131

150132
return $args;
151133
}
134+
135+
/**
136+
* Process variables.
137+
*
138+
* @param string|array<string>|callable():array<string> $vars Variables to mix into the tag.
139+
* @return array<int,string>|array<string,string>
140+
*/
141+
private function process_vars( string|callable|array $vars ): array {
142+
if ( \is_callable( $vars ) ) {
143+
return $vars();
144+
}
145+
146+
if ( \is_string( $vars ) && $this->container->has( $vars ) ) {
147+
return $this->container->get( $vars );
148+
}
149+
150+
return $vars;
151+
}
152152
}

src/Decorators/Filter.php

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,52 @@ public function get_container(): Container {
148148
return $this->container ??= $this->get_handler()->get_container();
149149
}
150150

151+
public function with_reflector( Reflector $r ): static {
152+
$this->args ??= $r->getNumberOfParameters();
153+
$this->method ??= $r->getName();
154+
155+
return parent::with_reflector( $r );
156+
}
157+
158+
public function can_load(): bool {
159+
return parent::can_load() && ( $this->get_handler()->is_lazy() || $this->get_handler()->is_loaded() );
160+
}
161+
162+
public function load(): bool {
163+
if ( $this->loaded ) {
164+
return true;
165+
}
166+
167+
if ( ! $this->init_handler( Can_Handle::INIT_LAZY ) ) {
168+
return false;
169+
}
170+
171+
$this->loaded = $this->load_hook();
172+
$this->init_hook ??= \current_action();
173+
174+
return $this->loaded;
175+
}
176+
177+
public function invoke( mixed ...$args ): mixed {
178+
if (
179+
! $this->init_handler( Can_Handle::INIT_JIT ) ||
180+
! parent::can_load() ||
181+
( $this->cb_valid( self::INV_ONCE ) && $this->fired ) ||
182+
( $this->cb_valid( self::INV_LOOPED ) && $this->firing )
183+
) {
184+
return $args[0] ?? null;
185+
}
186+
187+
try {
188+
return $this->fire_hook( ...$args );
189+
} catch ( \Throwable $e ) {
190+
return $this->handle_exception( $e, $args[0] ?? null );
191+
} finally {
192+
$this->firing = false;
193+
++$this->fired;
194+
}
195+
}
196+
151197
/**
152198
* Get the type of hook.
153199
*
@@ -168,17 +214,6 @@ protected function current(): string {
168214
return $cb();
169215
}
170216

171-
public function with_reflector( Reflector $r ): static {
172-
$this->args ??= $r->getNumberOfParameters();
173-
$this->method ??= $r->getName();
174-
175-
return parent::with_reflector( $r );
176-
}
177-
178-
public function can_load(): bool {
179-
return parent::can_load() && ( $this->get_handler()->is_lazy() || $this->get_handler()->is_loaded() );
180-
}
181-
182217
protected function init_handler( string $strategy ): bool {
183218
if ( $this->get_handler()->is_loaded() ) {
184219
return true;
@@ -208,21 +243,6 @@ protected function get_target(): array {
208243
: array( $this, 'invoke' );
209244
}
210245

211-
public function load(): bool {
212-
if ( $this->loaded ) {
213-
return true;
214-
}
215-
216-
if ( ! $this->init_handler( Can_Handle::INIT_LAZY ) ) {
217-
return false;
218-
}
219-
220-
$this->loaded = $this->load_hook();
221-
$this->init_hook ??= \current_action();
222-
223-
return $this->loaded;
224-
}
225-
226246
/**
227247
* Loads the hook.
228248
*
@@ -238,26 +258,6 @@ protected function load_hook( ?string $tag = null ): bool {
238258
);
239259
}
240260

241-
public function invoke( mixed ...$args ): mixed {
242-
if (
243-
! $this->init_handler( Can_Handle::INIT_JIT ) ||
244-
! parent::can_load() ||
245-
( $this->cb_valid( self::INV_ONCE ) && $this->fired ) ||
246-
( $this->cb_valid( self::INV_LOOPED ) && $this->firing )
247-
) {
248-
return $args[0] ?? null;
249-
}
250-
251-
try {
252-
return $this->fire_hook( ...$args );
253-
} catch ( \Throwable $e ) {
254-
return $this->handle_exception( $e, $args[0] ?? null );
255-
} finally {
256-
$this->firing = false;
257-
++$this->fired;
258-
}
259-
}
260-
261261
/**
262262
* Fire the hook.
263263
*

0 commit comments

Comments
 (0)