PluginProbe
Gutenberg / 23.3.2
Gutenberg v23.3.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-6.9 / block-comments.php

block-comments.php in Gutenberg 23.3.2, at lib/compat/wordpress-6.9/block-comments.php

314 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Adds support for block comments to the built-in post types.
5 *
6 * @return void
7 */
8 function gutenberg_block_comment_add_post_type_support() {
9 $post_types = array( 'post', 'page' );
10
11 foreach ( $post_types as $post_type ) {
12 if ( ! post_type_supports( $post_type, 'editor' ) ) {
13 continue;
14 }
15
16 $supports = get_all_post_type_supports( $post_type );
17 $editor_supports = array( 'notes' => true );
18
19 // `add_post_type_support()` doesn't merge support sub-properties, so we explicitly merge it here.
20 if ( is_array( $supports['editor'] ) && isset( $supports['editor'][0] ) && is_array( $supports['editor'][0] ) ) {
21 $editor_supports = array_merge( $editor_supports, $supports['editor'][0] );
22 }
23
24 add_post_type_support( $post_type, 'editor', $editor_supports );
25 }
26 }
27 add_action( 'init', 'gutenberg_block_comment_add_post_type_support' );
28
29 /**
30 * Register comment metadata for block comment status.
31 */
32 function gutenberg_register_block_comment_metadata() {
33 register_meta(
34 'comment',
35 '_wp_note_status',
36 array(
37 'type' => 'string',
38 'description' => __( 'Note resolution status', 'gutenberg' ),
39 'single' => true,
40 'show_in_rest' => array(
41 'schema' => array(
42 'type' => 'string',
43 'enum' => array( 'resolved', 'reopen' ),
44 ),
45 ),
46 'auth_callback' => function ( $allowed, $meta_key, $object_id ) {
47 return current_user_can( 'edit_comment', $object_id );
48 },
49 )
50 );
51 }
52 add_action( 'init', 'gutenberg_register_block_comment_metadata' );
53
54 /**
55 * Updates the comment type for avatars in the WordPress REST API.
56 *
57 * This function adds the 'note' type to the list of comment types
58 * for which avatars should be retrieved in the WordPress REST API.
59 *
60 * @param array $comment_type The array of comment types.
61 * @return array The updated array of comment types.
62 */
63 if ( ! function_exists( 'update_get_avatar_comment_type' ) ) {
64 function update_get_avatar_comment_type( $comment_type ) {
65 $comment_type[] = 'note';
66 return $comment_type;
67 }
68 add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' );
69 }
70
71 /**
72 * Excludes block comments from the admin comments query.
73 *
74 * This function modifies the comments query to exclude comments of type 'note'
75 * when the query is for comments in the WordPress admin.
76 *
77 * @global wpdb $wpdb WordPress database abstraction object.
78 *
79 * @param string[] $clauses The current SQL clauses for the comments query.
80 * @param WP_Comment_Query $query The current comments query.
81 *
82 * @return string[] The modified SQL clauses for the comments query.
83 */
84 if ( ! function_exists( 'exclude_block_comments_from_admin' ) ) {
85 function exclude_block_comments_from_admin( $clauses, $query ) {
86 // Only modify the query if it's for comments
87 if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) {
88 $query->set( 'type', '' );
89
90 global $wpdb;
91 $clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'note'";
92 }
93
94 return $clauses;
95 }
96 add_action( 'comments_clauses', 'exclude_block_comments_from_admin', 10, 2 );
97 }
98
99 /**
100 * Filter the comment count query to exclude block_comment type comments.
101 *
102 * Note: we need to make sure this doesn't interfere with the "Editorial Comments" view
103 * once https://github.com/WordPress/gutenberg/issues/71621 is implemented.
104 *
105 * @param string $query The SQL query string.
106 * @return string The modified SQL query string.
107 */
108 function gutenberg_filter_comment_count_query_exclude_block_comments( $query ) {
109 // Adjust the query if it is a comment count query.
110 if ( str_starts_with( $query, 'SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM' ) && str_contains( $query, 'comment_approved' ) ) {
111 if ( ! str_contains( $query, "comment_type != 'note'" ) ) {
112 $query = str_replace( 'comment_approved', "comment_type != 'note' AND comment_approved", $query );
113 }
114 }
115 return $query;
116 }
117 add_filter( 'query', 'gutenberg_filter_comment_count_query_exclude_block_comments' );
118
119 /**
120 * Adjusts the comments list table query so `comment_type=note` never displays.
121 *
122 * @param array $args An array of get_comments() arguments.
123 * @return array Possibly modified arguments for get_comments().
124 */
125 function gutenberg_hide_note_from_comment_list_table( $args ) {
126 if ( ! empty( $_REQUEST['comment_type'] ) && 'note' === $_REQUEST['comment_type'] ) {
127 unset( $args['type'] );
128 }
129 return $args;
130 }
131 add_filter( 'comments_list_table_query_args', 'gutenberg_hide_note_from_comment_list_table' );
132
133 /**
134 * Override comment_count to exclude notes from the comment count.
135 *
136 * @param int|null $new The new comment count. Default null.
137 * @param int $old The old comment count.
138 * @param int $post_id Post ID.
139 * @return int|null The modified comment count.
140 */
141 function gutenberg_exclude_notes_from_comment_count( $new_count, $old_count, $post_id ) {
142 global $wpdb;
143 // If another filter already set a count, respect it.
144 if ( null !== $new_count ) {
145 return $new_count;
146 }
147 $new_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type != 'note'", $post_id ) );
148 return $new_count;
149 }
150 add_filter( 'pre_wp_update_comment_count_now', 'gutenberg_exclude_notes_from_comment_count', 10, 3 );
151
152 /**
153 * Registers the `wp_notes_notify` option and renders its UI on the Discussion screen.
154 *
155 * @return void
156 */
157 function gutenberg_register_wp_notes_notify_setting() {
158 if ( is_wp_version_compatible( '6.9' ) ) {
159 return;
160 }
161
162 register_setting(
163 'discussion',
164 'wp_notes_notify',
165 array(
166 'type' => 'boolean',
167 'description' => __( 'Email me whenever anyone posts a note', 'gutenberg' ),
168 'sanitize_callback' => 'rest_sanitize_boolean',
169 'default' => 1,
170 )
171 );
172
173 add_settings_field(
174 'wp_notes_notify',
175 __( 'Notes', 'gutenberg' ),
176 function () {
177 ?>
178 <label for="wp_notes_notify">
179 <input name="wp_notes_notify" type="checkbox" id="wp_notes_notify" value="1" <?php checked( '1', get_option( 'wp_notes_notify', 1 ) ); ?>/>
180 <?php _e( 'Email me whenever anyone posts a note', 'gutenberg' ); ?>
181 </label>
182 <?php
183 },
184 'discussion'
185 );
186 }
187 add_action( 'admin_init', 'gutenberg_register_wp_notes_notify_setting' );
188
189 /**
190 * Compatibility implementation of wp_new_comment_notify_postauthor() with notes support.
191 *
192 * @param int $comment_id The comment ID.
193 * @return bool True on success, false on failure.
194 */
195 function gutenberg_new_comment_notify_postauthor( $comment_id ) {
196 $comment = get_comment( $comment_id );
197 $is_note = ( $comment && 'note' === $comment->comment_type );
198
199 $maybe_notify = $is_note ? get_option( 'wp_notes_notify', 1 ) : get_option( 'comments_notify' );
200
201 /**
202 * Filters whether to send the post author new comment notification emails,
203 * overriding the site setting.
204 *
205 * @since 4.4.0
206 *
207 * @param bool $maybe_notify Whether to notify the post author about the new comment.
208 * @param int $comment_id The ID of the comment for the notification.
209 */
210 $maybe_notify = apply_filters( 'notify_post_author', $maybe_notify, $comment_id );
211
212 /*
213 * wp_notify_postauthor() checks if notifying the author of their own comment.
214 * By default, it won't, but filters can override this.
215 */
216 if ( ! $maybe_notify ) {
217 return false;
218 }
219
220 // Send notifications for approved comments and all notes.
221 if (
222 ! isset( $comment->comment_approved ) ||
223 ( '1' !== $comment->comment_approved && ! $is_note ) ) {
224 return false;
225 }
226
227 return wp_notify_postauthor( $comment_id );
228 }
229
230 /**
231 * Compatibility implementation of wp_new_comment_via_rest_notify_postauthor()
232 * function introduced in WordPress 6.9.
233 *
234 * @param WP_Comment $comment The comment object.
235 */
236 function gutenberg_new_comment_via_rest_notify_postauthor( $comment ) {
237 if ( $comment instanceof WP_Comment && 'note' === $comment->comment_type ) {
238 gutenberg_new_comment_notify_postauthor( (int) $comment->comment_ID );
239 }
240 }
241
242 if ( has_action( 'rest_insert_comment', 'wp_new_comment_via_rest_notify_postauthor' ) ) {
243 remove_action( 'rest_insert_comment', 'wp_new_comment_via_rest_notify_postauthor' );
244 add_action( 'rest_insert_comment', 'gutenberg_new_comment_via_rest_notify_postauthor' );
245 }
246
247 /**
248 * Filters the note notification text.
249 *
250 * @param string $notify_message The comment notification email text.
251 * @param string $comment_id Comment ID as a numeric string.
252 *
253 * @return string The filtered notification text.
254 */
255 function gutenberg_filter_note_notification_text( $notify_message, $comment_id ) {
256 $comment = get_comment( $comment_id );
257 if ( ! $comment || 'note' !== $comment->comment_type ) {
258 return $notify_message;
259 }
260
261 $post = get_post( $comment->comment_post_ID );
262 if ( ! $post ) {
263 return $notify_message;
264 }
265
266 $comment_author_domain = '';
267 if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) {
268 $comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
269 }
270
271 $comment_content = wp_specialchars_decode( $comment->comment_content );
272
273 /* translators: %s: Post title. */
274 $notify_message = sprintf( __( 'New note on your post "%s"', 'gutenberg' ), $post->post_title ) . "\r\n";
275 /* translators: 1: Note author's name, 2: Note author's IP address, 3: Note author's hostname. */
276 $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)', 'gutenberg' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
277 /* translators: %s: Note author email. */
278 $notify_message .= sprintf( __( 'Email: %s', 'gutenberg' ), $comment->comment_author_email ) . "\r\n";
279 /* translators: %s: Note text. */
280 $notify_message .= sprintf( __( 'Note: %s', 'gutenberg' ), "\r\n" . ( empty( $comment_content ) ? __( 'resolved/reopened' ) : $comment_content ) ) . "\r\n\r\n";
281 $notify_message .= __( 'You can see all notes on this post here:', 'gutenberg' ) . "\r\n";
282 $notify_message .= get_edit_post_link( $comment->comment_post_ID, 'url' ) . "\r\n";
283
284 return $notify_message;
285 }
286 add_filter( 'comment_notification_text', 'gutenberg_filter_note_notification_text', 10, 2 );
287
288 /**
289 * Filters the note notification subject.
290 *
291 * @param string $subject The comment notification email subject.
292 * @param string $comment_id Comment ID as a numeric string.
293 *
294 * @return string The filtered notification subject.
295 */
296 function gutenberg_filter_note_notification_subject( $subject, $comment_id ) {
297 $comment = get_comment( $comment_id );
298 if ( ! $comment || 'note' !== $comment->comment_type ) {
299 return $subject;
300 }
301
302 $post = get_post( $comment->comment_post_ID );
303 if ( ! $post ) {
304 return $subject;
305 }
306
307 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
308
309 /* translators: Note notification email subject. 1: Site title, 2: Post title. */
310 $subject = sprintf( __( '[%1$s] Note: "%2$s"', 'gutenberg' ), $blogname, $post->post_title );
311 return $subject;
312 }
313 add_filter( 'comment_notification_subject', 'gutenberg_filter_note_notification_subject', 10, 2 );
314