| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Mutation; |
| 4 |
|
| 5 |
use GraphQL\Error\UserError; |
| 6 |
use GraphQLRelay\Relay; |
| 7 |
use WPGraphQL\AppContext; |
| 8 |
use WPGraphQL\Utils\Utils; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class CommentRestore |
| 12 |
* |
| 13 |
* @package WPGraphQL\Mutation |
| 14 |
*/ |
| 15 |
class CommentRestore { |
| 16 |
/** |
| 17 |
* Registers the CommentRestore mutation. |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public static function register_mutation() { |
| 22 |
register_graphql_mutation( |
| 23 |
'restoreComment', |
| 24 |
[ |
| 25 |
'inputFields' => self::get_input_fields(), |
| 26 |
'outputFields' => self::get_output_fields(), |
| 27 |
'mutateAndGetPayload' => self::mutate_and_get_payload(), |
| 28 |
] |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Defines the mutation input field configuration. |
| 34 |
* |
| 35 |
* @return array<string,array<string,mixed>> |
| 36 |
*/ |
| 37 |
public static function get_input_fields() { |
| 38 |
return [ |
| 39 |
'id' => [ |
| 40 |
'type' => [ |
| 41 |
'non_null' => 'ID', |
| 42 |
], |
| 43 |
'description' => static function () { |
| 44 |
return __( 'The ID of the comment to be restored', 'wp-graphql' ); |
| 45 |
}, |
| 46 |
], |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Defines the mutation output field configuration. |
| 52 |
* |
| 53 |
* @return array<string,array<string,mixed>> |
| 54 |
*/ |
| 55 |
public static function get_output_fields() { |
| 56 |
return [ |
| 57 |
'restoredId' => [ |
| 58 |
'type' => 'Id', |
| 59 |
'description' => static function () { |
| 60 |
return __( 'The ID of the restored comment', 'wp-graphql' ); |
| 61 |
}, |
| 62 |
'resolve' => static function ( $payload ) { |
| 63 |
$restore = (object) $payload['commentObject']; |
| 64 |
|
| 65 |
return ! empty( $restore->comment_ID ) ? Relay::toGlobalId( 'comment', $restore->comment_ID ) : null; |
| 66 |
}, |
| 67 |
], |
| 68 |
'comment' => [ |
| 69 |
'type' => 'Comment', |
| 70 |
'description' => static function () { |
| 71 |
return __( 'The restored comment object', 'wp-graphql' ); |
| 72 |
}, |
| 73 |
'resolve' => static function ( $payload, $args, AppContext $context ) { |
| 74 |
if ( ! isset( $payload['commentObject']->comment_ID ) || ! absint( $payload['commentObject']->comment_ID ) ) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
return $context->get_loader( 'comment' )->load_deferred( absint( $payload['commentObject']->comment_ID ) ); |
| 78 |
}, |
| 79 |
], |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Defines the mutation data modification closure. |
| 85 |
* |
| 86 |
* @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed> |
| 87 |
*/ |
| 88 |
public static function mutate_and_get_payload() { |
| 89 |
return static function ( $input ) { |
| 90 |
// Stop now if a user isn't allowed to delete the comment. |
| 91 |
if ( ! current_user_can( 'moderate_comments' ) ) { |
| 92 |
throw new UserError( esc_html__( 'Sorry, you are not allowed to restore this comment.', 'wp-graphql' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
// Get the database ID for the comment. |
| 96 |
$comment_id = Utils::get_database_id_from_id( $input['id'] ); |
| 97 |
|
| 98 |
if ( false === $comment_id ) { |
| 99 |
throw new UserError( esc_html__( 'Sorry, you are not allowed to restore this comment.', 'wp-graphql' ) ); |
| 100 |
} |
| 101 |
|
| 102 |
// Delete the comment. |
| 103 |
wp_untrash_comment( $comment_id ); |
| 104 |
|
| 105 |
$comment = get_comment( $comment_id ); |
| 106 |
|
| 107 |
return [ |
| 108 |
'commentObject' => $comment, |
| 109 |
]; |
| 110 |
}; |
| 111 |
} |
| 112 |
} |
| 113 |
|