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,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 ) ] );
}
}
}