From cb77fd608214562ff34d053265085995fb0383c8 Mon Sep 17 00:00:00 2001 From: Rob Belfort Date: Mon, 27 Jan 2025 07:44:12 -0500 Subject: [PATCH 1/3] Fix WooGraphQL Product Type Expectations --- .../Type/WPObject/SeoObjects.php | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php b/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php index 30d3643..c32aea3 100644 --- a/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php +++ b/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php @@ -34,34 +34,22 @@ public static function init(): void { */ public static function register(): void { // Set SEO field types for product children. - $product_types = WP_GraphQL_WooCommerce::get_enabled_product_types(); - - foreach ( $product_types as $graphql_type_name ) { - Utils::overload_graphql_field_type( $graphql_type_name, 'seo', 'RankMathProductObjectSeo' ); - } - - // Register the Product Variation SEO type and apply it to the Product Variation and children. - $type_name_for_product_variation = 'RankMathProductVariationObjectSeo'; - - register_graphql_object_type( - $type_name_for_product_variation, + $product_types = array_merge( + WP_GraphQL_WooCommerce::get_enabled_product_types(), [ - 'description' => __( 'The product variation object SEO data', 'wp-graphql-rank-math' ), - 'interfaces' => [ ContentNodeSeo::get_type_name() ], - 'fields' => [], - 'eagerlyLoadType' => true, + 'ProductUnion', + 'ProductWithPricing', + 'ProductWithDimensions', + 'InventoriedProduct', + 'DownloadableProduct', + 'ProductWithAttributes', + 'ProductWithVariations', ] ); - $product_variations = array_merge( - [ - 'ProductVariation', - ], - WP_GraphQL_WooCommerce::get_enabled_product_variation_types(), - ); - - foreach ( $product_variations as $product_variation ) { - Utils::overload_graphql_field_type( $product_variation, 'seo', $type_name_for_product_variation ); + foreach ( $product_types as $graphql_type_name ) { + Utils::overload_graphql_field_type( $graphql_type_name, 'seo', 'RankMathProductObjectSeo' ); } + } } From 0db7237b2fff5099d462a0cfd86cd4194b9c1187 Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Sat, 15 Feb 2025 13:02:03 +0200 Subject: [PATCH 2/3] chore: phpcbf --- .../WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php b/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php index c32aea3..fb18de7 100644 --- a/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php +++ b/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php @@ -10,7 +10,6 @@ namespace WPGraphQL\RankMath\Extensions\WPGraphQLWooCommerce\Type\WPObject; -use WPGraphQL\RankMath\Type\WPInterface\ContentNodeSeo; use WPGraphQL\RankMath\Utils\Utils; use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Interfaces\Registrable; use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Traits\TypeNameTrait; @@ -50,6 +49,5 @@ public static function register(): void { foreach ( $product_types as $graphql_type_name ) { Utils::overload_graphql_field_type( $graphql_type_name, 'seo', 'RankMathProductObjectSeo' ); } - } } From b23ac1bc86c150a43be66c0efb61c4ccffac02a9 Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Sat, 3 May 2025 12:39:32 +0300 Subject: [PATCH 3/3] dev: add b/c with WooGraphQL < 0.21.1 --- .../Type/WPObject/SeoObjects.php | 70 ++++++++++++++++--- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php b/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php index fb18de7..2452c14 100644 --- a/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php +++ b/src/Extensions/WPGraphQLWooCommerce/Type/WPObject/SeoObjects.php @@ -10,6 +10,7 @@ namespace WPGraphQL\RankMath\Extensions\WPGraphQLWooCommerce\Type\WPObject; +use WPGraphQL\RankMath\Type\WPInterface\ContentNodeSeo; use WPGraphQL\RankMath\Utils\Utils; use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Interfaces\Registrable; use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Traits\TypeNameTrait; @@ -33,21 +34,68 @@ public static function init(): void { */ public static function register(): void { // Set SEO field types for product children. - $product_types = array_merge( - WP_GraphQL_WooCommerce::get_enabled_product_types(), + $product_types = WP_GraphQL_WooCommerce::get_enabled_product_types(); + /** + * @todo: remove this when we don't need to support WooGraphQL< 0.21.1 + * @see https://github.com/AxeWP/wp-graphql-rank-math/pull/115#issuecomment-2660900767 + */ + if ( defined( 'WPGRAPHQL_WOOCOMMERCE_VERSION' ) && version_compare( WPGRAPHQL_WOOCOMMERCE_VERSION, '0.21.1', '>=' ) ) { + $product_types = array_merge( + $product_types, + [ + 'ProductUnion', + 'ProductWithPricing', + 'ProductWithDimensions', + 'InventoriedProduct', + 'DownloadableProduct', + 'ProductWithAttributes', + 'ProductWithVariations', + ] + ); + } + + foreach ( $product_types as $graphql_type_name ) { + Utils::overload_graphql_field_type( $graphql_type_name, 'seo', 'RankMathProductObjectSeo' ); + } + + /** + * @todo: remove this when we don't need to support WooGraphQL< 0.21.1 + * @see https://github.com/AxeWP/wp-graphql-rank-math/pull/115#issuecomment-2660900767 + */ + if ( defined( 'WPGRAPHQL_WOOCOMMERCE_VERSION' ) && version_compare( WPGRAPHQL_WOOCOMMERCE_VERSION, '0.21.1', '<' ) ) { + self::register_product_variation_types(); + } + } + + /** + * Registers the SEO types for product variations. + * + * @todo: remove this when we don't need to support WooGraphQL< 0.21.1 + * @see https://github.com/AxeWP/wp-graphql-rank-math/pull/115#issuecomment-2660900767 + */ + private static function register_product_variation_types(): void { + // Register the Product Variation SEO type and apply it to the Product Variation and children. + $type_name_for_product_variation = 'RankMathProductVariationObjectSeo'; + + register_graphql_object_type( + $type_name_for_product_variation, [ - 'ProductUnion', - 'ProductWithPricing', - 'ProductWithDimensions', - 'InventoriedProduct', - 'DownloadableProduct', - 'ProductWithAttributes', - 'ProductWithVariations', + 'description' => __( 'The product variation object SEO data', 'wp-graphql-rank-math' ), + 'interfaces' => [ ContentNodeSeo::get_type_name() ], + 'fields' => [], + 'eagerlyLoadType' => true, ] ); - foreach ( $product_types as $graphql_type_name ) { - Utils::overload_graphql_field_type( $graphql_type_name, 'seo', 'RankMathProductObjectSeo' ); + $product_variations = array_merge( + [ + 'ProductVariation', + ], + WP_GraphQL_WooCommerce::get_enabled_product_variation_types(), + ); + + foreach ( $product_variations as $product_variation ) { + Utils::overload_graphql_field_type( $product_variation, 'seo', $type_name_for_product_variation ); } } }