Files
wp-graphql-woocommerce/includes/mutation/class-review-update.php
T
Geoff TaylorandGitHub 684732aa0d perf: Migrate type descriptions to lazy lambdas (#1005)
* perf: Migrate type descriptions to lazy lambdas

Convert all eager description strings from:
  'description' => __( '...', 'wp-graphql-woocommerce' ),
to deferred lambdas:
  'description' => static function () { return __( '...', 'wp-graphql-woocommerce' ); },

This defers the __() translation call until the description is actually
needed (e.g. during schema introspection), avoiding unnecessary
translation overhead on every GraphQL request. Mirrors the pattern
used in WPGraphQL core.

1206 descriptions converted across 169 files.

* chore: Linter compliances met
2026-03-30 23:37:51 -04:00

105 lines
2.3 KiB
PHP

<?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
*
* @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() {
return array_merge(
Review_Write::get_input_fields(),
[
'id' => [
'type' => [ 'non_null' => 'ID' ],
'description' => static function () {
return __( 'The ID of the review being updated.', 'wp-graphql-woocommerce' );
},
],
]
);
}
/**
* 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".
$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 ', 'wp-graphql-woocommerce' ) );
}
if ( array_intersect_key( $input, $skip ) !== $input ) {
$resolver = CommentUpdate::mutate_and_get_payload();
$payload = $resolver( $input, $context, $info );
}
// Check if product rating needs updating.
if ( ! empty( $payload['id'] ) && isset( $input['rating'] ) ) {
update_comment_meta( $payload['id'], 'rating', $input['rating'] );
}
return $payload;
};
}
}