Files
wp-graphql-woocommerce/includes/mutation/class-review-write.php
T
4ef46acd15 chore: setup PHPStan (#746)
* chore: setup PHPStan

* chore: General code cleanup for PHPStan

* chore: PHPStan to level 2

* chore: PHPStan to level 2

* fix: Syntax error fixed.

* devops: PHPStan level 2 reached.

* devops: PHPStan level 3 passed

* devops: PHPStan level 4 passed

* chore: fix bad typehints from jwt-auth [phpstan]

* chore: simplify conditional logic in WC_Terms::register_connections()

* chore: add stubs for WP type properties.

* fix: check for valid WC_Order_Item before caching order.

* chore: lint

* devops: PHPStan level 6 passed.

* chore: Linter compliance met.

* chore: simplify logic and check for valid term.

* fix: ensure valid types (partial)

* chore: [phpcs] allow inline type annotations

* devops: PHPStan level 8 reached.

* devops: More stability fixes.

* fix: general bugfixes and improvements

* devops: Code Quality GA script added.

* devops: Linter compliance met & 2 "phpstan-ignore" statements set.

* devops: PHPStan config and CI script fixed.

* fix: "cartItem" query resolution refactored

* devops: GA scripts updated.

* chore: Simple copy changes

---------

Co-authored-by: Geoff Taylor <geoff@axistaylor.com>
2023-06-13 16:17:02 -04:00

124 lines
2.8 KiB
PHP

<?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\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQLRelay\Relay;
use WPGraphQL\AppContext;
use WPGraphQL\Data\DataSource;
use WPGraphQL\Model\Comment;
use WPGraphQL\Mutation\CommentCreate;
/**
* Class Review_Write
*/
class Review_Write {
/**
* Registers mutation
*
* @return void
*/
public static function register_mutation() {
register_graphql_mutation(
'writeReview',
[
'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() {
$comment_input_fields = CommentCreate::get_input_fields();
unset( $comment_input_fields['type'] );
return array_merge(
$comment_input_fields,
[
'rating' => [
'type' => [ 'non_null' => 'Int' ],
'description' => __( 'Product rating', 'wp-graphql-woocommerce' ),
],
]
);
}
/**
* Defines the mutation output field configuration
*
* @return array
*/
public static function get_output_fields() {
return [
'rating' => [
'type' => 'Float',
'description' => __( 'The product rating of the review that was created', 'wp-graphql-woocommerce' ),
'resolve' => function( $payload ) {
if ( ! isset( $payload['id'] ) || ! absint( $payload['id'] ) ) {
return null;
}
return (float) get_comment_meta( $payload['id'], 'rating', true );
},
],
'review' => [
'type' => 'Comment',
'description' => __( 'The product review that was created', 'wp-graphql-woocommerce' ),
'resolve' => function( $payload, $args, AppContext $context ) {
if ( ! isset( $payload['id'] ) || ! absint( $payload['id'] ) ) {
return null;
}
$comment = get_comment( $payload['id'] );
if ( null === $comment ) {
return null;
}
return new Comment( $comment );
},
],
];
}
/**
* Defines the mutation data modification closure.
*
* @return callable
*/
public static function mutate_and_get_payload() {
return function( $input, AppContext $context, ResolveInfo $info ) {
// Set comment type to "review".
$input['type'] = 'review';
$resolver = CommentCreate::mutate_and_get_payload();
$payload = $resolver( $input, $context, $info );
if ( is_a( $payload, UserError::class ) ) {
throw $payload;
}
// Set product rating upon successful creation of the review.
if ( $payload['success'] ) {
add_comment_meta( $payload['id'], 'rating', $input['rating'] );
}
return $payload;
};
}
}