Skip to content

Commit

Permalink
Issue ThemeFuse/Unyson-Extensions-Approval - fix for wrong character set
Browse files Browse the repository at this point in the history
  • Loading branch information
Viorel committed Jul 2, 2018
1 parent ee17a67 commit ab4f5b1
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function get_db_collations() {
* @return string
*/
private function get_db_field( $sql, $field ) {
return preg_match( "/({$field}=)([^\s]+)/i", $sql, $matches ) && isset( $matches[2] ) ? $matches[2] : '';
return preg_match( "/({$field})(=)?(\s)?([^\s\"]+)/i", $sql, $matches ) && isset( $matches[4] ) ? $matches[4] : '';
}

/**
Expand Down Expand Up @@ -760,7 +760,24 @@ private function do_import(array $args, array $state) {
$not_exists_charset_collate = ! isset( $collations[ $collate ] ) || ! array_search( $charset, $collations );

if ( $is_invalid_charset || $not_exists_charset_collate ) {
$sql = str_replace( array( $collate, $charset ), array( 'utf8_general_ci', 'utf8' ), $sql );

$std_collate = 'utf8_general_ci';
$std_character_set = 'utf8';
$std_charset = 'utf8';

if ( isset( $collations['utf8mb4_general_ci'] ) ) {
$std_collate = 'utf8mb4_general_ci';
$std_character_set = 'utf8mb4';
$std_charset = $collations['utf8mb4_general_ci'];
}

$character_set = $this->get_db_field( $sql, 'CHARACTER SET' );

$sql = str_replace( $collate, $std_collate, preg_replace( "/(CHARSET)(=)?(\s)?([^\s\"]+)/i", "$1$2{$std_charset}", $sql ) );

if ( $character_set ) {
$sql = preg_replace("/(CHARACTER SET)(=)?(\s)?([^\s\"]+)/i", "$1$2 {$std_character_set}", $sql);
}
}

$query = $wpdb->query( $sql );
Expand Down

0 comments on commit ab4f5b1

Please # to comment.