Rework url_to_postid() to handle all supported permalink structs. Helps resolve pingback URIs to posts. http://mosquito.wordpress.org/view.php?id=1324

git-svn-id: http://svn.automattic.com/wordpress/trunk@2609 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-05-14 02:59:42 +00:00
parent 8788a9bd9e
commit e11647a638
1 changed files with 44 additions and 82 deletions

View File

@ -173,95 +173,57 @@ function get_usernumposts($userid) {
// examine a url (supposedly from this blog) and try to // examine a url (supposedly from this blog) and try to
// determine the post ID it represents. // determine the post ID it represents.
function url_to_postid($url = '') { function url_to_postid($url = '') {
global $wpdb; global $wp_rewrite;
$siteurl = get_settings('home');
// Take a link like 'http://example.com/blog/something'
// and extract just the '/something':
$uri = preg_replace("#$siteurl#i", '', $url);
// on failure, preg_replace just returns the subject string
// so if $uri and $siteurl are the same, they didn't match:
if ($uri == $siteurl)
return 0;
// First, check to see if there is a 'p=N' or 'page_id=N' to match against: // First, check to see if there is a 'p=N' or 'page_id=N' to match against:
preg_match('#[?&](p|page_id)=(\d+)#', $uri, $values); preg_match('#[?&](p|page_id)=(\d+)#', $url, $values);
$p = intval($values[2]); $id = intval($values[2]);
if ($p) return $p; if ($id) return $id;
// URI is probably a permalink.
$rewrite = $wp_rewrite->wp_rewrite_rules();
if ( empty($rewrite) )
return 0;
$req_uri = $url;
$home_path = parse_url(get_settings('home'));
$home_path = $home_path['path'];
// Match $uri against our permalink structure // Trim path info from the end and the leading home path from the
$permalink_structure = get_settings('permalink_structure'); // front. For path info requests, this leaves us with the requesting
// filename, if any. For 404 requests, this leaves us with the
// requested permalink.
$req_uri = str_replace($pathinfo, '', $req_uri);
$req_uri = str_replace($home_path, '', $req_uri);
$req_uri = trim($req_uri, '/');
$request = $req_uri;
// Matt's tokenizer code // Look for matches.
$rewritecode = array( $request_match = $request;
'%year%', foreach ($rewrite as $match => $query) {
'%monthnum%', // If the requesting file is the anchor of the match, prepend it
'%day%', // to the path info.
'%hour%', if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) {
'%minute%', $request_match = $req_uri . '/' . $request;
'%second%', }
'%postname%',
'%post_id%'
);
$rewritereplace = array(
'([0-9]{4})?',
'([0-9]{1,2})?',
'([0-9]{1,2})?',
'([0-9]{1,2})?',
'([0-9]{1,2})?',
'([0-9]{1,2})?',
'([_0-9a-z-]+)?',
'([0-9]+)?'
);
// Turn the structure into a regular expression if (preg_match("!^$match!", $request_match, $matches)) {
$matchre = str_replace('/', '/?', $permalink_structure); // Got a match.
$matchre = str_replace($rewritecode, $rewritereplace, $matchre); // Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\?!", '', $query);
// Extract the key values from the uri:
preg_match("#$matchre#",$uri,$values); // Substitute the substring matches into the query.
eval("\$query = \"$query\";");
// Extract the token names from the structure: $query = new WP_Query($query);
preg_match_all("#%(.+?)%#", $permalink_structure, $tokens); if ( !empty($query->post) )
return $query->post->ID;
for($i = 0; $i < count($tokens[1]); $i++) { else
$name = $tokens[1][$i]; return 0;
$value = $values[$i+1]; }
// Create a variable named $year, $monthnum, $day, $postname, or $post_id:
$$name = $value;
}
// If using %post_id%, we're done:
if (intval($post_id)) return intval($post_id);
// Otherwise, build a WHERE clause, making the values safe along the way:
if ($year) $where .= " AND YEAR(post_date) = '" . intval($year) . "'";
if ($monthnum) $where .= " AND MONTH(post_date) = '" . intval($monthnum) . "'";
if ($day) $where .= " AND DAYOFMONTH(post_date) = '" . intval($day) . "'";
if ($hour) $where .= " AND HOUR(post_date) = '" . intval($hour) . "'";
if ($minute) $where .= " AND MINUTE(post_date) = '" . intval($minute) . "'";
if ($second) $where .= " AND SECOND(post_date) = '" . intval($second) . "'";
if ($postname) $where .= " AND post_name = '" . $wpdb->escape($postname) . "' ";
// We got no indication, so we return false:
if (!strlen($where)) {
return false;
} }
// if all we got was a postname, it's probably a page, so we'll want to check for a possible subpage return 0;
if ($postname && !$year && !$monthnum && !$day && !$hour && !$minute && !$second) {
$postname = rtrim(strstr($uri, $postname), '/');
$uri_array = explode('/', $postname);
$postname = $uri_array[count($uri_array) - 1];
$where = " AND post_name = '" . $wpdb->escape($postname) . "' ";
}
// Run the query to get the post ID:
$id = intval($wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE 1 = 1 " . $where));
return $id;
} }