-
Notifications
You must be signed in to change notification settings - Fork 1
/
gp-live-export.php
328 lines (275 loc) · 9.21 KB
/
gp-live-export.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
<?php
/*
Plugin Name: GP Live Export
Description: Convert your WordPress site into all-in-one translation platform of editor and sandbox. GlotPress plugin Required.
Author: Mayo Moriyama
Version: 0.3
*/
class GPLE_Options_Page {
private $message = [];
private $project_name = '';
/**
* Constructor.
*/
function __construct() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
/**
* Registers a new settings page under Settings.
*/
function admin_menu() {
add_options_page(
esc_html__( 'GP Live Export', 'gp-live-export' ),
esc_html__( 'GP Live Export', 'gp-live-export' ),
'manage_options',
'gp-live-export',
array(
$this,
'page_rending'
)
);
}
/**
* Get a translation set for a project.
*
* @param string $project Project path
* @param string $locale Locale slug
* @param string $set Set slug
* @return GP_Translation_Set|WP_Error Translation set if available, error otherwise.
*/
protected function get_translation_set( $project, $locale, $set = 'default' ) {
$this->project = GP::$project->by_path( $project );
if ( ! $this->project ) {
return new WP_Error( 'gp_set_no_project', __( 'Project not found!', 'glotpress' ) );
}
$this->locale = GP_Locales::by_slug( $locale );
if ( ! $this->locale ) {
return new WP_Error( 'gp_set_no_locale', __( 'Locale not found!', 'glotpress' ) );
}
$this->translation_set = GP::$translation_set->by_project_id_slug_and_locale( $this->project->id, $set, $this->locale->slug );
if ( ! $this->translation_set ) {
return new WP_Error( 'gp_set_not_found', __( 'Translation set not found!', 'glotpress' ) );
}
return $this->translation_set;
}
function admin_init () {
if ( !is_plugin_active( 'glotpress/glotpress.php' ) ) :
$this->message[] = array(
'status' => 'error',
'content' => esc_html__( 'GlotPress plugin needs to be installed and activated', 'gp-live-export' ),
'path' => 'GP Live Export'
);
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
remove_submenu_page( 'options-general.php', 'gp-live-export' );
else:
if ( isset( $_GET["page"] )
&& isset( $_GET["project"] )
&& isset( $_GET["locale"] )
&& $_GET["page"] == 'gp-live-export' ) :
$set = ( isset( $_GET["set"] ) ) ? $_GET["set"] : 'default';
$this->export( $_GET["project"], $_GET["locale"], $set );
endif;
endif;
}
/**
* Export translation file.
*/
function export( $project, $locale, $set = 'default' ) {
$translation_set = $this->get_translation_set( $project, $locale, $set );
if ( is_wp_error( $translation_set ) ) {
$this->message[] = array(
'status' => 'error',
'content' => $translation_set->get_error_message(),
'path' => $project
);
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
return;
}
$filters = array( 'status' => 'current_or_waiting_or_fuzzy_or_untranslated' );
$entries = GP::$translation->for_translation( $this->project, $translation_set, 'no-limit', $filters );
$types = array( 'po', 'mo' );
foreach( $types as $type ) :
$format = gp_array_get( GP::$formats, $type, null );
if ( ! $format ) :
$this->message[] = array(
'status' => 'error',
'content' => __( 'No such format', 'glotpress' ),
'path' => $project
);
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
else:
$print = $format->print_exported_file( $this->project, $this->locale, $translation_set, $entries );
$name = $this->get_file_name( $project, GP_Locales::by_slug( $locale )->wp_locale, $type );
$path = path_join( WP_LANG_DIR, $name );
$save = $this->file_save( $print, $path );
if( is_wp_error( $save ) ) :
echo $save->get_error_message();
$this->message[] = array(
'status' => 'error',
'content' => $save->get_error_message(),
'path' => $name
);
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
else :
$this->message[] = array(
'status' => 'success',
'path' => $path
);
$this->project_name = $project;
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
endif;
endif;
endforeach;
}
/**
* Get a translation set for a project.
*
* @param string $print File content
* @param string $path Target file path
* @return GP_Translation_Set|WP_Error Translation set if available, error otherwise.
*/
function file_save ( $print = '', $path = '' ) {
if ( empty( $print ) || empty( $path ) ) {
return new WP_Error( 'gp_live_export_no_param', __( 'No content or file path', 'gp-live-export' ) );
}
$file_path = path_join( WP_LANG_DIR, $path );
if ( !$file = fopen( $path, 'w' ) ) {
return new WP_Error( 'gp_live_export_not_open', __( 'Cannot open file', 'gp-live-export' ) );
}
if ( fwrite( $file, $print ) === FALSE ) {
return new WP_Error( 'gp_live_export_not_rewritable', __( 'Cannot write to file', 'gp-live-export' ) );
}
return fclose( $file );
}
/**
* Generate a file path to save a file.
*
* @param string $name File name
* @param string $locale File locale
* @param string type File type
* @return $path File path to save
*/
function get_file_name ( $name = '', $locale = '', $type = 'po' ) {
if ( empty( $locale ) ) {
$locale = get_user_locale();
}
$name = str_replace( 'wp-', '', $name);
$name = "$name-$locale.$type";
return $name;
}
/**
* Settings page display callback.
*/
function page_rending () {
echo '<div class="wrap">'
.'<h1 class="wp-heading-inline">';
esc_html_e( 'GP Live Export', 'gp-live-export' );
echo '</h1>';
echo '<hr class="wp-header-end">';
$projects = GP::$project->top_level();
if ( empty( $projects ) ) {
printf(
/* Translators: %s: GlotPress dashboard link */
esc_html__( 'Let\'s go to your %s and set up your first project!', 'gp-live-export' ),
sprintf (
'<a href="%1$s">%2$s</a>',
gp_url_public_root(),
esc_html__( 'GlotPress dashboard', 'gp-live-export' )
)
);
echo '<br>';
printf(
/* Translators: %s: page link */
esc_html__( 'For setting up WordPress core/theme/plugin translation, you can find more instruction on %s.', 'gp-live-export' ),
sprintf (
'<a href="%1$s">%2$s</a>',
esc_html__( 'https://wordpress.org', 'gp-live-export' ) . '/plugins/gp-live-export/#faq',
esc_html__( 'WordPress.org Plugin page', 'gp-live-export' )
)
);
} else {
echo '<table class="wp-list-table widefat"><thead><tr>';
echo '<th>'. esc_html__( 'Parent', 'gp-live-export' ) .'</th>';
echo '<th>'. esc_html__( 'Name', 'gp-live-export' ) .'</th>';
echo '<th>'. esc_html__( 'Export', 'gp-live-export' ) .'</th>';
echo '</tr></thead><tbody>';
foreach ( $projects as $project ):
echo '<tr><td>'.__( 'N/A', 'gp-live-export' ).'</td><td>';
gp_link_project( $project->path, esc_html( $project->name ) );
echo '</td><td>';
$this->gp_link_export_project( $project );
echo '</td></tr>';
if ( $sub_projects = $project->sub_projects() ) {
foreach ( $sub_projects as $sub_project ):
echo
'<tr>'
.'<td>'.$project->name.'</td>'
.'<td>'
;
gp_link_project( $sub_project->path, esc_html( $sub_project->name ) );
echo
'</td>'
.'<td>';
$this->gp_link_export_project ( $sub_project );
echo '</td>'
.'</tr>'
;
endforeach;
}
endforeach;
echo '</tbody></table>';
} // endif;
echo '</div>';
}
/**
* Generate list of translation project.
*/
function gp_link_export_project ( $project ) {
if ( $translation_sets = GP::$translation_set->by_project_id( $project->id ) ) :
$project_path = $project->path;
foreach ( $translation_sets as $translation_set ):
$admin_url = 'options-general.php?page=gp-live-export';
$admin_url = ( $project = $project_path ) ? $admin_url.'&project='.$project : $admin_url;
$admin_url = ( $locale = $translation_set->locale ) ? $admin_url.'&locale='.$locale : $admin_url;
$admin_url = ( $set = $translation_set->slug ) ? $admin_url.'&set='.$set : $admin_url;
printf ( '<a href="%1$s" class="button">%2$s</a>',
esc_url ( admin_url ( $admin_url ) ),
GP_Locales::by_slug( $locale )->wp_locale . ' ('.$set.')'
);
endforeach;
else :
esc_html_e( 'No translation set found.', 'gp-live-export' );
endif;
}
/**
* Display notification on Admin screen.
*/
function admin_notice() {
foreach ( $this->message as $message ) {
?>
<div class="notice notice-<?php echo $message['status']; ?> is-dismissible">
<p><?php
switch ( $message['status'] ) {
case 'error':
printf(
/* Translators: %1$s: error sentence, %2$s: project name */
'%1$s: %2$s',
$message['content'],
'<b>' . $message['path'] . '</b>'
);
break;
case 'success':
printf(
esc_html__( "%s has been exported!", 'gp-live-export' ),
'<b>' . $message['path'] . '</b>'
);
break;
}
?></p>
</div>
<?php
}
}
}
new GPLE_Options_Page;