WordPress.org

Make WordPress Core

Ticket #3202: redirect-old-slugs.diff

File redirect-old-slugs.diff, 4.7 KB (added by markjaquith, 12 years ago)

Patch for /trunk/

  • wp-includes/default-filters.php

     
    146146add_filter('mce_plugins', '_mce_load_rtl_plugin');
    147147add_filter('mce_buttons', '_mce_add_direction_buttons');
    148148
     149// Redirect Old Slugs
     150add_action('template_redirect', 'wp_old_slug_redirect');
     151add_action('edit_post', 'wp_check_for_changed_slugs');
     152add_action('edit_form_advanced', 'wp_remember_old_slug');
     153
    149154// Actions
    150155add_action('wp_head', 'rsd_link');
    151156add_action('wp_head', 'locale_stylesheet');
  • wp-includes/query.php

     
    11241124        }
    11251125}
    11261126
     1127
     1128
     1129// Redirect old slugs
     1130function wp_old_slug_redirect () {
     1131        global $wp_query;
     1132        if ( is_404() && '' != $wp_query->query_vars['name'] ) :
     1133                global $wpdb;
     1134
     1135                $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'] . "'";
     1136
     1137                // if year, monthnum, or day have been specified, make our query more precise
     1138                // just in case there are multiple identical _wp_old_slug values
     1139                if ( '' != $wp_query->query_vars['year'] )
     1140                        $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
     1141                if ( '' != $wp_query->query_vars['monthnum'] )
     1142                        $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
     1143                if ( '' != $wp_query->query_vars['day'] )
     1144                        $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
     1145
     1146                $id = $wpdb->get_var($query);
     1147
     1148                if ( !$id )
     1149                        return;
     1150
     1151                $link = get_permalink($id);
     1152
     1153                if ( !$link )
     1154                        return;
     1155
     1156                wp_redirect($link, '301'); // Permanent redirect
     1157                exit;
     1158        endif;
     1159}
     1160
     1161
    11271162//
    11281163// Private helper functions
    11291164//
  • wp-includes/version.php

     
    33// This holds the version number in a separate file so we can bump it without cluttering the SVN
    44
    55$wp_version = '2.1-alpha3';
    6 $wp_db_version = 3846;
     6$wp_db_version = 3847;
    77
    88?>
     9 No newline at end of file
  • wp-admin/upgrade-functions.php

     
    175175        if ( $wp_current_db_version < 3845 )
    176176                upgrade_210();
    177177
     178        if ( $wp_current_db_version < 3847 )
     179                upgrade_old_slugs();
     180
    178181        $wp_rewrite->flush_rules();
    179182
    180183        update_option('db_version', $wp_db_version);
     
    544547        }
    545548}
    546549
     550function upgrade_old_slugs() {
     551        // upgrade people who were using the Redirect Old Slugs plugin
     552        global $wpdb;
     553        $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
     554}
     555
    547556// The functions we use to actually do stuff
    548557
    549558// General
  • wp-admin/admin-functions.php

     
    20232023        }
    20242024}
    20252025
     2026
     2027function wp_check_for_changed_slugs($post_id) {
     2028        if ( !strlen($_POST['wp-old-slug']) )
     2029                return $post_id;
     2030
     2031        $post = &get_post($post_id);
     2032
     2033        // we're only concerned with published posts
     2034        if ( $post->post_status != 'publish' || $post->post_type != 'post' )
     2035                return $post_id;
     2036
     2037        // only bother if the slug has changed
     2038        if ( $post->post_name == $_POST['wp-old-slug'] )
     2039                return $post_id;
     2040
     2041        $old_slugs = get_post_meta($post_id, '_wp_old_slug');
     2042
     2043        // if we haven't added this old slug before, add it now
     2044        if ( !count($old_slugs) || !in_array($_POST['wp-old-slug'], $old_slugs) )
     2045                add_post_meta($post_id, '_wp_old_slug', $_POST['wp-old-slug']);
     2046
     2047        // if the new slug was used previously, delete it from the list
     2048        if ( in_array($post->post_name, $old_slugs) )
     2049                delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
     2050
     2051        return $post_id;
     2052}
     2053
     2054
     2055function wp_remember_old_slug() {
     2056        global $post;
     2057        $name = wp_specialchars($post->post_name); // just in case
     2058        if ( strlen($name) )
     2059                echo '<input type="hidden" id="wp-old-slug" name="wp-old-slug" value="' . $name . '" />';
     2060}
     2061
     2062
    20262063// If siteurl or home changed, reset cookies and flush rewrite rules.
    20272064function update_home_siteurl($old_value, $value) {
    20282065        global $wp_rewrite, $user_login, $user_pass_md5;
     
    20412078add_action('update_option_home', 'update_home_siteurl', 10, 2);
    20422079add_action('update_option_siteurl', 'update_home_siteurl', 10, 2);
    20432080
    2044 ?>
     2081?>
     2082 No newline at end of file