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,55 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ICL20_Acknowledge {
private $end_point;
private $http;
/**
* WPML_TM_ICL20 constructor.
*
* @param WP_Http $http
* @param string $end_point
*/
public function __construct( WP_Http $http, $end_point ) {
$this->http = $http;
$this->end_point = $end_point;
}
/**
* @param string $ts_accesskey
* @param int $ts_id
*
* @return bool
* @throws \WPML_TM_ICL20MigrationException
*
* Note: `ts_id` (aka `website_id`) = `website_id`
*
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/icldev-2322
*/
public function acknowledge_icl( $ts_id, $ts_accesskey ) {
$url = $this->end_point . '/wpml/websites/' . $ts_id . '/migrated';
$url = add_query_arg( array(
'accesskey' => $ts_accesskey,
),
$url );
$response = $this->http->post( $url,
array(
'method' => 'POST',
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
),
) );
$code = (int) $response['response']['code'];
if ( $code !== 200 ) {
throw new WPML_TM_ICL20MigrationException( $response['response']['message'], $code );
}
return true;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ICL20_Migration_Container {
private $acknowledge;
private $project;
private $token;
public function __construct(
WPML_TM_ICL20_Token $token,
WPML_TM_ICL20_Project $project,
WPML_TM_ICL20_Acknowledge $ack
) {
$this->token = $token;
$this->project = $project;
$this->acknowledge = $ack;
}
/**
* @return WPML_TM_ICL20_Acknowledge
*/
public function get_acknowledge() {
return $this->acknowledge;
}
/**
* @return WPML_TM_ICL20_Project
*/
public function get_project() {
return $this->project;
}
/**
* @return WPML_TM_ICL20_Token
*/
public function get_token() {
return $this->token;
}
}

View File

@@ -0,0 +1,102 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ICL20_Project {
private $end_point;
private $http;
/**
* WPML_TM_ICL20 constructor.
*
* @param WP_Http $http
* @param string $end_point
*/
public function __construct( WP_Http $http, $end_point ) {
$this->http = $http;
$this->end_point = $end_point;
}
/**
* @param int $project_id
* @param string $access_key
* @param string $new_token
*
* @return bool|null
* @throws \WPML_TM_ICL20MigrationException
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/tsapi-887
*
*/
public function migrate( $project_id, $access_key, $new_token ) {
$url = $this->end_point . '/projects/' . $project_id . '/migrate_service.json';
$args = array(
'method' => 'POST',
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
),
'body' => wp_json_encode( array(
'accesskey' => $access_key,
'custom_fields' => array(
'api_token' => $new_token,
)
) )
);
$response = $this->http->post( $url, $args );
$code = (int) $response['response']['code'];
if ( $code !== 200 ) {
$message = $response['response']['message'];
if ( array_key_exists( 'body', $response ) ) {
$body = json_decode( $response['body'], JSON_OBJECT_AS_ARRAY );
if ( isset( $body['status']['message'] ) ) {
$message .= PHP_EOL . $body['status']['message'];
}
}
throw new WPML_TM_ICL20MigrationException( $message, $code );
}
return true;
}
/**
* @param int $project_id
* @param string $access_key
*
* @return bool
* @throws WPML_TM_ICL20MigrationException
*/
public function rollback_migration( $project_id, $access_key ) {
$url = $this->end_point . '/projects/' . $project_id . '/rollback_migration.json';
$args = array(
'method' => 'POST',
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
),
'body' => wp_json_encode( array( 'accesskey' => $access_key ) )
);
$response = $this->http->post( $url, $args );
$code = (int) $response['response']['code'];
if ( $code !== 200 ) {
$message = $response['response']['message'];
if ( array_key_exists( 'body', $response ) ) {
$body = json_decode( $response['body'], JSON_OBJECT_AS_ARRAY );
if ( isset( $body['status']['message'] ) ) {
$message .= PHP_EOL . $body['status']['message'];
}
}
throw new WPML_TM_ICL20MigrationException( $message, $code );
}
return true;
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ICL20_Token {
private $end_point;
private $http;
/**
* WPML_TM_ICL20 constructor.
*
* @param WP_Http $http
* @param string $end_point
*/
public function __construct( WP_Http $http, $end_point ) {
$this->http = $http;
$this->end_point = $end_point;
}
/**
* @param string $ts_accesskey
* @param int $ts_id
*
* @return string|null
* @throws \WPML_TM_ICL20MigrationException
*
* Note: `ts_id` (aka `website_id`) = `website_id`
*
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/icldev-2285
*/
public function get_token( $ts_id, $ts_accesskey ) {
if ( ! $ts_id ) {
throw new WPML_TM_ICL20MigrationException( 'Missing ICL Website ID ($ts_id)' );
}
if ( ! $ts_accesskey ) {
throw new WPML_TM_ICL20MigrationException( 'Missing ICL Access KEY ($ts_accesskey)' );
}
$url = $this->end_point . '/wpml/websites/' . $ts_id . '/token';
$url = add_query_arg( array(
'accesskey' => $ts_accesskey,
),
$url );
$response = $this->http->get( $url,
array(
'method' => 'GET',
'headers' => array(
'Accept: application/json'
),
) );
$code = (int) $response['response']['code'];
if ( 200 !== $code ) {
throw new WPML_TM_ICL20MigrationException( $response['response']['message'], $code );
}
if ( array_key_exists( 'body', $response ) ) {
$response_data = json_decode( $response['body'], JSON_OBJECT_AS_ARRAY );
if ( array_key_exists( 'api_token', $response_data )
&& '' !== trim( $response_data['api_token'] ) ) {
return $response_data['api_token'];
}
}
return null;
}
}