Make WordPress Core


Ignore:
Timestamp:
04/18/2006 04:47:26 AM (20 years ago)
Author:
ryan
Message:

Move url_to_postid() to rewrite.php. #2525

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.