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,46 @@
<?php
namespace WPML\TM\ATE\API\CacheStorage;
use WPML\FP\Obj;
class StaticVariable implements Storage {
/** @var array */
private static $cache = [];
/** @var self */
private static $instance;
public static function getInstance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get( $key, $default = null ) {
return Obj::propOr( $default, $key, self::$cache );
}
/**
* @param string $key
* @param mixed $value
*/
public function save( $key, $value ) {
self::$cache[ $key ] = $value;
}
/**
* @param string $key
*/
public function delete( $key ) {
self::$cache = [];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace WPML\TM\ATE\API\CacheStorage;
interface Storage {
/**
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get( $key, $default = null );
/**
* @param string $key
* @param mixed $value
*/
public function save( $key, $value );
/**
* @param string $key
*/
public function delete( $key );
}

View File

@@ -0,0 +1,34 @@
<?php
namespace WPML\TM\ATE\API\CacheStorage;
use WPML\LIB\WP\Transient as WPTransient;
class Transient implements Storage {
/**
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get( $key, $default = null ) {
return WPTransient::getOr( $key, $default );
}
/**
* @param string $key
* @param mixed $value
*/
public function save( $key, $value ) {
WPTransient::set( $key, $value, 3600 * 24 );
}
/**
* @param string $key
*/
public function delete( $key ) {
WPTransient::delete( $key );
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace WPML\TM\ATE\API;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\LIB\WP\Transient;
use WPML\FP\Obj;
use WPML\TM\ATE\API\CacheStorage\Storage;
use function WPML\FP\curryN;
class CachedATEAPI {
const CACHE_OPTION = 'wpml-tm-ate-api-cache';
/** @var \WPML_TM_ATE_API */
private $ateAPI;
/** @var Storage */
private $storage;
private $cachedFns = [ 'get_languages_supported_by_automatic_translations', 'get_language_details', 'get_language_mapping' ];
/**
* @param \WPML_TM_ATE_API $ateAPI
*/
public function __construct( \WPML_TM_ATE_API $ateAPI, Storage $storage ) {
$this->ateAPI = $ateAPI;
$this->storage = $storage;
}
public function __call( $name, $args ) {
return Lst::includes( $name, $this->cachedFns ) ? $this->callWithCache( $name, $args ) : call_user_func_array( [ $this->ateAPI, $name ], $args );
}
private function callWithCache( $fnName, $args ) {
$result = Obj::pathOr( null, [ $fnName, \serialize( $args ) ], $this->storage->get( self::CACHE_OPTION, [] ) );
if ( ! $result ) {
return call_user_func_array( [ $this->ateAPI, $fnName ], $args )->map( $this->cacheValue( $fnName, $args ) );
}
return Maybe::of( $result );
}
public function cacheValue( $fnName = null, $args = null, $result = null ) {
$fn = curryN( 3, function ( $fnName, $args, $result ) {
$data = $this->storage->get( self::CACHE_OPTION, [] );
$data[ $fnName ][ serialize( $args ) ] = $result;
$this->storage->save( self::CACHE_OPTION, $data );
return $result;
} );
return call_user_func_array( $fn, func_get_args() );
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace WPML\TM\ATE\ClonedSites;
use WPML\UIPage;
class ApiCommunication {
const SITE_CLONED_ERROR = 426;
const SITE_MOVED_OR_COPIED_MESSAGE = "WPML has detected a change in your site's URL. To continue translating your site, go to your <a href='%s'>WordPress Dashboard</a> and tell WPML if your site has been <a href='%s'>moved or copied</a>.";
const SITE_MOVED_OR_COPIED_DOCS_URL = 'https://wpml.org/documentation/translating-your-contents/advanced-translation-editor/using-advanced-translation-editor-when-you-move-or-use-a-copy-of-your-site/?utm_source=plugin&utm_medium=gui&utm_campaign=wpmltm';
/**
* @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() ) {
$errorMessage = sprintf( __( self::SITE_MOVED_OR_COPIED_MESSAGE, 'sitepress-multilingual-cms' ),
UIPage::getTMDashboard(),
self::SITE_MOVED_OR_COPIED_DOCS_URL
);
return new \WP_Error( self::SITE_CLONED_ERROR, $errorMessage );
}
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,37 @@
<?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 ) {
return isset( $lockData['stored_fingerprint'] )
&& isset( $lockData['received_fingerprint'] )
&& isset( $lockData['fingerprint_confirmed'] );
}
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,43 @@
<?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' ] );
}
/**
* @param string $reportType
*/
public function handleInstallerSiteUrlDetection($reportType) {
$this->reportHandler->report( $reportType );
}
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' );
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace WPML\TM\ATE\API;
class ErrorMessages {
public static function serverUnavailable( $uuid ) {
return [
'header' => self::serverUnavailableHeader(),
'description' => self::invalidResponseDescription( $uuid ),
];
}
public static function offline( $uuid ) {
$description = _x( 'WPML needs an Internet connection to translate your sites content. It seems that your server is not allowing external connections, or your network is temporarily down.', 'part1', 'wpml-translation-management' );
$description .= _x( 'If this is the first time youre seeing this message, please wait a minute and reload the page. If the problem persists, contact %1$s for help and mention that your website ID is %2$s.', 'part2', 'wpml-translation-management' );
return [
'header' => __( 'Cannot Connect to the Internet', 'wpml-translation-management' ),
'description' => sprintf( $description, self::getSupportLink(), $uuid ),
];
}
public static function invalidResponse( $uuid ) {
return [
'header' => __( 'WPMLs Advanced Translation Editor is not working', 'wpml-translation-management' ),
'description' => self::invalidResponseDescription( $uuid ),
];
}
public static function respondedWithError() {
return __( "WPML's Advanced Translation Editor responded with an error", 'wpml-translation-management' );
}
public static function serverUnavailableHeader() {
return __( 'WPMLs Advanced Translation Editor is not responding', 'wpml-translation-management' );
}
public static function invalidResponseDescription( $uuid ) {
$description = _x( 'WPML cannot connect to the translation editor. If this is the first time youre seeing this message, please wait a minute and reload the page.', 'part1', 'wpml-translation-management' );
$description .= _x( 'If the problem persists, contact %1$s for help and mention that your website ID is %2$s.', 'part2', 'wpml-translation-management' );
return sprintf( $description, self::getSupportLink(), $uuid );
}
public static function getSupportLink() {
return '<a href="https://wpml.org/forums/forum/english-support/" target="_blank" rel="noreferrer">'
. __( 'WPML support', 'wpml-translation-management' ) . '</a>';
}
public static function bodyWithoutRequiredFields() {
return __( 'The body does not contain the required fields', 'wpml-translation-management' );
}
public static function uuidAlreadyExists() {
return __( 'UUID already exists', 'wpml-translation-management' );
}
}

View File

@@ -0,0 +1,807 @@
<?php
use WPML\TM\ATE\ClonedSites\FingerprintGenerator;
use WPML\TM\ATE\Log\Entry;
use WPML\TM\ATE\Log\Storage;
use WPML\TM\ATE\Log\EventsTypes;
use WPML\TM\ATE\ClonedSites\ApiCommunication as ClonedSitesHandler;
use WPML\FP\Json;
use WPML\FP\Relation;
use WPML\FP\Either;
use WPML\TM\ATE\API\ErrorMessages;
use WPML\FP\Fns;
use function WPML\FP\pipe;
use WPML\FP\Logic;
use function WPML\FP\invoke;
/**
* @author OnTheGo Systems
*/
class WPML_TM_AMS_API {
const HTTP_ERROR_CODE_400 = 400;
private $auth;
private $endpoints;
private $wp_http;
/**
* @var ClonedSitesHandler
*/
private $clonedSitesHandler;
/**
* @var FingerprintGenerator
*/
private $fingerprintGenerator;
/**
* WPML_TM_ATE_API constructor.
*
* @param WP_Http $wp_http
* @param WPML_TM_ATE_Authentication $auth
* @param WPML_TM_ATE_AMS_Endpoints $endpoints
* @param ClonedSitesHandler $clonedSitesHandler
* @param FingerprintGenerator $fingerprintGenerator
*/
public function __construct(
WP_Http $wp_http,
WPML_TM_ATE_Authentication $auth,
WPML_TM_ATE_AMS_Endpoints $endpoints,
ClonedSitesHandler $clonedSitesHandler,
FingerprintGenerator $fingerprintGenerator
) {
$this->wp_http = $wp_http;
$this->auth = $auth;
$this->endpoints = $endpoints;
$this->clonedSitesHandler = $clonedSitesHandler;
$this->fingerprintGenerator = $fingerprintGenerator;
}
/**
* @param string $translator_email
*
* @return array|mixed|null|object|WP_Error
* @throws \InvalidArgumentException
*/
public function enable_subscription( $translator_email ) {
$result = null;
$verb = 'PUT';
$url = $this->endpoints->get_enable_subscription();
$url = str_replace( '{translator_email}', base64_encode( $translator_email ), $url );
$response = $this->signed_request( $verb, $url );
if ( $this->response_has_body( $response ) ) {
$result = $this->get_errors( $response );
if ( ! is_wp_error( $result ) ) {
$result = json_decode( $response['body'], true );
}
}
return $result;
}
/**
* @param string $translator_email
*
* @return bool|WP_Error
*/
public function is_subscription_activated( $translator_email ) {
$result = null;
$url = $this->endpoints->get_subscription_status();
$url = str_replace( '{translator_email}', base64_encode( $translator_email ), $url );
$url = str_replace( '{WEBSITE_UUID}', $this->auth->get_site_id(), $url );
$response = $this->signed_request( 'GET', $url );
if ( $this->response_has_body( $response ) ) {
$result = $this->get_errors( $response );
if ( ! is_wp_error( $result ) ) {
$result = json_decode( $response['body'], true );
$result = $result['subscription'];
}
}
return $result;
}
/**
* @return array|mixed|null|object|WP_Error
*
* @throws \InvalidArgumentException Exception.
*/
public function get_status() {
$result = null;
$registration_data = $this->get_registration_data();
$shared = array_key_exists( 'shared', $registration_data ) ? $registration_data['shared'] : null;
if ( $shared ) {
$url = $this->endpoints->get_ams_status();
$url = str_replace( '{SHARED_KEY}', $shared, $url );
$response = $this->request( 'GET', $url );
if ( $this->response_has_body( $response ) ) {
$response_body = json_decode( $response['body'], true );
$result = $this->get_errors( $response );
if ( ! is_wp_error( $result ) ) {
$registration_data = $this->get_registration_data();
if ( isset( $response_body['activated'] ) && (bool) $response_body['activated'] ) {
$registration_data['status'] = WPML_TM_ATE_Authentication::AMS_STATUS_ACTIVE;
$this->set_registration_data( $registration_data );
}
$result = $response_body;
}
}
}
return $result;
}
/**
* Used to register a manager and, at the same time, create a website in AMS.
* This is called only when registering the site with AMS.
* To register new managers or translators `\WPML_TM_ATE_AMS_Endpoints::get_ams_synchronize_managers`
* and `\WPML_TM_ATE_AMS_Endpoints::get_ams_synchronize_translators` will be used.
*
* @param WP_User $manager The WP_User instance of the manager.
* @param WP_User[] $translators An array of WP_User instances representing the current translators.
* @param WP_User[] $managers An array of WP_User instances representing the current managers.
*
* @return \WPML\FP\Either
*/
public function register_manager( WP_User $manager, array $translators, array $managers ) {
$uuid = wpml_get_site_id( WPML_TM_ATE::SITE_ID_SCOPE, false );
$makeRequest = $this->makeRegistrationRequest( $manager, $translators, $managers );
$logErrorResponse = $this->logErrorResponse();
$getErrors = Fns::memorize( function ( $response ) {
return $this->get_errors( $response, false );
} );
$handleErrorResponse = $this->handleErrorResponse( $logErrorResponse, $getErrors );
$handleGeneralError = $handleErrorResponse( Fns::identity(), pipe( [ ErrorMessages::class, 'invalidResponse' ], Either::left() ) );
$handle409Error = $this->handle409Error( $handleErrorResponse, $makeRequest );
return Either::of( $uuid )
->chain( $makeRequest )
->chain( $handle409Error )
->chain( $handleGeneralError )
->chain( $this->handleInvalidBodyError() )
->map( $this->saveRegistrationData( $manager ) );
}
private function makeRegistrationRequest( $manager, $translators, $managers ) {
$buildParams = function ( $uuid ) use ( $manager, $translators, $managers ) {
$manager_data = $this->get_user_data( $manager, true );
$translators_data = $this->get_users_data( $translators );
$managers_data = $this->get_users_data( $managers, true );
$sitekey = function_exists( 'OTGS_Installer' ) ? OTGS_Installer()->get_site_key( 'wpml' ) : null;
$params = $manager_data;
$params['website_url'] = get_site_url();
$params['website_uuid'] = $uuid;
$params['translators'] = $translators_data;
$params['translation_managers'] = $managers_data;
if ( $sitekey ) {
$params['site_key'] = $sitekey;
}
return $params;
};
$handleUnavailableATEError = function ( $response, $uuid ) {
if ( is_wp_error( $response ) ) {
$this->log_api_error(
ErrorMessages::serverUnavailableHeader(),
[ 'responseError' => $response->get_error_message(), 'website_uuid' => $uuid ]
);
$msg = $this->ping_healthy_wpml_endpoint() ? ErrorMessages::serverUnavailable( $uuid ) : ErrorMessages::offline( $uuid );
return Either::left( $msg );
}
return Either::of( [ $response, $uuid ] );
};
return function ( $uuid ) use ( $buildParams, $handleUnavailableATEError ) {
$response = $this->request( 'POST', $this->endpoints->get_ams_register_client(), $buildParams( $uuid ) );
return $handleUnavailableATEError( $response, $uuid );
};
}
private function logErrorResponse() {
return function ( $error, $uuid ) {
$this->log_api_error(
ErrorMessages::respondedWithError(),
[
'responseError' => $error->get_error_code() === 409 ? ErrorMessages::uuidAlreadyExists() : $error->get_error_message(),
'website_uuid' => $uuid
]
);
};
}
private function handleErrorResponse($logErrorResponse, $getErrors) {
return \WPML\FP\curryN( 3, function ( $shouldHandleError, $errorHandler, $data ) use ( $logErrorResponse, $getErrors ) {
list( $response, $uuid ) = $data;
$error = $getErrors( $response );
if ( $shouldHandleError( $error ) ) {
$logErrorResponse( $error, $uuid );
return $errorHandler( $uuid );
}
return Either::of( $data );
} );
}
private function handle409Error($handleErrorResponse, $makeRequest) {
$is409Error = Logic::both( Fns::identity(), pipe( invoke( 'get_error_code' ), Relation::equals( 409 ) ) );
return $handleErrorResponse($is409Error, function ( $uuid ) use ( $makeRequest ) {
$uuid = wpml_get_site_id( WPML_TM_ATE::SITE_ID_SCOPE, true );
return $makeRequest( $uuid );
} );
}
private function handleInvalidBodyError( ) {
return function ( $data ) {
list( $response, $uuid ) = $data;
if ( ! $this->response_has_keys( $response ) ) {
$this->log_api_error(
ErrorMessages::respondedWithError(),
[ 'responseError' => ErrorMessages::bodyWithoutRequiredFields(), 'response' => json_encode( $response ), 'website_uuid' => $uuid ]
);
return Either::left( ErrorMessages::invalidResponse( $uuid ) );
}
return Either::of( $data );
};
}
private function saveRegistrationData($manager) {
return function ( $data ) use ( $manager ) {
list( $response) = $data;
$registration_data = $this->get_registration_data();
$response_body = json_decode( $response['body'], true );
$registration_data['user_id'] = $manager->ID;
$registration_data['secret'] = $response_body['secret_key'];
$registration_data['shared'] = $response_body['shared_key'];
$registration_data['status'] = WPML_TM_ATE_Authentication::AMS_STATUS_ENABLED;
return $this->set_registration_data( $registration_data );
};
}
/**
* Gets the data required by AMS to register a user.
*
* @param WP_User $wp_user The user from which data should be extracted.
* @param bool $with_name_details True if name details should be included.
*
* @return array
*/
private function get_user_data( WP_User $wp_user, $with_name_details = false ) {
$data = array();
$data['email'] = $wp_user->user_email;
if ( $with_name_details ) {
$data['display_name'] = $wp_user->display_name;
$data['first_name'] = $wp_user->first_name;
$data['last_name'] = $wp_user->last_name;
} else {
$data['name'] = $wp_user->display_name;
}
return $data;
}
private function prepareClonedSiteArguments( $method ) {
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
FingerprintGenerator::NEW_SITE_FINGERPRINT_HEADER => $this->fingerprintGenerator->getSiteFingerprint(),
];
return [
'method' => $method,
'headers' => $headers,
];
}
/**
* @return array|WP_Error
*/
public function reportCopiedSite() {
return $this->processReport(
$this->endpoints->get_ams_site_copy(),
'POST'
);
}
/**
* @return array|WP_Error
*/
public function reportMovedSite() {
return $this->processReport(
$this->endpoints->get_ams_site_move(),
'PUT'
);
}
/**
* @param array $response Response from reportMovedSite()
*
* @return bool|WP_Error
*/
public function processMoveReport( $response ) {
if ( ! $this->response_has_body( $response ) ) {
return new WP_Error( 'auth_error', 'Unable to report site moved.' );
}
$response_body = json_decode( $response['body'], true );
if ( isset( $response_body['moved_successfully'] ) && (bool) $response_body['moved_successfully'] ) {
return true;
}
return new WP_Error( 'auth_error', 'Unable to report site moved.' );
}
/**
* @param array $response_body body from reportMovedSite() response.
*
* @return bool
*/
private function storeAuthData( $response_body ) {
$setRegistrationDataResult = $this->updateRegistrationData( $response_body );
$setUuidResult = $this->updateSiteUuId( $response_body );
return $setRegistrationDataResult && $setUuidResult;
}
/**
* @param array $response_body body from reportMovedSite() response.
*
* @return bool
*/
private function updateRegistrationData( $response_body ) {
$registration_data = $this->get_registration_data();
$registration_data['secret'] = $response_body['new_secret_key'];
$registration_data['shared'] = $response_body['new_shared_key'];
return $this->set_registration_data( $registration_data );
}
/**
* @param array $response_body body from reportMovedSite() response.
*
* @return bool
*/
private function updateSiteUuId( $response_body ) {
$this->override_site_id( $response_body['new_website_uuid'] );
return update_option(
WPML_Site_ID::SITE_ID_KEY . ':ate',
$response_body['new_website_uuid'],
false
);
}
private function sendSiteReportConfirmation() {
$url = $this->endpoints->get_ams_site_confirm();
$method = 'POST';
$args = $this->prepareClonedSiteArguments( $method );
$url_parts = wp_parse_url( $url );
$registration_data = $this->get_registration_data();
$query['new_shared_key'] = $registration_data['shared'];
$query['token'] = uuid_v5( wp_generate_uuid4(), $url );
$query['new_website_uuid'] = $this->auth->get_site_id();
$url_parts['query'] = http_build_query( $query );
$url = http_build_url( $url_parts );
$signed_url = $this->auth->signUrl( $method, $url );
$response = $this->wp_http->request( $signed_url, $args );
if ( $this->response_has_body( $response ) ) {
$response_body = json_decode( $response['body'], true );
return (bool) $response_body['confirmed'];
}
return new WP_Error( 'auth_error', 'Unable confirm site copied.' );
}
/**
* @param string $url
* @param string $method
*
* @return array|WP_Error
*/
private function processReport( $url, $method ) {
$args = $this->prepareClonedSiteArguments( $method );
$url_parts = wp_parse_url( $url );
$registration_data = $this->get_registration_data();
$query['shared_key'] = $registration_data['shared'];
$query['token'] = uuid_v5( wp_generate_uuid4(), $url );
$query['website_uuid'] = $this->auth->get_site_id();
$url_parts['query'] = http_build_query( $query );
$url = http_build_url( $url_parts );
$signed_url = $this->auth->signUrl( $method, $url );
return $this->wp_http->request( $signed_url, $args );
}
/**
* @param array $response Response from reportCopiedSite()
*
* @return bool
*/
public function processCopyReportConfirmation( $response ) {
if ( $this->response_has_body( $response ) ) {
$response_body = json_decode( $response['body'], true );
return $this->storeAuthData( $response_body ) && (bool) $this->sendSiteReportConfirmation();
}
return false;
}
/**
* Converts an array of WP_User instances into an array of data nedded by AMS to identify users.
*
* @param WP_User[] $users An array of WP_User instances.
* @param bool $with_name_details True if name details should be included.
*
* @return array
*/
private function get_users_data( array $users, $with_name_details = false ) {
$user_data = array();
foreach ( $users as $user ) {
$wp_user = get_user_by( 'id', $user->ID );
$user_data[] = $this->get_user_data( $wp_user, $with_name_details );
}
return $user_data;
}
/**
* Checks if a reponse has a body.
*
* @param array|\WP_Error $response The response of the remote request.
*
* @return bool
*/
private function response_has_body( $response ) {
return ! is_wp_error( $response ) && array_key_exists( 'body', $response );
}
private function get_errors( $response, $logError = true ) {
$response_errors = null;
if ( is_wp_error( $response ) ) {
$response_errors = $response;
} elseif ( array_key_exists( 'body', $response ) && $response['response']['code'] >= self::HTTP_ERROR_CODE_400 ) {
$main_error = array();
$errors = array();
$error_message = $response['response']['message'];
$response_body = json_decode( $response['body'], true );
if ( ! $response_body ) {
$error_message = $response['body'];
$main_error = array( $response['body'] );
} elseif ( array_key_exists( 'errors', $response_body ) ) {
$errors = $response_body['errors'];
$main_error = array_shift( $errors );
$error_message = $this->get_error_message( $main_error, $response['body'] );
}
$response_errors = new WP_Error( $response['response']['code'], $error_message, $main_error );
foreach ( $errors as $error ) {
$error_message = $this->get_error_message( $error, $response['body'] );
$error_status = isset( $error['status'] ) ? 'ams_error: ' . $error['status'] : '';
$response_errors->add( $error_status, $error_message, $error );
}
}
if ( $logError && $response_errors ) {
$this->log_api_error( $response_errors->get_error_message(), $response_errors->get_error_data() );
}
return $response_errors;
}
private function log_api_error( $message, $data ) {
$entry = new Entry();
$entry->eventType = EventsTypes::SERVER_AMS;
$entry->description = $message;
$entry->extraData = [ 'errorData' => $data ];
wpml_tm_ate_ams_log( $entry );
}
private function ping_healthy_wpml_endpoint() {
$response = $this->request( 'GET', defined( 'WPML_TM_INTERNET_CHECK_URL' ) ? WPML_TM_INTERNET_CHECK_URL : 'https://health.wpml.org/', [] );
return ! is_wp_error( $response ) && (int) \WPML\FP\Obj::path( [ 'response', 'code' ], $response ) === 200;
}
/**
* @param array $ams_error
* @param string $default
*
* @return string
*/
private function get_error_message( $ams_error, $default ) {
$title = isset( $ams_error['title'] ) ? $ams_error['title'] . ': ' : '';
$details = isset( $ams_error['detail'] ) ? $ams_error['detail'] : $default;
return $title . $details;
}
private function response_has_keys( $response ) {
$response_body = json_decode( $response['body'], true );
return array_key_exists( 'secret_key', $response_body ) && array_key_exists( 'shared_key', $response_body );
}
/**
* @return array
*/
public function get_registration_data() {
return get_option( WPML_TM_ATE_Authentication::AMS_DATA_KEY, [] );
}
/**
* @param $registration_data
*
* @return bool
*/
private function set_registration_data( $registration_data ) {
return update_option( WPML_TM_ATE_Authentication::AMS_DATA_KEY, $registration_data );
}
/**
* @param array $managers
*
* @return array|mixed|null|object|WP_Error
* @throws \InvalidArgumentException
*/
public function synchronize_managers( array $managers ) {
$result = null;
$managers_data = $this->get_users_data( $managers, true );
if ( $managers_data ) {
$url = $this->endpoints->get_ams_synchronize_managers();
$url = str_replace( '{WEBSITE_UUID}', wpml_get_site_id( WPML_TM_ATE::SITE_ID_SCOPE ), $url );
$params = array( 'translation_managers' => $managers_data );
$response = $this->signed_request( 'PUT', $url, $params );
if ( $this->response_has_body( $response ) ) {
$response_body = json_decode( $response['body'], true );
$result = $this->get_errors( $response );
if ( ! is_wp_error( $result ) ) {
$result = $response_body;
}
}
}
return $result;
}
/**
* @param array $translators
*
* @return array|mixed|null|object|WP_Error
* @throws \InvalidArgumentException
*/
public function synchronize_translators( array $translators ) {
$result = null;
$translators_data = $this->get_users_data( $translators );
if ( $translators_data ) {
$url = $this->endpoints->get_ams_synchronize_translators();
$params = array( 'translators' => $translators_data );
$response = $this->signed_request( 'PUT', $url, $params );
if ( $this->response_has_body( $response ) ) {
$response_body = json_decode( $response['body'], true );
$result = $this->get_errors( $response );
if ( ! is_wp_error( $result ) ) {
$result = $response_body;
}
} elseif ( is_wp_error( $response ) ) {
$result = $response;
}
}
return $result;
}
/**
* @param string $method
* @param string $url
* @param array|null $params
*
* @return array|WP_Error
*/
private function request( $method, $url, array $params = null ) {
$lock = $this->clonedSitesHandler->checkCloneSiteLock();
if ( $lock ) {
return $lock;
}
$method = strtoupper( $method );
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
FingerprintGenerator::SITE_FINGERPRINT_HEADER => $this->fingerprintGenerator->getSiteFingerprint(),
];
$args = [
'method' => $method,
'headers' => $headers,
'timeout' => max( ini_get( 'max_execution_time' ) / 2, 5 ),
];
if ( $params ) {
$args['body'] = wp_json_encode( $params, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
}
$response = $this->wp_http->request( $this->add_versions_to_url( $url ), $args );
if ( ! is_wp_error( $response ) ) {
$response = $this->clonedSitesHandler->handleClonedSiteError( $response );
}
return $response;
}
/**
* @param string $verb
* @param string $url
* @param array|null $params
*
* @return array|WP_Error
*/
private function signed_request( $verb, $url, array $params = null ) {
$verb = strtoupper( $verb );
$signed_url = $this->auth->get_signed_url_with_parameters( $verb, $url, $params );
if ( is_wp_error( $signed_url ) ) {
return $signed_url;
}
return $this->request( $verb, $signed_url, $params );
}
/**
* @param $url
*
* @return string
*/
private function add_versions_to_url( $url ) {
$url_parts = wp_parse_url( $url );
$query = array();
if ( array_key_exists( 'query', $url_parts ) ) {
parse_str( $url_parts['query'], $query );
}
$query['wpml_core_version'] = ICL_SITEPRESS_VERSION;
$query['wpml_tm_version'] = WPML_TM_VERSION;
$url_parts['query'] = http_build_query( $query );
$url = http_build_url( $url_parts );
return $url;
}
public function override_site_id( $site_id ) {
$this->auth->override_site_id( $site_id );
}
/**
* @return array|WP_Error
*/
public function getCredits() {
return $this->getSignedResult(
'GET',
$this->endpoints->get_credits()
);
}
/**
* @return array|WP_Error
*/
public function resumeAll() {
return $this->getSignedResult(
'GET',
$this->endpoints->get_resume_all()
);
}
public function send_sitekey( $sitekey ) {
$siteId = wpml_get_site_id( WPML_TM_ATE::SITE_ID_SCOPE );
$response = $this->getSignedResult(
'POST',
$this->endpoints->get_send_sitekey(),
[
'site_key' => $sitekey,
'website_uuid' => $siteId,
]
);
return Relation::propEq( 'updated_website', $siteId, $response );
}
/**
* @param string $verb
* @param string $url
* @param array|null $params
*
* @return array|WP_Error
*/
private function getSignedResult( $verb, $url, array $params = null ) {
$result = null;
$response = $this->signed_request( $verb, $url, $params );
if ( $this->response_has_body( $response ) ) {
$result = $this->get_errors( $response );
if ( ! is_wp_error( $result ) ) {
$result = Json::toArray( $response['body'] );
}
}
return $result;
}
}

View File

@@ -0,0 +1,713 @@
<?php
use WPML\TM\ATE\ClonedSites\FingerprintGenerator;
use WPML\TM\ATE\Log\Entry;
use WPML\TM\ATE\Log\EventsTypes;
use WPML\TM\ATE\ClonedSites\ApiCommunication as ClonedSitesHandler;
use WPML\FP\Obj;
use WPML\FP\Fns;
use WPML\FP\Either;
use WPML\FP\Lst;
use WPML\FP\Logic;
use WPML\FP\Str;
use WPML\Element\API\Entity\LanguageMapping;
use WPML\LIB\WP\WordPress;
use WPML\TM\Editor\ATEDetailedErrorMessage;
use function WPML\FP\invoke;
use function WPML\FP\pipe;
use WPML\Element\API\Languages;
use WPML\FP\Relation;
use WPML\FP\Maybe;
/**
* @author OnTheGo Systems
*/
class WPML_TM_ATE_API {
const TRANSLATED = 6;
const DELIVERING = 7;
const NOT_ENOUGH_CREDIT_STATUS = 31;
const CANCELLED_STATUS = 20;
const SHOULD_HIDE_STATUS = 42;
private $wp_http;
private $auth;
private $endpoints;
/**
* @var ClonedSitesHandler
*/
private $clonedSitesHandler;
/**
* @var FingerprintGenerator
*/
private $fingerprintGenerator;
/**
* WPML_TM_ATE_API constructor.
*
* @param WP_Http $wp_http
* @param WPML_TM_ATE_Authentication $auth
* @param WPML_TM_ATE_AMS_Endpoints $endpoints
* @param ClonedSitesHandler $clonedSitesHandler
* @param FingerprintGenerator $fingerprintGenerator
*/
public function __construct(
WP_Http $wp_http,
WPML_TM_ATE_Authentication $auth,
WPML_TM_ATE_AMS_Endpoints $endpoints,
ClonedSitesHandler $clonedSitesHandler,
FingerprintGenerator $fingerprintGenerator
) {
$this->wp_http = $wp_http;
$this->auth = $auth;
$this->endpoints = $endpoints;
$this->clonedSitesHandler = $clonedSitesHandler;
$this->fingerprintGenerator = $fingerprintGenerator;
}
/**
* @param array $params
*
* @see https://bitbucket.org/emartini_crossover/ate/wiki/API/V1/jobs/create
*
* @return mixed
* @throws \InvalidArgumentException
*/
public function create_jobs( array $params ) {
return $this->requestWithLog(
$this->endpoints->get_ate_jobs(),
[
'method' => 'POST',
'body' => $params,
]
);
}
/**
* @param int|string|array $ate_job_id
*
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function confirm_received_job( $ate_job_id ) {
return $this->requestWithLog( $this->endpoints->get_ate_confirm_job( $ate_job_id ) );
}
/**
* @param array|int $jobIds
* @param bool $onlyFailed
*
* @return array|mixed|object|string|\WP_Error|null
*/
public function cancelJobs( $jobIds, $onlyFailed = false ) {
return $this->requestWithLog(
$this->endpoints->getAteCancelJobs(),
[
'method' => 'POST',
'body' => [
'id' => (array) $jobIds,
'only_failed' => $onlyFailed
]
]
);
}
/**
* @param array|int $jobIds
* @param bool $force
*
* @return array|mixed|object|string|\WP_Error|null
*/
public function hideJobs( $jobIds, $force = false ) {
return $this->requestWithLog(
$this->endpoints->getAteHideJobs(),
[
'method' => 'POST',
'body' => [
'id' => (array) $jobIds,
'force' => $force
]
]
);
}
/**
* @param int $job_id
* @param string $return_url
*
* @return string|WP_Error
* @throws \InvalidArgumentException
*/
public function get_editor_url( $job_id, $return_url ) {
$lock = $this->clonedSitesHandler->checkCloneSiteLock();
if ( $lock ) {
return new WP_Error( 'communication_error', 'ATE communication is locked, please update configuration' );
}
$url = $this->endpoints->get_ate_editor();
$url = str_replace(
[
'{job_id}',
'{translator_email}',
'{return_url}',
],
[
$job_id,
urlencode( filter_var( wp_get_current_user()->user_email, FILTER_SANITIZE_URL ) ),
urlencode( filter_var( $return_url, FILTER_SANITIZE_URL ) ),
],
$url
);
return $this->auth->get_signed_url_with_parameters( 'GET', $url, null );
}
/**
* @param int $ate_job_id
* @param WPML_Element_Translation_Job $job_object
* @param int|null $sentFrom
*
* @return array
*/
public function clone_job( $ate_job_id, WPML_Element_Translation_Job $job_object, $sentFrom = null ) {
$url = $this->endpoints->get_clone_job( $ate_job_id );
$params = [
'id' => $ate_job_id,
'notify_url' =>
\WPML\TM\ATE\REST\PublicReceive::get_receive_ate_job_url( $job_object->get_id() ),
'site_identifier' => wpml_get_site_id( WPML_TM_ATE::SITE_ID_SCOPE ),
'source_id' => wpml_tm_get_records()
->icl_translate_job_by_job_id( $job_object->get_id() )
->rid(),
'permalink' => $job_object->get_url( true ),
'ate_ams_console_url' => wpml_tm_get_ams_ate_console_url(),
];
if ( $sentFrom ) {
$params['job_type'] = $sentFrom;
}
$result = $this->requestWithLog( $url, [ 'method' => 'POST', 'body' => $params ] );
return $result && ! is_wp_error( $result ) ?
[
'id' => $result->job_id,
'ate_status' => Obj::propOr( WPML_TM_ATE_AMS_Endpoints::ATE_JOB_STATUS_CREATED, 'status', $result )
] :
false;
}
/**
* @param int $ate_job_id
*
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function get_job( $ate_job_id ) {
if ( ! $ate_job_id ) {
return null;
}
return $this->requestWithLog( $this->endpoints->get_ate_jobs( $ate_job_id ) );
}
/**
* If `$job_ids` is not an empty array,
* the `$statuses` parameter will be ignored in ATE's endpoint.
*
* @see https://bitbucket.org/emartini_crossover/ate/wiki/API/V1/jobs/status
*
* @param null|array $job_ids
* @param null|array $statuses
*
* @return array|mixed|null|object|WP_Error
* @throws \InvalidArgumentException
*/
public function get_jobs( $job_ids, $statuses = null ) {
return $this->requestWithLog( $this->endpoints->get_ate_jobs( $job_ids, $statuses ) );
}
public function get_job_status_with_priority( $job_id ) {
return $this->requestWithLog(
$this->endpoints->get_ate_job_status(),
[
'method' => 'POST',
'body' => [ 'id' => $job_id,
'preview' => true],
]
);
}
/**
* @param $wpml_job_ids
*
* @return array|mixed|object|WP_Error|null
*/
public function get_jobs_by_wpml_ids( $wpml_job_ids ) {
return $this->requestWithLog( $this->endpoints->get_ate_jobs_by_wpml_job_ids( $wpml_job_ids ) );
}
/**
* @param array $pairs
* @see https://bitbucket.org/emartini_crossover/ate/wiki/API/V1/migration/migrate
* @return bool
*/
public function migrate_source_id( array $pairs ) {
$lock = $this->clonedSitesHandler->checkCloneSiteLock();
if ( $lock ) {
return false;
}
$verb = 'POST';
$url = $this->auth->get_signed_url_with_parameters( $verb, $this->endpoints->get_source_id_migration(), $pairs );
if ( is_wp_error( $url ) ) {
return $url;
}
$result = $this->wp_http->request(
$url,
array(
'timeout' => 60,
'method' => $verb,
'headers' => $this->json_headers(),
'body' => wp_json_encode( $pairs, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ),
)
);
if ( ! is_wp_error( $result ) ) {
$result = $this->clonedSitesHandler->handleClonedSiteError( $result );
}
return $this->get_response_errors( $result ) === null;
}
/**
* @param LanguageMapping[] $languagesToMap
*
* @return Either
*/
public function create_language_mapping( array $languagesToMap ) {
$result = $this->requestWithLog(
$this->endpoints->getLanguages(),
[
'method' => 'POST',
'body' => [
'mappings' => Fns::map( invoke( 'toATEFormat' ), Obj::values( $languagesToMap ) )
]
]
);
// it has an error when there is at least one record which has falsy "result" => "created" field
$hasError = Lst::find( Logic::complement( Obj::path( [ 'result', 'created' ] ) ) );
$logError = Fns::tap( function ( $data ) {
$entry = new Entry();
$entry->eventType = EventsTypes::SERVER_ATE;
$entry->description = __( 'Saving of Language mapping to ATE failed', 'wpml-translation-management' );
$entry->extraData = $data;
wpml_tm_ate_ams_log( $entry );
} );
return WordPress::handleError( $result )
->map( Obj::prop( 'mappings' ) )
->chain( Logic::ifElse( $hasError, pipe( $logError, Either::left() ), Either::right() ) );
}
/**
* @param $mappingIds
*
* @return false|array
*/
public function remove_language_mapping( $mappingIds ) {
$result = $this->requestWithLog(
$this->endpoints->getDeleteLanguagesMapping(),
[ 'method' => 'POST', 'body' => [ 'mappings' => $mappingIds ] ]
);
return is_wp_error( $result ) ? false : $result;
}
/**
* @param string[] $languageCodes
* @param null|string $sourceLanguage
*
* @return Maybe
*/
public function get_languages_supported_by_automatic_translations( $languageCodes, $sourceLanguage = null ) {
$sourceLanguage = $sourceLanguage ?: Languages::getDefaultCode();
$result = $this->requestWithLog(
$this->endpoints->getLanguagesCheckPairs(),
[
'method' => 'POST',
'body' => [
[
'source_language' => $sourceLanguage,
'target_languages' => $languageCodes,
]
]
]
);
return Maybe::of( $result )
->reject( 'is_wp_error' )
->map( Obj::prop( 'results' ) )
->map( Lst::find( Relation::propEq( 'source_language', $sourceLanguage ) ) )
->map( Obj::prop( 'target_languages' ) );
}
/**
* It returns language details from ATE including the info about translation engine supporting this language.
*
* If $inTheWebsiteContext is true, then we are taking into consideration user's translation engine settings.
* It means that generally language may be supported e.g. by google, but when he turns off this engine, it will be reflected in the response.
*
* @param string $languageCode
* @param bool $inTheWebsiteContext
*
* @return Maybe
*/
public function get_language_details( $languageCode, $inTheWebsiteContext = true ) {
$result = $this->requestWithLog( sprintf( $this->endpoints->getShowLanguage(), $languageCode ), [ 'method' => 'GET' ] );
return Maybe::of( $result )
->reject( 'is_wp_error' )
->map( Obj::prop( $inTheWebsiteContext ? 'website_language' : 'language' ) );
}
/**
* @return array
*/
public function get_available_languages() {
$result = $this->requestWithLog( $this->endpoints->getLanguages(), [ 'method' => 'GET' ] );
return is_wp_error( $result ) ? [] : $result;
}
/**
* @return Maybe
*/
public function get_language_mapping() {
$result = $this->requestWithLog( $this->endpoints->getLanguagesMapping(), [ 'method' => 'GET' ] );
return Maybe::of( $result )->reject( 'is_wp_error' );
}
public function start_translation_memory_migration() {
$result = $this->requestWithLog(
$this->endpoints->startTranlsationMemoryIclMigration(),
[
'method' => 'POST',
'body' => [
'site_identifier' => $this->get_website_id( site_url() ),
'ts_id' => 10,
// random numbers for now, we should check what needs to be done for the final version.
'ts_access_key' => 20,
],
]
);
return WordPress::handleError( $result );
}
public function check_translation_memory_migration() {
$result = $this->requestWithLog(
$this->endpoints->checkStatusTranlsationMemoryIclMigration(),
[
'method' => 'GET',
'body' => [
'site_identifier' => $this->get_website_id( site_url() ),
'ts_id' => 10,
// random numbers for now, we should check what needs to be done for the final version.
'ts_access_key' => 20,
],
]
);
return WordPress::handleError( $result );
}
/**
* @see https://ate.pages.onthegosystems.com/ate-docs/ATE/API/V1/icl/translators/import
*
* @param $iclToken
* @param $iclServiceId
*
* @return callable|Either
*/
public function import_icl_translators( $tsId, $tsAccessKey ) {
$params = [
'site_identifier' => $this->auth->get_site_id(),
'ts_id' => $tsId,
'ts_access_key' => $tsAccessKey
];
$result = $this->requestWithLog( $this->endpoints->importIclTranslators(),
[
'method' => 'POST',
'body' => $params
] );
return WordPress::handleError( $result );
}
private function get_response( $result ) {
$errors = $this->get_response_errors( $result );
if ( is_wp_error( $errors ) ) {
return $errors;
}
return $this->get_response_body( $result );
}
private function get_response_body( $result ) {
if ( is_array( $result ) && array_key_exists( 'body', $result ) && ! is_wp_error( $result ) ) {
$body = json_decode( $result['body'] );
if ( isset( $body->authenticated ) && ! (bool) $body->authenticated ) {
return new WP_Error( 'ate_auth_failed', $body->message );
}
return $body;
}
return $result;
}
private function get_response_errors( $response ) {
if ( is_wp_error( $response ) ) {
return $response;
}
$response_errors = null;
$response = (array) $response;
if ( array_key_exists( 'body', $response ) && $response['response']['code'] >= 400 ) {
$errors = array();
$response_body = json_decode( $response['body'], true );
if ( is_array( $response_body ) && array_key_exists( 'errors', $response_body ) ) {
$errors = $response_body['errors'];
}
$response_errors = new WP_Error( $response['response']['code'], $response['response']['message'], $errors );
}
return $response_errors;
}
/**
* @return array
*/
private function json_headers() {
return [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
FingerprintGenerator::SITE_FINGERPRINT_HEADER => $this->fingerprintGenerator->getSiteFingerprint(),
];
}
/**
* @param array $args
*
* @return string
*/
private function encode_body_args( array $args ) {
return wp_json_encode( $args, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
}
/**
* @param string $xliff_url
* @param array|\stdClass|false|null $job
*
* @return string
* @throws Requests_Exception
*/
public function get_remote_xliff_content( $xliff_url, $job = null ) {
$entry = $this->prepare_xliff_log_entry( $xliff_url, $job );
wpml_tm_ate_ams_log( $entry, true );
/** @var \WP_Error|array $response */
$response = $this->wp_http->get( $xliff_url, array(
'timeout' => min( 30, ini_get( 'max_execution_time' ) ?: 10 )
) );
wpml_tm_ate_ams_log_remove( $entry );
if ( is_wp_error( $response ) ) {
throw new Requests_Exception( $response->get_error_message(), $response->get_error_code() );
}
return $response['body'];
}
public function override_site_id( $site_id ) {
$this->auth->override_site_id( $site_id );
}
public function get_website_id( $site_url ) {
$lock = $this->clonedSitesHandler->checkCloneSiteLock();
if ( $lock ) {
return null;
}
$signed_url = $this->auth->get_signed_url_with_parameters( 'GET', $this->endpoints->get_websites() );
if ( is_wp_error( $signed_url ) ) {
return null;
}
$requestArguments = [ 'headers' => $this->json_headers() ];
$response = $this->wp_http->request( $signed_url, $requestArguments );
if ( ! is_wp_error( $response ) ) {
$response = $this->clonedSitesHandler->handleClonedSiteError( $response );
}
$sites = $this->get_response( $response );
foreach ( $sites as $site ) {
if ( $site->url === $site_url ) {
return $site->uuid;
}
}
return null;
}
/**
* @see https://bitbucket.org/emartini_crossover/ate/wiki/API/V1/sync/all
*
* @param array $ateJobIds
*
* @return array|mixed|null|object|WP_Error
* @throws \InvalidArgumentException
*/
public function sync_all( array $ateJobIds ) {
return $this->requestWithLog(
$this->endpoints->get_sync_all(),
[
'method' => 'POST',
'body' => [ 'ids' => $ateJobIds ],
]
);
}
/**
* @see https://bitbucket.org/emartini_crossover/ate/wiki/API/V1/sync/page
*
* @param string $token
* @param int $page
*
* @return array|mixed|null|object|WP_Error
* @throws \InvalidArgumentException
*/
public function sync_page( $token, $page ) {
return $this->requestWithLog( $this->endpoints->get_sync_page( $token, $page ) );
}
/**
* @param string $url
* @param array $requestArgs
*
* @return array|mixed|object|string|WP_Error|null
*/
private function request( $url, array $requestArgs = [] ) {
$lock = $this->clonedSitesHandler->checkCloneSiteLock();
if ( $lock ) {
return $lock;
}
$requestArgs = array_merge(
[
'timeout' => 60,
'method' => 'GET',
'headers' => $this->json_headers(),
],
$requestArgs
);
$bodyArgs = isset( $requestArgs['body'] ) && is_array( $requestArgs['body'] )
? $requestArgs['body'] : null;
$signedUrl = $this->auth->get_signed_url_with_parameters( $requestArgs['method'], $url, $bodyArgs );
if ( is_wp_error( $signedUrl ) ) {
return $signedUrl;
}
if ( $bodyArgs ) {
$requestArgs['body'] = $this->encode_body_args( $bodyArgs );
}
$result = $this->wp_http->request( $signedUrl, $requestArgs );
if ( ! is_wp_error( $result ) ) {
$result = $this->clonedSitesHandler->handleClonedSiteError( $result );
}
return $this->get_response( $result );
}
/**
* @param string $url
* @param array $requestArgs
*
* @return array|mixed|object|string|WP_Error|null
*/
private function requestWithLog( $url, array $requestArgs = [] ) {
$response = $this->request( $url, $requestArgs );
if ( is_wp_error( $response ) ) {
$entry = new Entry();
$entry->eventType = EventsTypes::SERVER_ATE;
$entry->description = $response->get_error_message();
$errorCode = $response->get_error_code();
$entry->extraData = [
'url' => $url,
'requestArgs' => $requestArgs,
];
if ( $errorCode ) {
$entry->extraData['status'] = $errorCode;
}
if ( $response->get_error_data( $errorCode ) ) {
$entry->extraData['details'] = $response->get_error_data( $errorCode );
}
wpml_tm_ate_ams_log( $entry );
ATEDetailedErrorMessage::saveDetailedError( $response );
}
return $response;
}
/**
* @param string $xliff_url
* @param array|\stdClass|false|null $job
*
* @return Entry
*/
private function prepare_xliff_log_entry( $xliff_url, $job ) {
$entry = new WPML\TM\ATE\Log\Entry();
if ( $job ) {
$entry->ateJobId = Obj::prop('ateJobId', $job);
$entry->wpmlJobId = Obj::prop('jobId', $job);
}
$entry->eventType = WPML\TM\ATE\Log\EventsTypes::SERVER_ATE;
$entry->description = 'Started attempt to download xliff file. The process did not finish.';
$entry->extraData = [ 'xliff_url' => $xliff_url ];
return $entry;
}
}

View File

@@ -0,0 +1,171 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Authentication {
const AMS_DATA_KEY = 'WPML_TM_AMS';
const AMS_STATUS_NON_ACTIVE = 'non-active';
const AMS_STATUS_ENABLED = 'enabled';
const AMS_STATUS_ACTIVE = 'active';
/** @var string|null $site_id */
private $site_id = null;
public function get_signed_url_with_parameters( $verb, $url, $params = null ) {
if ( $this->has_keys() ) {
$url = $this->add_required_arguments_to_url( $verb, $url, $params );
return $this->signUrl( $verb, $url, $params );
}
return new WP_Error( 'auth_error', 'Unable to authenticate' );
}
public function signUrl( $verb, $url, $params = null ) {
$url_parts = wp_parse_url( $url );
$query = $this->get_url_query( $url );
$query['signature'] = $this->get_signature( $verb, $url, $params );
$url_parts['query'] = $this->build_query( $query );
return http_build_url( $url_parts );
}
private function get_signature( $verb, $url, array $params = null ) {
if ( $this->has_keys() ) {
$verb = strtolower( $verb );
$url_parts = wp_parse_url( $url );
$query_to_sign = $this->get_url_query( $url );
if ( $params && 'get' !== $verb ) {
$query_to_sign['body'] = md5( wp_json_encode( $params, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) );
}
$url_parts_to_sign = $url_parts;
$url_parts_to_sign['query'] = $this->build_query( $query_to_sign );
$url_to_sign = http_build_url( $url_parts_to_sign );
$string_to_sign = strtolower( $verb ) . $url_to_sign;
$sha1 = hash_hmac( 'sha1', $string_to_sign, $this->get_secret(), true );
return base64_encode( $sha1 );
}
return null;
}
public function has_keys() {
return $this->get_secret() && $this->get_shared();
}
private function get_secret() {
return $this->get_ams_data_property( 'secret' );
}
private function get_shared() {
return $this->get_ams_data_property( 'shared' );
}
private function get_ams_data_property( $field ) {
$data = $this->get_ams_data();
if ( array_key_exists( $field, $data ) ) {
return $data[ $field ];
}
return null;
}
/**
* @return array
*/
private function get_ams_data() {
return get_option( self::AMS_DATA_KEY, [] );
}
/**
* @param string $verb
* @param string $url
* @param array|null $params
*
* @return string
*/
private function add_required_arguments_to_url( $verb, $url, array $params = null ) {
$verb = strtolower( $verb );
$url_parts = wp_parse_url( $url );
$query = $this->get_url_query( $url );
if ( $params && 'get' === $verb ) {
foreach ( $params as $key => $value ) {
$query[ $key ] = $value;
}
}
$query['wpml_core_version'] = ICL_SITEPRESS_VERSION;
$query['wpml_tm_version'] = WPML_TM_VERSION;
$query['shared_key'] = $this->get_shared();
$query['token'] = uuid_v5( wp_generate_uuid4(), $url );
$query['website_uuid'] = $this->get_site_id();
$query['ui_language_code'] = apply_filters(
'wpml_get_user_admin_language',
wpml_get_default_language(),
get_current_user_id()
);
if( function_exists( 'OTGS_Installer' ) ) {
$query['site_key'] = OTGS_Installer()->get_site_key( 'wpml' );
}
$url_parts['query'] = http_build_query( $query );
return http_build_url( $url_parts );
}
/**
* @param string $url
*
* @return array
*/
private function get_url_query( $url ) {
$url_parts = wp_parse_url( $url );
$query = array();
if ( array_key_exists( 'query', $url_parts ) ) {
parse_str( $url_parts['query'], $query );
}
return $query;
}
/**
* @param $query
*
* @return mixed|string
*/
protected function build_query( $query ) {
if ( PHP_VERSION_ID >= 50400 ) {
$final_query = http_build_query( $query, '', '&', PHP_QUERY_RFC3986 );
} else {
$final_query = str_replace(
array( '+', '%7E' ),
array( '%20', '~' ),
http_build_query( $query )
);
}
return $final_query;
}
/**
* @param string|null $site_id
*/
public function override_site_id( $site_id ) {
$this->site_id = $site_id;
}
public function get_site_id() {
return $this->site_id ? $this->site_id : wpml_get_site_id( WPML_TM_ATE::SITE_ID_SCOPE );
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace WPML\TM\ATE\Download;
use Exception;
use WPML\FP\Obj;
use WPML\TM\ATE\ReturnedJobsQueue;
use WPML_TM_ATE_API;
use WPML_TM_ATE_Jobs;
class Consumer {
/** @var WPML_TM_ATE_API $ateApi */
private $ateApi;
/** @var WPML_TM_ATE_Jobs $ateJobs */
private $ateJobs;
public function __construct( WPML_TM_ATE_API $ateApi, WPML_TM_ATE_Jobs $ateJobs ) {
$this->ateApi = $ateApi;
$this->ateJobs = $ateJobs;
}
/**
* @param $job
*
* @return array|\stdClass|false
* @throws Exception
*/
public function process( $job ) {
$xliffContent = $this->ateApi->get_remote_xliff_content( Obj::prop('url', $job), $job );
$wpmlJobId = $this->ateJobs->apply( $xliffContent );
if ( $wpmlJobId ) {
$processedJob = Obj::assoc( 'jobId', $wpmlJobId, $job );
ReturnedJobsQueue::remove( $wpmlJobId );
return $processedJob;
}
return false;
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace WPML\TM\ATE\Download;
class Job {
/** @var int $ateJobId */
public $ateJobId;
/** @var string $url */
public $url;
/** @var int */
public $ateStatus;
/**
* This property is not part of the database data,
* but it can be added when the job is downloaded
* to provide more information to the UI.
*
* @var int $jobId
*/
public $jobId;
/** @var int */
public $status = ICL_TM_IN_PROGRESS;
/**
* @param \stdClass $item
*
* @return Job
*/
public static function fromAteResponse( \stdClass $item ) {
$job = new self();
$job->ateJobId = $item->ate_id;
$job->url = $item->download_link;
$job->ateStatus = (int) $item->status;
$job->jobId = (int) $item->id;
return $job;
}
/**
* @param \stdClass $row
*
* @return Job
*/
public static function fromDb( \stdClass $row ) {
$job = new self();
$job->ateJobId = $row->editor_job_id;
$job->url = $row->download_url;
return $job;
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace WPML\TM\ATE\Download;
use Exception;
use WPML\Collect\Support\Collection;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\TM\ATE\Log\Entry;
use WPML\TM\ATE\Log\EventsTypes;
use WPML_TM_ATE_API;
class Process {
/** @var Consumer $consumer */
private $consumer;
/** @var WPML_TM_ATE_API $ateApi */
private $ateApi;
public function __construct( Consumer $consumer, WPML_TM_ATE_API $ateApi ) {
$this->consumer = $consumer;
$this->ateApi = $ateApi;
}
/**
* @param array $jobs
*
* @return Collection
*/
public function run( $jobs ) {
$jobs = \wpml_collect( $jobs )->map( function ( $job ) {
$processedJob = null;
try {
$processedJob = $this->consumer->process( $job );
if ( ! $processedJob ) {
global $iclTranslationManagement;
$message = 'The translation job could not be applied.';
if ( $iclTranslationManagement->messages_by_type( 'error' ) ) {
$iclTranslationManagementError = implode( ' ', Lst::pluck( 'text', $iclTranslationManagement->messages_by_type( 'error' ) ) );
$message .= ' ' . $iclTranslationManagementError;
}
throw new Exception( $message );
}
} catch ( Exception $e ) {
$this->logException( $e, $processedJob ?: $job );
}
return $processedJob;
} )->filter()->values();
$this->acknowledgeAte( $jobs );
return $jobs;
}
private function acknowledgeAte( Collection $processedJobs ) {
if ( $processedJobs->count() ) {
$this->ateApi->confirm_received_job( $processedJobs->pluck( 'ateJobId' )->toArray() );
}
}
/**
* @param Exception $e
* @param Job|null $job
*/
private function logException( Exception $e, $job = null ) {
$entry = new Entry();
$entry->description = $e->getMessage();
if ( $job ) {
$entry->ateJobId = Obj::prop('ateJobId', $job);
$entry->wpmlJobId = Obj::prop('jobId', $job);
$entry->extraData = [ 'downloadUrl' => Obj::prop('url', $job) ];
}
if ( $e instanceof \Requests_Exception ) {
$entry->eventType = EventsTypes::SERVER_XLIFF;
} else {
$entry->eventType = EventsTypes::JOB_DOWNLOAD;
}
wpml_tm_ate_ams_log( $entry );
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace WPML\TM\ATE\Hooks;
use function WPML\Container\make;
use WPML\Element\API\Languages;
use WPML\FP\Fns;
use function WPML\FP\invoke;
use WPML\FP\Lst;
use WPML\FP\Obj;
use function WPML\FP\pipe;
use WPML\FP\Relation;
use WPML\Setup\Option;
class JobActions implements \IWPML_Action {
/** @var \WPML_TM_ATE_API $apiClient */
private $apiClient;
public function __construct( \WPML_TM_ATE_API $apiClient ) {
$this->apiClient = $apiClient;
}
public function add_hooks() {
add_action( 'wpml_tm_job_cancelled', [ $this, 'cancelJobInATE' ] );
add_action( 'wpml_tm_jobs_cancelled', [ $this, 'cancelJobsInATE' ] );
add_action( 'wpml_set_translate_everything', [ $this, 'hideJobsAfterTranslationMethodChange' ] );
add_action( 'wpml_update_active_languages', [ $this, 'hideJobsAfterRemoveLanguage' ] );
}
public function cancelJobInATE( \WPML_TM_Post_Job_Entity $job ) {
if ( $job->is_ate_editor() ) {
$this->apiClient->cancelJobs( $job->get_editor_job_id() );
}
}
/**
* @param \WPML_TM_Post_Job_Entity[]|\WPML_TM_Post_Job_Entity $jobs
*
* @return void
*/
public function cancelJobsInATE( $jobs ) {
/**
* We need this check because if we pass only one job to the hook:
* do_action( 'wpml_tm_jobs_cancelled', [ $job ] )
* then WordPress converts it to $job.
*/
if ( is_object( $jobs ) ) {
$jobs = [ $jobs ];
}
$getIds = pipe(
Fns::filter( invoke( 'is_ate_editor' ) ),
Fns::map( invoke( 'get_editor_job_id' ) )
);
$this->apiClient->cancelJobs( $getIds( $jobs ) );
}
public function hideJobsAfterRemoveLanguage( $oldLanguages ) {
$removedLanguages = Lst::diff( array_keys( $oldLanguages ), array_keys( Languages::getActive() ) );
if ( $removedLanguages ) {
$inProgressJobsSearchParams = self::getInProgressSearch()
->set_target_language( array_values( $removedLanguages ) );
$this->hideJobs( $inProgressJobsSearchParams );
Fns::map( [ Option::class, 'removeLanguageFromCompleted' ], $removedLanguages );
}
}
public function hideJobsAfterTranslationMethodChange( $translateEverythingActive ) {
if ( ! $translateEverythingActive ) {
$this->hideJobs( self::getInProgressSearch() );
}
}
private static function getInProgressSearch() {
return ( new \WPML_TM_Jobs_Search_Params() )->set_status( [
ICL_TM_WAITING_FOR_TRANSLATOR,
ICL_TM_IN_PROGRESS
] );
}
private function hideJobs( \WPML_TM_Jobs_Search_Params $jobsSearchParams ) {
$translationJobs = wpml_collect( wpml_tm_get_jobs_repository()->get( $jobsSearchParams ) )
->filter( invoke( 'is_ate_editor' ) )
->filter( invoke( 'is_automatic' ) );
$canceledInATE = $this->apiClient->hideJobs(
$translationJobs->map( invoke( 'get_editor_job_id' ) )->values()->toArray()
);
$isResponseValid = $canceledInATE && ! is_wp_error( $canceledInATE );
$jobsHiddenInATE = $isResponseValid ? Obj::propOr( [], 'jobs', $canceledInATE ) : [];
$isHiddenInATE = function ( $job ) use ( $isResponseValid, $jobsHiddenInATE ) {
return $isResponseValid && Lst::includes( $job->get_editor_job_id(), $jobsHiddenInATE );
};
$setStatus = Fns::tap( function ( \WPML_TM_Post_Job_Entity $job ) use ( $isHiddenInATE ) {
$status = $isHiddenInATE( $job ) ? ICL_TM_ATE_CANCELLED : ICL_TM_NOT_TRANSLATED;
$job->set_status( $status );
} );
$translationJobs->map( $setStatus )
->map( Fns::tap( [ make( \WPML_TP_Sync_Update_Job::class ), 'update_state' ] ) );
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace WPML\TM\ATE\Hooks;
use function WPML\Container\make;
class JobActionsFactory implements \IWPML_Backend_Action_Loader, \IWPML_Frontend_Action_Loader {
public function create() {
return \WPML_TM_ATE_Status::is_enabled_and_activated()
? new JobActions( make( \WPML_TM_ATE_API::class ) )
: null;
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace WPML\TM\ATE\Hooks;
use WPML\TM\ATE\ReturnedJobsQueue;
class ReturnedJobActions implements \IWPML_Action {
/** @var callable :: int->string->void */
private $addToQueue;
/** @var callable :: int->string->void */
private $remove_translation_duplicate_status;
/**
* @param callable $addToQueue
* @param callable $removeTranslationDuplicateStatus
*/
public function __construct( callable $addToQueue, callable $removeTranslationDuplicateStatus ) {
$this->addToQueue = $addToQueue;
$this->remove_translation_duplicate_status = $removeTranslationDuplicateStatus;
}
public function add_hooks() {
add_action( 'init', [ $this, 'addToQueue' ] );
}
public function addToQueue() {
if ( isset( $_GET['ate_original_id'] ) ) {
$ateJobId = (int) $_GET['ate_original_id'];
if ( isset( $_GET['complete'] ) ) {
call_user_func( $this->addToQueue, $ateJobId, ReturnedJobsQueue::STATUS_COMPLETED );
call_user_func( $this->remove_translation_duplicate_status, $ateJobId );
} elseif ( isset( $_GET['back'] ) ) {
call_user_func( $this->addToQueue, $ateJobId, ReturnedJobsQueue::STATUS_BACK );
}
}
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace WPML\TM\ATE\Hooks;
use WPML\TM\ATE\ReturnedJobsQueue;
use function WPML\Container\make;
use function WPML\FP\partialRight;
class ReturnedJobActionsFactory implements \IWPML_Backend_Action_Loader, \IWPML_REST_Action_Loader {
public function create() {
$ateJobs = make( \WPML_TM_ATE_Jobs::class );
$add = partialRight( [ ReturnedJobsQueue::class, 'add' ], [ $ateJobs, 'get_wpml_job_id' ] );
$removeTranslationDuplicateStatus = partialRight( [ ReturnedJobsQueue::class, 'removeJobTranslationDuplicateStatus' ], [ $ateJobs, 'get_wpml_job_id' ] );
return new ReturnedJobActions( $add, $removeTranslationDuplicateStatus );
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_AMS_Synchronize_Actions_Factory implements IWPML_Backend_Action_Loader {
/**
* @return IWPML_Action|IWPML_Action[]|null
*/
public function create() {
if ( WPML_TM_ATE_Status::is_enabled_and_activated() ) {
$ams_api = WPML\Container\make( WPML_TM_AMS_API::class );
global $wpdb;
$user_query_factory = new WPML_WP_User_Query_Factory();
$wp_roles = wp_roles();
$translator_records = new WPML_Translator_Records( $wpdb, $user_query_factory, $wp_roles );
$manager_records = new WPML_Translation_Manager_Records( $wpdb, $user_query_factory, $wp_roles );
$admin_translators = new WPML_Translator_Admin_Records( $wpdb, $user_query_factory, $wp_roles );
$user_records = new WPML_TM_AMS_Users( $manager_records, $translator_records, $admin_translators );
$user_factory = new WPML_WP_User_Factory();
$translator_activation_records = new WPML_TM_AMS_Translator_Activation_Records( new WPML_WP_User_Factory() );
return new WPML_TM_AMS_Synchronize_Actions(
$ams_api,
$user_records,
$user_factory,
$translator_activation_records,
$manager_records,
$translator_records
);
}
return null;
}
}

View File

@@ -0,0 +1,115 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_AMS_Synchronize_Actions implements IWPML_Action {
const ENABLED_FOR_TRANSLATION_VIA_ATE = 'wpml_enabled_for_translation_via_ate';
/**
* @var WPML_TM_AMS_API
*/
private $ams_api;
/**
* @var WPML_TM_AMS_Users
*/
private $ams_user_records;
/**
* @var WPML_WP_User_Factory $user_factory
*/
private $user_factory;
/**
* @var WPML_TM_AMS_Translator_Activation_Records
*/
private $translator_activation_records;
/** @var WPML_Translation_Manager_Records */
private $tm_records;
/** @var WPML_Translator_Records */
private $translator_records;
/** @var int[] */
private $deletedManagerIds = [];
/** @var int[] */
private $deletedTranslatorIds = [];
public function __construct(
WPML_TM_AMS_API $ams_api,
WPML_TM_AMS_Users $ams_user_records,
WPML_WP_User_Factory $user_factory,
WPML_TM_AMS_Translator_Activation_Records $translator_activation_records,
WPML_Translation_Manager_Records $tm_records,
WPML_Translator_Records $translator_records
) {
$this->ams_api = $ams_api;
$this->ams_user_records = $ams_user_records;
$this->user_factory = $user_factory;
$this->translator_activation_records = $translator_activation_records;
$this->tm_records = $tm_records;
$this->translator_records = $translator_records;
}
public function add_hooks() {
add_action( 'wpml_tm_ate_synchronize_translators', array( $this, 'synchronize_translators' ) );
add_action( 'wpml_update_translator', array( $this, 'synchronize_translators' ) );
add_action( 'wpml_tm_ate_synchronize_managers', array( $this, 'synchronize_managers' ) );
add_action( 'wpml_tm_ate_enable_subscription', array( $this, 'enable_subscription' ) );
add_action( 'delete_user', array( $this, 'prepare_user_deleted' ), 10, 1 );
add_action( 'deleted_user', array( $this, 'user_changed' ), 10, 1 );
add_action( 'profile_update', array( $this, 'user_changed' ), 10, 1 );
}
/**
* @throws \InvalidArgumentException
*/
public function synchronize_translators() {
$result = $this->ams_api->synchronize_translators( $this->ams_user_records->get_translators() );
if ( ! is_wp_error( $result ) ) {
$this->translator_activation_records->update( isset( $result['translators'] ) ? $result['translators'] : array() );
}
}
/**
* @throws \InvalidArgumentException
*/
public function synchronize_managers() {
$this->ams_api->synchronize_managers( $this->ams_user_records->get_managers() );
}
public function enable_subscription( $user_id ) {
$user = $this->user_factory->create( $user_id );
if ( ! $user->get_meta( self::ENABLED_FOR_TRANSLATION_VIA_ATE ) ) {
$this->ams_api->enable_subscription( $user->user_email );
$user->update_meta( self::ENABLED_FOR_TRANSLATION_VIA_ATE, true );
}
}
/**
* @param int $user_id
*/
public function prepare_user_deleted( $user_id ) {
if ( $this->tm_records->does_user_have_capability( $user_id ) ) {
$this->deletedManagerIds[] = $user_id;
}
if ( $this->translator_records->does_user_have_capability( $user_id ) ) {
$this->deletedTranslatorIds[] = $user_id;
}
}
/**
* @param int $user_id
*/
public function user_changed( $user_id ) {
if ( in_array( $user_id, $this->deletedManagerIds ) || $this->tm_records->does_user_have_capability( $user_id ) ) {
$this->synchronize_managers();
}
if ( in_array( $user_id, $this->deletedTranslatorIds ) || $this->translator_records->does_user_have_capability( $user_id ) ) {
$this->synchronize_translators();
}
}
}

View File

@@ -0,0 +1,8 @@
<?php
class WPML_TM_AMS_Synchronize_Users_On_Access_Denied_Factory implements IWPML_Backend_Action_Loader {
public function create() {
return new WPML_TM_AMS_Synchronize_Users_On_Access_Denied();
}
}

View File

@@ -0,0 +1,89 @@
<?php
class WPML_TM_AMS_Synchronize_Users_On_Access_Denied {
const ERROR_MESSAGE = 'Authentication error, please contact your translation manager to check your subscription';
/** @var WPML_TM_AMS_Synchronize_Actions */
private $ams_synchronize_actions;
/** @var WPML_TM_ATE_Jobs */
private $ate_jobs;
public function add_hooks() {
if ( WPML_TM_ATE_Status::is_enabled_and_activated() ) {
add_action( 'init', array( $this, 'catch_access_error' ) );
}
}
public function catch_access_error() {
if ( ! $this->ate_redirected_due_to_lack_of_access() ) {
return;
}
$this->get_ams_synchronize_actions()->synchronize_translators();
if ( ! isset( $_GET['ate_job_id'] ) ) {
return;
}
$wpml_job_id = $this->get_ate_jobs()->get_wpml_job_id( $_GET['ate_job_id'] );
if ( ! $wpml_job_id ) {
return;
}
$url = admin_url(
'admin.php?page='
. WPML_TM_FOLDER
. '/menu/translations-queue.php&job_id='
. $wpml_job_id
);
wp_safe_redirect( $url, 302, 'WPML' );
}
/**
* @return bool
*/
private function ate_redirected_due_to_lack_of_access() {
return isset( $_GET['message'] ) && false !== strpos( $_GET['message'], self::ERROR_MESSAGE );
}
/**
* @return IWPML_Action|IWPML_Action[]|WPML_TM_AMS_Synchronize_Actions
*/
private function get_ams_synchronize_actions() {
if ( ! $this->ams_synchronize_actions ) {
$factory = new WPML_TM_AMS_Synchronize_Actions_Factory();
$this->ams_synchronize_actions = $factory->create();
}
return $this->ams_synchronize_actions;
}
/**
* @return WPML_TM_ATE_Jobs
*/
private function get_ate_jobs() {
if ( ! $this->ate_jobs ) {
$ate_jobs_records = wpml_tm_get_ate_job_records();
$this->ate_jobs = new WPML_TM_ATE_Jobs( $ate_jobs_records );
}
return $this->ate_jobs;
}
/**
* @param WPML_TM_AMS_Synchronize_Actions $ams_synchronize_actions
*/
public function set_ams_synchronize_actions( WPML_TM_AMS_Synchronize_Actions $ams_synchronize_actions ) {
$this->ams_synchronize_actions = $ams_synchronize_actions;
}
/**
* @param WPML_TM_ATE_Jobs $ate_jobs
*/
public function set_ate_jobs( WPML_TM_ATE_Jobs $ate_jobs ) {
$this->ate_jobs = $ate_jobs;
}
}

View File

@@ -0,0 +1,21 @@
<?php
class WPML_TM_ATE_API_Error {
public function log( $message ) {
$wpml_admin_notices = wpml_get_admin_notices();
$notice = new WPML_Notice(
WPML_TM_ATE_Jobs_Actions::RESPONSE_ATE_ERROR_NOTICE_ID,
sprintf(
__( 'There was a problem communicating with ATE: %s ', 'wpml-translation-management' ),
'(<i>' . $message . '</i>)'
),
WPML_TM_ATE_Jobs_Actions::RESPONSE_ATE_ERROR_NOTICE_GROUP
);
$notice->set_css_class_types( array( 'warning' ) );
$notice->add_capability_check( array( 'manage_options', 'wpml_manage_translation_management' ) );
$notice->set_flash();
$wpml_admin_notices->add_notice( $notice );
}
}

View File

@@ -0,0 +1,11 @@
<?php
class WPML_TM_ATE_Job_Data_Fallback_Factory implements IWPML_Backend_Action_Loader, IWPML_REST_Action_Loader {
/**
* @return WPML_TM_ATE_Job_Data_Fallback
*/
public function create() {
return \WPML\Container\make( '\WPML_TM_ATE_Job_Data_Fallback' );
}
}

View File

@@ -0,0 +1,39 @@
<?php
use WPML\TM\ATE\JobRecords;
class WPML_TM_ATE_Job_Data_Fallback implements IWPML_Action {
/** @var WPML_TM_ATE_API */
private $ate_api;
/**
* @param WPML_TM_ATE_API $ate_api
*/
public function __construct( WPML_TM_ATE_API $ate_api ) {
$this->ate_api = $ate_api;
}
public function add_hooks() {
add_filter( 'wpml_tm_ate_job_data_fallback', array( $this, 'get_data_from_api' ), 10, 2 );
}
/**
* @param array $data
* @param int $wpml_job_id
*
* @return array
*/
public function get_data_from_api( array $data, $wpml_job_id ) {
$response = $this->ate_api->get_jobs_by_wpml_ids( array( $wpml_job_id ) );
if ( ! $response || is_wp_error( $response ) ) {
return $data;
}
if ( ! isset( $response->{$wpml_job_id}->ate_job_id ) ) {
return $data;
}
return array( JobRecords::FIELD_ATE_JOB_ID => $response->{$wpml_job_id}->ate_job_id );
}
}

View File

@@ -0,0 +1,75 @@
<?php
use function WPML\Container\make;
use WPML\TM\ATE\ReturnedJobsQueue;
/**
* Factory class for \WPML_TM_ATE_Jobs_Actions.
*
* @package wpml\tm
*
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Jobs_Actions_Factory implements IWPML_Backend_Action_Loader, \IWPML_REST_Action_Loader {
/**
* The instance of \WPML_Current_Screen.
*
* @var WPML_Current_Screen
*/
private $current_screen;
/**
* It returns an instance of \WPML_TM_ATE_Jobs_Actions or null if ATE is not enabled and active.
*
* @return \WPML_TM_ATE_Jobs_Actions|null
* @throws \Auryn\InjectionException
*/
public function create() {
$ams_ate_factories = wpml_tm_ams_ate_factories();
if ( WPML_TM_ATE_Status::is_enabled_and_activated() ) {
$sitepress = $this->get_sitepress();
$current_screen = $this->get_current_screen();
$ate_api = $ams_ate_factories->get_ate_api();
$records = wpml_tm_get_ate_job_records();
$ate_jobs = new WPML_TM_ATE_Jobs( $records );
$translator_activation_records = new WPML_TM_AMS_Translator_Activation_Records( new WPML_WP_User_Factory() );
return new WPML_TM_ATE_Jobs_Actions(
$ate_api,
$ate_jobs,
$sitepress,
$current_screen,
$translator_activation_records
);
}
return null;
}
/**
* The global instance of \Sitepress.
*
* @return SitePress
*/
private function get_sitepress() {
global $sitepress;
return $sitepress;
}
/**
* It gets the instance of \WPML_Current_Screen.
*
* @return \WPML_Current_Screen
*/
private function get_current_screen() {
if ( ! $this->current_screen ) {
$this->current_screen = new WPML_Current_Screen();
}
return $this->current_screen;
}
}

View File

@@ -0,0 +1,538 @@
<?php
use WPML\API\Sanitize;
use WPML\FP\Fns;
use WPML\FP\Json;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\FP\Relation;
use WPML\TM\API\Jobs;
use WPML\FP\Wrapper;
use WPML\Settings\PostType\Automatic;
use WPML\Setup\Option;
use WPML\TM\ATE\JobRecords;
use WPML\TM\ATE\Log\Storage;
use WPML\TM\ATE\Log\Entry;
use function WPML\FP\partialRight;
use function WPML\FP\pipe;
use WPML\TM\API\ATE\LanguageMappings;
use WPML\Element\API\Languages;
/**
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Jobs_Actions implements IWPML_Action {
const RESPONSE_ATE_NOT_ACTIVE_ERROR = 403;
const RESPONSE_ATE_DUPLICATED_SOURCE_ID = 417;
const RESPONSE_ATE_UNEXPECTED_ERROR = 500;
const RESPONSE_ATE_ERROR_NOTICE_ID = 'ate-update-error';
const RESPONSE_ATE_ERROR_NOTICE_GROUP = 'default';
const CREATE_ATE_JOB_CHUNK_WORDS_LIMIT = 2000;
/**
* @var WPML_TM_ATE_API
*/
private $ate_api;
/**
* @var WPML_TM_ATE_Jobs
*/
private $ate_jobs;
/**
* @var WPML_TM_AMS_Translator_Activation_Records
*/
private $translator_activation_records;
/** @var bool */
private $is_second_attempt_to_get_jobs_data = false;
/**
* @var SitePress
*/
private $sitepress;
/**
* @var WPML_Current_Screen
*/
private $current_screen;
/**
* WPML_TM_ATE_Jobs_Actions constructor.
*
* @param \WPML_TM_ATE_API $ate_api
* @param \WPML_TM_ATE_Jobs $ate_jobs
* @param \SitePress $sitepress
* @param \WPML_Current_Screen $current_screen
* @param \WPML_TM_AMS_Translator_Activation_Records $translator_activation_records
*/
public function __construct(
WPML_TM_ATE_API $ate_api,
WPML_TM_ATE_Jobs $ate_jobs,
SitePress $sitepress,
WPML_Current_Screen $current_screen,
WPML_TM_AMS_Translator_Activation_Records $translator_activation_records
) {
$this->ate_api = $ate_api;
$this->ate_jobs = $ate_jobs;
$this->sitepress = $sitepress;
$this->current_screen = $current_screen;
$this->translator_activation_records = $translator_activation_records;
}
public function add_hooks() {
add_action( 'wpml_added_translation_job', [ $this, 'added_translation_job' ], 10, 2 );
add_action( 'wpml_added_translation_jobs', [ $this, 'added_translation_jobs' ], 10, 2 );
add_action( 'admin_notices', [ $this, 'handle_messages' ] );
add_filter( 'wpml_tm_ate_jobs_data', [ $this, 'get_ate_jobs_data_filter' ], 10, 2 );
add_filter( 'wpml_tm_ate_jobs_editor_url', [ $this, 'get_editor_url' ], 10, 3 );
}
public function handle_messages() {
if ( $this->current_screen->id_ends_with( WPML_TM_FOLDER . '/menu/translations-queue' ) ) {
if ( array_key_exists( 'message', $_GET ) ) {
if ( array_key_exists( 'ate_job_id', $_GET ) ) {
$ate_job_id = filter_var( $_GET['ate_job_id'], FILTER_SANITIZE_NUMBER_INT );
$this->resign_job_on_error( $ate_job_id );
}
$message = Sanitize::stringProp( 'message', $_GET );
?>
<div class="error notice-error notice otgs-notice">
<p><?php echo $message; ?></p>
</div>
<?php
}
}
}
/**
* @param int $job_id
* @param string $translation_service
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function added_translation_job( $job_id, $translation_service ) {
$this->added_translation_jobs( array( $translation_service => array( $job_id ) ) );
}
/**
* @param array $jobs
* @param int|null $sentFrom
*
* @return bool|void
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function added_translation_jobs( array $jobs, $sentFrom = null ) {
$oldEditor = wpml_tm_load_old_jobs_editor();
$job_ids = Fns::reject( [ $oldEditor, 'shouldStickToWPMLEditor' ], Obj::propOr( [], 'local', $jobs ) );
if ( ! $job_ids ) {
return;
}
$jobs = Fns::map( 'wpml_tm_create_ATE_job_creation_model', $job_ids );
$responses = Fns::map(
Fns::unary( partialRight( [ $this, 'create_jobs' ], $sentFrom ) ),
$this->getChunkedJobs( $jobs )
);
$created_jobs = $this->getResponsesJobs( $responses, $jobs );
if ( $created_jobs ) {
$created_jobs = $this->map_response_jobs( $created_jobs );
$this->ate_jobs->warm_cache( array_keys( $created_jobs ) );
foreach ( $created_jobs as $wpml_job_id => $ate_job_id ) {
$this->ate_jobs->store( $wpml_job_id, [ JobRecords::FIELD_ATE_JOB_ID => $ate_job_id ] );
$oldEditor->set( $wpml_job_id, WPML_TM_Editors::ATE );
$translationJob = wpml_tm_load_job_factory()->get_translation_job( $wpml_job_id, false, 0, true );
$jobType = $this->getJobType( $translationJob );
wpml_tm_load_job_factory()->update_job_data(
$wpml_job_id,
[ 'automatic' => $jobType === 'auto' ? 1 : 0 ]
);
if ( $sentFrom === Jobs::SENT_RETRY ) {
Jobs::setStatus( $wpml_job_id, ICL_TM_WAITING_FOR_TRANSLATOR );
}
}
$message = __( '%1$s jobs added to the Advanced Translation Editor.', 'wpml-translation-management' );
$this->add_message( 'updated', sprintf( $message, count( $created_jobs ) ), 'wpml_tm_ate_create_job' );
} else {
if ( Lst::includes( $sentFrom, [ Jobs::SENT_AUTOMATICALLY, Jobs::SENT_RETRY ] ) ) {
if ( $sentFrom === Jobs::SENT_RETRY ) {
$updateJob = function ($jobId) {
Jobs::incrementRetryCount($jobId);
$this->logRetryError( $jobId );
};
} else {
$updateJob = function ( $jobId ) use ( $oldEditor ) {
$this->logError( $jobId );
$translationJob = wpml_tm_load_job_factory()->get_translation_job( $jobId, false, 0, true );
$jobType = $this->getJobType( $translationJob );
if ( $jobType === 'auto' ) {
Jobs::setStatus( $jobId, ICL_TM_ATE_NEEDS_RETRY );
$oldEditor->set( $jobId, WPML_TM_Editors::ATE );
wpml_tm_load_job_factory()->update_job_data( $jobId, [ 'automatic' => 1 ] );
}
};
}
wpml_collect( $job_ids )->map( $updateJob );
}
$this->add_message(
'error',
__(
'Jobs could not be created in Advanced Translation Editor. Please try again or contact the WPML support for help.',
'wpml-translation-management'
),
'wpml_tm_ate_create_job'
);
}
}
private function map_response_jobs( $responseJobs ) {
$result = [];
foreach ( $responseJobs as $rid => $ate_job_id ) {
$jobId = \WPML\TM\API\Job\Map::fromRid( $rid );
if ( $jobId ) {
$result[ $jobId ] = $ate_job_id;
}
}
return $result;
}
/**
* @param string $type
* @param string $message
* @param string|null $id
*/
private function add_message( $type, $message, $id = null ) {
do_action( 'wpml_tm_basket_add_message', $type, $message, $id );
}
/**
* @param array $jobsData
* @param int|null $sentFrom
*
* @return mixed
* @throws \InvalidArgumentException
*/
public function create_jobs( array $jobsData, $sentFrom ) {
$setJobType = Logic::ifElse( Fns::always( $sentFrom ), Obj::assoc( 'job_type', $sentFrom ), Fns::identity() );
list( $existing, $new ) = Lst::partition(
pipe( Obj::propOr( null, 'existing_ate_id' ), Logic::isNotNull() ),
$jobsData['jobs']
);
$isAuto = Relation::propEq( 'type', 'auto', $jobsData );
return Wrapper::of( [ 'jobs' => $new, 'existing_jobs' => Lst::pluck( 'existing_ate_id', $existing ) ] )
->map( Obj::assoc( 'auto_translate', $isAuto && Option::shouldTranslateEverything() ) )
->map( Obj::assoc( 'preview', $isAuto && Option::shouldBeReviewed() ) )
->map( $setJobType )
->map( 'wp_json_encode' )
->map( Json::toArray() )
->map( [ $this->ate_api, 'create_jobs' ] )
->get();
}
/**
* After implementation of wpmltm-3211 and wpmltm-3391, we should not find missing ATE IDs anymore.
* Some code below seems dead but we'll keep it for now in case we are missing a specific context.
*
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/wpmltm-3211
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/wpmltm-3391
*/
private function get_ate_jobs_data( array $translation_jobs ) {
$ate_jobs_data = array();
$skip_getting_data = false;
$ate_jobs_to_create = array();
$this->ate_jobs->warm_cache( wpml_collect( $translation_jobs )->pluck( 'job_id' )->toArray() );
foreach ( $translation_jobs as $translation_job ) {
if ( $this->is_ate_translation_job( $translation_job ) ) {
$ate_job_id = $this->get_ate_job_id( $translation_job->job_id );
// Start of possibly dead code.
if ( ! $ate_job_id ) {
$ate_jobs_to_create[] = $translation_job->job_id;
$skip_getting_data = true;
}
// End of possibly dead code.
if ( ! $skip_getting_data ) {
$ate_jobs_data[ $translation_job->job_id ] = [ 'ate_job_id' => $ate_job_id ];
}
}
}
// Start of possibly dead code.
if (
! $this->is_second_attempt_to_get_jobs_data &&
$ate_jobs_to_create &&
$this->added_translation_jobs( array( 'local' => $ate_jobs_to_create ) )
) {
$ate_jobs_data = $this->get_ate_jobs_data( $translation_jobs );
$this->is_second_attempt_to_get_jobs_data = true;
}
// End of possibly dead code.
return $ate_jobs_data;
}
/**
* @param string $default_url
* @param int $job_id
* @param null|string $return_url
*
* @return string
* @throws \InvalidArgumentException
*/
public function get_editor_url( $default_url, $job_id, $return_url = null ) {
$isUserActivated = $this->translator_activation_records->is_current_user_activated();
if ( $isUserActivated || is_admin() ) {
$ate_job_id = $this->ate_jobs->get_ate_job_id( $job_id );
if ( $ate_job_id ) {
if ( ! $return_url ) {
$return_url = add_query_arg(
array(
'page' => WPML_TM_FOLDER . '/menu/translations-queue.php',
'ate-return-job' => $job_id,
),
admin_url( '/admin.php' )
);
}
$ate_job_url = $this->ate_api->get_editor_url( $ate_job_id, $return_url );
if ( $ate_job_url && ! is_wp_error( $ate_job_url ) ) {
return $ate_job_url;
}
}
}
return $default_url;
}
/**
* @param $ignore
* @param array $translation_jobs
*
* @return array
*/
public function get_ate_jobs_data_filter( $ignore, array $translation_jobs ) {
return $this->get_ate_jobs_data( $translation_jobs );
}
private function get_ate_job_id( $job_id ) {
return $this->ate_jobs->get_ate_job_id( $job_id );
}
/**
* @param mixed $response
*
* @throws \RuntimeException
*/
protected function check_response_error( $response ) {
if ( is_wp_error( $response ) ) {
$code = 0;
$message = $response->get_error_message();
if ( $response->error_data && is_array( $response->error_data ) ) {
foreach ( $response->error_data as $http_code => $error_data ) {
$code = $error_data[0]['status'];
$message = '';
switch ( (int) $code ) {
case self::RESPONSE_ATE_NOT_ACTIVE_ERROR:
$wp_admin_url = admin_url( 'admin.php' );
$mcsetup_page = add_query_arg(
array(
'page' => WPML_TM_FOLDER . WPML_Translation_Management::PAGE_SLUG_SETTINGS,
'sm' => 'mcsetup',
),
$wp_admin_url
);
$mcsetup_page .= '#ml-content-setup-sec-1';
$resend_link = '<a href="' . $mcsetup_page . '">'
. esc_html__( 'Resend that email', 'wpml-translation-management' )
. '</a>';
$message .= '<p>'
. esc_html__( 'WPML cannot send these documents to translation because the Advanced Translation Editor is not fully set-up yet.', 'wpml-translation-management' )
. '</p><p>'
. esc_html__( 'Please open the confirmation email that you received and click on the link inside it to confirm your email.', 'wpml-translation-management' )
. '</p><p>'
. $resend_link
. '</p>';
break;
case self::RESPONSE_ATE_DUPLICATED_SOURCE_ID:
case self::RESPONSE_ATE_UNEXPECTED_ERROR:
default:
$message = '<p>'
. __( 'Advanced Translation Editor error:', 'wpml-translation-management' )
. '</p><p>'
. $error_data[0]['message']
. '</p>';
}
$message = '<p>' . $message . '</p>';
}
}
/** @var WP_Error $response */
throw new RuntimeException( $message, $code );
}
}
/**
* @param $ate_job_id
*/
private function resign_job_on_error( $ate_job_id ) {
$job_id = $this->ate_jobs->get_wpml_job_id( $ate_job_id );
if ( $job_id ) {
wpml_load_core_tm()->resign_translator( $job_id );
}
}
/**
* @param $translation_job
*
* @return bool
*/
private function is_ate_translation_job( $translation_job ) {
return 'local' === $translation_job->translation_service
&& WPML_TM_Editors::ATE === $translation_job->editor;
}
/**
* @param array $responses
* @param \WPML_TM_ATE_Models_Job_Create[] $sentJobs
*
* @return array
*/
private function getResponsesJobs( $responses, $sentJobs ) {
$jobs = [];
foreach ( $responses as $response ) {
try {
$this->check_response_error( $response );
if ( $response && isset( $response->jobs ) ) {
$jobs = $jobs + (array) $response->jobs;
}
} catch ( RuntimeException $ex ) {
do_action( 'wpml_tm_basket_add_message', 'error', $ex->getMessage() );
}
}
$existingJobs = wpml_collect( $sentJobs )
->filter( Obj::prop( 'existing_ate_id' ) )
->map( Obj::pick( [ 'source_id', 'existing_ate_id' ] ) )
->keyBy( 'source_id' )
->map( Obj::prop( 'existing_ate_id' ) )
->toArray();
return $jobs + $existingJobs;
}
/**
* @param \WPML_TM_ATE_Models_Job_Create[] $jobs
*
* @return array
*/
private function getChunkedJobs( $jobs ) {
$chunkedJobs = [];
$currentChunk = -1;
$currentWordCount = 0;
$chunkType = 'auto';
$newChunk = function( $chunkType ) use ( &$chunkedJobs, &$currentChunk, &$currentWordCount ) {
$currentChunk ++;
$currentWordCount = 0;
$chunkedJobs[ $currentChunk ] = [ 'type' => $chunkType, 'jobs' => [] ];
};
$newChunk( $chunkType );
foreach ( $jobs as $job ) {
/** @var WPML_Element_Translation_Job $translationJob */
$translationJob = wpml_tm_load_job_factory()->get_translation_job( $job->id, false, 0, true );
if ( $translationJob ) {
if ( ! Obj::prop( 'existing_ate_id', $job ) ) {
$currentWordCount += $translationJob->estimate_word_count();
}
$jobType = $this->getJobType( $translationJob );
if ( $jobType !== $chunkType ) {
$chunkType = $jobType;
$newChunk( $chunkType );
}
if ( $currentWordCount > self::CREATE_ATE_JOB_CHUNK_WORDS_LIMIT && count( $chunkedJobs[ $currentChunk ] ) > 0 ) {
$newChunk( $chunkType );
}
}
$chunkedJobs[ $currentChunk ]['jobs'] [] = $job;
}
$hasJobs = pipe( Obj::prop( 'jobs' ), Lst::length() );
return Fns::filter( $hasJobs, $chunkedJobs );
}
/**
* @param int $jobId
*/
private function logRetryError( $jobId ) {
$job = Jobs::get( $jobId );
if ( $job && $job->ate_comm_retry_count ) {
Storage::add( Entry::retryJob( $jobId,
[
'retry_count' => $job->ate_comm_retry_count
]
) );
}
}
/**
* @param int $jobId
*/
private function logError( $jobId ) {
$job = Jobs::get( $jobId );
if ( $job ) {
Storage::add( Entry::retryJob( $jobId, [
'retry_count' => 0,
'comment' => 'Sending job to ate failed, queued to be sent again.',
]
) );
}
}
private function getJobType( $translationJob ) {
$document = $translationJob->get_original_document();
if ( ! $document || $document instanceof WPML_Package ) {
return 'manual';
} else {
return $translationJob->get_source_language_code() === Languages::getDefaultCode() &&
Jobs::isEligibleForAutomaticTranslations( $translationJob->get_id() ) ? 'auto' : 'manual';
}
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* @todo Perhaps this class is redundant
*
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Jobs_Store_Actions_Factory implements IWPML_Backend_Action_Loader {
/**
* @return IWPML_Action|IWPML_Action[]|null
*/
public function create() {
if ( WPML_TM_ATE_Status::is_enabled() ) {
$ate_jobs_records = wpml_tm_get_ate_job_records();
$ate_jobs = new WPML_TM_ATE_Jobs( $ate_jobs_records );
return new WPML_TM_ATE_Jobs_Store_Actions( $ate_jobs );
}
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* @todo The hook 'wpml_tm_ate_jobs_store' seems to be never used so this class and its factory may be obsolete
*
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Jobs_Store_Actions implements IWPML_Action {
/**
* @var WPML_TM_ATE_Jobs
*/
private $ate_jobs;
/**
* WPML_TM_ATE_Jobs_Actions constructor.
*
* @param WPML_TM_ATE_Jobs $ate_jobs
*/
public function __construct( WPML_TM_ATE_Jobs $ate_jobs ) {
$this->ate_jobs = $ate_jobs;
}
public function add_hooks() {
add_action( 'wpml_tm_ate_jobs_store', array( $this, 'store' ), 10, 2 );
}
/**
* @param int $wpml_job_id
* @param array $ate_job_data
*
* @return array|null
*/
public function store( $wpml_job_id, $ate_job_data ) {
return $this->ate_jobs->store( $wpml_job_id, $ate_job_data );
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Post_Edit_Actions_Factory implements IWPML_Backend_Action_Loader {
/**
* @return IWPML_Action|IWPML_Action[]|null
*/
public function create() {
$tm_ate = new WPML_TM_ATE();
$endpoints = WPML\Container\make( 'WPML_TM_ATE_AMS_Endpoints' );
if ( $tm_ate->is_translation_method_ate_enabled() ) {
return new WPML_TM_ATE_Post_Edit_Actions( $endpoints );
}
return null;
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Post_Edit_Actions implements IWPML_Action {
private $endpoints;
/**
* WPML_TM_ATE_Jobs_Actions constructor.
*
* @param WPML_TM_ATE_AMS_Endpoints $endpoints
*/
public function __construct( WPML_TM_ATE_AMS_Endpoints $endpoints ) {
$this->endpoints = $endpoints;
}
public function add_hooks() {
add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) );
}
public function allowed_redirect_hosts( $hosts ) {
$hosts[] = $this->endpoints->get_AMS_host();
$hosts[] = $this->endpoints->get_ATE_host();
return $hosts;
}
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Required_Actions_Base {
private $ate_enabled;
protected function is_ate_enabled() {
if ( null === $this->ate_enabled ) {
$tm_settings = wpml_get_setting_filter( null, 'translation-management' );
$doc_translation_method = null;
if ( array_key_exists( 'doc_translation_method', $tm_settings ) ) {
$doc_translation_method = $tm_settings['doc_translation_method'];
}
$this->ate_enabled = $doc_translation_method === ICL_TM_TMETHOD_ATE;
}
return $this->ate_enabled;
}
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* \WPML_TM_ATE_Translator_Login factory.
*
* @author OnTheGo Systems
*
* NOTE: This uses the Frontend loader because is_admin() returns false during wp_login
*/
class WPML_TM_ATE_Translator_Login_Factory implements IWPML_Frontend_Action_Loader {
/**
* It returns an instance of WPML_TM_ATE_Translator_Login is ATE is enabled and active.
*
* @return \WPML_TM_ATE_Translator_Logine|\IWPML_Frontend_Action_Loader|null
*/
public function create() {
if ( WPML_TM_ATE_Status::is_enabled_and_activated() ) {
return WPML\Container\make( WPML_TM_ATE_Translator_Login::class );
}
return null;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ATE_Translator_Login implements IWPML_Action {
/** @var WPML_TM_AMS_Translator_Activation_Records */
private $translator_activation_records;
/** @var WPML_Translator_Records */
private $translator_records;
/** @var WPML_TM_AMS_API */
private $ams_api;
public function __construct(
WPML_TM_AMS_Translator_Activation_Records $translator_activation_records,
WPML_Translator_Records $translator_records,
WPML_TM_AMS_API $ams_api
) {
$this->translator_activation_records = $translator_activation_records;
$this->translator_records = $translator_records;
$this->ams_api = $ams_api;
}
public function add_hooks() {
add_action( 'wp_login', array( $this, 'wp_login' ), 10, 2 );
}
public function wp_login( $user_login, $user ) {
if ( $this->translator_records->does_user_have_capability( $user->ID ) ) {
$result = $this->ams_api->is_subscription_activated( $user->user_email );
if ( ! is_wp_error( $result ) ) {
$this->translator_activation_records->set_activated(
$user->user_email,
$result
);
}
}
}
}

View File

@@ -0,0 +1,54 @@
<?php
use WPML\API\Sanitize;
class WPML_TM_ATE_Translator_Message_Classic_Editor_Factory implements IWPML_Backend_Action_Loader, IWPML_AJAX_Action_Loader {
/**
* @return \WPML_TM_ATE_Translator_Message_Classic_Editor|\IWPML_Action|null
*/
public function create() {
global $wpdb;
if ( $this->is_ajax_or_translation_queue() && $this->is_ate_enabled_and_manager_wizard_completed() && ! $this->is_editing_old_translation_and_te_is_used_for_old_translation() ) {
$email_twig_factory = wpml_tm_get_email_twig_template_factory();
return new WPML_TM_ATE_Translator_Message_Classic_Editor(
new WPML_Translation_Manager_Records(
$wpdb,
wpml_tm_get_wp_user_query_factory(),
wp_roles()
),
wpml_tm_get_wp_user_factory(),
new WPML_TM_ATE_Request_Activation_Email(
new WPML_TM_Email_Notification_View( $email_twig_factory->create() )
)
);
}
return null;
}
/**
* @return bool
*/
private function is_editing_old_translation_and_te_is_used_for_old_translation() {
return Sanitize::stringProp( 'job_id', $_GET )
&& get_option( WPML_TM_Old_Jobs_Editor::OPTION_NAME ) === WPML_TM_Editors::WPML; }
/**
* @return bool
*/
private function is_ate_enabled_and_manager_wizard_completed() {
return WPML_TM_ATE_Status::is_enabled_and_activated() && (bool) get_option( WPML_TM_Wizard_Options::WIZARD_COMPLETE_FOR_MANAGER, false );
}
/**
* @return bool
*/
private function is_ajax_or_translation_queue() {
return wpml_is_ajax() || WPML_TM_Page::is_translation_queue();
}
}

View File

@@ -0,0 +1,126 @@
<?php
use WPML\DocPage;
class WPML_TM_ATE_Translator_Message_Classic_Editor implements IWPML_Action {
const ACTION = 'wpml_ate_translator_classic_editor';
const USER_OPTION = 'wpml_ate_translator_classic_editor_minimized';
/** @var WPML_Translation_Manager_Records */
private $translation_manager_records;
/** @var WPML_WP_User_Factory */
private $user_factory;
/** @var WPML_TM_ATE_Request_Activation_Email */
private $activation_email;
public function __construct(
WPML_Translation_Manager_Records $translation_manager_records,
WPML_WP_User_Factory $user_factory,
WPML_TM_ATE_Request_Activation_Email $activation_email
) {
$this->translation_manager_records = $translation_manager_records;
$this->user_factory = $user_factory;
$this->activation_email = $activation_email;
}
public function add_hooks() {
add_action( 'wpml_tm_editor_messages', array( $this, 'classic_editor_message' ) );
add_action( 'wp_ajax_' . self::ACTION, array( $this, 'handle_ajax' ) );
}
public function classic_editor_message() {
$main_message = esc_html__( "This site can use WPML's Advanced Translation Editor, but you did not receive permission to use it. You are still translating with WPML's classic translation editor. Please ask your site's Translation Manager to enable the Advanced Translation Editor for you.", 'wpml-translation-management' );
$learn_more = esc_html__( "Learn more about WPML's Advanced Translation Editor", 'wpml-translation-management' );
$short_message = esc_html__( 'Advanced Translation Editor is disabled.', 'wpml-translation-management' );
$more = esc_html__( 'More', 'wpml-translation-management' );
$request_activation = esc_html__( 'Request activation from', 'wpml-translation-management' );
$link = DocPage::aboutATE();
$show_minimized = (bool) $this->user_factory->create_current()->get_option( self::USER_OPTION );
?>
<div
class="notice notice-info otgs-notice js-classic-editor-notice"
data-nonce="<?php echo wp_create_nonce( self::ACTION ); ?>"
data-action="<?php echo self::ACTION; ?>"
<?php
if ( $show_minimized ) {
?>
style="display: none" <?php } ?>
>
<p><?php echo $main_message; ?></p>
<p><a href="<?php echo esc_attr( $link ); ?>" class="wpml-external-link" target="_blank"><?php echo $learn_more; ?></a></p>
<p>
<a class="button js-request-activation"><?php echo $request_activation; ?></a> <?php $this->output_translation_manager_list(); ?>
</p>
<p class="js-email-sent" style="display: none"></p>
<a class="js-minimize otgs-notice-toggle">
<?php esc_html_e( 'Minimize', 'wpml-translation-management' ); ?>
</a>
</div>
<div
class="notice notice-info otgs-notice js-classic-editor-notice-minimized"
<?php
if ( ! $show_minimized ) {
?>
style="display: none" <?php } ?>
>
<p><?php echo $short_message; ?> <a class="js-maximize"><?php echo $more; ?></a></p>
</div>
<?php
}
private function output_translation_manager_list() {
$translation_managers = $this->translation_manager_records->get_users_with_capability();
?>
<select class="js-translation-managers">
<?php
foreach ( $translation_managers as $translation_manager ) {
$display_name = $translation_manager->user_login . ' (' . $translation_manager->user_email . ')';
?>
<option
value="<?php echo $translation_manager->ID; ?> "><?php echo $display_name; ?></option>
<?php } ?>
</select>
<?php
}
public function handle_ajax() {
if ( wp_verify_nonce( $_POST['nonce'], self::ACTION ) ) {
$current_user = $this->user_factory->create_current();
switch ( $_POST['command'] ) {
case 'minimize':
$current_user->update_option( self::USER_OPTION, true );
wp_send_json_success( array( 'message' => '' ) );
case 'maximize':
$current_user->update_option( self::USER_OPTION, false );
wp_send_json_success( array( 'message' => '' ) );
case 'requestActivation':
$manager = $this->user_factory->create( (int) $_POST['manager'] );
if ( $this->activation_email->send_email( $manager, $current_user ) ) {
$message = sprintf(
esc_html__( 'An email has been sent to %s', 'wpml-translation-management' ),
$manager->user_login
);
} else {
$message = sprintf(
esc_html__( 'Sorry, the email could not be sent to %s for an unknown reason.', 'wpml-translation-management' ),
$manager->user_login
);
}
wp_send_json_success( array( 'message' => $message ) );
}
}
}
}

View File

@@ -0,0 +1,8 @@
<?php
class WPML_TM_Old_Editor_Factory implements IWPML_Backend_Action_Loader, IWPML_AJAX_Action_Loader {
public function create() {
return new WPML_TM_Old_Editor();
}
}

View File

@@ -0,0 +1,50 @@
<?php
class WPML_TM_Old_Editor implements IWPML_Action {
const ACTION = 'icl_ajx_custom_call';
const CUSTOM_AJAX_CALL = 'icl_doc_translation_method';
const NOTICE_ID = 'wpml-translation-management-old-editor';
const NOTICE_GROUP = 'wpml-translation-management';
public function add_hooks() {
add_action( self::ACTION, array( $this, 'handle_custom_ajax_call' ), 10, 2 );
}
public function handle_custom_ajax_call( $call, $data ) {
if ( self::CUSTOM_AJAX_CALL === $call ) {
if ( ! isset( $data[ WPML_TM_Old_Jobs_Editor::OPTION_NAME ] ) ) {
return;
}
$old_editor = $data[ WPML_TM_Old_Jobs_Editor::OPTION_NAME ];
if ( ! in_array( $old_editor, array( WPML_TM_Editors::WPML, WPML_TM_Editors::ATE ), true ) ) {
return;
}
update_option( WPML_TM_Old_Jobs_Editor::OPTION_NAME, $old_editor );
if ( WPML_TM_Editors::WPML === $old_editor && $this->is_ate_enabled_and_manager_wizard_completed() ) {
$text = __( 'You activated the Advanced Translation Editor for this site, but you are updating an old translation. WPML opened the Standard Translation Editor, so you can update this translation. When you translate new content, you\'ll get the Advanced Translation Editor with all its features. To change your settings, go to WPML Settings.', 'sitepress' );
$notice = new WPML_Notice( self::NOTICE_ID, $text, self::NOTICE_GROUP );
$notice->set_css_class_types( 'notice-info' );
$notice->set_dismissible( true );
$notice->add_display_callback( 'WPML_TM_Page::is_translation_editor_page' );
wpml_get_admin_notices()->add_notice( $notice, true );
} else {
wpml_get_admin_notices()->remove_notice( self::NOTICE_GROUP, self::NOTICE_ID );
}
}
}
/**
* @return bool
*/
private function is_ate_enabled_and_manager_wizard_completed() {
return WPML_TM_ATE_Status::is_enabled_and_activated() && (bool) get_option( WPML_TM_Wizard_Options::WIZARD_COMPLETE_FOR_MANAGER, false );
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace WPML\TM\ATE;
use stdClass;
class JobRecord {
/** @var int $wpmlJobId */
public $wpmlJobId;
/** @var int $ateJobId */
public $ateJobId;
/**
* @todo: Remove this property.
*
* @var int $editTimestamp
*/
public $editTimestamp = 0;
public function __construct( stdClass $dbRow = null ) {
if ( $dbRow ) {
$this->wpmlJobId = (int) $dbRow->job_id;
$this->ateJobId = (int) $dbRow->editor_job_id;
}
}
/**
* @todo: Remove the "$editTimestamp" and "is_editing", not handled on WPML side anymore.
*
* The job is considered as being edited if
* the timestamp is not greater than 1 day.
*
* @return bool
*/
public function isEditing() {
$elapsedTime = time() - $this->editTimestamp;
return $elapsedTime < DAY_IN_SECONDS;
}
}

View File

@@ -0,0 +1,205 @@
<?php
namespace WPML\TM\ATE;
use Exception;
use WPML\Collect\Support\Collection;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML_TM_ATE_API_Error;
use WPML_TM_Editors;
class JobRecords {
const FIELD_ATE_JOB_ID = 'ate_job_id';
const FIELD_IS_EDITING = 'is_editing';
/** @var \wpdb $wpdb */
private $wpdb;
/** @var Collection $jobs */
private $jobs;
public function __construct( \wpdb $wpdb ) {
$this->wpdb = $wpdb;
$this->jobs = wpml_collect( [] );
}
/**
* This method will retrieve data from the ATE job ID.
* Beware of the returned data shape which is not standard.
*
* @param int $ateJobId
*
* @return array|null
*/
public function get_data_from_ate_job_id( $ateJobId ) {
$ateJobId = (int) $ateJobId;
$this->warmCache( [], [ $ateJobId ] );
$job = $this->jobs->first(
function( JobRecord $job ) use ( $ateJobId ) {
return $job->ateJobId === $ateJobId;
}
);
if ( $job ) {
/** @var JobRecord $job */
return [
'wpml_job_id' => $job->wpmlJobId,
'ate_job_data' => [
'ate_job_id' => $job->ateJobId,
],
];
}
return null;
}
/**
* @param int $wpmlJobId
* @param array $ateJobData
*/
public function store( $wpmlJobId, array $ateJobData ) {
$ateJobData['job_id'] = (int) $wpmlJobId;
$this->warmCache( [ $wpmlJobId ] );
$job = $this->jobs->get( $wpmlJobId );
if ( ! $job ) {
$job = new JobRecord();
$job->wpmlJobId = (int) $wpmlJobId;
}
if ( isset( $ateJobData[ self::FIELD_ATE_JOB_ID ] ) ) {
$job->ateJobId = $ateJobData[ self::FIELD_ATE_JOB_ID ];
}
$this->persist( $job );
}
/**
* @param JobRecord $job
*/
public function persist( JobRecord $job ) {
$this->jobs->put( $job->wpmlJobId, $job );
$this->wpdb->update(
$this->wpdb->prefix . 'icl_translate_job',
[ 'editor_job_id' => $job->ateJobId ],
[ 'job_id' => $job->wpmlJobId ],
[ '%d' ],
[ '%d' ]
);
}
/**
* This method will load in-memory the required jobs.
*
* @param array $wpmlJobIds
* @param array $ateJobIds
*/
public function warmCache( array $wpmlJobIds, array $ateJobIds = [] ) {
$wpmlJobIds = wpml_collect( $wpmlJobIds )->reject( $this->isAlreadyLoaded( 'wpmlJobId' ) )->toArray();
$ateJobIds = wpml_collect( $ateJobIds )->reject( $this->isAlreadyLoaded( 'ateJobId' ) )->toArray();
$where = [];
if ( $wpmlJobIds ) {
$where[] = 'job_id IN(' . wpml_prepare_in( $wpmlJobIds, '%d' ) . ')';
}
if ( $ateJobIds ) {
$where[] = 'editor_job_id IN(' . wpml_prepare_in( $ateJobIds, '%d' ) . ')';
}
if ( ! $where ) {
return;
}
$whereHasJobIds = implode( ' OR ', $where );
$rows = $this->wpdb->get_results(
"
SELECT job_id, editor_job_id
FROM {$this->wpdb->prefix}icl_translate_job
WHERE editor = '" . WPML_TM_Editors::ATE . "' AND ({$whereHasJobIds})
"
);
foreach ( $rows as $row ) {
$job = new JobRecord( $row );
$this->jobs->put( $job->wpmlJobId, $job );
}
}
/**
* @param $idPropertyName
*
* @return \Closure
*/
private function isAlreadyLoaded( $idPropertyName ) {
$loadedIds = $this->jobs->pluck( $idPropertyName )->values()->toArray();
return Lst::includes( Fns::__, $loadedIds );
}
/**
* @param int $wpmlJobId
*
* @return int
*/
public function get_ate_job_id( $wpmlJobId ) {
return $this->get( $wpmlJobId )->ateJobId;
}
/**
* @param int $wpmlJobId
*
* @return bool
*/
public function is_editing_job( $wpmlJobId ) {
return $this->get( $wpmlJobId )->isEditing();
}
/**
* @param $wpmlJobId
*
* @return JobRecord
*/
public function get( $wpmlJobId ) {
if ( ! $this->jobs->has( $wpmlJobId ) ) {
$this->warmCache( [ (int) $wpmlJobId ] );
}
/** @var null|JobRecord $job */
$job = $this->jobs->get( $wpmlJobId );
if ( ! $job || ! $job->ateJobId ) {
$this->restoreJobDataFromATE( $wpmlJobId );
$job = $this->jobs->get( $wpmlJobId, new JobRecord() );
}
return $job;
}
/**
* This method will try to recover the job data from ATE server,
* and persist it in the local repository.
*
* @param int $wpmlJobId
*/
private function restoreJobDataFromATE( $wpmlJobId ) {
$data = apply_filters( 'wpml_tm_ate_job_data_fallback', [], $wpmlJobId );
if ( $data ) {
try {
$this->store( $wpmlJobId, $data );
} catch ( Exception $e ) {
$error_log = new WPML_TM_ATE_API_Error();
$error_log->log( $e->getMessage() );
}
}
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace WPML\TM\ATE;
use WPML\Element\API\Languages;
use WPML\FP\Cast;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\TM\ATE\Review\ReviewStatus;
class Jobs {
const LONGSTANDING_AT_ATE_SYNC_COUNT = 100;
/**
* @param array $statuses
*
* @return array
*/
public static function getJobsWithStatus( array $statuses ) {
if ( ! $statuses ) {
return [];
}
global $wpdb;
$needsReviewCondition = '1=0';
if ( Lst::includes( ICL_TM_NEEDS_REVIEW, $statuses ) ) {
$reviewStatuses = wpml_prepare_in( [ ReviewStatus::NEEDS_REVIEW, ReviewStatus::EDITING ] );
$needsReviewCondition = 'translation_status.review_status IN ( ' . $reviewStatuses . ' )';
}
$statuses = \wpml_prepare_in( $statuses, '%d' );
$languages = \wpml_prepare_in( Lst::pluck( 'code', Languages::getActive() ) );
$sql = "
SELECT jobs.rid, MAX(jobs.job_id) as jobId, jobs.editor_job_id as ateJobId, jobs.automatic , translation_status.status,
translation_status.review_status, jobs.ate_sync_count > " . static::LONGSTANDING_AT_ATE_SYNC_COUNT . " as isLongstanding
FROM {$wpdb->prefix}icl_translate_job as jobs
INNER JOIN {$wpdb->prefix}icl_translation_status translation_status ON translation_status.rid = jobs.rid
INNER JOIN {$wpdb->prefix}icl_translations translations ON translation_status.translation_id = translations.translation_id
INNER JOIN {$wpdb->prefix}icl_translations parent_translations ON translations.trid = parent_translations.trid
AND parent_translations.source_language_code IS NULL
INNER JOIN {$wpdb->prefix}posts posts ON parent_translations.element_id = posts.ID
WHERE
jobs.editor = %s
AND ( translation_status.status IN ({$statuses}) OR $needsReviewCondition )
AND translations.language_code IN ({$languages})
AND posts.post_status <> 'trash'
GROUP BY jobs.rid;
";
return Fns::map( Obj::evolve( [
'rid' => Cast::toInt(),
'jobId' => Cast::toInt(),
'ateJobId' => Cast::toInt(),
'automatic' => Cast::toBool(),
'status' => Cast::toInt(),
'isLongstanding' => Cast::toBool(),
] ), $wpdb->get_results( $wpdb->prepare( $sql, \WPML_TM_Editors::ATE ) ) );
}
/**
* @return array
*/
public static function getJobsToSync() {
return self::getJobsWithStatus( [ ICL_TM_WAITING_FOR_TRANSLATOR, ICL_TM_IN_PROGRESS, ICL_TM_ATE_NEEDS_RETRY ] );
}
/**
* @return array
*/
public static function getJobsToRetry() {
return self::getJobsWithStatus( [ ICL_TM_ATE_NEEDS_RETRY ] );
}
/**
* @return int
*/
public static function getTotal() {
global $wpdb;
$sql = "
SELECT COUNT(jobs.job_id)
FROM {$wpdb->prefix}icl_translate_job as jobs
WHERE jobs.editor = %s
";
return (int) $wpdb->get_var( $wpdb->prepare( $sql, \WPML_TM_Editors::ATE ) );
}
/**
* @return bool
*/
public static function isThereJob() {
global $wpdb;
$noOfRowsToFetch = 1;
$sql = $wpdb->prepare( "SELECT EXISTS(SELECT %d FROM {$wpdb->prefix}icl_translate_job)", $noOfRowsToFetch );
return boolval( $wpdb->get_var( $sql ) );
}
}

View File

@@ -0,0 +1,209 @@
<?php
namespace WPML\TM\ATE;
use WPML\API\Settings;
use WPML\DocPage;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\LIB\WP\Hooks;
use WPML\LIB\WP\User;
use WPML\Setup\Option;
use WPML\TM\ATE\AutoTranslate\Endpoint\AutoTranslate;
use WPML\TM\ATE\AutoTranslate\Endpoint\CancelJobs;
use WPML\TM\ATE\AutoTranslate\Endpoint\EnableATE;
use WPML\TM\ATE\AutoTranslate\Endpoint\GetATEJobsToSync;
use WPML\TM\ATE\AutoTranslate\Endpoint\GetCredits;
use WPML\TM\ATE\AutoTranslate\Endpoint\GetStatus;
use WPML\TM\ATE\AutoTranslate\Endpoint\RefreshJobsStatus;
use WPML\TM\ATE\AutoTranslate\Endpoint\SyncLock;
use WPML\TM\ATE\Download\Queue;
use WPML\TM\ATE\Sync\Trigger;
use WPML\TM\WP\App\Resources;
use WPML\UIPage;
use function WPML\Container\make;
use function WPML\FP\invoke;
class Loader implements \IWPML_Backend_Action {
const JOB_ID_PLACEHOLDER = '###';
public function add_hooks() {
if ( wpml_is_ajax() ) {
// Prevent loading this for ajax calls.
// All tasks of this class are not relevant for ajax requests. Currently it's loaded by the root plugin.php
// which do not separate between ajax and non-ajax calls and loads this whenever is_admin() is true.
// Problem: ALL ajax calls return true for is_admin() - also on the frontend and for non logged-in users.
// TODO: Remove once wpmltm-4351 is done.
return;
}
if ( UIPage::isTMJobs( $_GET ) ) {
return;
}
if (
\WPML_TM_ATE_Status::is_enabled_and_activated()
|| Settings::pathOr( false, [ 'translation-management', 'doc_translation_method' ] ) === ICL_TM_TMETHOD_ATE
) {
StatusBar::add_hooks();
Hooks::onAction( 'in_admin_header' )
->then( [ self::class, 'showAteConsoleContainer' ] );
}
Hooks::onAction( 'wp_loaded' )
->then( [ self::class, 'getData' ] )
->then( Resources::enqueueApp( 'ate-jobs-sync' ) )
->then( Fns::always( make( \WPML_TM_Scripts_Factory::class ) ) )
->then( invoke( 'localize_script' )->with( 'wpml-ate-jobs-sync-ui' ) );
Hooks::onFilter( 'wpml_tm_get_wpml_auto_translate_container' )
->then( [ self::class, 'getWpmlAutoTranslateContainer' ] );
}
public static function getData() {
$jobsToSync = Jobs::getJobsToSync();
$anyJobsExist = Jobs::isThereJob();
$ateTab = admin_url( UIPage::getTMATE() );
return [
'name' => 'ate_jobs_sync',
'data' => [
'endpoints' => self::getEndpoints(),
'urls' => self::getUrls( $ateTab ),
'jobIdPlaceHolder' => self::JOB_ID_PLACEHOLDER,
'notices' => StatusBar::getNotices(),
'isTranslationManager' => User::getCurrent()->has_cap( \WPML_Manage_Translations_Role::CAPABILITY ),
'jobsToSync' => $jobsToSync,
'anyJobsExist' => $anyJobsExist,
'totalJobsCount' => Jobs::getTotal(),
'needsReviewCount' => count( Jobs::getJobsWithStatus( [ ICL_TM_NEEDS_REVIEW ] ) ),
'shouldTranslateEverything' => Option::shouldTranslateEverything() && ! TranslateEverything::isEverythingProcessed( true ),
'isAutomaticTranslations' => Option::shouldTranslateEverything(),
'isSyncRequired' => count( $jobsToSync ),
'needsFetchCredit' => Option::shouldTranslateEverything() && UIPage::isTMDashboard( $_GET ),
'strings' => self::getStrings(),
'ateConsole' => self::getAteData( Lst::pluck( 'ateJobId', $jobsToSync ) ),
'isAteActive' => \WPML_TM_ATE_Status::is_enabled_and_activated(),
'editorMode' => Settings::pathOr( false, [ 'translation-management', 'doc_translation_method' ] ),
],
];
}
/**
* @return string
*/
public static function getNotEnoughCreditPopup() {
$isTranslationManager = User::getCurrent()->has_cap( \WPML_Manage_Translations_Role::CAPABILITY );
$content = $isTranslationManager
? __(
"There is an issue with automatic translation that needs your attention.",
'wpml-translation-management'
)
: __(
" There is an issue with automatic translation that needs attention from a translation manager.",
'wpml-translation-management'
);
$fix = __( 'Fix it to continue translating automatically', 'wpml-translation-management' );
$primaryButton = $isTranslationManager
? '<button class="wpml-antd-button wpml-antd-button-primary" onclick="CREDITS_ACTION">' . $fix . '</button>'
: '';
$translate = __( 'Translate content myself', 'wpml-translation-management' );
$secondaryButton = UIPage::isTMDashboard( $_GET ) || ! $isTranslationManager
? ''
: '<button class="wpml-antd-button wpml-antd-button-secondary" onclick="window.location.href=\'TRANSLATE_LINK\'">' . $translate . '</button>';
return '<div class="wpml-not-enough-credit-popup">' .
'<p>' . $content . '</p>' .
$primaryButton .
$secondaryButton .
'</div>';
}
public static function showAteConsoleContainer() {
echo '<div id="wpml-ate-console-container"></div>';
}
public static function getWpmlAutoTranslateContainer() {
return '<div id="wpml-auto-translate" style="display:none">
<div class="content"></div>
<div class="connect"></div>
</div>';
}
private static function getAteData( $ateJobIds ) {
$registration_data = make( \WPML_TM_AMS_API::class )->get_registration_data();
return User::getCurrent()->has_cap( \WPML_Manage_Translations_Role::CAPABILITY )
? [
'host' => make( \WPML_TM_ATE_AMS_Endpoints::class )->get_base_url( \WPML_TM_ATE_AMS_Endpoints::SERVICE_AMS ),
'wpml_host' => get_site_url(),
'job_list' => $ateJobIds,
'widget_mode' => 'issue_solving',
'return_url' => \WPML\TM\API\Jobs::getCurrentUrl(),
'secret_key' => Obj::prop( 'secret', $registration_data ),
'shared_key' => Obj::prop( 'shared', $registration_data ),
'website_uuid' => make( \WPML_TM_ATE_Authentication::class )->get_site_id(),
'ui_language' => make( \SitePress::class )->get_user_admin_language( User::getCurrentId() ),
'restNonce' => wp_create_nonce( 'wp_rest' ),
'container' => '#wpml-ate-console-container',
]
: false;
}
private static function getEndpoints() {
return [
'auto-translate' => AutoTranslate::class,
'translate-everything' => TranslateEverything::class,
'getCredits' => GetCredits::class,
'enableATE' => EnableATE::class,
'getATEJobsToSync' => GetATEJobsToSync::class,
'syncLock' => SyncLock::class,
];
}
private static function getUrls( $ateTab ) {
return [
'editor' => \WPML_TM_Translation_Status_Display::get_link_for_existing_job( self::JOB_ID_PLACEHOLDER ),
'ateams' => $ateTab,
'automaticSettings' => \admin_url( UIPage::getSettings() ),
'translateAutomaticallyDoc' => DocPage::getTranslateAutomatically(),
'ateConsole' => make( \WPML_TM_ATE_AMS_Endpoints::class )
->get_base_url( \WPML_TM_ATE_AMS_Endpoints::SERVICE_AMS ) . '/mini_app/main.js',
'translationQueue' => \add_query_arg(
[ 'status' => ICL_TM_NEEDS_REVIEW ],
\admin_url( UIPage::getTranslationQueue() )
),
'currentUrl' => \WPML\TM\API\Jobs::getCurrentUrl(),
];
}
private static function getStrings() {
return [
'tooltip' => __(
'Processing translation (could take a few minutes)',
'wpml-translation-management'
),
'refreshing' => __( 'Refreshing translation status', 'wpml-translation-management' ),
'inProgress' => __( 'Translation in progress', 'wpml-translation-management' ),
'editTranslation' => __( 'Edit translation', 'wpml-translation-management' ),
'status' => __( 'Processing translation', 'wpml-translation-management' ),
'automaticTranslation' => __( 'This content is being automatically translated. If you want to do something different with it cancel translation jobs first.', 'wpml-translation-management' ),
'notEnoughCredit' => self::getNotEnoughCreditPopup(),
'cancelled' => __( 'Translation has been cancelled', 'wpml-translation-management' ),
];
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace WPML\TM\ATE\Log;
class Entry {
/**
* @var int $timestamp The log's creation timestamp.
*/
public $timestamp = 0;
/**
* @see EventsTypes
*
* @var int $eventType The event code that triggered the log.
*/
public $eventType = 0;
/**
* @var string $description The details of the log (e.g. exception message).
*/
public $description = '';
/**
* @var int $wpmlJobId [Optional] The WPML Job ID (when applies).
*/
public $wpmlJobId = 0;
/**
* @var int $ateJobId [Optional] The ATE Job ID (when applies).
*/
public $ateJobId = 0;
/**
* @var array $extraData [Optional] Complementary serialized data (e.g. API request/response data).
*/
public $extraData = [];
/**
* @param array $item
*
* @return Entry
*/
public function __construct( array $item = null ) {
if ( $item ) {
$this->timestamp = (int) $item['timestamp'];
$this->eventType = (int) ( isset( $item['eventType'] ) ? $item['eventType'] : $item['event'] );
$this->description = $item['description'];
$this->wpmlJobId = (int) $item['wpmlJobId'];
$this->ateJobId = (int) $item['ateJobId'];
$this->extraData = (array) $item['extraData'];
}
}
public static function createForType($eventType, $extraData) {
$entry = new self();
$entry->eventType = $eventType;
$entry->extraData = $extraData;
return $entry;
}
public static function retryJob( $wpmlJobId, $extraData ) {
$entry = self::createForType(EventsTypes::JOB_RETRY, $extraData);
$entry->wpmlJobId = $wpmlJobId;
return $entry;
}
/**
* @return string
*/
public function getFormattedDate() {
return date_i18n( 'Y/m/d g:i:s A', $this->timestamp );
}
/**
* @return string
*/
public function getExtraDataToString() {
return json_encode( $this->extraData );
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace WPML\TM\ATE\Log;
class EventsTypes {
/** Communication errors */
const SERVER_ATE = 1;
const SERVER_AMS = 2;
const SERVER_XLIFF = 3;
/** Internal errors */
const JOB_DOWNLOAD = 10;
/** Retry */
const JOB_RETRY = 20;
const SITE_REGISTRATION_RETRY = 21;
/** Sync */
const JOBS_SYNC = 30;
public static function getLabel( $eventType ) {
return wpml_collect(
[
EventsTypes::SERVER_ATE => 'ATE Server Communication',
EventsTypes::SERVER_AMS => 'AMS Server Communication',
EventsTypes::SERVER_XLIFF => 'XLIFF Server Communication',
EventsTypes::JOB_DOWNLOAD => 'Job Download',
EventsTypes::JOB_RETRY => 'Job resent to ATE',
EventsTypes::SITE_REGISTRATION_RETRY => 'Site registration request resent to ATE',
EventsTypes::JOBS_SYNC => 'Jobs sync request sent to ATE failed',
]
)->get( $eventType, '' );
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace WPML\TM\ATE\Log;
class Hooks implements \IWPML_Backend_Action, \IWPML_DIC_Action {
const SUBMENU_HANDLE = 'wpml-tm-ate-log';
/** @var ViewFactory $viewFactory */
private $viewFactory;
public function __construct( ViewFactory $viewFactory ) {
$this->viewFactory = $viewFactory;
}
public function add_hooks() {
add_action( 'wpml_support_page_after', [ $this, 'renderSupportSection' ] );
add_action( 'admin_menu', [ $this, 'addLogSubmenuPage' ] );
}
public function renderSupportSection() {
$this->viewFactory->create()->renderSupportSection();
}
public function addLogSubmenuPage() {
add_submenu_page(
WPML_PLUGIN_FOLDER . '/menu/support.php',
__( 'Advanced Translation Editor Error Logs', 'wpml-translation-management' ),
'ATE logs',
'manage_options',
self::SUBMENU_HANDLE,
[ $this, 'renderPage' ]
);
}
public function renderPage() {
$this->viewFactory->create()->renderPage();
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace WPML\TM\ATE\Log;
use WPML\Collect\Support\Collection;
use WPML\WP\OptionManager;
class Storage {
const OPTION_GROUP = 'TM\ATE\Log';
const OPTION_NAME = 'logs';
const MAX_ENTRIES = 50;
public static function add( Entry $entry, $avoidDuplication = false ) {
$entry->timestamp = $entry->timestamp ?: time();
$entries = self::getAll();
if ( $avoidDuplication ) {
$entries = $entries->reject(
function( $iteratedEntry ) use ( $entry ) {
return (
$iteratedEntry->wpmlJobId === $entry->wpmlJobId
&& $entry->ateJobId === $iteratedEntry->ateJobId
&& $entry->description === $iteratedEntry->description
&& $entry->eventType === $iteratedEntry->eventType
);
}
);
}
$entries->prepend( $entry );
$newOptionValue = $entries->forPage( 1, self::MAX_ENTRIES )
->map(
function( Entry $entry ) {
return (array) $entry; }
)
->toArray();
OptionManager::updateWithoutAutoLoad( self::OPTION_NAME, self::OPTION_GROUP, $newOptionValue );
}
/**
* @param Entry $entry
*/
public static function remove( Entry $entry ) {
$entries = self::getAll();
$entries = $entries->reject(
function( $iteratedEntry ) use ( $entry ) {
return $iteratedEntry->timestamp === $entry->timestamp && $entry->ateJobId === $iteratedEntry->ateJobId;
}
);
$newOptionValue = $entries->forPage( 1, self::MAX_ENTRIES )
->map(
function( Entry $entry ) {
return (array) $entry; }
)
->toArray();
OptionManager::updateWithoutAutoLoad( self::OPTION_NAME, self::OPTION_GROUP, $newOptionValue );
}
/**
* @return Collection Collection of Entry objects.
*/
public static function getAll() {
return wpml_collect( OptionManager::getOr( [], self::OPTION_NAME, self::OPTION_GROUP ) )
->map(
function( array $item ) {
return new Entry( $item );
}
);
}
}

View File

@@ -0,0 +1,113 @@
<?php
namespace WPML\TM\ATE\Log;
use WPML\Collect\Support\Collection;
class View {
/** @var Collection $logs */
private $logs;
public function __construct( Collection $logs ) {
$this->logs = $logs;
}
public function renderSupportSection() {
?>
<div class="wrap">
<h2 id="ate-log">
<?php esc_html_e( 'Advanced Translation Editor', 'wpml-translation-management' ); ?>
</h2>
<p>
<a href="<?php echo admin_url( 'admin.php?page=' . Hooks::SUBMENU_HANDLE ); ?>">
<?php echo sprintf( esc_html__( 'Error Logs (%d)', 'wpml-translation-management' ), $this->logs->count() ); ?>
</a>
</p>
</div>
<?php
}
public function renderPage() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'Advanced Translation Editor Error Logs', 'wpml-translation-management' ); ?></h1>
<br>
<table class="wp-list-table widefat fixed striped posts">
<thead><?php $this->renderTableHeader(); ?></thead>
<tbody id="the-list">
<?php
if ( $this->logs->isEmpty() ) {
$this->renderEmptyTable();
} else {
$this->logs->each( [ $this, 'renderTableRow' ] );
}
?>
</tbody>
<tfoot><?php $this->renderTableHeader(); ?></tfoot>
</table>
</div>
<?php
}
private function renderTableHeader() {
?>
<tr>
<th class="date">
<span><?php esc_html_e( 'Date', 'wpml-translation-management' ); ?></span>
</th>
<th class="event">
<span><?php esc_html_e( 'Event', 'wpml-translation-management' ); ?></span>
</th>
<th class="description">
<span><?php esc_html_e( 'Description', 'wpml-translation-management' ); ?></span>
</th>
<th class="wpml-job-id">
<span><?php esc_html_e( 'WPML Job ID', 'wpml-translation-management' ); ?></span>
</th>
<th class="ate-job-id">
<span><?php esc_html_e( 'ATE Job ID', 'wpml-translation-management' ); ?></span>
</th>
<th class="extra-data">
<span><?php esc_html_e( 'Extra data', 'wpml-translation-management' ); ?></span>
</th>
</tr>
<?php
}
public function renderTableRow( Entry $entry ) {
?>
<tr>
<td class="date">
<?php echo esc_html( $entry->getFormattedDate() ); ?>
</td>
<td class="event">
<?php echo esc_html( EventsTypes::getLabel( $entry->eventType ) ); ?>
</td>
<td class="description">
<?php echo esc_html( $entry->description ); ?>
</td>
<td class="wpml-job-id">
<?php echo esc_html( $entry->wpmlJobId ); ?>
</td>
<td class="ate-job-id">
<?php echo esc_html( $entry->ateJobId ); ?>
</td>
<td class="extra-data">
<?php echo esc_html( $entry->getExtraDataToString() ); ?>
</td>
</tr>
<?php
}
private function renderEmptyTable() {
?>
<tr>
<td colspan="6" class="title column-title has-row-actions column-primary">
<?php esc_html_e( 'No entries', 'wpml-translation-management' ); ?>
</td>
</tr>
<?php
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace WPML\TM\ATE\Log;
use function WPML\Container\make;
class ViewFactory {
public function create() {
$logs = make( Storage::class )->getAll();
return new View( $logs );
}
}

View File

@@ -0,0 +1,101 @@
<?php
/**
* @author OnTheGo Systems
*/
namespace WPML\TM\ATE\REST;
use WP_REST_Request;
use WPML\Collect\Support\Collection;
use WPML\Element\API\PostTranslations;
use WPML\FP\Cast;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\TM\API\Jobs;
use WPML\TM\ATE\Download\Process;
use WPML\TM\ATE\Review\PreviewLink;
use WPML\TM\ATE\Review\ReviewStatus;
use WPML\TM\ATE\Review\StatusIcons;
use WPML\TM\ATE\SyncLock;
use WPML\TM\REST\Base;
use WPML_TM_ATE_AMS_Endpoints;
use function WPML\Container\make;
use function WPML\FP\pipe;
class Download extends Base {
/**
* @return array
*/
public function get_routes() {
return [
[
'route' => WPML_TM_ATE_AMS_Endpoints::DOWNLOAD_JOBS,
'args' => [
'methods' => 'POST',
'callback' => [ $this, 'download' ],
],
],
];
}
/**
* @param WP_REST_Request $request
*
* @return array
*/
public function get_allowed_capabilities( WP_REST_Request $request ) {
return [
'manage_options',
'manage_translations',
'translate',
];
}
public function download( WP_REST_Request $request ) {
$lock = make( SyncLock::class );
if ( ! $lock->create( $request->get_param( 'lockKey' ) ) ) {
return [];
}
$jobs = make( Process::class )->run( $request->get_param( 'jobs' ) );
return $this->getJobs( $jobs, $request->get_param( 'returnUrl' ) )->all();
}
/**
* @param Collection $processedJobs
* @param string $returnUrl
*
* @return Collection
*/
public static function getJobs( Collection $processedJobs, $returnUrl ) {
$getLink = Logic::ifElse(
ReviewStatus::doesJobNeedReview(),
Fns::converge( PreviewLink::getWithSpecifiedReturnUrl( $returnUrl ), [ Obj::prop( 'translatedPostId' ), Obj::prop( 'jobId' ) ] ),
pipe( Obj::prop( 'jobId' ), Jobs::getEditUrl( $returnUrl ) )
);
$getLabel = Logic::ifElse(
ReviewStatus::doesJobNeedReview(),
StatusIcons::getReviewTitle( 'language_code' ),
StatusIcons::getEditTitle( 'language_code' )
);
return $processedJobs->pluck( 'jobId' )
->map( Jobs::get() )
->map( Obj::addProp( 'translatedPostId', Jobs::getTranslatedPostId() ) )
->map( Obj::renameProp( 'job_id', 'jobId' ) )
->map( Obj::renameProp( 'editor_job_id', 'ateJobId' ) )
->map( Obj::addProp( 'viewLink', $getLink ) )
->map( Obj::addProp( 'label', $getLabel ) )
->map( Obj::pick( [ 'jobId', 'viewLink', 'automatic', 'status', 'label', 'review_status', 'ateJobId' ] ) )
->map( Obj::evolve( [
'jobId' => Cast::toInt(),
'automatic' => Cast::toInt(),
'status' => Cast::toInt(),
'ateJobId' => Cast::toInt(),
] ) );
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace WPML\TM\ATE\REST;
use WPML\TM\ATE\ReturnedJobsQueue;
use WP_REST_Request;
use WPML\Rest\Adaptor;
use WPML\TM\REST\Base;
use WPML_TM_ATE_AMS_Endpoints;
use \WPML_TM_ATE_API;
use \WPML_TM_ATE_Jobs;
use \WPML_TM_Jobs_Repository;
use WPML\FP\Obj;
use WPML\TM\ATE\Log\Entry;
use WPML\TM\ATE\Log\EventsTypes;
class FixJob extends Base {
/**
* @var WPML_TM_ATE_Jobs
*/
private $ateJobs;
/**
* @var WPML_TM_ATE_API
*/
private $ateApi;
/**
* @var WPML_TM_Jobs_Repository
*/
private $jobsRepository;
const PARAM_ATE_JOB_ID = 'ateJobId';
const PARAM_WPML_JOB_ID = 'jobId';
public function __construct( Adaptor $adaptor, WPML_TM_ATE_API $ateApi, WPML_TM_ATE_Jobs $ateJobs ) {
parent::__construct( $adaptor );
$this->ateApi = $ateApi;
$this->ateJobs = $ateJobs;
$this->jobsRepository = wpml_tm_get_jobs_repository();
}
/**
* @return array
*/
public function get_routes() {
return [
[
'route' => WPML_TM_ATE_AMS_Endpoints::FIX_JOB,
'args' => [
'methods' => 'GET',
'callback' => [ $this, 'fix_job' ],
],
],
];
}
/**
* @param WP_REST_Request $request
*
* @return array
*/
public function get_allowed_capabilities( WP_REST_Request $request ) {
return [
'manage_options',
'manage_translations',
'translate',
];
}
/**
* @param WP_REST_Request $request
*
* @return bool[]
*/
public function fix_job( WP_REST_Request $request ) {
try {
$ateJobId = $request->get_param( self::PARAM_ATE_JOB_ID );
$wpmlJobId = $request->get_param( self::PARAM_WPML_JOB_ID );
$processedJobResult = $this->process( $ateJobId, $wpmlJobId );
if ( $processedJobResult ) {
return [ 'completed' => true, 'error' => false ];
}
} catch ( \Exception $e ) {
$this->logException( $e, [ 'ateJobId' => $ateJobId, 'wpmlJobId' => $wpmlJobId ] );
return [ 'completed' => false, 'error' => true ];
}
return [ 'completed' => false, 'error' => false ];
}
/**
* Processes the job status.
*
* @param $ateJobId
* @param $wpmlJobId
*
* @return bool
* @throws \Requests_Exception
*/
public function process( $ateJobId, $wpmlJobId ) {
$ateJob = $this->ateApi->get_job( $ateJobId )->$ateJobId;
$xliffUrl = Obj::prop('translated_xliff', $ateJob);
if ( $xliffUrl ) {
$xliffContent = $this->ateApi->get_remote_xliff_content( $xliffUrl, [ 'jobId' => $wpmlJobId, 'ateJobId' => $ateJobId ] );
$receivedWpmlJobId = $this->ateJobs->apply( $xliffContent );
if ( $receivedWpmlJobId && intval( $receivedWpmlJobId ) !== intval( $wpmlJobId ) ) {
$error_message = sprintf( 'The received wpmlJobId (%s) does not match (%s).', $receivedWpmlJobId, $wpmlJobId );
throw new \Exception( $error_message );
}
if ( $receivedWpmlJobId ) {
ReturnedJobsQueue::remove( $wpmlJobId );
return true;
}
}
return false;
}
/**
* @param \Exception $e
* @param array|null $job
*/
private function logException( \Exception $e, $job = null ) {
$entry = new Entry();
$entry->description = $e->getMessage();
if ( $job ) {
$entry->ateJobId = Obj::prop('ateJobId', $job);
$entry->wpmlJobId = Obj::prop('wpmlJobId', $job);
$entry->extraData = [ 'downloadUrl' => Obj::prop('url', $job) ];
}
if ( $e instanceof \Requests_Exception ) {
$entry->eventType = EventsTypes::SERVER_XLIFF;
} else {
$entry->eventType = EventsTypes::JOB_DOWNLOAD;
}
wpml_tm_ate_ams_log( $entry );
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace WPML\TM\ATE\REST;
use WPML\FP\Obj;
use WPML\TM\API\ATE;
use WPML\FP\Maybe;
use WPML\FP\Fns;
use WPML\TM\API\Jobs;
use WPML\TM\REST\Base;
use function WPML\Container\make;
use function WPML\FP\pipe;
use function WPML\FP\curryN;
/**
* @author OnTheGo Systems
*/
class PublicReceive extends \WPML_TM_ATE_Required_Rest_Base {
const CODE_UNPROCESSABLE_ENTITY = 422;
const CODE_OK = 200;
const ENDPOINT_JOBS_RECEIVE = '/ate/jobs/receive/';
function add_hooks() {
$this->register_routes();
}
function register_routes() {
parent::register_route(
self::ENDPOINT_JOBS_RECEIVE . '(?P<wpmlJobId>\d+)',
array(
'methods' => 'GET',
'callback' => array( $this, 'receive_ate_job' ),
'args' => array(
'wpmlJobId' => array(
'required' => true,
'type' => 'int',
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'integer' ),
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'integer' ),
),
),
'permission_callback' => '__return_true',
)
);
}
public function get_allowed_capabilities( \WP_REST_Request $request ) {
return [];
}
/**
* @param \WP_REST_Request $request
*
* @return true|\WP_Error
*/
public function receive_ate_job( \WP_REST_Request $request ) {
$wpmlJobId = $request->get_param( 'wpmlJobId' );
$ateAPI = make( ATE::class );
$getXLIFF = pipe(
Obj::prop( 'job_id' ),
Fns::safe( [ $ateAPI, 'checkJobStatus' ] ),
Fns::map( Obj::prop( 'translated_xliff' ) )
);
$applyTranslations = Fns::converge(
Fns::liftA3( curryN( 3, [ $ateAPI, 'applyTranslation' ] ) ),
[
Fns::safe( Obj::prop( 'job_id' ) ),
Fns::safe( Obj::prop( 'original_doc_id' ) ),
$getXLIFF
]
);
return Maybe::of( $wpmlJobId )
->map( Jobs::get() )
->chain( $applyTranslations )
->map( Fns::always( new \WP_REST_Response( null, self::CODE_OK ) ) )
->getOrElse( new \WP_Error( self::CODE_UNPROCESSABLE_ENTITY ) );
}
/**
* @param int $wpml_job_id
*
* @return string
*/
public static function get_receive_ate_job_url( $wpml_job_id ) {
return self::get_url( self::ENDPOINT_JOBS_RECEIVE . $wpml_job_id );
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace WPML\TM\ATE\REST;
use WP_REST_Request;
use WPML\TM\ATE\Retry\Arguments;
use WPML\TM\ATE\Retry\Process;
use WPML\TM\REST\Base;
use WPML_TM_ATE_AMS_Endpoints;
use function WPML\Container\make;
class Retry extends Base {
/**
* @return array
*/
public function get_routes() {
return [
[
'route' => WPML_TM_ATE_AMS_Endpoints::RETRY_JOBS,
'args' => [
'methods' => 'POST',
'callback' => [ $this, 'retry' ],
],
],
];
}
/**
* @param WP_REST_Request $request
*
* @return array
*/
public function get_allowed_capabilities( WP_REST_Request $request ) {
return [
'manage_options',
'manage_translations',
'translate',
];
}
/**
* @param WP_REST_Request $request
*
* @return array
* @throws \Auryn\InjectionException
*/
public function retry( WP_REST_Request $request ) {
return (array) make( Process::class )->run( $request->get_param( 'jobsToProcess' ) );
}
}

View File

@@ -0,0 +1,150 @@
<?php
namespace WPML\TM\ATE\REST;
use WP_REST_Request;
use WPML\Collect\Support\Collection;
use WPML\FP\Cast;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\Rest\Adaptor;
use WPML\TM\API\Jobs;
use WPML\TM\ATE\Download\Job;
use WPML\TM\ATE\Review\PreviewLink;
use WPML\TM\ATE\Review\ReviewStatus;
use WPML\TM\ATE\Review\StatusIcons;
use WPML\TM\ATE\Sync\Arguments;
use WPML\TM\ATE\Sync\Factory;
use WPML\TM\ATE\Sync\Process;
use WPML\TM\ATE\Sync\Result;
use WPML\TM\ATE\SyncLock;
use WPML\TM\REST\Base;
use WPML\Utilities\KeyedLock;
use WPML_TM_ATE_AMS_Endpoints;
use function WPML\Container\make;
use function WPML\FP\pipe;
class Sync extends Base {
/**
* @return array
*/
public function get_routes() {
return [
[
'route' => WPML_TM_ATE_AMS_Endpoints::SYNC_JOBS,
'args' => [
'methods' => 'POST',
'callback' => [ $this, 'sync' ],
'args' => [
'lockKey' => self::getStringType(),
'ateToken' => self::getStringType(),
'page' => self::getIntType(),
'numberOfPages' => self::getIntType(),
],
],
],
];
}
/**
* @param WP_REST_Request $request
*
* @return array
*/
public function get_allowed_capabilities( WP_REST_Request $request ) {
return [
'manage_options',
'manage_translations',
'translate',
];
}
/**
* @param WP_REST_Request $request
*
* @return array
* @throws \Auryn\InjectionException
*/
public function sync( WP_REST_Request $request ) {
$args = new Arguments();
$args->ateToken = $request->get_param( 'ateToken' );
$args->page = $request->get_param( 'nextPage' );
$args->numberOfPages = $request->get_param( 'numberOfPages' );
$args->includeManualAndLongstandingJobs = $request->get_param( 'includeManualAndLongstandingJobs' );
$lock = make( SyncLock::class );
$lockKey = $lock->create( $request->get_param( 'lockKey' ) );
if ( $lockKey ) {
$result = make( Process::class )->run( $args );
$result->lockKey = $lockKey;
$jobsFromDB = Fns::filter(
Logic::complement( $this->findSyncedJob( $result->jobs ) ),
$this->getJobStatuses( $request->get_param( 'jobIds' ), $request->get_param( 'returnUrl' ) )
);
$result = $this->createResultWithJobs( Lst::concat( $result->jobs, $jobsFromDB ), $result );
} else {
$result = $this->createResultWithJobs( $this->getJobStatuses( $request->get_param( 'jobIds' ), $request->get_param( 'returnUrl' ) ) );
}
return (array) $result;
}
private function getJobStatuses( $wpmlJobIds, $returnUrl ) {
if ( ! $wpmlJobIds ) {
return [];
}
global $wpdb;
$ids = wpml_prepare_in( $wpmlJobIds, '%d' );
$sql = "
SELECT jobs.job_id as jobId, statuses.status as status, jobs.editor_job_id as ateJobId FROM {$wpdb->prefix}icl_translate_job as jobs
INNER JOIN {$wpdb->prefix}icl_translation_status as statuses ON statuses.rid = jobs.rid
WHERE jobs.job_id IN ( {$ids} ) AND 1 = %d
"; // I need additional AND condition to utilize prepare function which is required to make writing unit tests easier. It's not perfect but saves a lot of time now
$result = $wpdb->get_results( $wpdb->prepare( $sql , 1) );
if ( ! is_array( $result ) ) {
return [];
}
$jobs = Fns::map( Obj::evolve( [
'jobId' => Cast::toInt(),
'status' => Cast::toInt(),
'ateJobId' => Cast::toInt(),
] ), $result );
list( $completed, $notCompleted ) = \wpml_collect( $jobs )->partition( Relation::propEq( 'status', ICL_TM_COMPLETE ) );
if ( count( $completed ) ) {
$completed = Download::getJobs( $completed, $returnUrl )->map( function ( $job ) {
return (array) $job;
} );
}
return $completed->merge( $notCompleted )->all();
}
private function findSyncedJob( $jobsFromATE ) {
return function ( $jobFromDb ) use ( $jobsFromATE ) {
return Lst::find( Relation::propEq( 'jobId', Obj::prop( 'jobId', $jobFromDb ) ), $jobsFromATE );
};
}
/**
* @param array $jobs
* @param Result|null $template
*
* @return Result
*/
private function createResultWithJobs( array $jobs, Result $template = null ) {
$result = $template ? clone $template : new Result();
$result->jobs = $jobs;
return $result;
}
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* @author OnTheGo Systems
*/
abstract class WPML_TM_ATE_Required_Rest_Base extends WPML_REST_Base {
const REST_NAMESPACE = 'wpml/tm/v1';
/**
* WPML_TM_ATE_Required_Rest_Base constructor.
*/
public function __construct() {
parent::__construct( self::REST_NAMESPACE );
}
/**
* @param WP_REST_Request $request
*
* @return bool
*/
public function validate_permission( WP_REST_Request $request ) {
return WPML_TM_ATE_Status::is_enabled() && parent::validate_permission( $request );
}
/**
* @param string $endpoint
*
* @return string
*/
static function get_url( $endpoint ) {
return get_rest_url( null, '/' . self::REST_NAMESPACE . $endpoint );
}
}

View File

@@ -0,0 +1,12 @@
<?php
class WPML_TM_REST_AMS_Clients_Factory extends WPML_REST_Factory_Loader {
/**
* @return \WPML_TM_REST_AMS_Clients
* @throws \Auryn\InjectionException
*/
public function create() {
return \WPML\Container\make( '\WPML_TM_REST_AMS_Clients' );
}
}

View File

@@ -0,0 +1,152 @@
<?php
/**
* @author OnTheGo Systems
*/
use WPML\FP\Fns;
use WPML\FP\Obj;
class WPML_TM_REST_AMS_Clients extends WPML_REST_Base {
private $api;
private $ams_user_records;
/** @var WPML_TM_AMS_Translator_Activation_Records $translator_activation_records */
private $translator_activation_records;
/**
* @var WPML_TM_ATE_AMS_Endpoints
*/
private $strings;
public function __construct(
WPML_TM_AMS_API $api,
WPML_TM_AMS_Users $ams_user_records,
WPML_TM_AMS_Translator_Activation_Records $translator_activation_records,
WPML_TM_MCS_ATE_Strings $strings
) {
parent::__construct( 'wpml/tm/v1' );
$this->api = $api;
$this->ams_user_records = $ams_user_records;
$this->translator_activation_records = $translator_activation_records;
$this->strings = $strings;
}
function add_hooks() {
$this->register_routes();
}
function register_routes() {
parent::register_route(
'/ams/register_manager',
array(
'methods' => 'POST',
'callback' => array( $this, 'register_manager' ),
)
);
parent::register_route(
'/ams/synchronize/translators',
array(
'methods' => 'GET',
'callback' => array( $this, 'synchronize_translators' ),
)
);
parent::register_route(
'/ams/synchronize/managers',
array(
'methods' => 'GET',
'callback' => array( $this, 'synchronize_managers' ),
)
);
parent::register_route(
'/ams/status',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_status' ),
)
);
parent::register_route(
'/ams/console',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_console' ),
)
);
}
/**
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function register_manager() {
$current_user = wp_get_current_user();
$translators = $this->ams_user_records->get_translators();
$managers = $this->ams_user_records->get_managers();
$handleError = function ( $error ) {
return [
'enabled' => false,
'error' => $error
];
};
return $this->api->register_manager( $current_user, $translators, $managers )
->coalesce( $handleError, Fns::always( [ 'enabled' => true ] ) )
->get();
}
/**
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function synchronize_translators() {
$translators = $this->ams_user_records->get_translators();
$result = $this->api->synchronize_translators( $translators );
if ( is_wp_error( $result ) ) {
return $result;
}
$this->translator_activation_records->update( $result['translators'] );
return array( 'result' => $result );
}
/**
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function synchronize_managers() {
$managers = $this->ams_user_records->get_managers();
$result = $this->api->synchronize_managers( $managers );
if ( is_wp_error( $result ) ) {
return $result;
}
return array( 'result' => $result );
}
/**
* @return array|mixed|null|object|WP_Error
* @throws \InvalidArgumentException
*/
public function get_status() {
return $this->api->get_status();
}
public function get_console() {
return $this->strings->get_auto_login();
}
function get_allowed_capabilities( WP_REST_Request $request ) {
return array( 'manage_translations', 'manage_options' );
}
}

View File

@@ -0,0 +1,12 @@
<?php
class WPML_TM_REST_ATE_API_Factory extends WPML_REST_Factory_Loader {
/**
* @return \WPML_TM_REST_ATE_API
* @throws \Auryn\InjectionException
*/
public function create() {
return \WPML\Container\make( '\WPML_TM_REST_ATE_API' );
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_REST_ATE_API extends WPML_TM_ATE_Required_Rest_Base {
const CAPABILITY_CREATE = 'manage_translations';
const CAPABILITY_READ = 'translate';
private $api;
/**
* WPML_TM_REST_AMS_Clients constructor.
*
* @param WPML_TM_ATE_API $api
*/
public function __construct( WPML_TM_ATE_API $api ) {
parent::__construct();
$this->api = $api;
}
function add_hooks() {
$this->register_routes();
}
function register_routes() {
parent::register_route(
'/ate/jobs',
array(
'methods' => 'POST',
'callback' => array( $this, 'create_jobs' ),
)
);
parent::register_route(
'/ate/jobs/(?P<ateJobId>\d+)',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_job' ),
)
);
}
/**
* @param WP_REST_Request $request
*
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function create_jobs( WP_REST_Request $request ) {
return $this->api->create_jobs( $request->get_params() );
}
/**
* @param WP_REST_Request $request
*
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function get_job( WP_REST_Request $request ) {
$ate_job_id = $request->get_param( 'ateJobId' );
return $this->api->get_job( $ate_job_id );
}
function get_allowed_capabilities( WP_REST_Request $request ) {
if ( 'GET' === $request->get_method() ) {
return array( self::CAPABILITY_CREATE, self::CAPABILITY_READ );
}
return self::CAPABILITY_CREATE;
}
}

View File

@@ -0,0 +1,14 @@
<?php
class WPML_TM_REST_ATE_Jobs_Factory extends WPML_REST_Factory_Loader {
public function create() {
$ate_jobs_records = wpml_tm_get_ate_job_records();
$ate_jobs = new WPML_TM_ATE_Jobs( $ate_jobs_records );
return new WPML_TM_REST_ATE_Jobs(
$ate_jobs,
wpml_tm_get_ate_jobs_repository()
);
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_REST_ATE_Jobs extends WPML_TM_ATE_Required_Rest_Base {
const CAPABILITY = 'manage_translations';
private $ate_jobs;
/** @var WPML_TM_ATE_Job_Repository */
private $job_repository;
/**
* WPML_TM_REST_ATE_Jobs constructor.
*
* @param WPML_TM_ATE_Jobs $ate_jobs
* @param WPML_TM_ATE_Job_Repository $job_repository
*/
public function __construct( WPML_TM_ATE_Jobs $ate_jobs, WPML_TM_ATE_Job_Repository $job_repository ) {
parent::__construct();
$this->ate_jobs = $ate_jobs;
$this->job_repository = $job_repository;
}
function add_hooks() {
$this->register_routes();
}
function register_routes() {
parent::register_route(
WPML_TM_ATE_AMS_Endpoints::STORE_JOB,
array(
'methods' => 'POST',
'callback' => array( $this, 'store_ate_job' ),
'args' => array(
'wpml_job_id' => array(
'required' => true,
'type' => 'string',
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'integer' ),
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'integer' ),
),
'ate_job_data' => array(
'required' => true,
'type' => 'array',
),
),
)
);
}
/**
* @param WP_REST_Request $request
*
* @return bool
* @throws \InvalidArgumentException
*/
public function store_ate_job( WP_REST_Request $request ) {
$wpml_job_id = $request->get_param( 'wpml_job_id' );
$ate_job_data = $request->get_param( 'ate_job_data' );
$this->ate_jobs->store( $wpml_job_id, $ate_job_data );
return true;
}
function get_allowed_capabilities( WP_REST_Request $request ) {
return self::CAPABILITY;
}
}

View File

@@ -0,0 +1,8 @@
<?php
class WPML_TM_REST_XLIFF_Factory extends WPML_REST_Factory_Loader {
public function create() {
return new WPML_TM_REST_XLIFF();
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_REST_XLIFF extends WPML_TM_ATE_Required_Rest_Base {
const CAPABILITY = 'translate';
function add_hooks() {
$this->register_routes();
}
function register_routes() {
parent::register_route(
'/xliff/fetch/(?P<jobId>\d+)',
array(
'methods' => 'GET',
'callback' => array( $this, 'fetch_xliff' ),
)
);
}
/**
* @param WP_REST_Request $request
*
* @return array
* @throws \InvalidArgumentException
*/
public function fetch_xliff( WP_REST_Request $request ) {
$result = null;
$wpml_translation_job_factory = wpml_tm_load_job_factory();
$iclTranslationManagement = wpml_load_core_tm();
$job_id = $request->get_param( 'jobId' );
$writer = new WPML_TM_Xliff_Writer( $wpml_translation_job_factory );
$xliff = base64_encode( $writer->generate_job_xliff( $job_id ) );
$job = $iclTranslationManagement->get_translation_job( (int) $job_id, false, false, 1 );
$result = array(
'content' => $xliff,
'sourceLang' => $job->source_language_code,
'targetLang' => $job->language_code,
);
return $result;
}
function get_allowed_capabilities( WP_REST_Request $request ) {
return self::CAPABILITY;
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace WPML\TM\ATE\Retry;
use WPML\Collect\Support\Collection;
use WPML\FP\Fns;
use WPML\FP\Relation;
use WPML\TM\API\Jobs;
use WPML_TM_ATE_Job_Repository;
use function WPML\FP\pipe;
class Process {
const JOBS_PROCESSED_PER_REQUEST = 10;
/** @var WPML_TM_ATE_Job_Repository $ateRepository */
private $ateRepository;
/** @var Trigger $trigger */
private $trigger;
public function __construct(
WPML_TM_ATE_Job_Repository $ateRepository,
Trigger $trigger
) {
$this->ateRepository = $ateRepository;
$this->trigger = $trigger;
}
/**
* @param array $jobsToProcess
*
* @return Result
*/
public function run( $jobsToProcess ) {
$result = new Result();
if ( $jobsToProcess ) {
$result = $this->retry( $result, wpml_collect( $jobsToProcess ) );
} else {
$result = $this->runRetryInit( $result );
}
if ( $result->jobsToProcess->isEmpty() && $this->trigger->isRetryRequired() ) {
$this->trigger->setLastRetry( time() );
}
return $result;
}
/**
* @param Result $result
*
* @return Result
*/
private function runRetryInit( Result $result ) {
$wpmlJobIds = $this->getWpmlJobIdsToRetry();
if ( $this->trigger->isRetryRequired() && ! $wpmlJobIds->isEmpty() ) {
$result = $this->retry( $result, $wpmlJobIds );
}
return $result;
}
/**
* @param Result $result
* @param Collection $jobs
*
* @return Result
*/
private function retry( Result $result, Collection $jobs ) {
$jobsChunks = $jobs->chunk( self::JOBS_PROCESSED_PER_REQUEST );
$result->processed = $this->handleJobs( $jobsChunks->shift() );
$result->jobsToProcess = $jobsChunks->flatten( 1 );
return $result;
}
/**
* @return Collection
*/
private function getWpmlJobIdsToRetry() {
return wpml_collect( $this->ateRepository->get_jobs_to_retry()->map_to_property( 'translate_job_id' ) );
}
/**
* @param Collection $items
*
* @return array $items [[wpmlJobId, status, ateJobId], ...]
*/
private function handleJobs( Collection $items ) {
do_action( 'wpml_added_translation_jobs', [ 'local' => $items->toArray() ], Jobs::SENT_RETRY );
return $items->toArray();
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace WPML\TM\ATE\Retry;
use WPML\Collect\Support\Collection;
class Result {
/** @var Collection */
public $jobsToProcess;
/** @var array[wpmlJobId] */
public $processed = [];
public function __construct() {
$this->jobsToProcess = wpml_collect();
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace WPML\TM\ATE\Retry;
use WPML\WP\OptionManager;
class Trigger {
const RETRY_TIMEOUT = 10 * MINUTE_IN_SECONDS;
const OPTION_GROUP = 'WPML\TM\ATE\Retry';
const RETRY_LAST = 'last';
/**
* @return bool
*/
public function isRetryRequired() {
$retrySync = OptionManager::getOr( 0, self::RETRY_LAST, self::OPTION_GROUP );
return ( time() - self::RETRY_TIMEOUT ) > $retrySync;
}
public function setLastRetry( $time ) {
OptionManager::updateWithoutAutoLoad( self::RETRY_LAST, self::OPTION_GROUP, $time );
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace WPML\TM\ATE;
use WPML\FP\Obj;
use function WPML\Container\make;
/**
* Class ReturnedJobsQueue
*
* @package WPML\TM\ATE
*
* IMPORTANT!
* In this class `wpmlJobId` represents job_id column in icl_translate_job
*/
class ReturnedJobsQueue {
const OPTION_NAME = 'ATE_RETURNED_JOBS_QUEUE';
const STATUS_COMPLETED = 'complete';
const STATUS_BACK = 'back';
/**
* @param int $ateJobId
* @param string $status
* @param callable $ateIdToWpmlId @see comment in the class description
*/
public static function add( $ateJobId, $status, callable $ateIdToWpmlId ) {
$wpmlId = $ateIdToWpmlId( $ateJobId );
if ( in_array( $status, [ self::STATUS_BACK, self::STATUS_COMPLETED ] ) && $wpmlId ) {
$options = get_option( self::OPTION_NAME, [] );
$options[ $wpmlId ] = $status;
update_option( self::OPTION_NAME, $options );
}
}
/**
* For jobs that are completed in ATE, but belong to a Translation that is currently marked as "Duplicate".
* In such cases, we want to get rid of the Duplicate status, otherwise it will not be processed during ATE sync.
*
* @see \WPML\TM\ATE\Loader::getData
* @see \WPML_Meta_Boxes_Post_Edit_HTML::post_edit_languages_duplicate_of How it's handled for CTE.
*
* @param int $ateJobId
* @param callable $ateIdToWpmlId
*/
public static function removeJobTranslationDuplicateStatus( $ateJobId, callable $ateIdToWpmlId ) {
$wpmlJobId = $ateIdToWpmlId( $ateJobId );
if ( $wpmlJobId ) {
/** @var \WPML_TM_Records $tm_records */
$tm_records = make( \WPML_TM_Records::class );
$jobTranslation = $tm_records->icl_translate_job_by_job_id( $wpmlJobId );
$translationStatus = $tm_records->icl_translation_status_by_rid( $jobTranslation->rid() );
if ( ICL_TM_DUPLICATE === $translationStatus->status() ) {
$translationStatus->update( [ 'status' => ICL_TM_IN_PROGRESS ] );
}
}
}
/**
* @param int $wpmlJobId @see comment in the class description
*
* @return string|null
*/
public static function getStatus( $wpmlJobId ) {
return Obj::prop( $wpmlJobId, get_option( self::OPTION_NAME, [] ) );
}
/**
* @param $wpmlJobId @see comment in the class description
*/
public static function remove( $wpmlJobId ) {
$options = get_option( self::OPTION_NAME, [] );
if ( isset( $options[ $wpmlJobId ] ) ) {
unset( $options[ $wpmlJobId ] );
update_option( self::OPTION_NAME, $options );
}
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\LIB\WP\Post;
use WPML\TM\API\Jobs;
use function WPML\FP\partial;
use function WPML\FP\pipe;
class AcceptTranslation implements IHandler {
public function run( Collection $data ) {
$postId = $data->get( 'postId' );
$jobId = $data->get( 'jobId' );
$canEdit = partial( 'current_user_can', 'edit_post' );
$completeJob = Fns::tap( pipe(
Fns::always( $jobId ),
Fns::tap( Jobs::setStatus( Fns::__, ICL_TM_COMPLETE ) ),
Fns::tap( Jobs::setReviewStatus( Fns::__, ReviewStatus::ACCEPTED ) )
) );
return Either::of( $postId )
->filter( $canEdit )
->map( Post::setStatusWithoutFilters( Fns::__, 'publish' ) )
->map( $completeJob )
->bimap( Fns::always( $jobId ), Fns::always( $jobId ) );
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\LIB\WP\Hooks;
use WPML\LIB\WP\Post;
use WPML\Setup\Option;
use WPML\TM\API\Jobs;
use function WPML\FP\pipe;
use function WPML\FP\spreadArgs;
class ApplyJob implements \IWPML_Backend_Action, \IWPML_REST_Action, \IWPML_AJAX_Action {
/** @var string[] */
private static $excluded_from_review = [ 'st-batch', 'package' ];
public function add_hooks() {
if ( Option::shouldBeReviewed() ) {
self::addJobStatusHook();
self::addTranslationCompleteHook();
self::addTranslationPreSaveHook();
}
}
private static function addJobStatusHook() {
$applyReviewStatus = function ( $status, $job ) {
if (
self::shouldBeReviewed( $job )
&& $status === ICL_TM_COMPLETE
) {
Jobs::setReviewStatus(
(int) $job->job_id,
ReviewStatus::NEEDS_REVIEW );
}
return $status;
};
Hooks::onFilter( 'wpml_tm_applied_job_status', 10, 2 )
->then( spreadArgs( $applyReviewStatus ) );
}
private static function addTranslationCompleteHook() {
$isHoldToReviewMode = Fns::always( Option::getReviewMode() === 'before-publish' );
$isPostNewlyCreated = Fns::converge( Relation::equals(), [
Obj::prop( 'post_date' ),
Obj::prop( 'post_modified' )
] );
$setPostStatus = pipe(
Maybe::of(),
Fns::filter( $isHoldToReviewMode ),
Fns::map( Post::get() ),
Fns::filter( Logic::isNotNull() ),
Fns::filter( $isPostNewlyCreated ),
Fns::map( Obj::prop( 'ID' ) ),
Fns::map( Post::setStatus( Fns::__, 'draft' ) )
);
Hooks::onAction( 'wpml_pro_translation_completed' )
->then( spreadArgs( $setPostStatus ) );
}
private static function addTranslationPreSaveHook() {
$keepDraftPostsDraftIfNeedsReview = function ( $postArr, $job ) {
if (
self::shouldBeReviewed( $job )
&& isset( $postArr['ID'] )
&& get_post_status( $postArr['ID'] ) === 'draft'
) {
$postArr['post_status'] = 'draft';
}
return $postArr;
};
Hooks::onFilter( 'wpml_pre_save_pro_translation', 10, 2 )
->then( spreadArgs( $keepDraftPostsDraftIfNeedsReview ) );
}
/**
* @param $job
*
* @return bool
*/
private static function shouldBeReviewed( $job ) {
return ! Lst::includes( $job->element_type_prefix, self::$excluded_from_review )
&& $job->automatic
&& (int) $job->original_doc_id !== (int) get_option( 'page_for_posts' );
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\TM\API\Jobs;
use function WPML\Container\make;
class ApproveTranslations implements IHandler {
public function run( Collection $data ) {
$jobIds = $data->get( 'jobsIds' );
return wpml_collect( $jobIds )
->map( Jobs::get() )
->filter( ReviewStatus::doesJobNeedReview() )
->map( Obj::addProp( 'translated_id', Jobs::getTranslatedPostId() ) )
->map( Obj::props( [ 'job_id', 'translated_id' ] ) )
->map( Lst::zipObj( [ 'jobId', 'postId' ] ) )
->map( 'wpml_collect' )
->map( [ make( AcceptTranslation::class ), 'run' ] )
->map( function ( Either $result ) {
$isJobApproved = Fns::isRight( $result );
$jobId = $result->coalesce( Fns::identity(), Fns::identity() )->getOrElse( 0 );
return [ 'jobId' => $jobId, 'status' => $isJobApproved ];
} );
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Cast;
use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\LIB\WP\Post;
use WPML\TM\API\Job\Map;
use WPML\TM\API\Jobs;
use function WPML\FP\pipe;
class Cancel implements IHandler {
public function run( Collection $data ) {
$jobIds = $data->get( 'jobsIds' );
$deleteDrafts = $data->get( 'deleteDrafts' );
$reviewJobs = wpml_collect( $jobIds )
->map( Jobs::get() )
->filter( ReviewStatus::doesJobNeedReview() );
if ( $reviewJobs->count() ) {
$reviewJobs->map( Obj::prop( 'job_id' ) )
->map( Jobs::clearReviewStatus() );
if ( $deleteDrafts ) {
$this->deleteDrafts( $reviewJobs );
}
return $reviewJobs->pluck( 'job_id' )->map( Cast::toInt() );
}
return [];
}
private function deleteDrafts( Collection $reviewJobs ) {
$doCancelJobsAction = function ( Collection $jobIds ) {
$getJobEntity = function ( $jobId ) {
return wpml_tm_get_jobs_repository()->get_job( Map::fromJobId( $jobId ), \WPML_TM_Job_Entity::POST_TYPE );
};
$jobEntities = $jobIds->map( $getJobEntity )->toArray();
do_action( 'wpml_tm_jobs_cancelled', $jobEntities );
};
$getTranslatedId = Fns::memorize( pipe( Jobs::get(), Jobs::getTranslatedPostId() ) );
$isDraft = pipe( $getTranslatedId, Post::getStatus(), Relation::equals( 'draft' ) );
$deleteTranslatedPost = pipe( $getTranslatedId, Post::delete() );
$reviewJobs = $reviewJobs->map( Obj::prop( 'job_id' ) )
->map( Jobs::setNotTranslatedStatus() )
->map( Jobs::clearTranslated() );
$doCancelJobsAction( $reviewJobs );
$reviewJobs->filter( $isDraft )
->map( Fns::tap( $deleteTranslatedPost ) )
->map( Jobs::delete() );
}
}

View File

@@ -0,0 +1,137 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\Element\API\Post;
use WPML\Element\API\PostTranslations;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\TM\API\Translators;
use WPML_Translations_Queue;
use function WPML\FP\invoke;
use function WPML\FP\partial;
use function WPML\FP\pipe;
class NextTranslationLink {
public static function get( $currentJob ) {
global $sitepress;
$doNotFilterPreviewLang = Fns::always( false );
$addPreviewLangFilter = function () use ( $doNotFilterPreviewLang ) {
add_filter( 'wpml_should_filter_preview_lang', $doNotFilterPreviewLang );
};
$removePreviewLangFilter = function () use ( $doNotFilterPreviewLang ) {
remove_filter( 'wpml_should_filter_preview_lang', $doNotFilterPreviewLang );
};
$getTranslationPostId = Fns::memorize( self::getTranslationPostId() );
$switchToPostLang = function ( $job ) use ( $sitepress, $getTranslationPostId ) {
Maybe::of( $job )
->chain( $getTranslationPostId )
->map( Post::getLang() )
->map( [ $sitepress, 'switch_lang' ] );
};
$restoreLang = function () use ( $sitepress ) {
$sitepress->switch_lang( null );
};
$getLink = Fns::converge( Fns::liftA2( PreviewLink::get() ), [
$getTranslationPostId,
Maybe::safe( invoke( 'get_translate_job_id' ) )
] );
return Maybe::of( $currentJob )
->map( self::getNextJob() )
->map( Fns::tap( $switchToPostLang ) )
->map( Fns::tap( $addPreviewLangFilter ) )
->chain( $getLink )
->map( Fns::tap( $removePreviewLangFilter ) )
->map( Fns::tap( $restoreLang ) )
->getOrElse( null );
}
private static function getTranslationPostId() {
return function ( $nextJob ) {
return Maybe::of( $nextJob )
->map( pipe( invoke( 'get_original_element_id' ), PostTranslations::get() ) )
->map( Obj::prop( $nextJob->get_target_language() ) )
->map( Obj::prop( 'element_id' ) );
};
}
/**
* @return \Closure :: \stdClass -> \WPML_TM_Post_Job_Entity
*/
private static function getNextJob() {
return function ( $currentJob ) {
$getJob = function ( $sourceLanguage, $targetLanguages ) use ( $currentJob ) {
$excludeCurrentJob = pipe( invoke( 'get_translate_job_id' ), Relation::equals( (int) $currentJob->job_id ), Logic::not() );
$samePostTypes = function ( $nextJob ) use ( $currentJob ) {
$currentJobPostType = \get_post_type( $currentJob->original_doc_id );
$nextJobPostType = \get_post_type( $nextJob->get_original_element_id() );
return $currentJobPostType === $nextJobPostType;
};
$nextJob = \wpml_collect(wpml_tm_get_jobs_repository()
->get( self::buildSearchParams( $sourceLanguage, $targetLanguages ) ) )
->filter( $samePostTypes )
->first( $excludeCurrentJob );
if ( ! $nextJob ) {
$nextJob = \wpml_collect( wpml_tm_get_jobs_repository()
->get( self::buildSearchParams( $sourceLanguage, $targetLanguages ) ) )
->first( $excludeCurrentJob );
}
return $nextJob;
};
$languagePairs = \wpml_collect( Obj::propOr( [], 'language_pairs', Translators::getCurrent() ) );
$filterTargetLanguages = function ( $targetLanguages, $sourceLanguage ) {
$icl_translation_filter = WPML_Translations_Queue::get_cookie_filters();
if ( isset( $icl_translation_filter['to'] ) && '' !== $icl_translation_filter['to'] ) {
return [
'source' => $sourceLanguage,
'targets' => [ $icl_translation_filter['to'] ],
];
}
return [
'source' => $sourceLanguage,
'targets' => $targetLanguages,
];
};
$filterJobByPairOfLanguages = function ( $job, $pairOfLanguages ) use ( $getJob ) {
return $job ?: $getJob( Obj::prop( 'source', $pairOfLanguages ), Obj::prop( 'targets', $pairOfLanguages ) );
};
return $languagePairs
->map($filterTargetLanguages)
->reduce($filterJobByPairOfLanguages);
};
}
/**
* @param string $sourceLang
* @param string[] $targetLanguages
*
* @return \WPML_TM_Jobs_Search_Params
*/
private static function buildSearchParams( $sourceLang, array $targetLanguages ) {
return ( new \WPML_TM_Jobs_Search_Params() )
->set_needs_review()
->set_source_language( $sourceLang )
->set_target_language( $targetLanguages );
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Obj;
/**
* This will allow displaying private CPT reviews on the frontend.
*/
class NonPublicCPTPreview {
const POST_TYPE = 'wpmlReviewPostType';
/**
* @param array $args
*
* @return array
*/
public static function addArgs( array $args ) {
return Obj::assoc( self::POST_TYPE, \get_post_type( $args['preview_id'] ), $args );
}
/**
* @return callable
*/
public static function allowReviewPostTypeQueryVar() {
return Lst::append( self::POST_TYPE );
}
/**
* @return callable
*/
public static function enforceReviewPostTypeIfSet() {
return Logic::ifElse(
Obj::prop( self::POST_TYPE ),
Obj::renameProp( self::POST_TYPE, 'post_type' ),
Fns::identity()
) ;
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\Collect\Support\Traits\Macroable;
use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\TM\API\Jobs;
use function WPML\FP\curryN;
/**
* Class PreviewLink
*
* @package WPML\TM\ATE\Review
*
* @method static callable|string getWithSpecifiedReturnUrl( ...$returnUrl, ...$translationPostId, ...$jobId ) : Curried:: int->int->string
* @method static callable|string get( ...$translationPostId, ...$jobId ) : Curried:: int->int->string
* @method static callable|string getByJob( ...$job ) : Curried:: \stdClass->string
* @method static Callable|string getNonceName( ...$translationPostId ) : Curried:: int->string
*/
class PreviewLink {
use Macroable;
public static function init() {
self::macro( 'getWithSpecifiedReturnUrl', curryN( 3, function ( $returnUrl, $translationPostId, $jobId ) {
return \add_query_arg(
NonPublicCPTPreview::addArgs( [
'p' => $translationPostId,
'preview_id' => $translationPostId,
'preview_nonce' => \wp_create_nonce( self::getNonceName( $translationPostId ) ),
'preview' => true,
'jobId' => $jobId,
'returnUrl' => urlencode( $returnUrl ),
] ),
\get_permalink( $translationPostId )
);
} ) );
self::macro( 'get', curryN( 2, function ( $translationPostId, $jobId ) {
$returnUrl = Obj::propOr( Obj::prop( 'REQUEST_URI', $_SERVER ), 'returnUrl', $_GET );
return self::getWithSpecifiedReturnUrl( $returnUrl, $translationPostId, $jobId );
} ) );
self::macro( 'getByJob', curryN( 1, Fns::converge(
self::get(),
[
Jobs::getTranslatedPostId(),
Obj::prop( 'job_id' ),
]
) ) );
self::macro( 'getNonceName', Str::concat( 'post_preview_' ) );
}
}
PreviewLink::init();

View File

@@ -0,0 +1,19 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\FP\Relation;
class ReviewCompletedNotice implements \IWPML_Backend_Action {
public function add_hooks() {
if ( Relation::propEq( 'reviewCompleted', 'inWPML', $_GET ) ) {
$text = esc_html__( "You've completed reviewing everything. WPML will let you know when there's new content to review.", 'wpml-translation-management' );
wpml_get_admin_notices()->add_notice(
\WPML_Notice::make( 'reviewCompleted', $text )
->set_css_class_types( 'notice-info' )
->set_flash()
);
}
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\Collect\Support\Traits\Macroable;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Obj;
use function WPML\FP\curryN;
use function WPML\FP\pipe;
/**
* Class ReviewStatus
* @package WPML\TM\ATE\Review
*
* @method static callable|bool needsReview( ...$reviewStatus ) - Curried :: string->bool
* @method static callable|bool doesJobNeedReview( ...$job ) - Curried :: \stdClass->bool
*/
class ReviewStatus {
use Macroable;
const NEEDS_REVIEW = 'NEEDS_REVIEW';
const EDITING = 'EDITING';
const ACCEPTED = 'ACCEPTED';
public static function init() {
self::macro( 'needsReview', Lst::includes( Fns::__, [ ReviewStatus::NEEDS_REVIEW, ReviewStatus::EDITING ] ) );
self::macro( 'doesJobNeedReview', curryN( 1, Logic::ifElse(
Fns::identity(),
pipe( Obj::prop( 'review_status' ), self::needsReview() ),
Fns::always(false)
) ));
}
}
ReviewStatus::init();

View File

@@ -0,0 +1,194 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\API\Sanitize;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Relation;
use WPML\LIB\WP\Hooks;
use WPML\LIB\WP\Post;
use WPML\Element\API\Post as WPMLPost;
use WPML\TM\API\Jobs;
use WPML\TM\API\Translators;
use WPML\TM\WP\App\Resources;
use WPML\FP\Obj;
use function WPML\FP\pipe;
use function WPML\FP\spreadArgs;
class ReviewTranslation implements \IWPML_Frontend_Action, \IWPML_Backend_Action {
public function add_hooks() {
if ( self::hasValidNonce() ) {
Hooks::onFilter( 'query_vars' )
->then( spreadArgs( NonPublicCPTPreview::allowReviewPostTypeQueryVar() ) );
Hooks::onFilter( 'request' )
->then( spreadArgs( NonPublicCPTPreview::enforceReviewPostTypeIfSet() ) );
Hooks::onFilter( 'the_preview' )
->then( Hooks::getArgs( [ 0 => 'post' ] ) )
->then( $this->handleTranslationReview() );
}
Hooks::onFilter( 'user_has_cap', 10, 3 )
->then( spreadArgs( function ( $userCaps, $requiredCaps, $args ) {
if ( Relation::propEq( 0, 'edit_post', $args ) ) {
$translator = Translators::getCurrent();
if ( $translator->ID ) {
$postId = $args[2];
$job = Jobs::getPostJob( $postId, Post::getType( $postId ), WPMLPost::getLang( $postId ) );
if ( ReviewStatus::doesJobNeedReview( $job ) && self::canEditLanguage( $translator, $job ) ) {
return Lst::concat( $userCaps, Lst::zipObj( $requiredCaps, Lst::repeat( true, count( $requiredCaps ) ) ) );
}
}
return $userCaps;
}
return $userCaps;
} ) );
Hooks::onFilter( 'wpml_tm_allowed_translators_for_job', 10, 2 )
->then( spreadArgs( function ( $allowedTranslators, \WPML_Element_Translation_Job $job ) {
$job = $job->to_array();
$translator = Translators::getCurrent();
if ( ReviewStatus::doesJobNeedReview( $job ) && self::canEditLanguage( $translator, $job ) ) {
return array_merge( $allowedTranslators, [ $translator->ID ] );
}
return $allowedTranslators;
} ) );
}
private static function canEditLanguage( $translator, $job ) {
if ( ! $job ) {
return false;
}
return Lst::includes( Obj::prop('language_code', $job), Obj::pathOr( [], [ 'language_pairs', Obj::prop('source_language_code', $job) ], $translator ) );
}
/**
* This will ensure to block the standard preview
* for non-public CPTs.
*
* @return bool
*/
private static function hasValidNonce() {
$get = Obj::prop( Fns::__, $_GET );
return (bool) \wp_verify_nonce(
$get( 'preview_nonce' ),
PreviewLink::getNonceName( (int) $get( 'preview_id' ) )
);
}
public function handleTranslationReview() {
return function ( $data ) {
$post = Obj::prop( 'post', $data );
$jobId = filter_input( INPUT_GET, 'jobId', FILTER_SANITIZE_NUMBER_INT );
if ( $jobId ) {
/**
* This hooks is fired as soon as a translation review is about to be displayed.
*
* @since 4.5.0
*
* @param int $jobId The job Id.
* @param object|\WP_Post $post The job's related object to be reviewed.
*/
do_action( 'wpml_tm_handle_translation_review', $jobId, $post );
Hooks::onFilter( 'wp_redirect' )
->then( [ __CLASS__, 'failGracefullyOnPreviewRedirection' ] );
Hooks::onAction( 'template_redirect', PHP_INT_MAX )
->then( function () {
Hooks::onAction( 'wp_footer' )
->then( [ __CLASS__, 'printReviewToolbarAnchor' ] );
} );
show_admin_bar( false );
$enqueue = Resources::enqueueApp( 'translationReview' );
$enqueue( $this->getData( $jobId, $post ) );
}
return $post;
};
}
public static function printReviewToolbarAnchor() {
echo '
<script type="text/javascript" >
var ajaxurl = "' . \admin_url( 'admin-ajax.php', 'relative' ) . '"
</script>
<div id="wpml_translation_review"></div>
';
}
/**
* @return null This will stop the redirection.
*/
public static function failGracefullyOnPreviewRedirection() {
do_action( 'wp_head' );
self::printReviewToolbarAnchor();
echo '
<div class="wpml-review__modal-mask wpml-review__modal-mask-transparent">
<div class="wpml-review__modal-box wpml-review__modal-box-transparent wpml-review__modal-preview-not-available">
<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M24 0C10.8 0 0 10.8 0 24C0 37.2 10.8 48 24 48C37.2 48 48 37.2 48 24C48 10.8 37.2 0 24 0ZM24 43.5C13.2 43.5 4.5 34.8 4.5 24C4.5 13.2 13.2 4.5 24 4.5C34.8 4.5 43.5 13.2 43.5 24C43.5 34.8 34.8 43.5 24 43.5Z" fill="url(#paint0_linear)"/><path d="M24 10.2C22.5 10.2 21 11.4 21 13.2C21 15 22.2 16.2 24 16.2C25.8 16.2 27 15 27 13.2C27 11.4 25.5 10.2 24 10.2ZM24 20.4C22.8 20.4 21.9 21.3 21.9 22.5V35.7C21.9 36.9 22.8 37.8 24 37.8C25.2 37.8 26.1 36.9 26.1 35.7V22.5C26.1 21.3 25.2 20.4 24 20.4Z" fill="url(#paint1_linear)"/><defs><linearGradient id="paint0_linear" x1="38.6667" y1="6.66666" x2="8" y2="48" gradientUnits="userSpaceOnUse"><stop stop-color="#27AD95"/><stop offset="1" stop-color="#2782AD"/></linearGradient><linearGradient id="paint1_linear" x1="38.6667" y1="6.66666" x2="8" y2="48" gradientUnits="userSpaceOnUse"><stop stop-color="#27AD95"/><stop offset="1" stop-color="#2782AD"/></linearGradient></defs></svg>
<h2>'. esc_html__( 'Preview is not available', 'wpml-translation-management' ) .'</h2>
<p>'. sprintf(esc_html__( 'Click %sEdit Translation%s in the toolbar above to review your translation in the editor.', 'wpml-translation-management' ), '<strong>', '</strong>') .'</p>
</div>
</div>
';
return null;
}
public function getData( $jobId, $post ) {
$job = Jobs::get( $jobId );
$editUrl = \add_query_arg( [ 'preview' => 1 ], Jobs::getEditUrl( Jobs::getCurrentUrl(), $jobId ) );
return [
'name' => 'reviewTranslation',
'data' => [
'jobEditUrl' => $editUrl,
'nextJobUrl' => NextTranslationLink::get( $job ),
'jobId' => (int) $jobId,
'postId' => $post->ID,
'isPublished' => Relation::propEq( 'post_status', 'publish', $post ) ? 1 : 0,
'needsReview' => ReviewStatus::doesJobNeedReview( $job ),
'completedInATE' => $this->isCompletedInATE( $_GET ),
'needsUpdate' => Relation::propEq( 'review_status', ReviewStatus::EDITING, $job ),
'previousTranslation' => Sanitize::stringProp( 'previousTranslation', $_GET ),
'backUrl' => Obj::prop( 'returnUrl', $_GET ),
'endpoints' => [
'accept' => AcceptTranslation::class,
'update' => UpdateTranslation::class
],
]
];
}
public function isCompletedInATE( $params ) {
$completedInATE = pipe(
Obj::prop( 'complete_no_changes' ),
'strval',
Logic::cond( [
[ Relation::equals( '1' ), Fns::always( 'COMPLETED_WITHOUT_CHANGED' ) ],
[ Relation::equals( '0' ), Fns::always( 'COMPLETED' ) ],
[ Fns::always( true ), Fns::always( 'NOT_COMPLETED' ) ],
] )
);
return $completedInATE( $params );
}
}

View File

@@ -0,0 +1,116 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\Element\API\Languages;
use WPML\Element\API\PostTranslations;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\LIB\WP\Hooks;
use WPML\Setup\Option;
use WPML\TM\API\Jobs;
use function WPML\FP\partial;
use function WPML\FP\pipe;
class StatusIcons implements \IWPML_Backend_Action {
public function add_hooks() {
$ifNeedsReview = function ( $fn ) {
$getJob = Fns::converge( Jobs::getTridJob(), [
Obj::prop( 'trid' ),
Obj::prop( 'languageCode' )
] );
$doesNeedReview = pipe(
$getJob,
ReviewStatus::doesJobNeedReview()
);
return Logic::ifElse( $doesNeedReview, $fn, Obj::prop( 'default' ) );
};
Hooks::onFilter( 'wpml_css_class_to_translation', PHP_INT_MAX , 5 )
->then( Hooks::getArgs( [ 0 => 'default', 2 => 'languageCode', 3 => 'trid', 4 => 'status' ] ) )
->then( $ifNeedsReview( Fns::always( 'otgs-ico-needs-review' ) ) );
Hooks::onFilter( 'wpml_text_to_translation', PHP_INT_MAX, 6 )
->then( Hooks::getArgs( [ 0 => 'default', 2 => 'languageCode', 3 => 'trid', 5 => 'status' ] ) )
->then( $ifNeedsReview ( self::getReviewTitle( 'languageCode' ) ) );
Hooks::onFilter( 'wpml_link_to_translation', PHP_INT_MAX, 6 )
->then( Hooks::getArgs( [ 0 => 'default', 1 => 'postId', 2 => 'langCode', 3 => 'trid', 5 => 'status' ] ) )
->then( $this->setLink() );
}
public static function getReviewTitle( $langProp ) {
return pipe(
self::getLanguageName( $langProp ),
Fns::unary( partial( 'sprintf', __( 'Review %s language', 'wpml-translation-management' ) ) )
);
}
public static function getEditTitle( $langProp ) {
return pipe(
self::getLanguageName( $langProp ),
Fns::unary( partial( 'sprintf', __( 'Edit %s translation', 'wpml-translation-management' ) ) )
);
}
private static function getLanguageName( $langProp ) {
return Fns::memorize( pipe(
Obj::prop( $langProp ),
Languages::getLanguageDetails(),
Obj::prop( 'display_name' )
) );
}
private function setLink() {
return function ( $data ) {
$isInProgress = pipe(
Obj::prop( 'status' ),
Lst::includes( Fns::__, [ ICL_TM_WAITING_FOR_TRANSLATOR, ICL_TM_IN_PROGRESS, ICL_TM_ATE_NEEDS_RETRY ] )
);
$isInProgressOrCompleted = Logic::anyPass( [ $isInProgress, Relation::propEq( 'status', ICL_TM_COMPLETE ) ] );
$getTranslations = Fns::memorize( PostTranslations::get() );
$getTranslation = Fns::converge( Obj::prop(), [
Obj::prop( 'langCode' ),
pipe( Obj::prop( 'postId' ), $getTranslations )
] );
$getJob = Fns::converge( Jobs::getPostJob(), [
Obj::prop( 'postId' ),
Fns::always( 'post' ),
Obj::prop( 'langCode' )
] );
$doesNeedsReview = pipe( Obj::prop( 'job' ), ReviewStatus::doesJobNeedReview() );
$getPreviewLink = Fns::converge( PreviewLink::get(), [
Obj::path( [ 'translation', 'element_id' ] ),
Obj::path( [ 'job', 'job_id' ] )
] );
$disableInProgressIconOfAutomaticJob = Logic::ifElse(
Logic::both( $isInProgress, Obj::path( [ 'job', 'automatic' ] ) ),
Fns::always( 0 ), // no link at all
Obj::prop( 'default' )
);
return Maybe::of( $data )
->filter( $isInProgressOrCompleted )
->map( Obj::addProp( 'translation', $getTranslation ) )
->filter( Obj::prop( 'translation' ) )
->reject( Obj::path( [ 'translation', 'original' ] ) )
->map( Obj::addProp( 'job', $getJob ) )
->filter( Obj::prop( 'job' ) )
->map( Logic::ifElse( $doesNeedsReview, $getPreviewLink, $disableInProgressIconOfAutomaticJob ) )
->getOrElse( Obj::prop( 'default', $data ) );
};
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace WPML\TM\ATE\Review;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\TM\API\ATE;
use WPML\TM\API\Jobs;
use function WPML\Container\make;
use function WPML\FP\partial;
use function WPML\FP\pipe;
class UpdateTranslation implements IHandler {
public function run( Collection $data ) {
$jobId = $data->get( 'jobId' );
$postId = $data->get( 'postId' );
$completedInATE = $data->get( 'completedInATE' );
if ( $completedInATE === 'COMPLETED_WITHOUT_CHANGED' ) {
return $this->completeWithoutChanges( $jobId );
}
$ateAPI = make( ATE::class );
$applyTranslation = pipe(
partial( [ $ateAPI, 'applyTranslation' ], $jobId, $postId ),
Logic::ifElse(
Fns::identity(),
Fns::tap( function () use ( $jobId ) {
Jobs::setReviewStatus( $jobId, ReviewStatus::NEEDS_REVIEW );
} ),
Fns::identity()
)
);
$hasStatus = function ( $statuses ) {
return pipe( Obj::prop( 'status_id' ), Lst::includes( Fns::__, $statuses ) );
};
$shouldApplyXLIFF = $hasStatus( [
\WPML_TM_ATE_AMS_Endpoints::ATE_JOB_STATUS_DELIVERING,
\WPML_TM_ATE_AMS_Endpoints::ATE_JOB_STATUS_TRANSLATED,
\WPML_TM_ATE_AMS_Endpoints::ATE_JOB_STATUS_EDITED,
] );
$applyXLIFF = Logic::ifElse(
pipe( Obj::prop( 'translated_xliff' ), $applyTranslation ),
Fns::always( Either::of( 'applied' ) ),
Fns::always( Either::left( 'error' ) )
);
$isDelivered = $hasStatus( [ \WPML_TM_ATE_AMS_Endpoints::ATE_JOB_STATUS_DELIVERED ] );
$isTranslating = $hasStatus( [ \WPML_TM_ATE_AMS_Endpoints::ATE_JOB_STATUS_TRANSLATING ] );
$userClickedCompleteInATE = Fns::always( $completedInATE === 'COMPLETED' );
$otherwise = Fns::always( true );
$handleATEResult = Logic::cond( [
[ $shouldApplyXLIFF, $applyXLIFF ],
[ $isDelivered, Fns::always( Either::of( 'applied-without-changes' ) ) ],
[ $userClickedCompleteInATE, Fns::always( Either::of( 'underway' ) ) ],
[ $isTranslating, Fns::always( Either::of( 'in-progress' ) ) ],
[ Logic::isEmpty(), Fns::always( Either::left( 'error' ) ) ],
[ $otherwise, Fns::always( Either::of( 'in-progress' ) ) ]
] );
return Either::of( $jobId )
->map( [ $ateAPI, 'checkJobStatus' ] )
->chain( $handleATEResult );
}
private function completeWithoutChanges( $jobId ) {
$applyWithoutChanges = pipe(
Fns::tap( Jobs::setStatus( Fns::__, ICL_TM_COMPLETE ) ),
Fns::tap( Jobs::setReviewStatus( Fns::__, ReviewStatus::NEEDS_REVIEW ) )
);
return Either::of( $jobId )
->map( $applyWithoutChanges )
->map( Fns::always( 'applied-without-changes' ) );
}
}

View File

@@ -0,0 +1,134 @@
<?php
namespace WPML\TM\ATE;
use WPML\Element\API\Languages;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\LIB\WP\Hooks;
use WPML\LIB\WP\User;
use WPML\TM\API\ATE\CachedLanguageMappings;
use WPML\TM\API\ATE\LanguageMappings;
use WPML\UIPage;
use function WPML\FP\pipe;
use function WPML\FP\spreadArgs;
use WPML\Setup\Option;
class StatusBar {
public static function add_hooks() {
if (
User::getCurrent()->has_cap( \WPML_Manage_Translations_Role::CAPABILITY )
&& Option::shouldTranslateEverything()
) {
Hooks::onAction( 'admin_bar_menu', 999 )
->then( spreadArgs( [ self::class, 'add' ] ) );
}
}
public static function add( \WP_Admin_Bar $adminBar ) {
$iconurl = "'" . ICL_PLUGIN_URL . '/res/img/icon16.png' . "'";
$iconspan = '<span class="" style="
float:left; width:22px !important; height:22px !important;
margin-left: 5px !important; margin-top: 5px !important;
background-image:url(' . $iconurl . ');"></span>';
$title = '<div id="wpml-status-bar-count" style="display:inline-block"></div>';
$adminBar->add_node(
[
'parent' => false,
'id' => 'ate-status-bar',
'title' => $iconspan . $title,
'href' => false
]
);
$adminBar->add_node(
[
'parent' => 'ate-status-bar',
'id' => 'ate-status-bar-content',
'meta' => [ 'html' => '<div id="wpml-ate-status-bar-content"></div>' ],
'href' => false,
]
);
}
public static function getNotices() {
return self::getNoticeAboutIneligibleLanguages();
}
private static function getNoticeAboutIneligibleLanguages() {
if ( ! \WPML_TM_ATE_Status::is_enabled_and_activated() ) {
return [];
}
$isNotDontMap = pipe( Obj::pathOr( null, [ 'mapping', 'targetId' ] ), Relation::equals( LanguageMappings::IGNORE_MAPPING_ID ), Logic::not() );
$isDefault = Relation::propEq( 'code', Languages::getDefaultCode() );
$findDefault = Fns::memorize( Lst::find( $isDefault ) );
$supportedLanguages = Maybe::of( Languages::getActive() )
->map( CachedLanguageMappings::withCanBeTranslatedAutomatically() )
->map( Fns::filter( Obj::prop( 'can_be_translated_automatically' ) ) )
->reject( Logic::isEmpty() )
->getOrElse([]);
$createNoticeWithText = function ( $text, $type = 'important', $isDefault = false ) use ( $supportedLanguages ) {
return [
[
'id' => 'unsupportedLanguages',
'text' => $text,
'type' => $type,
'href' => add_query_arg( [ 'trop' => 1 ] , UIPage::getLanguages() ),
'target' => '_self',
'_hasSupportedLanguages' => ! $isDefault && count( $supportedLanguages ) > 1,
// We consider that we support Automatic Translation when default and at least 1 more language is supported.
]
];
};
$createNoticeForDefaultLanguage = function ( $defaultLanguage ) use ( $createNoticeWithText ) {
$txt = sprintf(
__( "Your default language, %s, must be mapped to a supported language in order to use automatic translation.", 'wpml-translation-management' ),
Obj::prop( 'english_name', $defaultLanguage )
);
return $createNoticeWithText( $txt, 'important', true );
};
$createNoticeForSecondaryLanguages = function ( $languages ) use ( $createNoticeWithText ) {
if ( Lst::find( Logic::complement( CachedLanguageMappings::hasTheSameMappingAsDefaultLang() ), $languages ) ) {
$txt = sprintf(
__( "The following language(s) on your site must be mapped to a supported language in order to use automatic translation: %s", 'wpml-translation-management' ),
Lst::join( ', ', Lst::pluck( 'english_name', $languages ) )
);
return $createNoticeWithText( $txt );
}
return $createNoticeWithText( self::getNoticeMsgForLanguageWithTheSameMappingAsDefault( $languages ), 'info' );
};
$createNotice = Logic::ifElse( $findDefault, pipe( $findDefault, $createNoticeForDefaultLanguage ), $createNoticeForSecondaryLanguages );
return Maybe::of( Languages::getActive() )
->map( CachedLanguageMappings::withCanBeTranslatedAutomatically() )
->map( Fns::reject( Obj::prop( 'can_be_translated_automatically' ) ) )
->reject( Logic::isEmpty() )
->map( CachedLanguageMappings::withMapping() )
->map( Fns::filter( Logic::anyPass( [ $isDefault, $isNotDontMap ] ) ) )
->reject( Logic::isEmpty() )
->map( $createNotice )
->getOrElse( [] );
}
public static function getNoticeMsgForLanguageWithTheSameMappingAsDefault( $languages ) {
return sprintf(
__( "It's not possible to use automatic translation with your current language setup, as the following languages are mapped to the same language as the default: %s", 'wpml-translation-management' ),
Lst::join( ', ', Lst::pluck( 'english_name', $languages ) )
);
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace WPML\TM\ATE;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\LIB\WP\Hooks;
use WPML\TM\API\Jobs;
use function WPML\FP\spreadArgs;
class StatusIcons implements \IWPML_Backend_Action {
/** @var bool */
private $alreadyFound = false;
public function add_hooks() {
if (
(int) Obj::prop( 'ate_job_id', $_GET )
&& self::hasTranslatedStatusInAte()
) {
Hooks::onFilter( 'wpml_css_class_to_translation', PHP_INT_MAX, 5 )
->then( spreadArgs( [ $this, 'setSpinningIconOnPageList' ] ) );
Hooks::onFilter( 'wpml_tm_translation_queue_job_icon', 10, 2 )
->then( spreadArgs( [ $this, 'setSpinningIconInTranslationQueue' ] ) );
}
}
private static function hasTranslatedStatusInAte() {
return Lst::includes(
(int) Obj::prop( 'ate_status', $_GET ),
[ \WPML_TM_ATE_API::TRANSLATED, \WPML_TM_ATE_API::DELIVERING ]
);
}
public function setSpinningIconOnPageList( $default, $postId, $languageCode, $trid, $status ) {
if ( $this->alreadyFound ) {
return $default;
} else {
return $this->getIcon( $default, Jobs::getTridJob( $trid, $languageCode ) );
}
}
public function setSpinningIconInTranslationQueue( $default, $job ) {
return $this->getIcon( $default, $job );
}
public function getIcon( $default, $job ) {
if ( $job &&
(int) Obj::prop( 'editor_job_id', $job ) === (int) Obj::prop( 'ate_job_id', $_GET )
&& Relation::propEq( 'editor', \WPML_TM_Editors::ATE, $job )
&& Lst::includes( (int) Obj::prop( 'status', $job ), [ ICL_TM_WAITING_FOR_TRANSLATOR, ICL_TM_IN_PROGRESS ] )
) {
$this->alreadyFound = true;
return 'otgs-ico-refresh-spin';
} else {
return $default;
}
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace WPML\TM\ATE\Sync;
class Arguments {
/** @var string|null $lockKey */
public $lockKey;
/** @var string|null $ateToken */
public $ateToken;
/** @var int|null $page */
public $page;
/** @var int|null $numberOfPages */
public $numberOfPages;
/** @var boolean $includeManualAndLongstandingJobs */
public $includeManualAndLongstandingJobs;
}

View File

@@ -0,0 +1,163 @@
<?php
namespace WPML\TM\ATE\Sync;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\TM\API\Job\Map;
use WPML\TM\API\Jobs;
use WPML\TM\ATE\Download\Job;
use WPML\TM\ATE\Log\EventsTypes;
use WPML_TM_ATE_API;
use WPML_TM_ATE_Job_Repository;
use WPML\TM\ATE\Log\Storage;
use WPML\TM\ATE\Log\Entry;
use function WPML\FP\pipe;
class Process {
const LOCK_RELEASE_TIMEOUT = 1 * MINUTE_IN_SECONDS;
/** @var WPML_TM_ATE_API $api */
private $api;
/** @var WPML_TM_ATE_Job_Repository $ateRepository */
private $ateRepository;
public function __construct( WPML_TM_ATE_API $api, WPML_TM_ATE_Job_Repository $ateRepository ) {
$this->api = $api;
$this->ateRepository = $ateRepository;
}
/**
* @param Arguments $args
*
* @return Result
*/
public function run( Arguments $args ) {
$result = new Result();
if ( $args->page ) {
$result = $this->runSyncOnPages( $result, $args );
} else {
$includeManualAndLongstandingJobs = (bool) Obj::propOr( true , 'includeManualAndLongstandingJobs', $args);
$result = $this->runSyncInit( $result, $includeManualAndLongstandingJobs );
}
return $result;
}
/**
* This will run the sync on extra pages.
*
* @param Result $result
* @param Arguments $args
*
* @return Result
*/
private function runSyncOnPages( Result $result, Arguments $args ) {
$apiPage = $args->page - 1; // ATE API pagination starts at 0.
$data = $this->api->sync_page( $args->ateToken, $apiPage );
$jobs = Obj::propOr( [], 'items', $data );
$result->jobs = $this->handleJobs( $jobs );
if ( !$result->jobs ){
Storage::add( Entry::createForType(
EventsTypes::JOBS_SYNC,
[
'numberOfPages' => $args->numberOfPages,
'page' => $args->page,
'downloadQueueSize' => $result->downloadQueueSize,
'nextPage' => $result->nextPage,
]
) );
}
if ( $args->numberOfPages > $args->page ) {
$result->nextPage = $args->page + 1;
$result->numberOfPages = $args->numberOfPages;
$result->ateToken = $args->ateToken;
}
return $result;
}
/**
* This will run the first sync iteration.
* We send all the job IDs we want to sync.
*
* @param Result $result
* @param boolean $includeManualAndLongstandingJobs
*
* @return Result
*/
private function runSyncInit( Result $result, $includeManualAndLongstandingJobs = true ) {
$ateJobIds = $this->getAteJobIdsToSync( $includeManualAndLongstandingJobs );
if ( $ateJobIds ) {
$this->ateRepository->increment_ate_sync_count( $ateJobIds );
$data = $this->api->sync_all( $ateJobIds );
$jobs = Obj::propOr( [], 'items', $data );
$result->jobs = $this->handleJobs( $jobs );
if ( isset( $data->next->pagination_token, $data->next->pages_number ) ) {
$result->ateToken = $data->next->pagination_token;
$result->numberOfPages = $data->next->pages_number;
$result->nextPage = 1; // We start pagination at 1 to avoid carrying a falsy value.
}
}
return $result;
}
/**
* @param boolean $includeManualAndLongstandingJobs
*
* @return array
*/
private function getAteJobIdsToSync( $includeManualAndLongstandingJobs = true ) {
return $this->ateRepository
->get_jobs_to_sync( $includeManualAndLongstandingJobs )
->map_to_property( 'editor_job_id' );
}
/**
* @param array $items
*
* @return Job[] $items
*/
private function handleJobs( array $items ) {
$setStatus = function ( $status ) {
return pipe(
Fns::tap( Jobs::setStatus( Fns::__, $status ) ),
Obj::assoc('status', $status)
);
};
$updateStatus = Logic::cond( [
[
Relation::propEq( 'status', \WPML_TM_ATE_API::CANCELLED_STATUS ),
$setStatus( ICL_TM_NOT_TRANSLATED ),
],
[
Relation::propEq( 'status', \WPML_TM_ATE_API::SHOULD_HIDE_STATUS ),
$setStatus( ICL_TM_ATE_CANCELLED ),
],
[
Fns::always( true ),
Fns::identity(),
]
] );
return wpml_collect( $items )
->map( [ Job::class, 'fromAteResponse' ] )
->map( Obj::over( Obj::lensProp( 'jobId' ), Map::fromRid() ) ) // wpmlJobId returned by ATE endpoint represents RID column in wp_icl_translation_status
->map( $updateStatus )
->toArray();
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace WPML\TM\ATE\Sync;
class Result {
/** @var string|false|null $lockKey */
public $lockKey;
/** @var string|null $ateToken */
public $ateToken;
/** @var int|null $nextPage */
public $nextPage;
/** @var int|null $numberOfPages */
public $numberOfPages;
/** @var int $downloadQueueSize */
public $downloadQueueSize = 0;
/** @var array[wpmlJobId, wpmlStatus, ateStatus, wpmlJobStatus] */
public $jobs = [];
}

View File

@@ -0,0 +1,31 @@
<?php
namespace WPML\TM\ATE;
use WPML\Utilities\KeyedLock;
use function WPML\Container\make;
class SyncLock {
/** @var KeyedLock */
private $keyedLock;
public function __construct() {
$this->keyedLock = make( KeyedLock::class, [ ':name' => 'ate_sync' ] );
}
/**
* @param null~string $key
*
* @return false|string
*/
public function create( $key = null ) {
return $this->keyedLock->create( $key, 30 );
}
/**
* @return bool
*/
public function release() {
return $this->keyedLock->release();
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace WPML\TM\ATE;
use WPML\API\PostTypes;
use WPML\Collect\Support\Collection;
use WPML\Element\API\Languages;
use WPML\FP\Fns;
use WPML\FP\Left;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\FP\Relation;
use WPML\FP\Right;
use WPML\FP\Str;
use WPML\FP\Wrapper;
use WPML\LIB\WP\Post;
use WPML\Media\Option as MediaOption;
use WPML\Records\Translations;
use WPML\Setup\Option;
use WPML\TM\API\ATE\CachedLanguageMappings;
use WPML\TM\API\ATE\LanguageMappings;
use WPML\TM\ATE\TranslateEverything\UntranslatedPosts;
use WPML\TM\AutomaticTranslation\Actions\Actions;
use WPML\Utilities\KeyedLock;
use function WPML\Container\make;
use function WPML\FP\invoke;
use function WPML\FP\pipe;
class TranslateEverything {
const LOCK_RELEASE_TIMEOUT = 2 * MINUTE_IN_SECONDS;
const QUEUE_SIZE = 15;
public function run(
Collection $data,
Actions $actions
) {
if ( ! MediaOption::isSetupFinished() ) {
return Left::of( [ 'key' => 'waiting' ] );
}
$lock = make( KeyedLock::class, [ ':name' => self::class ] );
$key = $lock->create( $data->get( 'key' ), self::LOCK_RELEASE_TIMEOUT );
if ( $key ) {
$createdJobs = [];
if ( Option::shouldTranslateEverything() ) {
$createdJobs = $this->translateEverything( $actions );
}
if ( self::isEverythingProcessed() || ! Option::shouldTranslateEverything() ) {
$lock->release();
$key = false;
}
return Right::of( [ 'key' => $key, 'createdJobs' => $createdJobs ] );
} else {
return Left::of( [ 'key' => 'in-use', ] );
}
}
/**
* @param Actions $actions
*/
private function translateEverything( Actions $actions ) {
$defaultLang = Languages::getDefaultCode();
$secondaryLanguages = LanguageMappings::geCodesEligibleForAutomaticTranslations();
$postType = self::getPostTypeToProcess( $secondaryLanguages );
$queueSize = $postType == 'attachment' ? self::QUEUE_SIZE * 2 : self::QUEUE_SIZE;
$elements = UntranslatedPosts::get( $secondaryLanguages, $postType, $queueSize + 1 );
if ( count( $elements ) <= $queueSize ) {
Option::markPostTypeAsCompleted( $postType, $secondaryLanguages );
}
return count( $elements ) ?
$actions->createNewTranslationJobs( $defaultLang, Lst::slice( 0, $queueSize, $elements ) ) :
[];
}
/**
* @param string[] $secondaryLanguages
*
* @return string
*/
private static function getPostTypeToProcess( array $secondaryLanguages ) {
$postTypes = self::getPostTypesToTranslate( PostTypes::getAutomaticTranslatable(), $secondaryLanguages );
return wpml_collect( $postTypes )
->prioritize( Relation::equals( 'post' ) )
->prioritize( Relation::equals( 'page' ) )
->first();
}
/**
* @param array $postTypes
* @param array $targetLanguages
*
* @return string[] E.g. ['post', 'page']
*/
public static function getPostTypesToTranslate( array $postTypes, array $targetLanguages ) {
$completed = Option::getTranslateEverythingCompleted();
$postTypesNotCompletedForTargets = pipe( Obj::propOr( [], Fns::__, $completed ), Lst::diff( $targetLanguages ), Lst::length() );
return Fns::filter( $postTypesNotCompletedForTargets, $postTypes );
}
/**
* @param string $postType
* @param array $targetLanguages
*
* @return string[] Eg. ['fr', 'de', 'es']
*/
public static function getLanguagesToTranslate( $postType, array $targetLanguages ) {
$completed = Option::getTranslateEverythingCompleted();
return Lst::diff( $targetLanguages, Obj::propOr( [], $postType, $completed ) );
}
/**
* Checks if Translate Everything is processed for a given Post Type and Language.
*
* @param string $postType
* @param string $language
*
* @return bool
*/
public static function isEverythingProcessedForPostTypeAndLanguage( $postType, $language ) {
$completed = Option::getTranslateEverythingCompleted();
return isset( $completed[ $postType ] ) && in_array( $language, $completed[ $postType ] );
}
/**
* @param bool $cached
*
* @return bool
*/
public static function isEverythingProcessed( $cached = false ) {
$postTypes = PostTypes::getAutomaticTranslatable();
$getTargetLanguages = [ $cached ? CachedLanguageMappings::class : LanguageMappings::class, 'geCodesEligibleForAutomaticTranslations'];
return count( self::getPostTypesToTranslate( $postTypes, $getTargetLanguages() ) ) === 0;
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace WPML\TM\ATE\TranslateEverything;
use WPML\FP\Cast;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Str;
class UntranslatedPosts {
public static function get( array $secondaryLanguages, $postType, $queueSize ) {
global $wpdb;
$languagesPart = Lst::join( ' UNION ALL ', Fns::map( Str::replace( '__', Fns::__, "SELECT '__' AS code" ), $secondaryLanguages ) );
$acceptableStatuses = ICL_TM_NOT_TRANSLATED . ', ' . ICL_TM_ATE_CANCELLED;
$sql = "
SELECT original_element.element_id, languages.code
FROM {$wpdb->prefix}icl_translations original_element
INNER JOIN ( $languagesPart ) as languages
LEFT JOIN {$wpdb->prefix}icl_translations translations ON translations.trid = original_element.trid AND translations.language_code = languages.code
LEFT JOIN {$wpdb->prefix}icl_translation_status translation_status ON translation_status.translation_id = translations.translation_id
INNER JOIN {$wpdb->posts} posts ON posts.ID = original_element.element_id
WHERE original_element.element_type = %s and original_element.source_language_code IS NULL AND (
translation_status.status IS NULL OR translation_status.status IN ({$acceptableStatuses}) OR translation_status.needs_update = 1
) AND posts.post_status IN ( 'publish', 'inherit' )
ORDER BY original_element.element_id, languages.code
LIMIT %d
";
$result = $wpdb->get_results( $wpdb->prepare( $sql, 'post_' . $postType, $queueSize ), ARRAY_N );
return Fns::map( Obj::evolve( [ 0 => Cast::toInt() ] ), $result );
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\API\PostTypes;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\Setup\Option;
class ActivateLanguage {
public function run( Collection $data ) {
$newLanguages = $data->get( 'languages' );
$translateExistingContent = $data->get( 'translate-existing-content', false );
$mergingFn = $translateExistingContent ? Lst::diff() : Lst::concat();
$postTypes = PostTypes::getAutomaticTranslatable();
if ( $newLanguages && $postTypes ) {
$completed = Option::getTranslateEverythingCompleted();
foreach ( $postTypes as $postType ) {
$existingLanguages = Obj::propOr( [], $postType, $completed );
Option::markPostTypeAsCompleted( $postType, $mergingFn( $existingLanguages, $newLanguages ) );
}
}
return Either::of( 'ok' );
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Left;
use WPML\FP\Obj;
use WPML\FP\Right;
use WPML\TM\API\Jobs;
class AutoTranslate implements IHandler {
public function run( Collection $data ) {
global $wpml_translation_job_factory;
$trid = $data->get( 'trid' );
$language_code = $data->get( 'language' );
if ( $trid && $language_code ) {
$post_id = \SitePress::get_original_element_id_by_trid( $trid );
if ( ! $post_id ) {
return Either::left( 'Post cannot be found by trid' );
}
$job_id = $wpml_translation_job_factory->create_local_post_job( $post_id, $language_code );
$job = Jobs::get( $job_id );
if ( ! $job ) {
return Either::left( 'Job could not be created' );
}
if ( Obj::prop( 'automatic', $job ) ) {
return Right::of( [ 'jobId' => $job_id, 'automatic' => 1 ] );
} else {
return Right::of( [ 'jobId' => $job_id, 'automatic' => 0, 'editUrl' => Jobs::getEditUrl( $data->get( 'currentUrl' ), $job_id ) ] );
}
}
return Left::of( 'invalid data' );
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use function WPML\Container\make;
use function WPML\FP\invoke;
class CancelJobs implements IHandler {
public function run( Collection $data ) {
if ( $data->get( 'getTotal', false ) ) {
return Either::of( wpml_tm_get_jobs_repository()->get_count( $this->getSearchParams() ) );
}
$batchSize = $data->get( 'batchSize', 1000 );
$params = $this->getSearchParams()->set_limit( $batchSize );
$toCancel = wpml_collect( wpml_tm_get_jobs_repository()->get( $params ) );
$toCancel->map( Fns::tap( invoke( 'set_status' )->with( ICL_TM_NOT_TRANSLATED ) ) )
->map( Fns::tap( [ make( \WPML_TP_Sync_Update_Job::class ), 'update_state' ] ) );
return Either::of( $toCancel->count() );
}
/**
* @return \WPML_TM_Jobs_Search_Params
*/
private function getSearchParams() {
$searchParams = new \WPML_TM_Jobs_Search_Params();
$searchParams->set_status( [ ICL_TM_WAITING_FOR_TRANSLATOR, ICL_TM_IN_PROGRESS ] );
$searchParams->set_custom_where_conditions( [ 'translate_job.automatic = 1' ] );
return $searchParams;
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\TM\API\ATE\LanguageMappings;
class CheckLanguageSupport {
public function run( Collection $data ) {
return Either::of( $data->get( 'languages', [] ) )
->map( Fns::map( Obj::objOf( 'code' ) ) )
->map( LanguageMappings::withCanBeTranslatedAutomatically() );
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Ajax\IHandler;
use WPML\API\Settings;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Logic;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\LIB\WP\User;
use WPML\Setup\Option;
use WPML\TM\API\ATE\LanguageMappings;
use function WPML\Container\make;
use function WPML\FP\pipe;
class EnableATE implements IHandler {
public function run( Collection $data ) {
Settings::assoc( 'translation-management', 'doc_translation_method', ICL_TM_TMETHOD_ATE );
$cache = wpml_get_cache( \WPML_Translation_Roles_Records::CACHE_GROUP );
$cache->flush_group_cache();
/** @var \WPML_TM_AMS_API $ateApi */
$ateApi = make( \WPML_TM_AMS_API::class );
$status = $ateApi->get_status();
if ( Obj::propOr( false, 'activated', $status ) ) {
$result = Either::right( true );
} else {
$amsUsers = make( \WPML_TM_AMS_Users::class );
$saveLanguageMapping = Fns::tap( pipe(
[ Option::class, 'getLanguageMappings' ],
Logic::ifElse( Logic::isEmpty(), Fns::always( true ), [ LanguageMappings::class, 'saveMapping'] )
) );
$result = make( \WPML_TM_AMS_API::class )->register_manager(
User::getCurrent(),
$amsUsers->get_translators(),
$amsUsers->get_managers()
)->map( $saveLanguageMapping );
$ateApi->get_status(); // Required to get the active status and store it.
}
return $result->map( Fns::tap( [ make( \WPML_TM_AMS_Synchronize_Actions::class ), 'synchronize_translators' ] ) )
->bimap( pipe( Lst::make(), Lst::keyWith( 'error' ), Lst::nth(0) ), Fns::identity() );
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use function WPML\Container\make;
class GetATEJobsToSync implements IHandler {
public function run( Collection $data ) {
return Either::of(
make( \WPML_TM_ATE_Job_Repository::class )->get_jobs_to_sync()->map_to_property( 'editor_job_id' )
);
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\LIB\WP\Option;
use WPML\TM\API\ATE\Account;
use WPML\WP\OptionManager;
use function WPML\Container\make;
class GetCredits implements IHandler {
public function run( Collection $data ) {
return Either::of( Account::getCredits() );
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\API\PostTypes;
use WPML\Collect\Support\Collection;
class GetNumberOfPosts {
public function run( Collection $data, \wpdb $wpdb ) {
$postIn = wpml_prepare_in( $data->get( 'postTypes', PostTypes::getAutomaticTranslatable() ) );
return $wpdb->get_var(
"SELECT COUNT(id) FROM {$wpdb->posts} WHERE post_type IN ({$postIn}) AND post_status='publish'"
);
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use function WPML\Container\make;
class ResumeAll implements IHandler {
public function run( Collection $data ) {
return Either::of( make( \WPML_TM_AMS_API::class )->resumeAll() );
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\Settings\PostType\Automatic;
class SetForPostType {
public function run( Collection $data ) {
$postTypes = $data->get( 'postTypes' );
foreach ( $postTypes as $type => $state ) {
Automatic::set( $type, (bool) $state );
}
return Either::of( true );
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace WPML\TM\ATE\AutoTranslate\Endpoint;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\Utilities\KeyedLock;
use function WPML\Container\make;
class SyncLock implements IHandler {
public function run( Collection $data ) {
$lock = make( \WPML\TM\ATE\SyncLock::class );
$action = $data->get( 'action', 'acquire' );
if ( $action === 'release' ) {
$lockKey = $lock->create( $data->get( 'lockKey' ) );
if ( $lockKey ) {
$lock->release();
}
return Either::of( [ 'action' => 'release', 'result' => (bool) $lockKey ] );
} else {
return Either::of( [ 'action' => 'acquire', 'result' => $lock->create( null ) ] );
}
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* Used for helping building other factories.
*
* @see Usage.
*
* @author OnTheGo Systems
*/
class WPML_TM_AMS_ATE_Factories {
/**
* It returns an cached instance of \WPML_TM_ATE_API.
*
* @return \WPML_TM_ATE_API
*/
public function get_ate_api() {
return WPML\Container\make( WPML_TM_ATE_API::class );
}
/**
* It returns an cached instance of \WPML_TM_ATE_API.
*
* @return \WPML_TM_AMS_API
*/
public function get_ams_api() {
return WPML\Container\make( WPML_TM_AMS_API::class );
}
/**
* If ATE is active, it returns true.
*
* @return bool
*/
public function is_ate_active() {
if ( ! WPML_TM_ATE_Status::is_active() ) {
try {
$this->get_ams_api()->get_status();
return WPML_TM_ATE_Status::is_active();
} catch ( Exception $ex ) {
return false;
}
}
return true;
}
}

Some files were not shown because too many files have changed in this diff Show More