2020-09-07 16:42:59 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Connection type - Comments
|
|
|
|
|
*
|
|
|
|
|
* Registers connections to comments from woocommerce post-types
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Connection
|
|
|
|
|
* @since 0.3.2
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace WPGraphQL\WooCommerce\Connection;
|
|
|
|
|
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
2023-01-24 17:21:16 -05:00
|
|
|
use WPGraphQL\Type\Connection\Comments as Comments_Core;
|
2020-09-07 16:42:59 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class - Comments
|
|
|
|
|
*/
|
2023-01-24 17:21:16 -05:00
|
|
|
class Comments extends Comments_Core {
|
2020-09-07 16:42:59 -04:00
|
|
|
/**
|
|
|
|
|
* Registers connection.
|
|
|
|
|
*/
|
|
|
|
|
public static function register_connections() {
|
|
|
|
|
// From Products.
|
|
|
|
|
register_graphql_connection(
|
|
|
|
|
self::get_connection_config(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2020-09-07 16:42:59 -04:00
|
|
|
'fromType' => 'Product',
|
|
|
|
|
'toType' => 'Comment',
|
|
|
|
|
'fromFieldName' => 'reviews',
|
2022-08-26 14:53:36 -04:00
|
|
|
'connectionFields' => [
|
|
|
|
|
'averageRating' => [
|
2020-09-07 16:42:59 -04:00
|
|
|
'type' => 'Float',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Average review rating for this product.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $source ) {
|
2020-09-07 16:42:59 -04:00
|
|
|
if ( empty( $source['edges'] ) ) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$product = $source['edges'][0]['source'];
|
|
|
|
|
return $product->averageRating; // @codingStandardsIgnoreLine
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'edgeFields' => [
|
|
|
|
|
'rating' => [
|
2020-09-07 16:42:59 -04:00
|
|
|
'type' => 'Float',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Review rating', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $source ) {
|
2020-09-07 16:42:59 -04:00
|
|
|
$review = $source['node'];
|
2021-01-21 22:41:43 -05:00
|
|
|
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
|
2020-09-07 16:42:59 -04:00
|
|
|
$rating = get_comment_meta( $review->commentId, 'rating', true );
|
|
|
|
|
return $rating ? $rating : 0;
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
],
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
|
2021-03-17 15:51:27 -04:00
|
|
|
$resolver = new \WPGraphQL\Data\Connection\CommentConnectionResolver( $source, $args, $context, $info );
|
|
|
|
|
|
|
|
|
|
$resolver->set_query_arg( 'post_type', 'product' );
|
|
|
|
|
$resolver->set_query_arg( 'post_id', $source->ID );
|
|
|
|
|
|
|
|
|
|
return $resolver->get_connection();
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2020-09-07 16:42:59 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// From Orders.
|
|
|
|
|
register_graphql_connection(
|
|
|
|
|
self::get_connection_config(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2021-01-21 22:41:43 -05:00
|
|
|
'fromType' => 'Order',
|
2025-10-11 16:29:36 -04:00
|
|
|
'toType' => 'OrderNote',
|
2021-01-21 22:41:43 -05:00
|
|
|
'fromFieldName' => 'orderNotes',
|
2022-08-26 14:53:36 -04:00
|
|
|
'edgeFields' => [
|
|
|
|
|
'isCustomerNote' => [
|
2020-09-07 16:42:59 -04:00
|
|
|
'type' => 'Boolean',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Is this a customer note?', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $source ) {
|
2020-09-07 16:42:59 -04:00
|
|
|
$note = $source['node'];
|
2021-01-21 22:41:43 -05:00
|
|
|
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
|
2020-09-07 16:42:59 -04:00
|
|
|
return get_comment_meta( $note->commentId, 'is_customer_note', true );
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
],
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
|
2020-09-07 16:42:59 -04:00
|
|
|
$resolver = new \WPGraphQL\Data\Connection\CommentConnectionResolver( $source, $args, $context, $info );
|
|
|
|
|
|
|
|
|
|
$resolver->set_query_arg( 'post_id', $source->ID );
|
|
|
|
|
$resolver->set_query_arg( 'approve', 'approve' );
|
|
|
|
|
$resolver->set_query_arg( 'type', '' );
|
|
|
|
|
|
|
|
|
|
if ( ! current_user_can( 'edit_shop_orders', $source->ID ) ) {
|
|
|
|
|
$resolver->set_query_arg( 'meta_key', 'is_customer_note' );
|
|
|
|
|
$resolver->set_query_arg( 'meta_value', true );
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
remove_filter( 'comments_clauses', [ 'WC_Comments', 'exclude_order_comments' ] );
|
2020-09-07 16:42:59 -04:00
|
|
|
|
|
|
|
|
$connection = $resolver->get_connection();
|
|
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
add_filter( 'comments_clauses', [ 'WC_Comments', 'exclude_order_comments' ] );
|
2020-09-07 16:42:59 -04:00
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2020-09-07 16:42:59 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|