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,102 @@
<?php
/**
* Class WPML_TF_TP_Ratings_Synchronize
*
* @author OnTheGoSystems
*/
class WPML_TF_TP_Ratings_Synchronize {
const MAX_RATINGS_TO_SYNCHRONIZE = 5;
const PENDING_SYNC_RATING_IDS_OPTION = 'wpml_tf_pending_sync_rating_ids';
const MAX_ATTEMPTS_TO_SYNC = 3;
/** @var WPML_TF_Data_Object_Storage $feedback_storage */
private $feedback_storage;
/** @var WPML_TP_API_TF_Ratings $tp_ratings */
private $tp_ratings;
/** @var array $pending_ids */
private $pending_ids;
/**
* WPML_TF_TP_Ratings_Synchronize constructor.
*
* @param WPML_TF_Data_Object_Storage $feedback_storage
* @param WPML_TP_API_TF_Ratings $tp_ratings
*/
public function __construct( WPML_TF_Data_Object_Storage $feedback_storage, WPML_TP_API_TF_Ratings $tp_ratings ) {
$this->feedback_storage = $feedback_storage;
$this->tp_ratings = $tp_ratings;
}
/** @param bool $clear_all_pending_ratings */
public function run( $clear_all_pending_ratings = false ) {
$this->pending_ids = get_option( self::PENDING_SYNC_RATING_IDS_OPTION, array() );
$filter_args = array(
'pending_tp_ratings' => $clear_all_pending_ratings ? -1 : self::MAX_RATINGS_TO_SYNCHRONIZE,
);
$feedback_filter = new WPML_TF_Feedback_Collection_Filter( $filter_args );
/** @var WPML_TF_Feedback_Collection $feedback_collection */
$feedback_collection = $this->feedback_storage->get_collection( $feedback_filter );
$time_threshold = 5 * MINUTE_IN_SECONDS;
foreach ( $feedback_collection as $feedback ) {
/** @var WPML_TF_Feedback $feedback */
$time_since_creation = time() - strtotime( $feedback->get_date_created() );
if ( ! $clear_all_pending_ratings && $time_since_creation < $time_threshold ) {
continue;
}
$tp_rating_id = $this->tp_ratings->send( $feedback );
if ( $tp_rating_id || $clear_all_pending_ratings ) {
$this->set_tp_rating_id( $feedback, $tp_rating_id );
} else {
$this->handle_pending_rating_sync( $feedback );
}
}
if ( $this->pending_ids ) {
update_option( self::PENDING_SYNC_RATING_IDS_OPTION, $this->pending_ids, false );
} else {
delete_option( self::PENDING_SYNC_RATING_IDS_OPTION );
}
}
private function set_tp_rating_id( WPML_TF_Feedback $feedback, $tp_rating_id ) {
$feedback->get_tp_responses()->set_rating_id( (int) $tp_rating_id );
$this->feedback_storage->persist( $feedback );
}
private function handle_pending_rating_sync( WPML_TF_Feedback $feedback ) {
$this->increment_attempts( $feedback->get_id() );
if ( $this->exceeds_max_attempts( $feedback->get_id() ) ) {
$this->set_tp_rating_id( $feedback, 0 );
unset( $this->pending_ids[ $feedback->get_id() ] );
}
}
/**
* @param int $id
*
* @return bool
*/
private function exceeds_max_attempts( $id ) {
return isset( $this->pending_ids[ $id ] ) && $this->pending_ids[ $id ] >= self::MAX_ATTEMPTS_TO_SYNC;
}
/** @param int $id */
private function increment_attempts( $id ) {
if ( ! isset( $this->pending_ids[ $id ] ) ) {
$this->pending_ids[ $id ] = 1;
} else {
$this->pending_ids[ $id ]++;
}
}
}

View File

