WordPress.org

Make WordPress Core

Changeset 20287


Ignore:
Timestamp:
03/24/12 15:24:31 (3 years ago)
Author:
nacin
Message:

Introduce $wpdb->delete(). props justindgivens, scribu. fixes #18948.

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/bookmark.php

    r20256 r20287  
    8585    wp_delete_object_term_relationships( $link_id, 'link_category' ); 
    8686 
    87     $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->links WHERE link_id = %d", $link_id ) ); 
     87    $wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) ); 
    8888 
    8989    do_action( 'deleted_link', $link_id ); 
  • trunk/wp-admin/includes/ms.php

    r20199 r20287  
    9090        } 
    9191 
    92         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) ); 
     92        $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) ); 
     93 
    9394        $dir = apply_filters( 'wpmu_delete_blog_upload_dir', WP_CONTENT_DIR . "/blogs.dir/{$blog_id}/files/", $blog_id ); 
    9495        $dir = rtrim( $dir, DIRECTORY_SEPARATOR ); 
     
    159160    } 
    160161 
    161     $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->users WHERE ID = %d", $id ) ); 
    162     $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); 
     162    $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); 
     163    $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $id ) ); 
    163164 
    164165    clean_user_cache( $id ); 
  • trunk/wp-admin/includes/upgrade.php

    r20148 r20287  
    301301        // Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id. 
    302302        if ( !is_super_admin( $user_id ) && $user_id != 1 ) 
    303             $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $wpdb->base_prefix.'1_capabilities') ); 
     303            $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $user_id , 'meta_key' => $wpdb->base_prefix.'1_capabilities' ) ); 
    304304    } 
    305305} 
  • trunk/wp-admin/includes/user.php

    r19712 r20287  
    267267    // FINALLY, delete user 
    268268    if ( !is_multisite() ) { 
    269         $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) ); 
    270         $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) ); 
     269        $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $id ) ); 
     270        $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); 
    271271    } else { 
    272272        $level_key = $wpdb->get_blog_prefix() . 'capabilities'; // wpmu site admins don't have user_levels 
    273         $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = $id AND meta_key = '{$level_key}'"); 
     273        $wpdb->delete( $wpdb->usermeta, array( 'user_id' => $id , 'meta_key' => $level_key ) ); 
    274274    } 
    275275 
  • trunk/wp-includes/comment.php

    r20127 r20287  
    989989    } 
    990990 
    991     if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) ) 
     991    if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) ) 
    992992        return false; 
    993993    do_action('deleted_comment', $comment_id); 
  • trunk/wp-includes/meta.php

    r19712 r20287  
    490490 
    491491        // Run the query, will return true if deleted, false otherwise 
    492         $result = (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $id_column = %d LIMIT 1;", $meta_id ) ); 
     492        $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); 
    493493 
    494494        // Clear the caches. 
  • trunk/wp-includes/ms-functions.php

    r20101 r20287  
    497497        // If registered more than two days ago, cancel registration and let this signup go through. 
    498498        if ( $diff > 172800 ) 
    499             $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_login = %s", $user_name) ); 
     499            $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) ); 
    500500        else 
    501501            $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.')); 
     
    510510        // If registered more than two days ago, cancel registration and let this signup go through. 
    511511        if ( $diff > 172800 ) 
    512             $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_email = %s", $user_email) ); 
     512            $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) ); 
    513513        else 
    514514            $errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.')); 
     
    613613        // If registered more than two days ago, cancel registration and let this signup go through. 
    614614        if ( $diff > 172800 ) 
    615             $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); 
     615            $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) ); 
    616616        else 
    617617            $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.')); 
     
    11611161 
    11621162    // remove all perms 
    1163     $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'user_level') ); 
    1164     $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'capabilities') ); 
     1163    $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $table_prefix.'user_level' ) ); 
     1164 
     1165    $wpdb->delete( $wpdb->usermeta, array( 'meta_key' => $table_prefix.'capabilities' ) ); 
    11651166 
    11661167    $wpdb->suppress_errors( false ); 
  • trunk/wp-includes/option.php

    r19602 r20287  
    368368        return false; 
    369369    do_action( 'delete_option', $option ); 
    370     $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $option) ); 
     370    $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) ); 
    371371    if ( ! defined( 'WP_INSTALLING' ) ) { 
    372372        if ( 'yes' == $row->autoload ) { 
     
    858858        wp_cache_delete( $cache_key, 'site-options' ); 
    859859 
    860         $result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ); 
     860        $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) ); 
    861861    } 
    862862 
  • trunk/wp-includes/post.php

    r20271 r20287  
    20542054 
    20552055    do_action( 'delete_post', $postid ); 
    2056     $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid )); 
     2056    $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); 
    20572057    do_action( 'deleted_post', $postid ); 
    20582058 
     
    38113811    wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); 
    38123812 
    3813     $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND meta_value = %d", $post_id )); 
     3813    $wpdb->delete( $wpdb->postmeta, array( 'meta_key' => '_thumbnail_id' , 'meta_value' => $post_id ) ); 
    38143814 
    38153815    $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id )); 
     
    38303830 
    38313831    do_action( 'delete_post', $post_id ); 
    3832     $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $post_id )); 
     3832    $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); 
    38333833    do_action( 'deleted_post', $post_id ); 
    38343834 
  • trunk/wp-includes/taxonomy.php

    r20023 r20287  
    18031803 
    18041804    do_action( 'delete_term_taxonomy', $tt_id ); 
    1805     $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) ); 
     1805    $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); 
    18061806    do_action( 'deleted_term_taxonomy', $tt_id ); 
    18071807 
    18081808    // Delete the term if no taxonomies use it. 
    18091809    if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) ) 
    1810         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) ); 
     1810        $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) ); 
    18111811 
    18121812    clean_term_cache($term, $taxonomy); 
  • trunk/wp-includes/wp-db.php

    r19773 r20287  
    405405     * 
    406406     * @since 2.8.0 
    407      * @see wpdb:prepare() 
    408      * @see wpdb:insert() 
    409      * @see wpdb:update() 
     407     * @see wpdb::prepare() 
     408     * @see wpdb::insert() 
     409     * @see wpdb::update() 
     410     * @see wpdb::delete() 
    410411     * @see wp_set_wpdb_vars() 
    411412     * @access public 
     
    12711272        return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); 
    12721273    } 
     1274 
     1275    /** 
     1276     * Delete a row in the table 
     1277     * 
     1278     * <code> 
     1279     * wpdb::delete( 'table', array( 'ID' => 1 ) ) 
     1280     * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) ) 
     1281     * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ), 1 ) 
     1282     * </code> 
     1283     * 
     1284     * @since 2.5.0 
     1285     * @see wpdb::prepare() 
     1286     * @see wpdb::$field_types 
     1287     * @see wp_set_wpdb_vars() 
     1288     * 
     1289     * @param string $table table name 
     1290     * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". 
     1291     * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types. 
     1292     * @return int|false The number of rows updated, or false on error. 
     1293     */ 
     1294    function delete( $table, $where, $where_format = null ) { 
     1295        if ( ! is_array( $where ) ) 
     1296            return false; 
     1297 
     1298        $bits = $wheres = array(); 
     1299 
     1300        $where_formats = $where_format = (array) $where_format; 
     1301 
     1302        foreach ( array_keys( $where ) as $field ) { 
     1303            if ( !empty( $where_format ) ) { 
     1304                $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; 
     1305            } elseif ( isset( $this->field_types[ $field ] ) ) { 
     1306                $form = $this->field_types[ $field ]; 
     1307            } else { 
     1308                $form = '%s'; 
     1309            } 
     1310 
     1311            $wheres[] = "$field = $form"; 
     1312        } 
     1313 
     1314        $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres ); 
     1315        return $this->query( $this->prepare( $sql, $where ) ); 
     1316    } 
     1317 
    12731318 
    12741319    /** 
Note: See TracChangeset for help on using the changeset viewer.