/var/www/html_sp/wp-content/plugins/loco-translate/src/api/Providers.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
<?php
/**
 * Third party API helpers
 */
abstract class Loco_api_Providers {


    
/**
     * Export API credentials for all supported APIs
     * @return array[]
     */
    
public static function export():array {
        
$apis = [];
        foreach( 
self::builtin() as $a ){
            
$hook 'loco_api_provider_'.$a['id'];
            
$apis[] = apply_filters($hook$a );
        }
        return 
apply_filters'loco_api_providers'$apis );
    }
    
    
    
/**
     * @return array[]
     */
    
public static function builtin():array {
        
$settings Loco_data_Settings::get();
        return  [
             [
                
'id' => 'deepl',
                
'name' => 'DeepL Translator',
                
'key' => $settings->offsetGet('deepl_api_key'),
                
'url' => 'https://www.deepl.com/translator',
            ],[
                
'id' => 'google',
                
'name' => 'Google Translate',
                
'key' => $settings->offsetGet('google_api_key'),
                
'url' => 'https://translate.google.com/',
            ],[
                
'id' => 'microsoft',
                
'name' => 'Microsoft Translator',
                
'key' => $settings->offsetGet('microsoft_api_key'),
                
'region' => $settings->offsetGet('microsoft_api_region'),
                
'url' => 'https://aka.ms/MicrosoftTranslatorAttribution',
            ],[
                
'id' => 'lecto',
                
'name' => 'Lecto AI',
                
'key' => $settings->offsetGet('lecto_api_key'),
                
'url' => 'https://lecto.ai/?ref=loco',
            ],[
                
'id' => 'openai',
                
'name' => 'OpenAI',
                
'key' => $settings->offsetGet('openai_api_key'),
                
'model' => $settings->offsetGet('openai_api_model'),
                
'prompt' => $settings->offsetGet('openai_api_prompt'),
                
'url' => 'https://openai.com/policies/usage-policies/',
            ]
        ];
    }
    
    
    
/**
     * Get only configured APIs, and sort them fairly
     * @return array[]
     */
    
public static function configured():array {
        return 
self::sortarray_filterself::export(), [__CLASS__,'filterConfigured'] ) );
    }


    
/**
     * @internal
     * @param $api string[]
     */
    
private static function filterConfigured( array $api ):bool {
        return 
array_key_exists('key',$api) && is_string($api['key']) && '' !== $api['key'];
    }


    
/**
     * @internal
     * @param string[] $a
     * @param string[] $b
     * @return int
     */
    
private static function compareNames( array $a, array $b ):int {
        return 
strcasecmp($a['name'],$b['name']);
    }
    
    
    
/**
     * Sort providers alphabetically
     * @param array[] $apis
     */
    
public static function sort( array $apis ):array {
        
usort$apis, [__CLASS__,'compareNames'] );
        return 
$apis;
    }

}