In $wpdb->update()
, prevent explosions when $where
is empty.
Adds unit tests. Props UmeshSingla, wonderboymusic. Fixes #26106 git-svn-id: https://develop.svn.wordpress.org/trunk@28814 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
76bd35fa79
commit
220fda0095
@ -1785,7 +1785,9 @@ class wpdb {
|
|||||||
$wheres[] = "`$field` = {$form}";
|
$wheres[] = "`$field` = {$form}";
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
|
$wheres = empty( $where ) ? '' : ( ' WHERE ' . implode( ' AND ', $wheres ) );
|
||||||
|
|
||||||
|
$sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . $wheres;
|
||||||
return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
|
return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -436,4 +436,21 @@ class Tests_DB extends WP_UnitTestCase {
|
|||||||
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $last ) );
|
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $last ) );
|
||||||
$this->assertEquals( 'Walter Replace Sobchak', $row->display_name );
|
$this->assertEquals( 'Walter Replace Sobchak', $row->display_name );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @ticket 26106
|
||||||
|
*/
|
||||||
|
function test_empty_where() {
|
||||||
|
global $wpdb;
|
||||||
|
$wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array() );
|
||||||
|
|
||||||
|
$expected1 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito'";
|
||||||
|
$this->assertEquals( $expected1, $wpdb->last_query );
|
||||||
|
|
||||||
|
$wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array( 'post_status' => 'taco' ) );
|
||||||
|
|
||||||
|
$expected2 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE `post_status` = 'taco'";
|
||||||
|
$this->assertEquals( $expected2, $wpdb->last_query );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user