2020-04-16 14:56:52 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - writeReview
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for creating a new product review.
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
|
|
|
|
* @since 0.5.1
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
|
|
|
|
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
|
|
|
|
use WPGraphQL\Model\Comment;
|
|
|
|
|
use WPGraphQL\Mutation\CommentCreate;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Review_Write
|
|
|
|
|
*/
|
|
|
|
|
class Review_Write {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2020-04-16 14:56:52 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'writeReview',
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2020-04-16 14:56:52 -04:00
|
|
|
'inputFields' => self::get_input_fields(),
|
|
|
|
|
'outputFields' => self::get_output_fields(),
|
|
|
|
|
'mutateAndGetPayload' => self::mutate_and_get_payload(),
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2020-04-16 14:56:52 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation input field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_input_fields() {
|
2021-01-21 22:41:43 -05:00
|
|
|
$comment_input_fields = CommentCreate::get_input_fields();
|
|
|
|
|
unset( $comment_input_fields['type'] );
|
2020-04-16 14:56:52 -04:00
|
|
|
|
|
|
|
|
return array_merge(
|
|
|
|
|
$comment_input_fields,
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
'rating' => [
|
|
|
|
|
'type' => [ 'non_null' => 'Int' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Product rating', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
]
|
2020-04-16 14:56:52 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation output field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_output_fields() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
|
|
|
|
'rating' => [
|
2021-01-21 22:41:43 -05:00
|
|
|
'type' => 'Float',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The product rating of the review that was created', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2020-04-16 14:56:52 -04:00
|
|
|
if ( ! isset( $payload['id'] ) || ! absint( $payload['id'] ) ) {
|
|
|
|
|
return null;
|
2021-01-21 22:41:43 -05:00
|
|
|
}
|
|
|
|
|
return (float) get_comment_meta( $payload['id'], 'rating', true );
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'review' => [
|
2021-01-21 22:41:43 -05:00
|
|
|
'type' => 'Comment',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The product review that was created', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2023-11-30 15:41:32 +02:00
|
|
|
'resolve' => static function ( $payload ) {
|
2020-04-16 14:56:52 -04:00
|
|
|
if ( ! isset( $payload['id'] ) || ! absint( $payload['id'] ) ) {
|
|
|
|
|
return null;
|
2021-01-21 22:41:43 -05:00
|
|
|
}
|
|
|
|
|
$comment = get_comment( $payload['id'] );
|
2023-06-13 23:17:02 +03:00
|
|
|
|
|
|
|
|
if ( null === $comment ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-16 14:56:52 -04:00
|
|
|
return new Comment( $comment );
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
];
|
2020-04-16 14:56:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation data modification closure.
|
|
|
|
|
*
|
|
|
|
|
* @return callable
|
|
|
|
|
*/
|
|
|
|
|
public static function mutate_and_get_payload() {
|
2023-07-20 00:11:25 +03:00
|
|
|
return static function ( $input, AppContext $context, ResolveInfo $info ) {
|
2021-01-21 22:41:43 -05:00
|
|
|
// Set comment type to "review".
|
|
|
|
|
$input['type'] = 'review';
|
2020-04-16 14:56:52 -04:00
|
|
|
|
2021-01-21 22:41:43 -05:00
|
|
|
$resolver = CommentCreate::mutate_and_get_payload();
|
2020-04-16 14:56:52 -04:00
|
|
|
|
2021-01-21 22:41:43 -05:00
|
|
|
$payload = $resolver( $input, $context, $info );
|
2020-04-16 14:56:52 -04:00
|
|
|
|
2021-01-21 22:41:43 -05:00
|
|
|
// Set product rating upon successful creation of the review.
|
|
|
|
|
if ( $payload['success'] ) {
|
|
|
|
|
add_comment_meta( $payload['id'], 'rating', $input['rating'] );
|
|
|
|
|
}
|
2020-04-16 14:56:52 -04:00
|
|
|
|
|
|
|
|
return $payload;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|