class-wpml-tm-troubleshooting-reset-pro-trans-config.php
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
use WPML\API\Sanitize;
class WPML_TM_Troubleshooting_Reset_Pro_Trans_Config extends WPML_TM_AJAX_Factory_Obsolete {
const SCRIPT_HANDLE = 'wpml_reset_pro_trans_config';
/**
* Wpdb Object to perform DB queries.
*
* @var wpdb $wpdb
*/
private $wpdb;
/**
* SitePress object - superclass for all WPML classes using the global wpdb object.
*
* @var SitePress
*/
private $sitepress;
/**
* Translation Proxy service.
*
* @var WPML_Translation_Proxy_API $translation_proxy
*/
private $translation_proxy;
/**
* WPML_TM_Troubleshooting_Clear_TS constructor.
*
* @param SitePress $sitepress SitePress object.
* @param WPML_Translation_Proxy_API $translation_proxy Translation Proxy service.
* @param WPML_WP_API $wpml_wp_api WPML WordPress API wrapper.
* @param wpdb $wpdb Wpdb Object to perform DB queries.
*/
public function __construct( &$sitepress, &$translation_proxy, &$wpml_wp_api, &$wpdb ) {
parent::__construct( $wpml_wp_api );
$this->sitepress = &$sitepress;
$this->translation_proxy = &$translation_proxy;
$this->wpdb = &$wpdb;
add_action( 'init', array( $this, 'load_action' ) );
$this->add_ajax_action( 'wp_ajax_wpml_reset_pro_trans_config', array( $this, 'reset_pro_translation_configuration_action' ) );
$this->init();
}
/**
* Loading actions.
*/
public function load_action() {
$page = Sanitize::stringProp( 'page', $_GET );
$should_proceed = SitePress_Setup::setup_complete()
&& ! $this->wpml_wp_api->is_heartbeat()
&& ! $this->wpml_wp_api->is_ajax()
&& ! $this->wpml_wp_api->is_cron_job()
&& $page
&& strpos( $page, 'sitepress-multilingual-cms/menu/troubleshooting.php' ) === 0;
if ( $should_proceed ) {
$this->add_hooks();
}
}
/**
* Adding WP hooks.
*/
private function add_hooks() {
add_action( 'after_setup_complete_troubleshooting_functions', array( $this, 'render_ui' ) );
}
/**
* Registering WP scripts.
*/
public function register_resources() {
wp_register_script( self::SCRIPT_HANDLE, WPML_TM_URL . '/res/js/reset-pro-trans-config.js', array( 'jquery', 'jquery-ui-dialog' ), false, true );
}
/**
* Enqueue WordPress resources.
*
* @param string $hook_suffix Hook suffix.
*/
public function enqueue_resources( $hook_suffix ) {
if ( $this->wpml_wp_api->is_troubleshooting_page() ) {
$this->register_resources();
$translation_service_name = $this->translation_proxy->get_current_service_name();
$strings = array(
'placeHolder' => 'icl_reset_pro',
'reset' => wp_create_nonce( 'reset_pro_translation_configuration' ),
/* translators: Reset professional translation state confirmation ("%1$s" is the service name) */
'confirmation' => sprintf( __( 'Are you sure you want to reset the %1$s translation process?', 'wpml-translation-management' ), $translation_service_name ),
'action' => self::SCRIPT_HANDLE,
'nonce' => wp_create_nonce( self::SCRIPT_HANDLE ),
);
wp_localize_script( self::SCRIPT_HANDLE, self::SCRIPT_HANDLE . '_strings', $strings );
wp_enqueue_script( self::SCRIPT_HANDLE );
}
}
/**
* Rendering user interface.
*/
public function render_ui() {
$clear_ts_factory = new WPML_TM_Troubleshooting_Reset_Pro_Trans_Config_UI_Factory();
$clear_ts = $clear_ts_factory->create();
// phpcs:disable WordPress.XSS.EscapeOutput.OutputNotEscaped
echo $clear_ts->show();
// phpcs:enable WordPress.XSS.EscapeOutput.OutputNotEscaped
}
/**
* Resetting professional translation configuration action.
*
* @return array|null
*/
public function reset_pro_translation_configuration_action() {
$action = Sanitize::stringProp( 'action', $_POST );
$nonce = Sanitize::stringProp( 'nonce', $_POST );
$valid_nonce = $nonce && $action && wp_verify_nonce( $nonce, $action );
if ( $valid_nonce ) {
return $this->wpml_wp_api->wp_send_json_success( $this->reset_pro_translation_configuration() );
} else {
return $this->wpml_wp_api->wp_send_json_error( __( "You can't do that!", 'wpml-translation-management' ) );
}
}
/**
* Implementation of core class functionality - resetting professional translation configuration.
*
* @return string
*/
public function reset_pro_translation_configuration() {
$translation_service_name = $this->translation_proxy->get_current_service_name();
$this->sitepress->set_setting( 'content_translation_languages_setup', false );
$this->sitepress->set_setting( 'content_translation_setup_complete', false );
$this->sitepress->set_setting( 'content_translation_setup_wizard_step', false );
$this->sitepress->set_setting( 'translator_choice', false );
$this->sitepress->set_setting( 'icl_lang_status', false );
$this->sitepress->set_setting( 'icl_balance', false );
$this->sitepress->set_setting( 'icl_support_ticket_id', false );
$this->sitepress->set_setting( 'icl_current_session', false );
$this->sitepress->set_setting( 'last_get_translator_status_call', false );
$this->sitepress->set_setting( 'last_icl_reminder_fetch', false );
$this->sitepress->set_setting( 'icl_account_email', false );
$this->sitepress->set_setting( 'translators_management_info', false );
$this->sitepress->set_setting( 'site_id', false );
$this->sitepress->set_setting( 'access_key', false );
$this->sitepress->set_setting( 'ts_site_id', false );
$this->sitepress->set_setting( 'ts_access_key', false );
if ( class_exists( 'TranslationProxy_Basket' ) ) {
// Cleaning the basket.
TranslationProxy_Basket::delete_all_items_from_basket();
}
global $wpdb;
$sql_for_remote_rids = $wpdb->prepare(
"FROM {$wpdb->prefix}icl_translation_status
WHERE translation_service != 'local'
AND translation_service != 0
AND status IN ( %d, %d )",
ICL_TM_WAITING_FOR_TRANSLATOR,
ICL_TM_IN_PROGRESS
);
// phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
// Delete all translation service jobs with status "waiting for translator" or "in progress".
$wpdb->query( "DELETE FROM {$wpdb->prefix}icl_translate_job WHERE rid IN (SELECT rid {$sql_for_remote_rids})" );
// Delete all translation statuses with status "waiting for translator" or "in progress".
$wpdb->query( "DELETE {$sql_for_remote_rids}" );
// phpcs:enable WordPress.WP.PreparedSQL.NotPrepared
// Cleaning up Translation Proxy settings.
$this->sitepress->set_setting( 'icl_html_status', false );
$this->sitepress->set_setting( 'language_pairs', false );
if ( ! $this->translation_proxy->has_preferred_translation_service() ) {
$this->sitepress->set_setting( 'translation_service', false );
$this->sitepress->set_setting( 'icl_translation_projects', false );
}
$this->sitepress->save_settings();
$this->wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}icl_core_status" );
$this->wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}icl_content_status" );
$this->wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}icl_string_status" );
$this->wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}icl_node" );
$this->wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}icl_reminders" );
if ( $this->translation_proxy->has_preferred_translation_service() && $translation_service_name ) {
/* translators: Confirmation that translation process was reset ("%1$s" is the service name) */
$confirm_message = sprintf( __( 'The translation process with %1$s was reset.', 'wpml-translation-management' ), $translation_service_name );
} elseif ( $translation_service_name ) {
/* translators: Confirmation that site has been disconnected from translation service ("%1$s" is the service name) */
$confirm_message = sprintf( __( 'Your site was successfully disconnected from %1$s. Go to the translators tab to connect a new %1$s account or use a different translation service.', 'wpml-translation-management' ), $translation_service_name );
} else {
$confirm_message = __( 'PRO translation has been reset.', 'wpml-translation-management' );
}
return $confirm_message;
}
}