Changeset 3712

Show
Ignore:
Timestamp:
04/18/06 04:47:26 (3 years ago)
Author:
ryan
Message:

Move url_to_postid() to rewrite.php. #2525

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-includes/functions.php

    r3711 r3712  
    179179} 
    180180 
    181  
    182181function get_usernumposts($userid) { 
    183182    global $wpdb; 
    184183    return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$userid' AND post_type = 'post' AND post_status = 'publish'"); 
    185184} 
    186  
    187  
    188 // examine a url (supposedly from this blog) and try to 
    189 // determine the post ID it represents. 
    190 function url_to_postid($url) { 
    191     global $wp_rewrite; 
    192  
    193     // First, check to see if there is a 'p=N' or 'page_id=N' to match against 
    194     preg_match('#[?&](p|page_id)=(\d+)#', $url, $values); 
    195     $id = intval($values[2]); 
    196     if ( $id ) return $id; 
    197  
    198     // Check to see if we are using rewrite rules 
    199     $rewrite = $wp_rewrite->wp_rewrite_rules(); 
    200  
    201     // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options 
    202     if ( empty($rewrite) ) 
    203         return 0; 
    204  
    205     // $url cleanup by Mark Jaquith 
    206     // This fixes things like #anchors, ?query=strings, missing 'www.', 
    207     // added 'www.', or added 'index.php/' that will mess up our WP_Query 
    208     // and return a false negative 
    209  
    210     // Get rid of the #anchor 
    211     $url_split = explode('#', $url); 
    212     $url = $url_split[0]; 
    213  
    214     // Get rid of URI ?query=string 
    215     $url_split = explode('?', $url); 
    216     $url = $url_split[0]; 
    217  
    218     // Add 'www.' if it is absent and should be there 
    219     if ( false !== strpos(get_settings('home'), '://www.') && false === strpos($url, '://www.') ) 
    220         $url = str_replace('://', '://www.', $url); 
    221  
    222     // Strip 'www.' if it is present and shouldn't be 
    223     if ( false === strpos(get_settings('home'), '://www.') ) 
    224         $url = str_replace('://www.', '://', $url); 
    225  
    226     // Strip 'index.php/' if we're not using path info permalinks 
    227     if ( false === strpos($rewrite, 'index.php/') ) 
    228         $url = str_replace('index.php/', '', $url); 
    229  
    230     if ( false !== strpos($url, get_settings('home')) ) { 
    231         // Chop off http://domain.com 
    232         $url = str_replace(get_settings('home'), '', $url); 
    233     } else { 
    234         // Chop off /path/to/blog 
    235         $home_path = parse_url(get_settings('home')); 
    236         $home_path = $home_path['path']; 
    237         $url = str_replace($home_path, '', $url); 
    238     } 
    239  
    240     // Trim leading and lagging slashes 
    241     $url = trim($url, '/'); 
    242  
    243     $request = $url; 
    244  
    245     // Done with cleanup 
    246  
    247     // Look for matches. 
    248     $request_match = $request; 
    249     foreach ($rewrite as $match => $query) { 
    250         // If the requesting file is the anchor of the match, prepend it 
    251         // to the path info. 
    252         if ( (! empty($url)) && (strpos($match, $url) === 0) ) { 
    253             $request_match = $url . '/' . $request; 
    254         } 
    255  
    256         if ( preg_match("!^$match!", $request_match, $matches) ) { 
    257             // Got a match. 
    258             // Trim the query of everything up to the '?'. 
    259             $query = preg_replace("!^.+\?!", '', $query); 
    260  
    261             // Substitute the substring matches into the query. 
    262             eval("\$query = \"$query\";"); 
    263             $query = new WP_Query($query); 
    264             if ( $query->is_single || $query->is_page ) 
    265                 return $query->post->ID; 
    266             else 
    267                 return 0; 
    268         } 
    269     } 
    270     return 0; 
    271 } 
    272  
    273185 
    274186function maybe_unserialize($original) { 
  • trunk/wp-includes/rewrite.php

    r3697 r3712  
    5757    global $wp_rewrite; 
    5858    $wp_rewrite->add_endpoint($name, $places); 
     59} 
     60 
     61// examine a url (supposedly from this blog) and try to 
     62// determine the post ID it represents. 
     63function url_to_postid($url) { 
     64    global $wp_rewrite; 
     65 
     66    // First, check to see if there is a 'p=N' or 'page_id=N' to match against 
     67    preg_match('#[?&](p|page_id)=(\d+)#', $url, $values); 
     68    $id = intval($values[2]); 
     69    if ( $id ) return $id; 
     70 
     71    // Check to see if we are using rewrite rules 
     72    $rewrite = $wp_rewrite->wp_rewrite_rules(); 
     73 
     74    // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options 
     75    if ( empty($rewrite) ) 
     76        return 0; 
     77 
     78    // $url cleanup by Mark Jaquith 
     79    // This fixes things like #anchors, ?query=strings, missing 'www.', 
     80    // added 'www.', or added 'index.php/' that will mess up our WP_Query 
     81    // and return a false negative 
     82 
     83    // Get rid of the #anchor 
     84    $url_split = explode('#', $url); 
     85    $url = $url_split[0]; 
     86 
     87    // Get rid of URI ?query=string 
     88    $url_split = explode('?', $url); 
     89    $url = $url_split[0]; 
     90 
     91    // Add 'www.' if it is absent and should be there 
     92    if ( false !== strpos(get_settings('home'), '://www.') && false === strpos($url, '://www.') ) 
     93        $url = str_replace('://', '://www.', $url); 
     94 
     95    // Strip 'www.' if it is present and shouldn't be 
     96    if ( false === strpos(get_settings('home'), '://www.') ) 
     97        $url = str_replace('://www.', '://', $url); 
     98 
     99    // Strip 'index.php/' if we're not using path info permalinks 
     100    if ( false === strpos($rewrite, 'index.php/') ) 
     101        $url = str_replace('index.php/', '', $url); 
     102 
     103    if ( false !== strpos($url, get_settings('home')) ) { 
     104        // Chop off http://domain.com 
     105        $url = str_replace(get_settings('home'), '', $url); 
     106    } else { 
     107        // Chop off /path/to/blog 
     108        $home_path = parse_url(get_settings('home')); 
     109        $home_path = $home_path['path']; 
     110        $url = str_replace($home_path, '', $url); 
     111    } 
     112 
     113    // Trim leading and lagging slashes 
     114    $url = trim($url, '/'); 
     115 
     116    $request = $url; 
     117 
     118    // Done with cleanup 
     119 
     120    // Look for matches. 
     121    $request_match = $request; 
     122    foreach ($rewrite as $match => $query) { 
     123        // If the requesting file is the anchor of the match, prepend it 
     124        // to the path info. 
     125        if ( (! empty($url)) && (strpos($match, $url) === 0) ) { 
     126            $request_match = $url . '/' . $request; 
     127        } 
     128 
     129        if ( preg_match("!^$match!", $request_match, $matches) ) { 
     130            // Got a match. 
     131            // Trim the query of everything up to the '?'. 
     132            $query = preg_replace("!^.+\?!", '', $query); 
     133 
     134            // Substitute the substring matches into the query. 
     135            eval("\$query = \"$query\";"); 
     136            $query = new WP_Query($query); 
     137            if ( $query->is_single || $query->is_page ) 
     138                return $query->post->ID; 
     139            else 
     140                return 0; 
     141        } 
     142    } 
     143    return 0; 
    59144} 
    60145