Skip to content

Commit a9033bb

Browse files
authored
Merge pull request #158 from kidunot89/feature/wp-graphql-acf-integration
Fixes WPGraphQL ACF integration
2 parents 95f4402 + 360a303 commit a9033bb

15 files changed

+123
-11
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ branches:
2222
cache:
2323
apt: true
2424
directories:
25-
- vendor
2625
- $HOME/.composer/cache
2726

2827
matrix:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Adds filters that modify WPGraphQL ACF schema.
4+
*
5+
* @package \WPGraphQL\Extensions\WooCommerce
6+
* @since 0.2.2
7+
*/
8+
9+
namespace WPGraphQL\Extensions\WooCommerce;
10+
11+
/**
12+
* Class ACF_Schema_Filters
13+
*/
14+
class ACF_Schema_Filters {
15+
16+
/**
17+
* Register filters
18+
*/
19+
public static function add_filters() {
20+
// Registers WooCommerce CPTs && taxonomies.
21+
add_filter( 'graphql_acf_get_root_id', array( __CLASS__, 'resolve_crud_root_id' ), 10, 2 );
22+
}
23+
24+
/**
25+
* Resolve post object ID from CRUD object Model.
26+
*
27+
* @param integer|null $id Post object database ID.
28+
* @param mixed $root Root resolver.
29+
*
30+
* @return integer|null
31+
*/
32+
public static function resolve_crud_root_id( $id, $root ) {
33+
switch ( true ) {
34+
case $root instanceof \WPGraphQL\Extensions\WooCommerce\Model\CRUD_CPT:
35+
$id = absint( $root->ID );
36+
break;
37+
}
38+
39+
return $id;
40+
}
41+
}

