From 220fda00958e074f14aa64942f75b81c5f41c17c Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 24 Jun 2014 00:23:09 +0000 Subject: [PATCH] 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 --- src/wp-includes/wp-db.php | 4 +++- tests/phpunit/tests/db.php | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 7a09d62a9e..7366172101 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -1785,7 +1785,9 @@ class wpdb { $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 ) ) ) ); } diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index 39a1f11339..19db983075 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -436,4 +436,21 @@ class Tests_DB extends WP_UnitTestCase { $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $last ) ); $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 ); + } }