class-wpml-table-collate-fix.php
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
class WPML_Table_Collate_Fix implements IWPML_AJAX_Action, IWPML_Backend_Action, IWPML_DIC_Action {
const AJAX_ACTION = 'fix_tables_collation';
/**
* @var wpdb
*/
private $wpdb;
/** @var WPML_Upgrade_Schema $schema */
private $schema;
public function __construct( wpdb $wpdb, WPML_Upgrade_Schema $schema ) {
$this->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() {
?>
<p>
<input id="wpml_fix_tables_collation" type="button" class="button-secondary"
value="<?php _e( 'Fix WPML tables collation', 'sitepress' ); ?>"/><br/>
<?php wp_nonce_field( self::AJAX_ACTION, 'wpml-fix-tables-collation-nonce' ); ?>
<small style="margin-left:10px;"><?php esc_attr_e( 'Fixes the collation of WPML tables in order to match the collation of default WP tables.', 'sitepress' ); ?></small>
</p>
<?php
}
public function enqueue_scripts( $hook ) {
if ( WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php' === $hook ) {
wp_enqueue_script( 'wpml-fix-tables-collation', ICL_PLUGIN_URL . '/res/js/fix-tables-collation.js', array( 'jquery' ), ICL_SITEPRESS_VERSION );
}
}
public function fix_collate() {
if ( did_action( 'upgrader_process_complete' ) > 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
);
}
}