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:
parent
8788a9bd9e
commit
e11647a638
|
@ -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;
|
||||||
|
|
||||||
// Match $uri against our permalink structure
|
// URI is probably a permalink.
|
||||||
$permalink_structure = get_settings('permalink_structure');
|
$rewrite = $wp_rewrite->wp_rewrite_rules();
|
||||||
|
|
||||||
// Matt's tokenizer code
|
if ( empty($rewrite) )
|
||||||
$rewritecode = array(
|
return 0;
|
||||||
'%year%',
|
|
||||||
'%monthnum%',
|
|
||||||
'%day%',
|
|
||||||
'%hour%',
|
|
||||||
'%minute%',
|
|
||||||
'%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
|
$req_uri = $url;
|
||||||
$matchre = str_replace('/', '/?', $permalink_structure);
|
$home_path = parse_url(get_settings('home'));
|
||||||
$matchre = str_replace($rewritecode, $rewritereplace, $matchre);
|
$home_path = $home_path['path'];
|
||||||
|
|
||||||
// Extract the key values from the uri:
|
// Trim path info from the end and the leading home path from the
|
||||||
preg_match("#$matchre#",$uri,$values);
|
// 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;
|
||||||
|
|
||||||
// Extract the token names from the structure:
|
// Look for matches.
|
||||||
preg_match_all("#%(.+?)%#", $permalink_structure, $tokens);
|
$request_match = $request;
|
||||||
|
foreach ($rewrite as $match => $query) {
|
||||||
for($i = 0; $i < count($tokens[1]); $i++) {
|
// If the requesting file is the anchor of the match, prepend it
|
||||||
$name = $tokens[1][$i];
|
// to the path info.
|
||||||
$value = $values[$i+1];
|
if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) {
|
||||||
|
$request_match = $req_uri . '/' . $request;
|
||||||
// Create a variable named $year, $monthnum, $day, $postname, or $post_id:
|
|
||||||
$$name = $value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If using %post_id%, we're done:
|
if (preg_match("!^$match!", $request_match, $matches)) {
|
||||||
if (intval($post_id)) return intval($post_id);
|
// Got a match.
|
||||||
|
// Trim the query of everything up to the '?'.
|
||||||
|
$query = preg_replace("!^.+\?!", '', $query);
|
||||||
|
|
||||||
// Otherwise, build a WHERE clause, making the values safe along the way:
|
// Substitute the substring matches into the query.
|
||||||
if ($year) $where .= " AND YEAR(post_date) = '" . intval($year) . "'";
|
eval("\$query = \"$query\";");
|
||||||
if ($monthnum) $where .= " AND MONTH(post_date) = '" . intval($monthnum) . "'";
|
$query = new WP_Query($query);
|
||||||
if ($day) $where .= " AND DAYOFMONTH(post_date) = '" . intval($day) . "'";
|
if ( !empty($query->post) )
|
||||||
if ($hour) $where .= " AND HOUR(post_date) = '" . intval($hour) . "'";
|
return $query->post->ID;
|
||||||
if ($minute) $where .= " AND MINUTE(post_date) = '" . intval($minute) . "'";
|
else
|
||||||
if ($second) $where .= " AND SECOND(post_date) = '" . intval($second) . "'";
|
return 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue