-
Notifications
You must be signed in to change notification settings - Fork 11
/
ZipProvider.php
333 lines (284 loc) · 7.55 KB
/
ZipProvider.php
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* Language pack provider implementation
*
* @since 2.0.0
*
* @package Required\Traduttore
*/
namespace Required\Traduttore;
use DateTime;
use DateTimeZone;
use GP;
use GP_Locale;
use GP_Locales;
use GP_Translation_Set;
use WP_Filesystem_Base;
use ZipArchive;
/**
* Class used to generate language packs for translations.
*
* @since 2.0.0
*/
class ZipProvider {
/**
* Traduttore cache directory name.
*
* @since 2.0.0
*
* @var string Cache directory for ZIP files.
*/
protected const CACHE_DIR = 'traduttore';
/**
* Build time meta key.
*
* @since 2.0.0
*
* @var string Build time meta key.
*/
protected const BUILD_TIME_KEY = '_traduttore_build_time';
/**
* The current GlotPress translation set.
*
* @since 2.0.0
*
* @var GP_Translation_Set The translation set.
*/
protected $translation_set;
/**
* The current GlotPress locale.
*
* @since 3.0.0
*
* @var GP_Locale The locale.
*/
protected $locale;
/**
* The current project.
*
* @since 3.0.0
*
* @var Project The project.
*/
protected $project;
/**
* ZipProvider constructor.
*
* @since 2.0.0
*
* @param GP_Translation_Set $translation_set Translation set to get the ZIP for.
*/
public function __construct( GP_Translation_Set $translation_set ) {
$this->translation_set = $translation_set;
$this->locale = GP_Locales::by_slug( $this->translation_set->locale );
$this->project = new Project( GP::$project->get( $this->translation_set->project_id ) );
}
/**
* Schedules ZIP generation for the current translation set.
*
* Adds a single cron event to generate the ZIP archive after a short amount of time.
*
* @since 3.0.0
*/
public function schedule_generation(): void {
$translation_set_id = (int) $this->translation_set->id;
/**
* Filters the delay for scheduled language pack generation.
*
* @since 3.0.0
*
* @param int $delay Delay in minutes. Default is 5 minutes.
* @param GP_Translation_Set $translation_set Translation set the ZIP generation will be scheduled for.
*/
$delay = (int) apply_filters( 'traduttore.generate_zip_delay', MINUTE_IN_SECONDS * 5, $translation_set_id );
$next_schedule = wp_next_scheduled( 'traduttore.generate_zip', [ $translation_set_id ] );
if ( $next_schedule ) {
wp_unschedule_event( 'traduttore.generate_zip', $next_schedule, [ $translation_set_id ] );
}
wp_schedule_single_event( time() + $delay, 'traduttore.generate_zip', [ $translation_set_id ] );
}
/**
* Generates and caches a ZIP file for a translation set.
*
* @since 2.0.0
*
* @global WP_Filesystem_Base $wp_filesystem
*
* @return bool True on success, false on failure.
*/
public function generate_zip_file() : bool {
/* @var WP_Filesystem_Base $wp_filesystem */
global $wp_filesystem;
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/admin.php';
if ( ! \WP_Filesystem() ) {
return false;
}
}
// Make sure the cache directory exists.
if ( ! is_dir( static::get_cache_dir() ) ) {
$wp_filesystem->mkdir( static::get_cache_dir(), FS_CHMOD_DIR );
}
$export = new Export( $this->translation_set );
$files_for_zip = $export->export_strings();
if ( ! $files_for_zip ) {
return false;
}
$zip = new ZipArchive();
$temp_zip_file = wp_tempnam( $this->get_zip_filename() );
if ( $zip->open( $temp_zip_file, ZipArchive::CREATE ) === true ) {
foreach ( $files_for_zip as $file_name => $temp_file ) {
$zip->addFile( $temp_file, $file_name );
}
$zip->close();
}
$wp_filesystem->move( $temp_zip_file, $this->get_zip_path(), true );
foreach ( $files_for_zip as $temp_file ) {
$wp_filesystem->delete( $temp_file );
}
$last_modified = $this->translation_set->last_modified();
if ( $last_modified ) {
$last_modified = new DateTime( $last_modified, new DateTimeZone( 'UTC' ) );
} else {
$last_modified = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
}
gp_update_meta( $this->translation_set->id, static::BUILD_TIME_KEY, $last_modified->format( DATE_ATOM ), 'translation_set' );
/**
* Fires after a language pack for a given translation set has been generated.
*
* @since 3.0.0
*
* @param string $file Path to the generated language pack.
* @param string $url URL to the generated language pack.
* @param GP_Translation_Set $translation_set Translation set the language pack is for.
* @param Project $project The translation set's project.
*/
do_action( 'traduttore.zip_generated', $this->get_zip_path(), $this->get_zip_url(), $this->translation_set, $this->project );
return true;
}
/**
* Removes the ZIP file for a translation set.
*
* @since 3.0.0
*
* @global WP_Filesystem_Base $wp_filesystem
*
* @return bool True on success, false on failure.
*/
public function remove_zip_file() : bool {
if ( ! file_exists( $this->get_zip_path() ) ) {
return false;
}
/* @var WP_Filesystem_Base $wp_filesystem */
global $wp_filesystem;
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/admin.php';
if ( ! \WP_Filesystem() ) {
return false;
}
}
$success = $wp_filesystem->rmdir( $this->get_zip_path(), true );
if ( $success ) {
gp_update_meta( $this->translation_set->id, static::BUILD_TIME_KEY, '', 'translation_set' );
}
return $success;
}
/**
* Returns the name of the ZIP file without the path.
*
* @since 2.0.0
*
* @return string ZIP filename.
*/
protected function get_zip_filename() : string {
$slug = str_replace( '/', '-', $this->project->get_slug() );
$version = $this->project->get_version();
if ( $version ) {
return sprintf(
'%1$s-%2$s-%3$s.zip',
$slug,
$this->locale->wp_locale,
$version
);
}
return sprintf(
'%1$s-%2$s.zip',
$slug,
$this->locale->wp_locale
);
}
/**
* Returns the last ZIP build time for a given translation set.
*
* @since 2.0.0
*
* @return DateTime Last build time.
*/
public function get_last_build_time() :? DateTime {
$meta = gp_get_meta( 'translation_set', $this->translation_set->id, static::BUILD_TIME_KEY );
return $meta ? new DateTime( $meta, new DateTimeZone( 'UTC' ) ) : null;
}
/**
* Returns the full URL to the ZIP file.
*
* @since 2.0.0
*
* @return string ZIP file URL.
*/
public function get_zip_url() : string {
$url = content_url( self::CACHE_DIR );
/**
* Filters the path to Traduttore's cache directory.
*
* Useful when language packs should be stored somewhere else.
*
* @since 3.0.0
*
* @param string $url Cache directory URL.
*/
$url = apply_filters( 'traduttore.content_url', $url );
return sprintf(
'%1$s/%2$s',
$url,
$this->get_zip_filename()
);
}
/**
* Returns the full path to the ZIP file.
*
* @since 2.0.0
*
* @return string ZIP file path.
*/
public function get_zip_path() : string {
return sprintf(
'%1$s/%2$s',
static::get_cache_dir(),
$this->get_zip_filename()
);
}
/**
* Returns the full path to the directory where language packs are stored.
*
* @since 2.0.0
*
* @return string Cache directory path.
*/
public static function get_cache_dir() : string {
$dir = sprintf(
'%1$s/%2$s',
WP_CONTENT_DIR,
self::CACHE_DIR
);
/**
* Filters the path to Traduttore's cache directory.
*
* Useful when language packs should be stored somewhere else.
*
* @since 3.0.0
*
* @param string $dir Cache directory path.
*/
return apply_filters( 'traduttore.content_dir', $dir );
}
}