first commit
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Backend_AJAX_Feedback_Edit_Hooks
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Backend_AJAX_Feedback_Edit_Hooks implements IWPML_Action {
|
||||
|
||||
/** @var WPML_TF_Feedback_Edit $feedback_edit */
|
||||
private $feedback_edit;
|
||||
|
||||
/** @var WPML_TF_Backend_Feedback_Row_View $row_view */
|
||||
private $row_view;
|
||||
|
||||
/** @var array $post_data */
|
||||
private $post_data;
|
||||
|
||||
/**
|
||||
* WPML_TF_Backend_AJAX_Feedback_Edit_Hooks constructor.
|
||||
*
|
||||
* @param WPML_TF_Feedback_Edit $feedback_edit
|
||||
* @param WPML_TF_Backend_Feedback_Row_View $row_view
|
||||
* @param array $post_data
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TF_Feedback_Edit $feedback_edit,
|
||||
WPML_TF_Backend_Feedback_Row_View $row_view,
|
||||
array $post_data
|
||||
) {
|
||||
$this->feedback_edit = $feedback_edit;
|
||||
$this->row_view = $row_view;
|
||||
$this->post_data = $post_data;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action(
|
||||
'wp_ajax_' . WPML_TF_Backend_AJAX_Feedback_Edit_Hooks_Factory::AJAX_ACTION,
|
||||
array( $this, 'edit_feedback_callback' )
|
||||
);
|
||||
}
|
||||
|
||||
public function edit_feedback_callback() {
|
||||
try {
|
||||
$this->check_post_data_key( 'feedback_id' );
|
||||
$feedback_id = filter_var( $this->post_data['feedback_id'], FILTER_SANITIZE_NUMBER_INT );
|
||||
$feedback = $this->feedback_edit->update( $feedback_id, $this->post_data );
|
||||
|
||||
if ( ! $feedback ) {
|
||||
throw new WPML_TF_AJAX_Exception( esc_html__( 'Failed to update the feedback.', 'sitepress' ) );
|
||||
}
|
||||
|
||||
$response = array(
|
||||
'summary_row' => $this->row_view->render_summary_row( $feedback ),
|
||||
'details_row' => $this->row_view->render_details_row( $feedback ),
|
||||
);
|
||||
|
||||
wp_send_json_success( $response );
|
||||
|
||||
} catch ( WPML_TF_AJAX_Exception $e ) {
|
||||
wp_send_json_error( $e->getMessage() );
|
||||
} catch ( WPML_TF_Feedback_Update_Exception $e ) {
|
||||
wp_send_json_error( $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @throws WPML_TF_AJAX_Exception
|
||||
*/
|
||||
private function check_post_data_key( $key ) {
|
||||
if ( ! isset( $this->post_data[ $key ] ) ) {
|
||||
$message = sprintf(
|
||||
esc_html__( 'Missing key "%s".', 'sitepress' ),
|
||||
$key
|
||||
);
|
||||
|
||||
throw new WPML_TF_AJAX_Exception( $message );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Backend_Hooks
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Backend_Hooks implements IWPML_Action {
|
||||
|
||||
const PAGE_HOOK = 'wpml-translation-feedback-list';
|
||||
|
||||
/** @var WPML_TF_Backend_Bulk_Actions_Factory */
|
||||
private $bulk_actions_factory;
|
||||
|
||||
/** @var WPML_TF_Backend_Feedback_List_View_Factory feedback_list_view_factory */
|
||||
private $feedback_list_view_factory;
|
||||
|
||||
/** @var WPML_TF_Backend_Styles $backend_styles */
|
||||
private $backend_styles;
|
||||
|
||||
/** @var WPML_TF_Backend_Scripts */
|
||||
private $backend_scripts;
|
||||
|
||||
/** @var wpdb $wpdb */
|
||||
private $wpdb;
|
||||
|
||||
/**
|
||||
* WPML_TF_Backend_Hooks constructor.
|
||||
*
|
||||
* @param WPML_TF_Backend_Bulk_Actions_Factory $bulk_actions_factory
|
||||
* @param WPML_TF_Backend_Feedback_List_View_Factory $feedback_list_view_factory
|
||||
* @param WPML_TF_Backend_Styles $backend_styles
|
||||
* @param WPML_TF_Backend_Scripts $backend_scripts
|
||||
* @param wpdb $wpdb
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TF_Backend_Bulk_Actions_Factory $bulk_actions_factory,
|
||||
WPML_TF_Backend_Feedback_List_View_Factory $feedback_list_view_factory,
|
||||
WPML_TF_Backend_Styles $backend_styles,
|
||||
WPML_TF_Backend_Scripts $backend_scripts,
|
||||
wpdb $wpdb
|
||||
) {
|
||||
$this->bulk_actions_factory = $bulk_actions_factory;
|
||||
$this->feedback_list_view_factory = $feedback_list_view_factory;
|
||||
$this->backend_styles = $backend_styles;
|
||||
$this->backend_scripts = $backend_scripts;
|
||||
$this->wpdb = $wpdb;
|
||||
}
|
||||
|
||||
/**
|
||||
* method add_hooks
|
||||
*/
|
||||
public function add_hooks() {
|
||||
add_action( 'wpml_admin_menu_configure', array( $this, 'add_translation_feedback_list_menu' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts_action' ) );
|
||||
add_action( 'current_screen', array( $this, 'bulk_actions_callback' ) );
|
||||
add_filter( 'posts_where', array( $this, 'maybe_exclude_rating_only_status' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Define translation feedback list menu and callback
|
||||
*
|
||||
* @param string $menu_id
|
||||
*/
|
||||
public function add_translation_feedback_list_menu( $menu_id ) {
|
||||
if ( 'WPML' !== $menu_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( current_user_can( 'manage_options' ) ) {
|
||||
$menu = array();
|
||||
$menu['order'] = 1200;
|
||||
$menu['page_title'] = __( 'Translation Feedback', 'sitepress' );
|
||||
$menu['menu_title'] = __( 'Translation Feedback', 'sitepress' );
|
||||
$menu['capability'] = 'manage_options';
|
||||
$menu['menu_slug'] = self::PAGE_HOOK;
|
||||
$menu['function'] = array( $this, 'translation_feedback_list_display' );
|
||||
|
||||
do_action( 'wpml_admin_menu_register_item', $menu );
|
||||
|
||||
} else {
|
||||
add_menu_page(
|
||||
__( 'Translation Feedback', 'sitepress' ),
|
||||
__( 'Translation Feedback', 'sitepress' ),
|
||||
'read',
|
||||
self::PAGE_HOOK,
|
||||
array( $this, 'translation_feedback_list_display' )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback to display the feedback list page
|
||||
*/
|
||||
public function translation_feedback_list_display() {
|
||||
$view = $this->feedback_list_view_factory->create();
|
||||
echo $view->render_page();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $hook
|
||||
*/
|
||||
public function admin_enqueue_scripts_action( $hook ) {
|
||||
if ( $this->is_page_hook( $hook ) ) {
|
||||
$this->backend_styles->enqueue();
|
||||
$this->backend_scripts->enqueue();
|
||||
}
|
||||
}
|
||||
|
||||
public function bulk_actions_callback( $current_screen ) {
|
||||
$hook = isset( $current_screen->id ) ? $current_screen->id : null;
|
||||
|
||||
if ( $this->is_page_hook( $hook ) ) {
|
||||
$bulk_actions = $this->bulk_actions_factory->create();
|
||||
$bulk_actions->process();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $hook
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_page_hook( $hook ) {
|
||||
return strpos( $hook, self::PAGE_HOOK ) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $where
|
||||
* @param WP_Query $query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function maybe_exclude_rating_only_status( $where, $query ) {
|
||||
if ( isset( $query->query_vars['exclude_tf_rating_only'] ) && $query->query_vars['exclude_tf_rating_only'] ) {
|
||||
$where .= " AND {$this->wpdb->posts}.post_status <> 'rating_only'";
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Backend_Options_AJAX_Hooks
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Backend_Options_AJAX_Hooks implements IWPML_Action {
|
||||
|
||||
/** @var WPML_TF_Settings $settings */
|
||||
private $settings;
|
||||
|
||||
/** @var WPML_TF_Settings_Write $settings_write */
|
||||
private $settings_write;
|
||||
|
||||
/** @var WPML_TF_Promote_Notices $promote_notices */
|
||||
private $promote_notices;
|
||||
|
||||
/** @var array $post_data */
|
||||
private $post_data;
|
||||
|
||||
/**
|
||||
* WPML_TF_Frontend_AJAX_Hooks constructor.
|
||||
*
|
||||
* @param WPML_TF_Settings $settings
|
||||
* @param WPML_TF_Settings_Write $settings_write
|
||||
* @param WPML_TF_Promote_Notices $promote_notices
|
||||
* @param array $post_data
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TF_Settings $settings,
|
||||
WPML_TF_Settings_Write $settings_write,
|
||||
WPML_TF_Promote_Notices $promote_notices,
|
||||
array $post_data
|
||||
) {
|
||||
$this->settings = $settings;
|
||||
$this->settings_write = $settings_write;
|
||||
$this->promote_notices = $promote_notices;
|
||||
$this->post_data = $post_data;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'wp_ajax_' . WPML_TF_Backend_Options_AJAX_Hooks_Factory::AJAX_ACTION, array( $this, 'save_settings_callback' ) );
|
||||
}
|
||||
|
||||
public function save_settings_callback() {
|
||||
$new_settings = array();
|
||||
|
||||
if ( isset( $this->post_data['settings'] ) ) {
|
||||
parse_str( $this->post_data['settings'], $new_settings );
|
||||
}
|
||||
|
||||
if ( isset( $new_settings['enabled'] ) ) {
|
||||
$this->settings->set_enabled( true );
|
||||
$this->promote_notices->remove();
|
||||
} else {
|
||||
$this->settings->set_enabled( false );
|
||||
}
|
||||
|
||||
if ( isset( $new_settings['button_mode'] ) ) {
|
||||
$this->settings->set_button_mode( $new_settings['button_mode'] );
|
||||
}
|
||||
|
||||
if ( isset( $new_settings['icon_style'] ) ) {
|
||||
$this->settings->set_icon_style( $new_settings['icon_style'] );
|
||||
}
|
||||
|
||||
if ( isset( $new_settings['languages_to'] ) ) {
|
||||
$this->settings->set_languages_to( $new_settings['languages_to'] );
|
||||
} else {
|
||||
$this->settings->set_languages_to( array() );
|
||||
}
|
||||
|
||||
if ( isset( $new_settings['display_mode'] ) ) {
|
||||
$this->settings->set_display_mode( $new_settings['display_mode'] );
|
||||
}
|
||||
|
||||
if ( isset( $new_settings['expiration_mode'] ) ) {
|
||||
$this->settings->set_expiration_mode( $new_settings['expiration_mode'] );
|
||||
}
|
||||
|
||||
if ( isset( $new_settings['expiration_delay_quantity'] ) ) {
|
||||
$this->settings->set_expiration_delay_quantity( $new_settings['expiration_delay_quantity'] );
|
||||
}
|
||||
|
||||
if ( isset( $new_settings['expiration_delay_unit'] ) ) {
|
||||
$this->settings->set_expiration_delay_unit( $new_settings['expiration_delay_unit'] );
|
||||
}
|
||||
|
||||
$this->settings_write->save( $this->settings );
|
||||
|
||||
wp_send_json_success( __( 'Settings saved', 'sitepress' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Backend_Options_Hooks
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Backend_Options_Hooks implements IWPML_Action {
|
||||
|
||||
const WPML_LOVE_ID = '#lang-sec-10';
|
||||
|
||||
/** @var WPML_TF_Backend_Options_View $options_view */
|
||||
private $options_view;
|
||||
|
||||
/** @var WPML_TF_Backend_Options_Scripts $scripts */
|
||||
private $scripts;
|
||||
|
||||
/** @var WPML_TF_Backend_Options_Styles $styles */
|
||||
private $styles;
|
||||
|
||||
/** @var WPML_TF_Translation_Service $translation_service */
|
||||
private $translation_service;
|
||||
|
||||
/**
|
||||
* WPML_TF_Backend_Options_Hooks constructor.
|
||||
*
|
||||
* @param WPML_TF_Backend_Options_View $options_view
|
||||
* @param WPML_TF_Backend_Options_Scripts $scripts
|
||||
* @param WPML_TF_Backend_Options_Styles $styles
|
||||
* @param WPML_TF_Translation_Service $translation_service
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TF_Backend_Options_View $options_view,
|
||||
WPML_TF_Backend_Options_Scripts $scripts,
|
||||
WPML_TF_Backend_Options_Styles $styles,
|
||||
WPML_TF_Translation_Service $translation_service
|
||||
) {
|
||||
$this->options_view = $options_view;
|
||||
$this->scripts = $scripts;
|
||||
$this->styles = $styles;
|
||||
$this->translation_service = $translation_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method add_hooks
|
||||
*/
|
||||
public function add_hooks() {
|
||||
if ( $this->translation_service->allows_translation_feedback() ) {
|
||||
add_action( 'wpml_after_settings', array( $this, 'display_options_ui' ) );
|
||||
add_filter( 'wpml_admin_languages_navigation_items', array( $this, 'insert_navigation_item' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_action' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to render the options UI
|
||||
*/
|
||||
public function display_options_ui() {
|
||||
echo $this->options_view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $items
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function insert_navigation_item( $items ) {
|
||||
$insert_key_index = array_search( self::WPML_LOVE_ID, array_keys( $items ) );
|
||||
$insert_position = false === $insert_key_index ? count( $items ) : $insert_key_index + 1;
|
||||
|
||||
return array_merge(
|
||||
array_slice( $items, 0, $insert_position ),
|
||||
array( '#wpml-translation-feedback-options' => esc_html__( 'Translation Feedback', 'sitepress' ) ),
|
||||
array_slice( $items, $insert_position )
|
||||
);
|
||||
}
|
||||
|
||||
public function enqueue_scripts_action() {
|
||||
$this->scripts->enqueue();
|
||||
$this->styles->enqueue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Backend_Post_List_Hooks
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Backend_Post_List_Hooks implements IWPML_Action {
|
||||
|
||||
const RATING_COLUMN_ID = 'translation_rating';
|
||||
|
||||
/** @var WPML_TF_Post_Rating_Metrics $post_rating_metrics*/
|
||||
private $post_rating_metrics;
|
||||
|
||||
/** @var WPML_TF_Document_Information $document_information */
|
||||
private $document_information;
|
||||
|
||||
/** @var WPML_TF_Backend_Styles $styles */
|
||||
private $styles;
|
||||
|
||||
public function __construct(
|
||||
WPML_TF_Post_Rating_Metrics $post_rating_metrics,
|
||||
WPML_TF_Document_Information $document_information,
|
||||
WPML_TF_Backend_Styles $styles
|
||||
) {
|
||||
$this->post_rating_metrics = $post_rating_metrics;
|
||||
$this->document_information = $document_information;
|
||||
$this->styles = $styles;
|
||||
}
|
||||
|
||||
/** @return array */
|
||||
private function get_post_types() {
|
||||
return array(
|
||||
'edit-post' => 'posts',
|
||||
'edit-page' => 'pages',
|
||||
);
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
foreach ( $this->get_post_types() as $screen_id => $post_type ) {
|
||||
add_filter( 'manage_' . $post_type . '_columns' , array( $this, 'add_rating_column_header' ) );
|
||||
add_action( 'manage_' . $post_type . '_custom_column', array( $this, 'add_rating_column_content' ), 10, 2 );
|
||||
add_filter( 'manage_' . $screen_id .'_sortable_columns', array( $this, 'add_rating_sortable_column' ) );
|
||||
}
|
||||
|
||||
add_action( 'pre_get_posts', array( $this, 'order_posts_by_rating' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts_action' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $columns
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_rating_column_header( array $columns ) {
|
||||
$key_to_insert_before = 'date';
|
||||
$column_name = __( 'Translation rating', 'sitepress' );
|
||||
|
||||
if ( array_key_exists( $key_to_insert_before, $columns ) ) {
|
||||
$insert_position = array_search( $key_to_insert_before, array_keys( $columns ) );
|
||||
$columns_before = array_slice( $columns, 0, $insert_position, true );
|
||||
$columns_to_insert = array( self::RATING_COLUMN_ID => $column_name );
|
||||
$columns_after = array_slice( $columns, $insert_position, null, true );
|
||||
$columns = array_merge( $columns_before, $columns_to_insert, $columns_after );
|
||||
} else {
|
||||
$columns[ self::RATING_COLUMN_ID ] = $column_name;
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $column_name
|
||||
* @param int $post_id
|
||||
*/
|
||||
public function add_rating_column_content( $column_name, $post_id ) {
|
||||
if ( self::RATING_COLUMN_ID !== $column_name ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post_type = get_post_type( $post_id );
|
||||
$this->document_information->init( $post_id, 'post_' . $post_type );
|
||||
|
||||
if ( $this->document_information->get_source_language() ) {
|
||||
echo $this->post_rating_metrics->get_display( $post_id );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $columns
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function add_rating_sortable_column( array $columns ) {
|
||||
$columns[ self::RATING_COLUMN_ID ] = self::RATING_COLUMN_ID;
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public function order_posts_by_rating( WP_Query $query ) {
|
||||
$orderby = $query->get( 'orderby');
|
||||
|
||||
if( self::RATING_COLUMN_ID === $orderby ) {
|
||||
$query->set( 'meta_key', WPML_TF_Post_Rating_Metrics::AVERAGE_KEY );
|
||||
$query->set( 'orderby', 'meta_value_num' );
|
||||
$query->set( 'meta_type', 'NUMERIC' );
|
||||
}
|
||||
}
|
||||
|
||||
public function admin_enqueue_scripts_action() {
|
||||
$current_screen = get_current_screen();
|
||||
|
||||
if ( isset( $current_screen->id ) && array_key_exists( $current_screen->id, $this->get_post_types() ) ) {
|
||||
$this->styles->enqueue();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
use WPML\FP\Obj;
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Backend_Promote_Hooks
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Backend_Promote_Hooks implements IWPML_Action {
|
||||
/** @var WPML_TF_Promote_Notices $promote_notices */
|
||||
private $promote_notices;
|
||||
|
||||
/** @var WPML_TF_Translation_Service $translation_service */
|
||||
private $translation_service;
|
||||
|
||||
/** @var bool $is_setup_complete */
|
||||
private $is_setup_complete;
|
||||
|
||||
/**
|
||||
* WPML_TF_Backend_Promote_Hooks constructor.
|
||||
*
|
||||
* @param WPML_TF_Promote_Notices $promote_notices
|
||||
* @param bool $is_setup_complete
|
||||
* @param WPML_TF_Translation_Service $translation_service
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TF_Promote_Notices $promote_notices,
|
||||
$is_setup_complete,
|
||||
WPML_TF_Translation_Service $translation_service
|
||||
) {
|
||||
$this->promote_notices = $promote_notices;
|
||||
$this->is_setup_complete = $is_setup_complete;
|
||||
$this->translation_service = $translation_service;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
if ( $this->translation_service->allows_translation_feedback() ) {
|
||||
if ( $this->is_setup_complete && get_option( WPML_Installation::WPML_START_VERSION_KEY ) ) {
|
||||
add_action(
|
||||
'wpml_pro_translation_completed',
|
||||
[ $this, 'add_notice_for_manager_on_job_completed' ],
|
||||
10, 3
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $new_post_id
|
||||
* @param array $fields
|
||||
* @param stdClass $job
|
||||
*/
|
||||
public function add_notice_for_manager_on_job_completed( $new_post_id, $fields, $job ) {
|
||||
if ( Obj::prop( 'translation_service', $job ) !== 'local' ) {
|
||||
$this->promote_notices->show_notice_for_new_site( (int) $job->manager_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Common_Hooks
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Common_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;
|
||||
}
|
||||
|
||||
/**
|
||||
* method init
|
||||
*/
|
||||
public function add_hooks() {
|
||||
add_action( 'init', array( $this, 'init_action' ) );
|
||||
add_action( 'deleted_post', array( $this, 'cleanup_post_feedback_data' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* method init_action
|
||||
*/
|
||||
public function init_action() {
|
||||
register_post_type( WPML_TF_Feedback_Post_Convert::POST_TYPE );
|
||||
register_post_type( WPML_TF_Message_Post_Convert::POST_TYPE );
|
||||
}
|
||||
|
||||
/** @param int $post_id */
|
||||
public function cleanup_post_feedback_data( $post_id ) {
|
||||
if ( WPML_TF_Feedback_Post_Convert::POST_TYPE === get_post_type( $post_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'post_id' => $post_id,
|
||||
);
|
||||
|
||||
$collection_filter = new WPML_TF_Feedback_Collection_Filter( $args );
|
||||
$feedback_collection = $this->feedback_storage->get_collection( $collection_filter );
|
||||
|
||||
foreach ( $feedback_collection as $feedback ) {
|
||||
$this->feedback_storage->delete( $feedback->get_id(), true );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Frontend_AJAX_Hooks
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Frontend_AJAX_Hooks implements IWPML_Action {
|
||||
|
||||
/** @var WPML_TF_Data_Object_Storage $feedback_storage */
|
||||
private $feedback_storage;
|
||||
|
||||
/** @var WPML_TF_Document_Information $document_information */
|
||||
private $document_information;
|
||||
|
||||
/** @var WPML_TF_Post_Rating_Metrics $post_rating_metrics */
|
||||
private $post_rating_metrics;
|
||||
|
||||
/** @var WPML_TP_Client_Factory $tp_client_factory */
|
||||
private $tp_client_factory;
|
||||
|
||||
/** @var WPML_TP_Client $tp_client */
|
||||
private $tp_client;
|
||||
|
||||
private $post_data;
|
||||
|
||||
/**
|
||||
* WPML_TF_Frontend_AJAX_Hooks constructor.
|
||||
*
|
||||
* @param WPML_TF_Data_Object_Storage $feedback_storage
|
||||
* @param WPML_TF_Document_Information $document_information
|
||||
* @param WPML_TF_Post_Rating_Metrics $post_rating_metrics
|
||||
* @param WPML_TP_Client_Factory|null $tp_client_factory
|
||||
* @param mixed[]|null $post_data
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TF_Data_Object_Storage $feedback_storage,
|
||||
WPML_TF_Document_Information $document_information,
|
||||
WPML_TF_Post_Rating_Metrics $post_rating_metrics,
|
||||
WPML_TP_Client_Factory $tp_client_factory = null,
|
||||
array $post_data = null
|
||||
) {
|
||||
$this->feedback_storage = $feedback_storage;
|
||||
$this->document_information = $document_information;
|
||||
$this->post_rating_metrics = $post_rating_metrics;
|
||||
$this->tp_client_factory = $tp_client_factory;
|
||||
$this->post_data = $post_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* method init
|
||||
*/
|
||||
public function add_hooks() {
|
||||
add_action( 'wp_ajax_nopriv_' . WPML_TF_Frontend_AJAX_Hooks_Factory::AJAX_ACTION, array( $this, 'save_feedback_callback' ) );
|
||||
add_action( 'wp_ajax_' . WPML_TF_Frontend_AJAX_Hooks_Factory::AJAX_ACTION, array( $this, 'save_feedback_callback' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Method callback
|
||||
*/
|
||||
public function save_feedback_callback() {
|
||||
$feedback = null;
|
||||
|
||||
if ( isset( $this->post_data['feedback_id'] ) ) {
|
||||
$feedback = $this->update_feedback( $this->post_data['feedback_id'] );
|
||||
} elseif ( isset( $this->post_data['rating'], $this->post_data['document_id'], $this->post_data['document_type'] ) ) {
|
||||
$feedback = $this->create_feedback();
|
||||
}
|
||||
|
||||
if ( $feedback ) {
|
||||
$feedback_id = $this->feedback_storage->persist( $feedback );
|
||||
$this->post_rating_metrics->refresh( $feedback->get_document_id() );
|
||||
wp_send_json_success( array( 'feedback_id' => $feedback_id ) );
|
||||
} else {
|
||||
wp_send_json_error( esc_html__( 'Invalid request!', 'sitepress' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $feedback_id
|
||||
*
|
||||
* @return WPML_TF_Feedback|null|false
|
||||
*/
|
||||
private function update_feedback( $feedback_id ) {
|
||||
$feedback = $this->feedback_storage->get( $feedback_id );
|
||||
|
||||
if ( ! $feedback ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var WPML_TF_Feedback $feedback */
|
||||
if ( isset( $this->post_data['content'] ) ) {
|
||||
$feedback->set_content( $this->post_data['content'] );
|
||||
}
|
||||
|
||||
if ( isset( $this->post_data['rating'] ) ) {
|
||||
$feedback->set_rating( $this->post_data['rating'] );
|
||||
}
|
||||
|
||||
$feedback->set_status( $this->get_filtered_status() );
|
||||
|
||||
return $feedback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WPML_TF_Feedback
|
||||
*/
|
||||
private function create_feedback() {
|
||||
$this->document_information->init( $this->post_data['document_id'], $this->post_data['document_type'] );
|
||||
|
||||
$args = array(
|
||||
'rating' => $this->post_data['rating'],
|
||||
'status' => $this->get_filtered_status(),
|
||||
'document_id' => $this->post_data['document_id'],
|
||||
'document_type' => $this->post_data['document_type'],
|
||||
'language_from' => $this->document_information->get_source_language(),
|
||||
'language_to' => $this->document_information->get_language(),
|
||||
'job_id' => $this->document_information->get_job_id(),
|
||||
);
|
||||
|
||||
if ( $this->document_information->is_local_translation( $args['job_id'] ) ) {
|
||||
$args['tp_rating_id'] = 0;
|
||||
} elseif ( $this->get_tp_client() ) {
|
||||
$active_service = $this->get_tp_client()->services()->get_active();
|
||||
|
||||
if ( isset( $active_service->feedback_forward_method ) ) {
|
||||
$args['feedback_forward_method'] = $active_service->feedback_forward_method;
|
||||
}
|
||||
}
|
||||
|
||||
return new WPML_TF_Feedback( $args );
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
private function get_filtered_status() {
|
||||
$rating = isset( $this->post_data['rating'] ) ? $this->post_data['rating'] : null;
|
||||
$status = 'pending';
|
||||
|
||||
if ( 3 < (int) $rating || empty( $this->post_data['content'] ) ) {
|
||||
$status = 'rating_only';
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/** @return null|WPML_TP_Client */
|
||||
private function get_tp_client() {
|
||||
if ( $this->tp_client_factory && ! $this->tp_client ) {
|
||||
$this->tp_client = $this->tp_client_factory->create();
|
||||
}
|
||||
|
||||
return $this->tp_client;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TF_Frontend_Hooks
|
||||
*
|
||||
* @author OnTheGoSystems
|
||||
*/
|
||||
class WPML_TF_Frontend_Hooks implements IWPML_Action {
|
||||
|
||||
/** @var WPML_TF_Frontend_Feedback_View $feedback_view */
|
||||
private $feedback_view;
|
||||
|
||||
/** @var WPML_TF_Frontend_Scripts $scripts */
|
||||
private $scripts;
|
||||
|
||||
/** @var WPML_TF_Frontend_Styles $styles */
|
||||
private $styles;
|
||||
|
||||
/**
|
||||
* WPML_TF_Frontend_Hooks constructor.
|
||||
*
|
||||
* @param WPML_TF_Frontend_Feedback_View $feedback_view
|
||||
* @param WPML_TF_Frontend_Scripts $scripts
|
||||
* @param WPML_TF_Frontend_Styles $styles
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TF_Frontend_Feedback_View $feedback_view,
|
||||
WPML_TF_Frontend_Scripts $scripts,
|
||||
WPML_TF_Frontend_Styles $styles
|
||||
) {
|
||||
$this->feedback_view = $feedback_view;
|
||||
$this->scripts = $scripts;
|
||||
$this->styles = $styles;
|
||||
}
|
||||
|
||||
/**
|
||||
* method init
|
||||
*/
|
||||
public function add_hooks() {
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_action' ) );
|
||||
add_action( 'wp_footer', array( $this, 'render_feedback_form' ) );
|
||||
add_action( 'wpml_tf_feedback_open_link', array( $this, 'render_custom_form_open_link' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* method enqueue_scripts_action
|
||||
*/
|
||||
public function enqueue_scripts_action() {
|
||||
$this->scripts->enqueue();
|
||||
$this->styles->enqueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* method render_feedback_form
|
||||
*/
|
||||
public function render_feedback_form() {
|
||||
echo $this->feedback_view->render_open_button();
|
||||
echo $this->feedback_view->render_form();
|
||||
}
|
||||
|
||||
/** @param string|array $args */
|
||||
public function render_custom_form_open_link( $args ) {
|
||||
echo $this->feedback_view->render_custom_open_link( $args );
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user