Skip to content

Commit 57af025

Browse files
authored
fix: updated data-loaders to be in compliance with WPGraphQL v2.3.x+ (#953)
* fix: updated data-loaders to be in compliance with WPGraphQL v2.3.x * fix: general improvements and bugfixes * chore: test dependencies removed * chore: Unnecessary files removed * chore: Linter compliances met * chore: Linter compliances met
1 parent f61b55f commit 57af025

18 files changed

+410
-163
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ codeception.yml
3030
plugin-build
3131
bin/strauss.phar
3232
CLAUDE.md
33-
.claude
33+
.claude/

composer.lock

Lines changed: 150 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/class-core-schema-filters.php

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111
use GraphQL\Error\UserError;
1212
use WPGraphQL\WooCommerce\Data\Factory;
1313
use WPGraphQL\WooCommerce\Data\Loader\WC_CPT_Loader;
14+
use WPGraphQL\WooCommerce\Data\Loader\WC_Cart_Item_Loader;
1415
use WPGraphQL\WooCommerce\Data\Loader\WC_Customer_Loader;
15-
use WPGraphQL\WooCommerce\Data\Loader\WC_Db_Loader;
16+
use WPGraphQL\WooCommerce\Data\Loader\WC_Downloadable_Item_Loader;
17+
use WPGraphQL\WooCommerce\Data\Loader\WC_Order_Item_Loader;
18+
use WPGraphQL\WooCommerce\Data\Loader\WC_Shipping_Method_Loader;
19+
use WPGraphQL\WooCommerce\Data\Loader\WC_Shipping_Zone_Loader;
20+
use WPGraphQL\WooCommerce\Data\Loader\WC_Tax_Class_Loader;
21+
use WPGraphQL\WooCommerce\Data\Loader\WC_Tax_Rate_Loader;
1622
use WPGraphQL\WooCommerce\WP_GraphQL_WooCommerce as WooGraphQL;
1723

1824
/**
@@ -33,7 +39,7 @@ public static function add_filters() {
3339
add_filter( 'register_taxonomy_args', [ self::class, 'register_taxonomy_args' ], 10, 2 );
3440

3541
// Add data-loaders to AppContext.
36-
add_filter( 'graphql_data_loaders', [ self::class, 'graphql_data_loaders' ], 10, 2 );
42+
add_filter( 'graphql_data_loader_classes', [ self::class, 'graphql_data_loader_classes' ], 10 );
3743

3844
// Add node resolvers.
3945
add_filter(
@@ -266,35 +272,25 @@ public static function register_taxonomy_args( $args, $taxonomy ) {
266272
/**
267273
* Registers data-loaders to be used when resolving WooCommerce-related GraphQL types
268274
*
269-
* @param array $loaders - assigned loaders.
270-
* @param \WPGraphQL\AppContext $context - AppContext instance.
275+
* @param array $loaders Assigned loaders.
271276
*
272277
* @return array
273278
*/
274-
public static function graphql_data_loaders( $loaders, $context ) {
279+
public static function graphql_data_loader_classes( $loaders ) {
275280
// WooCommerce customer loader.
276-
$customer_loader = new WC_Customer_Loader( $context );
277-
$loaders['wc_customer'] = &$customer_loader;
281+
$loaders['wc_customer'] = WC_Customer_Loader::class;
278282

279283
// WooCommerce CPT loader.
280-
$cpt_loader = new WC_CPT_Loader( $context );
281-
$loaders['wc_post'] = &$cpt_loader;
284+
$loaders['wc_post'] = WC_CPT_Loader::class;
282285

283286
// WooCommerce DB loaders.
284-
$cart_item_loader = new WC_Db_Loader( $context, 'CART_ITEM' );
285-
$loaders['cart_item'] = &$cart_item_loader;
286-
$downloadable_item_loader = new WC_Db_Loader( $context, 'DOWNLOADABLE_ITEM' );
287-
$loaders['downloadable_item'] = &$downloadable_item_loader;
288-
$tax_class_loader = new WC_Db_Loader( $context, 'TAX_CLASS' );
289-
$loaders['tax_class'] = &$tax_class_loader;
290-
$tax_rate_loader = new WC_Db_Loader( $context, 'TAX_RATE' );
291-
$loaders['tax_rate'] = &$tax_rate_loader;
292-
$order_item_loader = new WC_Db_Loader( $context, 'ORDER_ITEM' );
293-
$loaders['order_item'] = &$order_item_loader;
294-
$shipping_item_loader = new WC_Db_Loader( $context, 'SHIPPING_METHOD' );
295-
$loaders['shipping_method'] = &$shipping_item_loader;
296-
$shipping_zone_loader = new WC_Db_Loader( $context, 'SHIPPING_ZONE' );
297-
$loaders['shipping_zone'] = &$shipping_zone_loader;
287+
$loaders['cart_item'] = WC_Cart_Item_Loader::class;
288+
$loaders['downloadable_item'] = WC_Downloadable_Item_Loader::class;
289+
$loaders['tax_class'] = WC_Tax_Class_Loader::class;
290+
$loaders['tax_rate'] = WC_Tax_Rate_Loader::class;
291+
$loaders['order_item'] = WC_Order_Item_Loader::class;
292+
$loaders['shipping_method'] = WC_Shipping_Method_Loader::class;
293+
$loaders['shipping_zone'] = WC_Shipping_Zone_Loader::class;
298294
return $loaders;
299295
}
300296

@@ -404,11 +400,28 @@ public static function inject_type_resolver( $type, $value ) {
404400
public static function resolve_product_type( $value ) {
405401
$type_registry = \WPGraphQL::get_type_registry();
406402
$possible_types = WooGraphQL::get_enabled_product_types();
407-
$product_type = $value->get_type();
403+
404+
if ( $value instanceof \WPGraphQL\Model\Post && ( 'product' === $value->post_type || 'product_variation' === $value->post_type ) ) {
405+
$product_model = \WPGraphQL::get_app_context()
406+
->get_loader( 'wc_post' )
407+
->load( $value->ID );
408+
} elseif ( $value instanceof \WPGraphQL\Model\Post && ( 'product' !== $value->post_type && 'product_variation' !== $value->post_type ) ) {
409+
throw new UserError(
410+
sprintf(
411+
/* translators: %s: Post type slug */
412+
__( 'The "%s" post type is not a valid product type.', 'wp-graphql-woocommerce' ),
413+
$value->post_type
414+
)
415+
);
416+
} else {
417+
$product_model = $value;
418+
}
419+
420+
$product_type = $product_model->get_type();
408421
if ( isset( $possible_types[ $product_type ] ) ) {
409422
return $type_registry->get_type( $possible_types[ $product_type ] );
410-
} elseif ( $value instanceof \WPGraphQL\WooCommerce\Model\Product_Variation ) {
411-
return self::resolve_product_variation_type( $value );
423+
} elseif ( $product_model instanceof \WPGraphQL\WooCommerce\Model\Product_Variation ) {
424+
return self::resolve_product_variation_type( $product_model );
412425
} elseif ( 'on' === woographql_setting( 'enable_unsupported_product_type', 'off' ) ) {
413426
$unsupported_type = WooGraphQL::get_supported_product_type();
414427
return $type_registry->get_type( $unsupported_type );
@@ -418,7 +431,7 @@ public static function resolve_product_type( $value ) {
418431
sprintf(
419432
/* translators: %s: Product type */
420433
__( 'The "%s" product type is not supported by the core WPGraphQL for WooCommerce (WooGraphQL) schema.', 'wp-graphql-woocommerce' ),
421-
$value->type
434+
$product_model->type
422435
)
423436
);
424437
}
@@ -469,8 +482,8 @@ public static function make_order_notes_visible( $is_private, $model_name, $data
469482
if ( $data instanceof \WP_Comment && 'order_note' === $data->comment_type ) {
470483
// Get the parent order.
471484
$order_id = absint( $data->comment_post_ID );
472-
$order = wc_get_order( $order_id );
473-
485+
$order = wc_get_order( $order_id );
486+
474487
if ( ! $order ) {
475488
return true; // Keep it private if order not found.
476489
}
@@ -481,9 +494,18 @@ public static function make_order_notes_visible( $is_private, $model_name, $data
481494
}
482495

483496
// Allow customers to see customer notes on their own orders.
484-
$is_customer_note = get_comment_meta( $data->comment_ID, 'is_customer_note', true );
485-
if ( $is_customer_note && get_current_user_id() === $order->get_customer_id() ) {
486-
return false; // Not private.
497+
$comment_id = absint( $data->comment_ID );
498+
$is_customer_note = get_comment_meta( $comment_id, 'is_customer_note', true );
499+
if ( $is_customer_note && ! is_bool( $order ) && is_a( $order, \WC_Order::class ) ) {
500+
if ( get_current_user_id() === $order->get_customer_id() ) {
501+
return false; // Not private.
502+
}
503+
} elseif ( $is_customer_note && ! is_bool( $order ) && is_a( $order, \WC_Order_Refund::class ) ) {
504+
/** @var \WC_Order|false $parent */
505+
$parent = wc_get_order( $order->get_parent_id() );
506+
if ( $parent && get_current_user_id() === $parent->get_customer_id() ) {
507+
return false; // Not private.
508+
}
487509
}
488510

489511
// Otherwise keep it private.
@@ -496,11 +518,11 @@ public static function make_order_notes_visible( $is_private, $model_name, $data
496518
/**
497519
* Filter to set order notes visibility to public for authorized users.
498520
*
499-
* @param string $visibility The visibility of the object.
500-
* @param string $model_name The name of the model being checked.
501-
* @param mixed $data The data being checked.
502-
* @param int|null $owner The owner of the object.
503-
* @param \WP_User $current_user The current user.
521+
* @param string $visibility The visibility of the object.
522+
* @param string $model_name The name of the model being checked.
523+
* @param mixed $data The data being checked.
524+
* @param int|null $owner The owner of the object.
525+
* @param \WP_User $current_user The current user.
504526
*
505527
* @return string
506528
*/
@@ -513,10 +535,14 @@ public static function set_order_notes_visibility( $visibility, $model_name, $da
513535
// Check if this is an order note and if user owns the order.
514536
if ( $data instanceof \WP_Comment && 'order_note' === $data->comment_type ) {
515537
$order = wc_get_order( $data->comment_post_ID );
516-
538+
517539
// If user is the order owner, make it public.
518-
if ( $order && get_current_user_id() === $order->get_customer_id() ) {
519-
return 'public';
540+
if ( $order && ! is_bool( $order ) && is_a( $order, \WC_Order::class ) ) {
541+
return get_current_user_id() === $order->get_customer_id() ? 'public' : $visibility;
542+
} elseif ( $order && ! is_bool( $order ) && is_a( $order, \WC_Order_Refund::class ) ) {
543+
/** @var \WC_Order|false $parent */
544+
$parent = wc_get_order( $order->get_parent_id() );
545+
return $parent && get_current_user_id() === $parent->get_customer_id() ? 'public' : $visibility;
520546
}
521547
}
522548

includes/class-wp-graphql-woocommerce.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ private function includes() {
181181
require $include_directory_path . 'data/loader/class-wc-cpt-loader.php';
182182
require $include_directory_path . 'data/loader/class-wc-customer-loader.php';
183183
require $include_directory_path . 'data/loader/class-wc-db-loader.php';
184+
require $include_directory_path . 'data/loader/class-wc-cart-item-loader.php';
185+
require $include_directory_path . 'data/loader/class-wc-downloadable-item-loader.php';
186+
require $include_directory_path . 'data/loader/class-wc-order-item-loader.php';
187+
require $include_directory_path . 'data/loader/class-wc-shipping-method-loader.php';
188+
require $include_directory_path . 'data/loader/class-wc-shipping-zone-loader.php';
189+
require $include_directory_path . 'data/loader/class-wc-tax-class-loader.php';
190+
require $include_directory_path . 'data/loader/class-wc-tax-rate-loader.php';
184191

185192
// Include connection resolver trait/class files.
186193
require $include_directory_path . 'data/connection/trait-wc-db-loader-common.php';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ public function sanitize_input_fields( array $where_args ) {
491491
$term_taxonomy_ids = [];
492492
foreach ( $terms as $term_slug ) {
493493
$term = get_term_by( 'slug', $term_slug, $taxonomy );
494-
if ( ! $term || is_wp_error( $term ) ) {
494+
if ( ! $term ) {
495495
continue;
496496
}
497497
$term_taxonomy_ids[] = $term->term_taxonomy_id;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* DataLoader - WC_Cart_Item_Loader
4+
*
5+
* Loads Models for WooCommerce Shipping Methods defined in custom DB tables.
6+
*
7+
* @package WPGraphQL\WooCommerce\Data\Loader
8+
* @since TBD
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Data\Loader;
12+
13+
/**
14+
* Class WC_Cart_Item_Loader
15+
*/
16+
class WC_Cart_Item_Loader extends WC_Db_Loader {
17+
/**
18+
* WC_Cart_Item_Loader constructor
19+
*
20+
* @param \WPGraphQL\AppContext $context AppContext instance.
21+
*/
22+
public function __construct( $context ) {
23+
parent::__construct( $context, 'CART_ITEM' );
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* DataLoader - WC_Downloadable_Item_Loader
4+
*
5+
* Loads Models for WooCommerce Downloadable Items defined in custom DB tables.
6+
*
7+
* @package WPGraphQL\WooCommerce\Data\Loader
8+
* @since TBD
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Data\Loader;
12+
13+
/**
14+
* Class WC_Downloadable_Item_Loader
15+
*/
16+
class WC_Downloadable_Item_Loader extends WC_Db_Loader {
17+
/**
18+
* WC_Downloadable_Item_Loader constructor
19+
*
20+
* @param \WPGraphQL\AppContext $context AppContext instance.
21+
*/
22+
public function __construct( $context ) {
23+
parent::__construct( $context, 'DOWNLOADABLE_ITEM' );
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* DataLoader - WC_Order_Item_Loader
4+
*
5+
* Loads Models for WooCommerce Order Items defined in custom DB tables.
6+
*
7+
* @package WPGraphQL\WooCommerce\Data\Loader
8+
* @since TBD
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Data\Loader;
12+
13+
/**
14+
* Class WC_Order_Item_Loader
15+
*/
16+
class WC_Order_Item_Loader extends WC_Db_Loader {
17+
/**
18+
* WC_Order_Item_Loader constructor
19+
*
20+
* @param \WPGraphQL\AppContext $context AppContext instance.
21+
*/
22+
public function __construct( $context ) {
23+
parent::__construct( $context, 'ORDER_ITEM' );
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* DataLoader - WC_Shipping_Method_Loader
4+
*
5+
* Loads Models for WooCommerce Shipping Methods defined in custom DB tables.
6+
*
7+
* @package WPGraphQL\WooCommerce\Data\Loader
8+
* @since TBD
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Data\Loader;
12+
13+
/**
14+
* Class WC_Shipping_Method_Loader
15+
*/
16+
class WC_Shipping_Method_Loader extends WC_Db_Loader {
17+
/**
18+
* WC_Shipping_Method_Loader constructor
19+
*
20+
* @param \WPGraphQL\AppContext $context AppContext instance.
21+
*/
22+
public function __construct( $context ) {
23+
parent::__construct( $context, 'SHIPPING_METHOD' );
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* DataLoader - WC_Shipping_Zone_Loader
4+
*
5+
* Loads Models for WooCommerce Shipping Zones defined in custom DB tables.
6+
*
7+
* @package WPGraphQL\WooCommerce\Data\Loader
8+
* @since TBD
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Data\Loader;
12+
13+
/**
14+
* Class WC_Shipping_Zone_Loader
15+
*/
16+
class WC_Shipping_Zone_Loader extends WC_Db_Loader {
17+
/**
18+
* WC_Shipping_Zone_Loader constructor
19+
*
20+
* @param \WPGraphQL\AppContext $context AppContext instance.
21+
*/
22+
public function __construct( $context ) {
23+
parent::__construct( $context, 'SHIPPING_ZONE' );
24+
}
25+
}

0 commit comments

Comments
 (0)