Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Ignore location rules when adding fields to the schema. Fixes #52. #82

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4c43dc5
Ignore location rules when adding fields to the schema. Fixes #52.
Oct 29, 2019
c78cbfa
Only ignore location when we're doing a GraphQL request.
peterjanes Oct 29, 2019
6a2de50
Fixed allowed post type check
igoojoe Oct 29, 2019
bcc8e42
Merge pull request #83 from igoojoe/patch-1
jasonbahl Oct 29, 2019
5d8377e
- Update version number
jasonbahl Nov 12, 2019
b15c191
Merge pull request #88 from jasonbahl/develop
jasonbahl Nov 12, 2019
3ede9a7
#91 - Don't add flex fields to the Schema if there are no layouts con…
jasonbahl Dec 3, 2019
5626a48
#93 - apply autop and nl2br on textarea fields depending on their for…
jasonbahl Dec 3, 2019
bc20b9d
Merge pull request #94 from jasonbahl/bug/#91-flex-field-with-no-layo…
jasonbahl Dec 3, 2019
239c0ca
Implement graphql_field_name for fields
esamattis Dec 5, 2019
1bb597b
Merge pull request #96 from esamattis/graphql_field_name
jasonbahl Dec 5, 2019
828ce2b
Merge pull request #95 from jasonbahl/feature/#93-textarea-autop
jasonbahl Dec 5, 2019
537ef66
target post-types changed
kidunot89 Dec 9, 2019
c5c36a4
source resolver filter added
kidunot89 Dec 10, 2019
8387fe0
Support fields for Google Maps field
dsturm Dec 13, 2019
e0dea5c
Merge pull request #101 from dsturm/google-map-add-fields-5.8.6
jasonbahl Dec 13, 2019
35753df
Merge commit 'c5c36a4079f2bf6f71c32ac53e4e4f0d5e27d185' into release/…
jasonbahl Dec 13, 2019
b1df7fb
- Update PostObjectFieldsTest
jasonbahl Dec 13, 2019
5943cd6
- Remove php 5.6 tests from Travis (we're supporting 7.0+)
jasonbahl Dec 13, 2019
2a377b1
Merge pull request #103 from wp-graphql/release/v0.3.1
jasonbahl Dec 13, 2019
00a771a
Ignore location rules when adding fields to the schema. Fixes #52.
Oct 29, 2019
b87dc54
Only ignore location when we're doing a GraphQL request.
peterjanes Oct 29, 2019
381cc25
Merge branch 'issue-52' of github.com:peterjanes/wp-graphql-acf into …
peterjanes Dec 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ matrix:
env: WP_VERSION=latest
- php: 7.0
env: WP_VERSION=trunk
- php: 5.6
env: WP_VERSION=4.9
- php: 5.6
env: WP_VERSION=latest
- php: 5.6
env: WP_TRAVISCI=phpcs

install:
- |
Expand Down
7 changes: 6 additions & 1 deletion src/class-acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ private function actions() {
* Setup filters
*/
private function filters() {

add_filter( 'acf/location/rule_match', function( $query ) {
if ( defined( 'GRAPHQL_HTTP_REQUEST' ) ) {
return true;
}
return $query;
});
}

/**
Expand Down
176 changes: 148 additions & 28 deletions src/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use WPGraphQL\Model\Post;
use WPGraphQL\Model\Term;
use WPGraphQL\Model\User;
use WPGraphQL\Types;

/**
* Config class.
Expand Down Expand Up @@ -367,7 +366,6 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {
case 'button_group':
case 'color_picker':
case 'email':
case 'textarea':
case 'text':
case 'message':
case 'oembed':
Expand All @@ -379,6 +377,24 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {
// 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':

/**
Expand Down Expand Up @@ -499,7 +515,7 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {
} else {
$type_names = [];
foreach ( $acf_field['post_type'] as $post_type ) {
if ( in_array( $post_type, \WPGraphQL::$allowed_post_types, true ) ) {
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;
}
}
Expand Down Expand Up @@ -531,7 +547,22 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {
return new Post( $value );
}

return absint( $value ) ? DataSource::resolve_post_object( (int) $value, $context ) : 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',
absint( $value ) ? DataSource::resolve_post_object( (int) $value, $context ) : null,
$value,
$context,
$info
);

},
];
Expand Down Expand Up @@ -672,34 +703,113 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {
$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' ),
'resolve' => function( $root ) {
return isset( $root['city'] ) ? $root['city'] : null;
},
],
'state' => [
'type' => 'String',
'description' => __( 'The state associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['state'] ) ? $root['state'] : null;
},
],
'stateShort' => [
'type' => 'String',
'description' => __( 'The state abbreviation associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['state_short'] ) ? $root['state_short'] : null;
},
],
'postCode' => [
'type' => 'String',
'description' => __( 'The post code associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['post_code'] ) ? $root['post_code'] : null;
},
],
'country' => [
'type' => 'String',
'description' => __( 'The country associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['country'] ) ? $root['country'] : null;
},
],
'countryShort' => [
'type' => 'String',
'description' => __( 'The country abbreviation associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['country_short'] ) ? $root['country_short'] : null;
},
],
'placeId' => [
'type' => 'String',
'description' => __( 'The country associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['place_id'] ) ? $root['place_id'] : null;
},
],
'zoom' => [
'type' => 'String',
'description' => __( 'The zoom defined with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['zoom'] ) ? $root['zoom'] : null;
},
],
];
}

register_graphql_object_type(
$field_type_name,
[
'description' => __( 'Google Map field', 'wp-graphql-acf' ),
'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;
},
],
],
'fields' => $fields,
]
);
$field_config['type'] = $field_type_name;
Expand Down Expand Up @@ -777,12 +887,21 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {

$flex_field_layout_name = ! empty( $layout['name'] ) ? ucfirst( self::camel_case( $layout['name'] ) ) : null;
$flex_field_layout_name = ! empty( $flex_field_layout_name ) ? $field_type_name . '_' . $flex_field_layout_name : null;

/**
* If there are no layouts defined for the Flex Field
*/
if ( empty( $flex_field_layout_name ) ) {
continue;
}

$layout_type = $this->type_registry->get_type( $flex_field_layout_name );

if ( $layout_type ) {
$union_types[ $layout['name'] ] = $layout_type;
} else {


register_graphql_object_type( $flex_field_layout_name, [
'description' => __( 'Group within the flex field', 'wp-graphql-acf' ),
'fields' => [
Expand Down Expand Up @@ -874,7 +993,8 @@ protected function add_field_group_fields( $field_group, $type_name ) {
/**
* Setup data for register_graphql_field
*/
$name = ! empty( $acf_field['name'] ) ? self::camel_case( $acf_field['name'] ) : null;
$explicit_name = ! empty( $acf_field['graphql_field_name'] ) ? $acf_field['graphql_field_name'] : null;
$name = empty( $explicit_name ) && ! empty( $acf_field['name'] ) ? self::camel_case( $acf_field['name'] ) : $explicit_name;
$show_in_graphql = isset( $acf_field['show_in_graphql'] ) ? (bool) $acf_field['show_in_graphql'] : true;
$description = isset( $acf_field['instructions'] ) ? $acf_field['instructions'] : __( 'ACF Field added to the Schema by WPGraphQL ACF' );

Expand Down
8 changes: 4 additions & 4 deletions tests/wpunit/PostObjectFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ public function testQueryMultipleSelectField() {
id
title
postFields {
selectFieldMultiple
selectMultiple
}
}
}';
Expand All @@ -1135,7 +1135,7 @@ public function testQueryMultipleSelectField() {
codecept_debug( $actual );

$this->assertArrayNotHasKey( 'errors', $actual );
$this->assertSame( $expected_value, $actual['data']['postBy']['postFields']['selectFieldMultiple'] );
$this->assertSame( $expected_value, $actual['data']['postBy']['postFields']['selectMultiple'] );


}
Expand Down Expand Up @@ -1169,7 +1169,7 @@ public function testQueryMultipleSelectFieldWithNoValueSet() {
id
title
postFields {
selectFieldMultiple
selectMultiple
}
}
}';
Expand All @@ -1185,7 +1185,7 @@ public function testQueryMultipleSelectFieldWithNoValueSet() {

codecept_debug( $actual );

$this->assertSame( [], $actual['data']['postBy']['postFields']['selectFieldMultiple'] );
$this->assertSame( [], $actual['data']['postBy']['postFields']['selectMultiple'] );

}

Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit3fbb9843644ca7389b188c6353f475fc::getLoader();
return ComposerAutoloaderInit2c922a3b1933e10e7ad3704a3cd98524::getLoader();
7 changes: 0 additions & 7 deletions vendor/autoload_commands.php

This file was deleted.

7 changes: 0 additions & 7 deletions vendor/autoload_framework.php

This file was deleted.

Loading