1111use GraphQL \Error \UserError ;
1212use WPGraphQL \WooCommerce \Data \Factory ;
1313use WPGraphQL \WooCommerce \Data \Loader \WC_CPT_Loader ;
14- use WPGraphQL \WooCommerce \Data \Loader \WC_Customer_Loader ;
1514use WPGraphQL \WooCommerce \Data \Loader \WC_Cart_Item_Loader ;
15+ use WPGraphQL \WooCommerce \Data \Loader \WC_Customer_Loader ;
1616use WPGraphQL \WooCommerce \Data \Loader \WC_Downloadable_Item_Loader ;
1717use WPGraphQL \WooCommerce \Data \Loader \WC_Order_Item_Loader ;
1818use WPGraphQL \WooCommerce \Data \Loader \WC_Shipping_Method_Loader ;
@@ -39,7 +39,7 @@ public static function add_filters() {
3939 add_filter ( 'register_taxonomy_args ' , [ self ::class, 'register_taxonomy_args ' ], 10 , 2 );
4040
4141 // Add data-loaders to AppContext.
42- add_filter ( 'graphql_data_loader_classes ' , [ self ::class, 'graphql_data_loader_classes ' ], 10 , 2 );
42+ add_filter ( 'graphql_data_loader_classes ' , [ self ::class, 'graphql_data_loader_classes ' ], 10 );
4343
4444 // Add node resolvers.
4545 add_filter (
@@ -272,8 +272,7 @@ public static function register_taxonomy_args( $args, $taxonomy ) {
272272 /**
273273 * Registers data-loaders to be used when resolving WooCommerce-related GraphQL types
274274 *
275- * @param array $loaders - assigned loaders.
276- * @param \WPGraphQL\AppContext $context - AppContext instance.
275+ * @param array $loaders Assigned loaders.
277276 *
278277 * @return array
279278 */
@@ -409,6 +408,7 @@ public static function resolve_product_type( $value ) {
409408 } elseif ( $ value instanceof \WPGraphQL \Model \Post && ( 'product ' !== $ value ->post_type && 'product_variation ' !== $ value ->post_type ) ) {
410409 throw new UserError (
411410 sprintf (
411+ /* translators: %s: Post type slug */
412412 __ ( 'The "%s" post type is not a valid product type. ' , 'wp-graphql-woocommerce ' ),
413413 $ value ->post_type
414414 )
@@ -417,7 +417,7 @@ public static function resolve_product_type( $value ) {
417417 $ product_model = $ value ;
418418 }
419419
420- $ product_type = $ product_model ->get_type ();
420+ $ product_type = $ product_model ->get_type ();
421421 if ( isset ( $ possible_types [ $ product_type ] ) ) {
422422 return $ type_registry ->get_type ( $ possible_types [ $ product_type ] );
423423 } elseif ( $ product_model instanceof \WPGraphQL \WooCommerce \Model \Product_Variation ) {
@@ -482,8 +482,8 @@ public static function make_order_notes_visible( $is_private, $model_name, $data
482482 if ( $ data instanceof \WP_Comment && 'order_note ' === $ data ->comment_type ) {
483483 // Get the parent order.
484484 $ order_id = absint ( $ data ->comment_post_ID );
485- $ order = wc_get_order ( $ order_id );
486-
485+ $ order = wc_get_order ( $ order_id );
486+
487487 if ( ! $ order ) {
488488 return true ; // Keep it private if order not found.
489489 }
@@ -494,9 +494,14 @@ public static function make_order_notes_visible( $is_private, $model_name, $data
494494 }
495495
496496 // Allow customers to see customer notes on their own orders.
497- $ is_customer_note = get_comment_meta ( $ data ->comment_ID , 'is_customer_note ' , true );
498- if ( $ is_customer_note && get_current_user_id () === $ order ->get_customer_id () ) {
499- 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 () ) return false ; // Not private.
501+ } elseif ( $ is_customer_note && ! is_bool ( $ order ) && is_a ( $ order , \WC_Order_Refund::class ) ) {
502+ /** @var \WC_Order|false $parent */
503+ $ parent = wc_get_order ( $ order ->get_parent_id () );
504+ if ( $ parent && get_current_user_id () === $ parent ->get_customer_id () ) return false ; // Not private.
500505 }
501506
502507 // Otherwise keep it private.
@@ -509,11 +514,11 @@ public static function make_order_notes_visible( $is_private, $model_name, $data
509514 /**
510515 * Filter to set order notes visibility to public for authorized users.
511516 *
512- * @param string $visibility The visibility of the object.
513- * @param string $model_name The name of the model being checked.
514- * @param mixed $data The data being checked.
515- * @param int|null $owner The owner of the object.
516- * @param \WP_User $current_user The current user.
517+ * @param string $visibility The visibility of the object.
518+ * @param string $model_name The name of the model being checked.
519+ * @param mixed $data The data being checked.
520+ * @param int|null $owner The owner of the object.
521+ * @param \WP_User $current_user The current user.
517522 *
518523 * @return string
519524 */
@@ -526,10 +531,14 @@ public static function set_order_notes_visibility( $visibility, $model_name, $da
526531 // Check if this is an order note and if user owns the order.
527532 if ( $ data instanceof \WP_Comment && 'order_note ' === $ data ->comment_type ) {
528533 $ order = wc_get_order ( $ data ->comment_post_ID );
529-
534+
530535 // If user is the order owner, make it public.
531- if ( $ order && get_current_user_id () === $ order ->get_customer_id () ) {
532- return 'public ' ;
536+ if ( $ order && ! is_bool ( $ order ) && is_a ( $ order , \WC_Order::class ) ) {
537+ return get_current_user_id () === $ order ->get_customer_id () ? 'public ' : $ visibility ;
538+ } else if ( $ order && ! is_bool ( $ order ) && is_a ( $ order , \WC_Order_Refund::class ) ) {
539+ /** @var \WC_Order|false $parent */
540+ $ parent = wc_get_order ( $ order ->get_parent_id () );
541+ return $ parent && get_current_user_id () === $ parent ->get_customer_id () ? 'public ' : $ visibility ;
533542 }
534543 }
535544
0 commit comments