Ticket #15953: 15953.patch
File 15953.patch, 7.5 KB (added by , 8 years ago) |
---|
-
src/wp-includes/default-filters.php
331 331 add_action( 'post_updated', 'wp_check_for_changed_slugs', 12, 3 ); 332 332 add_action( 'attachment_updated', 'wp_check_for_changed_slugs', 12, 3 ); 333 333 334 // Redirect Old Term Slugs 335 add_action( 'template_redirect', 'wp_old_term_slug_redirect' ); 336 add_action( 'edited_term', 'wp_check_for_changed_term_slugs', 10, 5 ); 337 334 338 // Nonce check for Post Previews 335 339 add_action( 'init', '_show_post_preview' ); 336 340 -
src/wp-includes/query.php
920 920 } 921 921 922 922 /** 923 * Redirect old term slugs to the correct permalink. 924 * 925 * Attempts to find the current slug from the past slugs for a term. 926 * 927 * @since 4.7.1 928 * 929 * @global wpdb $wpdb WordPress database abstraction object. 930 */ 931 function wp_old_term_slug_redirect() { 932 if( is_404() && '' !== get_query_var( 'taxonomy' ) && '' !== get_query_var( 'term' ) ) { 933 global $wpdb; 934 935 $term_id = get_term( array( 936 'taxonomy' => get_query_var( 'taxonomy' ), 937 'fields' => 'ids', 938 'number' => 1, 939 'hide_empty' => false, 940 'meta_query' => array( 941 array( 942 'key' => '_wp_old_slug', 943 'value' => get_query_var( 'term' ) 944 ) 945 ) 946 ) ); 947 948 if ( is_wp_error( $term_id ) || empty( $term_id ) ){ 949 return; 950 } 951 952 $link = get_term_link( $term_id[0], get_query_var( 'taxonomy' ) ); 953 954 if ( isset( $GLOBALS['wp_query']->query_vars['paged'] ) && $GLOBALS['wp_query']->query_vars['paged'] > 1 ) { 955 $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . $GLOBALS['wp_query']->query_vars['paged'] ); 956 } elseif( is_embed() ) { 957 $link = user_trailingslashit( trailingslashit( $link ) . 'embed' ); 958 } 959 960 /** 961 * Filters the old term slug redirect URL. 962 * 963 * @since 4.7.1 964 * 965 * @param string $link The redirect URL. 966 */ 967 $link = apply_filters( 'old_term_slug_redirect_url', $link ); 968 969 if ( ! $link ) { 970 return; 971 } 972 973 wp_redirect( $link, 301 ); // Permanent redirect 974 exit; 975 } 976 } 977 978 /** 923 979 * Set up global post data. 924 980 * 925 981 * @since 1.5.0 -
src/wp-includes/taxonomy.php
2574 2574 2575 2575 // First, get all of the original args 2576 2576 $term = get_term( $term_id, $taxonomy ); 2577 $term_before = $term; 2577 2578 2578 2579 if ( is_wp_error( $term ) ) { 2579 2580 return $term; … … 2769 2770 2770 2771 clean_term_cache($term_id, $taxonomy); 2771 2772 2773 $term_after = get_term( $term_id ); 2774 2772 2775 /** 2773 2776 * Fires after a term has been updated, and the term cache has been cleaned. 2774 2777 * 2775 2778 * @since 2.3.0 2776 2779 * 2777 * @param int $term_id Term ID. 2778 * @param int $tt_id Term taxonomy ID. 2779 * @param string $taxonomy Taxonomy slug. 2780 * @param int $term_id Term ID. 2781 * @param int $tt_id Term taxonomy ID. 2782 * @param string $taxonomy Taxonomy slug. 2783 * @param WP_Term $term_after Term object following the update. 2784 * @param WP_Term $term_before Term object before the update. 2780 2785 */ 2781 do_action( "edited_term", $term_id, $tt_id, $taxonomy );2786 do_action( "edited_term", $term_id, $tt_id, $taxonomy, $term_after, $term_before ); 2782 2787 2783 2788 /** 2784 2789 * Fires after a term for a specific taxonomy has been updated, and the term … … 4227 4232 4228 4233 return $parent; 4229 4234 } 4235 4236 /** 4237 * Check for changed slugs for the term and save the old slug. 4238 * 4239 * The function is used when a term is updated, 4240 * by comparing the current and previous term objects. 4241 * 4242 * If the slug was changed and not already part of the old slugs then it will be 4243 * added to the term meta field ('_wp_old_slug') for storing old slugs for 4244 * that term. 4245 * 4246 * The most logically usage of this function is redirecting changed term objects, so 4247 * that those that linked to a changed term will be redirected to the new term. 4248 * 4249 * @since 4.7.1 4250 * 4251 * @param int $term_id Term ID. 4252 * @param int $tt_id Taxonomy Term ID. 4253 * @param string $taxonomy Taxonomy 4254 * @param WP_Term $term_after Term object following the update 4255 * @param WP_Term $term_before Term object before the update 4256 */ 4257 function wp_check_for_changed_term_slugs( $term_id, $tt_id, $taxonomy, $term_after, $term_before ) { 4258 // Don't bother if it hasn't changed. 4259 if ( $term_after->slug == $term_before->slug ) { 4260 return; 4261 } 4262 4263 $old_slugs = (array) get_term_meta( $term_id, '_wp_old_slug' ); 4264 4265 // If we haven't added this old slug before, add it now. 4266 if ( ! empty( $term_before->slug ) && ! in_array( $term_before->slug, $old_slugs ) ) { 4267 add_term_meta( $term_id, '_wp_old_slug', $term_before->slug ); 4268 } 4269 4270 // If the new slug was used previously, delete it from the list. 4271 if ( in_array( $term_after->slug, $old_slugs ) ) { 4272 delete_term_meta( $term_id, '_wp_old_slug', $term_after->slug ); 4273 } 4274 } -
tests/phpunit/tests/rewrite/oldTermSlugRedirect.php
1 <?php 2 3 /** 4 * @group rewrite 5 * @ticket 15953 6 */ 7 class Tests_Rewrite_OldTermSlugRedirect extends WP_UnitTestCase { 8 protected $old_term_slug_redirect_url; 9 10 protected $term_id; 11 12 public function setUp() { 13 global $wp_rewrite; 14 15 parent::setUp(); 16 17 $this->set_permalink_structure( '/%postname%/' ); 18 19 register_taxonomy( 'wptests_tax', array( 'post' ) ); 20 21 $this->term_id = self::factory()->term->create( array( 22 'taxonomy' => 'wptests_tax', 23 'name' => 'Foo Bar', 24 'slug' => 'foo-bar' 25 ) ); 26 27 $wp_rewrite->flush_rules(); 28 29 add_filter( 'old_term_slug_redirect_url', array( $this, 'filter_old_term_slug_redirect_url' ), 10, 1 ); 30 } 31 32 public function tearDown() { 33 parent::tearDown(); 34 35 $this->old_term_slug_redirect_url = null; 36 37 remove_filter( 'old_term_slug_redirect_url', array( $this, 'filter_old_term_slug_redirect_url' ), 10 ); 38 } 39 40 public function test_old_term_slug_redirect() { 41 $old_permalink = user_trailingslashit( get_term_link( $this->term_id, 'wptests_tax' ) ); 42 43 wp_update_term( $this->term_id, 'wptests_tax', array( 44 'slug' => 'bar-baz' 45 ) ); 46 47 $permalink = user_trailingslashit( get_term_link( $this->term_id, 'wptests_tax' ) ); 48 49 $this->go_to( $old_permalink ); 50 wp_old_term_slug_redirect(); 51 $this->assertEquals( $permalink, $this->old_term_slug_redirect_url ); 52 } 53 54 public function test_old_slug_doesnt_redirect_when_reused() { 55 $old_permalink = user_trailingslashit( get_term_link( $this->term_id ) ); 56 57 wp_update_term( $this->term_id, 'wptests_tax', array( 58 'slug' => 'bar-baz', 59 ) ); 60 61 $new_term_id = self::factory()->term->create( array( 62 'taxonomy' => 'wptests_tax', 63 'name' => 'Foo Bar', 64 'slug' => 'foo-bar' 65 ) ); 66 67 $permalink = user_trailingslashit( get_term_link( $new_term_id ) ); 68 69 $this->assertEquals( $old_permalink, $permalink ); 70 71 $this->go_to( $old_permalink ); 72 wp_old_term_slug_redirect(); 73 $this->assertNull( $this->old_term_slug_redirect_url ); 74 } 75 76 public function filter_old_term_slug_redirect_url( $url ) { 77 $this->old_term_slug_redirect_url = $url; 78 return false; 79 } 80 }