WordPress.org

Make WordPress Core

Changeset 7645


Ignore:
Timestamp:
04/14/2008 04:13:25 PM (11 years ago)
Author:
ryan
Message:

Prepare DB queries in more places. Props filosofo. see #6644

Location:
trunk
Files:
24 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r7509 r7645  
    1616    if ( strstr( $s, ',' ) )
    1717        die; // it's a multiple tag insert, we won't find anything
    18     $results = $wpdb->get_col( "SELECT name FROM $wpdb->terms WHERE name LIKE ('%$s%')" );
     18    $results = $wpdb->get_col( $wpdb->prepare("SELECT name FROM $wpdb->terms WHERE name LIKE (%s)", '%' . $s . '%') );
    1919    echo join( $results, "\n" );
    2020    die;
  • trunk/wp-admin/edit-comments.php

    r7424 r7645  
    1313    foreach ($_REQUEST['delete_comments'] as $comment) : // Check the permissions on each
    1414        $comment = (int) $comment;
    15         $post_id = (int) $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = $comment");
    16         // $authordata = get_userdata( $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $post_id") );
     15        $post_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID = %d", $comment) );
    1716        if ( !current_user_can('edit_post', $post_id) )
    1817            continue;
  • trunk/wp-admin/edit-pages.php

    r7485 r7645  
    176176if ( 1 == count($posts) && is_singular() ) :
    177177
    178     $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved != 'spam' ORDER BY comment_date");
     178    $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved != 'spam' ORDER BY comment_date", $id) );
    179179    if ( $comments ) :
    180180        // Make sure comments, post, and post_author are cached
  • trunk/wp-admin/edit.php

    r7625 r7645  
    206206if ( 1 == count($posts) && is_singular() ) :
    207207
    208     $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved != 'spam' ORDER BY comment_date");
     208    $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved != 'spam' ORDER BY comment_date", $id) );
    209209    if ( $comments ) :
    210210        // Make sure comments, post, and post_author are cached
  • trunk/wp-admin/import/blogger.php

    r7072 r7645  
    642642
    643643        // Get an array of posts => authors
    644         $post_ids = (array) $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'blogger_blog' AND meta_value = '$host'");
     644        $post_ids = (array) $wpdb->get_col( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'blogger_blog' AND meta_value = %s", $host) );
    645645        $post_ids = join( ',', $post_ids );
    646646        $results = (array) $wpdb->get_results("SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = 'blogger_author' AND post_id IN ($post_ids)");
     
    659659            $post_ids = join( ',', $post_ids);
    660660
    661             $wpdb->query("UPDATE $wpdb->posts SET post_author = $user_id WHERE id IN ($post_ids)");
     661            $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE id IN ($post_ids)", $user_id) );
    662662            $this->blogs[$importing_blog]['authors'][$author][1] = $user_id;
    663663        }
  • trunk/wp-admin/import/dotclear.php

    r7397 r7645  
    1414    {
    1515        global $wpdb;
    16         return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
     16        return $wpdb->get_var( $wpdb->prepare("SELECT count(*) FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
    1717    }
    1818}
     
    2323    {
    2424        global $wpdb;
    25         return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$linkname.'"');
     25        return $wpdb->get_var( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_name = %s", $linkname) );
    2626    }
    2727}
  • trunk/wp-admin/import/textpattern.php

    r7397 r7645  
    99    {
    1010        global $wpdb;
    11         return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
     11        return $wpdb->get_var( $wpdb->prepare("SELECT count(*) FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
    1212    }
    1313}
     
    1818    {
    1919        global $wpdb;
    20         return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$wpdb->escape($linkname).'"');
     20        return $wpdb->get_var( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_name = %s", $linkname) );
    2121    }
    2222}
  • trunk/wp-admin/import/wp-cat2tag.php

    r6950 r7645  
    165165                    $posts = get_objects_in_term($category->term_id, 'category');
    166166                    foreach ( $posts as $post ) {
    167                         if ( !$wpdb->get_var("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = '$post' AND term_taxonomy_id = '$id'") )
    168                             $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$post', '$id')");
     167                        if ( !$wpdb->get_var( $wpdb->prepare("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $post, $id) ) )
     168                            $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES (%d, %d)", $post, $id) );
    169169                        clean_post_cache($post);
    170170                    }
    171171                } else {
    172                     $tt_ids = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
     172                    $tt_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) );
    173173                    if ( $tt_ids ) {
    174174                        $posts = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . join(',', $tt_ids) . ") GROUP BY object_id");
     
    178178
    179179                    // Change the category to a tag.
    180                     $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
    181 
    182                     $terms = $wpdb->get_col("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
     180                    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = %d AND taxonomy = 'category'", $category->term_id) );
     181
     182                    $terms = $wpdb->get_col( $wpdb->prepare("SELECT term_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = 'category'", $category->term_id) );
    183183                    foreach ( (array) $terms as $term )
    184184                        clean_category_cache($term);
    185185
    186186                    // Set all parents to 0 (root-level) if their parent was the converted tag
    187                     $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
     187                    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = %d AND taxonomy = 'category'", $category->term_id) );
    188188                }
    189189                // Clean the cache
  • trunk/wp-admin/includes/bookmark.php

    r7193 r7645  
    4848    wp_delete_object_term_relationships($link_id, 'link_category');
    4949
    50     $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
     50    $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->links WHERE link_id = %d", $link_id) );
    5151
    5252    do_action('deleted_link', $link_id);
     
    120120
    121121    if ( $update ) {
    122         $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
    123             link_name='$link_name', link_image='$link_image',
    124             link_target='$link_target',
    125             link_visible='$link_visible', link_description='$link_description',
    126             link_rating='$link_rating', link_rel='$link_rel',
    127             link_notes='$link_notes', link_rss = '$link_rss'
    128             WHERE link_id='$link_id'");
     122        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_url = %s,
     123            link_name = %s, link_image = %s, link_target = %s,
     124            link_visible = %s, link_description = %s, link_rating = %s,
     125            link_rel = %s, link_notes = %s, link_rss = %s
     126            WHERE link_id = %s", $link_url, $link_name, $link_image, $link_target, $link_visible, $link_description, $link_rating, $link_rel, $link_notes, $link_rss, $link_id) );
    129127    } else {
    130         $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
     128        $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
     129        $link_url,$link_name, $link_image, $link_target, $link_description, $link_visible, $link_owner, $link_rating, $link_rel, $link_notes, $link_rss) );
    131130        $link_id = (int) $wpdb->insert_id;
    132131    }
  • trunk/wp-admin/includes/comment.php

    r7609 r7645  
    44    global $wpdb;
    55
    6     return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
    7             WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
     6    return $wpdb->get_var( $wpdb->prepare("SELECT comment_post_ID FROM $wpdb->comments
     7            WHERE comment_author = %s AND comment_date = %s", $comment_author, $comment_date) );
    88}
    99
     
    6868    global $wpdb;
    6969    $post_id = (int) $post_id;
    70     $pending = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = $post_id AND comment_approved = '0'" );
     70    $pending = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '0'", $post_id) );
    7171    return $pending;
    7272}
  • trunk/wp-admin/includes/export.php

    r7299 r7645  
    1818if ( $author and $author != 'all' ) {
    1919    $author_id = (int) $author;
    20     $where = " WHERE post_author = '$author_id' ";
     20    $where = $wpdb->prepare(" WHERE post_author = %d ", $author_id);
    2121}
    2222
     
    218218<?php } ?>
    219219<?php
    220 $postmeta = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = $post->ID");
     220$postmeta = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID) );
    221221if ( $postmeta ) {
    222222?>
     
    229229<?php } ?>
    230230<?php
    231 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID");
     231$comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d", $post->ID) );
    232232if ( $comments ) { foreach ( $comments as $c ) { ?>
    233233<wp:comment>
  • trunk/wp-admin/includes/post.php

    r7638 r7645  
    195195
    196196    if (!empty ($post_date))
    197         $post_date = "AND post_date = '$post_date'";
     197        $post_date = $wpdb->prepare("AND post_date = %s", $post_date);
    198198
    199199    if (!empty ($title))
    200         return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
     200        return $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s $post_date", $title) );
    201201    else
    202202        if (!empty ($content))
    203             return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
     203            return $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_content = %s $post_date", $content) );
    204204
    205205    return 0;
     
    381381        wp_cache_delete($post_ID, 'post_meta');
    382382
    383         $wpdb->query( "
    384                 INSERT INTO $wpdb->postmeta
    385                 (post_id,meta_key,meta_value )
    386                 VALUES ('$post_ID','$metakey','$metavalue' )
    387             " );
     383        $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->postmeta
     384            (post_id,meta_key,meta_value ) VALUES (%s, %s, %s)",
     385            $post_ID, $metakey, $metavalue) );
    388386        return $wpdb->insert_id;
    389387    }
     
    395393    $mid = (int) $mid;
    396394
    397     $post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = '$mid'");
     395    $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
    398396    wp_cache_delete($post_id, 'post_meta');
    399397
    400     return $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
     398    return $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
    401399}
    402400
     
    418416    $mid = (int) $mid;
    419417
    420     $meta = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_id = '$mid'" );
     418    $meta = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
    421419    if ( is_serialized_string( $meta->meta_value ) )
    422420        $meta->meta_value = maybe_unserialize( $meta->meta_value );
     
    428426    global $wpdb;
    429427
    430     return $wpdb->get_results( "
    431             SELECT meta_key, meta_value, meta_id, post_id
    432             FROM $wpdb->postmeta
    433             WHERE post_id = '$postid'
    434             ORDER BY meta_key,meta_id", ARRAY_A );
     428    return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
     429            FROM $wpdb->postmeta WHERE post_id = %d
     430            ORDER BY meta_key,meta_id", $postid), ARRAY_A );
    435431
    436432}
     
    444440        return false;
    445441
    446     $post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = '$mid'");
     442    $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_id = %d", $mid) );
    447443    wp_cache_delete($post_id, 'post_meta');
    448444
     
    450446    $mvalue = $wpdb->escape( $mvalue );
    451447    $mid = (int) $mid;
    452     return $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '$mkey', meta_value = '$mvalue' WHERE meta_id = '$mid'" );
     448    return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->postmeta SET meta_key = %s, meta_value = %s WHERE meta_id = %d", $mkey, $mvalue, $mid) );
    453449}
    454450
     
    503499    $old_ID = (int) $old_ID;
    504500    $new_ID = (int) $new_ID;
    505     return $wpdb->query( "UPDATE $wpdb->posts SET post_parent = $new_ID WHERE post_parent = $old_ID" );
     501    return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE post_parent = %d", $new_ID, $old_ID) );
    506502}
    507503
  • trunk/wp-admin/includes/template.php

    r7595 r7645  
    893893function parent_dropdown( $default = 0, $parent = 0, $level = 0 ) {
    894894    global $wpdb, $post_ID;
    895     $items = $wpdb->get_results( "SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = $parent AND post_type = 'page' ORDER BY menu_order" );
     895    $items = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' ORDER BY menu_order", $parent) );
    896896
    897897    if ( $items ) {
  • trunk/wp-admin/includes/upgrade.php

    r7628 r7645  
    219219            if ('' == $post->post_name) {
    220220                $newtitle = sanitize_title($post->post_title);
    221                 $wpdb->query("UPDATE $wpdb->posts SET post_name = '$newtitle' WHERE ID = '$post->ID'");
     221                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
    222222            }
    223223        }
     
    228228        if ('' == $category->category_nicename) {
    229229            $newtitle = sanitize_title($category->cat_name);
    230             $wpdb->query("UPDATE $wpdb->categories SET category_nicename = '$newtitle' WHERE cat_ID = '$category->cat_ID'");
     230            $wpdb->query( $wpdb->prepare("UPDATE $wpdb->categories SET category_nicename = %s WHERE cat_ID = %d", $newtitle, $category->cat_ID) );
    231231        }
    232232    }
     
    251251        foreach ($allposts as $post) {
    252252            // Check to see if it's already been imported
    253             $cat = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post->ID AND category_id = $post->post_category");
     253            $cat = $wpdb->get_row( $wpdb->("SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) );
    254254            if (!$cat && 0 != $post->post_category) { // If there's no result
    255                 $wpdb->query("
    256                     INSERT INTO $wpdb->post2cat
     255                $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->post2cat
    257256                    (post_id, category_id)
    258                     VALUES
    259                     ('$post->ID', '$post->post_category')
    260                     ");
     257                    VALUES (%s, %s)
     258                    ", $post->ID, $post->post_category) );
    261259            }
    262260        }
     
    286284        if ('' == $user->user_nicename) {
    287285            $newname = sanitize_title($user->user_nickname);
    288             $wpdb->query("UPDATE $wpdb->users SET user_nicename = '$newname' WHERE ID = '$user->ID'");
     286            $wpdb->query( $wpdb->prepare("UPDATE $wpdb->users SET user_nicename = %s WHERE ID = %d", $newname, $user->ID) );
    289287        }
    290288    }
     
    402400        if ( 1 != $option->dupes ) { // Could this be done in the query?
    403401            $limit = $option->dupes - 1;
    404             $dupe_ids = $wpdb->get_col("SELECT option_id FROM $wpdb->options WHERE option_name = '$option->option_name' LIMIT $limit");
     402            $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
    405403            $dupe_ids = join($dupe_ids, ',');
    406404            $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
     
    446444            if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
    447445            if (!$idmode) $id = $user->user_nickname;
    448             $id = $wpdb->escape( $id );
    449             $wpdb->query("UPDATE $wpdb->users SET display_name = '$id' WHERE ID = '$user->ID'");
     446            $wpdb->query( $wpdb->prepare("UPDATE $wpdb->users SET display_name = %s WHERE ID = %d", $id, $user->ID) );
    450447        endif;
    451448
     
    469466    if( is_array( $comments ) ) {
    470467        foreach ($comments as $comment) {
    471             $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $comment->c WHERE ID = '$comment->comment_post_ID'" );
     468            $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_count = %d WHERE ID = %d", $comment->c, $comment->comment_post_ID) );
    472469        }
    473470    }
     
    478475        $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'");
    479476        foreach ($objects as $object) {
    480             $wpdb->query("UPDATE $wpdb->posts SET post_status = 'attachment',
    481             post_mime_type = '$object->post_type',
     477            $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'attachment',
     478            post_mime_type = %s,
    482479            post_type = ''
    483             WHERE ID = $object->ID");
     480            WHERE ID = %d", $object->post_type, $object->ID) );
    484481
    485482            $meta = get_post_meta($object->ID, 'imagedata', true);
     
    509506            }
    510507
    511             $wpdb->query("UPDATE $wpdb->posts SET post_status = '$status', post_type = '$type' WHERE ID = '$post->ID'");
     508            $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
    512509        }
    513510    }
     
    542539    foreach ($categories as $category) {
    543540        $term_id = (int) $category->cat_ID;
    544         $name = $wpdb->escape($category->cat_name);
    545         $description = $wpdb->escape($category->category_description);
    546         $slug = $wpdb->escape($category->category_nicename);
    547         $parent = $wpdb->escape($category->category_parent);
    548541        $term_group = 0;
    549542
    550543        // Associate terms with the same slug in a term group and make slugs unique.
    551         if ( $exists = $wpdb->get_results("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$slug'") ) {
     544        if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
    552545            $term_group = $exists[0]->term_group;
    553546            $id = $exists[0]->term_id;
     
    556549                $alt_slug = $slug . "-$num";
    557550                $num++;
    558                 $slug_check = $wpdb->get_var("SELECT slug FROM $wpdb->terms WHERE slug = '$alt_slug'");
     551                $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) );
    559552            } while ( $slug_check );
    560553
     
    563556            if ( empty( $term_group ) ) {
    564557                $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1;
    565                 $wpdb->query("UPDATE $wpdb->terms SET term_group = '$term_group' WHERE term_id = '$id'");
     558                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) );
    566559            }
    567560        }
    568561
    569         $wpdb->query("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES ('$term_id', '$name', '$slug', '$term_group')");
     562        $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES
     563        (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) );
    570564
    571565        $count = 0;
     
    573567            $count = (int) $category->category_count;
    574568            $taxonomy = 'category';
    575             $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '$count')");
     569            $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
    576570            $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
    577571        }
     
    580574            $count = (int) $category->link_count;
    581575            $taxonomy = 'link_category';
    582             $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '$count')");
     576            $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
    583577            $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
    584578        }
     
    588582            $count = (int) $category->tag_count;
    589583            $taxonomy = 'post_tag';
    590             $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '$count')");
     584            $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
    591585            $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
    592586        }
     
    595589            $count = 0;
    596590            $taxonomy = 'category';
    597             $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '$count')");
     591            $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
    598592            $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
    599593        }
     
    615609            continue;
    616610
    617         $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$post_id', '$tt_id')");
     611        $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $post_id, $tt_id) );
    618612    }
    619613
     
    634628
    635629            // Associate terms with the same slug in a term group and make slugs unique.
    636             if ( $exists = $wpdb->get_results("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$slug'") ) {
     630            if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
    637631                $term_group = $exists[0]->term_group;
    638632                $term_id = $exists[0]->term_id;
     
    640634
    641635            if ( empty($term_id) ) {
    642                 $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')");
     636                $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES (%s, %s, %d)", $name, $slug, $term_group) );
    643637                $term_id = (int) $wpdb->insert_id;
    644638            }
     
    647641            $default_link_cat = $term_id;
    648642
    649             $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', 'link_category', '', '0', '0')");
     643            $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES (%d, 'link_category', '', '0', '0')", $term_id) );
    650644            $tt_ids[$term_id] = (int) $wpdb->insert_id;
    651645        }
     
    663657                continue;
    664658
    665             $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$link->link_id', '$tt_id')");
     659            $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link->link_id, $tt_id) );
    666660        }
    667661
     
    678672                continue;
    679673
    680             $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$link_id', '$tt_id')");
     674            $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link_id, $tt_id) );
    681675        }
    682676    }
     
    691685    foreach ( (array) $terms as $term ) {
    692686        if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) )
    693             $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = '$term->term_taxonomy_id'");
     687            $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term->term_taxonomy_id) );
    694688        else
    695             $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$term->term_taxonomy_id'");
    696         $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term->term_taxonomy_id'");
     689            $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) );
     690        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_taxonomy_id = %d", $count, $term->term_taxonomy_id) );
    697691    }
    698692}
     
    824818    }
    825819
    826     $option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");
     820    $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting) );
    827821
    828822    if ( 'home' == $setting && '' == $option )
  • trunk/wp-admin/includes/user.php

    r7313 r7645  
    142142    global $wpdb;
    143143    $level_key = $wpdb->prefix . 'user_level';
    144 
    145     $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
    146 
    147     return $wpdb->get_col( $query );
     144    return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value != '0'", $level_key) );
    148145}
    149146
     
    177174    $level_key = $wpdb->prefix . 'user_level';
    178175
    179     $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
     176    $query = $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s", $level_key);
    180177    if ( $exclude_zeros )
    181178        $query .= " AND meta_value != '0'";
     
    188185    $level_key = $wpdb->prefix . 'user_level';
    189186
    190     $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
    191 
    192     return $wpdb->get_col( $query );
     187    return $wpdb->get_col( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = '0'", $level_key) );
    193188}
    194189
     
    209204    } else {
    210205        $editable = join(',', $editable);
    211         $other_unpubs = $wpdb->get_results("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != '$user_id' ORDER BY post_modified $dir");
     206        $other_unpubs = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_title, post_author FROM $wpdb->posts WHERE post_type = 'post' AND $type_sql AND post_author IN ($editable) AND post_author != %d ORDER BY post_modified $dir", $user_id) );
    212207    }
    213208
     
    242237function get_users_drafts( $user_id ) {
    243238    global $wpdb;
    244     $user_id = (int) $user_id;
    245     $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = $user_id ORDER BY post_modified DESC";
     239    $query = $wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id);
    246240    $query = apply_filters('get_users_drafts', $query);
    247241    return $wpdb->get_results( $query );
     
    254248
    255249    if ($reassign == 'novalue') {
    256         $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
     250        $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );
    257251
    258252        if ($post_ids) {
     
    262256
    263257        // Clean links
    264         $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
     258        $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->links WHERE link_owner = %d", $id) );
    265259    } else {
    266260        $reassign = (int) $reassign;
    267         $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
    268         $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
     261        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $id) );
     262        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d}", $reassign, $id) );
    269263    }
    270264
     
    272266    do_action('delete_user', $id);
    273267
    274     $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
    275     $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
     268    $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) );
     269    $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) );
    276270
    277271    wp_cache_delete($id, 'users');
     
    324318        global $wpdb;
    325319        $this->first_user = ($this->page - 1) * $this->users_per_page;
    326         $this->query_limit = ' LIMIT ' . $this->first_user . ',' . $this->users_per_page;
     320        $this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page);
    327321        $this->query_sort = ' ORDER BY user_login';
    328322        $search_sql = '';
     
    338332        $this->query_from_where = "FROM $wpdb->users";
    339333        if ( $this->role )
    340             $this->query_from_where .= " INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE '%$this->role%'";
     334            $this->query_from_where .= $wpdb->prepare(" INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id WHERE $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%');
    341335        else
    342336            $this->query_from_where .= " WHERE 1=1";
  • trunk/wp-admin/update-links.php

    r5843 r7645  
    3737
    3838    foreach ($returns as $return) :
    39         $time = $wpdb->escape( substr($return, 0, 19) );
    40         $uri = $wpdb->escape( preg_replace('/(.*?) | (.*?)/', '$2', $return) );
    41         $wpdb->query("UPDATE $wpdb->links SET link_updated = '$time' WHERE link_url = '$uri'");
     39        $time = substr($return, 0, 19);
     40        $uri = preg_replace('/(.*?) | (.*?)/', '$2', $return);
     41        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_updated = %s WHERE link_url = %s", $time, $uri) );
    4242    endforeach;
    4343}
  • trunk/wp-admin/upload.php

    r7542 r7645  
    212212if ( 1 == count($posts) && is_singular() ) :
    213213   
    214     $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $id AND comment_approved != 'spam' ORDER BY comment_date");
     214    $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved != 'spam' ORDER BY comment_date", $id) );
    215215    if ( $comments ) :
    216216        // Make sure comments, post, and post_author are cached
  • trunk/wp-comments-post.php

    r6716 r7645  
    1212$comment_post_ID = (int) $_POST['comment_post_ID'];
    1313
    14 $status = $wpdb->get_row("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = '$comment_post_ID'");
     14$status = $wpdb->get_row( $wpdb->prepare("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );
    1515
    1616if ( empty($status->comment_status) ) {
  • trunk/wp-includes/comment.php

    r7425 r7645  
    242242    $where = '';
    243243    if ( $post_id > 0 ) {
    244         $where = "WHERE comment_post_ID = {$post_id}";
     244        $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
    245245    }
    246246
     
    380380    if ( current_user_can( 'manage_options' ) )
    381381        return; // don't throttle admins
    382     if ( $lasttime = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = '$ip' OR comment_author_email = '$email' ORDER BY comment_date DESC LIMIT 1") ) {
     382    if ( $lasttime = $wpdb->get_var( $wpdb->prepare("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = %s OR comment_author_email = %s ORDER BY comment_date DESC LIMIT 1", $ip, $email) ) ) {
    383383        $time_lastcomment = mysql2date('U', $lasttime);
    384384        $time_newcomment  = mysql2date('U', $date);
     
    488488    $comment = get_comment($comment_id);
    489489
    490     if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
     490    if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
    491491        return false;
    492492
     
    586586        $user_id = 0;
    587587
    588     $result = $wpdb->query("INSERT INTO $wpdb->comments
     588    $result = $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->comments
    589589    (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id)
    590     VALUES
    591     ('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id')
    592     ");
     590    VALUES (%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d)",
     591    $comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, $comment_date, $comment_date_gmt, $comment_content, $comment_approved, $comment_agent, $comment_type, $comment_parent, $user_id) );
    593592
    594593    $id = (int) $wpdb->insert_id;
     
    715714    switch ( $comment_status ) {
    716715        case 'hold':
    717             $query = "UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID='$comment_id' LIMIT 1";
     716            $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='0' WHERE comment_ID = %d LIMIT 1", $comment_id);
    718717            break;
    719718        case 'approve':
    720             $query = "UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID='$comment_id' LIMIT 1";
     719            $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='1' WHERE comment_ID = %d LIMIT 1", $comment_id);
    721720            break;
    722721        case 'spam':
    723             $query = "UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID='$comment_id' LIMIT 1";
     722            $query = $wpdb->prepare("UPDATE $wpdb->comments SET comment_approved='spam' WHERE comment_ID = %d LIMIT 1", $comment_id);
    724723            break;
    725724        case 'delete':
     
    775774    $comment_date_gmt = get_gmt_from_date($comment_date);
    776775
    777     $wpdb->query(
    778         "UPDATE $wpdb->comments SET
    779             comment_content      = '$comment_content',
    780             comment_author       = '$comment_author',
    781             comment_author_email = '$comment_author_email',
    782             comment_approved     = '$comment_approved',
    783             comment_author_url   = '$comment_author_url',
    784             comment_date         = '$comment_date',
    785             comment_date_gmt     = '$comment_date_gmt'
    786         WHERE comment_ID = $comment_ID" );
     776    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->comments SET
     777            comment_content      = %s,
     778            comment_author       = %s,
     779            comment_author_email = %s,
     780            comment_approved     = %s,
     781            comment_author_url   = %s,
     782            comment_date         = %s,
     783            comment_date_gmt     = %s
     784        WHERE comment_ID = %d",
     785            $comment_content,
     786            $comment_author,
     787            $comment_author_email,
     788            $comment_approved,
     789            $comment_author_url,
     790            $comment_date,
     791            $comment_date_gmt
     792            $comment_ID) );
    787793
    788794    $rval = $wpdb->rows_affected;
     
    880886
    881887    $old = (int) $post->comment_count;
    882     $new = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
    883     $wpdb->query("UPDATE $wpdb->posts SET comment_count = '$new' WHERE ID = '$post_id'");
     888    $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
     889    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_count = %d WHERE ID = %d", $new, $post_id) );
    884890
    885891    if ( 'page' == $post->post_type )
     
    10091015    // Do Enclosures
    10101016    while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
    1011         $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$enclosure->ID} AND meta_key = '_encloseme';");
     1017        $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme';", $enclosure->ID) );
    10121018        do_enclose($enclosure->post_content, $enclosure->ID);
    10131019    }
     
    10361042    global $wpdb;
    10371043
    1038     $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = $post_id");
     1044    $post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) );
    10391045    $to_ping = get_to_ping($post_id);
    10401046    $pinged  = get_pung($post_id);
    10411047    if ( empty($to_ping) ) {
    1042         $wpdb->query("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = '$post_id'");
     1048        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = '' WHERE ID = %d", $post_id) );
    10431049        return;
    10441050    }
     
    10611067                $pinged[] = $tb_ping;
    10621068            } else {
    1063                 $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = '$post_id'");
     1069                $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_ping', '')) WHERE ID = %d", $post_id) );
    10641070            }
    10651071        }
     
    12261232
    12271233    $tb_url = addslashes( $tb_url );
    1228     $wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
    1229     return $wpdb->query("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = '$ID'");
     1234    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = %d", $ID) );
     1235    return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, '$tb_url', '')) WHERE ID = %d", $ID) );
    12301236}
    12311237
  • trunk/wp-includes/post.php

    r7638 r7645  
    475475    $query .= empty( $post_parent ) ? '' : $wpdb->prepare("AND $wpdb->posts.post_parent = %d ", $post_parent);
    476476    // expected_slashed ($meta_key, $meta_value) -- Also, this looks really funky, doesn't seem like it works
    477     $query .= empty( $meta_key ) | empty($meta_value)  ? '' : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )";
     477    $query .= empty( $meta_key ) | empty($meta_value)  ? '' : $wpdb->prepare(" AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = %s AND $wpdb->postmeta.meta_value = %s )", $meta_key, $meta_value);
    478478    $query .= empty( $post_mime_type ) ? '' : wp_post_mime_type_where($post_mime_type);
    479479    $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
     
    19611961    $query .= " WHERE (post_type = 'page' AND post_status = 'publish') $exclusions $inclusions " ;
    19621962    // expected_slashed ($meta_key, $meta_value) -- also, it looks funky
    1963     $query .= ( empty( $meta_key ) | empty($meta_value)  ? "" : " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )" ) ;
     1963    $query .= ( empty( $meta_key ) | empty($meta_value)  ? "" : $wpdb->prepare(" AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = %s AND $wpdb->postmeta.meta_value = %s )", $meta_key, $meta_value) ) ;
    19641964    $query .= $author_query;
    19651965    $query .= " ORDER BY " . $sort_column . " " . $sort_order ;
     
    27382738    do_action('clean_page_cache', $id);
    27392739
    2740     if ( $children = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_parent = '$id'" ) )
     2740    if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) )
    27412741        foreach( $children as $cid )
    27422742            clean_post_cache( $cid );
     
    29752975
    29762976    $id = $_post->ancestors[] = $_post->post_parent;
    2977     while ( $ancestor = $wpdb->get_var("SELECT `post_parent` FROM $wpdb->posts WHERE ID= '{$id}' LIMIT 1") ) {
     2977    while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
    29782978        if ( $id == $ancestor )
    29792979            break;
  • trunk/wp-includes/taxonomy.php

    r7545 r7645  
    750750
    751751    if ( !empty($taxonomy) )
    752         return $wpdb->get_row("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = '$taxonomy'", ARRAY_A);
     752        return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $taxonomy), ARRAY_A);
    753753
    754754    return $wpdb->get_var("SELECT term_id FROM $wpdb->terms as t WHERE $where");
     
    889889        $where = 'AND count > 0';
    890890
    891     $taxonomy = $wpdb->escape( $taxonomy );
    892     return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = '$taxonomy' $where");
     891    return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = %s $where", $taxonomy) );
    893892}
    894893
     
    919918        $terms = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
    920919        $in_terms = "'" . implode("', '", $terms) . "'";
    921         $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_terms)");
     920        $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_terms)", $object_id) );
    922921        wp_update_term_count($terms, $taxonomy);
    923922    }
     
    12941293        if ( $delete_terms ) {
    12951294            $in_delete_terms = "'" . implode("', '", $delete_terms) . "'";
    1296             $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_delete_terms)");
     1295            $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );
    12971296            wp_update_term_count($delete_terms, $taxonomy);
    12981297        }
  • trunk/wp-includes/user.php

    r7268 r7645  
    5858    if ( !$user )
    5959        $user = $wpdb->escape($_COOKIE[USER_COOKIE]);
    60     return $wpdb->get_var("SELECT $field FROM $wpdb->users WHERE user_login = '$user'");
     60    return $wpdb->get_var( $wpdb->prepare("SELECT $field FROM $wpdb->users WHERE user_login = %s", $user) );
    6161}
    6262
     
    6464    global $wpdb;
    6565    $userid = (int) $userid;
    66     return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$userid' AND post_type = 'post' AND " . get_private_posts_cap_sql('post'));
     66    return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND post_type = 'post' AND ", $userid) . get_private_posts_cap_sql('post'));
    6767}
    6868
     
    131131
    132132    if ( ! empty($meta_value) )
    133         $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key' AND meta_value = '$meta_value'");
    134     else
    135         $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
     133        $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s AND meta_value = %s", $userid, $meta_key, $meta_value) );
     134    else
     135        $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
    136136
    137137    wp_cache_delete($user_id, 'users');
     
    149149    if ( !empty($meta_key) ) {
    150150        $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    151         $metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
     151        $metas = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
    152152    } else {
    153         $metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
     153        $metas = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id) );
    154154    }
    155155
     
    186186    }
    187187
    188     $cur = $wpdb->get_row("SELECT * FROM $wpdb->usermeta WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
     188    $cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %d", $user_id, $meta_key) );
    189189    if ( !$cur ) {
    190190        $wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value )
     
    192192        ( '$user_id', '$meta_key', '$meta_value' )");
    193193    } else if ( $cur->meta_value != $meta_value ) {
    194         $wpdb->query("UPDATE $wpdb->usermeta SET meta_value = '$meta_value' WHERE user_id = '$user_id' AND meta_key = '$meta_key'");
     194        $wpdb->query( $wpdb->prepare("UPDATE $wpdb->usermeta SET meta_value = %s WHERE user_id = %d AND meta_key = %s", $meta_value, $user_id, $meta_key) );
    195195    } else {
    196196        return false;
  • trunk/wp-trackback.php

    r7559 r7645  
    8787    $comment_type = 'trackback';
    8888
    89     $dupe = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_author_url = '$comment_author_url'");
     89    $dupe = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $comment_post_ID, $comment_author_url) );
    9090    if ( $dupe )
    9191        trackback_response(1, 'We already have a ping from that URL for this post.');
  • trunk/xmlrpc.php

    r7617 r7645  
    13531353            foreach( $attachments as $file ) {
    13541354                if( strpos( $post_content, $file->guid ) !== false ) {
    1355                     $wpdb->query( "UPDATE {$wpdb->posts} SET post_parent = '$post_ID' WHERE ID = '{$file->ID}'" );
     1355                    $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE ID = %d", $post_ID, $file->ID) );
    13561356                }
    13571357            }
     
    20942094        }
    20952095
    2096         $comments = $wpdb->get_results("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = $post_ID");
     2096        $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
    20972097
    20982098        if (!$comments) {
     
    22072207                // ...or a string #title, a little more complicated
    22082208                $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
    2209                 $sql = "SELECT ID FROM $wpdb->posts WHERE post_title RLIKE '$title'";
     2209                $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title);
    22102210                if (! ($post_ID = $wpdb->get_var($sql)) ) {
    22112211                    // returning unknown error '0' is better than die()ing
     
    22362236
    22372237        // Let's check that the remote site didn't already pingback this entry
    2238         $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post_ID' AND comment_author_url = '$pagelinkedfrom'");
     2238        $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) );
    22392239
    22402240        if ( $wpdb->num_rows ) // We already have a Pingback from this URL
     
    23452345        }
    23462346
    2347         $comments = $wpdb->get_results("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = $post_ID");
     2347        $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
    23482348
    23492349        if (!$comments) {
Note: See TracChangeset for help on using the changeset viewer.