first commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Jobs_Deadline_Cron_Hooks_Factory implements IWPML_Backend_Action_Loader, IWPML_Frontend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
global $iclTranslationManagement;
|
||||
|
||||
return new WPML_TM_Jobs_Deadline_Cron_Hooks( new WPML_TM_Overdue_Jobs_Report_Factory(), $iclTranslationManagement );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Jobs_Deadline_Cron_Hooks implements IWPML_Action {
|
||||
|
||||
const CHECK_OVERDUE_JOBS_EVENT = 'wpml-tm-check-overdue-jobs-event';
|
||||
|
||||
/** @var WPML_TM_Overdue_Jobs_Report_Factory $overdue_jobs_report_factory */
|
||||
private $overdue_jobs_report_factory;
|
||||
|
||||
/** @var TranslationManagement $notification_settings */
|
||||
private $translation_management;
|
||||
|
||||
public function __construct(
|
||||
WPML_TM_Overdue_Jobs_Report_Factory $overdue_jobs_report_factory,
|
||||
TranslationManagement $translation_management
|
||||
) {
|
||||
$this->overdue_jobs_report_factory = $overdue_jobs_report_factory;
|
||||
$this->translation_management = $translation_management;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
if ( is_admin() ) {
|
||||
add_action( 'init', array( $this, 'schedule_event' ), $this->get_init_priority() );
|
||||
}
|
||||
|
||||
add_action( self::CHECK_OVERDUE_JOBS_EVENT, array( $this, 'send_overdue_email_report' ) );
|
||||
}
|
||||
|
||||
public function schedule_event() {
|
||||
if ( $this->is_notification_enabled() ) {
|
||||
if ( ! wp_next_scheduled( self::CHECK_OVERDUE_JOBS_EVENT ) ) {
|
||||
wp_schedule_event( time(), 'daily', self::CHECK_OVERDUE_JOBS_EVENT );
|
||||
}
|
||||
} else {
|
||||
$timestamp = wp_next_scheduled( self::CHECK_OVERDUE_JOBS_EVENT );
|
||||
wp_unschedule_event( $timestamp, self::CHECK_OVERDUE_JOBS_EVENT );
|
||||
}
|
||||
}
|
||||
|
||||
/** @return int */
|
||||
private function get_init_priority() {
|
||||
return $this->translation_management->get_init_priority() + 1;
|
||||
}
|
||||
|
||||
/** @return bool */
|
||||
private function is_notification_enabled() {
|
||||
$settings = $this->translation_management->get_settings();
|
||||
return ICL_TM_NOTIFICATION_NONE !== (int) $settings['notification']['overdue'];
|
||||
}
|
||||
|
||||
public function send_overdue_email_report() {
|
||||
$overdue_jobs_report = $this->overdue_jobs_report_factory->create();
|
||||
$overdue_jobs_report->send();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Jobs_Deadline_Estimate_AJAX_Action_Factory extends WPML_AJAX_Base_Factory {
|
||||
|
||||
/** @return null|WPML_TM_Jobs_Deadline_Estimate_AJAX_Action */
|
||||
public function create() {
|
||||
$hooks = null;
|
||||
|
||||
if ( $this->is_valid_action( 'wpml-tm-jobs-deadline-estimate-ajax-action' ) ) {
|
||||
$deadline_estimate_factory = new WPML_TM_Jobs_Deadline_Estimate_Factory();
|
||||
|
||||
$hooks = new WPML_TM_Jobs_Deadline_Estimate_AJAX_Action(
|
||||
$deadline_estimate_factory->create(),
|
||||
TranslationProxy_Basket::get_basket(),
|
||||
$_POST
|
||||
);
|
||||
}
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Jobs_Deadline_Estimate_AJAX_Action implements IWPML_Action {
|
||||
|
||||
/** @var WPML_TM_Jobs_Deadline_Estimate $deadline_estimate */
|
||||
private $deadline_estimate;
|
||||
|
||||
/** @var array $translation_basket */
|
||||
private $translation_basket;
|
||||
|
||||
/** @var array $post_data */
|
||||
private $post_data;
|
||||
|
||||
public function __construct(
|
||||
WPML_TM_Jobs_Deadline_Estimate $deadline_estimate,
|
||||
array $translation_basket,
|
||||
array $post_data
|
||||
) {
|
||||
$this->deadline_estimate = $deadline_estimate;
|
||||
$this->translation_basket = $translation_basket;
|
||||
$this->post_data = $post_data;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action(
|
||||
'wp_ajax_' . 'wpml-tm-jobs-deadline-estimate-ajax-action',
|
||||
array( $this, 'refresh' )
|
||||
);
|
||||
}
|
||||
|
||||
public function refresh() {
|
||||
if ( isset( $this->post_data['translators'] ) ) {
|
||||
$deadline_per_lang = array();
|
||||
|
||||
foreach ( $this->post_data['translators'] as $lang_to => $translator_data ) {
|
||||
list( $translator_id, $service ) = $this->parse_translator_data( $translator_data );
|
||||
|
||||
$translator_args = array(
|
||||
'translator_id' => $translator_id,
|
||||
'service' => $service,
|
||||
'to' => $lang_to,
|
||||
);
|
||||
|
||||
$deadline_per_lang[ $lang_to ] = $this->deadline_estimate->get( $this->translation_basket, $translator_args );
|
||||
}
|
||||
|
||||
$response_data = array(
|
||||
'deadline' => max( $deadline_per_lang ),
|
||||
);
|
||||
|
||||
wp_send_json_success( $response_data );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The translator data for a remote service will be like "ts-7" with 7 the ID of the remote service
|
||||
* For a local translator, the translation data will be the ID
|
||||
*
|
||||
* @param string $translator_data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function parse_translator_data( $translator_data ) {
|
||||
$translator_id = $translator_data;
|
||||
$service = 'local';
|
||||
|
||||
if ( false !== strpos( $translator_data, 'ts-' ) ) {
|
||||
$translator_id = 0;
|
||||
$service = (int) preg_replace( '/^ts-/', '', $translator_data );
|
||||
}
|
||||
|
||||
return array( $translator_id, $service );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Jobs_Deadline_Estimate_Factory {
|
||||
|
||||
public function create() {
|
||||
$word_count_records_factory = new WPML_TM_Word_Count_Records_Factory();
|
||||
$word_count_records = $word_count_records_factory->create();
|
||||
$single_process_factory = new WPML_TM_Word_Count_Single_Process_Factory();
|
||||
$single_process = $single_process_factory->create();
|
||||
|
||||
$st_package_factory = class_exists( 'WPML_ST_Package_Factory' ) ? new WPML_ST_Package_Factory() : null;
|
||||
|
||||
$translatable_element_provider = new WPML_TM_Translatable_Element_Provider( $word_count_records, $single_process, $st_package_factory );
|
||||
|
||||
return new WPML_TM_Jobs_Deadline_Estimate( $translatable_element_provider, wpml_tm_get_jobs_repository() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Jobs_Deadline_Estimate {
|
||||
|
||||
const LATENCY_DAYS = 1;
|
||||
const WORDS_PER_DAY = 1200;
|
||||
|
||||
/** @var WPML_TM_Translatable_Element_Provider */
|
||||
private $translatable_element_provider;
|
||||
|
||||
/** @var WPML_TM_Jobs_Repository */
|
||||
private $jobs_repository;
|
||||
|
||||
public function __construct(
|
||||
WPML_TM_Translatable_Element_Provider $translatable_element_provider,
|
||||
WPML_TM_Jobs_Repository $jobs_repository
|
||||
) {
|
||||
$this->translatable_element_provider = $translatable_element_provider;
|
||||
$this->jobs_repository = $jobs_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $basket
|
||||
* @param array $translator_options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get( array $basket, array $translator_options ) {
|
||||
$pending_jobs = $this->get_pending_jobs_for_translator( $translator_options );
|
||||
|
||||
$words_per_langs = $this->get_pending_words_per_langs( $pending_jobs );
|
||||
$words_per_langs = $this->add_basket_words_per_langs( $basket, $words_per_langs );
|
||||
|
||||
$max_words_in_lang = 0;
|
||||
|
||||
if ( $words_per_langs ) {
|
||||
$max_words_in_lang = max( $words_per_langs );
|
||||
}
|
||||
|
||||
$estimated_days = ceil( $max_words_in_lang / self::WORDS_PER_DAY );
|
||||
$estimated_days += self::LATENCY_DAYS;
|
||||
return date( 'Y-m-d', strtotime( '+' . $estimated_days . ' day' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $translator_options
|
||||
*
|
||||
* @return WPML_TM_Jobs_Collection
|
||||
*/
|
||||
private function get_pending_jobs_for_translator( array $translator_options ) {
|
||||
$translator_id = $translator_options['translator_id'];
|
||||
$params = new WPML_TM_Jobs_Search_Params();
|
||||
$params->set_status( array( ICL_TM_IN_PROGRESS, ICL_TM_WAITING_FOR_TRANSLATOR ) );
|
||||
|
||||
if ( false !== strpos( $translator_id, 'ts-' ) ) {
|
||||
$params->set_scope( WPML_TM_Jobs_Search_Params::SCOPE_REMOTE );
|
||||
} else {
|
||||
$params->set_scope( WPML_TM_Jobs_Search_Params::SCOPE_LOCAL );
|
||||
$params->set_translated_by( $translator_id );
|
||||
}
|
||||
|
||||
if ( array_key_exists( 'to', $translator_options ) ) {
|
||||
$params->set_target_language( $translator_options['to'] );
|
||||
}
|
||||
|
||||
return $this->jobs_repository->get( $params );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Jobs_Collection $pending_jobs
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function get_pending_words_per_langs( WPML_TM_Jobs_Collection $pending_jobs ) {
|
||||
$words_per_langs = array();
|
||||
|
||||
foreach ( $pending_jobs as $pending_job ) {
|
||||
if ( ! isset( $words_per_langs[ $pending_job->get_target_language() ] ) ) {
|
||||
$words_per_langs[ $pending_job->get_target_language() ] = 0;
|
||||
}
|
||||
|
||||
$translatable_element = $this->translatable_element_provider->get_from_job( $pending_job );
|
||||
|
||||
if ( $translatable_element ) {
|
||||
$words_per_langs[ $pending_job->get_target_language() ] += $translatable_element->get_words_count();
|
||||
}
|
||||
}
|
||||
|
||||
return $words_per_langs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $basket
|
||||
* @param int[] $words_per_langs
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function add_basket_words_per_langs( array $basket, array $words_per_langs ) {
|
||||
$element_types = array( 'post', 'string', 'package' );
|
||||
|
||||
foreach ( $element_types as $element_type ) {
|
||||
if ( isset( $basket[ $element_type ] ) ) {
|
||||
foreach ( $basket[ $element_type ] as $element_id => $data ) {
|
||||
foreach ( $data['to_langs'] as $to_lang => $v ) {
|
||||
if ( ! isset( $words_per_langs[ $to_lang ] ) ) {
|
||||
$words_per_langs[ $to_lang ] = 0;
|
||||
}
|
||||
|
||||
$translatable_element = $this->translatable_element_provider->get_from_type( $element_type, $element_id );
|
||||
$words_per_langs[ $to_lang ] += $translatable_element->get_words_count();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $words_per_langs;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user