PluginProbe
Translation with DeepL API / trunk
Translation with DeepL API vtrunk
trunk 0.1.20180323 1.0 1.2.5.1 1.5.2.5 1.7.5 2.4.3.12 2.4.3.9 2.4.5
wpdeepl / deepl-configuration.class.php

deepl-configuration.class.php in Translation with DeepL API trunk, at deepl-configuration.class.php

369 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3 class DeeplConfiguration {
4 static function getAPIKey() {
5 return apply_filters( 'wpdeepl_' . __METHOD__, trim( get_option( 'wpdeepl_api_key' ) ) );
6 }
7 static function getAPIServer() {
8 $choice = get_option( 'wpdeepl_api_server');
9 if ( !$choice ) {
10 $choice = 'paid_api';
11 }
12 $possibilities = self::getDeepLAPIServers();
13
14 if ( isset( $possibilities[$choice] ) ) {
15 return apply_filters( 'wpdeepl_' . __METHOD__, $possibilities[$choice]['server'], $choice, $possibilities );
16 }
17 else {
18 return apply_filters( 'wpdeepl_' . __METHOD__, false, $choice, $possibilities );
19 }
20 }
21
22 static function getDeeplAPIServers() {
23 $array = array(
24 'paid_api' => array(
25 'server' => 'https://api.deepl.com/v2/',
26 'description' => __('Regular paid API Plan (https://api.deepl.com/v2/)', 'wpdeepl')
27 ),
28 'free_plan' => array(
29 'server' => 'https://api-free.deepl.com/v2/',
30 'description' => __('Free API plan (https://api-free.deepl.com)', 'wpdeepl' )
31 ),
32 );
33 return $array;
34 }
35
36 static function getLocaleNameForLn( $ln, $type = 'source ' ) {
37 $wp_locale = get_locale();
38 $languages_data = self::DefaultsAllLanguages();
39 foreach( $languages_data as $locale => $locale_data ) {
40 $compare = $type == 'target' ? 'astarget' : 'assource';
41 if( strtolower( $locale_data[$compare] ) == $ln ) {
42 return $locale_data['labels'][$wp_locale];
43 }
44 }
45 return false;
46 }
47
48 static function usingGlossaries() {
49 return get_option('wpdeepl_glossaries') && is_array( get_option('wpdeepl_glossaries') );
50 }
51
52 static function getLocaleNameForIsoCode2( $isocode2, $type = 'source ' ) {
53 $wp_locale = get_locale();
54 $languages_data = self::DefaultsAllLanguages();
55 foreach( $languages_data as $locale => $locale_data ) {
56 if( strtolower( $locale_data['isocode'] ) == $isocode2 ) {
57 $label = $locale_data['labels'][$wp_locale];
58 $label = preg_replace('#\(.*?\)#ism', '', $label );
59 return trim( $label );
60 }
61 }
62 return false;
63 }
64
65 static function getLogLevel() {
66 return apply_filters( 'wpdeepl_' . __METHOD__, get_option( 'wpdeepl_log_level') );
67 }
68
69 static function getActivePostTypes() {
70 $return = get_option( 'wpdeepl_metabox_post_types' ) ? get_option( 'wpdeepl_metabox_post_types' ) : array();
71 $return = get_option( 'wpdeepl_pro_post_types' ) ? array_merge( $return, get_option( 'wpdeepl_pro_post_types' ) ) : $return;
72
73 $return = array_unique( $return );
74 return apply_filters( 'wpdeepl_' . __METHOD__, $return );
75
76 }
77 static function getMetaBoxPostTypes() {
78 $post_types = get_option( 'wpdeepl_metabox_post_types' );
79 if ( empty( $post_types ) ) {
80 $post_types = array( 'post', 'page' );
81 }
82 return apply_filters( 'wpdeepl_' . __METHOD__, $post_types );
83 }
84 static function getMetaBoxDefaultBehaviour() {
85 return 'replace';
86 return apply_filters( 'wpdeepl_' . __METHOD__, get_option( 'wpdeepl_metabox_behaviour' ) );
87 }
88 static function getDefaultTargetLanguage() {
89 return apply_filters( 'wpdeepl_' . __METHOD__, get_option( 'wpdeepl_default_language' ) );
90 }
91
92 static function getDisplayedLanguages() {
93 $value = get_option( 'wpdeepl_displayed_languages') ;
94 return apply_filters( 'wpdeepl_' . __METHOD__, $value );
95 }
96 static function getMetaBoxContext() {
97 $context = get_option( 'wpdeepl_metabox_context' );
98 if ( empty( $context ) ) {
99 $context = 'side';
100 }
101 return apply_filters( 'wpdeepl_' . __METHOD__, $context );
102 }
103 static function getMetaBoxPriority() {
104 $priority = get_option( 'wpdeepl_metabox_priority' );
105 if ( empty( $priority ) ) {
106 $priority = 'high';
107 }
108 return apply_filters( 'wpdeepl_' . __METHOD__, $priority );
109 }
110
111 static function usingMultilingualPlugins() {
112 return false;
113 }
114
115 static function getNonceAction() {
116 return apply_filters( 'wpdeepl_' . __METHOD__, 'deepl_translate_post' );
117 }
118
119 static function getActiveGlossaryFor( $source_language, $target_language) {
120 $source = !empty( $source_language ) ? strtolower( substr( $source_language, 0, 2 ) ) : false;
121 $target = !empty( $target_language ) ? strtolower( substr( $target_language, 0, 2 ) ) : false;
122 $active_glossary_id = false;
123 if( $source && $target) $active_glossary_id = get_option( 'wpdeepl_glossary_' . $source. '_' . $target );
124 return apply_filters( 'wpdeepl_' . __METHOD__, $active_glossary_id );
125 }
126
127
128
129
130 /**
131 * 0 = activated
132 * 1 = activated and setup
133 */
134 static function isPluginInstalled() {
135 return get_option( 'wpdeepl_plugin_installed' );
136 }
137
138
139 static function validateLang( $language_string, $output = 'assource' ) {
140 // output source will return 2 characters
141 // output source will return AA(-AA)
142
143 $language_string = htmlspecialchars( $language_string );
144 $language_string = str_replace( '-', '_', $language_string );
145
146 if( $language_string == 'NB' ) {
147 $language_string = 'NO';
148 }
149
150 $all_languages = DeeplConfiguration::DefaultsAllLanguages();
151 $locale = get_locale();
152 //wpdeepl_debug_display( $locale, "\n locale" );
153
154
155 //if( $language_string == 'nn_NO' ) $language_string = 'no_NO';
156
157 if( strpos( $locale, '_formal' ) != false ) {
158 $locale = str_replace( '_formal', '', $locale );
159 }
160 if( strpos( $locale, '_informal' ) != false ) {
161 $locale = str_replace( '_informal', '', $locale );
162 }
163
164 $language = false;
165 if( $output == 'astarget' ) {
166 //wpdeepl_debug_display( $all_languages[$language_string]," lang $language_string locale $locale"); wpdeepl_debug_display( $all_languages);
167 }
168 if ( isset( $all_languages[$language_string] ) ) {
169 $language = $all_languages[$language_string];
170 }
171 else {
172 foreach ( $all_languages as $try_locale => $try_language ) {
173 if ( $try_language['allcaps'] == strtoupper( $language_string ) ) {
174 $language = $try_language;
175 break;
176 }
177 if ( $try_language['isocode'] == strtoupper( $language_string ) ) {
178 $language = $try_language;
179 break;
180 }
181 if( $output == 'astarget' && $try_language['astarget'] == strtoupper( $language_string ) ) {
182 $language = $try_language;
183 break;
184 }
185 }
186 }
187
188 if ( !$language ) {
189 return false;
190 }
191
192 if( $output == 'astarget' ) {
193 //wpdeepl_debug_display( $language, "language, locale $locale");
194 }
195 $language['label'] = isset( $language['labels'][$locale] ) ? $language['labels'][$locale] : false;
196
197 if ( $output == 'assource' ) {
198 return $language['assource'];
199 }
200 elseif ( $output == 'astarget' ) {
201 return $language['astarget'];
202 }
203 elseif ( $output == 'isocode' ) {
204 return $language['isocode'];
205 }
206 elseif ( $output == 'full' ) {
207 return $language;
208 }
209 elseif ( $output == 'label' ) {
210
211 return $language['label'];
212 }
213 else {
214 return $language['isocode'];
215 }
216 }
217 static function DefaultsMetaboxBehaviours() {
218 $array = array(
219 'replace' => __( 'Replace content', 'wpdeepl' ),
220 'append' => __( 'Append to content', 'wpdeepl' )
221 );
222 return apply_filters( 'wpdeepl_' . __METHOD__ , $array );
223 }
224
225 static function getFormalityLevel( $target_lang = false ) {
226 if ( $target_lang ) {
227 $formality_level = get_option('wpdeepl_formality_' . $target_lang );
228 }
229 if( !$formality_level || $formality_level == 'wpdeepl' ) {
230 $formality_level = get_option( 'wpdeepl_default_formality' );
231 }
232
233 /**
234 * . This feature currently only works for target languages DE (German), FR (French), IT (Italian), ES (Spanish), NL (Dutch), PL (Polish), PT-PT, PT-BR (Portuguese) and RU (Russian). Setting this parameter with a target language that does not support formality will fail, unless one of the prefer_... options are used. Possible options are:
235 * default (default)
236 more - for a more formal language
237 less - for a more informal language
238 prefer_more - for a more formal language if available, otherwise fallback to default formality
239 prefer_less - for a more informal language if available, otherwise fallback to default formality
240 * */
241
242 if( $formality_level != 'wpdeepl' ) {
243 $formality_level = 'prefer_' . $formality_level;
244 }
245
246 return apply_filters( 'wpdeepl_' . __METHOD__, $formality_level, $target_lang );
247 }
248
249 static function getLanguagesAllowingFormality() {
250 // This feature currently works for target languages DE, FR, IT, ES, NL, PL, PT-PT, PT-BR, RU, JA.
251 return array('de_DE', 'fr_FR', 'it_IT', 'es_ES', 'es_419', 'nl_NL', 'ja_JP', 'pl_PL', 'pt_PT', 'pt_BR', 'ru_RU' );
252 }
253
254 static function DefaultsISOCodes() {
255 $locale = get_locale();
256 $locale = str_replace( '_formal', '', $locale );
257 $locale = str_replace( '_informal', '', $locale );
258 $all_languages = DeeplConfiguration::DefaultsAllLanguages();
259 $languages = array();
260 foreach ( $all_languages as $isocode => $labels ) {
261 $languages[$isocode] = isset( $labels['labels']) && isset( $labels['labels'][$locale] ) ? $labels['labels'][$locale] : false;
262 }
263 return apply_filters( 'wpdeepl_' . __METHOD__ , $languages );
264 }
265
266 static function getLanguageFromIsoCode2( $isocode2, $context = 'target' ) {
267 $isocode2 = strtolower( $isocode2 );
268 $all_languages = self::DefaultsAllLanguages();
269 $key = ( $context == 'source' ) ? 'assource' : 'astarget';
270 foreach( $all_languages as $lang => $language ) {
271 $short = substr( strtolower( $language[$key] ), 0, 2 );
272 if( $short == $isocode2 ) {
273 return $lang;
274 }
275 }
276 return false;
277 }
278
279 static function DefaultsAllLanguages() {
280 $languages = array();
281
282 global $wp_filesystem;
283 if ( empty( $wp_filesystem ) ) {
284 require_once( ABSPATH . 'wp-admin/includes/file.php' );
285 if ( ! WP_Filesystem() ) {
286 return array(); // Retourne un tableau vide en cas d'échec
287 }
288 }
289 $file_path = wp_normalize_path( trailingslashit( WPDEEPL_PATH ) . 'languages.csv' );
290 $csv_contents = $wp_filesystem->get_contents( $file_path );
291 $csv_data = array();
292
293 if ( $csv_contents ) {
294 $lines = explode( "\n", $csv_contents );
295 foreach ( $lines as $line ) {
296 // Ignorer les lignes vides
297 $line = trim( $line );
298 if ( empty( $line ) ) {
299 continue;
300 }
301 $data = str_getcsv( $line, ',', '"', "\\" );
302
303 if ( is_array( $data ) && ! empty( $data ) ) {
304 $csv_data[] = $data;
305 }
306 }
307 }
308
309 //wpdeepl_debug_display( $csv_data, " alors data");
310 $headers = array_shift( $csv_data );
311 if( $csv_data ) foreach ( $csv_data as $labels ) {
312 $labels = array_combine( $headers, $labels );
313
314 $locale = $labels['locale'];
315 unset( $labels['locale'] );
316
317 $extra_country = false;
318 if ( strlen( $labels['astarget'] ) > 2 ) {
319 $extra_country = substr( $labels['astarget'], 3 );
320 }
321
322 $languages[$locale] = array();
323 foreach ( array('allcaps', 'assource', 'astarget', 'isocode' ) as $key ) {
324 $languages[$locale][$key] = $labels[$key];
325 unset( $labels[$key] );
326 }
327
328
329 if ( $extra_country ) {
330 foreach ( $labels as $code => $label ) {
331 $labels[$code] = $label . ' (' . $extra_country . ')';
332 }
333 }
334 $languages[$locale]['labels'] = $labels;
335 }
336
337 return apply_filters( 'wpdeepl_' . __METHOD__, $languages );
338
339 // used for.. something else
340 /* translators: strings used in extended functions. Target lang */
341 $test = __('Translate to %s', 'wpdeepl' );
342 /* translators: strings used in extended functions. Number of posts */
343 $test = __('Translated %d posts.', 'wpdeepl');
344 $test = __('Bulk translate', 'wpdeepl');
345 $test = __('Content types', 'wpdeepl');
346 $test = __( 'Translate contents', 'wpdeepl' );
347 $test = __( 'Select which kind of content you want to be able to translate', 'wpdeepl' );
348 $test = __( 'Target languages for bulk actions', 'wpdeepl' );
349 $test = __( 'Show these target languages in bulk menu', 'wpdeepl' );
350 }
351
352 // might serve somewhere
353 static function getContentTypes() {
354 return apply_filters( 'wpdeepl_' . __METHOD__, get_option('wpdeepl_contents_to_translate') );
355 }
356 static function getTargetLocales() {
357 return apply_filters( 'wpdeepl_' . __METHOD__, get_option( 'wpdeepl_target_locales') );
358 }
359
360 static function usingPolylang() {
361 return function_exists( 'pll_the_languages' );
362 }
363
364 static function getBulkTargetLanguages() {
365 return apply_filters( 'wpdeepl_' . __METHOD__, get_option( 'wpdeepl_bulk_target_locales' ) );
366 }
367 }
368
369