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

View File

@@ -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;
}
}

View File

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

View File

@@ -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 );
}
);
}
}