auto-download-mo.php
10 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
global $sitepress, $WPML_ST_MO_Downloader;
$language = isset( $_GET['download_mo'] ) ? filter_var( $_GET['download_mo'], FILTER_SANITIZE_STRING ) : false;
$active_languages = $sitepress->get_active_languages();
$version = isset( $_GET['version'] ) ? filter_var( $_GET['version'], FILTER_SANITIZE_STRING ) : false;
$translations = array();
if ( isset( $active_languages[ $language ] ) ) {
try {
$WPML_ST_MO_Downloader->load_xml();
$WPML_ST_MO_Downloader->get_translation_files();
$types = array();
$version_projects = explode( ';', (string) $version );
foreach ( $version_projects as $project ) {
$exp = explode( '|', $project );
$types[] = $exp[0];
}
$translations = $WPML_ST_MO_Downloader->get_translations( $language, array( 'types' => $types ) );
} catch ( Exception $error ) {
$user_errors[] = $error->getMessage();
}
}
$user_messages = [];
if ( isset( $_POST['action'] ) && $_POST['action'] == 'icl_admo_add_translations' && wp_verify_nonce( $_POST['_wpnonce'], 'icl_adm_save_translations' ) ) {
$translations_add = array();
if ( ! empty( $_POST['add_new'] ) && array_key_exists( 'new', $translations ) ) {
$new_translations = $translations['new'];
foreach ( $new_translations as $tr ) {
$translations_add[] = array(
'string' => filter_var( $tr['string'], FILTER_SANITIZE_STRING ),
'translation' => filter_var( $tr['new'], FILTER_SANITIZE_STRING ),
'name' => filter_var( $tr['name'], FILTER_SANITIZE_STRING ),
'gettext_context' => filter_var( $tr['gettext_context'], FILTER_SANITIZE_STRING ),
);
}
if ( ! empty( $translations_add ) ) {
$user_messages[] = sprintf( _n( '%d new translation was added.', '%d new translations were added.', count( $translations_add ), 'wpml-string-translation' ), count( $translations_add ) );
}
}
if ( ! empty( $_POST['selected'] ) ) {
$translations_updated = 0;
foreach ( $_POST['selected'] as $idx => $v ) {
if ( ! empty( $v ) ) {
$translations_add[] = array(
'string' => filter_var( base64_decode( $_POST['string'][ $idx ] ), FILTER_SANITIZE_STRING ),
'translation' => filter_var( base64_decode( $_POST['translation'][ $idx ] ), FILTER_SANITIZE_STRING ),
'name' => filter_var( base64_decode( $_POST['name'] [ $idx ] ), FILTER_SANITIZE_STRING ),
'gettext_context' => filter_var( base64_decode( $_POST['gettext_context'][ $idx ] ), FILTER_SANITIZE_STRING ),
);
$translations_updated ++;
}
}
if ( $translations_updated ) {
$user_messages[] = sprintf( _n( '%d translation was updated.', '%d translations were updated.', $translations_updated, 'wpml-string-translation' ), $translations_updated );
}
}
if($translations_add){
$WPML_ST_MO_Downloader->save_translations($translations_add, $_POST['language'], $_POST['version']);
}else{
$user_errors[] = __('No action performed. Please select the strings that you need to update or add.', 'wpml-string-translation');
}
}
?>
<div class="wrap">
<h2><?php _e('Auto-download WordPress translations', 'wpml-string-translation') ?></h2>
<?php if(!empty($translations_updated) || !empty($translations_add)):?>
<p><strong><?php _e('Success!', 'wpml-string-translation') ?></strong></p>
<?php
foreach ( $user_messages as $umessage ) {
?>
<p><?php echo $umessage ?></p>
<?php
}
?>
<a href="<?php echo admin_url('admin.php?page=' . ICL_PLUGIN_FOLDER . '/menu/theme-localization.php'); ?>" class="button-secondary"><?php _e('Check other languages', 'wpml-string-translation') ?></a>
<?php elseif(!$version): ?>
<div class="error">
<p><?php _e('Missing version number for translation.', 'wpml-string-translation') ?></p>
</div>
<?php elseif(!isset($active_languages[$language])): ?>
<div class="error">
<p><?php printf(__('Invalid language: %s', 'wpml-string-translation'), $language) ?></p>
</div>
<?php elseif(!empty($user_errors)): ?>
<div class="error below-h2">
<?php foreach($user_errors as $uerror): ?>
<p><?php echo $uerror ?></p>
<?php endforeach; ?>
</div>
<?php elseif(!empty($translations)): ?>
<br />
<div class="icl_cyan_box">
<?php printf(__('This update includes %d new strings and %d updated strings. You can review the strings below. Then, go to the <a%s>bottom of this page</a> and click on the Proceed button.', 'wpml-string-translation'),
isset($translations['new']) ? count($translations['new']) : 0,
isset($translations['updated']) ? count($translations['updated']) : 0,
' href="#adm-proceed"');
?>
</div>
<form id="icl_admo_list" method="post" action="">
<input type="hidden" name="action" value="icl_admo_add_translations" />
<input type="hidden" name="language" value="<?php echo $language ?>" />
<input type="hidden" name="version" value="<?php echo $version ?>" />
<?php wp_nonce_field('icl_adm_save_translations'); ?>
<?php if(!empty($translations['updated'])): ?>
<h3><?php printf(__('Updated translations (%d)', 'wpml-string-translation'), count($translations['updated'])) ?></h3>
<table id="icl_admo_list_table" class="widefat">
<thead>
<tr>
<th class="manage-column column-cb check-column" scope="col"><input type="checkbox" name="" value="" checked="checked" /></th>
<th><?php _e('String', 'wpml-string-translation') ?></th>
<th style="text-align:center;"><?php _e('Existing translation', 'wpml-string-translation') ?></th>
<th style="text-align:center;"><?php _e('New translation', 'wpml-string-translation') ?></th>
</tr>
</thead>
<tbody>
<?php foreach($translations['updated'] as $idx => $translation): ?>
<tr>
<td class="column-cb">
<input type="hidden" name="selected[<?php echo $idx ?>]" value="0" />
<input type="checkbox" name="selected[<?php echo $idx ?>]" value="1" checked="checked" />
</td>
<td>
<?php echo esc_html($translation['string']) ?>
<input type="hidden" name="string[<?php echo $idx ?>]" value="<?php echo base64_encode($translation['string']); ?>" />
<input type="hidden" name="name[<?php echo $idx ?>]" value="<?php echo base64_encode($translation['name']); ?>" />
<input type="hidden" name="gettext_context[<?php echo $idx ?>]" value="<?php echo base64_encode($translation['gettext_context']); ?>" />
</td>
<td colspan="2">
<?php echo wp_text_diff($translation['translation'], $translation['new']); ?>
<input type="hidden" name="translation[<?php echo $idx ?>]" value="<?php echo base64_encode($translation['new']); ?>" />
</td>
</tr>
<?php $idx++; ?>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<th class="manage-column column-cb check-column" scope="col"><input type="checkbox" name="" value="" checked="checked" /></th>
<th><?php _e('String', 'wpml-string-translation') ?></th>
<th style="text-align:center;"><?php _e('Existing translation', 'wpml-string-translation') ?></th>
<th style="text-align:center;"><?php _e('New translation', 'wpml-string-translation') ?></th>
</tr>
</tfoot>
</table>
<?php endif; ?>
<?php if(!empty($translations['new'])): ?>
<h3><?php printf(__('New translations (%d)', 'wpml-string-translation'), count($translations['new'])) ?></h3>
<table class="widefat">
<thead>
<tr>
<th><?php _e('String', 'wpml-string-translation') ?></th>
<th><?php _e('Translation', 'wpml-string-translation') ?></th>
</tr>
</thead>
<tbody>
<?php foreach($translations['new'] as $idx => $translation): ?>
<tr>
<td>
<?php echo esc_html($translation['string']) ?>
</td>
<td><?php echo esc_html($translation['new']) ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<th><?php _e('String', 'wpml-string-translation') ?></th>
<th><?php _e('Translation', 'wpml-string-translation') ?></th>
</tr>
</tfoot>
</table>
<p>
<label><input type="checkbox" name="add_new" value="1" checked="checked" /> <?php _e('Add the new translations.', 'wpml-string-translation'); ?></label>
</p>
<?php endif; ?>
<a name="adm-proceed"></a>
<p class="submit">
<input type="submit" class="button-primary" value="<?php esc_attr_e('Proceed', 'wpml-string-translation') ?>" />
<a class="button-secondary" href="<?php echo admin_url('admin.php?page=' . ICL_PLUGIN_FOLDER . '/menu/theme-localization.php') ?>"><?php _e('Cancel', 'wpml-string-translation') ?></a>
</p>
</form>
<?php else: ?>
<p><?php _e('There is nothing to be updated or to be added.', 'wpml-string-translation') ?></p>
<p><a href="<?php echo admin_url('admin.php?page=' . ICL_PLUGIN_FOLDER . '/menu/theme-localization.php'); ?>" class="button-secondary"><?php _e('Check other languages', 'wpml-string-translation') ?></a></p>
<?php endif; ?>
</div>