Skip to content
61 changes: 24 additions & 37 deletions admin/class-boldgrid-backup-admin-db-dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ public function dump( $file ) {
return true;
}

/**
* Fetch MySQL port number from global DB variables.
*/
public function get_db_port() {
if ( ! isset( $wpdb ) ) {
global $wpdb;
}

return $wpdb->get_row( "SHOW GLOBAL VARIABLES LIKE 'PORT'" )->Value;
}

/**
* Get our PDO DSN connection string.
*
Expand All @@ -152,57 +163,33 @@ public function dump( $file ) {
* @return string
*/
public function get_connection_string( $db_host = null, $db_name = null ) {
$params = array();
global $wpdb;

// Configure parameters passed in.
$db_name = empty( $db_name ) ? DB_NAME : $db_name;
$db_host = empty( $db_host ) ? DB_HOST : $db_host;
$db_host = explode( ':', $db_host );

// Parse info and get hostname, port, and socket. Not all required. See comments below.
switch ( count( $db_host ) ) {
/*
* Examples:
*
* # localhost
* # /var/lib/mysql/mysql.sock
*/
case 1:
$has_socket = 'sock' === pathinfo( $db_host[0], PATHINFO_EXTENSION );

if ( $has_socket ) {
$params['unix_socket'] = $db_host[0];
} else {
$params['host'] = $db_host[0];
}

break;
/*
* Examples:
*
* # localhost:/var/lib/mysql/mysql.sock
* # localhost:3306
*/
case 2:
$has_socket = 'sock' === pathinfo( $db_host[1], PATHINFO_EXTENSION );
$has_port = is_numeric( $db_host[1] );
$params = array();

$params['host'] = $db_host[0];
$db_host_params = $wpdb->parse_db_host( $db_host );

if ( $db_host_params[0] ) {
$params['host'] = $db_host_params[0];
}

if ( $has_socket ) {
$params['unix_socket'] = $db_host[1];
} elseif ( $has_port ) {
$params['port'] = $db_host[1];
}
if ( $db_host_params[0] && ! $db_host_params[2] ) {
$params['port'] = $this->get_db_port();
}

break;
if ( $db_host_params[2] ) {
$params['unix-socket'] = $db_host_params[2];
}

$connection_string = 'mysql:';
foreach ( $params as $key => $value ) {
$connection_string .= $key . '=' . $value . ';';
}
$connection_string .= 'dbname=' . $db_name;
$connection_string .= 'dbname=' . $db_name . ';';

return $connection_string;
}
Expand Down
7 changes: 5 additions & 2 deletions admin/class-boldgrid-backup-admin-db-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ public function import_lines( $lines ) {
return false;
}

/* phpcs:disable WordPress.DB.RestrictedClasses */
$db = new PDO( sprintf( 'mysql:host=%1$s;dbname=%2$s;', DB_HOST, DB_NAME ), DB_USER, DB_PASSWORD );
$core = apply_filters( 'boldgrid_backup_get_core', null );

$db_connection_string = Boldgrid_Backup_Admin_Db_Dump::get_connection_string();

$db = new PDO( $db_connection_string, DB_USER, DB_PASSWORD );

$templine = '';

Expand Down
2 changes: 1 addition & 1 deletion admin/class-boldgrid-backup-admin-restore-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function post_restore( $info ) {

// Determine if the file was changed during restoration.
$post_sha1 = sha1_file( $original );
$has_changed = $this->monitors_files[ $key ]['pre_sha1'] !== $post_sha1;
$has_changed = $this->monitor_files[ $key ]['pre_sha1'] !== $post_sha1;

if ( $has_changed ) {
/**
Expand Down