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,116 @@
<?php
/**
* Class WPML_TP_Abstract_API
*
* @author OnTheGoSystems
*/
abstract class WPML_TP_Abstract_API {
/** @var WPML_TP_Client $tp_client */
protected $tp_client;
/** @var null|Exception $exception */
protected $exception;
/** @var null|string $error_message */
protected $error_message;
public function __construct( WPML_TP_Client $tp_client ) {
$this->tp_client = $tp_client;
}
/** @return string */
abstract protected function get_endpoint_uri();
/** @return bool */
abstract protected function is_authenticated();
/**
* @param array $params
*
* @return mixed
*/
protected function get( array $params = array() ) {
return $this->remote_call( $params, 'GET' );
}
/**
* @param array $params
*
* @return mixed
*/
protected function post( array $params = array() ) {
return $this->remote_call( $params, 'POST' );
}
protected function put( array $params = array() ) {
// @todo: Implement put
}
protected function delete( array $params = array() ) {
// @todo: Implement delete
}
/**
* @param array $params
* @param string $method
*
* @return mixed
*/
private function remote_call( array $params, $method ) {
$response = false;
try {
$params = $this->pre_process_params( $params );
$response = TranslationProxy_Api::proxy_request( $this->get_endpoint_uri(), $params, $method );
} catch ( Exception $e ) {
$this->exception = $e;
}
return $response;
}
/**
* @param array $params
*
* @return array
*/
private function pre_process_params( array $params ) {
if ( $this->is_authenticated() ) {
$params['accesskey'] = $this->tp_client->get_project()->get_access_key();
}
return $params;
}
/**
* WPML does not store the Translation Proxy Job ID
* We have to identify the job somehow.
* This is why we are using `original_file_id`.
* It is the same as used in the XLIFF file as a value of `original` attribute.
* The combination of `original_file_id` and `batch_id` will be always unique.
* Translation Proxy provides this call, with these arguments, for this specific reason.
*
* @see https://git.onthegosystems.com/tp/translation-proxy/wikis/rate_translation
* @see https://git.onthegosystems.com/tp/translation-proxy/wikis/send_feedback
*
* @param int $job_id
* @param int $document_source_id
*
* @return string
*/
protected function get_original_file_id( $job_id, $document_source_id ) {
return $job_id . '-' . md5( $job_id . $document_source_id );
}
/** @return null|Exception */
public function get_exception() {
return $this->exception;
}
/** @return null|string */
public function get_error_message() {
return $this->exception->getMessage();
}
}

View File

@@ -0,0 +1,99 @@
<?php
/**
* Class WPML_TP_API_Batches
*/
class WPML_TP_API_Batches extends WPML_TP_Abstract_API {
const API_VERSION = 1.1;
const CREATE_BATCH_ENDPOINT = '/projects/{project_id}/batches.json';
const ADD_JOB_ENDPOINT = '/batches/{batch_id}/jobs.json';
private $endpoint_uri;
protected function get_endpoint_uri() {
return $this->endpoint_uri;
}
protected function is_authenticated() {
return true;
}
/**
* @throws WPML_TP_Batch_Exception
*
* @param array $batch_data
* @param false|array $extra_fields
*
* @return false|stdClass
*
* @link https://git.onthegosystems.com/tp/translation-proxy/wikis/create_batch_job
*/
public function create( array $batch_data, $extra_fields ) {
$batch = false;
$this->endpoint_uri = self::CREATE_BATCH_ENDPOINT;
$params = array(
'api_version' => self::API_VERSION,
'project_id' => $this->tp_client->get_project()->get_id(),
'batch' => $batch_data,
);
if ( $extra_fields ) {
$params['extra_fields'] = $extra_fields;
}
$response = $this->post( $params );
if ( $this->get_exception() ) {
throw new WPML_TP_Batch_Exception( $this->get_error_message() );
}
if ( $response ) {
$batch = new WPML_TP_Batch( $response->batch );
}
return $batch;
}
/**
* @param int $batch_id
* @param array $job_data
*
* @return false|WPML_TP_Job
*
* @link https://git.onthegosystems.com/tp/translation-proxy/wikis/add_files_batch_job
*/
public function add_job( $batch_id, array $job_data ) {
$job = false;
$this->endpoint_uri = self::ADD_JOB_ENDPOINT;
$params = array(
'api_version' => self::API_VERSION,
'batch_id' => $batch_id,
'job' => $job_data,
);
$response = $this->post( $params );
if ( $response ) {
$job = new WPML_TP_Job( $response->job );
}
return $job;
}
/**
* @link https://git.onthegosystems.com/tp/translation-proxy/wikis/commit_batch_job
*/
public function commit() {
// To be implemented
}
/**
* @link https://git.onthegosystems.com/tp/translation-proxy/wikis/send-preview-bundle-job
*/
public function send_preview_bundle() {
// To be implemented
}
}

