first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace WPML\TM\Troubleshooting;
use WPML\API\Sanitize;
class ResetPreferredTranslationService implements \IWPML_Backend_Action {
const ACTION_ID = 'wpml-tm-reset-preferred-translation-service';
public function add_hooks() {
add_action( 'after_setup_complete_troubleshooting_functions', [ $this, 'displayButton' ], 11 );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ] );
add_action( 'wp_ajax_' . self::ACTION_ID, [ $this, 'resetAndFetchPreferredTS' ] );
}
public function displayButton() {
$resetTitle = sprintf( __( 'Reset preferred translation service', 'wpml-translation-manager' ) );
$resetMessage = sprintf( __( 'Reset and fetch local preferred translation service to use Preferred Translation Service configured on WPML.org account.', 'wpml-translation-manager' ) );
$resetButton = sprintf( __( 'Reset & Fetch', 'wpml-translation-manager' ) );
$html = '<div class="icl_cyan_box" id="wpml_tm_reset_preferred_translation_service_btn">' .
wp_nonce_field( self::ACTION_ID, 'wpml_tm_reset_preferred_translation_service_nonce' ) .
'<h3>' . $resetTitle . '</h3>
<p>' . $resetMessage . '</p>
<a class="button-primary" href="#">' . $resetButton . '</a><span class="spinner"></span>
</div>';
echo $html;
}
public function resetAndFetchPreferredTS() {
$action = Sanitize::stringProp( 'action', $_POST );
$nonce = Sanitize::stringProp( 'nonce', $_POST );
if ( wp_verify_nonce( $nonce, $action ) ) {
OTGS_Installer()->settings['repositories']['wpml']['ts_info']['preferred'] = null;
OTGS_Installer()->refresh_subscriptions_data();
wp_send_json_success();
} else {
wp_send_json_error();
}
}
public function enqueueScripts( $page ) {
if ( WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php' === $page ) {
wp_enqueue_script(
self::ACTION_ID,
WPML_TM_URL . '/res/js/reset-preferred-ts.js',
[ 'jquery' ],
WPML_TM_VERSION
);
}
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace WPML\TM\Troubleshooting\SynchronizeSourceIdOfATEJobs;
use WPML\TM\Upgrade\Commands\SynchronizeSourceIdOfATEJobs\Command;
use WPML\Upgrade\CommandsStatus;
class TriggerSynchronization implements \IWPML_Backend_Action, \IWPML_DIC_Action {
const ACTION_ID = 'wpml-tm-ate-source-id-migration';
/** @var CommandsStatus */
private $commandStatus;
/**
* @param CommandsStatus $commandStatus
*/
public function __construct( CommandsStatus $commandStatus ) {
$this->commandStatus = $commandStatus;
}
public function add_hooks() {
add_action( 'wpml_troubleshooting_after_fix_element_type_collation', [ $this, 'displayButton' ] );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueueScripts' ) );
add_action( 'wp_ajax_' . self::ACTION_ID, array( $this, 'clearExecutedStateToForceUpgrade' ) );
}
public function displayButton() {
?>
<p>
<input id="wpml_tm_ate_source_id_migration_btn"
type="button"
class="button-secondary"
value="<?php esc_attr_e( 'Synchronize local job ids with ATE jobs', 'wpml-translation-manager' ); ?>"
data-action="<?php echo self::ACTION_ID; ?>"
data-nonce="<?php echo wp_create_nonce( self::ACTION_ID ); ?>"
/>
<br/>
<small style="margin-left:10px;">
<?php
esc_attr_e(
'Synchronize local job ids with their ATE counterparts. You will have to refresh a few times any admin page to accomplish the process.',
'wpml-translation-manager'
)
?>
</small>
</p>
<?php
}
public function enqueueScripts( $hook ) {
if ( WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php' === $hook ) {
wp_enqueue_script(
self::ACTION_ID,
WPML_TM_URL . '/res/js/ate-jobs-migration.js',
[ 'jquery' ],
WPML_TM_VERSION
);
}
}
public function clearExecutedStateToForceUpgrade() {
$this->commandStatus->markAsExecuted( Command::class, false );
wp_send_json_success();
}
}

View File

@@ -0,0 +1,27 @@
<?php
class WPML_TM_Troubleshooting_Reset_Pro_Trans_Config_UI_Factory {
/**
* Sets template base directory.
*/
private function get_template_base_dir() {
return array(
WPML_TM_PATH . '/templates/troubleshooting',
);
}
/**
* Creates WPML_TM_Troubleshooting_Reset_Pro_Trans_Config_UI instance
*
* @return WPML_TM_Troubleshooting_Reset_Pro_Trans_Config_UI
*/
public function create() {
$template_paths = $this->get_template_base_dir();
$template_loader = new WPML_Twig_Template_Loader( $template_paths );
$template_service = $template_loader->get_template();
return new WPML_TM_Troubleshooting_Reset_Pro_Trans_Config_UI( $template_service );
}
}

View File

@@ -0,0 +1,66 @@
<?php
class WPML_TM_Troubleshooting_Reset_Pro_Trans_Config_UI {
const TROUBLESHOOTING_RESET_PRO_TRANS_TEMPLATE = 'reset-pro-trans-config.twig';
/**
* Template service.
*
* @var IWPML_Template_Service
*/
private $template_service;
/**
* WPML_TM_Troubleshooting_Reset_Pro_Trans_Config_UI constructor.
*
* @param IWPML_Template_Service $template_service WPML_Twig_Template twig service.
*/
public function __construct( IWPML_Template_Service $template_service ) {
$this->template_service = $template_service;
}
/**
* Returns of template service render result.
*
* @return string
*/
public function show() {
return $this->template_service->show( $this->get_model(), self::TROUBLESHOOTING_RESET_PRO_TRANS_TEMPLATE );
}
/**
* Returns model array for Troubleshooting Reset Pro Trans.
*
* @return array
*/
private function get_model() {
$translation_service_name = TranslationProxy::get_current_service_name();
if ( ! $translation_service_name ) {
$translation_service_name = 'PRO';
$alert_2 = __( 'Only select this option if you have no pending jobs or you are sure of what you are doing.', 'wpml-translation-management' );
} else {
if ( ! TranslationProxy::has_preferred_translation_service() ) {
/* translators: Reset professional translation state ("%1$s" is the service name) */
$alert_2 = sprintf( __( 'If you have sent content to %1$s, you should cancel the projects in %1$s system. Any work that completes after you do this reset cannot be received by your site.', 'wpml-translation-management' ), $translation_service_name );
} else {
$alert_2 = __( 'Any work that completes after you do this reset cannot be received by your site.', 'wpml-translation-management' );
}
}
$model = array(
'strings' => array(
'title' => __( 'Reset professional translation state', 'wpml-translation-management' ),
'alert1' => __( 'Use this feature when you want to reset your translation process. All your existing translations will remain unchanged. Any translation work that is currently in progress will be stopped.', 'wpml-translation-management' ),
'alert2' => $alert_2,
/* translators: Reset professional translation state ("%1$s" is the service name) */
'checkBoxLabel' => sprintf( __( 'I am about to stop any ongoing work done by %1$s.', 'wpml-translation-management' ), $translation_service_name ),
'button' => __( 'Reset professional translation state', 'wpml-translation-management' ),
),
'placeHolder' => 'icl_reset_pro',
);
return $model;
}
}

View File

@@ -0,0 +1,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 = wp_verify_nonce( $nonce, $action );
if ( $valid_nonce && $_POST ) {
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;
}
}