Skip to content

Commit 5c6800a

Browse files
committed
feat: Added context check fns
1 parent 2ad099f commit 5c6800a

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

phpcs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414
<severity>0</severity>
1515
</rule>
1616

17+
<file>config/</file>
1718
<file>src/</file>
19+
<file>wc-sync-core.php</file>
1820
</ruleset>
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
* @subpackage Dependency Injection
77
*/
88

9-
namespace XWP\DI;
10-
119
use Automattic\Jetpack\Constants;
1210

1311
/**
1412
* Determines execution context.
1513
*
1614
* @since 1.0.0
1715
*/
18-
final class Hook_Context {
16+
final class XWP_Context {
1917
/**
2018
* Frontend context.
2119
*/
@@ -120,6 +118,17 @@ public static function admin(): bool {
120118
return \is_admin() && ! self::ajax();
121119
}
122120

121+
/**
122+
* Check if the request is an admin request for a specific page.
123+
*
124+
* @param string $page The page to check.
125+
* @param string|null $type The post type to check.
126+
* @return bool
127+
*/
128+
public static function admin_page( string $page, ?string $type = null ): bool {
129+
return self::admin() && ( $GLOBALS['pagenow'] ?? '' ) === $page && ( ! $type || ( $GLOBALS['typenow'] ?? '' ) === $type );
130+
}
131+
123132
/**
124133
* Check if the request is an AJAX request.
125134
*
@@ -129,6 +138,16 @@ public static function ajax(): bool {
129138
return Constants::is_true( 'DOING_AJAX' );
130139
}
131140

141+
/**
142+
* Check if the request is an AJAX request for a specific action.
143+
*
144+
* @param string $action The action to check.
145+
* @return bool
146+
*/
147+
public static function ajax_action( string $action ): bool {
148+
return self::ajax() && \xwp_fetch_req_var( 'action', '' ) === $action;
149+
}
150+
132151
/**
133152
* Check if the request is a cron request.
134153
*

src/Decorators/Handler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ public function is_lazy(): bool {
284284
}
285285

286286
public function is_hookable(): bool {
287-
return null === $this->hookable && $this->check_context() || $this->hookable;
287+
if ( ! $this->check_context() ) {
288+
return false;
289+
}
290+
291+
return $this->hookable ?? true;
288292
}
289293
}

src/Decorators/Hook.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use DI\Container;
1313
use ReflectionClass;
1414
use ReflectionMethod;
15-
use XWP\DI\Hook_Context;
1615
use XWP\DI\Interfaces\Can_Hook;
16+
use XWP_Context;
1717

1818
/**
1919
* Base hook from which the action and filter decorators inherit.
@@ -111,7 +111,7 @@ public function can_load(): bool {
111111
* @return bool
112112
*/
113113
public function check_context(): bool {
114-
return Hook_Context::validate( $this->context );
114+
return XWP_Context::validate( $this->context );
115115
}
116116

117117
/**

0 commit comments

Comments
 (0)