wpml-st-script-translations-hooks.php
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
class WPML_ST_Script_Translations_Hooks implements IWPML_Action {
const PRIORITY_OVERRIDE_JED_FILE = 10;
/** @var WPML_ST_Translations_File_Dictionary $dictionary */
private $dictionary;
/** @var WPML_ST_JED_File_Manager $jed_file_manager */
private $jed_file_manager;
/** @var WPML_File $wpml_file */
private $wpml_file;
public function __construct(
WPML_ST_Translations_File_Dictionary $dictionary,
WPML_ST_JED_File_Manager $jed_file_manager,
WPML_File $wpml_file
) {
$this->dictionary = $dictionary;
$this->jed_file_manager = $jed_file_manager;
$this->wpml_file = $wpml_file;
}
public function add_hooks() {
add_filter( 'load_script_translation_file', array( $this, 'override_jed_file' ), self::PRIORITY_OVERRIDE_JED_FILE, 3 );
}
/**
* @param string $filepath
* @param string $handler
* @param string $domain
*
* @return string
*/
public function override_jed_file( $filepath, $handler, $domain ) {
if ( ! $filepath ) {
return $filepath;
}
$locale = $this->get_file_locale( $filepath, $domain );
$domain = WPML_ST_JED_Domain::get( $domain, $handler );
$wpml_filepath = $this->jed_file_manager->get( $domain, $locale );
if ( $wpml_filepath ) {
return $wpml_filepath;
}
return $filepath;
}
/**
* @param string $filepath
*
* @return bool
*/
private function is_file_imported( $filepath ) {
$relative_path = $this->wpml_file->get_relative_path( $filepath );
$file = $this->dictionary->find_file_info_by_path( $relative_path );
$statuses = array( WPML_ST_Translations_File_Entry::IMPORTED, WPML_ST_Translations_File_Entry::FINISHED );
return $file && in_array( $file->get_status(), $statuses, true );
}
/**
* @param string $filepath
* @param string $domain
*
* @return string
*/
private function get_file_locale( $filepath, $domain ) {
return \WPML\Container\make( \WPML_ST_Translations_File_Locale::class )->get( $filepath, $domain );
}
}