Last active
November 2, 2016 09:41
-
-
Save willybahuaud/d618ac821770863961ad1c95e4342154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Filter links in comments | |
*/ | |
add_filter( 'preprocess_comment', 'willy_filter_links_in_comments' ); | |
function willy_filter_links_in_comments( $commentdata ) { | |
// Ne pas filtrer les liens des utilisateurs enregistrés | |
if ( $commentdata['user_id'] ) { | |
return $commentdata; | |
} | |
// Vérifier la date | |
$comment_post = get_post( intval( $commentdata['comment_post_ID'] ) ); | |
$date = ! empty( $commentdata['comment_date_gmt'] ) ? $commentdata['comment_date_gmt'] : current_time( 'mysql', 1 ); | |
$post_age = strtotime( $date ) - strtotime( $comment_post->post_date_gmt ); | |
// Si l’article est vieux, ne pas autoriser les liens | |
if ( 6 * MONTH_IN_SECONDS > $post_age ) { | |
$allowed_links = 0; | |
} else { | |
// Sinon, déduire le nombre de liens autorisés selon le nombre de mots | |
$max = 3; | |
$words_per_link = 50; | |
$words = str_word_count( strip_tags( $commentdata['comment_content'] ) ); | |
$allowed_links = ( $max < $count = floor( $words / $words_per_link ) ) ? $max : $count; | |
} | |
// Supprimer les lien sur `comment_author_url` | |
if ( $allowed_links && ! empty( $commentdata['comment_author_url'] ) ) { | |
$allowed_links--; | |
} else { | |
$commentdata['comment_author_url'] = false; | |
} | |
// La regex qui trouve les liens | |
$re = '/<a[^>]*>([\s\S]*?)<\/a>/mi'; | |
// Supprimer les liens dans `comment_content` | |
if ( preg_match_all( $re, $commentdata['comment_content'], $links ) | |
&& count( $links ) > $allowed_links ) { | |
$commentdata['comment_content'] = preg_replace_callback( $re, | |
function( $link ) use ( &$allowed_links ) { | |
if ( $allowed_links ) { | |
$allowed_links--; | |
return $link[0]; | |
} else { | |
return preg_replace( '/^https?:\/\//', '', $link[1] ); | |
} | |
}, | |
$commentdata['comment_content'] | |
); | |
} | |
return $commentdata; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'preprocess_comment', 'willy_filter_links_in_comments' ); | |
function willy_filter_links_in_comments( $commentdata ) { | |
// bypass registred users | |
if ( $commentdata['user_id'] ) { | |
return $commentdata; | |
} | |
// Déduire le nombre de liens autorisés selon le nombre de mots | |
$max = 3; | |
$words_per_link = 50; | |
$words = str_word_count( strip_tags( $commentdata['comment_content'] ) ); | |
$allowed_links = ( $max < $count = floor( $words / $words_per_link ) ) ? $max : $count; | |
// Supprimer les lien sur `comment_author_url` | |
if ( $allowed_links && ! empty( $commentdata['comment_author_url'] ) ) { | |
$allowed_links--; | |
} else { | |
$commentdata['comment_author_url'] = false; | |
} | |
// La regex qui trouve les liens | |
$re = '/<a[^>]*>([\s\S]*?)<\/a>/mi'; | |
// Supprimer les liens dans `comment_content` | |
if ( preg_match_all( $re, $commentdata['comment_content'], $links ) | |
&& count( $links ) > $allowed_links ) { | |
$commentdata['comment_content'] = preg_replace_callback( $re, | |
function( $link ) use ( &$allowed_links ) { | |
if ( $allowed_links ) { | |
$allowed_links--; | |
return $link[0]; | |
} else { | |
return preg_replace( '/^https?:\/\//', '', $link[1] ); | |
} | |
}, | |
$commentdata['comment_content'] | |
); | |
} | |
return $commentdata; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment