Make WordPress Core


Ignore:
Timestamp:
05/24/2004 08:22:18 AM (21 years ago)
Author:
saxmatt
Message:

Giant commit, sorry mailing list people. Move all table names to new $wpdb versions. Works but the whole app needs thorough testing now.

File:
1 edited

Legend:

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

    r1354 r1355  
    9696
    9797function get_lastpostdate($timezone = 'server') {
    98     global $tableposts, $cache_lastpostdate, $pagenow, $wpdb;
     98    global $cache_lastpostdate, $pagenow, $wpdb;
    9999    $add_seconds_blog = get_settings('gmt_offset') * 3600;
    100100    $add_seconds_server = date('Z');
     
    103103        switch(strtolower($timezone)) {
    104104            case 'gmt':
    105                 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $tableposts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
     105                $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
    106106                break;
    107107            case 'blog':
    108                 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $tableposts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
     108                $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
    109109                break;
    110110            case 'server':
    111                 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $tableposts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
     111                $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
    112112                break;
    113113        }
     
    120120
    121121function get_lastpostmodified($timezone = 'server') {
    122     global $tableposts, $cache_lastpostmodified, $pagenow, $wpdb;
     122    global $cache_lastpostmodified, $pagenow, $wpdb;
    123123    $add_seconds_blog = get_settings('gmt_offset') * 3600;
    124124    $add_seconds_server = date('Z');
     
    127127        switch(strtolower($timezone)) {
    128128            case 'gmt':
    129                 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $tableposts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
     129                $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
    130130                break;
    131131            case 'blog':
    132                 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $tableposts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
     132                $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
    133133                break;
    134134            case 'server':
    135                 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $tableposts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
     135                $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
    136136                break;
    137137        }
     
    173173
    174174function get_userdata($userid) {
    175     global $wpdb, $cache_userdata, $tableusers;
     175    global $wpdb, $cache_userdata;
    176176    if ( empty($cache_userdata[$userid]) ) {
    177         $user = $wpdb->get_row("SELECT * FROM $tableusers WHERE ID = '$userid'");
     177        $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$userid'");
    178178        $user->user_nickname = stripslashes($user->user_nickname);
    179179        $user->user_firstname = stripslashes($user->user_firstname);
     
    188188
    189189function get_userdatabylogin($user_login) {
    190     global $tableusers, $cache_userdata, $wpdb;
     190    global $cache_userdata, $wpdb;
    191191    if ( empty($cache_userdata["$user_login"]) ) {
    192         $user = $wpdb->get_row("SELECT * FROM $tableusers WHERE user_login = '$user_login'");
     192        $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'");
    193193        $cache_userdata["$user_login"] = $user;
    194194    } else {
     
    199199
    200200function get_userid($user_login) {
    201     global $tableusers, $cache_userdata, $wpdb;
     201    global $cache_userdata, $wpdb;
    202202    if ( empty($cache_userdata["$user_login"]) ) {
    203         $user_id = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_login = '$user_login'");
     203        $user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$user_login'");
    204204
    205205        $cache_userdata["$user_login"] = $user_id;
     
    211211
    212212function get_usernumposts($userid) {
    213     global $tableposts, $tablecomments, $wpdb;
    214     return $wpdb->get_var("SELECT COUNT(*) FROM $tableposts WHERE post_author = '$userid'");
     213    global $wpdb;
     214    return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$userid'");
    215215}
    216216
     
    218218// determine the post ID it represents.
    219219function url_to_postid($url = '') {
    220     global $wpdb, $tableposts;
     220    global $wpdb;
    221221
    222222    $siteurl = get_settings('home');
     
    291291
    292292    // Run the query to get the post ID:
    293     $id = intval($wpdb->get_var("SELECT ID FROM $tableposts WHERE 1 = 1 " . $where));
     293    $id = intval($wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE 1 = 1 " . $where));
    294294
    295295    return $id;
     
    322322
    323323function get_alloptions() {
    324     global $tableoptions, $wpdb;
    325     $options = $wpdb->get_results("SELECT option_name, option_value FROM $tableoptions");
     324    global $wpdb;
     325    $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
    326326    if ($options) {
    327327        foreach ($options as $option) {
     
    339339
    340340function update_option($option_name, $newvalue) {
    341     global $wpdb, $tableoptions, $cache_settings;
     341    global $wpdb, $cache_settings;
    342342    $newvalue = stripslashes($newvalue);
    343343    $newvalue = trim($newvalue); // I can't think of any situation we wouldn't want to trim
    344344    $newvalue = $wpdb->escape($newvalue);
    345     $wpdb->query("UPDATE $tableoptions SET option_value = '$newvalue' WHERE option_name = '$option_name'");
     345    $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
    346346    $cache_settings = get_alloptions(); // Re cache settings
    347347    return true;
     
    352352function add_option($name, $value='') {
    353353    // Adds an option if it doesn't already exist
    354     global $wpdb, $tableoptions;
     354    global $wpdb;
    355355    if(!get_settings($name)) {
    356356        $name = $wpdb->escape($name);
    357357        $value = $wpdb->escape($value);
    358         $wpdb->query("INSERT INTO $tableoptions (option_name, option_value) VALUES ('$name', '$value')");
     358        $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value) VALUES ('$name', '$value')");
    359359
    360360        if($wpdb->insert_id) {
     
    367367
    368368function delete_option($name) {
    369     global $wpdb, $tableoptions, $tableoptiongroup_options;
     369    global $wpdb;
    370370    // Get the ID, if no ID then return
    371     $option_id = $wpdb->get_var("SELECT option_id FROM $tableoptions WHERE option_name = '$name'");
     371    $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
    372372    if (!$option_id) return false;
    373     $wpdb->query("DELETE FROM $tableoptiongroup_options WHERE option_id = '$option_id'");
    374     $wpdb->query("DELETE FROM $tableoptions WHERE option_name = '$name'");
     373    $wpdb->query("DELETE FROM $wpdb->optiongroup_options WHERE option_id = '$option_id'");
     374    $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
    375375    return true;
    376376}
    377377
    378378function get_postdata($postid) {
    379     global $post, $tableposts, $wpdb;
    380 
    381     $post = $wpdb->get_row("SELECT * FROM $tableposts WHERE ID = '$postid'");
     379    global $post, $wpdb;
     380
     381    $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = '$postid'");
    382382   
    383383    $postdata = array (
     
    403403
    404404function get_commentdata($comment_ID,$no_cache=0,$include_unapproved=false) { // less flexible, but saves DB queries
    405     global $postc,$id,$commentdata,$tablecomments, $wpdb;
     405    global $postc,$id,$commentdata, $wpdb;
    406406    if ($no_cache) {
    407         $query = "SELECT * FROM $tablecomments WHERE comment_ID = '$comment_ID'";
     407        $query = "SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_ID'";
    408408        if (false == $include_unapproved) {
    409409            $query .= " AND comment_approved = '1'";
     
    432432
    433433function get_catname($cat_ID) {
    434     global $tablecategories, $cache_catnames, $wpdb;
     434    global $cache_catnames, $wpdb;
    435435    if ( !$cache_catnames ) {
    436         $results = $wpdb->get_results("SELECT * FROM $tablecategories") or die('Oops, couldn\'t query the db for categories.');
     436        $results = $wpdb->get_results("SELECT * FROM $wpdb->categories") or die('Oops, couldn\'t query the db for categories.');
    437437        foreach ($results as $post) {
    438438            $cache_catnames[$post->cat_ID] = $post->cat_name;
     
    545545// Send a Trackback
    546546function trackback($trackback_url, $title, $excerpt, $ID) {
    547     global $wpdb, $tableposts;
     547    global $wpdb;
    548548    $title = urlencode(stripslashes($title));
    549549    $excerpt = urlencode(stripslashes($excerpt));
     
    573573    @fclose($fs);
    574574
    575     $wpdb->query("UPDATE $tableposts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
    576     $wpdb->query("UPDATE $tableposts SET to_ping = REPLACE(to_ping, '$tb_url', '') WHERE ID = '$ID'");
     575    $wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
     576    $wpdb->query("UPDATE $wpdb->posts SET to_ping = REPLACE(to_ping, '$tb_url', '') WHERE ID = '$ID'");
    577577    return $result;
    578578}
     
    899899 */
    900900function wp_set_comment_status($comment_id, $comment_status) {
    901     global $wpdb, $tablecomments;
     901    global $wpdb;
    902902
    903903    switch($comment_status) {
    904904        case 'hold':
    905             $query = "UPDATE $tablecomments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
     905            $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
    906906        break;
    907907        case 'approve':
    908             $query = "UPDATE $tablecomments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
     908            $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
    909909        break;
    910910        case 'delete':
    911             $query = "DELETE FROM $tablecomments WHERE comment_ID='$comment_id' LIMIT 1";
     911            $query = "DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1";
    912912        break;
    913913        default:
     
    935935 */
    936936function wp_get_comment_status($comment_id) {
    937     global $wpdb, $tablecomments;
     937    global $wpdb;
    938938   
    939     $result = $wpdb->get_var("SELECT comment_approved FROM $tablecomments WHERE comment_ID='$comment_id' LIMIT 1");
     939    $result = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
    940940    if ($result == NULL) {
    941941        return "deleted";
     
    950950
    951951function wp_notify_postauthor($comment_id, $comment_type='comment') {
    952     global $wpdb, $tablecomments, $tableposts, $tableusers;
     952    global $wpdb;
    953953    global $querystring_start, $querystring_equal, $querystring_separator;
    954954   
    955     $comment = $wpdb->get_row("SELECT * FROM $tablecomments WHERE comment_ID='$comment_id' LIMIT 1");
    956     $post = $wpdb->get_row("SELECT * FROM $tableposts WHERE ID='$comment->comment_post_ID' LIMIT 1");
    957     $user = $wpdb->get_row("SELECT * FROM $tableusers WHERE ID='$post->post_author' LIMIT 1");
     955    $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
     956    $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
     957    $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID='$post->post_author' LIMIT 1");
    958958
    959959    if ('' == $user->user_email) return false; // If there's no email to send the comment to
     
    10101010 */
    10111011function wp_notify_moderator($comment_id) {
    1012     global $wpdb, $tablecomments, $tableposts, $tableusers;
     1012    global $wpdb;
    10131013    global $querystring_start, $querystring_equal, $querystring_separator;
    10141014   
    1015     $comment = $wpdb->get_row("SELECT * FROM $tablecomments WHERE comment_ID='$comment_id' LIMIT 1");
    1016     $post = $wpdb->get_row("SELECT * FROM $tableposts WHERE ID='$comment->comment_post_ID' LIMIT 1");
    1017     $user = $wpdb->get_row("SELECT * FROM $tableusers WHERE ID='$post->post_author' LIMIT 1");
     1015    $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
     1016    $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
     1017    $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID='$post->post_author' LIMIT 1");
    10181018
    10191019    $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
    1020     $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $tablecomments WHERE comment_approved = '0'");
     1020    $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
    10211021
    10221022    $notify_message  = "A new comment on the post #$comment->comment_post_ID \"".stripslashes($post->post_title)."\" is waiting for your approval\r\n\r\n";
     
    13171317
    13181318function get_posts($args) {
    1319     global $wpdb, $tableposts;
     1319    global $wpdb;
    13201320    parse_str($args, $r);
    13211321    if (!isset($r['numberposts'])) $r['numberposts'] = 5;
     
    13281328    $now = current_time('mysql');
    13291329
    1330     $posts = $wpdb->get_results("SELECT DISTINCT * FROM $tableposts WHERE post_date <= '$now' AND (post_status = 'publish') GROUP BY $tableposts.ID ORDER BY post_date DESC LIMIT " . $r['offset'] . ',' . $r['numberposts']);
     1330    $posts = $wpdb->get_results("SELECT DISTINCT * FROM $wpdb->posts WHERE post_date <= '$now' AND (post_status = 'publish') GROUP BY $wpdb->posts.ID ORDER BY post_date DESC LIMIT " . $r['offset'] . ',' . $r['numberposts']);
    13311331   
    13321332    return $posts;
     
    13551355
    13561356function query_posts($query) {
    1357     global $wpdb, $tablepost2cat, $tableposts, $tablecategories, $tableusers,
     1357    global $wpdb,
    13581358        $pagenow;
    13591359
     
    14861486            $andor = 'OR';
    14871487        }
    1488         $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) ";
     1488        $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) ";
    14891489        $cat_array = explode(' ',$cat);
    14901490        $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
     
    15121512        }
    15131513        $category_name = preg_replace('|[^a-z0-9-_]|i', '', $category_name);
    1514         $tables = ", $tablepost2cat, $tablecategories";
    1515         $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) LEFT JOIN $tablecategories ON ($tablepost2cat.category_id = $tablecategories.cat_ID) ";
     1514        $tables = ", $wpdb->post2cat, $wpdb->categories";
     1515        $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
    15161516        $whichcat = " AND (category_nicename = '$category_name'";
    1517         $cat = $wpdb->get_var("SELECT cat_ID FROM $tablecategories WHERE category_nicename = '$category_name'");
     1517        $cat = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_name'");
    15181518        $whichcat .= get_category_children($cat, " OR category_id = ");
    15191519        $whichcat .= ")";
     
    15561556        }
    15571557        $author_name = preg_replace('|[^a-z0-9-_]|', '', strtolower($author_name));
    1558         $author = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_nicename='".$author_name."'");
     1558        $author = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$author_name."'");
    15591559        $whichauthor .= ' AND (post_author = '.intval($author).')';
    15601560    }
     
    16631663    else
    16641664        $where .= ')';
    1665     $where .= " GROUP BY $tableposts.ID";
    1666     $request = " SELECT $distinct * FROM $tableposts $join WHERE 1=1".$where." ORDER BY post_$orderby $limits";
     1665    $where .= " GROUP BY $wpdb->posts.ID";
     1666    $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1".$where." ORDER BY post_$orderby $limits";
    16671667
    16681668
     
    16831683function update_post_caches($posts) {
    16841684    global $category_cache, $comment_count_cache, $post_meta_cache;
    1685     global $tablecategories, $tablepost2cat, $tableposts, $tablecomments,
    1686         $tablepostmeta, $wpdb;
     1685    global $wpdb;
    16871686
    16881687    // No point in doing all this work if we didn't match any posts.
     
    16991698    $dogs = $wpdb->get_results("SELECT DISTINCT
    17001699        ID, category_id, cat_name, category_nicename, category_description, category_parent
    1701         FROM $tablecategories, $tablepost2cat, $tableposts
     1700        FROM $wpdb->categories, $wpdb->post2cat, $wpdb->posts
    17021701        WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)");
    17031702       
     
    17081707    // Do the same for comment numbers
    17091708    $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
    1710         FROM $tableposts
    1711         LEFT JOIN $tablecomments ON ( comment_post_ID = ID  AND comment_approved =  '1')
     1709        FROM $wpdb->posts
     1710        LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID  AND comment_approved =  '1')
    17121711        WHERE post_status =  'publish' AND ID IN ($post_id_list)
    17131712        GROUP BY ID");
     
    17221721    if ( $meta_list = $wpdb->get_results("
    17231722            SELECT post_id,meta_key,meta_value
    1724             FROM $tablepostmeta
     1723            FROM $wpdb->postmeta
    17251724            WHERE post_id IN($post_id_list)
    17261725            ORDER BY post_id,meta_key
     
    17471746
    17481747function update_category_cache() {
    1749     global $cache_categories, $tablecategories, $wpdb;
    1750     $dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1");
     1748    global $cache_categories, $wpdb;
     1749    $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories WHERE 1=1");
    17511750    foreach ($dogs as $catt) {
    17521751        $cache_categories[$catt->cat_ID] = $catt;
     
    17551754
    17561755function update_user_cache() {
    1757     global $cache_userdata, $tableusers, $wpdb;
    1758 
    1759     $users = $wpdb->get_results("SELECT * FROM $tableusers WHERE user_level > 0");
     1756    global $cache_userdata, $wpdb;
     1757
     1758    $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level > 0");
    17601759    foreach ($users as $user) {
    17611760        $cache_userdata[$user->ID] = $user;
Note: See TracChangeset for help on using the changeset viewer.