diff --git a/.travis.yml b/.travis.yml index 73744c9a..17e9168e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,6 @@ branches: cache: apt: true directories: - - vendor - $HOME/.composer/cache matrix: diff --git a/includes/class-acf-schema-filters.php b/includes/class-acf-schema-filters.php new file mode 100644 index 00000000..ab1f8a92 --- /dev/null +++ b/includes/class-acf-schema-filters.php @@ -0,0 +1,41 @@ +ID ); + break; + } + + return $id; + } +} diff --git a/includes/class-core-schema-filters.php b/includes/class-core-schema-filters.php index 542a1182..f1115364 100644 --- a/includes/class-core-schema-filters.php +++ b/includes/class-core-schema-filters.php @@ -33,6 +33,11 @@ class Core_Schema_Filters { * Register filters */ public static function add_filters() { + // Registers WooCommerce CPTs. + add_filter( 'register_post_type_args', array( __CLASS__, 'register_post_types' ), 10, 2 ); + add_filter( 'graphql_post_entities_allowed_post_types', array( __CLASS__, 'skip_type_registry' ), 10 ); + add_filter( 'graphql_union_resolve_type', array( __CLASS__, 'graphql_union_resolve_type' ), 10, 3 ); + // Registers WooCommerce taxonomies. add_filter( 'register_taxonomy_args', array( __CLASS__, 'register_taxonomy_args' ), 10, 2 ); @@ -102,10 +107,72 @@ class Core_Schema_Filters { return self::$customer_loader; } + /** + * Registers WooCommerce post-types to be used in GraphQL schema + * + * @param array $args - allowed post-types. + * @param string $post_type - name of taxonomy being checked. + * + * @return array + */ + public static function register_post_types( $args, $post_type ) { + if ( 'product' === $post_type ) { + $args['show_in_graphql'] = true; + $args['graphql_single_name'] = 'Product'; + $args['graphql_plural_name'] = 'Products'; + $args['skip_graphql_type_registry'] = true; + } + if ( 'product_variation' === $post_type ) { + $args['show_in_graphql'] = true; + $args['graphql_single_name'] = 'ProductVariation'; + $args['graphql_plural_name'] = 'ProductVariations'; + $args['skip_graphql_type_registry'] = true; + } + if ( 'shop_coupon' === $post_type ) { + $args['show_in_graphql'] = true; + $args['graphql_single_name'] = 'Coupon'; + $args['graphql_plural_name'] = 'Coupons'; + $args['skip_graphql_type_registry'] = true; + } + if ( 'shop_order' === $post_type ) { + $args['show_in_graphql'] = true; + $args['graphql_single_name'] = 'Order'; + $args['graphql_plural_name'] = 'Orders'; + $args['skip_graphql_type_registry'] = true; + } + if ( 'shop_order_refund' === $post_type ) { + $args['show_in_graphql'] = true; + $args['graphql_single_name'] = 'Refund'; + $args['graphql_plural_name'] = 'Refunds'; + $args['skip_graphql_type_registry'] = true; + } + + return $args; + } + + /** + * Filters "allowed_post_types" and removed Woocommerce CPTs. + * + * @param array $post_types Post types registered in GraphQL schema. + * + * @return array + */ + public static function skip_type_registry( $post_types ) { + return array_diff( + $post_types, + get_post_types( + [ + 'show_in_graphql' => true, + 'skip_graphql_type_registry' => true, + ] + ) + ); + } + /** * Registers WooCommerce taxonomies to be used in GraphQL schema * - * @param array $args - allowed post-types. + * @param array $args - allowed taxonomies. * @param string $taxonomy - name of taxonomy being checked. * * @return array diff --git a/includes/class-wp-graphql-woocommerce.php b/includes/class-wp-graphql-woocommerce.php index 5fa4b591..a0bae8f0 100644 --- a/includes/class-wp-graphql-woocommerce.php +++ b/includes/class-wp-graphql-woocommerce.php @@ -149,6 +149,9 @@ if ( ! class_exists( 'WP_GraphQL_WooCommerce' ) ) : // Register WPGraphQL core filters. \WPGraphQL\Extensions\WooCommerce\Core_Schema_Filters::add_filters(); + // Register WPGraphQL ACF filters. + \WPGraphQL\Extensions\WooCommerce\ACF_Schema_Filters::add_filters(); + // Register WPGraphQL JWT Authentication filters. \WPGraphQL\Extensions\WooCommerce\JWT_Auth_Schema_Filters::add_filters(); diff --git a/includes/data/connection/class-product-connection-resolver.php b/includes/data/connection/class-product-connection-resolver.php index 1f9631f5..8fb7e8d5 100644 --- a/includes/data/connection/class-product-connection-resolver.php +++ b/includes/data/connection/class-product-connection-resolver.php @@ -487,7 +487,7 @@ class Product_Connection_Resolver extends AbstractConnectionResolver { // Handle visibility. $post_type_obj = get_post_type_object( $this->post_type ); - if ( ! empty( $where_args['visibility'] ) && current_user_can( $post_type_obj->cap->read_private_posts ) ) { + if ( ! empty( $where_args['visibility'] ) ) { switch ( $where_args['visibility'] ) { case 'search': $tax_query[] = array( diff --git a/includes/type/object/class-coupon-type.php b/includes/type/object/class-coupon-type.php index 10d3405e..0c46611f 100644 --- a/includes/type/object/class-coupon-type.php +++ b/includes/type/object/class-coupon-type.php @@ -30,7 +30,7 @@ class Coupon_Type { 'Coupon', array( 'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ), - 'interfaces' => array('Node'), + 'interfaces' => array( 'Node' ), 'fields' => array( 'id' => array( 'type' => array( 'non_null' => 'ID' ), diff --git a/includes/type/object/class-customer-type.php b/includes/type/object/class-customer-type.php index b5d50484..b34aa640 100644 --- a/includes/type/object/class-customer-type.php +++ b/includes/type/object/class-customer-type.php @@ -31,7 +31,7 @@ class Customer_Type { 'Customer', array( 'description' => __( 'A customer object', 'wp-graphql-woocommerce' ), - 'interfaces' => array('Node'), + 'interfaces' => array( 'Node' ), 'fields' => array( 'id' => array( 'type' => array( 'non_null' => 'ID' ), diff --git a/includes/type/object/class-order-type.php b/includes/type/object/class-order-type.php index d01e1aac..1001ac9d 100644 --- a/includes/type/object/class-order-type.php +++ b/includes/type/object/class-order-type.php @@ -30,7 +30,7 @@ class Order_Type { 'Order', array( 'description' => __( 'A order object', 'wp-graphql-woocommerce' ), - 'interfaces' => array('Node'), + 'interfaces' => array( 'Node' ), 'fields' => array( 'id' => array( 'type' => array( 'non_null' => 'ID' ), diff --git a/includes/type/object/class-product-type.php b/includes/type/object/class-product-type.php index 49eeaf2c..5a06faec 100644 --- a/includes/type/object/class-product-type.php +++ b/includes/type/object/class-product-type.php @@ -31,7 +31,7 @@ class Product_Type { 'Product', array( 'description' => __( 'A product object', 'wp-graphql-woocommerce' ), - 'interfaces' => array('Node'), + 'interfaces' => array( 'Node' ), 'fields' => array( 'id' => array( 'type' => array( 'non_null' => 'ID' ), diff --git a/includes/type/object/class-product-variation-type.php b/includes/type/object/class-product-variation-type.php index 3cb6a301..a05a4344 100644 --- a/includes/type/object/class-product-variation-type.php +++ b/includes/type/object/class-product-variation-type.php @@ -31,7 +31,7 @@ class Product_Variation_Type { 'ProductVariation', array( 'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ), - 'interfaces' => array('Node'), + 'interfaces' => array( 'Node' ), 'fields' => array( 'id' => array( 'type' => array( 'non_null' => 'ID' ), diff --git a/includes/type/object/class-refund-type.php b/includes/type/object/class-refund-type.php index 05902ad5..c9e3992a 100644 --- a/includes/type/object/class-refund-type.php +++ b/includes/type/object/class-refund-type.php @@ -30,7 +30,7 @@ class Refund_Type { 'Refund', array( 'description' => __( 'A refund object', 'wp-graphql-woocommerce' ), - 'interfaces' => array('Node'), + 'interfaces' => array( 'Node' ), 'fields' => array( 'id' => array( 'type' => array( 'non_null' => 'ID' ), diff --git a/includes/type/object/class-shipping-method-type.php b/includes/type/object/class-shipping-method-type.php index af1ae891..f241ef19 100644 --- a/includes/type/object/class-shipping-method-type.php +++ b/includes/type/object/class-shipping-method-type.php @@ -30,7 +30,7 @@ class Shipping_Method_Type { 'ShippingMethod', array( 'description' => __( 'A shipping method object', 'wp-graphql-woocommercer' ), - 'interfaces' => array('Node'), + 'interfaces' => array( 'Node' ), 'fields' => array( 'id' => array( 'type' => array( 'non_null' => 'ID' ), diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index dc8f1f9b..fcc8e1b3 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -6,6 +6,7 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( + 'WPGraphQL\\Extensions\\WooCommerce\\ACF_Schema_Filters' => $baseDir . '/includes/class-acf-schema-filters.php', 'WPGraphQL\\Extensions\\WooCommerce\\Connection\\Cart_Items' => $baseDir . '/includes/connection/class-cart-items.php', 'WPGraphQL\\Extensions\\WooCommerce\\Connection\\Coupons' => $baseDir . '/includes/connection/class-coupons.php', 'WPGraphQL\\Extensions\\WooCommerce\\Connection\\Customers' => $baseDir . '/includes/connection/class-customers.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 2f336695..ee8e74eb 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -25,6 +25,7 @@ class ComposerStaticInit71eeabde6fbf0f089c103ca7b0ed7868 ); public static $classMap = array ( + 'WPGraphQL\\Extensions\\WooCommerce\\ACF_Schema_Filters' => __DIR__ . '/../..' . '/includes/class-acf-schema-filters.php', 'WPGraphQL\\Extensions\\WooCommerce\\Connection\\Cart_Items' => __DIR__ . '/../..' . '/includes/connection/class-cart-items.php', 'WPGraphQL\\Extensions\\WooCommerce\\Connection\\Coupons' => __DIR__ . '/../..' . '/includes/connection/class-coupons.php', 'WPGraphQL\\Extensions\\WooCommerce\\Connection\\Customers' => __DIR__ . '/../..' . '/includes/connection/class-customers.php', diff --git a/wp-graphql-woocommerce.php b/wp-graphql-woocommerce.php index 27c6f5e3..ee87abe0 100644 --- a/wp-graphql-woocommerce.php +++ b/wp-graphql-woocommerce.php @@ -94,7 +94,7 @@ function wp_graphql_woocommerce_init() {