diff --git a/includes/client/comments.php b/includes/client/comments.php index 3d7d78b..8bf6558 100644 --- a/includes/client/comments.php +++ b/includes/client/comments.php @@ -5,6 +5,7 @@ require_once plugin_dir_path( __FILE__ ) . '../server/activities/create.php'; require_once plugin_dir_path( __FILE__ ) . '../server/activities/update.php'; require_once plugin_dir_path( __FILE__ ) . '../server/activities/delete.php'; 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 ) { @@ -21,10 +22,7 @@ function handle_edit_comment( $comment_id ) { } function handle_transition_comment_status( $new_status, $old_status, $comment ) { - global $wpdb; - $existing = $wpdb->get_row( $wpdb->prepare( - "SELECT * FROM {$wpdb->prefix}pterotype_comments WHERE comment_id = %d", $comment->comment_ID - ) ); + $existing = \pterotype\commentlinks\get_object_id( $comment->comment_ID ); if ( $existing ) { return; } @@ -98,7 +96,6 @@ function get_comment_email_actor_slug( $comment ) { } function comment_to_object( $comment, $actor_slug ) { - global $wpdb; $post = \get_post( $comment->comment_post_ID ); \setup_postdata( $post ); $post_permalink = \get_permalink( $post ); @@ -106,15 +103,9 @@ function comment_to_object( $comment, $actor_slug ) { $inReplyTo = $post_object['id']; if ( $comment->comment_parent !== '0' ) { $parent_comment = \get_comment( $comment->comment_parent ); - $parent_object_activitypub_id = $wpdb->get_var( $wpdb->prepare( - " - SELECT activitypub_id FROM {$wpdb->prefix}pterotype_comments - JOIN {$wpdb->prefix}pterotype_objects - ON {$wpdb->prefix}pterotype_comments.object_id = {$wpdb->prefix}pterotype_objects.id - WHERE comment_id = %d - ", + $parent_object_activitypub_id = \pterotype\commentlinks\get_object_activitypub_id( $parent_comment->comment_ID - ) ); + ); if ( $parent_object_activitypub_id ) { $inReplyTo = $parent_object_activitypub_id; } @@ -130,16 +121,9 @@ function comment_to_object( $comment, $actor_slug ) { 'url' => $link, 'inReplyTo' => $inReplyTo, ); - $existing_activitypub_id = $parent_object_activitypub_id = $wpdb->get_var( $wpdb->prepare( - " - SELECT activitypub_id FROM {$wpdb->prefix}pterotype_comments - JOIN {$wpdb->prefix}pterotype_objects - ON {$wpdb->prefix}pterotype_comments.object_id = {$wpdb->prefix}pterotype_objects.id - WHERE comment_id = %d - ", - $comment->comment_ID - ) ); - + $existing_activitypub_id = \pterotype\commentlinks\get_object_activitypub_id( + $comment->comment_ID + ); if ( $existing_activitypub_id ) { $object['id'] = $existing_activitypub_id; } diff --git a/includes/commentlinks.php b/includes/commentlinks.php new file mode 100644 index 0000000..13d0721 --- /dev/null +++ b/includes/commentlinks.php @@ -0,0 +1,41 @@ +get_var( $wpdb->prepare( + "SELECT object_id FROM {$wpdb->prefix}pterotype_comments WHERE comment_id = %d", + $comment_id; + ) ); +} + +function get_comment_id( $object_id ) { + global $wpdb; + return $wpdb->get_var( $wpdb->prepare( + "SELECT comment_id FROM {$wpdb->prefix}pterotype_comments WHERE object_id = %d", + $object_id; + ) ); +} + +function get_object_activitypub_id( $comment_id ) { + global $wpdb; + return $wpdb->get_var( $wpdb->prepare( + " + SELECT activitypub_id FROM {$wpdb->prefix}pterotype_comments + JOIN {$wpdb->prefix}pterotype_objects + ON {$wpdb->prefix}pterotype_comments.object_id = {$wpdb->prefix}pterotype_objects.id + WHERE comment_id = %d + ", + $comment_id + ) ); +} + +function link_comment( $comment_id, $object_id ) { + global $wpdb; + return $wpdb->insert( + "{$wpdb->prefix}pterotype_comments", + array( 'comment_id' => $comment_id, 'object_id' => $object_id ), + '%d' + ); +} +?> diff --git a/includes/server/activities/create.php b/includes/server/activities/create.php index 94ba5c8..4067eb9 100644 --- a/includes/server/activities/create.php +++ b/includes/server/activities/create.php @@ -4,6 +4,7 @@ namespace pterotype\activities\create; require_once plugin_dir_path( __FILE__ ) . '../objects.php'; require_once plugin_dir_path( __FILE__ ) . '../actors.php'; require_once plugin_dir_path( __FILE__ ) . '../../util.php'; +require_once plugin_dir_path( __FILE__ ) . '../../commentlinks.php'; /* Create a new post or comment (depending on $activity["object"]["type"]), @@ -108,7 +109,6 @@ function make_create( $actor_slug, $object ) { } function link_comment( $object ) { - global $wpdb; $object = \pterotype\util\dereference_object( $object ); if ( ! array_key_exists( 'url', $object ) ) { return; @@ -121,21 +121,13 @@ function link_comment( $object ) { return; } $object_id = \pterotype\objects\get_object_id( $object['id'] ); - $wpdb->insert( - "{$wpdb->prefix}pterotype_comments", - array( 'comment_id' => $comment_id, 'object_id' => $object_id ), - '%d' - ); + \pterotype\commentlinks\link_comment( $comment_id, $object_id ); } function sync_comments( $activity ) { - global $wpdb; $object = $activity['object']; $object_id = \pterotype\objects\get_object_id( $object['id'] ); - $comment_exists = $wpdb->get_row( $wpdb->prepare( - "SELECT * FROM {$wpdb->prefix}pterotype_comments WHERE object_id = %d", - $object_id - ) ); + $comment_exists = \pterotype\commentlinks\get_comment_id( $object_id ); if ( $comment_exists ) { return; } @@ -151,10 +143,7 @@ function sync_comments( $activity ) { if ( ! $parent_row || is_wp_error( $parent_row ) ) { return; } - $parent_comment_id = $wpdb->get_var( $wpdb->prepare( - "SELECT comment_id FROM {$wpdb->prefix}pterotype_comments WHERE object_id = %d", - $parent_row->id - ) ); + $parent_comment_id = \pterotype\commentlinks\get_comment_id( $parent_row->id ); if ( $parent_comment_id ) { $parent_comment = \get_comment( $parent_comment_id ); if ( ! $parent_comment ) { @@ -181,12 +170,7 @@ function sync_comments( $activity ) { } function link_new_comment( $comment_id, $object_id ) { - global $wpdb; - return $wpdb->insert( - "{$wpdb->prefix}pterotype_comments", - array( 'comment_id' => $comment_id, 'object_id' => $object_id ), - '%d' - ); + return \pterotype\commentlinks\link_comment( $comment_id, $object_id ); } function get_comment_id_from_url( $url ) {