From c5f86b1bab23129a8de2b16276c6cfcea13a0612 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Tue, 9 Jul 2019 22:40:54 -0400 Subject: [PATCH 1/2] "image" field added to "ProductCategory". --- includes/class-actions.php | 17 +++++++++++++++++ tests/wpunit/ProductQueriesTest.php | 10 +++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/includes/class-actions.php b/includes/class-actions.php index ba2ba8620..7a8e27667 100644 --- a/includes/class-actions.php +++ b/includes/class-actions.php @@ -10,6 +10,8 @@ namespace WPGraphQL\Extensions\WooCommerce; +use WPGraphQL\AppContext; +use WPGraphQL\Data\DataSource; use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Backorders; use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Catalog_Visibility; use WPGraphQL\Extensions\WooCommerce\Type\WPEnum\Countries; @@ -172,6 +174,21 @@ public static function graphql_register_types() { Order_Delete::register_mutation(); Checkout::register_mutation(); + register_graphql_field( + 'ProductCategory', + 'image', + array( + 'type' => 'MediaItem', + 'description' => __( 'Product category image', 'wp-graphql-woocommerce' ), + 'resolve' => function( $source, array $args, AppContext $context ) { + $thumbnail_id = get_term_meta( $source->term_id, 'thumbnail_id', true ); + return ! empty( $thumbnail_id ) + ? DataSource::resolve_post_object( $thumbnail_id, $context ) + : null; + }, + ) + ); + if ( class_exists( '\WPGraphQL\JWT_Authentication\ManageTokens' ) ) { $fields = array(); foreach ( \WPGraphQL\JWT_Authentication\ManageTokens::add_user_fields() as $field_name => $field ) { diff --git a/tests/wpunit/ProductQueriesTest.php b/tests/wpunit/ProductQueriesTest.php index 6e7507cad..c70c3ab30 100644 --- a/tests/wpunit/ProductQueriesTest.php +++ b/tests/wpunit/ProductQueriesTest.php @@ -29,15 +29,17 @@ public function setUp() { 'post_content' => 'product image', ) ); + $category_id = $this->helper->create_product_category( $this->product_cat ); $this->product = $this->helper->create_simple( array( 'tag_ids' => array( $this->helper->create_product_tag( $this->product_tag ) ), - 'category_ids' => array( $this->helper->create_product_category( $this->product_cat ) ), + 'category_ids' => array( $category_id ), 'image_id' => $this->image_id, 'gallery_image_ids' => array( $this->image_id ), 'downloads' => array( ProductHelper::create_download() ), ) ); + update_term_meta( $category_id, 'thumbnail_id', $this->image_id ); } public function tearDown() { @@ -484,6 +486,9 @@ public function testTermToProductConnection() { productCategories( where: { hideEmpty: true } ) { nodes { name + image { + id + } products { nodes { id @@ -515,6 +520,9 @@ public function testTermToProductConnection() { 'nodes' => array( array( 'name' => $this->product_cat, + 'image' => array( + 'id' => Relay::toGlobalId( 'attachment', $this->image_id ), + ), 'products' => array( 'nodes' => array( array ( From 0171ff5dbd8f712ec941de879a195eeb1f8ab516 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Tue, 9 Jul 2019 23:01:09 -0400 Subject: [PATCH 2/2] "Actions" class cleaned up and refactored. --- includes/class-actions.php | 39 ++---------------- includes/type/object/class-customer-type.php | 20 +++++++++ .../object/class-product-category-type.php | 41 +++++++++++++++++++ vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 5 files changed, 67 insertions(+), 35 deletions(-) create mode 100644 includes/type/object/class-product-category-type.php diff --git a/includes/class-actions.php b/includes/class-actions.php index 7a8e27667..7c76900dd 100644 --- a/includes/class-actions.php +++ b/includes/class-actions.php @@ -49,6 +49,7 @@ use WPGraphQL\Extensions\WooCommerce\Type\WPObject\Shipping_Method_Type; use WPGraphQL\Extensions\WooCommerce\Type\WPObject\Cart_Type; use WPGraphQL\Extensions\WooCommerce\Type\WPObject\Variation_Attribute_Type; +use WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Category_Type; use WPGraphQL\Extensions\WooCommerce\Connection\Posts; use WPGraphQL\Extensions\WooCommerce\Connection\WC_Terms; use WPGraphQL\Extensions\WooCommerce\Connection\Coupons; @@ -143,6 +144,9 @@ public static function graphql_register_types() { Cart_Type::register(); Variation_Attribute_Type::register(); + // Object fields. + Product_Category_Type::register_fields(); + // Connections. Posts::register_connections(); WC_Terms::register_connections(); @@ -173,40 +177,5 @@ public static function graphql_register_types() { Order_Update::register_mutation(); Order_Delete::register_mutation(); Checkout::register_mutation(); - - register_graphql_field( - 'ProductCategory', - 'image', - array( - 'type' => 'MediaItem', - 'description' => __( 'Product category image', 'wp-graphql-woocommerce' ), - 'resolve' => function( $source, array $args, AppContext $context ) { - $thumbnail_id = get_term_meta( $source->term_id, 'thumbnail_id', true ); - return ! empty( $thumbnail_id ) - ? DataSource::resolve_post_object( $thumbnail_id, $context ) - : null; - }, - ) - ); - - if ( class_exists( '\WPGraphQL\JWT_Authentication\ManageTokens' ) ) { - $fields = array(); - foreach ( \WPGraphQL\JWT_Authentication\ManageTokens::add_user_fields() as $field_name => $field ) { - $root_resolver = $field['resolve']; - $fields[ $field_name ] = array_merge( - $field, - array( - 'resolve' => function( $source ) use ( $root_resolver ) { - $user = get_user_by( 'id', $source->ID ); - return $root_resolver( $user ); - }, - ) - ); - } - register_graphql_fields( - 'Customer', - $fields - ); - } } } diff --git a/includes/type/object/class-customer-type.php b/includes/type/object/class-customer-type.php index 4d43af73b..eee6f6ca9 100644 --- a/includes/type/object/class-customer-type.php +++ b/includes/type/object/class-customer-type.php @@ -183,5 +183,25 @@ public static function register() { }, ) ); + + if ( class_exists( '\WPGraphQL\JWT_Authentication\ManageTokens' ) ) { + $fields = array(); + foreach ( \WPGraphQL\JWT_Authentication\ManageTokens::add_user_fields() as $field_name => $field ) { + $root_resolver = $field['resolve']; + $fields[ $field_name ] = array_merge( + $field, + array( + 'resolve' => function( $source ) use ( $root_resolver ) { + $user = get_user_by( 'id', $source->ID ); + return $root_resolver( $user ); + }, + ) + ); + } + register_graphql_fields( + 'Customer', + $fields + ); + } } } diff --git a/includes/type/object/class-product-category-type.php b/includes/type/object/class-product-category-type.php new file mode 100644 index 000000000..93c2ba474 --- /dev/null +++ b/includes/type/object/class-product-category-type.php @@ -0,0 +1,41 @@ + 'MediaItem', + 'description' => __( 'Product category image', 'wp-graphql-woocommerce' ), + 'resolve' => function( $source, array $args, AppContext $context ) { + $thumbnail_id = get_term_meta( $source->term_id, 'thumbnail_id', true ); + return ! empty( $thumbnail_id ) + ? DataSource::resolve_post_object( $thumbnail_id, $context ) + : null; + }, + ) + ); + } +} diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 922c087b4..8f00419d6 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -98,6 +98,7 @@ 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Order_Item_Type' => $baseDir . '/includes/type/object/class-order-item-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Order_Type' => $baseDir . '/includes/type/object/class-order-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Attribute_Type' => $baseDir . '/includes/type/object/class-product-attribute-type.php', + 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Category_Type' => $baseDir . '/includes/type/object/class-product-category-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Download_Type' => $baseDir . '/includes/type/object/class-product-download-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Type' => $baseDir . '/includes/type/object/class-product-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Variation_Type' => $baseDir . '/includes/type/object/class-product-variation-type.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 92e3b9e42..660d341b3 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -113,6 +113,7 @@ class ComposerStaticInitee0d17af17b841ed3a93c4a0e5cc5e5f 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Order_Item_Type' => __DIR__ . '/../..' . '/includes/type/object/class-order-item-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Order_Type' => __DIR__ . '/../..' . '/includes/type/object/class-order-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Attribute_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-attribute-type.php', + 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Category_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-category-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Download_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-download-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-type.php', 'WPGraphQL\\Extensions\\WooCommerce\\Type\\WPObject\\Product_Variation_Type' => __DIR__ . '/../..' . '/includes/type/object/class-product-variation-type.php',