forked from wp-graphql/wp-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSource.php
executable file
·860 lines (746 loc) · 24.6 KB
/
DataSource.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
<?php
namespace WPGraphQL\Data;
use GraphQL\Deferred;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Types;
/**
* Class DataSource
*
* This class serves as a factory for all the resolvers for queries and mutations. This layer of
* abstraction over the actual resolve functions allows easier, granular control over versioning as
* we can change big things behind the scenes if/when needed, and we just need to ensure the
* call to the DataSource method returns the expected data later on. This should make it easy
* down the road to version resolvers if/when changes to the WordPress API are rolled out.
*
* @package WPGraphQL\Data
* @since 0.0.4
*/
class DataSource {
/**
* Stores an array of node definitions
*
* @var array $node_definition
* @since 0.0.4
* @access protected
*/
protected static $node_definition;
/**
* Retrieves a WP_Comment object for the id that gets passed
*
* @param int $id ID of the comment we want to get the object for
*
* @return \WP_Comment object
* @throws UserError
* @since 0.0.5
* @access public
*/
public static function resolve_comment( $id ) {
$comment = \WP_Comment::get_instance( $id );
if ( empty( $comment ) ) {
throw new UserError( sprintf( __( 'No comment was found with ID %d', 'wp-graphql' ), absint( $id ) ) );
}
return $comment;
}
/**
* Retrieves a WP_Comment object for the ID that gets passed
*
* @param string $author_email The ID of the comment the comment author is associated with.
*
* @return array
* @throws
*/
public static function resolve_comment_author( $author_email ) {
global $wpdb;
$comment_author = $wpdb->get_row( $wpdb->prepare( "SELECT comment_author_email, comment_author, comment_author_url, comment_author_email from $wpdb->comments WHERE comment_author_email = %s LIMIT 1", esc_sql( $author_email ) ) );
$comment_author = ! empty( $comment_author ) ? ( array ) $comment_author : [];
$comment_author['is_comment_author'] = true;
return $comment_author;
}
/**
* Wrapper for the CommentsConnectionResolver class
*
* @param mixed object $source
* @param array $args Query args to pass to the connection resolver
* @param AppContext $context The context of the query to pass along
* @param ResolveInfo $info The ResolveInfo object
*
* @return mixed
* @since 0.0.5
* @throws \Exception
*/
public static function resolve_comments_connection( $source, array $args, $context, ResolveInfo $info ) {
$resolver = new CommentConnectionResolver();
return $resolver->resolve( $source, $args, $context, $info );
}
/**
* Returns an array of data about the plugin you are requesting
*
* @param string $name Name of the plugin you want info for
*
* @return null|array
* @throws \Exception
* @since 0.0.5
* @access public
*/
public static function resolve_plugin( $name ) {
// Puts input into a url friendly slug format.
$slug = sanitize_title( $name );
$plugin = null;
// The file may have not been loaded yet.
require_once ABSPATH . 'wp-admin/includes/plugin.php';
/**
* NOTE: This is missing must use and drop in plugins.
*/
$plugins = apply_filters( 'all_plugins', get_plugins() );
/**
* Loop through the plugins and find the matching one
*
* @since 0.0.5
*/
foreach ( $plugins as $path => $plugin_data ) {
if ( sanitize_title( $plugin_data['Name'] ) === $slug ) {
$plugin = $plugin_data;
$plugin['path'] = $path;
// Exit early when plugin is found.
break;
}
}
/**
* Return the plugin, or throw an exception
*/
if ( ! empty( $plugin ) ) {
return $plugin;
} else {
throw new UserError( sprintf( __( 'No plugin was found with the name %s', 'wp-graphql' ), $name ) );
}
}
/**
* Wrapper for PluginsConnectionResolver::resolve
*
* @param \WP_Post $source WP_Post object
* @param array $args Array of arguments to pass to reolve method
* @param AppContext $context AppContext object passed down
* @param ResolveInfo $info The ResolveInfo object
*
* @return array
* @since 0.0.5
* @access public
*/
public static function resolve_plugins_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
return PluginConnectionResolver::resolve( $source, $args, $context, $info );
}
/**
* Returns the post object for the ID and post type passed
*
* @param int $id ID of the post you are trying to retrieve
* @param string $post_type Post type the post is attached to
*
* @throws UserError
* @since 0.0.5
* @return \WP_Post
* @access public
*/
public static function resolve_post_object( $id, $post_type ) {
$post_object = \WP_Post::get_instance( $id );
if ( empty( $post_object ) ) {
throw new UserError( sprintf( __( 'No %1$s was found with the ID: %2$s', 'wp-graphql' ), $post_type, $id ) );
}
/**
* Mimic core functionality for templates, as seen here:
* https://github.com/WordPress/WordPress/blob/6fd8080e7ee7599b36d4528f72a8ced612130b8c/wp-includes/template-loader.php#L56
*/
if ( 'attachment' === $post_type ) {
remove_filter( 'the_content', 'prepend_attachment' );
}
/**
* Set the resolving post to the global $post. That way any filters that
* might be applied when resolving fields can rely on global post and
* post data being set up.
*/
$GLOBALS['post'] = $post_object;
setup_postdata( $post_object );
return $post_object;
}
/**
* Wrapper for PostObjectsConnectionResolver
*
* @param $source
* @param array $args Arguments to pass to the resolve method
* @param AppContext $context AppContext object to pass down
* @param ResolveInfo $info The ResolveInfo object
* @param string $post_type Post type of the post we are trying to resolve
*
* @return mixed
* @since 0.0.5
* @access public
* @throws \Exception
*/
public static function resolve_post_objects_connection( $source, array $args, AppContext $context, ResolveInfo $info, $post_type ) {
$resolver = new PostObjectConnectionResolver( $post_type );
return $resolver->resolve( $source, $args, $context, $info );
}
/**
* Gets the post type object from the post type name
*
* @param string $post_type Name of the post type you want to retrieve the object for
*
* @return \WP_Post_Type object
* @throws UserError
* @since 0.0.5
* @access public
*/
public static function resolve_post_type( $post_type ) {
/**
* Get the allowed_post_types
*/
$allowed_post_types = \WPGraphQL::get_allowed_post_types();
/**
* If the $post_type is one of the allowed_post_types
*/
if ( in_array( $post_type, $allowed_post_types, true ) ) {
return get_post_type_object( $post_type );
} else {
throw new UserError( sprintf( __( 'No post_type was found with the name %s', 'wp-graphql' ), $post_type ) );
}
}
/**
* Retrieves the taxonomy object for the name of the taxonomy passed
*
* @param string $taxonomy Name of the taxonomy you want to retrieve the taxonomy object for
*
* @return \WP_Taxonomy object
* @throws UserError
* @since 0.0.5
* @access public
*/
public static function resolve_taxonomy( $taxonomy ) {
/**
* Get the allowed_taxonomies
*/
$allowed_taxonomies = \WPGraphQL::get_allowed_taxonomies();
/**
* If the $post_type is one of the allowed_post_types
*/
if ( in_array( $taxonomy, $allowed_taxonomies, true ) ) {
return get_taxonomy( $taxonomy );
} else {
throw new UserError( sprintf( __( 'No taxonomy was found with the name %s', 'wp-graphql' ), $taxonomy ) );
}
}
/**
* Get the term object for a term
*
* @param int $id ID of the term you are trying to retrieve the object for
* @param string $taxonomy Name of the taxonomy the term is in
*
* @return mixed
* @throws UserError
* @since 0.0.5
* @access public
*/
public static function resolve_term_object( $id, $taxonomy ) {
$term_object = \WP_Term::get_instance( $id, $taxonomy );
if ( empty( $term_object ) ) {
throw new UserError( sprintf( __( 'No %1$s was found with the ID: %2$s', 'wp-graphql' ), $taxonomy, $id ) );
}
return $term_object;
}
/**
* Wrapper for TermObjectConnectionResolver::resolve
*
* @param $source
* @param array $args Array of args to be passed to the resolve method
* @param AppContext $context The AppContext object to be passed down
* @param ResolveInfo $info The ResolveInfo object
* @param \WP_Taxonomy $taxonomy The WP_Taxonomy object of the taxonomy the term is connected to
*
* @return array
* @since 0.0.5
* @access public
* @throws \Exception
*/
public static function resolve_term_objects_connection( $source, array $args, $context, ResolveInfo $info, $taxonomy ) {
$resolver = new TermObjectConnectionResolver( $taxonomy );
return $resolver->resolve( $source, $args, $context, $info );
}
/**
* Retrieves the theme object for the theme you are looking for
*
* @param string $stylesheet Directory name for the theme.
*
* @return \WP_Theme object
* @throws UserError
* @since 0.0.5
* @access public
*/
public static function resolve_theme( $stylesheet ) {
$theme = wp_get_theme( $stylesheet );
if ( $theme->exists() ) {
return $theme;
} else {
throw new UserError( sprintf( __( 'No theme was found with the stylesheet: %s', 'wp-graphql' ), $stylesheet ) );
}
}
/**
* Retrieves and formats theme modification data
*
* @param array|null $theme_mods - array of raw theme modification data
* @return array|null
*/
public static function resolve_theme_mods_data() {
/**
* Output array
*/
$theme_mod_data = [];
/**
* Loop through raw active theme mods array and format theme mod data
*/
$theme_mods = array_merge(
get_theme_mods(),
[
'background_image' => get_theme_mod( 'background_image', get_theme_support( 'custom-background', 'default-image' ) ),
'background_color' => get_theme_mod( 'background_color', get_theme_support( 'custom-background', 'default-color' ) ),
]
);
foreach( $theme_mods as $mod_name => $mod_data ){
if( gettype($mod_name) === 'integer' ) continue; // Skip mods without keys
if( $mod_name === 'sidebars_widgets' ) continue; // Skip sidebars
switch( $mod_name ) {
/**
* Custom CSS Post Id
*/
case 'custom_css_post_id':
$theme_mod_data[ $mod_name ] = absint($mod_data);
break;
/**
* Background
*/
case 'background_preset':
case 'background_size':
case 'background_repeat':
case 'background_attachment':
$key = str_replace('background_', '', $mod_name );
$theme_mod_data[ 'background' ][ $key ] = $mod_data;
break;
case 'background_image':
$theme_mod_data[ 'background' ]['id'] = attachment_url_to_postid( (string) $mod_data );
break;
case 'background_color':
$theme_mod_data[ $mod_name ] = (string) $mod_data;
break;
/**
* Custom Logo
*/
case 'custom_logo':
$theme_mod_data[ $mod_name ] = absint( $mod_data );
break;
/**
* Header Image
*/
case 'header_image_data':
if ( ! isset( $theme_mod_data[ 'header_image' ] ) ) $theme_mod_data[ 'header_image' ] = [];
$theme_mod_data[ 'header_image' ] += get_object_vars( $mod_data );
break;
case 'header_image':
\Codeception\Util\Debug::debug( attachment_url_to_postid( $mod_data ) );
$theme_mod_data[ 'header_image' ]['id'] = attachment_url_to_postid( (string) $mod_data );
break;
/**
* Nav Menu Locations and Custom Mods
*/
default:
$theme_mod_data[ $mod_name ] = $mod_data;
}
}
return $theme_mod_data;
}
/**
* Wrapper for the ThemesConnectionResolver::resolve method
*
* @param $source
* @param array $args Passes an array of arguments to the resolve method
* @param AppContext $context The AppContext object to be passed down
* @param ResolveInfo $info The ResolveInfo object
*
* @return array
* @since 0.0.5
* @access public
*/
public static function resolve_themes_connection( $source, array $args, $context, ResolveInfo $info ) {
return ThemeConnectionResolver::resolve( $source, $args, $context, $info );
}
/**
* Gets the user object for the user ID specified
*
* @param int $id ID of the user you want the object for
*
* @return Deferred
* @since 0.0.5
* @access public
*/
public static function resolve_user( $id ) {
Loader::addOne( 'user', $id );
$loader = function () use ( $id ) {
Loader::loadBuffered( 'user' );
return Loader::loadOne( 'user', $id );
};
return new Deferred( $loader );
}
/**
* Wrapper for the UsersConnectionResolver::resolve method
*
* @param $source
* @param array $args Array of args to be passed down to the resolve method
* @param AppContext $context The AppContext object to be passed down
* @param ResolveInfo $info The ResolveInfo object
*
* @return array
* @since 0.0.5
* @access public
* @throws \Exception
*/
public static function resolve_users_connection( $source, array $args, $context, ResolveInfo $info ) {
return UserConnectionResolver::resolve( $source, $args, $context, $info );
}
/**
* Returns an array of data about the user role you are requesting
*
* @param string $name Name of the user role you want info for
*
* @return null|array
* @throws \Exception
* @since 0.0.30
* @access public
*/
public static function resolve_user_role( $name ) {
$role = get_role( $name );
if ( null === $role ) {
throw new UserError( sprintf( __( 'No user role was found with the name %s', 'wp-graphql' ), $name ) );
} else {
$role = (array) $role;
$role['id'] = $name;
return $role;
}
}
/**
* Resolve the connection data for user roles
*
* @param array $source The Query results
* @param array $args The query arguments
* @param AppContext $context The AppContext passed down to the query
* @param ResolveInfo $info The ResloveInfo object
*
* @return array
*/
public static function resolve_user_role_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
return UserRoleConnectionResolver::resolve( $source, $args, $context, $info );
}
/**
* Get all of the allowed settings by group and return the
* settings group that matches the group param
*
* @access public
*
* @param string $group
*
* @return array $settings_groups[ $group ]
*/
public static function get_setting_group_fields( $group ) {
/**
* Get all of the settings, sorted by group
*/
$settings_groups = self::get_allowed_settings_by_group();
return ! empty( $settings_groups[ $group ] ) ? $settings_groups[ $group ] : [];
}
/**
* Get all of the allowed settings by group
*
* @access public
* @return array $allowed_settings_by_group
*/
public static function get_allowed_settings_by_group() {
/**
* Get all registered settings
*/
$registered_settings = get_registered_settings();
/**
* Loop through the $registered_settings array and build an array of
* settings for each group ( general, reading, discussion, writing, reading, etc. )
* if the setting is allowed in REST or GraphQL
*/
foreach ( $registered_settings as $key => $setting ) {
if ( ! isset( $setting['show_in_graphql'] ) ) {
if ( isset( $setting['show_in_rest'] ) && false !== $setting['show_in_rest'] ) {
$setting['key'] = $key;
$allowed_settings_by_group[ $setting['group'] ][ $key ] = $setting;
}
} else if ( true === $setting['show_in_graphql'] ) {
$setting['key'] = $key;
$allowed_settings_by_group[ $setting['group'] ][ $key ] = $setting;
}
};
/**
* Set the setting groups that are allowed
*/
$allowed_settings_by_group = ! empty( $allowed_settings_by_group ) && is_array( $allowed_settings_by_group ) ? $allowed_settings_by_group : [];
/**
* Filter the $allowed_settings_by_group to allow enabling or disabling groups in the GraphQL Schema.
*
* @param array $allowed_settings_by_group
*/
$allowed_settings_by_group = apply_filters( 'graphql_allowed_settings_by_group', $allowed_settings_by_group );
return $allowed_settings_by_group;
}
/**
* Get all of the $allowed_settings
*
* @access public
* @return array $allowed_settings
*/
public static function get_allowed_settings() {
/**
* Get all registered settings
*/
$registered_settings = get_registered_settings();
/**
* Loop through the $registered_settings and if the setting is allowed in REST or GraphQL
* add it to the $allowed_settings array
*/
foreach ( $registered_settings as $key => $setting ) {
if ( ! isset( $setting['show_in_graphql'] ) ) {
if ( isset( $setting['show_in_rest'] ) && false !== $setting['show_in_rest'] ) {
$setting['key'] = $key;
$allowed_settings[ $key ] = $setting;
}
} else if ( true === $setting['show_in_graphql'] ) {
$setting['key'] = $key;
$allowed_settings[ $key ] = $setting;
}
};
/**
* Verify that we have the allowed settings
*/
$allowed_settings = ! empty( $allowed_settings ) && is_array( $allowed_settings ) ? $allowed_settings : [];
/**
* Filter the $allowed_settings to allow some to be enabled or disabled from showing in
* the GraphQL Schema.
*
* @param array $allowed_settings
*
* @return array
*/
$allowed_settings = apply_filters( 'graphql_allowed_setting_groups', $allowed_settings );
return $allowed_settings;
}
/**
* We get the node interface and field from the relay library.
*
* The first method is the way we resolve an ID to its object. The second is the way we resolve
* an object that implements node to its type.
*
* @return array
* @throws UserError
* @access public
*/
public static function get_node_definition() {
if ( null === self::$node_definition ) {
$node_definition = Relay::nodeDefinitions(
// The ID fetcher definition
function ( $global_id ) {
if ( empty( $global_id ) ) {
throw new UserError( __( 'An ID needs to be provided to resolve a node.', 'wp-graphql' ) );
}
/**
* Convert the encoded ID into an array we can work with
*
* @since 0.0.4
*/
$id_components = Relay::fromGlobalId( $global_id );
/**
* If the $id_components is a proper array with a type and id
*
* @since 0.0.5
*/
if ( is_array( $id_components ) && ! empty( $id_components['id'] ) && ! empty( $id_components['type'] ) ) {
/**
* Get the allowed_post_types and allowed_taxonomies
*
* @since 0.0.5
*/
$allowed_post_types = \WPGraphQL::get_allowed_post_types();
$allowed_taxonomies = \WPGraphQL::get_allowed_taxonomies();
switch ( $id_components['type'] ) {
case in_array( $id_components['type'], $allowed_post_types, true ):
$node = self::resolve_post_object( $id_components['id'], $id_components['type'] );
break;
case in_array( $id_components['type'], $allowed_taxonomies, true ):
$node = self::resolve_term_object( $id_components['id'], $id_components['type'] );
break;
case 'comment':
$node = self::resolve_comment( $id_components['id'] );
break;
case 'commentAuthor':
$node = self::resolve_comment_author( $id_components['id'] );
break;
case 'plugin':
$node = self::resolve_plugin( $id_components['id'] );
break;
case 'postType':
$node = self::resolve_post_type( $id_components['id'] );
break;
case 'taxonomy':
$node = self::resolve_taxonomy( $id_components['id'] );
break;
case 'theme':
$node = self::resolve_theme( $id_components['id'] );
break;
case 'user':
$node = self::resolve_user( $id_components['id'] );
break;
default:
/**
* Add a filter to allow externally registered node types to resolve based on
* the id_components
*
* @param int $id The id of the node, from the global ID
* @param string $type The type of node to resolve, from the global ID
*
* @since 0.0.6
*/
$node = apply_filters( 'graphql_resolve_node', null, $id_components['id'], $id_components['type'] );
break;
}
/**
* If the $node is not properly resolved, throw an exception
*
* @since 0.0.6
*/
if ( null === $node ) {
throw new UserError( sprintf( __( 'No node could be found with global ID: %s', 'wp-graphql' ), $global_id ) );
}
/**
* Return the resolved $node
*
* @since 0.0.5
*/
return $node;
} else {
throw new UserError( sprintf( __( 'The global ID isn\'t recognized ID: %s', 'wp-graphql' ), $global_id ) );
}
},
// Type resolver
function ( $node ) {
if ( true === is_object( $node ) ) {
switch ( true ) {
case $node instanceof \WP_Post:
$type = Types::post_object( $node->post_type );
break;
case $node instanceof \WP_Term:
$type = Types::term_object( $node->taxonomy );
break;
case $node instanceof \WP_Comment:
$type = 'Comment';
break;
case $node instanceof \WP_Post_Type:
$type = 'PostType';
break;
case $node instanceof \WP_Taxonomy:
$type = 'Taxonomy';
break;
case $node instanceof \WP_Theme:
$type = 'Theme';
break;
case $node instanceof \WP_User:
$type = 'User';
break;
default:
$type = null;
}
// Some nodes might return an array instead of an object
} elseif ( is_array( $node ) ) {
switch ( $node ) {
case array_key_exists( 'PluginURI', $node ):
$type = 'Plugin';
break;
case array_key_exists( 'is_comment_author', $node ):
$type = 'CommentAuthor';
break;
default:
$type = null;
}
}
/**
* Add a filter to allow externally registered node types to return the proper type
* based on the node_object that's returned
*
* @param mixed|object|array $type The type definition the node should resolve to.
* @param mixed|object|array $node The $node that is being resolved
*
* @since 0.0.6
*/
$type = apply_filters( 'graphql_resolve_node_type', $type, $node );
/**
* If the $type is not properly resolved, throw an exception
*
* @since 0.0.6
*/
if ( null === $type ) {
throw new UserError( __( 'No type was found matching the node', 'wp-graphql' ) );
}
/**
* Return the resolved $type for the $node
*
* @since 0.0.5
*/
return $type;
}
);
self::$node_definition = $node_definition;
}
return self::$node_definition;
}
/**
* Cached version of get_page_by_path so that we're not making unnecessary SQL all the time
*
* This is a modified version of the cached function from WordPress.com VIP MU Plugins here.
*
* @param string $uri
* @param string $output Optional. Output type; OBJECT*, ARRAY_N, or ARRAY_A.
* @param string $post_type Optional. Post type; default is 'post'.
*
* @return \WP_Post|null WP_Post on success or null on failure
* @see https://github.com/Automattic/vip-go-mu-plugins/blob/52549ae9a392fc1343b7ac9dba4ebcdca46e7d55/vip-helpers/vip-caching.php#L186
* @link http://vip.wordpress.com/documentation/uncached-functions/ Uncached Functions
*/
public static function get_post_object_by_uri( $uri, $output = OBJECT, $post_type = 'post' ) {
if ( is_array( $post_type ) ) {
$cache_key = sanitize_key( $uri ) . '_' . md5( serialize( $post_type ) );
} else {
$cache_key = $post_type . '_' . sanitize_key( $uri );
}
$post_id = wp_cache_get( $cache_key, 'get_post_object_by_path' );
if ( false === $post_id ) {
$post = get_page_by_path( $uri, $output, $post_type );
$post_id = $post ? $post->ID : 0;
if ( 0 === $post_id ) {
wp_cache_set( $cache_key, $post_id, 'get_post_object_by_path', ( 1 * HOUR_IN_SECONDS + mt_rand( 0, HOUR_IN_SECONDS ) ) ); // We only store the ID to keep our footprint small
} else {
wp_cache_set( $cache_key, $post_id, 'get_post_object_by_path', 0 ); // We only store the ID to keep our footprint small
}
}
if ( $post_id ) {
return self::resolve_post_object( $post_id, $post_type );
}
return null;
}
/**
* Returns an array of nav menu location names
*/
public static function get_registered_nav_menu_locations() {
global $_wp_registered_nav_menus;
return array_keys( $_wp_registered_nav_menus );
}
}