From 725fc1e029f3111cefb908252f9f4b360d1d77c6 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 31 Oct 2018 07:56:22 -0400 Subject: [PATCH] Fix duplicate comments --- includes/client/comments.php | 5 +---- includes/server/async.php | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/includes/client/comments.php b/includes/client/comments.php index e33f230..f74d9f4 100644 --- a/includes/client/comments.php +++ b/includes/client/comments.php @@ -8,10 +8,7 @@ require_once plugin_dir_path( __FILE__ ) . '../server/objects.php'; require_once plugin_dir_path( __FILE__ ) . '../commentlinks.php'; function handle_comment_post( $comment_id, $comment_approved ) { - if ( $comment_approved ) { - $comment = \get_comment( $comment_id ); - handle_transition_comment_status( 'approved', 'nonexistent', $comment ); - } + do_action( 'pterotype_handle_comment_post', $comment_id, $comment_approved ); } function handle_edit_comment( $comment_id ) { diff --git a/includes/server/async.php b/includes/server/async.php index 51d8bb3..3b047f7 100644 --- a/includes/server/async.php +++ b/includes/server/async.php @@ -2,6 +2,7 @@ namespace pterotype\async; require_once plugin_dir_path( __FILE__ ) . 'outbox.php'; +require_once plugin_dir_path( __FILE__ ) . '../client/comments.php'; class Send_Accept_Task extends \WP_Async_Task { protected $action = 'pterotype_send_accept'; @@ -16,13 +17,38 @@ class Send_Accept_Task extends \WP_Async_Task { $actor_slug = $_POST['actor_slug']; $accept = $_POST['accept']; if ( $actor_slug && $accept ) { - sleep( 5 ); + sleep( 2 ); \pterotype\outbox\handle_activity( $actor_slug, $accept ); } } } +class Handle_Comment_Post_Task extends \WP_Async_Task { + protected $action = 'pterotype_handle_comment_post'; + + protected function prepare_data( $data ) { + $comment_id = $data[0]; + $comment_approved = $data[1]; + return array( 'comment_id' => $comment_id, 'comment_approved' => $comment_approved ); + } + + protected function run_action() { + $comment_id = $_POST['comment_id']; + $comment_approved = $_POST['comment_approved']; + if ( $comment_approved ) { + // There's potentially a race between this task and linking the comment + // in activities\create. It should be okay since getting the comment takes + // some time, but something to keep in mind + $comment = \get_comment( $comment_id ); + \pterotype\comments\handle_transition_comment_status( + 'approved', 'nonexistent', $comment + ); + } + } +} + function init_tasks() { new Send_Accept_Task(); + new Handle_Comment_Post_Task(); } ?>