taxonomy-translation-display.class.php
12.1 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
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
<?php
new WPML_Taxonomy_Translation_Sync_Display();
class WPML_Taxonomy_Translation_Table_Display {
private static function get_strings_translation_array() {
$st_plugin = '<a href="' . get_admin_url( null, 'plugins.php' ) . '" target="_blank" class="wpml-external-link">WPML String Translation</a>';
$term_results_cap = defined( 'WPML_TAXONOMY_TRANSLATION_MAX_TERMS_RESULTS_SET' ) ?
WPML_TAXONOMY_TRANSLATION_MAX_TERMS_RESULTS_SET :
WPML_Taxonomy_Translation_Screen_Data::WPML_TAXONOMY_TRANSLATION_MAX_TERMS_RESULTS_SET;
$labels = array(
'Show' => __( 'Show', 'sitepress' ),
'untranslated' => __( 'untranslated', 'sitepress' ),
'all' => __( 'all', 'sitepress' ),
'in' => __( 'in', 'sitepress' ),
'to' => __( 'to', 'sitepress' ),
'of' => __( 'of', 'sitepress' ),
'taxonomy' => __( 'Taxonomy', 'sitepress' ),
'anyLang' => __( 'any language', 'sitepress' ),
'apply' => __( 'Refresh', 'sitepress' ),
'synchronizeBtn' => __( 'Update Taxonomy Hierarchy', 'sitepress' ),
'searchPlaceHolder' => __( 'Search', 'sitepress' ),
'selectParent' => __( 'select parent', 'sitepress' ),
'taxToTranslate' => __( 'Select the taxonomy to translate: ', 'sitepress' ),
'translate' => sprintf( __( '%1$s Translation', 'sitepress' ), '%taxonomy%' ),
'Synchronize' => __( 'Hierarchy Synchronization', 'sitepress' ),
'lowercaseTranslate' => __( 'translate', 'sitepress' ),
'copyToAllLanguages' => __( 'Copy to all languages', 'sitepress' ),
'copyToAllMessage' => sprintf( __( 'Copy this term from original: %1$s to all other languages?' ), '%language%' ),
'copyAllOverwrite' => __( 'Overwrite existing translations', 'sitepress' ),
'willBeRemoved' => __( 'Will be removed', 'sitepress' ),
'willBeAdded' => __( 'Will be added', 'sitepress' ),
'legend' => __( 'Legend:', 'sitepress' ),
'refLang' => sprintf( __( 'Synchronize taxonomy hierarchy according to: %1$s language.', 'sitepress' ), '%language%' ),
'targetLang' => __( 'Target Language', 'sitepress' ),
'termPopupDialogTitle' => __( 'Term translation', 'sitepress' ),
'originalTermPopupDialogTitle' => __( 'Original term', 'sitepress' ),
'labelPopupDialogTitle' => __( 'Label translation', 'sitepress' ),
'copyFromOriginal' => __( 'Copy from original', 'sitepress' ),
'original' => __( 'Original:', 'sitepress' ),
'translationTo' => __( 'Translation to:', 'sitepress' ),
'Name' => __( 'Name', 'sitepress' ),
'Slug' => __( 'Slug', 'sitepress' ),
'Description' => __( 'Description', 'sitepress' ),
'Ok' => __( 'OK', 'sitepress' ),
'save' => __( 'Save', 'sitepress' ),
'Singular' => __( 'Singular', 'sitepress' ),
'Plural' => __( 'Plural', 'sitepress' ),
'changeLanguage' => __( 'Change language', 'sitepress' ),
'cancel' => __( 'Cancel', 'sitepress' ),
'loading' => __( 'loading', 'sitepress' ),
'Save' => __( 'Save', 'sitepress' ),
'currentPage' => __( 'Current page', 'sitepress' ),
'goToPreviousPage' => __( 'Go to previous page', 'sitepress' ),
'goToNextPage' => __( 'Go to the next page', 'sitepress' ),
'goToFirstPage' => __( 'Go to the first page', 'sitepress' ),
'goToLastPage' => __( 'Go to the last page', 'sitepress' ),
'hieraSynced' => __( 'The taxonomy hierarchy is now synchronized.', 'sitepress' ),
'hieraAlreadySynced' => __( 'The taxonomy hierarchy is already synchronized.', 'sitepress' ),
'noTermsFound' => sprintf( __( 'No %1$s found.', 'sitepress' ), '%taxonomy%' ),
'items' => __( 'items', 'sitepress' ),
'item' => __( 'item', 'sitepress' ),
'summaryTerms' => sprintf( __( 'Translation of %1$s', 'sitepress' ), '%taxonomy%' ),
'summaryLabels' => sprintf( __( 'Translations of taxonomy %1$s labels and slug', 'sitepress' ), '%taxonomy%' ),
'activateStringTranslation' => sprintf( __( 'To translate taxonomy labels and slug you need %s plugin.', 'sitepress' ), $st_plugin ),
'preparingTermsData' => __( 'Loading ...', 'sitepress' ),
'firstColumnHeading' => sprintf( __( '%1$s terms (in original language)', 'sitepress' ), '%taxonomy%' ),
'resultsTruncated' => sprintf( __( 'Because too many %1$s were found, only the first %2$s results are listed. You can refine the results using the Search field below.', 'sitepress' ), '%taxonomy%', '<strong>' . $term_results_cap . '</strong>' ),
'wpml_save_term_nonce' => wp_create_nonce( 'wpml_save_term_nonce' ),
'wpml_tt_sync_hierarchy_nonce' => wp_create_nonce( 'wpml_tt_sync_hierarchy_nonce' ),
'wpml_generate_unique_slug_nonce' => wp_create_nonce( 'wpml_generate_unique_slug_nonce' ),
'wpml_taxonomy_translation_nonce' => wp_create_nonce( 'wpml_taxonomy_translation_nonce' ),
'addTranslation' => __( 'Add translation', 'sitepress' ),
'editTranslation' => __( 'Edit translation', 'sitepress' ),
'originalLanguage' => __( 'Original language', 'sitepress' ),
'termMetaLabel' => __( 'This term has additional meta fields:', 'sitepress' ),
);
return $labels;
}
public static function enqueue_taxonomy_table_resources( $sitepress ) {
WPML_Simple_Language_Selector::enqueue_scripts();
wp_enqueue_style( 'translate-taxonomy', ICL_PLUGIN_URL . '/res/css/taxonomy-translation.css', array(), ICL_SITEPRESS_VERSION );
$core_dependencies = array( 'jquery', 'jquery-ui-dialog', 'backbone', 'wpml-underscore-template-compiler' );
wp_register_script(
'templates-compiled',
ICL_PLUGIN_URL . '/res/js/taxonomy-translation/templates-compiled.js',
$core_dependencies,
'1.2.4'
);
$core_dependencies[] = 'templates-compiled';
wp_register_script( 'main-util', ICL_PLUGIN_URL . '/res/js/taxonomy-translation/util.js', $core_dependencies );
wp_register_script( 'main-model', ICL_PLUGIN_URL . '/res/js/taxonomy-translation/main.js', $core_dependencies );
$core_dependencies[] = 'main-model';
$dependencies = $core_dependencies;
wp_register_script(
'term-rows-collection',
ICL_PLUGIN_URL . '/res/js/taxonomy-translation/collections/term-rows.js',
array_merge( $core_dependencies, array( 'term-row-model' ) )
);
$dependencies[] = 'term-rows-collection';
wp_register_script(
'term-model',
ICL_PLUGIN_URL . '/res/js/taxonomy-translation/models/term.js',
$core_dependencies
);
$dependencies[] = 'term-model';
wp_register_script(
'taxonomy-model',
ICL_PLUGIN_URL . '/res/js/taxonomy-translation/models/taxonomy.js',
$core_dependencies
);
$dependencies[] = 'taxonomy-model';
wp_register_script(
'term-row-model',
ICL_PLUGIN_URL . '/res/js/taxonomy-translation/models/term-row.js',
$core_dependencies
);
$dependencies[] = 'term-row-model';
foreach ( array(
'filter-view',
'nav-view',
'table-view',
'taxonomy-view',
'term-popup-view',
'original-term-popup-view',
'label-popup-view',
'term-row-view',
'label-row-view',
'term-rows-view',
'term-view',
'term-original-view',
'copy-all-popup-view',
) as $script ) {
wp_register_script(
$script,
ICL_PLUGIN_URL . '/res/js/taxonomy-translation/views/' . $script . '.js',
$core_dependencies,
'1.2.4'
);
$dependencies[] = $script;
}
wp_localize_script( 'main-model', 'labels', self::get_strings_translation_array() );
wp_localize_script( 'main-model', 'wpml_taxonomies', self::wpml_get_table_taxonomies( $sitepress ) );
$need_enqueue = $dependencies;
$need_enqueue[] = 'main-model';
$need_enqueue[] = 'main-util';
$need_enqueue[] = 'templates';
foreach ( $need_enqueue as $handle ) {
wp_enqueue_script( $handle );
}
wp_register_script( 'taxonomy-hierarchy-sync-message', ICL_PLUGIN_URL . '/res/js/taxonomy-hierarchy-sync-message.js', array( 'jquery' ) );
wp_enqueue_script( 'taxonomy-hierarchy-sync-message' );
}
public static function wpml_get_table_taxonomies( SitePress $sitepress ) {
$taxonomies = $sitepress->get_wp_api()->get_taxonomies( array(), 'objects' );
$result = array(
'taxonomies' => array(),
'activeLanguages' => array(),
'allLanguages' => array(),
);
$sitepress->set_admin_language();
$active_langs = $sitepress->get_active_languages();
$default_lang = $sitepress->get_default_language();
$result['activeLanguages'][ $default_lang ] = array(
'label' => esc_js( $active_langs[ $default_lang ]['display_name'] ),
'flag' => esc_url( $sitepress->get_flag_url( $default_lang ) ),
);
foreach ( $active_langs as $code => $lang ) {
if ( $code !== $default_lang ) {
$result['activeLanguages'][ $code ] = array(
'label' => esc_js( $lang['display_name'] ),
'flag' => esc_url( $sitepress->get_flag_url( $code ) ),
);
}
}
$all_languages = $sitepress->get_languages();
foreach ( $all_languages as $code => $lang ) {
$result['allLanguages'][ $code ] = array(
'label' => esc_js( $lang['display_name'] ),
'flag' => esc_url( $sitepress->get_flag_url( $code ) ),
);
}
foreach ( $taxonomies as $key => $tax ) {
if ( $sitepress->is_translated_taxonomy( $key ) ) {
$result['taxonomies'][ $key ] = array(
'label' => $tax->label,
'singularLabel' => $tax->labels->singular_name,
'hierarchical' => $tax->hierarchical,
'name' => $key,
);
}
}
return $result;
}
public static function wpml_get_terms_and_labels_for_taxonomy_table() {
global $sitepress;
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'wpml_taxonomy_translation_nonce' ) ) {
wp_send_json_error( __( 'Wrong nonce', 'sitepress' ) );
return;
}
$taxonomy = false;
$request_post_taxonomy = filter_input(
INPUT_POST,
'taxonomy',
FILTER_SANITIZE_FULL_SPECIAL_CHARS,
FILTER_NULL_ON_FAILURE
);
if ( $request_post_taxonomy ) {
$taxonomy = html_entity_decode( $request_post_taxonomy );
}
if ( $taxonomy ) {
$terms_data = new WPML_Taxonomy_Translation_Screen_Data( $sitepress, $taxonomy );
$term_results = $terms_data->terms();
$labels = apply_filters( 'wpml_label_translation_data', false, $taxonomy );
$def_lang = $sitepress->get_default_language();
$bottom_content = apply_filters( 'wpml_taxonomy_translation_bottom', $html = '', $taxonomy, get_taxonomy( $taxonomy ) );
wp_send_json(
array(
'terms' => $term_results['terms'],
'resultsTruncated' => $term_results['truncated'],
'taxLabelTranslations' => $labels,
'defaultLanguage' => $def_lang,
'bottomContent' => $bottom_content,
'taxLangSelector' => self::render_tax_language_selector( $labels, $taxonomy ),
)
);
} else {
wp_send_json_error();
}
}
private static function render_tax_language_selector( $labels, $taxonomy ) {
global $sitepress;
if ( ! isset( $labels['st_default_lang'] ) ) {
return null;
}
$args = array(
'selected' => $labels['st_default_lang'],
'name' => 'string_lang[' . $taxonomy . ']',
'show_please_select' => false,
'echo' => false,
'class' => 'js-tax-lang-selector',
);
$lang_selector = new WPML_Simple_Language_Selector( $sitepress );
return $lang_selector->render( $args );
}
}