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,215 @@
<?php
/**
* Class WPML_TF_Backend_Document_Information
*
* @author OnTheGoSystems
*/
class WPML_TF_Backend_Document_Information extends WPML_TF_Document_Information {
/** @var WPML_TP_Client_Factory|null $tp_client_factory */
private $tp_client_factory;
/** @var WPML_TP_Client|null $tp_client */
private $tp_client;
/**
* WPML_TF_Backend_Document_Information constructor.
*
* @param SitePress $sitepress
* @param WPML_TP_Client_Factory $tp_client_factory
*/
public function __construct( SitePress $sitepress, WPML_TP_Client_Factory $tp_client_factory = null ) {
parent::__construct( $sitepress );
$this->tp_client_factory = $tp_client_factory;
}
/**
* @return false|null|string
*/
public function get_url() {
$url = null;
if ( $this->is_post_document() ) {
$url = get_permalink( $this->id );
}
return $url;
}
/**
* @return null|string
*/
public function get_title() {
$title = null;
if ( $this->is_post_document() ) {
$title = $this->get_post_title( $this->id );
}
return $title;
}
/**
* @return bool
*/
private function is_post_document() {
return 0 === strpos( $this->type, 'post_' );
}
/**
* @param string $language_code
*
* @return string
*/
public function get_flag_url( $language_code ) {
return $this->sitepress->get_flag_url( $language_code );
}
/**
* @return string
*/
public function get_edit_url() {
$this->load_link_to_translation_tm_filters();
$url = 'post.php?' . http_build_query(
array(
'lang' => $this->get_language(),
'action' => 'edit',
'post_type' => $this->type,
'post' => $this->id,
)
);
return apply_filters( 'wpml_link_to_translation', $url, $this->get_source_id(), $this->get_language(), $this->get_trid() );
}
private function load_link_to_translation_tm_filters() {
if ( ! did_action( 'wpml_pre_status_icon_display' ) ) {
do_action( 'wpml_pre_status_icon_display' );
}
}
/**
* @return null|int
*/
public function get_source_id() {
$source_id = null;
$translations = $this->get_translations();
if ( isset( $translations[ $this->get_source_language() ]->element_id ) ) {
$source_id = (int) $translations[ $this->get_source_language() ]->element_id;
}
return $source_id;
}
/**
* @return null|string
*/
public function get_source_url() {
$url = null;
if ( $this->get_source_id() ) {
$url = get_the_permalink( $this->get_source_id() );
}
return $url;
}
/**
* @return null|string
*/
public function get_source_title() {
$title = null;
if ( $this->get_source_id() ) {
$title = $this->get_post_title( $this->get_source_id() );
}
return $title;
}
/**
* @return array|bool|mixed
*/
private function get_translations() {
return $this->sitepress->get_element_translations( $this->get_trid(), $this->type );
}
/**
* @param int $job_id
*
* @return string
*/
public function get_translator_name( $job_id ) {
$translation_job = $this->get_translation_job( $job_id );
if ( isset( $translation_job->translation_service ) ) {
if ( 'local' === $translation_job->translation_service && isset( $translation_job->translator_id ) ) {
$translator_name = get_the_author_meta( 'display_name', $translation_job->translator_id );
$translator_name .= ' (' . __( 'local translator', 'sitepress' ) . ')';
} else {
$translator_name = __( 'Unknown remote translation service', 'sitepress' );
$tp_client = $this->get_tp_client();
if ( $tp_client ) {
$translator_name = $tp_client->services()->get_name( $translation_job->translation_service );
}
}
} else {
$post_author_id = get_post_field( 'post_author', $this->id );
$translator_name = get_the_author_meta( 'display_name', $post_author_id );
}
return $translator_name;
}
/**
* @param string $from
* @param string $to
*
* @return array
*/
public function get_available_translators( $from, $to ) {
$translators = array();
if ( function_exists( 'wpml_tm_load_blog_translators' ) ) {
$args = array(
'from' => $from,
'to' => $to,
);
$users = wpml_tm_load_blog_translators()->get_blog_translators( $args );
$translators = wp_list_pluck( $users, 'display_name', 'ID' );
}
return $translators;
}
/**
* @param int $post_id
*
* @return null|string
*/
private function get_post_title( $post_id ) {
$title = null;
$post = get_post( $post_id );
if ( isset( $post->post_title ) ) {
$title = $post->post_title;
}
return $title;
}
/** @return null|WPML_TP_Client */
private function get_tp_client() {
if ( ! $this->tp_client && $this->tp_client_factory ) {
$this->tp_client = $this->tp_client_factory->create();
}
return $this->tp_client;
}
}

