PluginProbe
HTTP 410 (Gone) responses / 0.2
HTTP 410 (Gone) responses v0.2
1.2.1 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.6.1 0.7 0.7.1 0.7.2 0.8.1 0.8.2 1.0.2 1.0.3 1.1.0
wp-410 / wp-410.php

wp-410.php in HTTP 410 (Gone) responses 0.2, at wp-410.php

197 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.2
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 const version_opt = 'wp_410_options_version';
19 const db_version = 1; // DB version, since 0.2
20 private $permalinks;
21
22 function __construct() {
23 $struct = get_option('permalink_structure');
24 $this->permalinks = !empty( $struct );
25 load_plugin_textdomain( self::dom, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
26
27 if( get_option( self::version_opt, 0 ) < self::db_version )
28 add_action( 'init', array( &$this, 'upgrade' ) );
29
30 if( is_admin() )
31 add_action( 'admin_menu', array( &$this, 'settings_menu' ) );
32
33 add_action('delete_post', array( &$this, 'note_deleted_post' ) );
34 add_action('wp_insert_post', array( &$this, 'note_inserted_post' ) );
35 add_action('template_redirect', array( &$this, 'check_for_410' ) );
36 }
37
38 function upgrade() {
39 // upgrade from 0.1 to 0.2
40 $old_links = get_option( self::link_opt, array() );
41 $new_links = array();
42 foreach( $old_links as $link )
43 $new_links[$link] = $this->build_regex( $link ); // new links have key = display link and value = regex
44
45 update_option( self::link_opt, $new_links );
46 update_option( self::version_opt, self::db_version );
47 }
48
49 function settings_menu() {
50 add_submenu_page('plugins.php', __('410 for Wordpress', self::dom), __('410 for Wordpress', self::dom), 'manage_options', 'wp_410_settings', array(&$this, 'settings_page') );
51 }
52
53 function settings_page() {
54 $old_links = get_option( self::link_opt, array() );
55
56 if ( isset( $_POST['submit'] ) ) {
57 $failed_to_add = array();
58 if( !empty( $_POST['old_links_to_add'] ) ) {
59 $added_links = preg_split( '/(\r?\n)+/', $_POST['old_links_to_add'], -1, PREG_SPLIT_NO_EMPTY );
60
61 foreach( $added_links as $k => $link ) {
62 $parts = parse_url( $link );
63 if( !$parts || empty( $parts['scheme'] ) || empty( $parts['host'] ) ) // invalid URL
64 $failed_to_add[] = '<code>' . htmlspecialchars( $link ) . '</code>';
65 else
66 $old_links[$link] = $this->build_regex( $link ); // key is display link, value is regex
67 }
68 }
69
70 if( !empty( $_POST['old_links_to_remove'] ) )
71 foreach( $_POST['old_links_to_remove'] as $key )
72 unset( $old_links[$key] );
73
74 ksort( $old_links );
75
76 update_option( self::link_opt, $old_links );
77 echo '<div class="updated fade"><p>'.__('Options updated.', self::dom).'</p></div>';
78
79 if( $failed_to_add )
80 echo '<div class="error"><p>'. __('The following entries could not be recognised as valid URLs, and were not added to the list.', self::dom). '<br />- ' . implode( '<br />- ', $failed_to_add ) .'</p></div>';
81 }
82 ?>
83 <style>
84 .indent {padding-left: 2em}
85 .wp_gone_deleted label {text-decoration: line-through}
86 </style>
87 <div class="wrap">
88 <h2><?php _e('410 for Wordpress', self::dom);?></h2>
89 <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 this and issues a 410 response when pages unique to that article are 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>
90 <p><?php _e('Note: this plugin will only return a 410 response if Wordpress has not found something valid to display for the requested URL.', self::dom);?></p>
91 <form action="" method="post" id="wp-gone-captcha-settings">
92 <h3><?php _e('Obsolete URLs', self::dom);?></h3>
93 <?php
94 if( empty( $old_links ) ) {
95 echo '<p>' . __( 'There are currently no obsolete URLs in this list. You can add some manually below.', self::dom ) . '</p>';
96 }
97 else {
98 echo '<p>' . __( 'The following URLs are currently considered obsolete. To remove items from the list, check the corresponding box.', self::dom ) .'</p>';
99 echo '<ul id="wp_gone_old_links" class="indent">';
100 foreach( array_keys( $old_links ) as $k )
101 echo "<li><input type='checkbox' name='old_links_to_remove[]' value='" . htmlspecialchars( $k ) . "' id='old_link_$k' /> <label for='old_link_$k'><code>" . htmlspecialchars( $k ). "</code></label></li>";
102 echo '</ul>';
103 }
104 ?>
105 <p><small><?php _e('If you create or update an article whose URL matches one of those above, that URL will automatically be removed from this list.', self::dom);?></small></p>
106 <h3><?php _e('Manually add URLs', self::dom);?></h3>
107 <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.', self::dom);?></p>
108 <p><?php printf( __( 'Use %s as a wildcard character. So %s will match all URLs ending in %s.'), '<code>*</code>', '<code>http://www.example.com/*/music/</code>', '<code>/music/</code>' );?></p>
109 <textarea name="old_links_to_add" rows="8" cols="80"></textarea>
110 <p class="submit"><input class="button-primary" type="submit" name="submit" value="<?php _e('Update settings', self::dom);?>" /></p>
111 </form>
112 </div>
113 <script>
114 jQuery(document).ready(function(){
115 jQuery('#wp_gone_old_links input').change( function(){
116 this.parentNode.className = jQuery(this).is(":checked") ? 'wp_gone_deleted' : '';
117 });
118 });
119 </script>
120 <?php
121 }
122
123 function note_deleted_post( $id ) {
124 // NOTE: function wp_delete_post calls the delete_post action twice
125 static $delete_noted = false;
126
127 if( $delete_noted )
128 return;
129 $delete_noted = true;
130
131 $post = get_post( $id );
132
133 if( 'revision' == $post->post_type )
134 return;
135
136 $link = get_permalink( $id );
137 if( $this->permalinks )
138 $link .= '*'; // catch everything like /page/ and /feed/
139
140 $list = get_option( self::link_opt, array() );
141
142 $list[$link] = $this->build_regex( $link );
143
144 update_option( self::link_opt, $list );
145 }
146
147 function note_inserted_post( $id ) {
148 $post = get_post( $id );
149
150 if( 'revision' == $post->post_type || 'draft' == $post->post_status )
151 return;
152
153 // Check our list of URLs against the new/updated post's permalink, and if they match, scratch it from our list
154 $links = array();
155
156 $links[] = get_permalink( $id );
157 $links[] = get_post_comments_feed_link( $id ); // back compat
158
159 if( $this->permalinks )
160 $links[0] .= '*';
161
162 $list = get_option( self::link_opt, array() );
163
164 foreach( $links as $link )
165 unset( $list[$link] );
166
167 update_option( self::link_opt, $list );
168 }
169
170 function check_for_410() {
171 // Don't mess if Wordpress has found something to display
172 if( !is_404() )
173 return;
174
175 $links = get_option( self::link_opt, array() );
176 $req = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
177
178 foreach( $links as $regex ) {
179 if( @preg_match( $regex, $req ) ) {
180 status_header( 410 );
181 _e( 'Sorry, the page you requested has been permanently removed.', self::dom );
182 exit;
183 }
184 }
185 }
186
187 private function build_regex( $link ) {
188 $parts = preg_split( '/(\*)/', $link, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
189 foreach( $parts as &$part ) if( '*' != $part )
190 $part = preg_quote( $part, '|' );
191 $parts = str_replace( '*', '.*', $parts );
192 return '|^' . implode( '', $parts ) . '$|';
193 }
194 }
195
196 new WP_410();
197 ?>