PluginProbe
Gutenberg / 21.8.2
Gutenberg v21.8.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 / experimental / block-comments.php

block-comments.php in Gutenberg 21.8.2, at lib/experimental/block-comments.php

116 lines 4.3 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( 'block-comments' => 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 * Updates the comment type in the REST API.
31 *
32 * This function is used as a filter callback for the 'rest_pre_insert_comment' hook.
33 * It checks if the 'comment_type' parameter is set to 'block_comment' in the REST API request,
34 * and if so, updates the 'comment_type' and 'comment_approved' properties of the prepared comment.
35 *
36 * @param array $prepared_comment The prepared comment data.
37 * @param WP_REST_Request $request The REST API request object.
38 * @return array The updated prepared comment data.
39 */
40 if ( ! function_exists( 'update_comment_type_in_rest_api_6_8' ) ) {
41 function update_comment_type_in_rest_api_6_8( $prepared_comment, $request ) {
42 if ( ! empty( $request['comment_type'] ) && 'block_comment' === $request['comment_type'] ) {
43 $prepared_comment['comment_type'] = $request['comment_type'];
44 $prepared_comment['comment_approved'] = $request['comment_approved'];
45 }
46
47 return $prepared_comment;
48 }
49 add_filter( 'rest_pre_insert_comment', 'update_comment_type_in_rest_api_6_8', 10, 2 );
50 }
51
52 /**
53 * Updates the comment type for avatars in the WordPress REST API.
54 *
55 * This function adds the 'block_comment' type to the list of comment types
56 * for which avatars should be retrieved in the WordPress REST API.
57 *
58 * @param array $comment_type The array of comment types.
59 * @return array The updated array of comment types.
60 */
61 if ( ! function_exists( 'update_get_avatar_comment_type' ) ) {
62 function update_get_avatar_comment_type( $comment_type ) {
63 $comment_type[] = 'block_comment';
64 return $comment_type;
65 }
66 add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' );
67 }
68
69 /**
70 * Excludes block comments from the admin comments query.
71 *
72 * This function modifies the comments query to exclude comments of type 'block_comment'
73 * when the query is for comments in the WordPress admin.
74 *
75 * @global wpdb $wpdb WordPress database abstraction object.
76 *
77 * @param string[] $clauses The current SQL clauses for the comments query.
78 * @param WP_Comment_Query $query The current comments query.
79 *
80 * @return string[] The modified SQL clauses for the comments query.
81 */
82 if ( ! function_exists( 'exclude_block_comments_from_admin' ) ) {
83 function exclude_block_comments_from_admin( $clauses, $query ) {
84 // Only modify the query if it's for comments
85 if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) {
86 $query->set( 'type', '' );
87
88 global $wpdb;
89 $clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'";
90 }
91
92 return $clauses;
93 }
94 add_action( 'comments_clauses', 'exclude_block_comments_from_admin', 10, 2 );
95 }
96
97 /**
98 * Filter the comment count query to exclude block_comment type comments.
99 *
100 * Note: we need to make sure this doesn't interfere with the "Editorial Comments" view
101 * once https://github.com/WordPress/gutenberg/issues/71621 is implemented.
102 *
103 * @param string $query The SQL query string.
104 * @return string The modified SQL query string.
105 */
106 function gutenberg_filter_comment_count_query_exclude_block_comments( $query ) {
107 // Adjust the query if it is a comment count query.
108 if ( str_starts_with( $query, 'SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM' ) && str_contains( $query, 'comment_approved' ) ) {
109 if ( ! str_contains( $query, "comment_type != 'block_comment'" ) ) {
110 $query = str_replace( 'comment_approved', "comment_type != 'block_comment' AND comment_approved", $query );
111 }
112 }
113 return $query;
114 }
115 add_filter( 'query', 'gutenberg_filter_comment_count_query_exclude_block_comments' );
116