first commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\TranslationProxy\Services;
|
||||
|
||||
use WPML\TM\TranslationProxy\Services\Project\Manager;
|
||||
|
||||
class Authorization {
|
||||
/** @var Storage */
|
||||
private $storage;
|
||||
|
||||
/** @var Manager */
|
||||
private $projectManager;
|
||||
|
||||
/**
|
||||
* @param Storage $storage
|
||||
* @param Manager $projectManager
|
||||
*/
|
||||
public function __construct( Storage $storage, Manager $projectManager ) {
|
||||
$this->storage = $storage;
|
||||
$this->projectManager = $projectManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $credentials
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @throws \WPML_TP_API_Exception
|
||||
*/
|
||||
public function authorize( \stdClass $credentials ) {
|
||||
$service = $this->getCurrentService();
|
||||
$service->custom_fields_data = $credentials;
|
||||
|
||||
$project = $this->projectManager->create( $service );
|
||||
$this->storage->setCurrentService( $service );
|
||||
|
||||
do_action( 'wpml_tm_translation_service_authorized', $service, $project );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $credentials
|
||||
*
|
||||
* @throws \WPML_TP_API_Exception
|
||||
*/
|
||||
public function updateCredentials( \stdClass $credentials ) {
|
||||
$service = $this->getCurrentService();
|
||||
|
||||
$this->projectManager->updateCredentials( $service, $credentials );
|
||||
|
||||
$service->custom_fields_data = $credentials;
|
||||
$this->storage->setCurrentService( $service );
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function deauthorize() {
|
||||
$service = $this->getCurrentService();
|
||||
$service->custom_fields_data = null;
|
||||
|
||||
$this->storage->setCurrentService( $service );
|
||||
|
||||
do_action( 'wpml_tp_service_de_authorized', $service );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \stdClass
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
private function getCurrentService() {
|
||||
$service = $this->storage->getCurrentService();
|
||||
if ( (bool) $service === false ) {
|
||||
throw new \RuntimeException( 'Tried to authenticate a service, but no service is active!' );
|
||||
}
|
||||
|
||||
return $service;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\TranslationProxy\Services;
|
||||
|
||||
use WPML\TM\TranslationProxy\Services\Project\Manager;
|
||||
use function WPML\Container\make;
|
||||
|
||||
class AuthorizationFactory {
|
||||
/**
|
||||
* @return Authorization
|
||||
* @throws \Auryn\InjectionException
|
||||
*/
|
||||
public function create() {
|
||||
$projectManager = make(
|
||||
Manager::class,
|
||||
[
|
||||
':projectApi' => wpml_tm_get_tp_project_api(),
|
||||
]
|
||||
);
|
||||
|
||||
return make( Authorization::class, [ ':projectManager' => $projectManager ] );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\TranslationProxy\Services\Project;
|
||||
|
||||
class Manager {
|
||||
|
||||
/** @var \WPML_TP_Project_API */
|
||||
private $projectApi;
|
||||
|
||||
/** @var Storage */
|
||||
private $projectStorage;
|
||||
|
||||
/** @var SiteDetails */
|
||||
private $siteDetails;
|
||||
|
||||
/**
|
||||
* @param \WPML_TP_Project_API $projectApi
|
||||
* @param Storage $projectStorage
|
||||
* @param SiteDetails $siteDetails
|
||||
*/
|
||||
public function __construct(
|
||||
\WPML_TP_Project_API $projectApi,
|
||||
Storage $projectStorage,
|
||||
SiteDetails $siteDetails
|
||||
) {
|
||||
$this->projectApi = $projectApi;
|
||||
$this->projectStorage = $projectStorage;
|
||||
$this->siteDetails = $siteDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $service
|
||||
*
|
||||
* @return Project
|
||||
* @throws \WPML_TP_API_Exception
|
||||
*/
|
||||
public function create( \stdClass $service ) {
|
||||
$project = $this->projectStorage->getByService( $service ) ?: $this->fromTranslationProxy( $service );
|
||||
|
||||
$project->extraFields = $this->projectApi->get_extra_fields( $project );
|
||||
$this->projectStorage->save( $service, $project );
|
||||
|
||||
do_action( 'wpml_tp_project_created', $service, $project, $this->projectStorage->getProjects()->toArray() );
|
||||
|
||||
return $project;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $service
|
||||
* @param \stdClass $credentials
|
||||
*
|
||||
* @return Project|null
|
||||
* @throws \WPML_TP_API_Exception
|
||||
*/
|
||||
public function updateCredentials( \stdClass $service, \stdClass $credentials ) {
|
||||
$project = $this->projectStorage->getByService( $service );
|
||||
if ( ! $project ) {
|
||||
throw new \RuntimeException( 'Project does not exist' );
|
||||
}
|
||||
$this->projectApi->update_project_credentials( $project, $credentials );
|
||||
|
||||
$project->extraFields = $this->projectApi->get_extra_fields( $project );
|
||||
$this->projectStorage->save( $this->createServiceWithNewCredentials( $service, $credentials ), $project );
|
||||
|
||||
return $project;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $service
|
||||
*
|
||||
* @return Project
|
||||
* @throws \WPML_TP_API_Exception
|
||||
*/
|
||||
private function fromTranslationProxy( \stdClass $service ) {
|
||||
$response = $this->projectApi->create_project( $service, $this->siteDetails );
|
||||
|
||||
return Project::fromResponse( $response->project );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $service
|
||||
* @param \stdClass $credentials
|
||||
*
|
||||
* @return \stdClass
|
||||
*/
|
||||
private function createServiceWithNewCredentials( \stdClass $service, \stdClass $credentials ) {
|
||||
$updatedService = clone $service;
|
||||
$updatedService->custom_fields_data = $credentials;
|
||||
|
||||
return $updatedService;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\TranslationProxy\Services\Project;
|
||||
|
||||
class Project {
|
||||
/** @var int */
|
||||
public $id;
|
||||
|
||||
/** @var string */
|
||||
public $accessKey;
|
||||
|
||||
/** @var string */
|
||||
public $tsId;
|
||||
|
||||
/** @var string */
|
||||
public $tsAccessKey;
|
||||
|
||||
/** @var \stdClass */
|
||||
public $extraFields;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function toArray() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'access_key' => $this->accessKey,
|
||||
'ts_id' => $this->tsId,
|
||||
'ts_access_key' => $this->tsAccessKey,
|
||||
'extra_fields' => $this->extraFields,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Project
|
||||
*/
|
||||
public static function fromArray( array $data ) {
|
||||
$project = new Project();
|
||||
|
||||
$project->id = $data['id'];
|
||||
$project->accessKey = $data['access_key'];
|
||||
$project->tsId = $data['ts_id'];
|
||||
$project->tsAccessKey = $data['ts_access_key'];
|
||||
$project->extraFields = $data['extra_fields'];
|
||||
|
||||
return $project;
|
||||
}
|
||||
|
||||
public static function fromResponse( \stdClass $response ) {
|
||||
$project = new Project();
|
||||
|
||||
$project->id = $response->id;
|
||||
$project->accessKey = $response->accesskey;
|
||||
$project->tsId = $response->ts_id;
|
||||
$project->tsAccessKey = $response->ts_accesskey;
|
||||
|
||||
return $project;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\TranslationProxy\Services\Project;
|
||||
|
||||
class SiteDetails {
|
||||
/** @var \SitePress */
|
||||
private $sitepress;
|
||||
|
||||
/**
|
||||
* @param \SitePress $sitepress
|
||||
*/
|
||||
public function __construct( \SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDeliveryMethod() {
|
||||
return (int) $this->sitepress->get_setting( 'translation_pickup_method' ) === ICL_PRO_TRANSLATION_PICKUP_XMLRPC
|
||||
? 'xmlrpc'
|
||||
: 'polling';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBlogInfo() {
|
||||
return [
|
||||
'url' => get_option( 'siteurl' ),
|
||||
'name' => get_option( 'blogname' ),
|
||||
'description' => get_option( 'blogdescription' ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getClientData() {
|
||||
$current_user = wp_get_current_user();
|
||||
|
||||
return [
|
||||
'email' => $current_user->user_email,
|
||||
'name' => $current_user->display_name,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\TranslationProxy\Services\Project;
|
||||
|
||||
use WPML\Collect\Support\Collection;
|
||||
|
||||
class Storage {
|
||||
/** @var \SitePress */
|
||||
private $sitepress;
|
||||
|
||||
/**
|
||||
* @param \SitePress $sitepress
|
||||
*/
|
||||
public function __construct( \SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $service
|
||||
*
|
||||
* @return Project|null
|
||||
*/
|
||||
public function getByService( \stdClass $service ) {
|
||||
return $this->getProjects()->get( \TranslationProxy_Project::generate_service_index( $service ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $service
|
||||
* @param Project $project
|
||||
*/
|
||||
public function save( \stdClass $service, Project $project ) {
|
||||
$index = \TranslationProxy_Project::generate_service_index( $service );
|
||||
$this->sitepress->set_setting(
|
||||
'icl_translation_projects',
|
||||
$this->getProjects()->put( $index, $project )->map(
|
||||
function ( Project $project ) {
|
||||
return $project->toArray();
|
||||
}
|
||||
)->toArray(),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getProjects() {
|
||||
$projects = $this->sitepress->get_setting( 'icl_translation_projects', [] );
|
||||
if ( ! is_array( $projects ) ) {
|
||||
$projects = [];
|
||||
}
|
||||
|
||||
return \wpml_collect( $projects )->map(
|
||||
function ( array $rawProject ) {
|
||||
return Project::fromArray( $rawProject );
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\TM\TranslationProxy\Services;
|
||||
|
||||
class Storage {
|
||||
/** @var \SitePress $sitepress */
|
||||
private $sitepress;
|
||||
|
||||
/**
|
||||
* @param \SitePress $sitepress
|
||||
*/
|
||||
public function __construct( \SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current translation service
|
||||
*
|
||||
* @return bool|\stdClass
|
||||
*/
|
||||
public function getCurrentService() {
|
||||
return $this->sitepress->get_setting( 'translation_service' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the input service as the current translation service setting.
|
||||
*
|
||||
* @param \stdClass $service
|
||||
*/
|
||||
public function setCurrentService( \stdClass $service ) {
|
||||
do_action( 'wpml_tm_before_set_translation_service', $service );
|
||||
$this->sitepress->set_setting( 'translation_service', $service, true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user