| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: 410 for Wordpress |
| 4 |
Plugin URI: http://rayofsolaris.net/code/410-for-wordpress |
| 5 |
Description: Sends HTTP 410 (Gone) responses to requests for pages that no longer exist on your blog. |
| 6 |
Version: 0.1 |
| 7 |
Author: Samir Shah |
| 8 |
Author URI: http://rayofsolaris.net/ |
| 9 |
License: GPL2 |
| 10 |
Text Domain: wp-410 |
| 11 |
*/ |
| 12 |
|
| 13 |
if( !defined('ABSPATH') ) exit; |
| 14 |
|
| 15 |
class WP_410 { |
| 16 |
const dom = 'wp-410'; |
| 17 |
const link_opt = 'wp_410_links_list'; |
| 18 |
|
| 19 |
function __construct() { |
| 20 |
load_plugin_textdomain( self::dom, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
| 21 |
|
| 22 |
if( is_admin() ) { |
| 23 |
add_action( 'admin_menu', array( &$this, 'settings_menu' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
add_action('delete_post', array( &$this, 'note_deleted_post' ) ); |
| 27 |
add_action('wp_insert_post', array( &$this, 'note_inserted_post' ) ); |
| 28 |
add_action('template_redirect', array( &$this, 'check_for_410' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
function settings_menu() { |
| 32 |
add_submenu_page('plugins.php', __('410 for Wordpress', self::dom), __('410 for Wordpress', self::dom), 'manage_options', 'wp_410_settings', array(&$this, 'settings_page') ); |
| 33 |
} |
| 34 |
|
| 35 |
function settings_page() { |
| 36 |
// global $wp_version; |
| 37 |
$old_links = get_option( self::link_opt, array() ); |
| 38 |
|
| 39 |
if ( isset( $_POST['submit'] ) ) { |
| 40 |
if( !empty( $_POST['old_links_to_add'] ) ){ |
| 41 |
$added_links = preg_split( '/(\r?\n)+/', $_POST['old_links_to_add'], -1, PREG_SPLIT_NO_EMPTY ); |
| 42 |
|
| 43 |
foreach( $added_links as $addlink ) |
| 44 |
$old_links[] = esc_url_raw( $addlink, array( 'http', 'https' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
if( !empty( $_POST['old_links_to_remove'] ) ) |
| 48 |
$old_links = array_diff( $old_links, $_POST['old_links_to_remove'] ); |
| 49 |
|
| 50 |
$old_links = array_unique( $old_links ); |
| 51 |
sort( $old_links ); |
| 52 |
|
| 53 |
update_option( self::link_opt, $old_links ); |
| 54 |
echo '<div id="message" class="updated fade"><p>'.__('Options updated.', self::dom).'</p></div>'; |
| 55 |
} |
| 56 |
?> |
| 57 |
<style> |
| 58 |
.indent {padding-left: 2em} |
| 59 |
.wp_gone_deleted label {text-decoration: line-through} |
| 60 |
</style> |
| 61 |
<div class="wrap"> |
| 62 |
<h2><?php _e('410 for Wordpress', self::dom);?></h2> |
| 63 |
<p><?php _e('This plugin will issue a HTTP 410 response to articles that no longer exist on your blog. When an article is deleted, it records the URL for that page and issues a 410 response when that URL is requested. This HTTP code informs robots visiting your site that the requested page has been permanently removed, and that they should stop trying to access it.', self::dom);?></p> |
| 64 |
<p><?php _e('Note: the plugin will only return a 410 response if Wordpress has not found something valid to display for the requested URL.', self::dom);?></p> |
| 65 |
<form action="" method="post" id="wp-gone-captcha-settings"> |
| 66 |
<h3><?php _e('Obsolete URLs', self::dom);?></h3> |
| 67 |
<?php |
| 68 |
if( empty( $old_links ) ) { |
| 69 |
echo '<p>' . __( 'There are currently no obsolete URLs in this list. You can add some manually below.', self::dom ) . '</p>'; |
| 70 |
} |
| 71 |
else { |
| 72 |
echo '<p>' . __( 'The following URLs are currently considered obsolete. To remove items from the list, check the corresponding box.', self::dom ) .'</p>'; |
| 73 |
echo '<ul id="wp_gone_old_links" class="indent">'; |
| 74 |
foreach( $old_links as $k => $link ) |
| 75 |
echo "<li><input type='checkbox' name='old_links_to_remove[]' value='" . htmlspecialchars( $link ) . "' id='old_link_$k' /> <label for='old_link_$k'><code>" . htmlspecialchars( $link ). "</code></label></li>"; |
| 76 |
echo '</ul>'; |
| 77 |
} |
| 78 |
?> |
| 79 |
<p><small><?php _e('If you create or update an article whose URL matches one of those below, that URL will automatically be removed from this list.', self::dom);?></small></p> |
| 80 |
<h3><?php _e('Manually add URLs', self::dom);?></h3> |
| 81 |
<p><?php _e('You can manually add items to the list by entering them below. Please enter one <strong>fully qualified</strong> URL per line.');?></p> |
| 82 |
<textarea name="old_links_to_add" rows="8" cols="80"></textarea> |
| 83 |
<p class="submit"><input class="button-primary" type="submit" name="submit" value="<?php _e('Update settings', self::dom);?>" /></p> |
| 84 |
</form> |
| 85 |
</div> |
| 86 |
<script> |
| 87 |
jQuery(document).ready(function(){ |
| 88 |
jQuery('#wp_gone_old_links input').change( function(){ |
| 89 |
this.parentNode.className = jQuery(this).is(":checked") ? 'wp_gone_deleted' : ''; |
| 90 |
}); |
| 91 |
}); |
| 92 |
</script> |
| 93 |
<?php |
| 94 |
} |
| 95 |
|
| 96 |
function note_deleted_post( $id ) { |
| 97 |
// NOTE: function wp_delete_post calls the delete_post action twice |
| 98 |
static $delete_noted = false; |
| 99 |
|
| 100 |
if( $delete_noted ) |
| 101 |
return; |
| 102 |
|
| 103 |
$delete_noted = true; |
| 104 |
|
| 105 |
$links_to_410 = array( |
| 106 |
get_permalink( $id ), |
| 107 |
get_post_comments_feed_link ( $id ) |
| 108 |
); |
| 109 |
|
| 110 |
$list = get_option( self::link_opt, array() ); |
| 111 |
|
| 112 |
foreach( $links_to_410 as $link ) if( !in_array( $link, $list ) ) |
| 113 |
$list[] = $link; |
| 114 |
|
| 115 |
update_option( self::link_opt, $list ); |
| 116 |
} |
| 117 |
|
| 118 |
function note_inserted_post( $id ) { |
| 119 |
// Check our list of URLs against the new/updated post's permalink, and if they match, scratch it from our list |
| 120 |
$links_not_to_410 = array( |
| 121 |
get_permalink( $id ), |
| 122 |
get_post_comments_feed_link ( $id ) |
| 123 |
); |
| 124 |
|
| 125 |
$list = get_option( self::link_opt, array() ); |
| 126 |
foreach( $links_not_to_410 as $link ) if( $key = array_search( $link, $list ) ) |
| 127 |
unset( $list[$key] ); |
| 128 |
|
| 129 |
update_option( self::link_opt, $list ); |
| 130 |
} |
| 131 |
|
| 132 |
function check_for_410() { |
| 133 |
// Don't mess if Wordpress has found something to display |
| 134 |
if( !is_404() ) |
| 135 |
return; |
| 136 |
|
| 137 |
$links = get_option( self::link_opt, array() ); |
| 138 |
$req = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 139 |
|
| 140 |
if( in_array( $req, $links ) ) { |
| 141 |
status_header( 410 ); |
| 142 |
_e( 'Sorry, the page you requested has been permanently removed.', self::dom ); |
| 143 |
exit; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
new WP_410(); |
| 149 |
?> |
| 150 |
|