Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dupe check #386

Merged
merged 6 commits into from
Apr 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 46 additions & 59 deletions includes/class-receiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,7 @@ public static function disable_wp_check_dupes( $dupe_id, $commentdata ) {
return $dupe_id;
}

if (
( isset( $commentdata['comment_type'] ) && 'webmention' === $commentdata['comment_type'] ) ||
( isset( $commentdata['comment_meta'] ) && ! empty( $commentdata['comment_meta']['semantic_linkbacks_type'] ) )
) {
if ( ! empty( $commentdata['comment_meta']['protocol'] ) && 'webmention' === $commentdata['comment_meta']['protocol'] ) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a check that is only supposed to be triggered on a Webmention, but no issue with an existence check

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, sorry. wasn't aware, that empty also checks for isset.

return 0;
}

Expand All @@ -523,77 +520,67 @@ public static function check_dupes( $commentdata ) {
return $commentdata;
}

$fragment = wp_parse_url( $commentdata['target'], PHP_URL_FRAGMENT );
if ( ! empty( $fragment ) ) {
// Check for the newer meta value before checking for the more traditional location
$args = array(
'post_id' => $commentdata['comment_post_ID'],
'meta_query' => array(
array(
'key' => 'webmention_source_url',
'value' => $commentdata['comment_author_url'],
'compare' => '=',
),
array(
'key' => 'webmention_target_fragment',
'value' => $fragment,
'compare' => '=',
),
),
);
} else {
$args = array(
'post_id' => $commentdata['comment_post_ID'],
'meta_key' => 'webmention_source_url',
'meta_value' => $commentdata['comment_author_url'],
);
// This check should never be tripped as all current webmentions should have the source url property.
if ( ! array_key_exists( 'comment_meta', $commentdata ) && ! array_key_exists( 'webmention_source_url', $commentdata['comment_meta'] ) ) {
return $commentdata;
}

$comments = get_comments( $args );
// check result
if ( ! empty( $comments ) ) {
$comment = $comments[0];
$commentdata['comment_ID'] = $comment->comment_ID;
$commentdata['comment_approved'] = $comment->comment_approved;
$fragment = wp_parse_url( $commentdata['target'], PHP_URL_FRAGMENT );
// Meta Query for searching for the URL
$meta_query = array(
'relation' => 'OR',
// This would catch incoming webmentions with the same source URL
array(
'key' => 'webmention_source_url',
'value' => $commentdata['comment_meta']['webmention_source_url'],
'compare' => '=',
),

return $commentdata;
}
// This should catch incoming webmentions with the same canonical URL for Bridgy
array(
'key' => 'url',
'value' => $commentdata['comment_meta']['webmention_source_url'],
'compare' => '=',
),
// check comments sent via salmon are also dupes
// or anyone else who can't use comment_author_url as the original link,
// but can use a _crossposting_link meta value.
// @link https://github.com/pfefferle/wordpress-salmon
array(
'key' => '_crossposting_link',
'value' => $commentdata['comment_meta']['webmention_source_url'],
'compare' => '=',
),

// Check in comment_author_url if the newer location is empty
// This would catch incoming activitypub matches, which uses source_url
array(
'key' => 'source_url',
'value' => $commentdata['comment_meta']['webmention_source_url'],
'compare' => '=',
),
);
$args = array(
'post_id' => $commentdata['comment_post_ID'],
'author_url' => $commentdata['comment_author_url'],
'meta_query' => array( $meta_query ),
);
// If there is a fragment in the target URL then use this in the dupe search

if ( ! empty( $fragment ) ) {
$args['meta_key'] = 'webmention_target_fragment';
$args['meta_value'] = $fragment;
}
$comments = get_comments( $args );
// check result
if ( ! empty( $comments ) ) {
$comment = $comments[0];
$commentdata['comment_ID'] = $comment->comment_ID;
$commentdata['comment_approved'] = $comment->comment_approved;
return $commentdata;
// Ensure that if there is a fragment it is matched
$args['meta_query'][] = array(
'key' => 'webmention_target_fragment',
'value' => $fragment,
'compare' => '=',
);
}

// check comments sent via salmon are also dupes
// or anyone else who can't use comment_author_url as the original link,
// but can use a _crossposting_link meta value.
// @link https://github.com/pfefferle/wordpress-salmon/blob/master/plugin.php#L192
$args = array(
'post_id' => $commentdata['comment_post_ID'],
'meta_key' => '_crossposting_link',
'meta_value' => $commentdata['comment_author_url'],
);
$comments = get_comments( $args );

// check result
if ( ! empty( $comments ) ) {
$comment = $comments[0];
$commentdata['comment_ID'] = $comment->comment_ID;
$commentdata['comment_approved'] = $comment->comment_approved;

return $commentdata;
}

return $commentdata;
Expand Down