Files

105 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?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;
use WPGraphQL\Utils\Utils;
/**
* Class Review_Update
*/
class Review_Update {
/**
* Registers mutation
2023-06-13 23:17:02 +03:00
*
* @return void
*/
public static function register_mutation() {
register_graphql_mutation(
'updateReview',
[
'inputFields' => self::get_input_fields(),
'outputFields' => self::get_output_fields(),
'mutateAndGetPayload' => self::mutate_and_get_payload(),
]
);
}
/**
* 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(
Review_Write::get_input_fields(),
[
'id' => [
'type' => [ 'non_null' => 'ID' ],
'description' => static function () {
return __( 'The ID of the review being updated.', 'graphql-for-ecommerce' );
},
],
]
);
}
/**
* 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() {
return static function ( $input, AppContext $context, ResolveInfo $info ) {
// Set comment type to "review".
2021-01-21 22:41:43 -05:00
$input['type'] = 'review';
$skip = [
'type' => 'review',
'id' => 1,
'rating' => 1,
'clientMutationId' => 1,
];
$payload = [];
$id = Utils::get_database_id_from_id( $input['id'] );
if ( ! $id ) {
throw new UserError( __( 'Provided review ID missing or invalid ', 'graphql-for-ecommerce' ) );
}
if ( array_intersect_key( $input, $skip ) !== $input ) {
$resolver = CommentUpdate::mutate_and_get_payload();
$payload = $resolver( $input, $context, $info );
}
2021-01-21 22:41:43 -05:00
// Check if product rating needs updating.
if ( ! empty( $payload['id'] ) && isset( $input['rating'] ) ) {
update_comment_meta( $payload['id'], 'rating', $input['rating'] );
2021-01-21 22:41:43 -05:00
}
return $payload;
};
}
}