Making WordPress.org

Changeset 12573


Ignore:
Timestamp:
05/05/2023 01:35:17 PM (3 years ago)
Author:
amieiro
Message:

Translate: Check if the OpenAI and DeepL API keys are correct or not and show an info message

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/templates/settings-edit.php

    r12511 r12573  
    2525
    2626$gp_external_translations = get_user_option( 'gp_external_translations' );
     27
     28$openai_key      = trim( gp_array_get( $gp_default_sort, 'openai_api_key' ) );
     29$openai_response = null;
     30if ( $openai_key ) {
     31        $openai_response = wp_remote_get(
     32                'https://api.openai.com/v1/usage?date=' . gmdate( 'Y-m-d' ),
     33                array(
     34                        'timeout' => 4,
     35                        'headers' => array(
     36                                'Content-Type'  => 'application/json',
     37                                'Authorization' => 'Bearer ' . $openai_key,
     38                        ),
     39                )
     40        );
     41}
     42$openai_response_code = wp_remote_retrieve_response_code( $openai_response );
     43
     44$deepl_key      = trim( gp_array_get( $gp_default_sort, 'deepl_api_key' ) );
     45$deepl_response = null;
     46if ( $deepl_key ) {
     47        $deepl_response = wp_remote_get(
     48                'https://api-free.deepl.com/v2/usage',
     49                array(
     50                        'timeout' => 4,
     51                        'headers' => array(
     52                                'Content-Type'  => 'application/json',
     53                                'Authorization' => 'DeepL-Auth-Key ' . $deepl_key,
     54                        ),
     55                )
     56        );
     57}
     58$deepl_response_code = wp_remote_retrieve_response_code( $deepl_response );
    2759?>
    2860
    … …  
    116148                                <br>
    117149                        </th>
    118                 <td><input type="text" class="openai_api_key" id="default_sort[openai_api_key]" name="default_sort[openai_api_key]" value="<?php echo esc_html( gp_array_get( $gp_default_sort, 'openai_api_key', '' ) ); ?>" placeholder="Enter your OpenAI API key" /></td>
     150                <td>
     151                        <input type="text" class="openai_api_key" id="default_sort[openai_api_key]" name="default_sort[openai_api_key]" value="<?php echo esc_html( gp_array_get( $gp_default_sort, 'openai_api_key', '' ) ); ?>" placeholder="Enter your OpenAI API key" />
     152                        <?php
     153                        if ( trim( $openai_key ) ) {
     154                                echo '<br>';
     155                                if ( 200 != $openai_response_code ) {
     156                                        echo '<small style="color:red;">';
     157                                        esc_html_e( 'Your OpenAI API Key is not correct.', 'glotpress' );
     158                                } else {
     159                                        echo '<small style="color:green;">';
     160                                        esc_html_e( 'Your OpenAI API Key is correct.', 'glotpress' );
     161                                }
     162                                echo '</small>';
     163                        }
     164                        ?>
     165                </td>
    119166        </tr>
    120167        <tr>
    … …  
    165212                                        </small></a>
    166213                        </th>
    167                 <td><input type="text" class="deepl_api_key" id="default_sort[deepl_api_key]" name="default_sort[deepl_api_key]" value="<?php echo esc_html( gp_array_get( $gp_default_sort, 'deepl_api_key' ) ); ?>" placeholder="Enter your DeepL API key" /></td>
     214                <td>
     215                        <input type="text" class="deepl_api_key" id="default_sort[deepl_api_key]" name="default_sort[deepl_api_key]" value="<?php echo esc_html( gp_array_get( $gp_default_sort, 'deepl_api_key' ) ); ?>" placeholder="Enter your DeepL API key" />
     216                        <?php
     217                        if ( trim( $deepl_key ) ) {
     218                                echo '<br>';
     219                                if ( 200 != $deepl_response_code ) {
     220                                        echo '<small style="color:red;">';
     221                                        esc_html_e( 'Your DeepL Free API Key is not correct.', 'glotpress' );
     222                                } else {
     223                                        echo '<small style="color:green;">';
     224                                        esc_html_e( 'Your DeepL Free API Key is correct.', 'glotpress' );
     225                                }
     226                                echo '</small>';
     227                        }
     228                        ?>
     229                </td>
    168230        </tr>
    169231</table>
Note: See TracChangeset for help on using the changeset viewer.