class-wpml-st-string-positions-in-source.php
3.07 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Class WPML_ST_String_Positions_In_Source
*/
class WPML_ST_String_Positions_In_Source extends WPML_ST_String_Positions {
const KIND = ICL_STRING_TRANSLATION_STRING_TRACKING_TYPE_SOURCE;
const TEMPLATE = 'positions-in-source.twig';
/**
* @var SitePress $sitepress
*/
private $sitepress;
/**
* @var WP_Filesystem_Direct $filesystem
*/
private $filesystem;
/**
* @var WPML_File_Name_Converter $filename_converter
*/
private $filename_converter;
/**
* @var \WPML_WP_API
*/
private $wp_api;
public function __construct(
SitePress $sitePress,
WPML_ST_DB_Mappers_String_Positions $string_position_mapper,
IWPML_Template_Service $template_service,
WPML_WP_API $wp_api
) {
$this->sitepress = $sitePress;
$this->wp_api = $wp_api;
parent::__construct( $string_position_mapper, $template_service );
}
protected function get_model( $string_id ) {
$positions = $this->get_positions( $string_id );
$st_settings = $this->sitepress->get_setting( 'st' );
$highlight_color = '#FFFF00';
if ( array_key_exists( 'hl_color', $st_settings ) ) {
$highlight_color = $st_settings['hl_color'];
}
return array(
'positions' => $positions,
'no_results_label' => __( 'No records found', 'wpml-string-translation' ),
'highlight_color' => $highlight_color,
);
}
protected function get_template_name() {
return self::TEMPLATE;
}
/**
* @param int $string_id
*
* @return array
*/
private function get_positions( $string_id ) {
$positions = array();
$paths = $this->get_mapper()->get_positions_by_string_and_kind( $string_id, self::KIND );
foreach ( $paths as $path ) {
$position = explode( '::', $path );
$path = isset( $position[0] ) ? $position[0] : null;
if( ! $this->get_filesystem()->exists( $path ) ) {
$path = $this->maybe_transform_from_relative_path_to_absolute_path( $path );
}
if ( $path && $this->get_filesystem()->is_readable( $path ) ) {
$positions[] = array(
'path' => $path,
'line' => isset( $position[1] ) ? $position[1] : null,
'content' => $this->get_filesystem()->get_contents_array( $path ),
);
}
}
return $positions;
}
/**
* @param string $path
*
* @return string|false
*/
private function maybe_transform_from_relative_path_to_absolute_path( $path ) {
$path = $this->get_filename_converter()->transform_reference_to_realpath( $path );
if ( $this->get_filesystem()->exists( $path ) ) {
return $path;
}
return false;
}
/**
* @return WP_Filesystem_Direct
*/
private function get_filesystem() {
if ( ! $this->filesystem ) {
$this->filesystem = $this->get_wp_api()->get_wp_filesystem_direct();
}
return $this->filesystem;
}
/**
* @return WPML_WP_API
*/
private function get_wp_api() {
if ( ! $this->wp_api ) {
$this->wp_api = new WPML_WP_API();
}
return $this->wp_api;
}
/**
* @return WPML_File_Name_Converter
*/
private function get_filename_converter() {
if ( ! $this->filename_converter ) {
$this->filename_converter = new WPML_File_Name_Converter();
}
return $this->filename_converter;
}
}