2020-04-16 14:56:52 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - updateReview
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for update an existing product review.
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
|
|
|
|
* @since 0.5.1
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
|
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
|
|
|
|
use WPGraphQL\Mutation\CommentUpdate;
|
2024-11-06 15:00:11 -05:00
|
|
|
use WPGraphQL\Utils\Utils;
|
2020-04-16 14:56:52 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Review_Update
|
|
|
|
|
*/
|
|
|
|
|
class Review_Update {
|
|
|
|
|
/**
|
|
|
|
|
* 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(
|
|
|
|
|
'updateReview',
|
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
|
|
|
return array_merge(
|
2020-04-16 14:56:52 -04:00
|
|
|
Review_Write::get_input_fields(),
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
'id' => [
|
|
|
|
|
'type' => [ 'non_null' => 'ID' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The ID of the review being updated.', '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() {
|
|
|
|
|
return Review_Write::get_output_fields();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 ) {
|
2020-04-16 14:56:52 -04:00
|
|
|
// Set comment type to "review".
|
2021-01-21 22:41:43 -05:00
|
|
|
$input['type'] = 'review';
|
2020-04-16 14:56:52 -04:00
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
$skip = [
|
2021-10-22 18:12:37 -04:00
|
|
|
'type' => 'review',
|
|
|
|
|
'id' => 1,
|
|
|
|
|
'rating' => 1,
|
|
|
|
|
'clientMutationId' => 1,
|
2022-08-26 14:53:36 -04:00
|
|
|
];
|
2020-04-16 14:56:52 -04:00
|
|
|
|
2024-11-06 15:00:11 -05:00
|
|
|
$payload = [];
|
|
|
|
|
$id = Utils::get_database_id_from_id( $input['id'] );
|
|
|
|
|
if ( ! $id ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Provided review ID missing or invalid ', 'graphql-for-ecommerce' ) );
|
2021-10-22 18:12:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( array_intersect_key( $input, $skip ) !== $input ) {
|
|
|
|
|
$resolver = CommentUpdate::mutate_and_get_payload();
|
|
|
|
|
|
|
|
|
|
$payload = $resolver( $input, $context, $info );
|
|
|
|
|
}
|
2020-04-16 14:56:52 -04:00
|
|
|
|
2021-01-21 22:41:43 -05:00
|
|
|
// Check if product rating needs updating.
|
|
|
|
|
if ( ! empty( $payload['id'] ) && isset( $input['rating'] ) ) {
|
2021-10-22 18:12:37 -04:00
|
|
|
update_comment_meta( $payload['id'], 'rating', $input['rating'] );
|
2021-01-21 22:41:43 -05:00
|
|
|
}
|
2020-04-16 14:56:52 -04:00
|
|
|
|
|
|
|
|
return $payload;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|