Skip to content

Instantly share code, notes, and snippets.

@gRegorLove
Last active April 2, 2018 02:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gRegorLove/8215cb9c9584b364aaf4ef2999416f56 to your computer and use it in GitHub Desktop.
Save gRegorLove/8215cb9c9584b364aaf4ef2999416f56 to your computer and use it in GitHub Desktop.
WordPress filter to approve webmentions from previously-approved domains
<?php
if ( !function_exists('indieweb_check_webmention') ) {
/**
* Using the webmention_source_url, approve webmentions that have been received from previously-
* approved domains. For example, once you approve a webmention from http://example.com/post,
* future webmentions from http://example.com will be automatically approved.
* Recommend placing in your theme's functions.php
*
* Based on check_comment()
* @see https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/comment.php#L113
*/
function indieweb_check_webmention($approved, $commentdata) {
global $wpdb;
if ( 1 == get_option('comment_whitelist')) {
if ( !empty($commentdata['comment_meta']['webmention_source_url']) ) {
$like_domain = sprintf('%s://%s%%', parse_url($commentdata['comment_meta']['webmention_source_url'], PHP_URL_SCHEME), parse_url($commentdata['comment_meta']['webmention_source_url'], PHP_URL_HOST));
$ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_url LIKE %s AND comment_approved = '1' LIMIT 1", $commentdata['comment_author'], $like_domain ) );
if ( 1 == $ok_to_comment ) {
return 1;
}
}
}
return $approved;
}
add_filter('pre_comment_approved', 'indieweb_check_webmention', '99', 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment