From 79d4a9f06ff0da47bafd5f92d4cbd1153f92d98a Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Fri, 4 Jul 2025 23:18:24 -0500 Subject: [PATCH 1/6] jetpack-mu-wpcom: Add comment like caching and only send requests when module active --- .../wpcom-comments/wpcom-comments.php | 301 ++++++++++++------ 1 file changed, 208 insertions(+), 93 deletions(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php index 50b1cc281c3b0..09cc182613c88 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php @@ -1,4 +1,4 @@ - 'GET' ), - null, - 'rest' - ); +class WPCom_Comments_Likes { + const CACHE_EXPIRATION = HOUR_IN_SECONDS; + + /** + * Singleton instance. + * + * @var WPCom_Comments_Likes + */ + private static $instance = null; + + /** + * Flag to track if hooks have been initialized. + * + * @var bool + */ + private $initialized = false; - // If the request fails, simply return the unmodified classes. - if ( is_wp_error( $response ) ) { - return $classes; + /** + * Private constructor to prevent direct instantiation. + */ + private function __construct() { + // Register REST API for Atomic sites. + if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) { + add_action( 'rest_api_init', array( $this, 'register_like_api' ) ); } - $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); + add_action( 'admin_init', array( $this, 'init' ) ); + } + + /** + * Get the singleton instance. + * + * @return WPCom_Comments_Likes + */ + public static function get_instance() { + if ( null === self::$instance ) { + self::$instance = new self(); + } + return self::$instance; + } - // If the response doesn't include 'i_like', don't add the class. - if ( empty( $response_data['i_like'] ) ) { - return $classes; + /** + * Initialize the comment likes functionality if enabled. + */ + public function init() { + // Only initialize if comment likes are enabled. + if ( ! $this->is_comment_likes_enabled() ) { + return; } - $liked = $response_data['i_like']; + if ( is_admin() ) { + add_filter( 'comment_class', array( $this, 'add_like_class' ), 10, 3 ); + add_filter( 'comment_row_actions', array( $this, 'enable_likes' ), 10, 2 ); + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 2 ); + } } - // Append the 'liked' class if the comment is liked. - if ( $liked ) { - $classes[] = 'liked'; + /** + * Check if comment likes are enabled. + * + * This is currently only expected to work for WoW sites. + * + * @return bool True if comment likes are enabled, false otherwise. + */ + private function is_comment_likes_enabled() { + // @phan-suppress-next-line PhanUndeclaredClassReference + if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'is_module_active' ) ) { + // @phan-suppress-next-line PhanUndeclaredClassMethod + return Jetpack::is_module_active( 'comment-likes' ); + } + + return false; } - return $classes; -} -add_filter( 'comment_class', 'wpcom_comments_add_like_class', 10, 3 ); + /** + * Get cache key for comment like status. + * + * @param int $comment_id The comment ID. + * @param int $user_id The user ID. + * @return string The cache key. + */ + private function get_cache_key( $comment_id, $user_id ) { + return "comment_like_{$comment_id}_{$user_id}"; + } -/** - * Adds "Like" and "Unlike" action buttons to comment rows. - * - * @param array $actions Array of actions for the comment. - * @param WP_Comment $comment The comment object. - * @return array Modified actions array. - */ -function wpcom_comments_enable_likes( $actions, $comment ) { - $actions['like'] = sprintf( - '', - $comment->comment_ID, - esc_attr__( 'Like this comment', 'jetpack-mu-wpcom' ), - esc_html__( 'Like', 'jetpack-mu-wpcom' ) - ); - - $actions['unlike'] = sprintf( - '', - $comment->comment_ID, - esc_attr__( 'Unlike this comment', 'jetpack-mu-wpcom' ), - esc_html__( 'Liked by you', 'jetpack-mu-wpcom' ) - ); - - return $actions; -} -add_filter( 'comment_row_actions', 'wpcom_comments_enable_likes', 10, 2 ); + /** + * Do a comment like API request. + * + * @param int $blog_id The blog ID. + * @param int $user_id The user ID. + * @param int $comment_id The comment ID. + * @return bool|WP_Error True if the comment is liked, false otherwise, or a WP_Error if the request fails. + */ + private function do_comment_like_api_request( $blog_id, $user_id, $comment_id ) { + $cache_key = $this->get_cache_key( $comment_id, $user_id ); -/** - * Enqueues the comment like assets (JavaScript and CSS) on the Edit Comments screen. - * - * @param string $hook The current admin page hook. - */ -function wpcom_enqueue_comment_like_script( $hook ) { - // Only enqueue assets on the edit-comments screen. - if ( 'edit-comments.php' !== $hook ) { - return; + $cached_result = get_transient( $cache_key ); + + if ( false !== $cached_result ) { + $liked = $cached_result; + } else { + $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( + "/sites/$blog_id/comments/$comment_id/likes", + 'v1.1', + array( 'method' => 'GET' ), + null, + 'rest' + ); + + // If the request fails, simply return the unmodified classes. + if ( is_wp_error( $response ) ) { + return $response; + } + + $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); + + $liked = empty( $response_data['i_like'] ) ? 'no' : 'yes'; + + // Cache the result. + set_transient( $cache_key, $liked, self::CACHE_EXPIRATION ); + } + + return $liked; } - // Enqueue the assets using the Jetpack MU WPCom helper function. - jetpack_mu_wpcom_enqueue_assets( 'wpcom-comment-like', array( 'js', 'css' ) ); - - // Localize the script with error messages. - wp_localize_script( - 'jetpack-mu-wpcom-wpcom-comment-like', - 'wpcomCommentLikesData', - array( - 'post_like_error' => __( 'Something went wrong when attempting to like that comment. Please try again.', 'jetpack-mu-wpcom' ), - 'post_unlike_error' => __( 'Something went wrong when attempting to unlike that comment. Please try again.', 'jetpack-mu-wpcom' ), - 'dismiss_notice_text' => __( 'Dismiss this notice', 'jetpack-mu-wpcom' ), - ) - ); -} -add_action( 'admin_enqueue_scripts', 'wpcom_enqueue_comment_like_script', 10, 2 ); + /** + * Adds a "liked" class to comments that the current user has liked. + * + * @param array $classes Array of comment classes. + * @param string $css_class Unused. + * @param int $comment_id The comment ID. + * @return array Modified array of comment classes. + */ + public function add_like_class( $classes, $css_class, $comment_id ) { + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { + $blog_id = get_current_blog_id(); + $liked = Likes::comment_like_current_user_likes( $blog_id, $comment_id ); + } else { + $blog_id = Jetpack_Options::get_option( 'id' ); + $user_id = get_current_user_id(); -/** - * Register an API to handle Likes on Atomic sites. - */ -function wpcom_comments_register_like_api() { - require_once __DIR__ . '/class-wp-rest-comment-like.php'; - $controller = new WP_REST_Comment_Like(); - $controller->register_routes(); -} -if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) { - add_action( 'rest_api_init', 'wpcom_comments_register_like_api' ); + $liked = $this->do_comment_like_api_request( $blog_id, $user_id, $comment_id ); + + if ( is_wp_error( $liked ) ) { + return $classes; + } + + // We use 'yes' and 'no' when we store the transient to minimize unexpected casting. + // So, we cast to boolean here. + $liked = 'yes' === $liked; + } + + // Append the 'liked' class if the comment is liked. + if ( $liked ) { + $classes[] = 'liked'; + } + + return $classes; + } + + /** + * Adds "Like" and "Unlike" action buttons to comment rows. + * + * @param array $actions Array of actions for the comment. + * @param WP_Comment $comment The comment object. + * @return array Modified actions array. + */ + public function enable_likes( $actions, $comment ) { + $actions['like'] = sprintf( + '', + $comment->comment_ID, + esc_attr__( 'Like this comment', 'jetpack-mu-wpcom' ), + esc_html__( 'Like', 'jetpack-mu-wpcom' ) + ); + + $actions['unlike'] = sprintf( + '', + $comment->comment_ID, + esc_attr__( 'Unlike this comment', 'jetpack-mu-wpcom' ), + esc_html__( 'Liked by you', 'jetpack-mu-wpcom' ) + ); + + return $actions; + } + + /** + * Enqueues the comment like assets (JavaScript and CSS) on the Edit Comments screen. + * + * @param string $hook The current admin page hook. + */ + public function enqueue_scripts( $hook ) { + // Only enqueue assets on the edit-comments screen. + if ( 'edit-comments.php' !== $hook ) { + return; + } + + // Enqueue the assets using the Jetpack MU WPCom helper function. + jetpack_mu_wpcom_enqueue_assets( 'wpcom-comment-like', array( 'js', 'css' ) ); + + // Localize the script with error messages. + wp_localize_script( + 'jetpack-mu-wpcom-wpcom-comment-like', + 'wpcomCommentLikesData', + array( + 'post_like_error' => __( 'Something went wrong when attempting to like that comment. Please try again.', 'jetpack-mu-wpcom' ), + 'post_unlike_error' => __( 'Something went wrong when attempting to unlike that comment. Please try again.', 'jetpack-mu-wpcom' ), + 'dismiss_notice_text' => __( 'Dismiss this notice', 'jetpack-mu-wpcom' ), + ) + ); + } + + /** + * Register an API to handle Likes on Atomic sites. + */ + public function register_like_api() { + require_once __DIR__ . '/class-wp-rest-comment-like.php'; + $controller = new WP_REST_Comment_Like(); + $controller->register_routes(); + } } + +WPCom_Comments_Likes::get_instance(); From 1ada9206f1234673c7a285ad3cb5a1d76bfbc782 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Fri, 4 Jul 2025 23:19:33 -0500 Subject: [PATCH 2/6] changelog --- .../update-jetpack-mu-wpcom-improve-comments-performance | 4 ++++ .../update-jetpack-mu-wpcom-improve-comments-performance | 4 ++++ .../update-jetpack-mu-wpcom-improve-comments-performance | 4 ++++ .../update-jetpack-mu-wpcom-improve-comments-performance | 4 ++++ .../update-jetpack-mu-wpcom-improve-comments-performance | 4 ++++ .../update-jetpack-mu-wpcom-improve-comments-performance | 4 ++++ .../update-jetpack-mu-wpcom-improve-comments-performance | 4 ++++ .../update-jetpack-mu-wpcom-improve-comments-performance | 4 ++++ 8 files changed, 32 insertions(+) create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/update-jetpack-mu-wpcom-improve-comments-performance create mode 100644 projects/plugins/boost/changelog/update-jetpack-mu-wpcom-improve-comments-performance create mode 100644 projects/plugins/protect/changelog/update-jetpack-mu-wpcom-improve-comments-performance create mode 100644 projects/plugins/search/changelog/update-jetpack-mu-wpcom-improve-comments-performance create mode 100644 projects/plugins/social/changelog/update-jetpack-mu-wpcom-improve-comments-performance create mode 100644 projects/plugins/starter-plugin/changelog/update-jetpack-mu-wpcom-improve-comments-performance create mode 100644 projects/plugins/videopress/changelog/update-jetpack-mu-wpcom-improve-comments-performance create mode 100644 projects/plugins/wpcomsh/changelog/update-jetpack-mu-wpcom-improve-comments-performance diff --git a/projects/packages/jetpack-mu-wpcom/changelog/update-jetpack-mu-wpcom-improve-comments-performance b/projects/packages/jetpack-mu-wpcom/changelog/update-jetpack-mu-wpcom-improve-comments-performance new file mode 100644 index 0000000000000..69b07f60c499b --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/update-jetpack-mu-wpcom-improve-comments-performance @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improves performance of wpcom comments liking by caching and minimizing API requests. diff --git a/projects/plugins/boost/changelog/update-jetpack-mu-wpcom-improve-comments-performance b/projects/plugins/boost/changelog/update-jetpack-mu-wpcom-improve-comments-performance new file mode 100644 index 0000000000000..69b07f60c499b --- /dev/null +++ b/projects/plugins/boost/changelog/update-jetpack-mu-wpcom-improve-comments-performance @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improves performance of wpcom comments liking by caching and minimizing API requests. diff --git a/projects/plugins/protect/changelog/update-jetpack-mu-wpcom-improve-comments-performance b/projects/plugins/protect/changelog/update-jetpack-mu-wpcom-improve-comments-performance new file mode 100644 index 0000000000000..69b07f60c499b --- /dev/null +++ b/projects/plugins/protect/changelog/update-jetpack-mu-wpcom-improve-comments-performance @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improves performance of wpcom comments liking by caching and minimizing API requests. diff --git a/projects/plugins/search/changelog/update-jetpack-mu-wpcom-improve-comments-performance b/projects/plugins/search/changelog/update-jetpack-mu-wpcom-improve-comments-performance new file mode 100644 index 0000000000000..69b07f60c499b --- /dev/null +++ b/projects/plugins/search/changelog/update-jetpack-mu-wpcom-improve-comments-performance @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improves performance of wpcom comments liking by caching and minimizing API requests. diff --git a/projects/plugins/social/changelog/update-jetpack-mu-wpcom-improve-comments-performance b/projects/plugins/social/changelog/update-jetpack-mu-wpcom-improve-comments-performance new file mode 100644 index 0000000000000..69b07f60c499b --- /dev/null +++ b/projects/plugins/social/changelog/update-jetpack-mu-wpcom-improve-comments-performance @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improves performance of wpcom comments liking by caching and minimizing API requests. diff --git a/projects/plugins/starter-plugin/changelog/update-jetpack-mu-wpcom-improve-comments-performance b/projects/plugins/starter-plugin/changelog/update-jetpack-mu-wpcom-improve-comments-performance new file mode 100644 index 0000000000000..69b07f60c499b --- /dev/null +++ b/projects/plugins/starter-plugin/changelog/update-jetpack-mu-wpcom-improve-comments-performance @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improves performance of wpcom comments liking by caching and minimizing API requests. diff --git a/projects/plugins/videopress/changelog/update-jetpack-mu-wpcom-improve-comments-performance b/projects/plugins/videopress/changelog/update-jetpack-mu-wpcom-improve-comments-performance new file mode 100644 index 0000000000000..69b07f60c499b --- /dev/null +++ b/projects/plugins/videopress/changelog/update-jetpack-mu-wpcom-improve-comments-performance @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improves performance of wpcom comments liking by caching and minimizing API requests. diff --git a/projects/plugins/wpcomsh/changelog/update-jetpack-mu-wpcom-improve-comments-performance b/projects/plugins/wpcomsh/changelog/update-jetpack-mu-wpcom-improve-comments-performance new file mode 100644 index 0000000000000..69b07f60c499b --- /dev/null +++ b/projects/plugins/wpcomsh/changelog/update-jetpack-mu-wpcom-improve-comments-performance @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improves performance of wpcom comments liking by caching and minimizing API requests. From e920d9ba5c6458ecc2900a665127b67e67c58705 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Sun, 6 Jul 2025 21:29:56 -0500 Subject: [PATCH 3/6] WPCOM Comments: Clear cache when liking/unliking a comment --- .../features/wpcom-comments/class-wp-rest-comment-like.php | 6 ++++++ .../src/features/wpcom-comments/wpcom-comments.php | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php index c22c8a2a24ce9..2f792168e6beb 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php @@ -73,6 +73,9 @@ public function new_like( WP_REST_Request $request ) { $comment_id = $request->get_param( 'comment_id' ); $blog_id = \Jetpack_Options::get_option( 'id' ); + $cache_key = WPCom_Comments_Likes::get_cache_key( $comment_id, get_current_user_id() ); + delete_transient( $cache_key ); + // Call WPCom remote API to record a new like. $response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( "/sites/$blog_id/comments/$comment_id/likes/new", @@ -96,6 +99,9 @@ public function delete_like( WP_REST_Request $request ) { $comment_id = $request->get_param( 'comment_id' ); $blog_id = \Jetpack_Options::get_option( 'id' ); + $cache_key = WPCom_Comments_Likes::get_cache_key( $comment_id, get_current_user_id() ); + delete_transient( $cache_key ); + // Call WPCom remote API to delete the current user's like. $response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( "/sites/$blog_id/comments/$comment_id/likes/mine/delete", diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php index 09cc182613c88..26e58039f8f8d 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php @@ -94,7 +94,7 @@ private function is_comment_likes_enabled() { * @param int $user_id The user ID. * @return string The cache key. */ - private function get_cache_key( $comment_id, $user_id ) { + public static function get_cache_key( $comment_id, $user_id ) { return "comment_like_{$comment_id}_{$user_id}"; } @@ -107,7 +107,7 @@ private function get_cache_key( $comment_id, $user_id ) { * @return bool|WP_Error True if the comment is liked, false otherwise, or a WP_Error if the request fails. */ private function do_comment_like_api_request( $blog_id, $user_id, $comment_id ) { - $cache_key = $this->get_cache_key( $comment_id, $user_id ); + $cache_key = self::get_cache_key( $comment_id, $user_id ); $cached_result = get_transient( $cache_key ); From 24a5fa15fdad70707049b14d279b03c60c5fef2a Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Sun, 6 Jul 2025 21:48:11 -0500 Subject: [PATCH 4/6] WPCOM Comments: Only show like actions on edit-comments --- .../features/wpcom-comments/wpcom-comments.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php index 26e58039f8f8d..be0c663b5f7b7 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php @@ -39,7 +39,7 @@ private function __construct() { add_action( 'rest_api_init', array( $this, 'register_like_api' ) ); } - add_action( 'admin_init', array( $this, 'init' ) ); + add_action( 'current_screen', array( $this, 'init' ) ); } /** @@ -63,11 +63,18 @@ public function init() { return; } - if ( is_admin() ) { - add_filter( 'comment_class', array( $this, 'add_like_class' ), 10, 3 ); - add_filter( 'comment_row_actions', array( $this, 'enable_likes' ), 10, 2 ); - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 2 ); + if ( ! is_admin() ) { + return; } + + $screen = get_current_screen(); + if ( ! $screen || 'edit-comments' !== $screen->id ) { + return; + } + + add_filter( 'comment_class', array( $this, 'add_like_class' ), 10, 3 ); + add_filter( 'comment_row_actions', array( $this, 'enable_likes' ), 10, 2 ); + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 2 ); } /** From 3b99031774983df62dec752373a04be3e5ab6893 Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Wed, 9 Jul 2025 11:15:37 +0200 Subject: [PATCH 5/6] WPCOM Comments: Use 15 minutes as cache expiration --- .../src/features/wpcom-comments/wpcom-comments.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php index be0c663b5f7b7..6fc015024debe 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php @@ -14,7 +14,7 @@ * WPCom Comments Likes functionality in a singleton pattern. */ class WPCom_Comments_Likes { - const CACHE_EXPIRATION = HOUR_IN_SECONDS; + const CACHE_EXPIRATION = 900; // 15 minutes in seconds. /** * Singleton instance. From ec49f68c7f87d1da8ae05e43dac905311abcc6fd Mon Sep 17 00:00:00 2001 From: Eric Binnion Date: Thu, 17 Jul 2025 21:07:15 -0500 Subject: [PATCH 6/6] jetpack-mu-wpcom: remove caching for wpcom-comments likes --- .../class-wp-rest-comment-like.php | 6 -- .../wpcom-comments/wpcom-comments.php | 62 +++++-------------- 2 files changed, 15 insertions(+), 53 deletions(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php index 2f792168e6beb..c22c8a2a24ce9 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/class-wp-rest-comment-like.php @@ -73,9 +73,6 @@ public function new_like( WP_REST_Request $request ) { $comment_id = $request->get_param( 'comment_id' ); $blog_id = \Jetpack_Options::get_option( 'id' ); - $cache_key = WPCom_Comments_Likes::get_cache_key( $comment_id, get_current_user_id() ); - delete_transient( $cache_key ); - // Call WPCom remote API to record a new like. $response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( "/sites/$blog_id/comments/$comment_id/likes/new", @@ -99,9 +96,6 @@ public function delete_like( WP_REST_Request $request ) { $comment_id = $request->get_param( 'comment_id' ); $blog_id = \Jetpack_Options::get_option( 'id' ); - $cache_key = WPCom_Comments_Likes::get_cache_key( $comment_id, get_current_user_id() ); - delete_transient( $cache_key ); - // Call WPCom remote API to delete the current user's like. $response = \Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( "/sites/$blog_id/comments/$comment_id/likes/mine/delete", diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php index 6fc015024debe..a3a82da873fbb 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-comments/wpcom-comments.php @@ -14,8 +14,6 @@ * WPCom Comments Likes functionality in a singleton pattern. */ class WPCom_Comments_Likes { - const CACHE_EXPIRATION = 900; // 15 minutes in seconds. - /** * Singleton instance. * @@ -94,55 +92,30 @@ private function is_comment_likes_enabled() { return false; } - /** - * Get cache key for comment like status. - * - * @param int $comment_id The comment ID. - * @param int $user_id The user ID. - * @return string The cache key. - */ - public static function get_cache_key( $comment_id, $user_id ) { - return "comment_like_{$comment_id}_{$user_id}"; - } - /** * Do a comment like API request. * * @param int $blog_id The blog ID. - * @param int $user_id The user ID. * @param int $comment_id The comment ID. * @return bool|WP_Error True if the comment is liked, false otherwise, or a WP_Error if the request fails. */ - private function do_comment_like_api_request( $blog_id, $user_id, $comment_id ) { - $cache_key = self::get_cache_key( $comment_id, $user_id ); - - $cached_result = get_transient( $cache_key ); - - if ( false !== $cached_result ) { - $liked = $cached_result; - } else { - $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( - "/sites/$blog_id/comments/$comment_id/likes", - 'v1.1', - array( 'method' => 'GET' ), - null, - 'rest' - ); - - // If the request fails, simply return the unmodified classes. - if ( is_wp_error( $response ) ) { - return $response; - } - - $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); - - $liked = empty( $response_data['i_like'] ) ? 'no' : 'yes'; + private function do_comment_like_api_request( $blog_id, $comment_id ) { + $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_user( + "/sites/$blog_id/comments/$comment_id/likes", + 'v1.1', + array( 'method' => 'GET' ), + null, + 'rest' + ); - // Cache the result. - set_transient( $cache_key, $liked, self::CACHE_EXPIRATION ); + // If the request fails, simply return the unmodified classes. + if ( is_wp_error( $response ) ) { + return $response; } - return $liked; + $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); + + return ! empty( $response_data['i_like'] ?? false ); } /** @@ -159,17 +132,12 @@ public function add_like_class( $classes, $css_class, $comment_id ) { $liked = Likes::comment_like_current_user_likes( $blog_id, $comment_id ); } else { $blog_id = Jetpack_Options::get_option( 'id' ); - $user_id = get_current_user_id(); - $liked = $this->do_comment_like_api_request( $blog_id, $user_id, $comment_id ); + $liked = $this->do_comment_like_api_request( $blog_id, $comment_id ); if ( is_wp_error( $liked ) ) { return $classes; } - - // We use 'yes' and 'no' when we store the transient to minimize unexpected casting. - // So, we cast to boolean here. - $liked = 'yes' === $liked; } // Append the 'liked' class if the comment is liked.