includes/class-core-schema-filters.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class Core_Schema_Filters {
3333
* Register filters
3434
*/
3535
public static function add_filters() {
36+
// Registers WooCommerce CPTs.
37+
add_filter( 'register_post_type_args', array( __CLASS__, 'register_post_types' ), 10, 2 );
38+
add_filter( 'graphql_post_entities_allowed_post_types', array( __CLASS__, 'skip_type_registry' ), 10 );
39+
add_filter( 'graphql_union_resolve_type', array( __CLASS__, 'graphql_union_resolve_type' ), 10, 3 );
40+
3641
// Registers WooCommerce taxonomies.
3742
add_filter( 'register_taxonomy_args', array( __CLASS__, 'register_taxonomy_args' ), 10, 2 );
3843

@@ -102,10 +107,72 @@ public static function customer_loader( $context ) {
102107
return self::$customer_loader;
103108
}
104109

110+
/**
111+
* Registers WooCommerce post-types to be used in GraphQL schema
112+
*
113+
* @param array $args - allowed post-types.
114+
* @param string $post_type - name of taxonomy being checked.
115+
*
116+
* @return array
117+
*/
118+
public static function register_post_types( $args, $post_type ) {
119+
if ( 'product' === $post_type ) {
120+
$args['show_in_graphql'] = true;
121+
$args['graphql_single_name'] = 'Product';
122+
$args['graphql_plural_name'] = 'Products';
123+
$args['skip_graphql_type_registry'] = true;
124+
}
125+
if ( 'product_variation' === $post_type ) {
126+
$args['show_in_graphql'] = true;
127+
$args['graphql_single_name'] = 'ProductVariation';
128+
$args['graphql_plural_name'] = 'ProductVariations';
129+
$args['skip_graphql_type_registry'] = true;
130+
}
131+
if ( 'shop_coupon' === $post_type ) {
132+
$args['show_in_graphql'] = true;
133+
$args['graphql_single_name'] = 'Coupon';
134+
$args['graphql_plural_name'] = 'Coupons';
135+
$args['skip_graphql_type_registry'] = true;
136+
}
137+
if ( 'shop_order' === $post_type ) {
138+
$args['show_in_graphql'] = true;
139+
$args['graphql_single_name'] = 'Order';
140+
$args['graphql_plural_name'] = 'Orders';
141+
$args['skip_graphql_type_registry'] = true;
142+
}
143+
if ( 'shop_order_refund' === $post_type ) {
144+
$args['show_in_graphql'] = true;
145+
$args['graphql_single_name'] = 'Refund';
146+
$args['graphql_plural_name'] = 'Refunds';
147+
$args['skip_graphql_type_registry'] = true;
148+
}
149+
150+
return $args;
151+
}
152+
153+
/**
154+
* Filters "allowed_post_types" and removed Woocommerce CPTs.
155+
*
156+
* @param array $post_types Post types registered in GraphQL schema.
157+
*
158+
* @return array
159+
*/
160+
public static function skip_type_registry( $post_types ) {
161+
return array_diff(
162+
$post_types,
163+
get_post_types(
164+
[
165+
'show_in_graphql' => true,
166+
'skip_graphql_type_registry' => true,
167+
]
168+
)
169+
);
170+
}
171+
105172
/**
106173
* Registers WooCommerce taxonomies to be used in GraphQL schema
107174
*
108-
* @param array $args - allowed post-types.
175+
* @param array $args - allowed taxonomies.
109176
* @param string $taxonomy - name of taxonomy being checked.
110177
*
111178
* @return array

includes/class-wp-graphql-woocommerce.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ private function setup() {
149149
// Register WPGraphQL core filters.
150150
\WPGraphQL\Extensions\WooCommerce\Core_Schema_Filters::add_filters();
151151

152+
// Register WPGraphQL ACF filters.
153+
\WPGraphQL\Extensions\WooCommerce\ACF_Schema_Filters::add_filters();
154+
152155
// Register WPGraphQL JWT Authentication filters.
153156
\WPGraphQL\Extensions\WooCommerce\JWT_Auth_Schema_Filters::add_filters();
154157

includes/data/connection/class-product-connection-resolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ public function sanitize_input_fields( array $where_args ) {
487487

488488
// Handle visibility.
489489
$post_type_obj = get_post_type_object( $this->post_type );
490-
if ( ! empty( $where_args['visibility'] ) && current_user_can( $post_type_obj->cap->read_private_posts ) ) {
490+
if ( ! empty( $where_args['visibility'] ) ) {
491491
switch ( $where_args['visibility'] ) {
492492
case 'search':
493493
$tax_query[] = array(

includes/type/object/class-coupon-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function register() {
3030
'Coupon',
3131
array(
3232
'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
33-
'interfaces' => array('Node'),
33+
'interfaces' => array( 'Node' ),
3434
'fields' => array(
3535
'id' => array(
3636
'type' => array( 'non_null' => 'ID' ),

includes/type/object/class-customer-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function register() {
3131
'Customer',
3232
array(
3333
'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
34-
'interfaces' => array('Node'),
34+
'interfaces' => array( 'Node' ),
3535
'fields' => array(
3636
'id' => array(
3737
'type' => array( 'non_null' => 'ID' ),

includes/type/object/class-order-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function register() {
3030
'Order',
3131
array(
3232
'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
33-
'interfaces' => array('Node'),
33+
'interfaces' => array( 'Node' ),
3434
'fields' => array(
3535
'id' => array(
3636
'type' => array( 'non_null' => 'ID' ),

includes/type/object/class-product-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function register() {
3131
'Product',
3232
array(
3333
'description' => __( 'A product object', 'wp-graphql-woocommerce' ),
34-
'interfaces' => array('Node'),
34+
'interfaces' => array( 'Node' ),
3535
'fields' => array(
3636
'id' => array(
3737
'type' => array( 'non_null' => 'ID' ),

includes/type/object/class-product-variation-type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function register() {
3131
'ProductVariation',
3232
array(
3333
'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
34-
'interfaces' => array('Node'),
34+
'interfaces' => array( 'Node' ),
3535
'fields' => array(
3636
'id' => array(
3737
'type' => array( 'non_null' => 'ID' ),

0 commit comments

Comments
 (0)