wpml-st-translations-file-entry.php
4.57 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
class WPML_ST_Translations_File_Entry {
const NOT_IMPORTED = 'not_imported';
const IMPORTED = 'imported';
const PARTLY_IMPORTED = 'partly_imported';
const FINISHED = 'finished';
const SKIPPED = 'skipped';
const PATTERN_SEARCH_LANG_MO = '#[-]?([a-z]+[_A-Z]*)\.mo$#i';
const PATTERN_SEARCH_LANG_JSON = '#([a-z]+[_A-Z]*)-[-a-z0-9]+\.json$#i';
/** @var string */
private $path;
/** @var string */
private $domain;
/** @var int */
private $status;
/** @var int */
private $imported_strings_count = 0;
/** @var int */
private $last_modified;
/** @var string */
private $component_type;
/** @var string */
private $component_id;
/**
* @param string $path
* @param string $domain
* @param string $status
*/
public function __construct( $path, $domain, $status = self::NOT_IMPORTED ) {
if ( ! is_string( $path ) ) {
throw new InvalidArgumentException( 'MO File path must be string type' );
}
if ( ! is_string( $domain ) ) {
throw new InvalidArgumentException( 'MO File domain must be string type' );
}
$this->path = $this->convert_to_relative_path( $path );
$this->domain = $domain;
$this->validate_status( $status );
$this->status = $status;
}
/**
* We can't rely on ABSPATH in out tests
*
* @param string $path
*
* @return string
*/
private function convert_to_relative_path( $path ) {
$parts = explode( DIRECTORY_SEPARATOR, $this->fix_dir_separator( WP_CONTENT_DIR ) );
return str_replace( WP_CONTENT_DIR, end( $parts ), $path );
}
/**
* @return string
*/
public function get_path() {
return $this->path;
}
public function get_full_path() {
$wp_content_dir = $this->fix_dir_separator( WP_CONTENT_DIR );
$parts = explode( DIRECTORY_SEPARATOR, $wp_content_dir );
return str_replace( end( $parts ), $wp_content_dir, $this->path );
}
/**
* @return string
*/
public function get_path_hash() {
return md5( $this->path );
}
/**
* @return string
*/
public function get_domain() {
return $this->domain;
}
/**
* @return int
*/
public function get_status() {
return $this->status;
}
/**
* @param string $status
*/
public function set_status( $status ) {
$this->validate_status( $status );
$this->status = $status;
}
/**
* @return int
*/
public function get_imported_strings_count() {
return $this->imported_strings_count;
}
/**
* @param int $imported_strings_count
*/
public function set_imported_strings_count( $imported_strings_count ) {
$this->imported_strings_count = (int) $imported_strings_count;
}
/**
* @return int
*/
public function get_last_modified() {
return $this->last_modified;
}
/**
* @param int $last_modified
*/
public function set_last_modified( $last_modified ) {
$this->last_modified = (int) $last_modified;
}
public function __get( $name ) {
if ( in_array( $name, array( 'path', 'domain', 'status', 'imported_strings_count', 'last_modified' ), true ) ) {
return $this->$name;
}
if ( $name === 'path_md5' ) {
return $this->get_path_hash();
}
return null;
}
/**
* It extracts locale from mo file path, examples
* '/wp-content/languages/admin-pl_PL.mo' => 'pl'
* '/wp-content/plugins/sitepress/sitepress-hr.mo' => 'hr'
*
* @return null|string
* @throws RuntimeException
*/
public function get_file_locale() {
return \WPML\Container\make( WPML_ST_Translations_File_Locale::class )->get( $this->get_path(), $this->get_domain() );
}
/**
* @return string
*/
public function get_component_type() {
return $this->component_type;
}
/**
* @param string $component_type
*/
public function set_component_type( $component_type ) {
$this->component_type = $component_type;
}
/**
* @return string
*/
public function get_component_id() {
return $this->component_id;
}
/**
* @param string $component_id
*/
public function set_component_id( $component_id ) {
$this->component_id = $component_id;
}
/**
* @param string $status
*/
private function validate_status( $status ) {
$allowed_statuses = array(
self::NOT_IMPORTED,
self::IMPORTED,
self::PARTLY_IMPORTED,
self::FINISHED,
self::SKIPPED,
);
if ( ! in_array( $status, $allowed_statuses, true ) ) {
throw new InvalidArgumentException( 'Status of MO file is invalid' );
}
}
/**
* @param string $path
*
* @return string
*/
private function fix_dir_separator( $path ) {
return ( '\\' === DIRECTORY_SEPARATOR ) ? str_replace( '/', '\\', $path ) : str_replace( '\\', '/', $path );
}
public function get_extension() {
return pathinfo( $this->path, PATHINFO_EXTENSION );
}
}