View File

@@ -0,0 +1,233 @@
<?php
use WPML\FP\Fns;
/**
* Class WPML_TP_API_Services
*
* @author OnTheGoSystems
*/
class WPML_TP_API_Services extends WPML_TP_Abstract_API {
const ENDPOINT_SERVICES = '/services.json';
const ENDPOINT_SERVICE = '/services/{service_id}.json';
const ENDPOINT_LANGUAGES_MAP = '/services/{service_id}/language_identifiers.json';
const ENDPOINT_CUSTOM_FIELDS = '/services/{service_id}/custom_fields.json';
const TRANSLATION_MANAGEMENT_SYSTEM = 'tms';
const PARTNER = 'partner';
const TRANSLATION_SERVICE = 'ts';
const CACHED_SERVICES_KEY_DATA = 'wpml_translation_services';
const CACHED_SERVICES_TRANSIENT_KEY = 'wpml_translation_services_list';
const CACHED_SERVICES_KEY_TIMESTAMP = 'wpml_translation_services_timestamp';
private $endpoint;
/** @return string */
protected function get_endpoint_uri() {
return $this->endpoint;
}
/** @return bool */
protected function is_authenticated() {
return false;
}
/**
* @param bool $reload
*
* @return array
*/
public function get_all( $reload = false ) {
$this->endpoint = self::ENDPOINT_SERVICES;
$translation_services = $reload ? null : $this->get_cached_services();
if ( ! $translation_services || $this->has_cache_services_expired() ) {
$fresh_translation_services = parent::get();
if ( $fresh_translation_services ) {
$translation_services = $this->convert_to_tp_services( $fresh_translation_services );
$this->cache_services( $translation_services );
}
}
return apply_filters( 'otgs_translation_get_services', $translation_services ? $translation_services : array() );
}
/**
* @return bool
*/
public function refresh_cache() {
update_option( self::CACHED_SERVICES_KEY_TIMESTAMP, strtotime( '-2 day', $this->get_cached_services_timestamp() ) );
return (bool) $this->get_all();
}
/**
* @return mixed
*/
private function get_cached_services() {
return get_option( self::CACHED_SERVICES_KEY_DATA );
}
/**
* @return mixed
*/
private function get_cached_services_timestamp() {
return get_option( self::CACHED_SERVICES_KEY_TIMESTAMP );
}
/**
* @param $services
*/
private function cache_services( $services ) {
update_option( self::CACHED_SERVICES_KEY_DATA, $services, 'no' );
update_option( self::CACHED_SERVICES_KEY_TIMESTAMP, time() );
}
/**
* @return bool
*/
private function has_cache_services_expired() {
return time() >= strtotime( '+1 day', $this->get_cached_services_timestamp() );
}
/**
* @param array $translation_services
*
* @return array
*/
private function convert_to_tp_services( $translation_services ) {
return Fns::map( Fns::constructN( 1, \WPML_TP_Service::class ), $translation_services );
}
/**
* @param bool $partner
* @return array
*/
public function get_translation_services( $partner = true ) {
return array_values(
wp_list_filter(
$this->get_all(),
array(
self::TRANSLATION_MANAGEMENT_SYSTEM => false,
self::PARTNER => $partner,
)
)
);
}
/**
* @return array
*/
public function get_translation_management_systems() {
return array_values( wp_list_filter( $this->get_all(), array( self::TRANSLATION_MANAGEMENT_SYSTEM => true ) ) );
}
/**
* @param bool $reload
*
* @return null|WPML_TP_Service
*/
public function get_active( $reload = false ) {
return $this->get_one( $this->tp_client->get_project()->get_translation_service_id(), $reload );
}
/**
* @param int $service_id
* @param bool $reload
*
* @return null|string
*/
public function get_name( $service_id, $reload = false ) {
$translator_name = null;
/** @var array $translation_services */
$translation_service = $this->get_one( $service_id, $reload );
if ( null !== $translation_service && isset( $translation_service->name ) ) {
$translator_name = $translation_service->name;
}
return $translator_name;
}
public function get_service( $service_id, $reload = false ) {
return $this->get_one( $service_id, $reload );
}
/**
* @param int $translation_service_id
* @param bool $reload
*
* @return null|WPML_TP_Service
*/
private function get_one( $translation_service_id, $reload = false ) {
$translation_service = null;
if ( ! $translation_service_id ) {
return $translation_service;
}
/** @var array $translation_services */
$translation_services = $this->get_all( $reload );
$translation_services = wp_list_filter(
$translation_services,
array(
'id' => (int) $translation_service_id,
)
);
if ( $translation_services ) {
$translation_service = current( $translation_services );
} else {
$translation_service = $this->get_unlisted_service( $translation_service_id );
}
return $translation_service;
}
/**
* @param string $translation_service_id
*
* @return null|WPML_TP_Service
*/
private function get_unlisted_service( $translation_service_id ) {
$this->endpoint = self::ENDPOINT_SERVICE;
$service = parent::get( array( 'service_id' => $translation_service_id ) );
if ( $service instanceof stdClass ) {
return new WPML_TP_Service( $service );
}
return null;
}
/**
* @param $service_id
*
* @return array
*/
public function get_languages_map( $service_id ) {
$this->endpoint = self::ENDPOINT_LANGUAGES_MAP;
$args = array(
'service_id' => $service_id,
);
return parent::get( $args );
}
/**
* @param $service_id
*
* @return mixed
*/
public function get_custom_fields( $service_id ) {
$this->endpoint = self::ENDPOINT_CUSTOM_FIELDS;
$args = array(
'service_id' => $service_id,
);
return parent::get( $args );
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* Class WPML_TP_API_TF_Feedback
*
* @author OnTheGoSystems
*/
class WPML_TP_API_TF_Feedback extends WPML_TP_Abstract_API {
const URI_SEND = '/batches/{batch_id}/jobs/{original_file_id}/feedbacks';
const URI_GET_STATUS = '/feedbacks/{feedback_id}';
/** @var string $endpoint_uri */
private $endpoint_uri;
/** @return string */
protected function get_endpoint_uri() {
return $this->endpoint_uri;
}
/** @return bool */
protected function is_authenticated() {
return true;
}
/**
* @param WPML_TF_Feedback $feedback
* @param array $args
*
* @return int|false
*/
public function send( WPML_TF_Feedback $feedback, array $args ) {
$previous_sent_feedback_id = $feedback->get_tp_responses()->get_feedback_id();
if ( $previous_sent_feedback_id ) {
return $previous_sent_feedback_id;
}
$this->endpoint_uri = self::URI_SEND;
$ret = false;
$feedback_parameters = array(
'message' => $feedback->get_content(),
);
if ( array_key_exists( 'email', $args ) ) {
$feedback_parameters['email'] = $args['email'];
}
$params = array(
'batch_id' => $this->tp_client->get_tm_jobs()->get_batch_id( $feedback->get_job_id() ),
'original_file_id' => $this->get_original_file_id(
$feedback->get_job_id(),
$feedback->get_document_information()->get_source_id()
),
'feedback' => $feedback_parameters,
);
$response = $this->post( $params );
if ( isset( $response->feedback->id ) ) {
$ret = (int) $response->feedback->id;
}
return $ret;
}
/**
* @param WPML_TF_Feedback $feedback
*
* @return false[string
*/
public function status( WPML_TF_Feedback $feedback ) {
$this->endpoint_uri = self::URI_GET_STATUS;
$status = false;
$params = array(
'feedback_id' => $feedback->get_tp_responses()->get_feedback_id(),
);
$response = $this->get( $params );
if ( isset( $response->status ) ) {
$status = $response->status;
}
return $status;
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Class WPML_TP_API_TF_Ratings
*
* @author OnTheGoSystems
*/
class WPML_TP_API_TF_Ratings extends WPML_TP_Abstract_API {
/** @return string */
protected function get_endpoint_uri() {
return '/batches/{batch_id}/jobs/{original_file_id}/ratings';
}
/** @return bool */
protected function is_authenticated() {
return true;
}
/**
* @param WPML_TF_Feedback $feedback
*
* @return int|false
*/
public function send( WPML_TF_Feedback $feedback ) {
$params = array(
'batch_id' => $this->tp_client->get_tm_jobs()->get_batch_id( $feedback->get_job_id() ),
'original_file_id' => $this->get_original_file_id(
$feedback->get_job_id(),
$feedback->get_document_information()->get_source_id()
),
'rating' => array(
'rating' => $feedback->get_rating(),
),
);
$response = $this->post( $params );
if ( isset( $response->rating->id ) ) {
return (int) $response->rating->id;
}
return false;
}
}