-
Notifications
You must be signed in to change notification settings - Fork 442
/
NodeResolver.php
599 lines (509 loc) · 20.2 KB
/
NodeResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<?php
namespace WPGraphQL\Data;
use Exception;
use GraphQL\Deferred;
use WP;
use WP_Post;
use WPGraphQL\AppContext;
use GraphQL\Error\UserError;
class NodeResolver {
/**
* @var \WP
*/
protected $wp;
/**
* @var \WPGraphQL\AppContext
*/
protected $context;
/**
* NodeResolver constructor.
*
* @param \WPGraphQL\AppContext $context
*
* @return void
*/
public function __construct( AppContext $context ) {
global $wp;
$this->wp = $wp;
$this->context = $context;
}
/**
* Given a Post object, validates it before returning it.
*
* @param \WP_Post $post
*
* @return \WP_Post|null
*/
public function validate_post( WP_Post $post ) {
if ( isset( $this->wp->query_vars['post_type'] ) && ( $post->post_type !== $this->wp->query_vars['post_type'] ) ) {
return null;
}
if ( ! $this->is_valid_node_type( 'ContentNode' ) ) {
return null;
}
/**
* Disabling the following code for now, since add_rewrite_uri() would cause a request to direct to a different valid permalink.
*/
/* phpcs:disable
if ( ! isset( $this->wp->query_vars['uri'] ) ) {
return $post;
}
$permalink = get_permalink( $post );
$parsed_path = $permalink ? wp_parse_url( $permalink, PHP_URL_PATH ) : null;
$trimmed_path = $parsed_path ? rtrim( ltrim( $parsed_path, '/' ), '/' ) : null;
$uri_path = rtrim( ltrim( $this->wp->query_vars['uri'], '/' ), '/' );
if ( $trimmed_path !== $uri_path ) {
return null;
}
phpcs:enable */
return $post;
}
/**
* Given a Term object, validates it before returning it.
*
* @param \WP_Term $term
*
* @return \WP_Term|null
*/
public function validate_term( \WP_Term $term ) {
if ( ! $this->is_valid_node_type( 'TermNode' ) ) {
return null;
}
if ( isset( $this->wp->query_vars['taxonomy'] ) && $term->taxonomy !== $this->wp->query_vars['taxonomy'] ) {
return null;
}
return $term;
}
/**
* Given the URI of a resource, this method attempts to resolve it and return the
* appropriate related object
*
* @param string $uri The path to be used as an identifier for the
* resource.
* @param mixed|array|string $extra_query_vars Any extra query vars to consider
*
* @return mixed
* @throws \Exception
*/
public function resolve_uri( string $uri, $extra_query_vars = '' ) {
/**
* When this filter return anything other than null, it will be used as a resolved node
* and the execution will be skipped.
*
* This is to be used in extensions to resolve their own nodes which might not use
* WordPress permalink structure.
*
* @param mixed|null $node The node, defaults to nothing.
* @param string $uri The uri being searched.
* @param \WPGraphQL\AppContext $content The app context.
* @param \WP $wp WP object.
* @param mixed|array|string $extra_query_vars Any extra query vars to consider.
*/
$node = apply_filters( 'graphql_pre_resolve_uri', null, $uri, $this->context, $this->wp, $extra_query_vars );
if ( ! empty( $node ) ) {
return $node;
}
/**
* Try to resolve the URI with WP_Query.
*
* This is the way WordPress native permalinks are resolved.
*
* @see \WP::main()
*/
// Parse the URI and sets the $wp->query_vars property.
$uri = $this->parse_request( $uri, $extra_query_vars );
/**
* If the URI is '/', we can resolve it now.
*
* We don't rely on $this->parse_request(), since the home page doesn't get a rewrite rule.
*/
if ( '/' === $uri ) {
return $this->resolve_home_page();
}
/**
* Filter the query class used to resolve the URI. By default this is WP_Query.
*
* This can be used by Extensions which use a different query class to resolve data.
*
* @param class-string $query_class The query class used to resolve the URI. Defaults to WP_Query.
* @param ?string $uri The uri being searched.
* @param \WPGraphQL\AppContext $content The app context.
* @param \WP $wp WP object.
* @param mixed|array|string $extra_query_vars Any extra query vars to consider.
*/
$query_class = apply_filters( 'graphql_resolve_uri_query_class', 'WP_Query', $uri, $this->context, $this->wp, $extra_query_vars );
if ( ! class_exists( $query_class ) ) {
throw new UserError(
sprintf(
/* translators: %s: The query class used to resolve the URI */
__( 'The query class %s used to resolve the URI does not exist.', 'wp-graphql' ),
$query_class
)
);
}
/** @var \WP_Query $query */
$query = new $query_class( $this->wp->query_vars );
$queried_object = $query->get_queried_object();
/**
* When this filter return anything other than null, it will be used as a resolved node
* and the execution will be skipped.
*
* This is to be used in extensions to resolve their own nodes which might not use
* WordPress permalink structure.
*
* It differs from 'graphql_pre_resolve_uri' in that it has been called after the query has been run using the query vars.
*
* @param mixed|null $node The node, defaults to nothing.
* @param ?string $uri The uri being searched.
* @param \WP_Term|\WP_Post_Type|\WP_Post|\WP_User|null $queried_object The queried object, if WP_Query returns one.
* @param \WP_Query $query The query object.
* @param \WPGraphQL\AppContext $content The app context.
* @param \WP $wp WP object.
* @param mixed|array|string $extra_query_vars Any extra query vars to consider.
*/
$node = apply_filters( 'graphql_resolve_uri', null, $uri, $queried_object, $query, $this->context, $this->wp, $extra_query_vars );
if ( ! empty( $node ) ) {
return $node;
}
// Resolve Post Objects.
if ( $queried_object instanceof WP_Post ) {
// If Page for Posts is set, we need to return the Page archive, not the page.
if ( $query->is_posts_page ) {
// If were intentionally querying for a something other than a ContentType, we need to return null instead of the archive.
if ( ! $this->is_valid_node_type( 'ContentType' ) ) {
return null;
}
$post_type_object = get_post_type_object( 'post' );
if ( ! $post_type_object ) {
return null;
}
return ! empty( $post_type_object->name ) ? $this->context->get_loader( 'post_type' )->load_deferred( $post_type_object->name ) : null;
}
// Validate the post before returning it.
if ( ! $this->validate_post( $queried_object ) ) {
return null;
}
return ! empty( $queried_object->ID ) ? $this->context->get_loader( 'post' )->load_deferred( $queried_object->ID ) : null;
}
// Resolve Terms.
if ( $queried_object instanceof \WP_Term ) {
// Validate the term before returning it.
if ( ! $this->validate_term( $queried_object ) ) {
return null;
}
return ! empty( $queried_object->term_id ) ? $this->context->get_loader( 'term' )->load_deferred( $queried_object->term_id ) : null;
}
// Resolve Post Types.
if ( $queried_object instanceof \WP_Post_Type ) {
// Bail if we're explictly requesting a different GraphQL type.
if ( ! $this->is_valid_node_type( 'ContentType' ) ) {
return null;
}
return ! empty( $queried_object->name ) ? $this->context->get_loader( 'post_type' )->load_deferred( $queried_object->name ) : null;
}
// Resolve Users
if ( $queried_object instanceof \WP_User ) {
// Bail if we're explictly requesting a different GraphQL type.
if ( ! $this->is_valid_node_type( 'User' ) ) {
return null;
}
return ! empty( $queried_object->ID ) ? $this->context->get_loader( 'user' )->load_deferred( $queried_object->ID ) : null;
}
/**
* This filter provides a fallback for resolving nodes that were unable to be resolved by NodeResolver::resolve_uri.
*
* This can be used by Extensions to resolve edge cases that are not handled by the core NodeResolver.
*
* @param mixed|null $node The node, defaults to nothing.
* @param ?string $uri The uri being searched.
* @param \WP_Term|\WP_Post_Type|\WP_Post|\WP_User|null $queried_object The queried object, if WP_Query returns one.
* @param \WP_Query $query The query object.
* @param \WPGraphQL\AppContext $content The app context.
* @param \WP $wp WP object.
* @param mixed|array|string $extra_query_vars Any extra query vars to consider.
*/
return apply_filters( 'graphql_post_resolve_uri', $node, $uri, $queried_object, $query, $this->context, $this->wp, $extra_query_vars );
}
/**
* Parses a URL to produce an array of query variables.
*
* Mimics WP::parse_request()
*
* @param string $uri
* @param array|string $extra_query_vars
*
* @return string|null The parsed uri.
*/
public function parse_request( string $uri, $extra_query_vars = '' ) {
// Attempt to parse the provided URI.
$parsed_url = wp_parse_url( $uri );
if ( false === $parsed_url ) {
graphql_debug( __( 'Cannot parse provided URI', 'wp-graphql' ), [
'uri' => $uri,
] );
return null;
}
// Bail if external URI.
if ( isset( $parsed_url['host'] ) ) {
$site_url = wp_parse_url( site_url() );
$home_url = wp_parse_url( home_url() );
/**
* @var array $home_url
* @var array $site_url
*/
if ( ! in_array(
$parsed_url['host'],
[
$site_url['host'],
$home_url['host'],
],
true
) ) {
graphql_debug( __( 'Cannot return a resource for an external URI', 'wp-graphql' ), [
'uri' => $uri,
] );
return null;
}
}
if ( isset( $parsed_url['query'] ) && ( empty( $parsed_url['path'] ) || '/' === $parsed_url['path'] ) ) {
$uri = $parsed_url['query'];
} elseif ( isset( $parsed_url['path'] ) ) {
$uri = $parsed_url['path'];
}
/**
* Follows pattern from WP::parse_request()
*
* @see https://github.com/WordPress/wordpress-develop/blob/6.0.2/src/wp-includes/class-wp.php#L135
*/
global $wp_rewrite;
$this->wp->query_vars = [];
$post_type_query_vars = [];
if ( is_array( $extra_query_vars ) ) {
$this->wp->query_vars = &$extra_query_vars;
} elseif ( ! empty( $extra_query_vars ) ) {
parse_str( $extra_query_vars, $this->wp->extra_query_vars );
}
// Set uri to Query vars.
$this->wp->query_vars['uri'] = $uri;
// Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
// Fetch the rewrite rules.
$rewrite = $wp_rewrite->wp_rewrite_rules();
if ( ! empty( $rewrite ) ) {
// If we match a rewrite rule, this will be cleared.
$error = '404';
$this->wp->did_permalink = true;
$pathinfo = ! empty( $uri ) ? $uri : '';
list( $pathinfo ) = explode( '?', $pathinfo );
$pathinfo = str_replace( '%', '%25', $pathinfo );
list( $req_uri ) = explode( '?', $pathinfo );
$home_path = parse_url( home_url(), PHP_URL_PATH ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
$home_path_regex = '';
if ( is_string( $home_path ) && '' !== $home_path ) {
$home_path = trim( $home_path, '/' );
$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
}
/*
* Trim path info from the end and the leading home path from the front.
* For path info requests, this leaves us with the requesting filename, if any.
* For 404 requests, this leaves us with the requested permalink.
*/
$query = '';
$matches = null;
$req_uri = str_replace( $pathinfo, '', $req_uri );
$req_uri = trim( $req_uri, '/' );
$pathinfo = trim( $pathinfo, '/' );
if ( ! empty( $home_path_regex ) ) {
$req_uri = preg_replace( $home_path_regex, '', $req_uri );
$req_uri = trim( $req_uri, '/' ); // @phpstan-ignore-line
$pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
$pathinfo = trim( $pathinfo, '/' ); // @phpstan-ignore-line
}
// The requested permalink is in $pathinfo for path info requests and
// $req_uri for other requests.
if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) {
$requested_path = $pathinfo;
} else {
// If the request uri is the index, blank it out so that we don't try to match it against a rule.
if ( $req_uri === $wp_rewrite->index ) {
$req_uri = '';
}
$requested_path = $req_uri;
}
$requested_file = $req_uri;
$this->wp->request = $requested_path;
// Look for matches.
$request_match = $requested_path;
if ( empty( $request_match ) ) {
// An empty request could only match against ^$ regex
if ( isset( $rewrite['$'] ) ) {
$this->wp->matched_rule = '$';
$query = $rewrite['$'];
$matches = [ '' ];
}
} else {
foreach ( (array) $rewrite as $match => $query ) {
// If the requested file is the anchor of the match, prepend it to the path info.
if ( ! empty( $requested_file ) && strpos( $match, $requested_file ) === 0 && $requested_file !== $requested_path ) {
$request_match = $requested_file . '/' . $requested_path;
}
if (
preg_match( "#^$match#", $request_match, $matches ) ||
preg_match( "#^$match#", urldecode( $request_match ), $matches )
) {
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
// This is a verbose page match, let's check to be sure about it.
$page = get_page_by_path( $matches[ $varmatch[1] ] ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_page_by_path_get_page_by_path
if ( ! $page ) {
continue;
}
$post_status_obj = get_post_status_object( $page->post_status );
if (
( ! isset( $post_status_obj->public ) || ! $post_status_obj->public ) &&
( ! isset( $post_status_obj->protected ) || ! $post_status_obj->protected ) &&
( ! isset( $post_status_obj->private ) || ! $post_status_obj->private ) &&
( ! isset( $post_status_obj->exclude_from_search ) || $post_status_obj->exclude_from_search )
) {
continue;
}
}
// Got a match.
$this->wp->matched_rule = $match;
break;
}
}
}
if ( ! empty( $this->wp->matched_rule ) ) {
// Trim the query of everything up to the '?'.
$query = preg_replace( '!^.+\?!', '', $query );
// Substitute the substring matches into the query.
$query = addslashes( \WP_MatchesMapRegex::apply( $query, $matches ) ); // @phpstan-ignore-line
$this->wp->matched_query = $query;
// Parse the query.
parse_str( $query, $perma_query_vars );
// If we're processing a 404 request, clear the error var since we found something.
// @phpstan-ignore-next-line
if ( '404' == $error ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
unset( $error );
}
}
}
/**
* Filters the query variables allowed before processing.
*
* Allows (publicly allowed) query vars to be added, removed, or changed prior
* to executing the query. Needed to allow custom rewrite rules using your own arguments
* to work, or any other custom query variables you want to be publicly available.
*
* @since 1.5.0
*
* @param string[] $public_query_vars The array of allowed query variable names.
*/
$this->wp->public_query_vars = apply_filters( 'query_vars', $this->wp->public_query_vars );
foreach ( get_post_types( [ 'show_in_graphql' => true ], 'objects' ) as $post_type => $t ) {
/** @var \WP_Post_Type $t */
if ( $t->query_var ) {
$post_type_query_vars[ $t->query_var ] = $post_type;
}
}
foreach ( $this->wp->public_query_vars as $wpvar ) {
$parsed_query = [];
if ( isset( $parsed_url['query'] ) ) {
parse_str( $parsed_url['query'], $parsed_query );
}
if ( isset( $this->wp->extra_query_vars[ $wpvar ] ) ) {
$this->wp->query_vars[ $wpvar ] = $this->wp->extra_query_vars[ $wpvar ];
} elseif ( isset( $_GET[ $wpvar ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$this->wp->query_vars[ $wpvar ] = $_GET[ $wpvar ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.NonceVerification.Recommended
} elseif ( isset( $perma_query_vars[ $wpvar ] ) ) {
$this->wp->query_vars[ $wpvar ] = $perma_query_vars[ $wpvar ];
} elseif ( isset( $parsed_query[ $wpvar ] ) ) {
$this->wp->query_vars[ $wpvar ] = $parsed_query[ $wpvar ];
}
if ( ! empty( $this->wp->query_vars[ $wpvar ] ) ) {
if ( ! is_array( $this->wp->query_vars[ $wpvar ] ) ) {
$this->wp->query_vars[ $wpvar ] = (string) $this->wp->query_vars[ $wpvar ];
} else {
foreach ( $this->wp->query_vars[ $wpvar ] as $vkey => $v ) {
if ( is_scalar( $v ) ) {
$this->wp->query_vars[ $wpvar ][ $vkey ] = (string) $v;
}
}
}
if ( isset( $post_type_query_vars[ $wpvar ] ) ) {
$this->wp->query_vars['post_type'] = $post_type_query_vars[ $wpvar ];
$this->wp->query_vars['name'] = $this->wp->query_vars[ $wpvar ];
}
}
}
// Convert urldecoded spaces back into '+'.
foreach ( get_taxonomies( [ 'show_in_graphql' => true ], 'objects' ) as $taxonomy => $t ) {
if ( $t->query_var && isset( $this->wp->query_vars[ $t->query_var ] ) ) {
$this->wp->query_vars[ $t->query_var ] = str_replace( ' ', '+', $this->wp->query_vars[ $t->query_var ] );
}
}
// Limit publicly queried post_types to those that are publicly_queryable
if ( isset( $this->wp->query_vars['post_type'] ) ) {
$queryable_post_types = get_post_types( [ 'show_in_graphql' => true ] );
if ( ! is_array( $this->wp->query_vars['post_type'] ) ) {
if ( ! in_array( $this->wp->query_vars['post_type'], $queryable_post_types, true ) ) {
unset( $this->wp->query_vars['post_type'] );
}
} else {
$this->wp->query_vars['post_type'] = array_intersect( $this->wp->query_vars['post_type'], $queryable_post_types );
}
}
// Resolve conflicts between posts with numeric slugs and date archive queries.
$this->wp->query_vars = wp_resolve_numeric_slug_conflicts( $this->wp->query_vars );
foreach ( (array) $this->wp->private_query_vars as $var ) {
if ( isset( $this->wp->extra_query_vars[ $var ] ) ) {
$this->wp->query_vars[ $var ] = $this->wp->extra_query_vars[ $var ];
}
}
if ( isset( $error ) ) {
$this->wp->query_vars['error'] = $error;
}
/**
* Filters the array of parsed query variables.
*
* @param array $query_vars The array of requested query variables.
*
* @since 2.1.0
*/
$this->wp->query_vars = apply_filters( 'request', $this->wp->query_vars );
// We don't need the GraphQL args anymore.
unset( $this->wp->query_vars['graphql'] );
do_action_ref_array( 'parse_request', [ &$this->wp ] );
return $uri;
}
/**
* Checks if the node type is set in the query vars and, if so, whether it matches the node type.
*/
protected function is_valid_node_type( string $node_type ) : bool {
return ! isset( $this->wp->query_vars['nodeType'] ) || $this->wp->query_vars['nodeType'] === $node_type;
}
/**
* Resolves the home page.
*
* If the homepage is a static page, return the page, otherwise we return the Posts `ContentType`.
*
* @todo Replace `ContentType` with an `Archive` type.
*/
protected function resolve_home_page() : ?Deferred {
$page_id = get_option( 'page_on_front', 0 );
$show_on_front = get_option( 'show_on_front', 'posts' );
// If the homepage is a static page, return the page.
if ( 'page' === $show_on_front && ! empty( $page_id ) ) {
$page = get_post( $page_id );
if ( empty( $page ) ) {
return null;
}
return $this->context->get_loader( 'post' )->load_deferred( $page->ID );
}
// If the homepage is set to latest posts, we need to make sure not to resolve it when when for other types.
if ( ! $this->is_valid_node_type( 'ContentType' ) ) {
return null;
}
// We dont have an 'Archive' type, so we resolve to the ContentType.
return $this->context->get_loader( 'post_type' )->load_deferred( 'post' );
}
}