first commit
This commit is contained in:
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -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' );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user