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,49 @@
<?php
namespace WPML\TM\ATE\ClonedSites;
class ApiCommunication {
const SITE_CLONED_ERROR = 426;
/**
* @var Lock
*/
private $lock;
/**
* @param Lock $lock
*/
public function __construct( Lock $lock ) {
$this->lock = $lock;
}
public function handleClonedSiteError( $response ) {
if ( self::SITE_CLONED_ERROR === $response['response']['code'] ) {
$parsedResponse = json_decode( $response['body'], true );
if ( isset( $parsedResponse['errors'] ) ) {
$this->handleClonedDetection( $parsedResponse['errors'] );
}
return new \WP_Error( self::SITE_CLONED_ERROR, 'Site Moved or Copied - Action Required' );
}
return $response;
}
public function checkCloneSiteLock() {
if ( Lock::isLocked() ) {
return new \WP_Error( self::SITE_CLONED_ERROR, 'Site Moved or Copied - Action Required - ATE communication locked.' );
}
return null;
}
public function unlockClonedSite() {
return $this->lock->unlock();
}
private function handleClonedDetection( $error_data ) {
$error = array_pop( $error_data );
$this->lock->lock( $error );
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace WPML\TM\ATE\ClonedSites;
class FingerprintGenerator {
const SITE_FINGERPRINT_HEADER = 'SITE-FINGERPRINT';
const NEW_SITE_FINGERPRINT_HEADER = 'NEW-SITE-FINGERPRINT';
public function getSiteFingerprint() {
$siteFingerprint = [
'wp_url' => $this->getSiteUrl(),
];
return json_encode( $siteFingerprint );
}
private function getSiteUrl() {
$siteUrl = defined( 'ATE_CLONED_SITE_URL' ) ? ATE_CLONED_SITE_URL : site_url();
return $this->getDefaultSiteUrl( $siteUrl );
}
private function getDefaultSiteUrl( $siteUrl ) {
global $sitepress;
$filteredSiteUrl = false;
if ( WPML_LANGUAGE_NEGOTIATION_TYPE_DOMAIN === (int) $sitepress->get_setting( 'language_negotiation_type' ) ) {
/* @var WPML_URL_Converter $wpml_url_converter */
global $wpml_url_converter;
$site_url_default_lang = $wpml_url_converter->get_default_site_url();
$filteredSiteUrl = filter_var( $site_url_default_lang, FILTER_SANITIZE_URL );
}
$defaultSiteUrl = $filteredSiteUrl ? $filteredSiteUrl : $siteUrl;
$defaultSiteUrl = defined( 'ATE_CLONED_DEFAULT_SITE_URL' ) ? ATE_CLONED_DEFAULT_SITE_URL : $defaultSiteUrl;
return $defaultSiteUrl;
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace WPML\TM\ATE\ClonedSites;
class Lock {
const CLONED_SITE_OPTION = 'otgs_wpml_tm_ate_cloned_site_lock';
public function lock( $lockData ) {
if ( $this->isLockDataPresent( $lockData ) ) {
update_option(
self::CLONED_SITE_OPTION,
[
'stored_fingerprint' => $lockData['stored_fingerprint'],
'received_fingerprint' => $lockData['received_fingerprint'],
'fingerprint_confirmed' => $lockData['fingerprint_confirmed'],
],
'no'
);
}
}
private function isLockDataPresent( $lockData ) {
if ( isset( $lockData['stored_fingerprint'] )
&& isset( $lockData['received_fingerprint'] )
&& isset( $lockData['fingerprint_confirmed'] ) ) {
return true;
}
return false;
}
public function unlock() {
delete_option( self::CLONED_SITE_OPTION );
}
public static function isLocked() {
return (bool) get_option( self::CLONED_SITE_OPTION, false ) && \WPML_TM_ATE_Status::is_enabled();
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace WPML\TM\ATE\ClonedSites;
use WPML\FP\Fns;
class Report {
const REPORT_TYPE_COPY = 'copy';
const REPORT_TYPE_MOVE = 'move';
/**
* @var \WPML_TM_AMS_API
*/
private $apiClient;
/**
* @var ApiCommunication
*/
private $apiCommunicationHandler;
/**
* @var \WPML_TM_ATE_Job_Repository
*/
private $ateJobsRepository;
/**
* Update jobs synchronisation
*
* @var \WPML_TP_Sync_Update_Job
*/
private $updateJobs;
/**
* @var \WPML_Translation_Job_Factory
*/
private $translationJobFactory;
/**
* @param \WPML_TM_AMS_API $apiClient
* @param ApiCommunication $apiCommunicationHandler
* @param \WPML_TM_ATE_Job_Repository $ateJobsRepository
* @param \WPML_Translation_Job_Factory $translationJobFactory
*/
public function __construct(
\WPML_TM_AMS_API $apiClient,
ApiCommunication $apiCommunicationHandler,
\WPML_TM_ATE_Job_Repository $ateJobsRepository,
\WPML_TP_Sync_Update_Job $updateJobs,
\WPML_Translation_Job_Factory $translationJobFactory
) {
$this->apiClient = $apiClient;
$this->apiCommunicationHandler = $apiCommunicationHandler;
$this->ateJobsRepository = $ateJobsRepository;
$this->updateJobs = $updateJobs;
$this->translationJobFactory = $translationJobFactory;
}
/**
* @param string $reportType
*
* @return bool
*/
public function report( $reportType ) {
$reportCallback = \wpml_collect( [
self::REPORT_TYPE_COPY => $this->reportCopiedSite(),
self::REPORT_TYPE_MOVE => $this->reportMovedSite(),
] )->get( $reportType, Fns::always( Fns::always( false ) ) );
$reportResult = $reportCallback();
if ($reportResult) {
do_action( 'wpml_tm_ate_synchronize_translators' );
}
return $reportResult;
}
private function reportCopiedSite() {
return function () {
$reportResult = $this->apiClient->reportCopiedSite();
$isConfirmed = $this->apiClient->processCopyReportConfirmation( $reportResult );
if ( $isConfirmed ) {
$jobsInProgress = $this->ateJobsRepository->get_jobs_to_sync();
/** @var \WPML_TM_Post_Job_Entity $jobInProgress */
foreach ( $jobsInProgress as $jobInProgress ) {
$jobInProgress->set_status( ICL_TM_NOT_TRANSLATED );
$this->updateJobs->update_state( $jobInProgress );
$this->translationJobFactory->delete_job_data( $jobInProgress->get_translate_job_id() );
}
$this->apiCommunicationHandler->unlockClonedSite();
}
return $isConfirmed;
};
}
private function reportMovedSite() {
return function () {
$reportResult = $this->apiClient->reportMovedSite();
$movedSuccessfully = $this->apiClient->processMoveReport( $reportResult );
if ( $movedSuccessfully ) {
$this->apiCommunicationHandler->unlockClonedSite();
}
return $movedSuccessfully;
};
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace WPML\TM\ATE\ClonedSites;
class ReportAjax implements \IWPML_Backend_Action, \IWPML_DIC_Action {
/**
* @var Report
*/
private $reportHandler;
/**
* @param Report $reportHandler
*/
public function __construct( Report $reportHandler ) {
$this->reportHandler = $reportHandler;
}
public function add_hooks() {
add_action( 'wp_ajax_wpml_save_cloned_sites_report_type', [ $this, 'reportSiteCloned' ] );
}
public function reportSiteCloned() {
if ( $this->isValidRequest() && $this->reportHandler->report( $_POST['reportType'] ) ) {
wp_send_json_success();
} else {
wp_send_json_error();
}
}
private function isValidRequest() {
return array_key_exists( 'nonce', $_POST )
&& array_key_exists( 'reportType', $_POST )
&& wp_verify_nonce( $_POST['nonce'], 'icl_doc_translation_method_cloned_nonce' );
}
}