first commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Job_Progress {
|
||||
/** @var wpdb */
|
||||
private $wpdb;
|
||||
|
||||
public function __construct() {
|
||||
global $wpdb;
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Job_Entity $job
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get( WPML_TM_Job_Entity $job ) {
|
||||
if ( $job->get_translation_service() !== 'local' ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( $job->get_status() !== ICL_TM_IN_PROGRESS ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( $job->get_type() === WPML_TM_Job_Entity::STRING_TYPE ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$sql = "
|
||||
SELECT field_finished FROM {$this->wpdb->prefix}icl_translate translate
|
||||
INNER JOIN {$this->wpdb->prefix}icl_translate_job translate_job ON translate_job.job_id = translate.job_id
|
||||
INNER JOIN {$this->wpdb->prefix}icl_translation_status translation_status ON translation_status.rid = translate_job.rid
|
||||
WHERE translation_status.rid = %d AND translate.field_translate = 1 AND LENGTH(translate.field_data) > 0
|
||||
";
|
||||
$sql = $this->wpdb->prepare( $sql, $job->get_id() );
|
||||
|
||||
$elements = $this->wpdb->get_col( $sql );
|
||||
$translated = array_filter( $elements );
|
||||
|
||||
$percentage = (int) ( count( $translated ) / count( $elements ) * 100 );
|
||||
|
||||
return sprintf( _x( '%s completed', 'Translation jobs list', 'wpml-transation-manager' ), $percentage . '%' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Job_Translator_Name {
|
||||
|
||||
public function get( $translator_id ) {
|
||||
$user = get_user_by( 'id', $translator_id );
|
||||
if ( $user instanceof WP_User ) {
|
||||
return $user->display_name;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Jobs_Columns {
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function get_columns() {
|
||||
return array(
|
||||
'id' => __( 'ID', 'wpml-translation-management' ),
|
||||
'title' => __( 'Title', 'wpml-translation-management' ),
|
||||
'languages' => __( 'Languages', 'wpml-translation-management' ),
|
||||
'batch_name' => __( 'Batch name', 'wpml-translation-management' ),
|
||||
'translator' => __( 'Translated by', 'wpml-translation-management' ),
|
||||
'sent_date' => __( 'Sent on', 'wpml-translation-management' ),
|
||||
'deadline' => __( 'Deadline', 'wpml-translation-management' ),
|
||||
'status' => __( 'Status', 'wpml-translation-management' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function get_sortable() {
|
||||
return array(
|
||||
'id' => __( 'ID', 'wpml-translation-management' ),
|
||||
'title' => __( 'Title', 'wpml-translation-management' ),
|
||||
'batch_name' => __( 'Batch name', 'wpml-translation-management' ),
|
||||
'language' => __( 'Language', 'wpml-translation-management' ),
|
||||
'sent_date' => __( 'Sent on', 'wpml-translation-management' ),
|
||||
'deadline_date' => __( 'Deadline', 'wpml-translation-management' ),
|
||||
'status' => __( 'Status', 'wpml-translation-management' ),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Jobs_Criteria_Parser {
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WPML_TM_Jobs_Search_Params
|
||||
*/
|
||||
public function build_criteria( WP_REST_Request $request ) {
|
||||
$params = new WPML_TM_Jobs_Search_Params();
|
||||
|
||||
$params = $this->set_scope( $params, $request );
|
||||
$params = $this->set_pagination( $params, $request );
|
||||
$params = $this->set_filters( $params, $request );
|
||||
$params = $this->set_sorting( $params, $request );
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Jobs_Search_Params $params
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WPML_TM_Jobs_Search_Params
|
||||
*/
|
||||
private function set_scope( WPML_TM_Jobs_Search_Params $params, WP_REST_Request $request ) {
|
||||
$scope = $request->get_param( 'scope' );
|
||||
if ( WPML_TM_Jobs_Search_Params::is_valid_scope( $scope ) ) {
|
||||
$params->set_scope( $scope );
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Jobs_Search_Params $params
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WPML_TM_Jobs_Search_Params
|
||||
*/
|
||||
private function set_pagination( WPML_TM_Jobs_Search_Params $params, WP_REST_Request $request ) {
|
||||
$limit = (int) $request->get_param( 'limit' );
|
||||
if ( $limit > 0 ) {
|
||||
$params->set_limit( $limit );
|
||||
|
||||
$offset = (int) $request->get_param( 'offset' );
|
||||
if ( $offset > 0 ) {
|
||||
$params->set_offset( $offset );
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
private function set_filters( WPML_TM_Jobs_Search_Params $params, WP_REST_Request $request ) {
|
||||
foreach ( [ 'source_language', 'translated_by' ] as $key ) {
|
||||
$value = (string) $request->get_param( $key );
|
||||
if ( $value ) {
|
||||
$params->{'set_' . $key}( $value );
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( [ 'local_job_ids', 'title', 'target_language', 'status', 'batch_name' ] as $key ) {
|
||||
$value = (string) $request->get_param( $key );
|
||||
if ( strlen( $value ) ) {
|
||||
$params->{'set_' . $key}( explode( ',', $value ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $request->get_param( 'needs_update' ) ) {
|
||||
$params->set_needs_update( new WPML_TM_Jobs_Needs_Update_Param( $request->get_param( 'needs_update' ) ) );
|
||||
}
|
||||
|
||||
$date_range_values = array( 'sent', 'deadline' );
|
||||
foreach ( $date_range_values as $date_range_value ) {
|
||||
$from = $request->get_param( $date_range_value . '_from' );
|
||||
$to = $request->get_param( $date_range_value . '_to' );
|
||||
|
||||
if ( $from || $to ) {
|
||||
$from = $from ? new DateTime( $from ) : $from;
|
||||
$to = $to ? new DateTime( $to ) : $to;
|
||||
|
||||
if ( $from && $to && $from >= $to ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$params->{'set_' . $date_range_value}( new WPML_TM_Jobs_Date_Range( $from, $to ) );
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
private function set_sorting( WPML_TM_Jobs_Search_Params $params, WP_REST_Request $request ) {
|
||||
$sorting = $request->get_param( 'sorting' );
|
||||
if ( $sorting ) {
|
||||
$params->set_sorting( $this->build_sorting_params( $sorting ) );
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $request_param
|
||||
*
|
||||
* @return WPML_TM_Jobs_Sorting_Param[]
|
||||
*/
|
||||
private function build_sorting_params( array $request_param ) {
|
||||
return \wpml_collect( $request_param )->map(
|
||||
function ( $direction, $column ) {
|
||||
return new WPML_TM_Jobs_Sorting_Param( $column, $direction );
|
||||
}
|
||||
)->toArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Jobs_Element_Info {
|
||||
/** @var WPML_TM_Rest_Jobs_Package_Helper_Factory */
|
||||
private $package_helper_factory;
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Rest_Jobs_Package_Helper_Factory $package_helper_factory
|
||||
*/
|
||||
public function __construct( WPML_TM_Rest_Jobs_Package_Helper_Factory $package_helper_factory ) {
|
||||
$this->package_helper_factory = $package_helper_factory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Job_Entity $job
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get( WPML_TM_Job_Entity $job ) {
|
||||
$type = $job->get_type();
|
||||
$id = $job->get_original_element_id();
|
||||
$result = [];
|
||||
|
||||
switch ( $type ) {
|
||||
case WPML_TM_Job_Entity::POST_TYPE:
|
||||
$result = $this->get_for_post( $id );
|
||||
break;
|
||||
case WPML_TM_Job_Entity::STRING_TYPE:
|
||||
case WPML_TM_Job_Entity::STRING_BATCH:
|
||||
$result = $this->get_for_title( $job->get_title() );
|
||||
break;
|
||||
case WPML_TM_Job_Entity::PACKAGE_TYPE:
|
||||
$result = $this->get_for_package( $id );
|
||||
break;
|
||||
}
|
||||
|
||||
if ( empty( $result ) ) {
|
||||
$result = array(
|
||||
'name' => '',
|
||||
'url' => null,
|
||||
);
|
||||
do_action( 'wpml_tm_jobs_log', 'WPML_TM_Rest_Jobs_Element_Info::get', array( $id, $type ), 'Empty result' );
|
||||
}
|
||||
|
||||
$result['url'] = apply_filters( 'wpml_tm_job_list_element_url', $result['url'], $id, $type );
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_for_post( $id ) {
|
||||
$result = array();
|
||||
|
||||
$post = get_post( $id );
|
||||
if ( $post ) {
|
||||
$permalink = get_permalink( $post );
|
||||
|
||||
$result = array(
|
||||
'name' => $post->post_title,
|
||||
'url' => $permalink,
|
||||
);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_for_package( $id ) {
|
||||
$result = array();
|
||||
|
||||
$helper = $this->package_helper_factory->create();
|
||||
if ( ! $helper ) {
|
||||
return array(
|
||||
'name' => __( 'String package job', 'wpml-translation-management' ),
|
||||
'url' => null,
|
||||
);
|
||||
}
|
||||
|
||||
$package = $helper->get_translatable_item( null, $id );
|
||||
if ( $package ) {
|
||||
$result = array(
|
||||
'name' => $package->title,
|
||||
'url' => $package->edit_link,
|
||||
);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_for_title( $title ) {
|
||||
return [
|
||||
'name' => $title,
|
||||
'url' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Jobs_Language_Names {
|
||||
/** @var SitePress */
|
||||
private $sitepress;
|
||||
|
||||
/** @var array */
|
||||
private $active_languages;
|
||||
|
||||
/**
|
||||
* @param SitePress $sitepress
|
||||
*/
|
||||
public function __construct( SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get( $code ) {
|
||||
$languages = $this->get_active_languages();
|
||||
|
||||
return isset( $languages[ $code ] ) ? $languages[ $code ] : $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_active_languages() {
|
||||
if ( ! $this->active_languages ) {
|
||||
foreach ( $this->sitepress->get_active_languages() as $code => $data ) {
|
||||
$this->active_languages[ $code ] = $data['display_name'];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->active_languages;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Jobs_Package_Helper_Factory {
|
||||
/** @var WPML_Package_Helper */
|
||||
private $package_helper = false;
|
||||
|
||||
/**
|
||||
* @return null|WPML_Package_Helper
|
||||
*/
|
||||
public function create() {
|
||||
if ( false === $this->package_helper ) {
|
||||
if ( defined( 'WPML_ST_VERSION' ) && class_exists( 'WPML_Package_Helper' ) ) {
|
||||
$this->package_helper = new WPML_Package_Helper();
|
||||
} else {
|
||||
$this->package_helper = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->package_helper;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Jobs_Translation_Service {
|
||||
/** @var WPML_WP_Cache */
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* @param WPML_WP_Cache $cache
|
||||
*/
|
||||
public function __construct( WPML_WP_Cache $cache ) {
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|int $service_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name( $service_id ) {
|
||||
$name = '';
|
||||
if ( is_numeric( $service_id ) ) {
|
||||
$service = $this->get_translation_service( (int) $service_id );
|
||||
if ( $service ) {
|
||||
$name = $service->name;
|
||||
}
|
||||
} else {
|
||||
$name = __( 'Local', 'wpml-translation-management' );
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
private function get_translation_service( $service_id ) {
|
||||
$key = 'service_' . $service_id;
|
||||
$found = false;
|
||||
$service = $this->cache->get( $key, $found );
|
||||
|
||||
if ( ! $found ) {
|
||||
$current_service = TranslationProxy::get_current_service();
|
||||
if ( $current_service && $current_service->id === $service_id ) {
|
||||
$service = $current_service;
|
||||
} else {
|
||||
$service = TranslationProxy_Service::get_service( $service_id );
|
||||
}
|
||||
|
||||
$this->cache->set( $key, $service );
|
||||
}
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WPML_TM_Rest_Jobs_Translation_Service
|
||||
*/
|
||||
public static function create() {
|
||||
$cache = new WPML_WP_Cache( 'wpml-tm-services' );
|
||||
|
||||
return new WPML_TM_Rest_Jobs_Translation_Service( $cache );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Rest_Jobs_View_Model {
|
||||
/** @var WPML_TM_Rest_Jobs_Translation_Service */
|
||||
private $translation_service;
|
||||
|
||||
/** @var WPML_TM_Rest_Jobs_Element_Info */
|
||||
private $element_info;
|
||||
|
||||
/** @var WPML_TM_Rest_Jobs_Language_Names */
|
||||
private $language_names;
|
||||
|
||||
/** @var WPML_TM_Rest_Job_Translator_Name */
|
||||
private $translator_name;
|
||||
|
||||
/** @var WPML_TM_Rest_Job_Progress */
|
||||
private $progress;
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Rest_Jobs_Translation_Service $translation_service
|
||||
* @param WPML_TM_Rest_Jobs_Element_Info $element_info
|
||||
* @param SitePress $sitepress
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TM_Rest_Jobs_Translation_Service $translation_service,
|
||||
WPML_TM_Rest_Jobs_Element_Info $element_info,
|
||||
WPML_TM_Rest_Jobs_Language_Names $language_names,
|
||||
WPML_TM_Rest_Job_Translator_Name $translator_name,
|
||||
WPML_TM_Rest_Job_Progress $progress
|
||||
) {
|
||||
$this->translation_service = $translation_service;
|
||||
$this->element_info = $element_info;
|
||||
$this->language_names = $language_names;
|
||||
$this->translator_name = $translator_name;
|
||||
$this->progress = $progress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Jobs_Collection $jobs
|
||||
* @param int $total_jobs_count
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function build( WPML_TM_Jobs_Collection $jobs, $total_jobs_count ) {
|
||||
$result = array( 'jobs' => array() );
|
||||
|
||||
foreach ( $jobs as $job ) {
|
||||
$result['jobs'][] = $this->map_job( $job );
|
||||
}
|
||||
|
||||
$result['total'] = $total_jobs_count;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Job_Entity $job
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function map_job( WPML_TM_Job_Entity $job ) {
|
||||
$extra_data = array();
|
||||
if ( $job instanceof WPML_TM_Post_Job_Entity ) {
|
||||
$extra_data['icl_translate_job_id'] = $job->get_translate_job_id();
|
||||
}
|
||||
|
||||
return array(
|
||||
'id' => $job->get_id(),
|
||||
'type' => $job->get_type(),
|
||||
'tp_id' => $job->get_tp_id(),
|
||||
'status' => $job->get_status(),
|
||||
'needs_update' => $job->does_need_update(),
|
||||
'language_codes' => array(
|
||||
'source' => $job->get_source_language(),
|
||||
'target' => $job->get_target_language(),
|
||||
),
|
||||
'languages' => array(
|
||||
'source' => $this->language_names->get( $job->get_source_language() ),
|
||||
'target' => $this->language_names->get( $job->get_target_language() ),
|
||||
),
|
||||
'translation_service_id' => $job->get_translation_service(),
|
||||
'translation_service' => $this->translation_service->get_name( $job->get_translation_service() ),
|
||||
'sent_date' => $job->get_sent_date()->format( 'Y-m-d' ),
|
||||
'deadline' => $job->get_deadline() ? $job->get_deadline()->format( 'Y-m-d' ) : '',
|
||||
'ts_status' => (string) $job->get_ts_status(),
|
||||
'element' => $this->element_info->get( $job ),
|
||||
'translator_name' => $job->get_translator_id() ? $this->translator_name->get( $job->get_translator_id() ) : '',
|
||||
'progress' => $this->progress->get( $job ),
|
||||
'batch' => array(
|
||||
'id' => $job->get_batch()->get_id(),
|
||||
'name' => $job->get_batch()->get_name(),
|
||||
'tp_id' => $job->get_batch()->get_tp_id(),
|
||||
),
|
||||
'extra_data' => $extra_data,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user