View File

@@ -0,0 +1,117 @@
<?php
/**
* Class WPML_TF_Document_Information
*
* @author OnTheGoSystems
*/
class WPML_TF_Document_Information {
/** @var SitePress $sitepress */
protected $sitepress;
/** @var int $id */
protected $id;
/** @var string $type */
protected $type;
/** @var stdClass $language_details */
protected $language_details;
/** @var null|int|stdClass */
private $translation_job;
/**
* WPML_TF_Document_Information constructor.
*
* @param SitePress $sitepress
*/
public function __construct( SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
/**
* @param int $id
* @param string $type
*/
public function init( $id, $type ) {
$this->id = $id;
$this->type = $type;
$this->language_details = $this->sitepress->get_element_language_details( $this->id, $this->type );
}
/**
* @return string|null
*/
public function get_source_language() {
return isset( $this->language_details->source_language_code )
? $this->language_details->source_language_code
: null;
}
/**
* @return string
*/
public function get_language() {
return isset( $this->language_details->language_code )
? $this->language_details->language_code
: null;
}
/**
* @return null|int
*/
public function get_job_id() {
$args = array(
'trid' => $this->get_trid(),
'language_code' => $this->get_language(),
);
$job_id = apply_filters( 'wpml_translation_job_id', false, $args );
return $job_id ? $job_id : null;
}
/**
* @return null|int
*/
protected function get_trid() {
$trid = null;
if ( isset( $this->language_details->trid ) ) {
$trid = $this->language_details->trid;
}
return $trid;
}
/**
* @param int $job_id
*
* @return bool
*/
public function is_local_translation( $job_id ) {
$is_local = true;
$translation_job = $this->get_translation_job( $job_id );
if ( isset( $translation_job->translation_service ) && 'local' !== $translation_job->translation_service ) {
$is_local = false;
}
return $is_local;
}
/**
* @param int $job_id
*
* @return int|stdClass
*/
protected function get_translation_job( $job_id ) {
if ( ! $this->translation_job ) {
$this->translation_job = apply_filters( 'wpml_get_translation_job', $job_id );
}
return $this->translation_job;
}
}

View File

@@ -0,0 +1,239 @@
<?php
/**
* Class WPML_TF_Feedback_Page_Filter
*
* @author OnTheGoSystems
*/
class WPML_TF_Feedback_Page_Filter {
/** @var SitePress $sitepress */
private $sitepress;
/** @var WPML_TF_Feedback_Query $feedback_query */
private $feedback_query;
/** @var array $statuses */
private $statuses = array();
/** @var array $languages */
private $languages = array();
/** @var array $url_args */
private $url_args;
/** @var string $current_url */
private $current_url;
/**
* WPML_TF_Feedback_Page_Filter constructor.
*
* @param SitePress $sitepress
* @param WPML_TF_Feedback_Query $feedback_query
*/
public function __construct( SitePress $sitepress, WPML_TF_Feedback_Query $feedback_query ) {
$this->sitepress = $sitepress;
$this->feedback_query = $feedback_query;
}
/**
* @return array
*/
public static function get_filter_keys() {
return array(
'status',
'language',
'post_id',
);
}
/**
* Will not create filters inside the trash
* And will not include the "trash" status in the status row
*/
public function populate_counters_and_labels() {
if ( $this->feedback_query->is_in_trash() ) {
return;
}
foreach ( $this->feedback_query->get_unfiltered_collection() as $feedback ) {
/** @var WPML_TF_Feedback $feedback */
if ( 'trash' === $feedback->get_status() ) {
continue;
}
if ( ! array_key_exists( $feedback->get_status(), $this->statuses ) ) {
$this->statuses[ $feedback->get_status() ] = array(
'count' => 1,
'label' => $feedback->get_text_status(),
);
} else {
$this->statuses[ $feedback->get_status() ]['count']++;
}
if ( ! array_key_exists( $feedback->get_language_to(), $this->languages ) ) {
$lang_details = $this->sitepress->get_language_details( $feedback->get_language_to() );
$this->languages[ $feedback->get_language_to() ] = array(
'count' => 1,
'label' => isset( $lang_details['display_name'] ) ? $lang_details['display_name'] : null,
);
} else {
$this->languages[ $feedback->get_language_to() ]['count']++;
}
}
ksort( $this->statuses );
ksort( $this->languages );
}
/**
* @return array
*/
public function get_all_and_trash_data() {
$main_data = array(
'all' => array(
'count' => $this->feedback_query->get_total_items_count(),
'label' => __( 'All', 'sitepress' ),
'url' => $this->get_reset_filters_url(),
'current' => $this->get_current_url() === $this->get_reset_filters_url(),
),
);
if ( $this->feedback_query->get_total_trashed_items_count() ) {
$main_data['trashed'] = array(
'count' => $this->feedback_query->get_total_trashed_items_count(),
'label' => __( 'Trash', 'sitepress' ),
'url' => $this->get_filter_url( 'status', 'trash' ),
'current' => $this->get_current_url() === $this->get_filter_url( 'status', 'trash' ),
);
}
if ( $this->is_current_filter( 'post_id' ) ) {
$filters = $this->get_current_filters();
$post = get_post( $filters['post_id'] );
if ( isset( $post->post_title ) ) {
$main_data['post_id'] = array(
'label' => sprintf(
__( 'For "%s"', 'sitepress' ),
mb_strimwidth( $post->post_title, 0, 40, '...' )
),
'current' => true,
);
}
}
return $main_data;
}
/**
* @return array
*/
public function get_statuses_data() {
foreach ( $this->statuses as $status => $data ) {
$this->statuses[ $status ]['url'] = $this->get_filter_url( 'status', $status );
$this->statuses[ $status ]['current'] = false;
if ( $this->is_current_filter( 'status', $status ) ) {
$this->statuses[ $status ]['current'] = true;
}
}
return $this->statuses;
}
/**
* @return array
*/
public function get_languages_data() {
foreach ( $this->languages as $language_code => $data ) {
$this->languages[ $language_code ]['url'] = $this->get_filter_url( 'language', $language_code );
$this->languages[ $language_code ]['current'] = false;
if ( $this->is_current_filter( 'language', $language_code ) ) {
$this->languages[ $language_code ]['current'] = true;
}
}
return $this->languages;
}
/**
* @param string $filter_name
* @param string $filter_value
*
* @return string
*/
private function get_filter_url( $filter_name, $filter_value ) {
return add_query_arg( $filter_name, $filter_value, $this->get_reset_filters_url() );
}
/**
* @param string $filter_key
* @param string $filter_value
*
* @return bool
*/
private function is_current_filter( $filter_key, $filter_value = null ) {
$is_current_filter = false;
$query_args = $this->get_url_args();
if ( array_key_exists( $filter_key, $query_args )
&& ( ! $filter_value || $query_args[ $filter_key ] === $filter_value )
) {
$is_current_filter = true;
}
return $is_current_filter;
}
/**
* @return array
*/
public function get_current_filters() {
$filters = array();
$query_args = $this->get_url_args();
foreach ( self::get_filter_keys() as $filter_key ) {
if ( array_key_exists( $filter_key, $query_args ) ) {
$filters[ $filter_key ] = $query_args[ $filter_key ];
}
}
return $filters;
}
/**
* @return array
*/
private function get_url_args() {
if ( ! $this->url_args ) {
$this->url_args = array();
$url_query = wpml_parse_url( $this->get_current_url(), PHP_URL_QUERY );
parse_str( $url_query, $this->url_args );
}
return $this->url_args;
}
/**
* @return string
*/
private function get_current_url() {
if ( ! $this->current_url ) {
$this->current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$this->current_url = remove_query_arg( 'paged', $this->current_url );
}
return $this->current_url;
}
/**
* @return string
*/
private function get_reset_filters_url() {
return remove_query_arg( self::get_filter_keys(), $this->get_current_url() );
}
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* Class WPML_TF_Frontend_Display_Requirements
*
* @author OnTheGoSystems
*/
class WPML_TF_Frontend_Display_Requirements {
/** @var WPML_Queried_Object $queried_object */
private $queried_object;
/** @var WPML_TF_Settings $settings */
private $settings;
/**
* WPML_TF_Frontend_Display_Requirements constructor.
*
* @param WPML_Queried_Object $queried_object
* @param WPML_TF_Settings $settings
*/
public function __construct( WPML_Queried_Object $queried_object, WPML_TF_Settings $settings ) {
$this->queried_object = $queried_object;
$this->settings = $settings;
}
/**
* @return bool
*/
public function verify() {
return $this->is_enabled_on_frontend()
&& $this->is_translation()
&& $this->is_allowed_language()
&& $this->is_not_expired();
}
/**
* @return bool
*/
private function is_enabled_on_frontend() {
return $this->settings->is_enabled()
&& $this->settings->get_button_mode() !== WPML_TF_Settings::BUTTON_MODE_DISABLED;
}
/**
* @return bool
*/
private function is_translation() {
return (bool) $this->queried_object->get_source_language_code();
}
/**
* @return bool
*/
private function is_allowed_language() {
return is_array( $this->settings->get_languages_to() )
&& in_array( $this->queried_object->get_language_code(), $this->settings->get_languages_to(), true );
}
/**
* @return bool
*/
private function is_not_expired() {
$is_expired = false;
if ( $this->settings->get_display_mode() === WPML_TF_Settings::DISPLAY_CUSTOM
&& $this->queried_object->is_post()
) {
$post = get_post( $this->queried_object->get_id() );
$now = strtotime( 'now' );
if ( $this->settings->get_expiration_mode() === WPML_TF_Settings::EXPIRATION_ON_PUBLISH_ONLY ) {
$post_date = strtotime( $post->post_date );
} else {
$post_date = max( strtotime( $post->post_date ), strtotime( $post->post_modified ) );
}
$post_age_in_days = ( $now - $post_date ) / DAY_IN_SECONDS;
if ( $post_age_in_days > $this->settings->get_expiration_delay_in_days() ) {
$is_expired = true;
}
}
return ! $is_expired;
}
}

View File

@@ -0,0 +1,92 @@
<?php
/**
* Class WPML_TF_Rating_Average
*
* @author OnTheGoSystems
*/
class WPML_TF_Post_Rating_Metrics {
const QUANTITY_KEY = 'wpml_tf_post_rating_quantity';
const AVERAGE_KEY = 'wpml_tf_post_rating_average';
/** @var wpdb $wpdb */
private $wpdb;
public function __construct( wpdb $wpdb ) {
$this->wpdb = $wpdb;
}
/**
* @param int $post_id
*
* @return string
*/
public function get_display( $post_id ) {
$quantity = (int) get_post_meta( $post_id, self::QUANTITY_KEY, true );
if ( 0 === $quantity ) {
return '';
}
$average = get_post_meta( $post_id, self::AVERAGE_KEY, true );
$title = sprintf( __( 'Average rating - %s', 'sitepress' ), $average );
$stars = round( $average );
$url = admin_url( 'admin.php?page=' . WPML_TF_Backend_Hooks::PAGE_HOOK . '&post_id=' . $post_id );
$output = '<div class="wpml-tf-rating" title="' . esc_attr( $title ) . '"><a href="'. esc_url( $url ) . '">';
for ( $i = 1; $i < 6; $i++ ) {
$output .= '<span class="otgs-ico-star';
if ( $i <= $stars ) {
$output .= ' full-star';
}
$output .= '"></span>';
}
$output .= '</a> <span class="rating-quantity">(' . esc_html( $quantity ) . ')</span></div>';
return $output;
}
/** @param int $post_id */
public function refresh( $post_id ) {
$document_id_key = WPML_TF_Data_Object_Storage::META_PREFIX . 'document_id';
$rating_key = WPML_TF_Data_Object_Storage::META_PREFIX . 'rating';
$subquery = $this->wpdb->prepare(
"SELECT post_id FROM {$this->wpdb->postmeta}
WHERE meta_key = %s AND meta_value = %d",
$document_id_key,
$post_id
);
$query = $this->wpdb->prepare(
"SELECT meta_value FROM {$this->wpdb->postmeta}
WHERE meta_key = %s AND post_id IN( {$subquery} )",
$rating_key
);
$ratings = $this->wpdb->get_results( $query );
if ( $ratings ) {
$total = 0;
foreach ( $ratings as $rating ) {
$total += (int) $rating->meta_value;
}
$quantity = count( $ratings );
$average = round( $total / $quantity, 1 );
update_post_meta( $post_id, self::QUANTITY_KEY, $quantity );
update_post_meta( $post_id, self::AVERAGE_KEY, $average );
} else {
delete_post_meta( $post_id, self::QUANTITY_KEY );
delete_post_meta( $post_id, self::AVERAGE_KEY );
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* Class WPML_TF_Translation_Service
*
* @author OnTheGoSystems
*/
class WPML_TF_Translation_Service {
/** @var WPML_TP_Client_Factory $tp_client_factory */
private $tp_client_factory;
/**
* WPML_TF_Translation_Service constructor.
*
* @param WPML_TP_Client_Factory $tp_client_factory
*/
public function __construct( WPML_TP_Client_Factory $tp_client_factory = null ) {
$this->tp_client_factory = $tp_client_factory;
}
/** @return bool */
public function allows_translation_feedback() {
if ( ! $this->tp_client_factory ) {
return true;
}
$translation_service = $this->tp_client_factory->create()->services()->get_active();
if ( isset( $translation_service->translation_feedback ) && ! $translation_service->translation_feedback ) {
return false;
}
return true;
}
}