wpdb = $wpdb;
$this->schema = $schema;
}
public function add_hooks() {
add_action(
'wpml_troubleshooting_after_fix_element_type_collation',
array(
$this,
'render_troubleshooting_button',
)
);
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'fix_collate_ajax' ) );
add_action( 'upgrader_process_complete', array( $this, 'fix_collate' ), PHP_INT_MAX );
}
public function fix_collate_ajax() {
if ( isset( $_POST['nonce'] )
&& wp_verify_nonce( $_POST['nonce'], self::AJAX_ACTION )
) {
$this->fix_collate();
wp_send_json_success();
}
}
public function render_troubleshooting_button() {
?>
1 ) {
return;
}
$wp_default_table_data = $this->wpdb->get_row(
$this->wpdb->prepare( 'SHOW TABLE status LIKE %s', $this->wpdb->posts )
);
if ( isset( $wp_default_table_data->Collation ) ) {
$charset = $this->schema->get_default_charset();
foreach ( $this->get_all_wpml_tables() as $table ) {
$table = reset( $table );
$table_data = $this->wpdb->get_row(
$this->wpdb->prepare( 'SHOW TABLE status LIKE %s', $table )
);
if ( isset( $table_data->Collation ) && $table_data->Collation !== $wp_default_table_data->Collation ) {
$this->wpdb->query(
$this->wpdb->prepare(
'ALTER TABLE ' . $table . ' CONVERT TO CHARACTER SET %s COLLATE %s',
$charset,
$wp_default_table_data->Collation
)
);
}
}
}
}
/**
* @return array
*/
private function get_all_wpml_tables() {
return $this->wpdb->get_results(
$this->wpdb->prepare(
'SHOW TABLES LIKE %s',
$this->wpdb->prefix . 'icl_%'
),
ARRAY_A
);
}
}