Skip to content

Commit 54a6a40

Browse files
committed
Introduce $wpdb->delete(). props justindgivens, scribu. fixes #18948.
git-svn-id: https://develop.svn.wordpress.org/trunk@20287 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2a020b2 commit 54a6a40

11 files changed

Lines changed: 72 additions & 25 deletions

File tree

wp-admin/includes/bookmark.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function wp_delete_link( $link_id ) {
8484

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 );
9090

wp-admin/includes/ms.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ function wpmu_delete_blog( $blog_id, $drop = false ) {
8989
$wpdb->query( "DROP TABLE IF EXISTS `$table`" );
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 );
9596
$top_dir = $dir;
@@ -158,8 +159,8 @@ function wpmu_delete_user( $id ) {
158159
}
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 );
165166

wp-admin/includes/upgrade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function wp_install_defaults($user_id) {
300300

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
}
306306
endif;

wp-admin/includes/user.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ function wp_delete_user( $id, $reassign = 'novalue' ) {
266266

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

276276
// allow for commit transaction

wp-includes/comment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ function wp_delete_comment($comment_id, $force_delete = false) {
988988
do_action( 'deleted_commentmeta', $meta_ids );
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);
994994

wp-includes/meta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ function delete_metadata_by_mid( $meta_type, $meta_id ) {
489489
do_action( 'delete_postmeta', $meta_id );
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.
495495
wp_cache_delete($object_id, $meta_type . '_meta');

wp-includes/ms-functions.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ function wpmu_validate_user_signup($user_name, $user_email) {
496496
$diff = $now - $registered_at;
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.'));
502502

@@ -509,7 +509,7 @@ function wpmu_validate_user_signup($user_name, $user_email) {
509509
$diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
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.'));
515515
}
@@ -612,7 +612,7 @@ function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
612612
$diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
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.'));
618618
}
@@ -1160,8 +1160,9 @@ function install_blog($blog_id, $blog_title = '') {
11601160
$wpdb->update( $wpdb->options, array('option_value' => ''), array('option_name' => 'admin_email') );
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 );
11671168
}

wp-includes/option.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ function delete_option( $option ) {
367367
if ( is_null( $row ) )
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 ) {
373373
$alloptions = wp_load_alloptions();
@@ -857,7 +857,7 @@ function delete_site_option( $option ) {
857857
$cache_key = "{$wpdb->siteid}:$option";
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

863863
if ( $result ) {

wp-includes/post.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ function wp_delete_post( $postid = 0, $force_delete = false ) {
20532053
}
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

20592059
if ( 'page' == $post->post_type ) {
@@ -3810,7 +3810,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
38103810
wp_delete_object_term_relationships($post_id, array('category', 'post_tag'));
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 ));
38163816
if ( ! empty( $comment_ids ) ) {
@@ -3829,7 +3829,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
38293829
}
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

38353835
$uploadpath = wp_upload_dir();

wp-includes/taxonomy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,12 +1802,12 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) {
18021802
clean_object_term_cache( $objects, $object_type );
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);
18131813

0 commit comments

Comments
 (0)