first commit
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Jobs_Collection {
|
||||
|
||||
private $project;
|
||||
|
||||
private $job_factory;
|
||||
|
||||
private $batch_factory;
|
||||
|
||||
private $jobs;
|
||||
|
||||
public function __construct(
|
||||
TranslationProxy_Project $project,
|
||||
WPML_TP_Job_Factory $job_factory,
|
||||
WPML_Translation_Batch_Factory $batch_factory
|
||||
) {
|
||||
$this->project = $project;
|
||||
$this->job_factory = $job_factory;
|
||||
$this->batch_factory = $batch_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WPML_TP_Job[]
|
||||
*/
|
||||
public function get_all() {
|
||||
$jobs_obj = array();
|
||||
if ( ! $this->jobs ) {
|
||||
$jobs = $this->project->jobs();
|
||||
foreach ( $jobs as $job ) {
|
||||
$jobs_obj[] = $this->job_factory->create( $job );
|
||||
}
|
||||
$this->jobs = $jobs_obj;
|
||||
}
|
||||
return $this->jobs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_Translation_Job $job
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_job_canceled( WPML_Translation_Job $job ) {
|
||||
$canceled = false;
|
||||
$batch = $this->batch_factory->create( $job->get_batch_id() );
|
||||
|
||||
foreach ( $this->get_all() as $tp_job ) {
|
||||
if ( (int) $batch->get_batch_tp_id() === $tp_job->get_batch()->id
|
||||
&& (int) $job->get_original_element_id() === $tp_job->get_original_element_id()
|
||||
&& WPML_TP_Job::CANCELLED === $tp_job->get_job_state()
|
||||
) {
|
||||
$canceled = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $canceled;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Batch_Exception extends WPML_TP_Exception {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Exception extends Exception {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @link https://git.onthegosystems.com/tp/translation-proxy/wikis/create_batch_job
|
||||
*/
|
||||
class WPML_TP_Batch extends WPML_TP_REST_Object {
|
||||
|
||||
private $id;
|
||||
|
||||
public function get_id() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function set_id( $id ) {
|
||||
$this->id = (int) $id;
|
||||
}
|
||||
|
||||
protected function get_properties() {
|
||||
return array(
|
||||
'id' => 'id',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Job_Factory {
|
||||
|
||||
/**
|
||||
* @param stdClass $job
|
||||
*
|
||||
* @return WPML_TP_Job
|
||||
*/
|
||||
public function create( stdClass $job ) {
|
||||
return new WPML_TP_Job( $job );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @link https://git.onthegosystems.com/tp/translation-proxy/wikis/add_files_batch_job
|
||||
*/
|
||||
class WPML_TP_Job extends WPML_TP_REST_Object {
|
||||
|
||||
const CANCELLED = 'cancelled';
|
||||
|
||||
/** @var int */
|
||||
private $id;
|
||||
|
||||
private $cms_id;
|
||||
|
||||
private $batch;
|
||||
|
||||
private $job_state;
|
||||
|
||||
/** @param int $id */
|
||||
public function set_id( $id ) {
|
||||
$this->id = (int) $id;
|
||||
}
|
||||
|
||||
/** @return int */
|
||||
public function get_id() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function get_cms_id() {
|
||||
return $this->cms_id;
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function get_job_state() {
|
||||
return $this->job_state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function get_original_element_id() {
|
||||
preg_match_all( '/\d+/', $this->get_cms_id(), $matches );
|
||||
return isset( $matches[0][0] ) ? (int) $matches[0][0] : null;
|
||||
}
|
||||
|
||||
/** @return stdClass */
|
||||
public function get_batch() {
|
||||
return $this->batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function set_cms_id( $id ) {
|
||||
$this->cms_id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $state
|
||||
*/
|
||||
public function set_job_state( $state ) {
|
||||
$this->job_state = $state;
|
||||
}
|
||||
|
||||
public function set_batch( stdClass $batch ) {
|
||||
$this->batch = $batch;
|
||||
}
|
||||
|
||||
/** @return array */
|
||||
protected function get_properties() {
|
||||
return array(
|
||||
'id' => 'id',
|
||||
'batch' => 'batch',
|
||||
'cms_id' => 'cms_id',
|
||||
'job_state' => 'job_state',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
abstract class WPML_TP_REST_Object {
|
||||
|
||||
public function __construct( stdClass $object = null ) {
|
||||
$this->populate_properties_from_object( $object );
|
||||
}
|
||||
|
||||
abstract protected function get_properties();
|
||||
|
||||
/**
|
||||
* @param stdClass|null $object
|
||||
*/
|
||||
protected function populate_properties_from_object( $object ) {
|
||||
if ( $object ) {
|
||||
$properties = $this->get_properties();
|
||||
|
||||
foreach ( $properties as $object_property => $new_property ) {
|
||||
if ( isset( $object->{$object_property} ) ) {
|
||||
$this->{"set_$new_property"}( $object->{$object_property} );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,725 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @link https://git.onthegosystems.com/tp/translation-proxy/wikis/translation_services
|
||||
*/
|
||||
class WPML_TP_Service extends WPML_TP_REST_Object implements Serializable {
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $logo_url;
|
||||
|
||||
/** @var string */
|
||||
public $logo_preview_url;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $doc_url;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $tms;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $partner;
|
||||
|
||||
/**
|
||||
* @var stdClass
|
||||
*/
|
||||
public $custom_fields;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* @deprecated
|
||||
*/
|
||||
public $custom_fields_data;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
* @deprecated
|
||||
*/
|
||||
public $requires_authentication;
|
||||
|
||||
/**
|
||||
* @var stdClass
|
||||
*/
|
||||
public $rankings;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public $has_language_pairs;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $url;
|
||||
|
||||
/** @var string */
|
||||
public $project_details_url;
|
||||
/** @var string */
|
||||
public $add_language_pair_url;
|
||||
/** @var string */
|
||||
public $custom_text_url;
|
||||
/** @var string */
|
||||
public $select_translator_iframe_url;
|
||||
/** @var string */
|
||||
public $translator_contact_iframe_url;
|
||||
/** @var string */
|
||||
public $quote_iframe_url;
|
||||
/** @var bool */
|
||||
public $has_translator_selection;
|
||||
/** @var int */
|
||||
public $project_name_length;
|
||||
/** @var string */
|
||||
public $suid;
|
||||
/** @var bool */
|
||||
public $notification;
|
||||
/** @var bool */
|
||||
public $preview_bundle;
|
||||
/** @var bool */
|
||||
public $deadline;
|
||||
/** @var bool */
|
||||
public $oauth;
|
||||
/** @var string */
|
||||
public $oauth_url;
|
||||
/** @var int */
|
||||
public $default_service;
|
||||
/** @var bool */
|
||||
public $translation_feedback;
|
||||
/** @var string */
|
||||
public $feedback_forward_method;
|
||||
/** @var int */
|
||||
public $last_refresh;
|
||||
|
||||
/** @var string */
|
||||
public $popup_message;
|
||||
|
||||
/** @var string */
|
||||
public $how_to_get_credentials_desc;
|
||||
|
||||
/** @var string */
|
||||
public $how_to_get_credentials_url;
|
||||
|
||||
/** @var string */
|
||||
public $client_create_account_page_url;
|
||||
|
||||
/** bool */
|
||||
public $redirect_to_ts;
|
||||
|
||||
/** @var \stdClass[] */
|
||||
public $countries = [];
|
||||
|
||||
public function __construct( stdClass $object = null ) {
|
||||
parent::__construct( $object );
|
||||
$this->set_custom_fields_data();
|
||||
$this->set_requires_authentication();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_logo_url() {
|
||||
return $this->logo_url;
|
||||
}
|
||||
|
||||
public function get_logo_preview_url() {
|
||||
return $this->logo_preview_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_doc_url() {
|
||||
return $this->doc_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_tms() {
|
||||
return $this->tms;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_partner() {
|
||||
return $this->partner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $partner
|
||||
*/
|
||||
public function set_partner( $partner ) {
|
||||
$this->partner = $partner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_custom_fields() {
|
||||
$custom_fields = array();
|
||||
|
||||
/** @TODO: This is odd. It appears that if it's the active service then it's
|
||||
* stored with an extra custom_fields property eg. $this->custom_fields->custom_fields
|
||||
* It looks like we call the api to get the custom field when we activate the service and store
|
||||
* it directly here
|
||||
*/
|
||||
|
||||
if ( is_object( $this->custom_fields ) && isset( $this->custom_fields->custom_fields ) ) {
|
||||
$custom_fields = $this->custom_fields->custom_fields;
|
||||
} elseif ( isset( $this->custom_fields ) ) {
|
||||
$custom_fields = $this->custom_fields;
|
||||
}
|
||||
|
||||
return $custom_fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_custom_fields_data() {
|
||||
return $this->custom_fields_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_requires_authentication() {
|
||||
return $this->requires_authentication;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_url() {
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_has_language_pairs() {
|
||||
return (bool) $this->has_language_pairs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return stdClass
|
||||
*/
|
||||
public function get_rankings() {
|
||||
return $this->rankings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_popup_message() {
|
||||
return $this->popup_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*/
|
||||
public function set_id( $id ) {
|
||||
$this->id = (int) $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $logo_url
|
||||
*/
|
||||
public function set_logo_url( $logo_url ) {
|
||||
$this->logo_url = $logo_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $logo_preview_url
|
||||
*/
|
||||
public function set_logo_preview_url( $logo_preview_url ) {
|
||||
$this->logo_preview_url = $logo_preview_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
*/
|
||||
public function set_url( $url ) {
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function set_name( $name ) {
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
*/
|
||||
public function set_description( $description ) {
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $doc_url
|
||||
*/
|
||||
public function set_doc_url( $doc_url ) {
|
||||
$this->doc_url = $doc_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $tms
|
||||
*/
|
||||
public function set_tms( $tms ) {
|
||||
$this->tms = (bool) $tms;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stdClass $rankings
|
||||
*/
|
||||
public function set_rankings( $rankings ) {
|
||||
$this->rankings = $rankings;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stdClass $custom_fields
|
||||
*/
|
||||
public function set_custom_fields( $custom_fields ) {
|
||||
$this->custom_fields = $custom_fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $popup_message
|
||||
*/
|
||||
public function set_popup_message( $popup_message ) {
|
||||
$this->popup_message = $popup_message;
|
||||
}
|
||||
|
||||
public function set_custom_fields_data() {
|
||||
global $sitepress;
|
||||
|
||||
$active_service = $sitepress->get_setting( 'translation_service' );
|
||||
if ( isset( $active_service->custom_fields_data, $active_service->id ) && $this->id === $active_service->id ) {
|
||||
$this->custom_fields_data = $active_service->custom_fields_data;
|
||||
}
|
||||
}
|
||||
|
||||
public function set_requires_authentication() {
|
||||
$this->requires_authentication = (bool) $this->custom_fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
*/
|
||||
public function set_has_language_pairs( $value ) {
|
||||
$this->has_language_pairs = (bool) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_project_details_url() {
|
||||
return $this->project_details_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $project_details_url
|
||||
*/
|
||||
public function set_project_details_url( $project_details_url ) {
|
||||
$this->project_details_url = $project_details_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_add_language_pair_url() {
|
||||
return $this->add_language_pair_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $add_language_pair_url
|
||||
*/
|
||||
public function set_add_language_pair_url( $add_language_pair_url ) {
|
||||
$this->add_language_pair_url = $add_language_pair_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_custom_text_url() {
|
||||
return $this->custom_text_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $custom_text_url
|
||||
*/
|
||||
public function set_custom_text_url( $custom_text_url ) {
|
||||
$this->custom_text_url = $custom_text_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_select_translator_iframe_url() {
|
||||
return $this->select_translator_iframe_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $select_translator_iframe_url
|
||||
*/
|
||||
public function set_select_translator_iframe_url( $select_translator_iframe_url ) {
|
||||
$this->select_translator_iframe_url = $select_translator_iframe_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_translator_contact_iframe_url() {
|
||||
return $this->translator_contact_iframe_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $translator_contact_iframe_url
|
||||
*/
|
||||
public function set_translator_contact_iframe_url( $translator_contact_iframe_url ) {
|
||||
$this->translator_contact_iframe_url = $translator_contact_iframe_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_quote_iframe_url() {
|
||||
return $this->quote_iframe_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $quote_iframe_url
|
||||
*/
|
||||
public function set_quote_iframe_url( $quote_iframe_url ) {
|
||||
$this->quote_iframe_url = $quote_iframe_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_has_translator_selection() {
|
||||
return $this->has_translator_selection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $has_translator_selection
|
||||
*/
|
||||
public function set_has_translator_selection( $has_translator_selection ) {
|
||||
$this->has_translator_selection = (bool) $has_translator_selection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_project_name_length() {
|
||||
return $this->project_name_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $project_name_length
|
||||
*/
|
||||
public function set_project_name_length( $project_name_length ) {
|
||||
$this->project_name_length = (int) $project_name_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_suid() {
|
||||
return $this->suid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $suid
|
||||
*/
|
||||
public function set_suid( $suid ) {
|
||||
$this->suid = $suid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_notification() {
|
||||
return $this->notification;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $notification
|
||||
*/
|
||||
public function set_notification( $notification ) {
|
||||
$this->notification = (bool) $notification;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_preview_bundle() {
|
||||
return $this->preview_bundle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $preview_bundle
|
||||
*/
|
||||
public function set_preview_bundle( $preview_bundle ) {
|
||||
$this->preview_bundle = (bool) $preview_bundle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_deadline() {
|
||||
return $this->deadline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $deadline
|
||||
*/
|
||||
public function set_deadline( $deadline ) {
|
||||
$this->deadline = (bool) $deadline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_oauth() {
|
||||
return $this->oauth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $oauth
|
||||
*/
|
||||
public function set_oauth( $oauth ) {
|
||||
$this->oauth = (bool) $oauth;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_oauth_url() {
|
||||
return $this->oauth_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $oauth_url
|
||||
*/
|
||||
public function set_oauth_url( $oauth_url ) {
|
||||
$this->oauth_url = $oauth_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function get_default_service() {
|
||||
return $this->default_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $default_service
|
||||
*/
|
||||
public function set_default_service( $default_service ) {
|
||||
$this->default_service = (int) $default_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function get_translation_feedback() {
|
||||
return $this->translation_feedback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $translation_feedback
|
||||
*/
|
||||
public function set_translation_feedback( $translation_feedback ) {
|
||||
$this->translation_feedback = (bool) $translation_feedback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_feedback_forward_method() {
|
||||
return $this->feedback_forward_method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $feedback_forward_method
|
||||
*/
|
||||
public function set_feedback_forward_method( $feedback_forward_method ) {
|
||||
$this->feedback_forward_method = $feedback_forward_method;
|
||||
}
|
||||
|
||||
/** @return null|int */
|
||||
public function get_last_refresh() {
|
||||
return $this->last_refresh;
|
||||
}
|
||||
|
||||
/** @param int */
|
||||
public function set_last_refresh( $timestamp ) {
|
||||
$this->last_refresh = $timestamp;
|
||||
}
|
||||
|
||||
/** @return null|string */
|
||||
public function get_how_to_get_credentials_desc() {
|
||||
return $this->how_to_get_credentials_desc;
|
||||
}
|
||||
|
||||
/** @param string */
|
||||
public function set_how_to_get_credentials_desc( $desc ) {
|
||||
$this->how_to_get_credentials_desc = $desc;
|
||||
}
|
||||
|
||||
/** @return null|string */
|
||||
public function get_how_to_get_credentials_url() {
|
||||
return $this->how_to_get_credentials_url;
|
||||
}
|
||||
|
||||
/** @param string */
|
||||
public function set_how_to_get_credentials_url( $url ) {
|
||||
$this->how_to_get_credentials_url = $url;
|
||||
}
|
||||
|
||||
/** @return null|string */
|
||||
public function get_client_create_account_page_url() {
|
||||
return $this->client_create_account_page_url;
|
||||
}
|
||||
|
||||
/** @param string */
|
||||
public function set_client_create_account_page_url( $url ) {
|
||||
$this->client_create_account_page_url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_redirect_to_ts() {
|
||||
return $this->redirect_to_ts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $redirect_to_ts
|
||||
*/
|
||||
public function set_redirect_to_ts( $redirect_to_ts ) {
|
||||
$this->redirect_to_ts = $redirect_to_ts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return stdClass[]
|
||||
*/
|
||||
public function get_countries() {
|
||||
return $this->countries;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param stdClass[] $countries
|
||||
*/
|
||||
public function set_countries( array $countries ) {
|
||||
$this->countries = $countries;
|
||||
}
|
||||
|
||||
|
||||
public function serialize() {
|
||||
return serialize( get_object_vars( $this ) );
|
||||
}
|
||||
|
||||
public function unserialize( $serialized ) {
|
||||
$simple_array = unserialize( $serialized );
|
||||
|
||||
foreach ( $simple_array as $property => $value ) {
|
||||
if ( property_exists( $this, $property ) ) {
|
||||
$this->$property = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function get_properties() {
|
||||
return [
|
||||
'id' => 'id',
|
||||
'logo_url' => 'logo_url',
|
||||
'logo_preview_url' => 'logo_preview_url',
|
||||
'url' => 'url',
|
||||
'name' => 'name',
|
||||
'description' => 'description',
|
||||
'doc_url' => 'doc_url',
|
||||
'tms' => 'tms',
|
||||
'partner' => 'partner',
|
||||
'custom_fields' => 'custom_fields',
|
||||
'custom_fields_data' => 'custom_fields_data',
|
||||
'requires_authentication' => 'requires_authentication',
|
||||
'has_language_pairs' => 'has_language_pairs',
|
||||
'rankings' => 'rankings',
|
||||
'popup_message' => 'popup_message',
|
||||
'project_details_url' => 'project_details_url',
|
||||
'add_language_pair_url' => 'add_language_pair_url',
|
||||
'custom_text_url' => 'custom_text_url',
|
||||
'select_translator_iframe_url' => 'select_translator_iframe_url',
|
||||
'translator_contact_iframe_url' => 'translator_contact_iframe_url',
|
||||
'quote_iframe_url' => 'quote_iframe_url',
|
||||
'has_translator_selection' => 'has_translator_selection',
|
||||
'project_name_length' => 'project_name_length',
|
||||
'suid' => 'suid',
|
||||
'notification' => 'notification',
|
||||
'preview_bundle' => 'preview_bundle',
|
||||
'deadline' => 'deadline',
|
||||
'oauth' => 'oauth',
|
||||
'oauth_url' => 'oauth_url',
|
||||
'default_service' => 'default_service',
|
||||
'translation_feedback' => 'translation_feedback',
|
||||
'feedback_forward_method' => 'feedback_forward_method',
|
||||
'last_refresh' => 'last_refresh',
|
||||
'how_to_get_credentials_desc' => 'how_to_get_credentials_desc',
|
||||
'how_to_get_credentials_url' => 'how_to_get_credentials_url',
|
||||
'client_create_account_page_url' => 'client_create_account_page_url',
|
||||
'redirect_to_ts?' => 'redirect_to_ts',
|
||||
'countries' => 'countries',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TP_Client_Factory
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TP_Client_Factory {
|
||||
|
||||
/** @return WPML_TP_Client */
|
||||
public function create() {
|
||||
global $sitepress, $wpdb;
|
||||
|
||||
$translation_service = $sitepress->get_setting( 'translation_service' );
|
||||
$translation_projects = $sitepress->get_setting( 'icl_translation_projects' );
|
||||
|
||||
$project = new WPML_TP_Project( $translation_service, $translation_projects );
|
||||
$tm_jobs = new WPML_TP_TM_Jobs( $wpdb );
|
||||
|
||||
return new WPML_TP_Client( $project, $tm_jobs );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TP_Client
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TP_Client {
|
||||
|
||||
/** @var WPML_TP_Project $project */
|
||||
private $project;
|
||||
|
||||
/** @var WPML_TP_TM_Jobs $tm_jobs */
|
||||
private $tm_jobs;
|
||||
|
||||
/** @var WPML_TP_API_Services $services */
|
||||
private $services;
|
||||
|
||||
/** @var WPML_TP_API_Batches $batches */
|
||||
private $batches;
|
||||
|
||||
/** @var WPML_TP_API_TF_Ratings $ratings */
|
||||
private $ratings;
|
||||
|
||||
/** @var WPML_TP_API_TF_Feedback $feedback */
|
||||
private $feedback;
|
||||
|
||||
public function __construct(
|
||||
WPML_TP_Project $project,
|
||||
WPML_TP_TM_Jobs $tm_jobs
|
||||
) {
|
||||
$this->project = $project;
|
||||
$this->tm_jobs = $tm_jobs;
|
||||
}
|
||||
|
||||
public function services() {
|
||||
if ( ! $this->services ) {
|
||||
$this->services = new WPML_TP_API_Services( $this );
|
||||
}
|
||||
|
||||
return $this->services;
|
||||
}
|
||||
|
||||
public function batches() {
|
||||
if ( ! $this->batches ) {
|
||||
$this->batches = new WPML_TP_API_Batches( $this );
|
||||
}
|
||||
|
||||
return $this->batches;
|
||||
}
|
||||
|
||||
/** @return WPML_TP_API_TF_Ratings */
|
||||
public function ratings() {
|
||||
if ( ! $this->ratings ) {
|
||||
$this->ratings = new WPML_TP_API_TF_Ratings( $this );
|
||||
}
|
||||
|
||||
return $this->ratings;
|
||||
}
|
||||
|
||||
/** @return WPML_TP_API_TF_Feedback */
|
||||
public function feedback() {
|
||||
if ( ! $this->feedback ) {
|
||||
$this->feedback = new WPML_TP_API_TF_Feedback( $this );
|
||||
}
|
||||
|
||||
return $this->feedback;
|
||||
}
|
||||
|
||||
/** @return WPML_TP_Project */
|
||||
public function get_project() {
|
||||
return $this->project;
|
||||
}
|
||||
|
||||
public function get_tm_jobs() {
|
||||
return $this->tm_jobs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
class WPML_TP_Jobs_Collection_Factory {
|
||||
|
||||
/**
|
||||
* @return WPML_TP_Jobs_Collection
|
||||
*/
|
||||
public function create() {
|
||||
$tp_jobs_collection = null;
|
||||
$current_project = TranslationProxy::get_current_project();
|
||||
|
||||
if ( $current_project ) {
|
||||
$tp_jobs_collection = new WPML_TP_Jobs_Collection(
|
||||
TranslationProxy::get_current_project(),
|
||||
new WPML_TP_Job_Factory(),
|
||||
new WPML_Translation_Batch_Factory()
|
||||
);
|
||||
}
|
||||
|
||||
return $tp_jobs_collection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TP_Project
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TP_Project {
|
||||
|
||||
/** @var false|stdClass $translation_service */
|
||||
private $translation_service;
|
||||
|
||||
/** @var false|array $translation_projects */
|
||||
private $translation_projects;
|
||||
|
||||
/** @var array $project */
|
||||
private $project;
|
||||
|
||||
/**
|
||||
* WPML_TP_Project constructor.
|
||||
*
|
||||
* @param false|stdClass $translation_service
|
||||
* @param false|array $translation_projects
|
||||
*/
|
||||
public function __construct( $translation_service, $translation_projects ) {
|
||||
$this->translation_service = $translation_service;
|
||||
$this->translation_projects = $translation_projects;
|
||||
}
|
||||
|
||||
private function init() {
|
||||
if ( ! $this->project ) {
|
||||
$project_index = TranslationProxy_Project::generate_service_index( $this->translation_service );
|
||||
|
||||
if ( isset( $this->translation_projects [ $project_index ] ) ) {
|
||||
$this->project = $this->translation_projects[ $project_index ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @return int|null */
|
||||
public function get_translation_service_id() {
|
||||
return isset( $this->translation_service->id ) ? (int) $this->translation_service->id : null;
|
||||
}
|
||||
|
||||
/** @return string|null */
|
||||
public function get_access_key() {
|
||||
return $this->get_project_property( 'access_key' );
|
||||
}
|
||||
|
||||
/** @return int|null */
|
||||
public function get_id() {
|
||||
return (int) $this->get_project_property( 'id' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $project_property
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function get_project_property( $project_property ) {
|
||||
$this->init();
|
||||
return isset( $this->project[ $project_property ] ) ? $this->project[ $project_property ] : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TP_TM_Jobs
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TP_TM_Jobs {
|
||||
|
||||
const CACHE_BATCH_ID = 'wpml_tp_tm_jobs_batch_id';
|
||||
|
||||
/** @var wpdb $wpdb */
|
||||
private $wpdb;
|
||||
|
||||
/**
|
||||
* WPML_TF_Rating_TP_API constructor.
|
||||
*
|
||||
* @param wpdb $wpdb
|
||||
*/
|
||||
public function __construct( wpdb $wpdb ) {
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $job_id
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
public function get_batch_id( $job_id ) {
|
||||
$cached_batch_id = wp_cache_get( $job_id, self::CACHE_BATCH_ID );
|
||||
|
||||
if ( $cached_batch_id ) {
|
||||
return $cached_batch_id;
|
||||
}
|
||||
|
||||
$query = "SELECT tb.tp_id FROM {$this->wpdb->prefix}icl_translation_batches AS tb
|
||||
LEFT JOIN {$this->wpdb->prefix}icl_translation_status AS ts ON tb.id = ts.batch_id
|
||||
LEFT JOIN {$this->wpdb->prefix}icl_translate_job AS tj ON ts.rid = tj.rid
|
||||
WHERE tj.job_id = %d";
|
||||
|
||||
$batch_id = $this->wpdb->get_var( $this->wpdb->prepare( $query, $job_id ) );
|
||||
|
||||
wp_cache_set( $job_id, $batch_id, self::CACHE_BATCH_ID );
|
||||
|
||||
return $batch_id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user