WordPress.org

Make WordPress Core

Ticket #4328: 4328.1.diff

File 4328.1.diff, 6.8 KB (added by soulseekah, 3 years ago)

Revival

  • src/wp-includes/canonical.php

    diff --git src/wp-includes/canonical.php src/wp-includes/canonical.php
    index ad219c2..b2bba4b 100644
    function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    294294
    295295                        // paging and feeds
    296296                if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) {
    297                         while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) ) {
     297
     298                        $feeds = array();
     299                        foreach ( $wp_rewrite->feeds as $_feed ) {
     300                                $feeds[] = preg_quote( $_feed, '#' );
     301                        }
     302                        $feeds = implode( '|', $feeds );
     303
     304                        while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( "#/(comments/?)?($feeds)(/+)?$#", $redirect['path'] ) || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) ) {
    298305                                // Strip off paging and feed
    299306                                $redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing paging
    300                                 $redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] ); // strip off feed endings
     307                                $redirect['path'] = preg_replace( "#/(comments/?)?($feeds)(/+|$)#", '/', $redirect['path'] ); // strip off feed endings
    301308                                $redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] ); // strip off any existing comment paging
    302309                        }
    303310
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 39e1891..51b0698 100644
    function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { 
    57085708        }
    57095709
    57105710        // We're only concerned with published, non-hierarchical objects.
    5711         if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
     5711        if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) ) {
    57125712                return;
    57135713        }
    57145714
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index ced8e40..b840ce7 100644
    function wp_old_slug_redirect() { 
    953953                        $post_type = get_query_var( 'post_type' );
    954954                } elseif ( get_query_var( 'attachment' ) ) {
    955955                        $post_type = 'attachment';
    956                 } elseif ( get_query_var( 'pagename' ) ) {
    957                         $post_type = 'page';
    958956                } else {
    959                         $post_type = 'post';
     957                        $post_type = ''; // Open to all options
    960958                }
    961959
    962960                if ( is_array( $post_type ) ) {
    function wp_old_slug_redirect() { 
    966964                        $post_type = reset( $post_type );
    967965                }
    968966
    969                 // Do not attempt redirect for hierarchical post types
    970                 if ( is_post_type_hierarchical( $post_type ) ) {
    971                         return;
    972                 }
    973 
    974967                $id = _find_post_by_old_slug( $post_type );
    975968
    976969                if ( ! $id ) {
    function wp_old_slug_redirect() { 
    10321025function _find_post_by_old_slug( $post_type ) {
    10331026        global $wpdb;
    10341027
    1035         $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) );
     1028        $default_post_type_whitelist = array_keys( get_post_types( array( 'public' => true ) ) );
     1029        $post_type_whitelist = apply_filters( 'find_post_by_old_slug_post_types', $default_post_type_whitelist );
     1030
     1031        if ( $post_type && ! in_array( $post_type, $post_type_whitelist ) ) {
     1032                return 0;
     1033        }
     1034
     1035        $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id";
     1036        if ( $post_type ) {
     1037                $query .= $wpdb->prepare( " AND post_type = %s", $post_type );
     1038        } else {
     1039                $placeholders = trim( str_repeat( '%s,', count( $post_type_whitelist ) ), ',' );
     1040                $query .= $wpdb->prepare( " AND post_type IN ($placeholders)", $post_type_whitelist );
     1041        }
     1042        $query .= $wpdb->prepare( " AND meta_key = '_wp_old_slug' AND meta_value = %s", get_query_var( 'name' ) );
    10361043
    10371044        // if year, monthnum, or day have been specified, make our query more precise
    10381045        // just in case there are multiple identical _wp_old_slug values
    function _find_post_by_old_slug( $post_type ) { 
    10671074function _find_post_by_old_date( $post_type ) {
    10681075        global $wpdb;
    10691076
     1077        $default_post_type_whitelist = array_keys( get_post_types( array( 'public' => true ) ) );
     1078        $post_type_whitelist = apply_filters( 'find_post_by_old_slug_post_types', $default_post_type_whitelist );
     1079
     1080        if ( $post_type && ! in_array( $post_type, $post_type_whitelist ) ) {
     1081                return 0;
     1082        }
     1083
    10701084        $date_query = '';
    10711085        if ( get_query_var( 'year' ) ) {
    10721086                $date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) );
    function _find_post_by_old_date( $post_type ) { 
    10801094
    10811095        $id = 0;
    10821096        if ( $date_query ) {
    1083                 $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) );
     1097                $query = "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id";
     1098                if ( $post_type ) {
     1099                        $query .= $wpdb->prepare( " AND post_type = %s", $post_type );
     1100                } else {
     1101                        $placeholders = trim( str_repeat( '%s,', count( $post_type_whitelist ) ), ',' );
     1102                        $query .= $wpdb->prepare( " AND post_type IN ($placeholders)", $post_type_whitelist );
     1103                }
     1104                $query .= $wpdb->prepare( " AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, get_query_var( 'name' ) );
     1105
     1106                $id = (int) $wpdb->get_var( $query );
    10841107
    10851108                if ( ! $id ) {
     1109                        $query = "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id";
     1110                        if ( $post_type ) {
     1111                                $query .= $wpdb->prepare( " AND post_type = %s", $post_type );
     1112                        } else {
     1113                                $placeholders = trim( str_repeat( '%s,', count( $post_type_whitelist ) ), ',' );
     1114                                $query .= $wpdb->prepare( " AND post_type IN ($placeholders)", $post_type_whitelist );
     1115                        }
     1116                        $query .= $wpdb->prepare( " AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, get_query_var( 'name' ) );
     1117
    10861118                        // Check to see if an old slug matches the old date
    1087                         $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
     1119                        $id = (int) $wpdb->get_var( $query );
    10881120                }
    10891121        }
    10901122