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,51 @@
<?php
namespace WPML\ST\Main\Ajax;
use WPML\Ajax\IHandler;
use WPML\Collect\Support\Collection;
use WPML\FP\Either;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
class FetchTranslationMemory implements IHandler {
/** @var \WPML_ST_Translation_Memory_Records $records */
private $records;
public function __construct( \WPML_ST_Translation_Memory_Records $records ) {
$this->records = $records;
}
public function run( Collection $data ) {
$translations = Obj::prop( 'batch', $data )
? $this->fetchBatchStrings( Obj::prop( 'strings', $data ) )
: $this->fetchSingleString( $data );
if ( $translations !== false ) {
return Either::of( $translations );
} else {
return Either::left( 'invalid data' );
}
}
private function fetchBatchStrings( $strings ) {
$results = Fns::map( [ $this, 'fetchSingleString' ], $strings );
return Lst::includes( false, $results ) ? false : $results;
}
public function fetchSingleString( $data ) {
$string = Obj::prop( 'string', $data );
$source = Obj::prop( 'source', $data );
$target = Obj::prop( 'target', $data );
if ( $string && $source && $target ) {
return $this->records->get( [ $string ], $source, $target );
} else {
return false;
}
}
}

View File

@@ -0,0 +1,87 @@
<?php
class WPML_ST_Translation_Memory_Records {
/** @var wpdb $wpdb */
private $wpdb;
public function __construct( wpdb $wpdb ) {
$this->wpdb = $wpdb;
}
/**
* @param array $strings
* @param string $source_lang
* @param string $target_lang
*
* @return array
*/
public function get( $strings, $source_lang, $target_lang ) {
if ( ! $strings ) {
return [];
}
$strings = $this->also_match_alternative_line_breaks( $strings );
$prepared_strings = wpml_prepare_in( $strings );
$sql = "
SELECT s.value as original, coalesce(st.value, st.mo_string) as translation, st.language as language
FROM {$this->wpdb->prefix}icl_strings as s
JOIN {$this->wpdb->prefix}icl_string_translations as st
ON s.id = st.string_id
WHERE s.value IN ({$prepared_strings}) AND s.language = '%s'
AND (
(st.value IS NOT NULL AND st.status IN (" . ICL_STRING_TRANSLATION_COMPLETE . "," . ICL_STRING_TRANSLATION_NEEDS_UPDATE . "))
OR (st.value IS NULL AND st.mo_string IS NOT NULL)
)";
$prepare_args = array( $source_lang );
if ( $target_lang ) {
$sql .= " AND st.language = '%s'";
$prepare_args[] = $target_lang;
} else {
$sql .= " AND st.language <> '%s'";
$prepare_args[] = $source_lang;
}
$records = $this->wpdb->get_results( $this->wpdb->prepare( $sql, $prepare_args ) );
$records = $this->also_include_matches_for_alternative_line_breaks( $records );
return $records;
}
private function also_match_alternative_line_breaks( $strings ) {
$new_strings = array();
foreach ( $strings as $string ) {
if ( mb_strpos( $string, "\r\n" ) !== false ) {
$new_strings[] = str_replace( "\r\n", "\n", $string );
}
if ( mb_strpos( $string, "\n" ) !== false && mb_strpos( $string, "\r" ) === false ) {
$new_strings[] = str_replace( "\n", "\r\n", $string );
}
}
return array_merge( $strings, $new_strings );
}
private function also_include_matches_for_alternative_line_breaks( $records ) {
$new_records = array();
foreach ( $records as $record ) {
if ( mb_strpos( $record->original, "\r\n" ) !== false ) {
$new_record = clone $record;
$new_record->original = str_replace( "\r\n", "\n", $record->original );
$new_records[] = $new_record;
}
if ( mb_strpos( $record->original, "\n" ) !== false && mb_strpos( $record->original, "\r" ) === false ) {
$new_record = clone $record;
$new_record->original = str_replace( "\n", "\r\n", $record->original );
$new_records[] = $new_record;
}
}
return array_merge( $records, $new_records );
}
}

View File

@@ -0,0 +1,37 @@
<?php
use \WPML\FP\Obj;
use \WPML\FP\Fns;
use \WPML\ST\Main\Ajax\FetchTranslationMemory;
class WPML_ST_Translation_Memory implements IWPML_AJAX_Action, IWPML_Backend_Action, IWPML_DIC_Action {
/** @var WPML_ST_Translation_Memory_Records $records */
private $records;
public function __construct( WPML_ST_Translation_Memory_Records $records ) {
$this->records = $records;
}
public function add_hooks() {
add_filter( 'wpml_st_get_translation_memory', [ $this, 'get_translation_memory' ], 10, 2 );
add_filter( 'wpml_st_translation_memory_endpoint', Fns::always( FetchTranslationMemory::class ) );
}
/**
* @param array $empty_array
* @param array $args with keys
* - `strings` an array of strings
* - `source_lang`
* - `target_lang`
*
* @return stdClass[]
*/
public function get_translation_memory( $empty_array, $args ) {
return $this->records->get(
Obj::propOr( [], 'strings', $args ),
Obj::propOr( '', 'source_lang', $args ),
Obj::propOr( '', 'target_lang', $args )
);
}
}