WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/12/2011 01:55:08 AM (7 years ago)
Author:
markjaquith
Message:

Eliminate verbose rewrite rules for ambiguous rewrite structures, resulting in massive performance gains. props andy, otto42, duck_. Nice work everyone! see #16687

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r18513 r18541  
    31473147function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
    31483148    global $wpdb;
    3149     $null = null;
     3149
    31503150    $page_path = rawurlencode(urldecode($page_path));
    31513151    $page_path = str_replace('%2F', '/', $page_path);
    31523152    $page_path = str_replace('%20', ' ', $page_path);
    3153     $page_paths = '/' . trim($page_path, '/');
    3154     $leaf_path  = sanitize_title(basename($page_paths));
    3155     $page_paths = explode('/', $page_paths);
    3156     $full_path = '';
    3157     foreach ( (array) $page_paths as $pathdir )
    3158         $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
    3159 
    3160     $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type ));
    3161 
    3162     if ( empty($pages) )
    3163         return $null;
    3164 
     3153    $parts = explode( '/', trim( $page_path, '/' ) );
     3154    $parts = array_map( 'esc_sql', $parts );
     3155    $parts = array_map( 'sanitize_title', $parts );
     3156
     3157    $in_string = "'". implode( "','", $parts ) . "'";
     3158    $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name IN ({$in_string}) AND (post_type = %s OR post_type = 'attachment')", $post_type ), OBJECT_K );
     3159
     3160    $revparts = array_reverse( $parts );
     3161
     3162    $foundid = 0;
    31653163    foreach ( $pages as $page ) {
    3166         $path = '/' . $leaf_path;
    3167         $curpage = $page;
    3168         while ( $curpage->post_parent != 0 ) {
    3169             $post_parent = $curpage->post_parent;
    3170             $curpage = wp_cache_get( $post_parent, 'posts' );
    3171             if ( false === $curpage )
    3172                 $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $post_parent, $post_type ) );
    3173             $path = '/' . $curpage->post_name . $path;
     3164        if ( $page->post_name == $revparts[0] ) {
     3165            $count = 0;
     3166            if ( $page->post_parent != 0 ) {
     3167                if ( null === ( $parent_page = $pages[ $page->post_parent ] ) )
     3168                    continue;
     3169
     3170                while ( $parent_page->ID != 0 ) {
     3171                    $count++;
     3172                    if ( $parent_page->post_name != $revparts[ $count ] )
     3173                        break;
     3174                    $parent_page = $pages[ $parent_page->post_parent ];
     3175                }
     3176
     3177                if ( $parent_page->ID == 0 && $count+1 == count($revparts) ) {
     3178                    $foundid = $page->ID;
     3179                    break;
     3180                }
     3181            } else if ( count($revparts) == 1 ) {
     3182                $foundid = $page->ID;
     3183                break;
     3184            }
    31743185        }
    3175 
    3176         if ( $path == $full_path )
    3177             return get_page($page->ID, $output, $post_type);
    3178     }
    3179 
    3180     return $null;
     3186    }
     3187
     3188    if ( $foundid )
     3189        return get_page($foundid, $output, $post_type);
     3190
     3191    return null;
    31813192}
    31823193
Note: See TracChangeset for help on using the changeset viewer.