PluginProbe
Translation with DeepL API / 2.4.5
Translation with DeepL API v2.4.5
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 2.4.5, at deepl-configuration.class.php

341 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class DeepLConfiguration {
4 static function getAPIKey() {
5 return apply_filters( __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( __METHOD__, $possibilities[$choice]['server'], $choice, $possibilities );
16 }
17 else {
18 return apply_filters( __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( __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( __METHOD__, $return );
75
76 }
77 static function getMetaBoxPostTypes() {
78 return apply_filters( __METHOD__, get_option( 'wpdeepl_metabox_post_types' ) );
79 }
80 static function getMetaBoxDefaultBehaviour() {
81 return 'replace';
82 return apply_filters( __METHOD__, get_option( 'wpdeepl_metabox_behaviour' ) );
83 }
84 static function getDefaultTargetLanguage() {
85 return apply_filters( __METHOD__, get_option( 'wpdeepl_default_language' ) );
86 }
87
88 static function getDisplayedLanguages() {
89 $value = get_option( 'wpdeepl_displayed_languages') ;
90 return apply_filters( __METHOD__, $value );
91 }
92 static function getMetaBoxContext() {
93 return apply_filters( __METHOD__, get_option( 'wpdeepl_metabox_context' ) );
94 }
95 static function getMetaBoxPriority() {
96 return apply_filters( __METHOD__, get_option( 'wpdeepl_metabox_priority' ) );
97 }
98
99 static function usingMultilingualPlugins() {
100 return false;
101 }
102
103 static function getNonceAction() {
104 return apply_filters( __METHOD__, 'deepl_translate_post' );
105 }
106
107 static function getActiveGlossaryFor( $source_language, $target_language) {
108 $source = !empty( $source_language ) ? strtolower( substr( $source_language, 0, 2 ) ) : false;
109 $target = !empty( $target_language ) ? strtolower( substr( $target_language, 0, 2 ) ) : false;
110 $active_glossary_id = false;
111 if( $source && $target) $active_glossary_id = get_option( 'wpdeepl_glossary_' . $source. '_' . $target );
112 return apply_filters( __METHOD__, $active_glossary_id );
113 }
114
115
116
117
118 /**
119 * 0 = activated
120 * 1 = activated and setup
121 */
122 static function isPluginInstalled() {
123 return get_option( 'wpdeepl_plugin_installed' );
124 }
125
126 static function execWorks() {
127 if ( exec( 'echo EXEC' ) == 'EXEC' ){
128 return true;
129 }
130 return false;
131 }
132
133 static function validateLang( $language_string, $output = 'assource' ) {
134 // output source will return 2 characters
135 // output source will return AA(-AA)
136
137 $language_string = htmlspecialchars( $language_string );
138 $language_string = str_replace( '-', '_', $language_string );
139
140 if( $language_string == 'NB' ) {
141 $language_string = 'NO';
142 }
143
144 $all_languages = DeepLConfiguration::DefaultsAllLanguages();
145 $locale = get_locale();
146 //plouf( $locale, "\n locale" );
147
148
149 //if( $language_string == 'nn_NO' ) $language_string = 'no_NO';
150
151 if( strpos( $locale, '_formal' ) != false ) {
152 $locale = str_replace( '_formal', '', $locale );
153 }
154 if( strpos( $locale, '_informal' ) != false ) {
155 $locale = str_replace( '_informal', '', $locale );
156 }
157
158 $language = false;
159 if( $output == 'astarget' ) {
160 //plouf( $all_languages[$language_string]," lang $language_string locale $locale"); plouf( $all_languages);
161 }
162 if ( isset( $all_languages[$language_string] ) ) {
163 $language = $all_languages[$language_string];
164 }
165 else {
166 foreach ( $all_languages as $try_locale => $try_language ) {
167 if ( $try_language['allcaps'] == strtoupper( $language_string ) ) {
168 $language = $try_language;
169 break;
170 }
171 if ( $try_language['isocode'] == strtoupper( $language_string ) ) {
172 $language = $try_language;
173 break;
174 }
175 if( $output == 'astarget' && $try_language['astarget'] == strtoupper( $language_string ) ) {
176 $language = $try_language;
177 break;
178 }
179 }
180 }
181
182 if ( !$language ) {
183 return false;
184 }
185
186 if( $output == 'astarget' ) {
187 //plouf( $language, "language, locale $locale");
188 }
189 $language['label'] = isset( $language['labels'][$locale] ) ? $language['labels'][$locale] : false;
190
191 if ( $output == 'assource' ) {
192 return $language['assource'];
193 }
194 elseif ( $output == 'astarget' ) {
195 return $language['astarget'];
196 }
197 elseif ( $output == 'isocode' ) {
198 return $language['isocode'];
199 }
200 elseif ( $output == 'full' ) {
201 return $language;
202 }
203 elseif ( $output == 'label' ) {
204
205 return $language['label'];
206 }
207 else {
208 return $language['isocode'];
209 }
210 }
211 static function DefaultsMetaboxBehaviours() {
212 $array = array(
213 'replace' => __( 'Replace content', 'wpdeepl' ),
214 'append' => __( 'Append to content', 'wpdeepl' )
215 );
216 return apply_filters( __METHOD__ , $array );
217 }
218
219 static function getFormalityLevel( $target_lang = false ) {
220 if ( $target_lang ) {
221 $formality_level = get_option('wpdeepl_formality_' . $target_lang );
222 }
223 if( !$formality_level || $formality_level == 'default' ) {
224 $formality_level = get_option( 'wpdeepl_default_formality' );
225 }
226
227 /**
228 * . 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:
229 * default (default)
230 more - for a more formal language
231 less - for a more informal language
232 prefer_more - for a more formal language if available, otherwise fallback to default formality
233 prefer_less - for a more informal language if available, otherwise fallback to default formality
234 * */
235
236 if( $formality_level != 'default' ) {
237 $formality_level = 'prefer_' . $formality_level;
238 }
239
240 return apply_filters( __METHOD__, $formality_level, $target_lang );
241 }
242
243 static function getLanguagesAllowingFormality() {
244 // 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).
245 return array('de_DE', 'fr_FR', 'it_IT', 'es_SP', 'nl_NL', 'pt_PT', 'pt_BR', 'ru_RU' );
246 }
247
248 static function DefaultsISOCodes() {
249 $locale = get_locale();
250 $locale = str_replace( '_formal', '', $locale );
251 $locale = str_replace( '_informal', '', $locale );
252 $all_languages = DeepLConfiguration::DefaultsAllLanguages();
253 $languages = array();
254 foreach ( $all_languages as $isocode => $labels ) {
255 $languages[$isocode] = isset( $labels['labels']) && isset( $labels['labels'][$locale] ) ? $labels['labels'][$locale] : false;
256 }
257 return apply_filters( __METHOD__ , $languages );
258 }
259
260 static function getLanguageFromIsoCode2( $isocode2, $context = 'target' ) {
261 $isocode2 = strtolower( $isocode2 );
262 $all_languages = self::DefaultsAllLanguages();
263 $key = ( $context == 'source' ) ? 'assource' : 'astarget';
264 foreach( $all_languages as $lang => $language ) {
265 $short = substr( strtolower( $language[$key] ), 0, 2 );
266 if( $short == $isocode2 ) {
267 return $lang;
268 }
269 }
270 return false;
271 }
272
273 static function DefaultsAllLanguages() {
274 $languages = array();
275
276 $handle = fopen( trailingslashit( WPDEEPL_PATH ) . 'languages.csv', 'r' );
277 $csv_data = array();
278 if ( $handle ) {
279 while( $data = fgetcsv( $handle, null, ',', '"' ) ) {
280 $csv_data[] = $data;
281 }
282 }
283 //plouf( $csv_data, " alors data");
284 $headers = array_shift( $csv_data );
285 if( $csv_data ) foreach ( $csv_data as $labels ) {
286 $labels = array_combine( $headers, $labels );
287
288 $locale = $labels['locale'];
289 unset( $labels['locale'] );
290
291 $extra_country = false;
292 if ( strlen( $labels['astarget'] ) > 2 ) {
293 $extra_country = substr( $labels['astarget'], 3, 2 );
294 }
295
296 $languages[$locale] = array();
297 foreach ( array('allcaps', 'assource', 'astarget', 'isocode' ) as $key ) {
298 $languages[$locale][$key] = $labels[$key];
299 unset( $labels[$key] );
300 }
301
302
303 if ( $extra_country ) {
304 foreach ( $labels as $code => $label ) {
305 $labels[$code] = $label . ' (' . $extra_country . ')';
306 }
307 }
308 $languages[$locale]['labels'] = $labels;
309 }
310
311 return apply_filters( __METHOD__, $languages );
312
313 // used for.. something else
314 $test = __('Translate to %s', 'wpdeepl' );
315 $test = __('Translated %d posts.', 'wpdeepl');
316 $test = __('Bulk translate', 'wpdeepl');
317 $test = __('Content types', 'wpdeepl');
318 $test = __( 'Translate contents', 'wpdeepl' );
319 $test = __( 'Select which kind of content you want to be able to translate', 'wpdeepl' );
320 $test = __( 'Target languages for bulk actions', 'wpdeepl' );
321 $test = __( 'Show these target languages in bulk menu', 'wpdeepl' );
322 }
323
324 // might serve somewhere
325 static function getContentTypes() {
326 return apply_filters( __METHOD__, get_option('wpdeepl_contents_to_translate') );
327 }
328 static function getTargetLocales() {
329 return apply_filters( __METHOD__, get_option( 'wpdeepl_target_locales') );
330 }
331
332 static function usingPolylang() {
333 return function_exists( 'pll_the_languages' );
334 }
335
336 static function getBulkTargetLanguages() {
337 return apply_filters( __METHOD__, get_option( 'wpdeepl_bulk_target_locales' ) );
338 }
339 }
340
341