Skip to content

Commit c06163f

Browse files
committed
Merge branch 'fix/performance-issues' of github.com:wp-graphql/wpgraphql-acf into fix/performance-issues
2 parents b537a3d + 04e62e8 commit c06163f

File tree

5 files changed

+106
-137
lines changed

5 files changed

+106
-137
lines changed

src/LocationRules/LocationRules.php

Lines changed: 9 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct( array $acf_field_groups = [] ) {
6161
* @param string $graphql_type_name The name of the GraphQL Type
6262
*/
6363
public function set_graphql_type( string $field_group_name, string $graphql_type_name ): void {
64-
$this->mapped_field_groups[ Utils::format_field_name( $field_group_name, true ) ][] = ucfirst( Utils::format_field_name( $graphql_type_name, true ) );
64+
$this->mapped_field_groups[ strtolower( Utils::format_field_name( $field_group_name, true ) ) ][] = ucfirst( Utils::format_field_name( $graphql_type_name, true ) );
6565
}
6666

6767
/**
@@ -267,12 +267,10 @@ public function determine_rules( string $field_group_name, string $param, string
267267
$this->determine_post_template_rules( $field_group_name, $param, $operator, $value );
268268
break;
269269
case 'post_status':
270-
$this->determine_post_status_rules( $field_group_name, $param, $operator, $value );
271270
break;
272271
case 'post_format':
273272
case 'post_category':
274273
case 'post_taxonomy':
275-
$this->determine_post_taxonomy_rules( $field_group_name, $param, $operator, $value );
276274
break;
277275
case 'post':
278276
$this->determine_post_rules( $field_group_name, $param, $operator, $value );
@@ -489,37 +487,6 @@ public function determine_post_template_rules( string $field_group_name, string
489487
}
490488
}
491489

492-
/**
493-
* Determines how the ACF Rules should apply to the WPGraphQL Schema
494-
*
495-
* @param string $field_group_name The name of the ACF Field Group the rule applies to
496-
* @param string $param The parameter of the rule
497-
* @param string $operator The operator of the rule
498-
* @param string $value The value of the rule
499-
*/
500-
public function determine_post_status_rules( string $field_group_name, string $param, string $operator, string $value ): void {
501-
// @todo: Should post status affect the GraphQL Schema at all?
502-
// If a field group is set to show on "post_status == publish" as the only rule, what post type does that apply to? All? 🤔
503-
// If a field group is set to show on "post_status != draft" does that mean the field group should be available on all post types in the Schema by default?
504-
// This seems like a very difficult rule to translate to the Schema.
505-
// Like, lets say I add a field group called: "Editor Notes" that I want to show for any status that is not "publish". In theory, if that's my only rule, that seems like it should apply to all post types across the board, and show in the Admin in any state of the post, other than publish. 🤔
506-
507-
// ACF Admin behavior seems to add it to the Admin on all post types, so WPGraphQL
508-
// should respect this rule and also add it to all post types. The resolver should
509-
// then determine whether to resolve the data or not, based on this rule.
510-
511-
// If Post Status is used to qualify a field group location,
512-
// It will be added to the Schema for any Post Type that is set to show in GraphQL
513-
$allowed_post_types = get_post_types( [ 'show_in_graphql' => true ] );
514-
foreach ( $allowed_post_types as $post_type ) {
515-
$post_type_object = get_post_type_object( $post_type );
516-
$graphql_name = $post_type_object->graphql_single_name ?? null;
517-
if ( ! empty( $graphql_name ) ) {
518-
$this->set_graphql_type( $field_group_name, $graphql_name );
519-
}
520-
}
521-
}
522-
523490
/**
524491
* Determines how the ACF Rules should apply to the WPGraphQL Schema
525492
*
@@ -551,21 +518,6 @@ public function determine_post_format_rules( string $field_group_name, string $p
551518
}
552519
}
553520

554-
/**
555-
* Determines how the ACF Rules should apply to the WPGraphQL Schema
556-
*
557-
* @param string $field_group_name The name of the ACF Field Group the rule applies to
558-
* @param string $param The parameter of the rule
559-
* @param string $operator The operator of the rule
560-
* @param string $value The value of the rule
561-
*/
562-
public function determine_post_taxonomy_rules( string $field_group_name, string $param, string $operator, string $value ): void {
563-
564-
// If Post Taxonomy is used to qualify a field group location,
565-
// It will be added to the Schema for the Post post type
566-
$this->set_graphql_type( $field_group_name, 'Post' );
567-
}
568-
569521
/**
570522
* Determines how the ACF Rules should apply to the WPGraphQL Schema
571523
*
@@ -580,35 +532,20 @@ public function determine_post_rules( string $field_group_name, string $param, s
580532
// It will be added to the Schema for the GraphQL Type for the post_type of the Post
581533
// it is assigned to
582534

583-
if ( '==' === $operator ) {
584-
if ( absint( $value ) ) {
585-
$post = get_post( absint( $value ) );
586-
if ( $post instanceof \WP_Post ) {
587-
$post_type_object = get_post_type_object( $post->post_type );
588-
if ( $post_type_object && true === $post_type_object->show_in_graphql && isset( $post_type_object->graphql_single_name ) ) {
589-
$this->set_graphql_type( $field_group_name, $post_type_object->graphql_single_name );
590-
}
535+
if ( ( '==' === $operator ) && absint( $value ) ) {
536+
$post = get_post( absint( $value ) );
537+
if ( $post instanceof \WP_Post ) {
538+
$post_type_object = get_post_type_object( $post->post_type );
539+
if ( $post_type_object && true === $post_type_object->show_in_graphql && isset( $post_type_object->graphql_single_name ) ) {
540+
$this->set_graphql_type( $field_group_name, $post_type_object->graphql_single_name );
591541
}
592542
}
593543
}
594544

595545
// If a single post is used as not equal,
596-
// the field group should be added to ALL post types in the Schema
546+
// the field group should not be added to any type
597547
if ( '!=' === $operator ) {
598-
$allowed_post_types = get_post_types( [ 'show_in_graphql' => true ] );
599-
600-
if ( empty( $allowed_post_types ) ) {
601-
return;
602-
}
603-
604-
// loop over and set all post types
605-
foreach ( $allowed_post_types as $allowed_post_type ) {
606-
$post_type_object = get_post_type_object( $allowed_post_type );
607-
$graphql_name = $post_type_object->graphql_single_name ?? null;
608-
if ( ! empty( $graphql_name ) ) {
609-
$this->set_graphql_type( $field_group_name, $graphql_name );
610-
}
611-
}
548+
return;
612549
}
613550
}
614551

@@ -627,30 +564,6 @@ public function determine_page_type_rules( string $field_group_name, string $par
627564
if ( in_array( $value, [ 'front_page', 'posts_page' ], true ) ) {
628565
$this->set_graphql_type( $field_group_name, 'Page' );
629566
}
630-
631-
// If top_level, parent, or child is set as equal_to or not_equal_to
632-
// then the field group should be shown on all hierarchical post types
633-
if ( in_array( $value, [ 'top_level', 'parent', 'child' ], true ) ) {
634-
$hierarchical_post_types = get_post_types(
635-
[
636-
'show_in_graphql' => true,
637-
'hierarchical' => true,
638-
]
639-
);
640-
641-
if ( empty( $hierarchical_post_types ) ) {
642-
return;
643-
}
644-
645-
// loop over and set all post types
646-
foreach ( $hierarchical_post_types as $allowed_post_type ) {
647-
$post_type_object = get_post_type_object( $allowed_post_type );
648-
$graphql_name = $post_type_object->graphql_single_name ?? null;
649-
if ( ! empty( $graphql_name ) ) {
650-
$this->set_graphql_type( $field_group_name, $graphql_name );
651-
}
652-
}
653-
}
654567
}
655568

656569
/**

src/Registry.php

Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use WPGraphQL\Acf\Model\AcfOptionsPage;
99
use WPGraphQL\AppContext;
1010
use WPGraphQL\Registry\TypeRegistry;
11-
use WPGraphQL\Utils\Utils;
1211

1312
class Registry {
1413

@@ -17,6 +16,18 @@ class Registry {
1716
*/
1817
protected $registered_fields = [];
1918

19+
/**
20+
* Stores location rules once they've been mapped
21+
*
22+
* @var array<mixed>
23+
*/
24+
protected $mapped_location_rules = [];
25+
26+
/**
27+
* @var array<mixed>
28+
*/
29+
protected $all_acf_field_groups = [];
30+
2031
/**
2132
* @todo should be protected with getter/setter?
2233
* @var array<mixed>
@@ -61,6 +72,15 @@ public function get_type_registry(): TypeRegistry {
6172
return $this->type_registry;
6273
}
6374

75+
/**
76+
* Returns the mapped location rules
77+
*
78+
* @return array<mixed>
79+
*/
80+
public function get_mapped_location_rules(): array {
81+
return $this->mapped_location_rules;
82+
}
83+
6484
/**
6585
* @param string $key
6686
* @param mixed $field_group
@@ -82,7 +102,7 @@ public function has_registered_field_group( string $key ): bool {
82102
* @param array<mixed> $acf_field_group
83103
*/
84104
public function should_field_group_show_in_graphql( array $acf_field_group ): bool {
85-
return \WPGraphQL\Acf\Utils::should_field_group_show_in_graphql( $acf_field_group );
105+
return Utils::should_field_group_show_in_graphql( $acf_field_group );
86106
}
87107

88108
/**
@@ -91,6 +111,11 @@ public function should_field_group_show_in_graphql( array $acf_field_group ): bo
91111
* @return array<mixed>
92112
*/
93113
public function get_acf_field_groups(): array {
114+
115+
if ( ! empty( $this->all_acf_field_groups ) ) {
116+
return $this->all_acf_field_groups;
117+
}
118+
94119
$all_acf_field_groups = acf_get_field_groups();
95120

96121
$graphql_field_groups = [];
@@ -105,7 +130,8 @@ public function get_acf_field_groups(): array {
105130
$graphql_field_groups[ $acf_field_group['key'] ] = $acf_field_group;
106131
}
107132

108-
return $graphql_field_groups;
133+
$this->all_acf_field_groups = $graphql_field_groups;
134+
return $this->all_acf_field_groups;
109135
}
110136

111137
/**
@@ -286,7 +312,8 @@ public function get_field_group_interfaces( array $acf_field_group ): array {
286312
* @throws \Exception
287313
*/
288314
public function register_options_pages(): void {
289-
$graphql_options_pages = \WPGraphQL\Acf\Utils::get_acf_options_pages();
315+
316+
$graphql_options_pages = Utils::get_acf_options_pages();
290317

291318
if ( empty( $graphql_options_pages ) ) {
292319
return;
@@ -342,7 +369,7 @@ public function register_options_pages(): void {
342369
]
343370
);
344371

345-
$field_name = Utils::format_field_name( $type_name );
372+
$field_name = \WPGraphQL\Utils\Utils::format_field_name( $type_name );
346373

347374
$interface_name = 'WithAcfOptionsPage' . $type_name;
348375

@@ -499,7 +526,7 @@ public function map_acf_field_to_graphql( array $acf_field, array $acf_field_gro
499526
* @throws \GraphQL\Error\Error
500527
*/
501528
public function get_field_group_name( array $field_group ): string {
502-
return \WPGraphQL\Acf\Utils::get_field_group_name( $field_group );
529+
return Utils::get_field_group_name( $field_group );
503530
}
504531

505532
/**
@@ -508,7 +535,7 @@ public function get_field_group_name( array $field_group ): string {
508535
* @throws \GraphQL\Error\Error
509536
*/
510537
public function get_graphql_field_name( array $acf_field ): string {
511-
return Utils::format_field_name( $this->get_field_group_name( $acf_field ), true );
538+
return \WPGraphQL\Utils\Utils::format_field_name( $this->get_field_group_name( $acf_field ), true );
512539
}
513540

514541
/**
@@ -540,7 +567,7 @@ public function get_field_group_graphql_type_name( array $field_group ): ?string
540567
return null;
541568
}
542569

543-
return Utils::format_type_name( $replaced );
570+
return \WPGraphQL\Utils\Utils::format_type_name( $replaced );
544571
}
545572

546573
/**
@@ -551,6 +578,11 @@ public function get_field_group_graphql_type_name( array $field_group ): ?string
551578
* @return array<mixed>
552579
*/
553580
public function get_location_rules( array $acf_field_groups = [] ): array {
581+
582+
if ( ! empty( $this->get_mapped_location_rules() ) ) {
583+
return $this->get_mapped_location_rules();
584+
}
585+
554586
$field_groups = $acf_field_groups;
555587
$rules = [];
556588

@@ -571,7 +603,36 @@ public function get_location_rules( array $acf_field_groups = [] ): array {
571603
$rules = new LocationRules();
572604
$rules->determine_location_rules();
573605

574-
return $rules->get_rules();
606+
// Set the mapped location rules for future use
607+
$rules_by_group = $rules->get_rules();
608+
609+
$this->mapped_location_rules = $rules_by_group;
610+
611+
// Return them
612+
return $this->mapped_location_rules;
613+
}
614+
615+
/**
616+
* Get the location rules grouped by location instead of grouped by field group
617+
*
618+
* @return array<mixed>
619+
*/
620+
public function get_location_rules_grouped_by_location(): array {
621+
$location_rules = $this->get_location_rules();
622+
$rules_by_location = [];
623+
if ( ! empty( $location_rules ) ) {
624+
foreach ( $location_rules as $group => $locations ) {
625+
if ( ! empty( $locations ) ) {
626+
foreach ( $locations as $location ) {
627+
if ( ! array_key_exists( $location, $rules_by_location ) ) {
628+
$rules_by_location[ $location ] = [];
629+
}
630+
$rules_by_location[ $location ][] = $group;
631+
}
632+
}
633+
}
634+
}
635+
return $rules_by_location;
575636
}
576637

577638
/**
@@ -583,37 +644,31 @@ public function get_location_rules( array $acf_field_groups = [] ): array {
583644
* @return array<mixed>
584645
*/
585646
public function get_graphql_locations_for_field_group( array $field_group, array $acf_field_groups ): array {
586-
if ( ! $this->should_field_group_show_in_graphql( $field_group ) ) {
587-
return [];
588-
}
589647

590-
$graphql_types = $field_group['graphql_types'] ?? [];
648+
$graphql_types = [];
591649

592-
$field_group_name = '';
650+
if ( ! $this->should_field_group_show_in_graphql( $field_group ) ) {
651+
return $graphql_types;
652+
}
593653

594-
if ( ! empty( $field_group['graphql_field_name'] ) ) {
595-
$field_group_name = $field_group['graphql_field_name'];
596-
} elseif ( ! empty( $field_group['title'] ) ) {
597-
$field_group_name = $field_group['title'];
598-
} elseif ( ! empty( $field_group['name'] ) ) {
599-
$field_group_name = $field_group['name'];
654+
if ( ! empty( $field_group['graphql_types'] ) && is_array( $field_group['graphql_types'] ) ) {
655+
return $field_group['graphql_types'];
600656
}
601657

602-
if ( empty( $field_group_name ) ) {
603-
return [];
658+
if ( empty( $field_group['location'] ) ) {
659+
return $graphql_types;
604660
}
605661

606-
$field_group_name = Utils::format_field_name( $field_group_name, true );
662+
$field_group_name = Utils::get_field_group_name( $field_group );
663+
$field_group_name = \WPGraphQL\Utils\Utils::format_field_name( $field_group_name, true );
664+
// The fields are mapped as lowercase strings and should be retrieved as such
665+
// see: LocationRules.php
666+
$field_group_name = strtolower( $field_group_name );
607667

608-
$manually_set_graphql_types = isset( $field_group['map_graphql_types_from_location_rules'] ) && (bool) $field_group['map_graphql_types_from_location_rules'];
668+
$location_rules = $this->get_location_rules( $acf_field_groups );
609669

610-
if ( false === $manually_set_graphql_types || empty( $graphql_types ) ) {
611-
if ( empty( $field_group['graphql_types'] ) ) {
612-
$location_rules = $this->get_location_rules( $acf_field_groups );
613-
if ( isset( $location_rules[ $field_group_name ] ) ) {
614-
$graphql_types = $location_rules[ $field_group_name ];
615-
}
616-
}
670+
if ( isset( $location_rules[ $field_group_name ] ) ) {
671+
$graphql_types = $location_rules[ $field_group_name ];
617672
}
618673

619674
return ! empty( $graphql_types ) && is_array( $graphql_types ) ? array_unique( array_filter( $graphql_types ) ) : [];
@@ -648,7 +703,7 @@ public function register_acf_field_groups_to_graphql( array $acf_field_groups =
648703
if ( ! empty( $locations ) ) {
649704
$with_field_group_interface_name = 'WithAcf' . $type_name;
650705

651-
$field_name = Utils::format_field_name( $type_name, true );
706+
$field_name = \WPGraphQL\Utils\Utils::format_field_name( $type_name, true );
652707

653708
if ( ! $this->has_registered_field_group( $with_field_group_interface_name ) ) {
654709
register_graphql_interface_type(
@@ -680,7 +735,7 @@ public function register_acf_field_groups_to_graphql( array $acf_field_groups =
680735
}
681736

682737
// If the field group has locations defined (Types to be added to)
683-
// Add the
738+
// Add the WithAcf$FieldGroup Interface to the corresponding graphql_types
684739
register_graphql_interfaces_to_types( [ $with_field_group_interface_name ], $locations );
685740
}
686741

0 commit comments

Comments
 (0)