@@ -0,0 +1,72 @@
<?php
/**
* Class WPML_TF_WP_Cron_Events
*
* @author OnTheGoSystems
*/
class WPML_TF_WP_Cron_Events implements IWPML_Action {
const SYNCHRONIZE_RATINGS_EVENT = 'wpml_tf_synchronize_ratings_event';
/** @var WPML_TF_Settings_Read $settings_read */
private $settings_read;
/** @var WPML_TF_Settings $settings */
private $settings;
/** @var WPML_TF_TP_Ratings_Synchronize_Factory $ratings_synchronize_factory */
private $ratings_synchronize_factory;
/**
* WPML_TF_WP_Cron_Events constructor.
*
* @param WPML_TF_Settings_Read $settings_read
* @param WPML_TF_TP_Ratings_Synchronize_Factory $ratings_synchronize_factory
*/
public function __construct(
WPML_TF_Settings_Read $settings_read,
WPML_TF_TP_Ratings_Synchronize_Factory $ratings_synchronize_factory
) {
$this->settings_read = $settings_read;
$this->ratings_synchronize_factory = $ratings_synchronize_factory;
}
public function add_hooks() {
add_action( 'init', array( $this, 'init_action' ) );
add_action( self::SYNCHRONIZE_RATINGS_EVENT, array( $this, 'synchronize_ratings' ) );
}
public function init_action() {
if ( $this->get_settings()->is_enabled() ) {
$this->add_synchronize_ratings_event();
} else {
$this->remove_synchronize_ratings_event();
}
}
private function add_synchronize_ratings_event() {
if ( ! wp_next_scheduled( self::SYNCHRONIZE_RATINGS_EVENT ) ) {
wp_schedule_event( time(), 'twicedaily', self::SYNCHRONIZE_RATINGS_EVENT );
}
}
private function remove_synchronize_ratings_event() {
$timestamp = wp_next_scheduled( self::SYNCHRONIZE_RATINGS_EVENT );
wp_unschedule_event( $timestamp, self::SYNCHRONIZE_RATINGS_EVENT );
}
public function synchronize_ratings() {
$ratings_synchronize = $this->ratings_synchronize_factory->create();
$ratings_synchronize->run();
}
/** @return WPML_TF_Settings */
private function get_settings() {
if ( ! $this->settings ) {
$this->settings = $this->settings_read->get( 'WPML_TF_Settings' );
}
return $this->settings;
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* Class WPML_TF_Translation_Queue_Hooks_Factory
*
* @author OnTheGoSystems
*/
class WPML_TF_Translation_Queue_Hooks_Factory extends WPML_Current_Screen_Loader_Factory {
/** @return string */
protected function get_screen_regex() {
return '/translations-queue/';
}
/** @return WPML_TF_Translation_Queue_Hooks */
protected function create_hooks() {
$feedback_storage = new WPML_TF_Data_Object_Storage( new WPML_TF_Feedback_Post_Convert() );
return new WPML_TF_Translation_Queue_Hooks( $feedback_storage );
}
}

View File

@@ -0,0 +1,18 @@
<?php
/**
* Class WPML_TF_Translation_Service_Change_Hooks_Factory
*
* @author OnTheGoSystems
*/
class WPML_TF_Translation_Service_Change_Hooks_Factory implements IWPML_Backend_Action_Loader {
/** @return WPML_TF_Translation_Service_Change_Hooks */
public function create() {
return new WPML_TF_Translation_Service_Change_Hooks(
new WPML_TF_Settings_Read(),
new WPML_TF_Settings_Write(),
new WPML_TF_TP_Ratings_Synchronize_Factory()
);
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Class WPML_TF_Common_Hooks_Factory
*
* @author OnTheGoSystems
*/
class WPML_TF_WP_Cron_Events_Factory implements IWPML_Backend_Action_Loader, IWPML_Frontend_Action_Loader {
/** @return WPML_TF_WP_Cron_Events */
public function create() {
return new WPML_TF_WP_Cron_Events(
new WPML_TF_Settings_Read(),
new WPML_TF_TP_Ratings_Synchronize_Factory()
);
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* Class WPML_TF_XML_RPC_Hooks_Factory
*
* @author OnTheGoSystems
*/
class WPML_TF_XML_RPC_Hooks_Factory implements IWPML_Frontend_Action_Loader {
/** @return WPML_TF_XML_RPC_Hooks */
public function create() {
return new WPML_TF_XML_RPC_Hooks(
new WPML_TF_XML_RPC_Feedback_Update_Factory(),
new WPML_WP_API()
);
}
}

View File

@@ -0,0 +1,14 @@
<?php
/**
* Class WPML_TM_TF_AJAX_Feedback_List_Hooks_Factory
*
* @author OnTheGoSystems
*/
class WPML_TM_TF_AJAX_Feedback_List_Hooks_Factory implements IWPML_AJAX_Action_Loader {
/** @return WPML_TM_TF_Feedback_List_Hooks */
public function create() {
return new WPML_TM_TF_Feedback_List_Hooks();
}
}

View File

@@ -0,0 +1,19 @@
<?php
/**
* Class WPML_TM_TF_Feedback_List_Hooks_Factory
*
* @author OnTheGoSystems
*/
class WPML_TM_TF_Feedback_List_Hooks_Factory extends WPML_Current_Screen_Loader_Factory {
/** @return string */
protected function get_screen_regex() {
return '/wpml-translation-feedback-list/';
}
/** @return WPML_TM_TF_Feedback_List_Hooks */
protected function create_hooks() {
return new WPML_TM_TF_Feedback_List_Hooks();
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Class WPML_TF_TP_Ratings_Synchronize
*
* @author OnTheGoSystems
*/
class WPML_TF_TP_Ratings_Synchronize_Factory {
/**
* @return WPML_TF_TP_Ratings_Synchronize
*/
public function create() {
$tp_client_factory = new WPML_TP_Client_Factory();
$tp_client = $tp_client_factory->create();
return new WPML_TF_TP_Ratings_Synchronize(
new WPML_TF_Data_Object_Storage( new WPML_TF_Feedback_Post_Convert() ),
$tp_client->ratings()
);
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* Class WPML_TF_XML_RPC_Feedback_Update_Factory
*
* @author OnTheGoSystems
*/
class WPML_TF_XML_RPC_Feedback_Update_Factory {
/** @return WPML_TF_XML_RPC_Feedback_Update */
public function create() {
global $sitepress;
$translation_service = $sitepress->get_setting( 'translation_service' );
$translation_projects = $sitepress->get_setting( 'icl_translation_projects' );
$tp_project = new WPML_TP_Project( $translation_service, $translation_projects );
$feedback_storage = new WPML_TF_Data_Object_Storage( new WPML_TF_Feedback_Post_Convert() );
return new WPML_TF_XML_RPC_Feedback_Update( $feedback_storage, $tp_project );
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Class WPML_TF_Translation_Queue_Hooks
*
* @author OnTheGoSystems
*/
class WPML_TF_Translation_Queue_Hooks implements IWPML_Action {
/** @var WPML_TF_Data_Object_Storage $feedback_storage */
private $feedback_storage;
public function __construct( WPML_TF_Data_Object_Storage $feedback_storage ) {
$this->feedback_storage = $feedback_storage;
}
public function add_hooks() {
add_filter( 'wpml_tm_allowed_translators_for_job', array( $this, 'add_reviewer_to_allowed_translators' ), 10, 2 );
}
public function add_reviewer_to_allowed_translators(
array $allowed_translators,
WPML_Translation_Job $translation_job
) {
$current_user_id = get_current_user_id();
if ( $translation_job->get_translator_id() !== $current_user_id ) {
$filter_args = array(
'reviewer_id' => $current_user_id,
);
$collection_filter = new WPML_TF_Feedback_Collection_Filter( $filter_args );
$feedback_collection = $this->feedback_storage->get_collection( $collection_filter );
foreach ( $feedback_collection as $feedback ) {
/** @var WPML_TF_Feedback $feedback */
if ( $feedback->get_job_id() === (int) $translation_job->get_id()
&& 'fixed' !== $feedback->get_status()
) {
$allowed_translators[] = $current_user_id;
break;
}
}
}
return $allowed_translators;
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* Class WPML_TF_Translation_Service_Change_Hooks
*
* @author OnTheGoSystems
*/
class WPML_TF_Translation_Service_Change_Hooks implements IWPML_Action {
/** @var WPML_TF_Settings_Read $settings_read */
private $settings_read;
/** @var WPML_TF_Settings_Write $settings_write */
private $settings_write;
/** @var WPML_TF_TP_Ratings_Synchronize_Factory $tp_ratings_synchronize_factory */
private $tp_ratings_synchronize_factory;
public function __construct(
WPML_TF_Settings_Read $settings_read,
WPML_TF_Settings_Write $settings_write,
WPML_TF_TP_Ratings_Synchronize_Factory $tp_ratings_synchronize_factory
) {
$this->settings_read = $settings_read;
$this->settings_write = $settings_write;
$this->tp_ratings_synchronize_factory = $tp_ratings_synchronize_factory;
}
public function add_hooks() {
add_action(
'wpml_tm_before_set_translation_service',
array( $this, 'before_set_translation_service_callback' )
);
}
public function before_set_translation_service_callback( stdClass $service ) {
$this->cleanup_pending_ratings_queue();
$this->disable_tf_if_not_allowed_by_ts( $service );
}
private function cleanup_pending_ratings_queue() {
$tp_ratings_sync = $this->tp_ratings_synchronize_factory->create();
$tp_ratings_sync->run( true );
}
private function disable_tf_if_not_allowed_by_ts( stdClass $service ) {
if ( isset( $service->translation_feedback ) && ! $service->translation_feedback ) {
/** @var WPML_TF_Settings $settings */
$settings = $this->settings_read->get( 'WPML_TF_Settings' );
$settings->set_enabled( false );
$this->settings_write->save( $settings );
}
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Class WPML_TF_XML_RPC_Hooks
*
* @author OnTheGoSystems
*/
class WPML_TF_XML_RPC_Hooks implements IWPML_Action {
/** @var WPML_TF_XML_RPC_Feedback_Update_Factory $xml_rpc_feedback_update_factory */
private $xml_rpc_feedback_update_factory;
/** @var WPML_WP_API $wp_api */
private $wp_api;
public function __construct(
WPML_TF_XML_RPC_Feedback_Update_Factory $xml_rpc_feedback_update_factory,
WPML_WP_API $wp_api
) {
$this->xml_rpc_feedback_update_factory = $xml_rpc_feedback_update_factory;
$this->wp_api = $wp_api;
}
public function add_hooks() {
if ( $this->wp_api->constant( 'XMLRPC_REQUEST' ) ) {
add_filter( 'xmlrpc_methods', array( $this, 'add_tf_xmlrpc_methods' ) );
}
}
/**
* @param array $methods
*
* @return array
*/
public function add_tf_xmlrpc_methods( $methods ) {
$methods['translationproxy.update_feedback_status'] = array( $this, 'update_feedback_status' );
return $methods;
}
/** @param array $args */
public function update_feedback_status( array $args ) {
$xml_rpc_feedback_update = $this->xml_rpc_feedback_update_factory->create();
$xml_rpc_feedback_update->set_status( $args );
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* Class WPML_TM_TF_Feedback_List_Hooks
*
* @author OnTheGoSystems
*/
class WPML_TM_TF_Feedback_List_Hooks implements IWPML_Action {
public function add_hooks() {
add_filter( 'wpml_use_tm_editor', array( $this, 'maybe_force_to_use_translation_editor' ) );
}
/**
* @param int|bool $use_translation_editor
*
* @return int|bool
*/
public function maybe_force_to_use_translation_editor( $use_translation_editor ) {
if ( ! current_user_can( 'edit_others_posts' ) ) {
$use_translation_editor = true;
}
return $use_translation_editor;
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* Class WPML_TM_TF_Module
*
* @author OnTheGoSystems
*/
class WPML_TM_TF_Module {
/** @var WPML_Action_Filter_Loader $action_filter_loader */
private $action_filter_loader;
/** @var WPML_TF_Settings $settings */
private $settings;
/**
* WPML_TF_Module constructor.
*
* @param WPML_Action_Filter_Loader $action_filter_loader
* @param WPML_TF_Settings $settings
*/
public function __construct( WPML_Action_Filter_Loader $action_filter_loader, WPML_TF_Settings $settings ) {
$this->action_filter_loader = $action_filter_loader;
$this->settings = $settings;
}
public function run() {
$this->action_filter_loader->load( $this->get_actions_to_load_always() );
if ( $this->settings->is_enabled() ) {
$this->action_filter_loader->load( $this->get_actions_to_load_when_module_enabled() );
}
}
/**
* @return array
*/
private function get_actions_to_load_always() {
return array(
'WPML_TF_WP_Cron_Events_Factory',
);
}
/**
* @return array
*/
private function get_actions_to_load_when_module_enabled() {
return array(
'WPML_TF_XML_RPC_Hooks_Factory',
'WPML_TF_Translation_Service_Change_Hooks_Factory',
'WPML_TF_Translation_Queue_Hooks_Factory',
'WPML_TM_TF_Feedback_List_Hooks_Factory',
'WPML_TM_TF_AJAX_Feedback_List_Hooks_Factory',
);
}
}

View File

@@ -0,0 +1,72 @@
<?php
/**
* Class WPML_TF_XML_RPC_Feedback_Update
*
* @author OnTheGoSystems
*/
class WPML_TF_XML_RPC_Feedback_Update {
/** @var WPML_TF_Data_Object_Storage $feedback_storage */
private $feedback_storage;
/** @var WPML_TP_Project $tp_project */
private $tp_project;
public function __construct( WPML_TF_Data_Object_Storage $feedback_storage, WPML_TP_Project $tp_project ) {
$this->feedback_storage = $feedback_storage;
$this->tp_project = $tp_project;
}
public function set_status( array $args ) {
if ( $this->valid_arguments( $args ) ) {
$feedback = $this->get_feedback( $args['feedback']['id'] );
if ( $feedback ) {
/** @var WPML_TF_Feedback $feedback */
$feedback->set_status( $args['feedback']['status'] );
$this->feedback_storage->persist( $feedback );
}
}
}
/**
* @param array $args
*
* @return bool
*/
private function valid_arguments( array $args ) {
$valid = false;
if ( isset( $args['feedback']['id'], $args['feedback']['status'], $args['authorization_hash'] ) ) {
$expected_hash = sha1( $this->tp_project->get_id() . $this->tp_project->get_access_key() . $args['feedback']['id'] );
if ( $expected_hash === $args['authorization_hash'] ) {
$valid = true;
}
}
return $valid;
}
/**
* @param int $tp_feedback_id
*
* @return null|WPML_TF_Feedback
*/
private function get_feedback( $tp_feedback_id ) {
$feedback = null;
$filter_args = array(
'tp_feedback_id' => $tp_feedback_id,
);
$collection_filter = new WPML_TF_Feedback_Collection_Filter( $filter_args );
$collection = $this->feedback_storage->get_collection( $collection_filter );
if ( $collection->count() ) {
$feedback = $collection->current();
}
return $feedback;
}
}