diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 1356a76163..f0ed7d9d34 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -3093,6 +3093,32 @@ class wpdb { wp_die($message); } + + /** + * Closes the current database connection. + * + * @since 4.5.0 + * + * @return bool True if the connection was succesfully closed, false if it wasn't, or the connection doesn't exist. + */ + public function close() { + if ( ! $this->dbh ) { + return false; + } + + if ( $this->use_mysqli ) { + $closed = mysqli_close( $this->dbh ); + } else { + $closed = mysql_close( $this->dbh ); + } + + if ( $closed ) { + $this->dbh = null; + } + + return $closed; + } + /** * Whether MySQL database is at least the required minimum version. * diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index 74036a2027..e74ca308d2 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -941,4 +941,20 @@ class Tests_DB extends WP_UnitTestCase { $this->assertNull( $row ); } + + /** + * @ticket 34903 + */ + function test_close() { + global $wpdb; + + $this->assertTrue( $wpdb->close() ); + $this->assertFalse( $wpdb->close() ); + + $wpdb->check_connection(); + + $this->assertTrue( $wpdb->close() ); + + $wpdb->check_connection(); + } }