| 1 |
<?php |
| 2 |
/** |
| 3 |
* Updates the comment type in the REST API. |
| 4 |
* |
| 5 |
* This function is used as a filter callback for the 'rest_pre_insert_comment' hook. |
| 6 |
* It checks if the 'comment_type' parameter is set to 'block_comment' in the REST API request, |
| 7 |
* and if so, updates the 'comment_type' and 'comment_approved' properties of the prepared comment. |
| 8 |
* |
| 9 |
* @param array $prepared_comment The prepared comment data. |
| 10 |
* @param WP_REST_Request $request The REST API request object. |
| 11 |
* @return array The updated prepared comment data. |
| 12 |
*/ |
| 13 |
if ( ! function_exists( 'update_comment_type_in_rest_api_6_8' ) ) { |
| 14 |
function update_comment_type_in_rest_api_6_8( $prepared_comment, $request ) { |
| 15 |
if ( ! empty( $request['comment_type'] ) && 'block_comment' === $request['comment_type'] ) { |
| 16 |
$prepared_comment['comment_type'] = $request['comment_type']; |
| 17 |
$prepared_comment['comment_approved'] = $request['comment_approved']; |
| 18 |
} |
| 19 |
|
| 20 |
return $prepared_comment; |
| 21 |
} |
| 22 |
add_filter( 'rest_pre_insert_comment', 'update_comment_type_in_rest_api_6_8', 10, 2 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Updates the comment type for avatars in the WordPress REST API. |
| 27 |
* |
| 28 |
* This function adds the 'block_comment' type to the list of comment types |
| 29 |
* for which avatars should be retrieved in the WordPress REST API. |
| 30 |
* |
| 31 |
* @param array $comment_type The array of comment types. |
| 32 |
* @return array The updated array of comment types. |
| 33 |
*/ |
| 34 |
if ( ! function_exists( 'update_get_avatar_comment_type' ) ) { |
| 35 |
function update_get_avatar_comment_type( $comment_type ) { |
| 36 |
$comment_type[] = 'block_comment'; |
| 37 |
return $comment_type; |
| 38 |
} |
| 39 |
add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Excludes block comments from the admin comments query. |
| 44 |
* |
| 45 |
* This function modifies the comments query to exclude comments of type 'block_comment' |
| 46 |
* when the query is for comments in the WordPress admin. |
| 47 |
* |
| 48 |
* @global wpdb $wpdb WordPress database abstraction object. |
| 49 |
* |
| 50 |
* @param WP_Comment_Query $query The current comments query. |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
if ( ! function_exists( 'exclude_block_comments_from_admin' ) ) { |
| 55 |
function exclude_block_comments_from_admin( $query ) { |
| 56 |
// Only modify the query if it's for comments |
| 57 |
if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) { |
| 58 |
$query->set( 'type', '' ); |
| 59 |
|
| 60 |
add_filter( |
| 61 |
'comments_clauses', |
| 62 |
function ( $clauses ) { |
| 63 |
global $wpdb; |
| 64 |
// Exclude comments of type 'block_comment' |
| 65 |
$clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'"; |
| 66 |
return $clauses; |
| 67 |
} |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
add_action( 'pre_get_comments', 'exclude_block_comments_from_admin' ); |
| 72 |
} |
| 73 |
|