wpml-troubleshooting-terms-menu.class.php
4.92 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
use WPML\API\Sanitize;
class WPML_Troubleshooting_Terms_Menu {
/**
* Displays the admin notice informing about terms in the old format, using the language suffix.
* The notice is displayed until it is either dismissed or the update button is pressed.
*/
public static function display_terms_with_suffix_admin_notice() {
global $sitepress;
if ( ! $sitepress->get_setting( 'taxonomy_names_checked' ) ) {
$suffix_count = count( WPML_Terms_Translations::get_all_terms_with_language_suffix() );
if ( $suffix_count > 0 ) {
$message = '<p>';
$message .= sprintf( __( 'In this version of WPML, you can give your taxonomy terms the same name across multiple languages. You need to update %d taxonomy terms on your website so that they display the same name without any language suffixes.', 'sitepress' ), $suffix_count );
$message .= '</p>';
if ( defined( 'ICL_PLUGIN_URL' ) ) {
$message .= '<p><a href="' . admin_url( 'admin.php?page=' . WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php#termsuffixupdate' ) . '"><button class="button-primary">Open terms update page</button></a>';
}
ICL_AdminNotifier::addMessage( 'termssuffixnotice', $message, 'error', true, false, false, 'terms-suffix', true );
}
$sitepress->set_setting( 'taxonomy_names_checked', true, true );
}
// TODO: [WPML 3.3] the ICL_AdminNotifier class got improved and we should not call \ICL_AdminNotifier::displayMessages to display an admin notice
ICL_AdminNotifier::displayMessages( 'terms-suffix' );
}
/**
* Returns the HTML for the display of all terms with a language suffix in the troubleshooting menu.
*
* @return string
*/
public static function display_terms_with_suffix() {
$terms_to_display = WPML_Terms_Translations::get_all_terms_with_language_suffix();
$output = '';
if ( ! empty( $terms_to_display ) ) {
$output = '<div class="icl_cyan_box">';
$output .= '<table class="widefat" id="icl-updated-term-names-table">';
$output .= '<a name="termsuffixupdate"></a>';
$output .= '<tr><h3>' . __( 'Remove language suffixes from taxonomy names.', 'sitepress' ) . '</h3></tr>';
$output .= '<tr id="icl-updated-term-names-headings"><th></th><th>' . __( 'Old Name', 'sitepress' ) . '</th><th>' . __( 'Updated Name', 'sitepress' ) . '</th><th>' . __( 'Affected Taxonomies', 'sitepress' ) . '</th></tr>';
foreach ( $terms_to_display as $term_id => $term ) {
$updated_term_name = self::strip_language_suffix( $term['name'] );
$output .= '<tr class="icl-term-with-suffix-row"><td>';
$output .= '<input type="checkbox" checked="checked" name="' . $updated_term_name . '" value="' . $term_id . '"/>';
$output .= '</td>';
$output .= '<td>' . $term['name'] . '</td>';
$output .= '<td id="term_' . $term_id . '">' . $updated_term_name . '</td>';
$output .= '<td>' . join( ', ', $term['taxonomies'] ) . '</td>';
$output .= '</tr>';
}
$output .= '</table>';
$output .= '</br></br>';
$output .= '<button id="icl-update-term-names" class="button-primary">' . __( 'Update term names', 'sitepress' ) . '</button>';
$output .= '<button id="icl-update-term-names-done" class="button-primary" disabled="disabled" style="display:none;">' . __( 'All term names updated', 'sitepress' ) . '</button>';
$output .= '</div>';
}
return $output;
}
/**
* @param string $term_name
* Strips a term off all language suffixes in the form @<lang_code> on it.
*
* @return string
*/
public static function strip_language_suffix( $term_name ) {
global $wpdb;
$lang_codes = $wpdb->get_col( "SELECT code FROM {$wpdb->prefix}icl_languages" );
$new_name_parts = explode( ' @', $term_name );
$new_name_parts = array_filter( $new_name_parts );
$last_part = array_pop( $new_name_parts );
while ( in_array( $last_part, $lang_codes ) ) {
$last_part = array_pop( $new_name_parts );
}
$new_name = '';
if ( ! empty( $new_name_parts ) ) {
$new_name = join( ' @', $new_name_parts ) . ' @';
}
$new_name .= $last_part;
return $new_name;
}
/**
* Ajax handler for the troubleshoot page. Updates the term name on those terms given via the Ajax action.
*/
public static function wpml_update_term_names_troubleshoot() {
global $wpdb;
ICL_AdminNotifier::removeMessage( 'termssuffixnotice' );
$term_names = array();
$nonce = Sanitize::stringProp( '_icl_nonce', $_POST );
if ( ! $nonce || ! wp_verify_nonce( $nonce, 'update_term_names_nonce' ) ) {
die( 'Wrong Nonce' );
}
$request_post_terms = Sanitize::stringProp( 'terms', $_POST );
if ( $request_post_terms ) {
$term_names = json_decode( stripcslashes( $request_post_terms ) );
if ( ! is_object( $term_names ) ) {
$term_names = array();
}
}
$updated = array();
foreach ( $term_names as $term_id => $new_name ) {
$res = $wpdb->update( $wpdb->terms, array( 'name' => $new_name ), array( 'term_id' => $term_id ) );
if ( $res ) {
$updated[] = $term_id;
}
}
wp_send_json_success( $updated );
}
}