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,35 @@
<?php
namespace WPML\TM\Menu\TranslationServices\Endpoints;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Logic;
use WPML\FP\Obj;
use WPML\TM\TranslationProxy\Services\AuthorizationFactory;
class Activate implements IHandler {
public function run( Collection $data ) {
$serviceId = $data->get( 'service_id' );
$apiTokenData = $data->get( 'api_token' );
$authorize = function ( $serviceId ) use ( $apiTokenData ) {
$authorization = ( new AuthorizationFactory )->create();
try {
$authorization->authorize( (object) Obj::pickBy( Logic::isNotEmpty(), $apiTokenData ) );
return Either::of( $serviceId );
} catch ( \Exception $e ) {
$authorization->deauthorize();
return Either::left( $e->getMessage() );
}
};
return Either::of( $serviceId )
->chain( [ Select::class, 'select' ] )
->chain( $authorize );
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace WPML\TM\Menu\TranslationServices\Endpoints;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
class Deactivate implements IHandler {
public function run( Collection $data ) {
if ( \TranslationProxy::get_current_service_id() ) {
\TranslationProxy::clear_preferred_translation_service();
\TranslationProxy::deselect_active_service();
}
return Either::of( 'OK' );
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace WPML\TM\Menu\TranslationServices\Endpoints;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
class Select implements IHandler {
public function run( Collection $data ) {
$serviceId = $data->get( 'service_id' );
return self::select( $serviceId );
}
public static function select( $serviceId ) {
$deactivateOldService = function () {
\TranslationProxy::clear_preferred_translation_service();
\TranslationProxy::deselect_active_service();
};
$activateService = function ( $serviceId ) {
$result = \TranslationProxy::select_service( $serviceId );
return \is_wp_error( $result ) ? Either::left( $result->get_error_message() ) : Either::of( $serviceId );
};
$currentServiceId = \TranslationProxy::get_current_service_id();
if ( $currentServiceId ) {
if ( $currentServiceId === $serviceId ) {
return Either::of( $serviceId );
} else {
$deactivateOldService();
}
}
return $activateService( $serviceId );
}
}