This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
class-config.php
1498 lines (1286 loc) · 43.9 KB
/
class-config.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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Config for WPGraphQL ACF
*
* @package wp-graphql-acf
*/
namespace WPGraphQL\ACF;
use Exception;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Model\Comment;
use WPGraphQL\Model\Menu;
use WPGraphQL\Model\MenuItem;
use WPGraphQL\Model\Post;
use WPGraphQL\Model\Term;
use WPGraphQL\Model\User;
use WPGraphQL\Registry\TypeRegistry;
use WPGraphQL\Utils\Utils;
/**
* Config class.
*/
class Config {
/**
* @var TypeRegistry
*/
protected $type_registry;
/**
* Stores the location rules for back compat
* @var array
*/
protected $location_rules = [];
/**
* @var array <string> List of field names registered to the Schema
*/
protected $registered_field_names;
/**
* @var array <string> List of options page slugs registered to the Schema
*/
protected $registered_options_pages = [];
/**
* Initialize WPGraphQL to ACF
*
* @param TypeRegistry $type_registry Instance of the WPGraphQL TypeRegistry
*
* @throws Exception
*/
public function init( TypeRegistry $type_registry ) {
/**
* Set the TypeRegistry
*/
$this->type_registry = $type_registry;
$this->register_initial_types();
/**
* Gets the location rules for backward compatibility.
*
* This allows for ACF Field Groups that were registered before the "graphql_types"
* field was respected can still work with the old GraphQL Schema rules that mapped
* from the ACF Location rules.
*/
$this->location_rules = $this->get_location_rules();
/**
* Add ACF Fields to GraphQL Types
*/
$this->add_options_pages_to_schema();
$this->add_acf_fields_to_graphql_types();
// This filter tells WPGraphQL to resolve revision meta for ACF fields from the revision's meta, instead
// of the parent (published post) meta.
add_filter( 'graphql_resolve_revision_meta_from_parent', function( $should, $object_id, $meta_key, $single ) {
// Loop through all registered ACF fields that show in GraphQL.
if ( is_array( $this->registered_field_names ) && ! empty( $this->registered_field_names ) ) {
$matches = null;
// Iterate over all field names
foreach ( $this->registered_field_names as $field_name ) {
// If the field name is an exact match with the $meta_key, the ACF field should
// resolve from the revision meta, so we can return false here, so that meta can
// resolve from the revision instead of the parent
if ( $field_name === $meta_key ) {
return false;
}
// For flex fields/repeaters, the meta keys are structured a bit funky.
// This checks to see if the $meta_key starts with the same string as one of the
// acf fields (a flex/repeater field) and then checks if it's preceeded by an underscore and a number.
if ( $field_name === substr( $meta_key, 0, strlen( $field_name ) ) ) {
// match any string that starts with the field name, followed by an underscore, followed by a number, followed by another string
// ex my_flex_field_0_text_field or some_repeater_field_12_25MostPopularDogToys
$pattern = '/' . $field_name . '_\d+_\w+/m';
preg_match( $pattern, $meta_key, $matches );
}
// If the meta key matches the pattern, treat it as a sub-field of an ACF Field Group
if ( null !== $matches ) {
return false;
}
}
}
return $should;
}, 10, 4 );
}
/**
* Registers initial Types for use with ACF Fields
*
* @throws Exception
*/
public function register_initial_types() {
$this->type_registry->register_interface_type(
'AcfFieldGroup',
[
'description' => __( 'A Field Group registered by ACF', 'wp-graphql-acf' ),
'fields' => [
'fieldGroupName' => [
'description' => __( 'The name of the ACF Field Group', 'wp-graphql-acf' ),
'type' => 'String',
],
]
]
);
$this->type_registry->register_object_type(
'AcfLink',
[
'description' => __( 'ACF Link field', 'wp-graphql-acf' ),
'fields' => [
'url' => [
'type' => 'String',
'description' => __( 'The url of the link', 'wp-graphql-acf' ),
],
'title' => [
'type' => 'String',
'description' => __( 'The title of the link', 'wp-graphql-acf' ),
],
'target' => [
'type' => 'String',
'description' => __( 'The target of the link (_blank, etc)', 'wp-graphql-acf' ),
],
],
]
);
}
/**
* Gets the location rules
* @return array
*/
protected function get_location_rules() {
$field_groups = acf_get_field_groups();
if ( empty( $field_groups ) || ! is_array( $field_groups ) ) {
return [];
}
$rules = [];
// Each field group that doesn't have GraphQL Types explicitly set should get the location
// rules interpreted.
foreach ( $field_groups as $field_group ) {
if ( ! isset( $field_group['graphql_types'] ) || ! is_array( $field_group['graphql_types'] ) ) {
$rules[] = $field_group;
}
}
if ( empty( $rules ) ) {
return [];
}
// If there are field groups with no graphql_types field set, inherit the rules from
// ACF Location Rules
$rules = new LocationRules();
$rules->determine_location_rules();
return $rules->get_rules();
}
protected function add_options_pages_to_schema() {
global $acf_options_page;
if ( ! isset( $acf_options_page ) ) {
return ;
}
/**
* Get a list of post types that have been registered to show in graphql
*/
$graphql_options_pages = acf_get_options_pages();
/**
* If there are no post types exposed to GraphQL, bail
*/
if ( empty( $graphql_options_pages ) || ! is_array( $graphql_options_pages ) ) {
return;
}
$options_pages_to_register = [];
/**
* Loop over the post types exposed to GraphQL
*/
foreach ( $graphql_options_pages as $options_page_key => $options_page ) {
if ( ! isset( $options_page['show_in_graphql'] ) || false === (bool) $options_page['show_in_graphql'] ) {
continue;
}
/**
* Get options page properties.
*/
$page_title = $options_page['page_title'];
$page_slug = $options_page['menu_slug'];
$type_name = isset( $options_page['graphql_field_name'] ) ? Utils::format_type_name( $options_page['graphql_field_name'] ) : Utils::format_type_name( $options_page['menu_slug'] );
$options_pages_to_register[ $type_name ] = [
'title' => $page_title,
'slug' => $page_slug,
'type_name' => $type_name,
'options_page' => $options_page,
];
}
if ( is_array( $options_pages_to_register ) && ! empty( $options_pages_to_register ) ) {
foreach ( $options_pages_to_register as $page_to_register ) {
$page_title = $page_to_register['title'];
$page_slug = $page_to_register['slug'];
$type_name = isset( $page_to_register['type_name'] ) ? Utils::format_type_name( $page_to_register['type_name'] ) : Utils::format_type_name( $page_to_register['slug'] );
$options_page = $page_to_register['options_page'];
$this->type_registry->register_object_type( $type_name, [
'description' => sprintf( __( '%s options.', 'wp-graphql-acf' ), $page_title ),
'fields' => [
'pageTitle' => [
'type' => 'String',
'resolve' => function( $source ) use ( $page_title ) {
return ! empty( $page_title ) ? $page_title : null;
},
],
'pageSlug' => [
'type' => 'String',
'resolve' => function( $source ) use ( $page_slug ) {
return ! empty( $page_slug ) ? $page_slug : null;
},
],
],
] );
$field_name = Utils::format_field_name( $type_name );
$options_page['type'] = 'options_page';
$this->type_registry->register_field(
'RootQuery',
$field_name,
[
'type' => $type_name,
'description' => sprintf( __( '%s options.', 'wp-graphql-acf' ), $page_title ),
'resolve' => function() use ( $options_page ) {
return ! empty( $options_page ) ? $options_page : null;
}
]
);
}
}
}
/**
* Determines whether a field group should be exposed to the GraphQL Schema. By default, field
* groups will not be exposed to GraphQL.
*
* @param array $field_group Undocumented.
*
* @return bool
*/
protected function should_field_group_show_in_graphql( $field_group ) {
/**
* By default, field groups will not be exposed to GraphQL.
*/
$show = false;
/**
* If
*/
if ( isset( $field_group['show_in_graphql'] ) && true === (bool) $field_group['show_in_graphql'] ) {
$show = true;
}
/**
* Determine conditions where the GraphQL Schema should NOT be shown in GraphQL for
* root groups, not nested groups with parent.
*/
if ( ! isset( $field_group['parent'] ) ) {
if (
( isset( $field_group['active'] ) && true != $field_group['active'] ) ||
( empty( $field_group['location'] ) || ! is_array( $field_group['location'] ) )
) {
$show = false;
}
}
/**
* Whether a field group should show in GraphQL.
*
* @var boolean $show Whether the field group should show in the GraphQL Schema
* @var array $field_group The ACF Field Group
* @var Config $this The Config for the ACF Plugin
*/
return apply_filters( 'wpgraphql_acf_should_field_group_show_in_graphql', $show, $field_group, $this );
}
/**
* Undocumented function
*
* @todo: This may be a good utility to add to WPGraphQL Core? May even have something already?
*
* @param string $str Unknown.
* @param array $no_strip Unknown.
*
* @return mixed|null|string|string[]
*/
public static function camel_case( $str, array $no_strip = [] ) {
// non-alpha and non-numeric characters become spaces.
$str = preg_replace( '/[^a-z0-9' . implode( '', $no_strip ) . ']+/i', ' ', $str );
$str = trim( $str );
// Lowercase the string
$str = strtolower( $str );
// uppercase the first character of each word.
$str = ucwords( $str );
// Replace spaces
$str = str_replace( ' ', '', $str );
// Lowecase first letter
$str = lcfirst( $str );
return $str;
}
/**
* Undocumented function
*
* @param [type] $root Undocumented.
* @param [type] $acf_field Undocumented.
* @param boolean $format Whether ACF should apply formatting to the field. Default false.
*
* @return mixed
*/
protected function get_acf_field_value( $root, $acf_field, $format = false ) {
$value = null;
$id = null;
if ( is_array( $root ) && isset( $root['node'] ) ) {
$id = $root['node']->ID;
}
if ( is_array( $root ) && ! ( ! empty( $root['type'] ) && 'options_page' === $root['type'] ) ) {
if ( isset( $root[ $acf_field['key'] ] ) ) {
$value = $root[ $acf_field['key'] ];
if ( 'wysiwyg' === $acf_field['type'] ) {
$value = apply_filters( 'the_content', $value );
}
}
} else {
switch ( true ) {
case $root instanceof Term:
$id = 'term_' . $root->term_id;
break;
case $root instanceof Post:
$id = absint( $root->databaseId );
break;
case $root instanceof MenuItem:
$id = absint( $root->menuItemId );
break;
case $root instanceof Menu:
$id = 'term_' . $root->menuId;
break;
case $root instanceof User:
$id = 'user_' . absint( $root->userId );
break;
case $root instanceof Comment:
$id = 'comment_' . absint( $root->databaseId );
break;
case is_array( $root ) && ! empty( $root['type'] ) && 'options_page' === $root['type']:
$id = $root['post_id'];
break;
default:
$id = null;
break;
}
}
if ( empty( $value ) ) {
/**
* Filters the root ID, allowing additional Models the ability to provide a way to resolve their ID
*
* @param int $id The ID of the object. Default null
* @param mixed $root The Root object being resolved. The ID is typically a property of this object.
*/
$id = apply_filters( 'graphql_acf_get_root_id', $id, $root );
if ( empty( $id ) ) {
return null;
}
$format = false;
if ( 'wysiwyg' === $acf_field['type'] ) {
$format = true;
}
if ( 'select' === $acf_field['type'] ) {
$format = true;
}
/**
* Check if cloned field and retrieve the key accordingly.
*/
if ( ! empty( $acf_field['_clone'] ) ) {
$key = $acf_field['__key'];
} else {
$key = $acf_field['key'];
}
$field_value = get_field( $key, $id, $format );
$value = ! empty( $field_value ) ? $field_value : null;
}
/**
* Filters the returned ACF field value
*
* @param mixed $value The resolved ACF field value
* @param array $acf_field The ACF field config
* @param mixed $root The Root object being resolved. The ID is typically a property of this object.
* @param int $id The ID of the object
*/
return apply_filters( 'graphql_acf_field_value', $value, $acf_field, $root, $id );
}
/**
* Get a list of supported fields that WPGraphQL for ACF supports.
*
* This is helpful for determining whether UI should be output for the field, and whether
* the field should be added to the Schema.
*
* Some fields, such as "Accordion" are not supported currently.
*
* @return array
*/
public static function get_supported_fields() {
$supported_fields = [
'text',
'textarea',
'number',
'range',
'email',
'url',
'password',
'image',
'file',
'wysiwyg',
'oembed',
'gallery',
'select',
'checkbox',
'radio',
'button_group',
'true_false',
'link',
'post_object',
'page_link',
'relationship',
'taxonomy',
'user',
'google_map',
'date_picker',
'date_time_picker',
'time_picker',
'color_picker',
'group',
'repeater',
'flexible_content'
];
/**
* filter the supported fields
*
* @param array $supported_fields
*/
return apply_filters( 'wpgraphql_acf_supported_fields', $supported_fields );
}
/**
* Undocumented function
*
* @param string $type_name The name of the GraphQL Type to add the field to.
* @param string $field_name The name of the field to add to the GraphQL Type.
* @param array $config The GraphQL configuration of the field.
*
* @return mixed
*/
protected function register_graphql_field( string $type_name, string $field_name, array $config ) {
$acf_field = isset( $config['acf_field'] ) ? $config['acf_field'] : null;
$acf_type = isset( $acf_field['type'] ) ? $acf_field['type'] : null;
if ( empty( $acf_type ) ) {
return false;
}
/**
* filter the field config for custom field types
*
* @param array $field_config
*/
$field_config = apply_filters( 'wpgraphql_acf_register_graphql_field', [
'type' => null,
'resolve' => isset( $config['resolve'] ) && is_callable( $config['resolve'] ) ? $config['resolve'] : function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
return ! empty( $value ) ? $value : null;
},
], $type_name, $field_name, $config );
switch ( $acf_type ) {
case 'button_group':
case 'color_picker':
case 'email':
case 'text':
case 'message':
case 'oembed':
case 'password':
case 'wysiwyg':
case 'url':
// Even though Selects and Radios in ACF can _technically_ be an integer
// we're choosing to always cast as a string because with
// GraphQL we can't return different types
$field_config['type'] = 'String';
break;
case 'textarea':
$field_config['type'] = 'String';
$field_config['resolve'] = function( $root ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
if ( ! empty( $acf_field['new_lines'] ) ) {
if ( 'wpautop' === $acf_field['new_lines'] ) {
$value = wpautop( $value );
}
if ( 'br' === $acf_field['new_lines'] ) {
$value = nl2br( $value );
}
}
return $value;
};
break;
case 'select':
/**
* If the select field is configured to not allow multiple values
* the field will return a string, but if it is configured to allow
* multiple values it will return a list of strings, and an empty array
* if no values are set.
*
* @see: https://github.com/wp-graphql/wp-graphql-acf/issues/25
*/
if ( empty( $acf_field['multiple'] ) ) {
if('array' === $acf_field['return_format'] ){
$field_config['type'] = [ 'list_of' => 'String' ];
$field_config['resolve'] = function( $root ) use ( $acf_field) {
$value = $this->get_acf_field_value( $root, $acf_field, true);
return ! empty( $value ) && is_array( $value ) ? $value : [];
};
}else{
$field_config['type'] = 'String';
}
} else {
$field_config['type'] = [ 'list_of' => 'String' ];
$field_config['resolve'] = function( $root ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
return ! empty( $value ) && is_array( $value ) ? $value : [];
};
}
break;
case 'radio':
$field_config['type'] = 'String';
break;
case 'number':
case 'range':
$field_config['type'] = 'Float';
break;
case 'true_false':
$field_config['type'] = 'Boolean';
break;
case 'date_picker':
case 'time_picker':
case 'date_time_picker':
$field_config = [
'type' => 'String',
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field, true );
if ( ! empty( $value ) && ! empty( $acf_field['return_format'] ) ) {
$value = date_i18n( $acf_field['return_format'], strtotime( $value ) );
}
return ! empty( $value ) ? $value : null;
},
];
break;
case 'relationship':
if ( isset( $acf_field['post_type'] ) && is_array( $acf_field['post_type'] ) ) {
$field_type_name = $type_name . '_' . ucfirst( self::camel_case( $acf_field['name'] ) );
if ( $this->type_registry->get_type( $field_type_name ) == $field_type_name ) {
$type = $field_type_name;
} else {
$type_names = [];
foreach ( $acf_field['post_type'] as $post_type ) {
if ( in_array( $post_type, get_post_types([ 'show_in_graphql' => true ]), true ) ) {
$type_names[ $post_type ] = get_post_type_object( $post_type )->graphql_single_name;
}
}
if ( empty( $type_names ) ) {
$type = 'PostObjectUnion';
} else {
register_graphql_union_type( $field_type_name, [
'typeNames' => $type_names,
'resolveType' => function( $value ) use ( $type_names ) {
$post_type_object = get_post_type_object( $value->post_type );
return ! empty( $post_type_object->graphql_single_name ) ? $this->type_registry->get_type( $post_type_object->graphql_single_name ) : null;
}
] );
$type = $field_type_name;
}
}
} else {
$type = 'PostObjectUnion';
}
$field_config = [
'type' => [ 'list_of' => $type ],
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$relationship = [];
$value = $this->get_acf_field_value( $root, $acf_field );
if ( ! empty( $value ) && is_array( $value ) ) {
foreach ( $value as $post_id ) {
$post_object = get_post( $post_id );
if ( $post_object instanceof \WP_Post ) {
$post_model = new Post( $post_object );
if ( 'private' != $post_model->get_visibility() ) {
$relationship[] = $post_model;
}
}
}
}
return empty( $relationship ) ? null : $relationship;
},
];
break;
case 'page_link':
case 'post_object':
if ( isset( $acf_field['post_type'] ) && is_array( $acf_field['post_type'] ) ) {
$field_type_name = $type_name . '_' . ucfirst( self::camel_case( $acf_field['name'] ) );
if ( $this->type_registry->get_type( $field_type_name ) == $field_type_name ) {
$type = $field_type_name;
} else {
$type_names = [];
foreach ( $acf_field['post_type'] as $post_type ) {
if ( in_array( $post_type, \get_post_types( [ 'show_in_graphql' => true ] ), true ) ) {
$type_names[ $post_type ] = get_post_type_object( $post_type )->graphql_single_name;
}
}
if ( empty( $type_names ) ) {
$field_config['type'] = null;
break;
}
register_graphql_union_type( $field_type_name, [
'typeNames' => $type_names,
'resolveType' => function( $value ) use ( $type_names ) {
$post_type_object = get_post_type_object( $value->post_type );
return ! empty( $post_type_object->graphql_single_name ) ? $this->type_registry->get_type( $post_type_object->graphql_single_name ) : null;
}
] );
$type = $field_type_name;
}
} else {
$type = 'PostObjectUnion';
}
// If the field is allowed to be a multi select
if ( 0 !== $acf_field['multiple'] ) {
$type = [ 'list_of' => $type ];
}
$field_config = [
'type' => $type,
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
$return = [];
if ( ! empty( $value ) ) {
if ( is_array( $value ) ) {
foreach ( $value as $id ) {
$post = get_post( $id );
if ( ! empty( $post ) ) {
$return[] = new Post( $post );
}
}
} else {
$post = get_post( absint( $value ) );
if ( ! empty( $post ) ) {
$return[] = new Post( $post );
}
}
}
// If the field is allowed to be a multi select
if ( 0 !== $acf_field['multiple'] ) {
$return = ! empty( $return ) ? $return : null;
} else {
$return = ! empty( $return[0] ) ? $return[0] : null;
}
/**
* This hooks allows for filtering of the post object source. In case an non-core defined
* post-type is being targeted.
*
* @param mixed|null $source GraphQL Type source.
* @param mixed|null $value Root ACF field value.
* @param AppContext $context AppContext instance.
* @param ResolveInfo $info ResolveInfo instance.
*/
return apply_filters(
'graphql_acf_post_object_source',
$return,
$value,
$context,
$info
);
},
];
break;
case 'link':
$field_config['type'] = 'AcfLink';
break;
case 'image':
case 'file':
$field_config = [
'type' => 'MediaItem',
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
return $context->get_loader( 'post' )->load_deferred( (int) $value );
},
];
break;
case 'checkbox':
$field_config = [
'type' => [ 'list_of' => 'String' ],
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
return is_array( $value ) ? $value : null;
},
];
break;
case 'gallery':
$field_config = [
'type' => [ 'list_of' => 'MediaItem' ],
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
$gallery = [];
if ( ! empty( $value ) && is_array( $value ) ) {
foreach ( $value as $image ) {
$post_object = get_post( (int) $image );
if ( $post_object instanceof \WP_Post ) {
$post_model = new Post( $post_object );
$gallery[] = $post_model;
}
}
}
return isset( $value ) ? $gallery : null;
},
];
break;
case 'user':
$type = 'User';
if ( isset( $acf_field['multiple'] ) && 1 === $acf_field['multiple'] ) {
$type = [ 'list_of' => $type ];
}
$field_config = [
'type' => $type,
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );
$return = [];
if ( ! empty( $value ) ) {
if ( is_array( $value ) ) {
foreach ( $value as $id ) {
$user = get_user_by( 'id', $id );
if ( ! empty( $user ) ) {
$user = new User( $user );
if ( 'private' !== $user->get_visibility() ) {
$return[] = $user;
}
}
}
} else {
$user = get_user_by( 'id', absint( $value ) );
if ( ! empty( $user ) ) {
$user = new User( $user );
if ( 'private' !== $user->get_visibility() ) {
$return[] = $user;
}
}
}
}
// If the field is allowed to be a multi select
if ( 0 !== $acf_field['multiple'] ) {
$return = ! empty( $return ) ? $return : null;
} else {
$return = ! empty( $return[0] ) ? $return[0] : null;
}
return $return;
},
];
break;
case 'taxonomy':
$type = 'TermObjectUnion';
if ( isset( $acf_field['taxonomy'] ) ) {
$tax_object = get_taxonomy( $acf_field['taxonomy'] );
if ( isset( $tax_object->graphql_single_name ) ) {
$type = $tax_object->graphql_single_name;
}
}
$is_multiple = isset( $acf_field['field_type'] ) && in_array( $acf_field['field_type'], array( 'checkbox', 'multi_select' ) );
$field_config = [
'type' => $is_multiple ? ['list_of' => $type ] : $type,
'resolve' => function( $root, $args, $context, $info ) use ( $acf_field, $is_multiple ) {
$value = $this->get_acf_field_value( $root, $acf_field );
/**
* If this is multiple, the value will most likely always be an array.
* If it isn't, we want to return a single term id.
*/
if ( ! empty( $value ) && is_array( $value ) ) {
foreach ( $value as $term ) {
$terms[] = $context->get_loader( 'term' )->load_deferred( (int) $term );
}
return $terms;
} else {
return $context->get_loader( 'term' )->load_deferred( (int) $value );
}
},
];
break;
// Accordions are not represented in the GraphQL Schema.
case 'accordion':
$field_config = null;
break;
case 'group':
$field_type_name = $type_name . '_' . ucfirst( self::camel_case( $acf_field['name'] ) );
if ( null !== $this->type_registry->get_type( $field_type_name ) ) {
$field_config['type'] = $field_type_name;
break;
}
$this->type_registry->register_object_type(
$field_type_name,
[
'description' => __( 'Field Group', 'wp-graphql-acf' ),
'interfaces' => [ 'AcfFieldGroup' ],
'fields' => [
'fieldGroupName' => [
'resolve' => function( $source ) use ( $acf_field ) {
return ! empty( $acf_field['name'] ) ? $acf_field['name'] : null;
},
],
],
]
);
$this->add_field_group_fields( $acf_field, $field_type_name );
$field_config['type'] = $field_type_name;
break;
case 'google_map':
$field_type_name = 'ACF_GoogleMap';
if ( $this->type_registry->get_type( $field_type_name ) == $field_type_name ) {
$field_config['type'] = $field_type_name;
break;
}
$fields = [
'streetAddress' => [
'type' => 'String',
'description' => __( 'The street address associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['address'] ) ? $root['address'] : null;
},
],
'latitude' => [
'type' => 'Float',
'description' => __( 'The latitude associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['lat'] ) ? $root['lat'] : null;
},
],
'longitude' => [
'type' => 'Float',
'description' => __( 'The longitude associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['lng'] ) ? $root['lng'] : null;
},
],
];
// ACF 5.8.6 added more data to Google Maps field value
// https://www.advancedcustomfields.com/changelog/
if ( \acf_version_compare(acf_get_db_version(), '>=', '5.8.6' ) ) {
$fields += [
'streetName' => [
'type' => 'String',
'description' => __( 'The street name associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['street_name'] ) ? $root['street_name'] : null;
},
],
'streetNumber' => [
'type' => 'String',
'description' => __( 'The street number associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['street_number'] ) ? $root['street_number'] : null;
},
],
'city' => [
'type' => 'String',
'description' => __( 'The city associated with the map', 'wp-graphql-acf' ),