Last active
August 29, 2015 14:07
-
-
Save jdembowski/f9dcfed5a2305255d727 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 | |
/* | |
Plugin Name: Shortlinks? BE GONE! | |
Description: This plugin modifies links in comments as they are submitted, searches for URLs and then expands them if necessary. | |
Author: Jan Dembowski | |
Author URI: https://blog.dembowski.net/ | |
Version: 0.6 | |
*/ | |
// Copied from | |
// http://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment#Examples | |
// I want this filter to run a early and I want to modify the comment data | |
// before it goes into the database. | |
add_filter( 'preprocess_comment' , 'mh_preprocess_comment_remove_shortlink' , 5 ); | |
function mh_preprocess_comment_remove_shortlink( $commentdata ) { | |
// Start with the author URL and expand that. | |
$commentdata['comment_author_url'] = mh_no_shortlink( $commentdata['comment_author_url'] ); | |
// If after expanding it's a dead link then just delete it. | |
if ( $commentdata['comment_author_url'] == '#' ) $commentdata['comment_author_url'] = ''; | |
// Extract the URLs from the comment content and expand them if necessary. | |
$mh_urls = wp_extract_urls( $commentdata['comment_content'] ); | |
for ( $mh_count = 0; $mh_count < count( $mh_urls ); $mh_count++ ) | |
{ | |
// Get the expanded link | |
$mh_new_url = mh_no_shortlink( $mh_urls[ $mh_count ] ); | |
// Replace the current link with the expanded one if necessary. | |
$commentdata['comment_content'] = str_replace( $mh_urls[ $mh_count ] , $mh_new_url , $commentdata['comment_content'] ); | |
} | |
return $commentdata; | |
} | |
// This is the function that un-shortens URLs. You feed it a URL and it gives | |
// you back a URL. | |
function mh_no_shortlink ( $url , $count = 0 ) { | |
// I'm using hash to generate transient names that are 40 | |
// characters long. I don't care about md5, I just want | |
// the names to be reproducable. | |
$mh_transient_name = 'mh_shurl' . hash('md5' , esc_url_raw( $url ) ); | |
// Have we done this URL already? | |
// Get the transient and return it if one exists | |
$mh_stored_url = get_transient( $mh_transient_name ); | |
if ( $mh_stored_url ) { | |
return $mh_stored_url; | |
} | |
// After 5 recursive tries we're done. Screw that link, return nada. | |
// In this case nada means a hashmark. | |
if ( $count >= 4 ) { | |
return '#'; | |
} | |
// I do not want WordPress to follow http redirects, | |
// I want the result from the first try. | |
$mh_get_args = array( | |
'redirection' => 0, | |
); | |
// Just get the HTTP head, don't actually get the whole web page. | |
$mh_response = wp_remote_head( esc_url_raw( $url ) , $mh_get_args ); | |
// I was thinking of checking the http status for 200 but the head | |
// request will happen anyway so why bother? | |
// | |
// $mh_response_code = wp_remote_retrieve_response_code( $mh_response ); | |
// Look for the http header for 'location' as that indicates a redirect. | |
$mh_unshortened = wp_remote_retrieve_header( $mh_response, 'location' ); | |
if ( $mh_unshortened ) { | |
// There was a redirect? Then let's increment count and check that URL too. | |
return mh_no_shortlink( $mh_unshortened , $count + 1 ); | |
} else { | |
// If no redirect so just return the URL we were given. | |
// Store the transient for 12 hours, return the real URL and we're finished. | |
if ( $count == 0 ) { | |
set_transient( $mh_transient_name , $url , 12 * HOUR_IN_SECONDS ); | |
} | |
return esc_url_raw( $url ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment