Skip to content

Commit 9082d9c

Browse files
authored
Merge pull request #102 from wp-graphql/fix/#97-location-mapping-bugs
fix: #97 location mapping bugs
2 parents 90a4fc1 + 2426aa5 commit 9082d9c

File tree

9 files changed

+170
-132
lines changed

9 files changed

+170
-132
lines changed

composer.lock

Lines changed: 30 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FieldConfig.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,30 @@ public function should_format_field_value( string $field_type ): bool {
285285
$types_to_format = [
286286
'select',
287287
'wysiwyg',
288+
'repeater',
288289
];
289290

290291
return in_array( $field_type, $types_to_format, true );
291292
}
292293

294+
/**
295+
* @param string $selector
296+
* @param string|null $parent_field_name
297+
* @param string|int|null $post_id
298+
* @param bool $should_format
299+
*
300+
* @return false|mixed
301+
*/
302+
protected function get_field( string $selector, ?string $parent_field_name = null, $post_id = null, bool $should_format = false ) {
303+
if ( ! empty( $parent_field_name ) ) {
304+
$value = get_sub_field( $selector, $should_format );
305+
} else {
306+
$value = get_field( $selector, $post_id, $should_format );
307+
}
308+
309+
return $value;
310+
}
311+
293312
/**
294313
* @param mixed $root
295314
* @param array $args
@@ -378,25 +397,23 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI
378397
}
379398
}
380399

400+
381401
// resolve block field
382-
if ( is_array( $node ) && isset( $node['blockName'] ) ) {
383-
if ( isset( $node['attrs']['data'] ) ) {
384-
$block_id = acf_get_block_id( $node['attrs']['data'] );
385-
acf_setup_meta( $node['attrs']['data'], $block_id, true );
386-
acf_prepare_block( $node['attrs'] );
387-
$return_value = get_field( $field_config['name'], $block_id, $should_format_value );
388-
389-
if ( empty( $return_value ) && isset( $node['attrs']['data'][ $field_config['name'] ] ) ) {
390-
$return_value = $node['attrs']['data'][ $field_config['name'] ];
391-
}
392-
393-
if ( empty( $return_value ) && ( ! empty( $parent_field_name ) && ! empty( $node['attrs']['data'][ $parent_field_name . '_' . $field_config['name'] ] ) ) ) {
394-
$return_value = $node['attrs']['data'][ $parent_field_name . '_' . $field_config['name'] ];
395-
}
396-
} elseif ( isset( $node['attrs'] ) ) {
397-
acf_prepare_block( $node['attrs'] );
398-
$return_value = get_field( $field_config['name'], false, $should_format_value );
399-
acf_reset_meta();
402+
if ( is_array( $node ) && isset( $node['blockName'] ) && isset( $node['attrs'] ) ) {
403+
$block = acf_prepare_block( $node['attrs'] );
404+
$block_id = acf_get_block_id( $node['attrs'] );
405+
$block_id = acf_ensure_block_id_prefix( $block_id );
406+
acf_setup_meta( $block['data'], $block_id, true );
407+
408+
$return_value = $this->get_field( $field_config['name'], $parent_field_name, $block_id, $should_format_value );
409+
acf_reset_meta( $block_id );
410+
411+
if ( empty( $return_value ) && isset( $node['attrs']['data'][ $field_config['name'] ] ) ) {
412+
$return_value = $node['attrs']['data'][ $field_config['name'] ];
413+
}
414+
415+
if ( empty( $return_value ) && ( ! empty( $parent_field_name ) && ! empty( $node['attrs']['data'][ $parent_field_name . '_' . $field_config['name'] ] ) ) ) {
416+
$return_value = $node['attrs']['data'][ $parent_field_name . '_' . $field_config['name'] ];
400417
}
401418
}
402419

@@ -407,7 +424,7 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI
407424

408425
// if a value hasn't been set yet, use the get_field() function to get the value
409426
if ( empty( $return_value ) ) {
410-
$return_value = get_field( $field_key, $node_id, $should_format_value );
427+
$return_value = $this->get_field( $field_key, $parent_field_name, $node_id, $should_format_value );
411428
}
412429

413430

src/FieldType/Gallery.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public static function register_field_type(): void {
3737
'resolve' => static function ( $root, $args, AppContext $context, $info ) use ( $field_config ) {
3838
$value = $field_config->resolve_field( $root, $args, $context, $info );
3939

40+
if ( empty( $value ) ) {
41+
return null;
42+
}
43+
4044
$value = array_filter( $value );
4145

4246
if ( empty( $value ) ) {

src/FieldType/Repeater.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public static function register_field_type(): void {
3131

3232
return [ 'list_of' => $type_name ];
3333
},
34-
3534
]
3635
);
3736
}

src/LocationRules/LocationRules.php

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __construct( array $acf_field_groups = [] ) {
6363
* @return void
6464
*/
6565
public function set_graphql_type( string $field_group_name, string $graphql_type_name ): void {
66-
$this->mapped_field_groups[ Utils::format_field_name( $field_group_name, true ) ][] = Utils::format_type_name( $graphql_type_name );
66+
$this->mapped_field_groups[ Utils::format_field_name( $field_group_name, true ) ][] = ucfirst( Utils::format_field_name( $graphql_type_name, true ) );
6767
}
6868

