Make WordPress Core

Changeset 28814


Ignore:
Timestamp:
06/24/2014 12:23:09 AM (10 years ago)
Author:
wonderboymusic
Message:

In $wpdb->update(), prevent explosions when $where is empty.

Adds unit tests.

Props UmeshSingla, wonderboymusic.
Fixes #26106

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r28711 r28814  
    17861786        }
    17871787
    1788         $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
     1788        $wheres = empty( $where ) ? '' : ( ' WHERE ' . implode( ' AND ', $wheres ) );
     1789
     1790        $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . $wheres;
    17891791        return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    17901792    }
  • trunk/tests/phpunit/tests/db.php

    r28711 r28814  
    437437        $this->assertEquals( 'Walter Replace Sobchak', $row->display_name );
    438438    }
     439
     440    /**
     441     *
     442     * @ticket 26106
     443     */
     444    function test_empty_where() {
     445        global $wpdb;
     446        $wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array() );
     447
     448        $expected1 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito'";
     449        $this->assertEquals( $expected1, $wpdb->last_query );
     450
     451        $wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array( 'post_status' => 'taco' ) );
     452
     453        $expected2 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE `post_status` = 'taco'";
     454        $this->assertEquals( $expected2, $wpdb->last_query );
     455    }
    439456}
Note: See TracChangeset for help on using the changeset viewer.