PluginProbe
WPGraphQL / 2.22.1
WPGraphQL v2.22.1
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Data / CommentMutation.php

CommentMutation.php in WPGraphQL 2.22.1, at src/Data/CommentMutation.php

179 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPGraphQL\Data;
4
5 use GraphQL\Error\UserError;
6 use GraphQL\Type\Definition\ResolveInfo;
7 use WPGraphQL\AppContext;
8 use WPGraphQL\Utils\Utils;
9
10 /**
11 * Class CommentMutation
12 *
13 * @package WPGraphQL\Type\Comment\Mutation
14 */
15 class CommentMutation {
16
17 /**
18 * This handles inserting the comment and creating
19 *
20 * @param array<string,mixed> $input The input for the mutation
21 * @param array<string,mixed> $output_args The output args
22 * @param string $mutation_name The name of the mutation being performed
23 * @param bool $update Whether it's an update action
24 *
25 * @return array<string,mixed>
26 * @throws \GraphQL\Error\UserError If the comment author is not provided.
27 */
28 public static function prepare_comment_object( array $input, array &$output_args, string $mutation_name, $update = false ) {
29 /**
30 * Prepare the data for inserting the comment
31 * NOTE: These are organized in the same order as: https://developer.wordpress.org/reference/functions/wp_insert_comment/
32 *
33 * Ex.
34 * 'comment_post_ID' => 1,
35 * 'comment_author' => 'admin',
36 * 'comment_author_email' => 'admin@admin.com',
37 * 'comment_author_url' => 'http://',
38 * 'comment_content' => 'content here',
39 * 'comment_type' => '',
40 * 'comment_parent' => 0,
41 * 'comment_date' => $time,
42 * 'comment_approved' => 1,
43 */
44
45 $user = self::get_comment_author( $input['authorEmail'] ?? null );
46
47 if ( false !== $user ) {
48 $output_args['user_id'] = $user->ID;
49
50 $input['author'] = ! empty( $input['author'] ) ? $input['author'] : $user->display_name;
51 $input['authorEmail'] = ! empty( $input['authorEmail'] ) ? $input['authorEmail'] : $user->user_email;
52 $input['authorUrl'] = ! empty( $input['authorUrl'] ) ? $input['authorUrl'] : $user->user_url;
53 }
54
55 if ( empty( $input['author'] ) ) {
56 if ( ! $update ) {
57 throw new UserError( esc_html__( 'Comment must include an authorName.', 'wp-graphql' ) );
58 }
59 } else {
60 $output_args['comment_author'] = $input['author'];
61 }
62
63 if ( ! empty( $input['authorEmail'] ) ) {
64 if ( false === is_email( apply_filters( 'pre_user_email', $input['authorEmail'] ) ) ) {
65 throw new UserError( esc_html__( 'The email address you are trying to use is invalid', 'wp-graphql' ) );
66 }
67 $output_args['comment_author_email'] = $input['authorEmail'];
68 }
69
70 if ( ! empty( $input['authorUrl'] ) ) {
71 $output_args['comment_author_url'] = $input['authorUrl'];
72 }
73
74 if ( ! empty( $input['commentOn'] ) ) {
75 $output_args['comment_post_ID'] = $input['commentOn'];
76 }
77
78 if ( ! empty( $input['date'] ) && false !== strtotime( $input['date'] ) ) {
79 $output_args['comment_date'] = gmdate( 'Y-m-d H:i:s', strtotime( $input['date'] ) );
80 }
81
82 if ( ! empty( $input['content'] ) ) {
83 $output_args['comment_content'] = $input['content'];
84 }
85
86 if ( ! empty( $input['parent'] ) ) {
87 $output_args['comment_parent'] = Utils::get_database_id_from_id( $input['parent'] );
88 }
89
90 if ( ! empty( $input['type'] ) ) {
91 $output_args['comment_type'] = $input['type'];
92 }
93
94 if ( ! empty( $input['status'] ) ) {
95 $output_args['comment_approved'] = $input['status'];
96 }
97
98 // Fallback to deprecated `approved` input.
99 if ( empty( $output_args['comment_approved'] ) && isset( $input['approved'] ) ) {
100 $output_args['comment_approved'] = $input['approved'];
101 }
102
103 /**
104 * Filter the $insert_post_args
105 *
106 * @param array<string,mixed> $output_args The array of $input_post_args that will be passed to wp_new_comment
107 * @param array<string,mixed> $input The data that was entered as input for the mutation
108 * @param string $mutation_name The type of mutation being performed ( create, edit, etc )
109 *
110 * @hookGroup models
111 * @since 0.0.5
112 */
113 $output_args = apply_filters( 'graphql_comment_insert_post_args', $output_args, $input, $mutation_name );
114
115 return $output_args;
116 }
117
118 /**
119 * This updates commentmeta.
120 *
121 * @param int $comment_id The ID of the postObject the comment is connected to
122 * @param array<string,mixed> $input The input for the mutation
123 * @param string $mutation_name The name of the mutation ( ex: create, update, delete )
124 * @param \WPGraphQL\AppContext $context The AppContext passed down to all resolvers
125 * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo passed down to all resolvers
126 *
127 * @return void
128 */
129 public static function update_additional_comment_data( int $comment_id, array $input, string $mutation_name, AppContext $context, ResolveInfo $info ) {
130
131 /**
132 * @todo: should account for authentication
133 */
134 $intended_comment_status = 0;
135 $default_comment_status = 0;
136
137 /**
138 * Fires after additional comment mutation data has been updated.
139 *
140 * @param int $comment_id The ID of the comment being mutated.
141 * @param array<string,mixed> $input The input for the mutation.
142 * @param string $mutation_name The name of the mutation (for example: create, update, delete).
143 * @param \WPGraphQL\AppContext $context The AppContext passed down to all resolvers.
144 * @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo passed down to all resolvers.
145 * @param int $intended_comment_status The status requested by mutation input.
146 * @param int $default_comment_status The fallback status when no explicit status is provided.
147 *
148 * @hookGroup models
149 * @since 0.0.5
150 */
151 do_action( 'graphql_comment_object_mutation_update_additional_data', $comment_id, $input, $mutation_name, $context, $info, $intended_comment_status, $default_comment_status );
152 }
153
154 /**
155 * Gets the user object for the comment author.
156 *
157 * @param ?string $author_email The authorEmail provided to the mutation input.
158 *
159 * @return \WP_User|false
160 */
161 protected static function get_comment_author( ?string $author_email = null ) {
162 $user = wp_get_current_user();
163
164 // Fail if no logged in user.
165 if ( 0 === $user->ID ) {
166 return false;
167 }
168
169 // Return the current user if they can only handle their own comments or if there's no specified author.
170 if ( empty( $author_email ) || ! $user->has_cap( 'moderate_comments' ) ) {
171 return $user;
172 }
173
174 $author = get_user_by( 'email', $author_email );
175
176 return ! empty( $author->ID ) ? $author : false;
177 }
178 }
179