1010
1111use WP_Query ;
1212
13+ use function Mantle \Support \Helpers \terminate_request ;
14+
1315/**
1416 * Path Dispatch
17+ *
18+ * @phpstan-type PathArgs array{
19+ * path?: string,
20+ * callback?: callable,
21+ * action?: string,
22+ * rewrite?: array{
23+ * rule: string,
24+ * redirect?: string,
25+ * position?: 'top'|'bottom',
26+ * query_vars?: string|array<string>
27+ * },
28+ * template?: string
29+ * }
1530 */
1631class Path_Dispatch {
1732
1833 /**
1934 * Query vars that should be allowed.
2035 *
21- * @var array
36+ * @var string[]
2237 */
23- public $ qv = [ 'dispatch ' ];
38+ public array $ qv = [ 'dispatch ' ];
2439
2540 /**
2641 * Array of basic paths.
2742 *
28- * @var array
43+ * @var array<string, PathArgs>
2944 */
30- public $ basic_paths = [];
45+ public array $ basic_paths = [];
3146
3247 /**
3348 * Array of rewrite paths.
3449 *
35- * @var array
50+ * @var array<string, PathArgs>
3651 */
37- public $ rewrite_paths = [];
52+ public array $ rewrite_paths = [];
3853
3954 /**
4055 * Instance of this class.
4156 *
42- * @var Path_Dispatch
57+ * @var Path_Dispatch|null
4358 */
44- private static $ instance ;
59+ private static ? Path_Dispatch $ instance ;
4560
4661 /**
4762 * Don't allow __clone.
@@ -62,17 +77,18 @@ public function __wakeup() {
6277 *
6378 * @return Path_Dispatch
6479 */
65- public static function instance () {
80+ public static function instance (): Path_Dispatch {
6681 if ( ! isset ( self ::$ instance ) ) {
6782 self ::$ instance = new Path_Dispatch ();
6883 }
84+
6985 return self ::$ instance ;
7086 }
7187
7288 /**
7389 * Clear the instance of this class.
7490 */
75- public static function clear_instance () {
91+ public static function clear_instance (): void {
7692 self ::$ instance = null ;
7793 }
7894
@@ -114,8 +130,9 @@ protected function __construct() {
114130 * @see http://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
115131 * }
116132 * }
133+ * @phpstan-param PathArgs|string $args
117134 */
118- public function add_path ( $ args = [] ) {
135+ public function add_path ( array | string $ args = [] ): void {
119136 if ( is_string ( $ args ) && ! empty ( $ args ) ) {
120137 $ args = [
121138 'path ' => $ args ,
@@ -143,11 +160,12 @@ public function add_path( $args = [] ) {
143160 /**
144161 * Add multiple paths in one call.
145162 *
146- * @see Path_Dispatch::add_path
163+ * @see Path_Dispatch::add_path()
147164 *
148165 * @param array $paths An array of arrays that would be passed to add_path.
166+ * @phpstan-param array<PathArgs> $paths
149167 */
150- public function add_paths ( $ paths ) {
168+ public function add_paths ( array $ paths ): void {
151169 foreach ( $ paths as $ path ) {
152170 $ this ->add_path ( $ path );
153171 }
@@ -156,17 +174,17 @@ public function add_paths( $paths ) {
156174 /**
157175 * Add the class query var "dispatch" as well as any others added through add_path.
158176 *
159- * @param array $qv The current query vars.
160- * @return array The modified query vars.
177+ * @param string[] $qv The current query vars.
178+ * @return string[] The modified query vars.
161179 */
162- public function add_query_var ( $ qv ) {
180+ public function add_query_var ( array $ qv ): array {
163181 return array_merge ( $ qv , $ this ->qv );
164182 }
165183
166184 /**
167185 * Add rewrite rules for our dispatched paths.
168186 */
169- public function add_rewrite_rules () {
187+ public function add_rewrite_rules (): void {
170188 if ( ! empty ( $ this ->basic_paths ) ) {
171189 $ slugs = array_map ( 'preg_quote ' , array_keys ( $ this ->basic_paths ) );
172190 $ slugs = implode ( '| ' , $ slugs );
@@ -194,9 +212,9 @@ public function add_rewrite_rules() {
194212 *
195213 * @param WP_Query $query The WP_Query instance.
196214 */
197- public function dispatch_path ( &$ query ) {
215+ public function dispatch_path ( &$ query ): void {
198216 $ path = get_query_var ( 'dispatch ' );
199- if ( $ query ->is_main_query () && $ path ) {
217+ if ( $ query ->is_main_query () && $ path && is_string ( $ path ) ) {
200218 $ args = [];
201219
202220 if ( ! empty ( $ this ->basic_paths [ $ path ] ) ) {
@@ -213,8 +231,23 @@ public function dispatch_path( &$query ) {
213231
214232 if ( ! empty ( $ args ['template ' ] ) ) {
215233 get_template_part ( 'dispatch ' , $ args ['template ' ] );
216- exit ;
234+
235+ $ this ->terminate_request ();
217236 }
218237 }
219238 }
239+
240+ /**
241+ * Terminate the request.
242+ *
243+ * @param int $exit_status Exit status code.
244+ * @return never
245+ */
246+ protected function terminate_request ( int $ exit_status = 0 ): never {
247+ if ( function_exists ( 'Mantle\Support\Helpers\terminate_request ' ) ) {
248+ \Mantle \Support \Helpers \terminate_request ( $ exit_status );
249+ }
250+
251+ exit ( (int ) $ exit_status );
252+ }
220253}
0 commit comments