From 42529f45167b00029038619b1681ac053f50e134 Mon Sep 17 00:00:00 2001 From: markjaquith Date: Thu, 30 Nov 2006 08:48:56 +0000 Subject: [PATCH] Remember old post slugs and automatically redirect to the new slug. fixes #3202 git-svn-id: http://svn.automattic.com/wordpress/trunk@4556 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-functions.php | 37 +++++++++++++++++++++++++++++++++ wp-admin/upgrade-functions.php | 10 +++++++++ wp-includes/default-filters.php | 5 +++++ wp-includes/query.php | 34 ++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 5 files changed, 87 insertions(+), 1 deletion(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 0181b1f59c..64e758a75f 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -2069,6 +2069,43 @@ function wp_reset_vars( $vars ) { } } + +function wp_check_for_changed_slugs($post_id) { + if ( !strlen($_POST['wp-old-slug']) ) + return $post_id; + + $post = &get_post($post_id); + + // we're only concerned with published posts + if ( $post->post_status != 'publish' || $post->post_type != 'post' ) + return $post_id; + + // only bother if the slug has changed + if ( $post->post_name == $_POST['wp-old-slug'] ) + return $post_id; + + $old_slugs = get_post_meta($post_id, '_wp_old_slug'); + + // if we haven't added this old slug before, add it now + if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) ) + add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']); + + // if the new slug was used previously, delete it from the list + if ( in_array($post->post_name, $old_slugs) ) + delete_post_meta($post_id, '_wp_old_slug', $post->post_name); + + return $post_id; +} + + +function wp_remember_old_slug() { + global $post; + $name = wp_specialchars($post->post_name); // just in case + if ( strlen($name) ) + echo ''; +} + + // If siteurl or home changed, reset cookies and flush rewrite rules. function update_home_siteurl( $old_value, $value ) { global $wp_rewrite, $user_login, $user_pass_md5; diff --git a/wp-admin/upgrade-functions.php b/wp-admin/upgrade-functions.php index 29f8200405..ac1a130db4 100644 --- a/wp-admin/upgrade-functions.php +++ b/wp-admin/upgrade-functions.php @@ -175,6 +175,9 @@ function upgrade_all() { if ( $wp_current_db_version < 3845 ) upgrade_210(); + if ( $wp_current_db_version < 4351 ) + upgrade_old_slugs(); + $wp_rewrite->flush_rules(); update_option('db_version', $wp_db_version); @@ -544,6 +547,13 @@ function upgrade_210() { } } +function upgrade_old_slugs() { + // upgrade people who were using the Redirect Old Slugs plugin + global $wpdb; + $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'"); +} + + // The functions we use to actually do stuff // General diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index ad60242edc..bde7baddb5 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -146,6 +146,11 @@ add_filter('option_blog_charset', 'wp_specialchars'); add_filter('mce_plugins', '_mce_load_rtl_plugin'); add_filter('mce_buttons', '_mce_add_direction_buttons'); +// Redirect Old Slugs +add_action('template_redirect', 'wp_old_slug_redirect'); +add_action('edit_post', 'wp_check_for_changed_slugs'); +add_action('edit_form_advanced', 'wp_remember_old_slug'); + // Actions add_action('wp_head', 'rsd_link'); add_action('wp_head', 'locale_stylesheet'); diff --git a/wp-includes/query.php b/wp-includes/query.php index aaab66f1c1..3f09839b34 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -1116,6 +1116,40 @@ class WP_Query { } } + +// Redirect old slugs +function wp_old_slug_redirect () { + global $wp_query; + if ( is_404() && '' != $wp_query->query_vars['name'] ) : + global $wpdb; + + $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND meta_key = '_wp_old_slug' AND meta_value='" . $wp_query->query_vars['name'] . "'"; + + // if year, monthnum, or day have been specified, make our query more precise + // just in case there are multiple identical _wp_old_slug values + if ( '' != $wp_query->query_vars['year'] ) + $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'"; + if ( '' != $wp_query->query_vars['monthnum'] ) + $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'"; + if ( '' != $wp_query->query_vars['day'] ) + $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'"; + + $id = (int) $wpdb->get_var($query); + + if ( !$id ) + return; + + $link = get_permalink($id); + + if ( !$link ) + return; + + wp_redirect($link, '301'); // Permanent redirect + exit; + endif; +} + + // // Private helper functions // diff --git a/wp-includes/version.php b/wp-includes/version.php index 4d8745c7de..ef40aac385 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -3,6 +3,6 @@ // This holds the version number in a separate file so we can bump it without cluttering the SVN $wp_version = '2.1-alpha3'; -$wp_db_version = 4350; +$wp_db_version = 4351; ?>