1111use GraphQL \Error \UserError ;
1212use WPGraphQL \WooCommerce \Data \Factory ;
1313use WPGraphQL \WooCommerce \Data \Loader \WC_CPT_Loader ;
14+ use WPGraphQL \WooCommerce \Data \Loader \WC_Cart_Item_Loader ;
1415use 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 ;
1622use 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
0 commit comments