6969
/**
@@ -76,7 +76,7 @@ public function set_graphql_type( string $field_group_name, string $graphql_type
7676
* @return void
7777
*/
7878
public function unset_graphql_type( string $field_group_name, string $graphql_type_name ): void {
79-
$this->unset_types[ Utils::format_field_name( $field_group_name, true ) ][] = Utils::format_type_name( $graphql_type_name );
79+
$this->unset_types[ Utils::format_field_name( $field_group_name, true ) ][] = ucfirst( Utils::format_field_name( $graphql_type_name, true ) );
8080
}
8181

8282
/**
@@ -323,6 +323,9 @@ public function determine_rules( string $field_group_name, string $param, string
323323
case 'widget':
324324
// @todo: Widgets are not currently supported in WPGraphQL
325325
break;
326+
case 'block':
327+
$this->determine_block_rules( $field_group_name, $param, $operator, $value );
328+
break;
326329
case 'nav_menu':
327330
$this->determine_nav_menu_rules( $field_group_name, $param, $operator, $value );
328331
break;
@@ -398,29 +401,11 @@ public function get_graphql_post_template_types(): array {
398401
'default' => 'DefaultTemplate',
399402
];
400403

401-
$registered_page_templates = wp_get_theme()->get_post_templates();
402-
403-
if ( ! empty( $registered_page_templates ) && is_array( $registered_page_templates ) ) {
404-
foreach ( $registered_page_templates as $post_type_templates ) {
405-
// Post templates are returned as an array of arrays. PHPStan believes they're returned as
406-
// an array of strings and believes this will always evaluate to false.
407-
// We should ignore the phpstan check here.
408-
if ( ! empty( $post_type_templates ) && is_array( $post_type_templates ) ) {
409-
foreach ( $post_type_templates as $file => $name ) {
410-
$name = ucwords( $name );
411-
$replaced_name = preg_replace( '/[^\w]/', '', $name );
412-
413-
if ( ! empty( $replaced_name ) ) {
414-
$name = $replaced_name;
415-
}
416-
417-
if ( preg_match( '/^\d/', $name ) || false === strpos( strtolower( $name ), 'template' ) ) {
418-
$name = 'Template_' . $name;
419-
}
420-
421-
$page_templates[ $file ] = $name;
422-
}
423-
}
404+
$allowed_post_types = \WPGraphQL::get_allowed_post_types();
405+
foreach ( $allowed_post_types as $post_type ) {
406+
$post_type_templates = wp_get_theme()->get_page_templates( null, $post_type );
407+
foreach ( $post_type_templates as $file => $name ) {
408+
$page_templates[ $file ] = Utils::format_type_name_for_wp_template( $name, $file );
424409
}
425410
}
426411

@@ -499,16 +484,13 @@ public function determine_post_type_rules( string $field_group_name, string $par
499484
public function determine_post_template_rules( string $field_group_name, string $param, string $operator, string $value ): void {
500485
$templates = $this->get_graphql_post_template_types();
501486

502-
if ( ! is_array( $templates ) || empty( $templates ) ) {
487+
if ( empty( $templates ) ) {
503488
return;
504489
}
505490

506-
if ( '==' === $operator ) {
507-
508-
// If the template is available in GraphQL, set it
509-
if ( isset( $templates[ $value ] ) ) {
510-
$this->set_graphql_type( $field_group_name, $templates[ $value ] );
511-
}
491+
// If the template is available in GraphQL, set it
492+
if ( ( '==' === $operator ) && isset( $templates[ $value ] ) ) {
493+
$this->set_graphql_type( $field_group_name, $templates[ $value ] );
512494
}
513495

514496
if ( '!=' === $operator ) {

0 commit comments

Comments
 (0)