Make WordPress Core


Ignore:
Timestamp:
11/14/2003 06:55:41 PM (22 years ago)
Author:
emc3
Message:

Added url_to_postid() function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/b2-include/b2functions.php

    r546 r548  
    166166    $j = @date($dateformatstring, $i);
    167167    if (!$j) {
    168 # // for debug purposes
    169 #       echo $i." ".$mysqlstring;
     168    // for debug purposes
     169    //  echo $i." ".$mysqlstring;
    170170    }
    171171    return $j;
     
    463463    return $wpdb->get_var("SELECT COUNT(*) FROM $tableposts WHERE post_author = $userid");
    464464}
     465
     466// examine a url (supposedly from this blog) and try to
     467// determine the post ID it represents.
     468function url_to_postid($url = '') {
     469    global $wpdb, $tableposts, $siteurl;
     470
     471    // Take a link like 'http://example.com/blog/something'
     472    // and extract just the '/something':
     473    $uri = preg_replace("#$siteurl#i", '', $url);
     474
     475    // on failure, preg_replace just returns the subject string
     476    // so if $uri and $siteurl are the same, they didn't match:
     477    if ($uri == $siteurl)
     478        return 0;
     479       
     480    // First, check to see if there is a 'p=N' to match against:
     481    preg_match('#[?&]p=(\d+)#', $uri, $values);
     482    $p = intval($values[1]);
     483    if ($p) return $p;
     484   
     485    // Match $uri against our permalink structure
     486    $permalink_structure = get_settings('permalink_structure');
     487   
     488    // Matt's tokenizer code
     489    $rewritecode = array(
     490        '%year%',
     491        '%monthnum%',
     492        '%day%',
     493        '%postname%'
     494    );
     495    $rewritereplace = array(
     496        '([0-9]{4})?',
     497        '([0-9]{1,2})?',
     498        '([0-9]{1,2})?',
     499        '([0-9a-z-]+)?'
     500    );
     501
     502    // Turn the structure into a regular expression
     503    $matchre = str_replace('/', '/?', $permalink_structure);
     504    $matchre = str_replace($rewritecode, $rewritereplace, $matchre);
     505
     506    // Extract the key values from the uri:
     507    preg_match("#$matchre#",$uri,$values);
     508
     509    // Extract the token names from the structure:
     510    preg_match_all("#%(.+?)%#", $permalink_structure, $tokens);
     511
     512    for($i = 0; $i < count($tokens[1]); $i++) {
     513        $name = $tokens[1][$i];
     514        $value = $values[$i+1];
     515
     516        // Create a variable named $year, $monthnum, $day, or $postname:
     517        $$name = $value;
     518    }
     519   
     520    // Build a WHERE clause, making the values safe along the way:
     521    if ($year) $where .= " AND YEAR(post_date) = " . intval($year);
     522    if ($monthnum) $where .= " AND MONTH(post_date) = " . intval($monthnum);
     523    if ($day) $where .= " AND DAYOFMONTH(post_date) = " . intval($day);
     524    if ($postname) $where .= " AND post_name = '" . $wpdb->escape($postname) . "' ";
     525
     526    // Run the query to get the post ID:
     527    return intval($wpdb->get_var("SELECT ID FROM $tableposts WHERE 1 = 1 " . $where));
     528}
     529
    465530
    466531/* Options functions */
Note: See TracChangeset for help on using the changeset viewer.