Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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