Skip to content

Commit f61c81a

Browse files
committed
WPCOM Comments: Clear cache when liking/unliking a comment
1 parent 906369b commit f61c81a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function new_like( WP_REST_Request $request ) {
7373
$comment_id = $request->get_param( 'comment_id' );
7474
$blog_id = \Jetpack_Options::get_option( 'id' );
7575

76+
$cache_key = WPCom_Comments_Likes::get_cache_key( $comment_id, get_current_user_id() );
77+
delete_transient( $cache_key );
78+
7679
// Call WPCom remote API to record a new like.
7780
$response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
7881
"/sites/$blog_id/comments/$comment_id/likes/new",
@@ -96,6 +99,9 @@ public function delete_like( WP_REST_Request $request ) {
9699
$comment_id = $request->get_param( 'comment_id' );
97100
$blog_id = \Jetpack_Options::get_option( 'id' );
98101

102+
$cache_key = WPCom_Comments_Likes::get_cache_key( $comment_id, get_current_user_id() );
103+
delete_transient( $cache_key );
104+
99105
// Call WPCom remote API to delete the current user's like.
100106
$response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user(
101107
"/sites/$blog_id/comments/$comment_id/likes/mine/delete",

projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private function is_comment_likes_enabled() {
9494
* @param int $user_id The user ID.
9595
* @return string The cache key.
9696
*/
97-
private function get_cache_key( $comment_id, $user_id ) {
97+
public static function get_cache_key( $comment_id, $user_id ) {
9898
return "comment_like_{$comment_id}_{$user_id}";
9999
}
100100

@@ -107,7 +107,7 @@ private function get_cache_key( $comment_id, $user_id ) {
107107
* @return bool|WP_Error True if the comment is liked, false otherwise, or a WP_Error if the request fails.
108108
*/
109109
private function do_comment_like_api_request( $blog_id, $user_id, $comment_id ) {
110-
$cache_key = $this->get_cache_key( $comment_id, $user_id );
110+
$cache_key = self::get_cache_key( $comment_id, $user_id );
111111

112112
$cached_result = get_transient( $cache_key );
113113

0 commit comments

Comments
 (0)