| @@ -1,220 +1,220 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -// If this file is called directly, abort. | |
| 4 | -if ( ! defined( 'WPINC' ) ) { | |
| 5 | - die; | |
| 6 | -} | |
| 7 | - | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * Get data from wpLingua API : API key validation | |
| 11 | - * | |
| 12 | - * Return an error message with error code | |
| 13 | - * or return the basic information of the API key | |
| 14 | - * | |
| 15 | - * API terms : https://wplingua.com/terms/ | |
| 16 | - * | |
| 17 | - * --------------------------------------------------- | |
| 18 | - * Data sent : | |
| 19 | - * --------------------------------------------------- | |
| 20 | - * - request : 'api_key' | |
| 21 | - * - version : API compatile version | |
| 22 | - * - context : Sends page calling URL | |
| 23 | - * - api_key : the API key of website | |
| 24 | - * | |
| 25 | - * --------------------------------------------------- | |
| 26 | - * Data received if successful : | |
| 27 | - * --------------------------------------------------- | |
| 28 | - * - language_original : A language ID | |
| 29 | - * - languages_target : An array of languages ID | |
| 30 | - * - features : Array of allowed API features | |
| 31 | - * - status : FREE | PREMIUM | VIP | |
| 32 | - * - expiration : A date dd/mm/yyyy | |
| 33 | - * | |
| 34 | - * --------------------------------------------------- | |
| 35 | - * Data received in case of failure | |
| 36 | - * --------------------------------------------------- | |
| 37 | - * - error : true | |
| 38 | - * - code : An integer | |
| 39 | - * - message : Error description | |
| 40 | - * | |
| 41 | - * @param string $api_key | |
| 42 | - * @return array | |
| 43 | - */ | |
| 44 | -function wplng_api_call_validate_api_key( $api_key = '' ) { | |
| 45 | - | |
| 46 | - /** | |
| 47 | - * Get and check the API key | |
| 48 | - */ | |
| 49 | - | |
| 50 | - if ( empty( $api_key ) ) { | |
| 51 | - | |
| 52 | - $api_key = wplng_get_api_key(); | |
| 53 | - | |
| 54 | - if ( empty( $api_key ) ) { | |
| 55 | - return array(); | |
| 56 | - } | |
| 57 | - } | |
| 58 | - | |
| 59 | - if ( ! wplng_is_valid_api_key_format( $api_key ) ) { | |
| 60 | - return array(); | |
| 61 | - } | |
| 62 | - | |
| 63 | - /** | |
| 64 | - * Get the API call | |
| 65 | - */ | |
| 66 | - | |
| 67 | - $request = wp_remote_post( | |
| 68 | - WPLNG_API_URL . '/app/', | |
| 69 | - array( | |
| 70 | - 'method' => 'POST', | |
| 71 | - 'timeout' => 5, | |
| 72 | - 'sslverify' => WPLNG_API_SSLVERIFY, | |
| 73 | - 'body' => array( | |
| 74 | - 'request' => 'api_key', | |
| 75 | - 'version' => WPLNG_API_VERSION, | |
| 76 | - 'context' => wplng_get_context(), | |
| 77 | - 'api_key' => $api_key, | |
| 78 | - ), | |
| 79 | - ) | |
| 80 | - ); | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Check if the API call worked | |
| 84 | - */ | |
| 85 | - | |
| 86 | - if ( is_wp_error( $request ) | |
| 87 | - || wp_remote_retrieve_response_code( $request ) !== 200 | |
| 88 | - ) { | |
| 89 | - return array(); | |
| 90 | - } | |
| 91 | - | |
| 92 | - /** | |
| 93 | - * Check and sanitize the API response | |
| 94 | - */ | |
| 95 | - | |
| 96 | - $response_checked = array(); | |
| 97 | - $response = json_decode( | |
| 98 | - wp_remote_retrieve_body( $request ), | |
| 99 | - true | |
| 100 | - ); | |
| 101 | - | |
| 102 | - if ( ! empty( $response['language_original'] ) | |
| 103 | - && ( | |
| 104 | - wplng_is_valid_language_id( $response['language_original'] ) | |
| 105 | - || 'all' === $response['language_original'] | |
| 106 | - ) | |
| 107 | - && ! empty( $response['languages_target'] ) | |
| 108 | - && is_array( $response['languages_target'] ) | |
| 109 | - && isset( $response['features'] ) | |
| 110 | - && is_array( $response['features'] ) | |
| 111 | - ) { | |
| 112 | - | |
| 113 | - /** | |
| 114 | - * API returned valid key informations | |
| 115 | - */ | |
| 116 | - | |
| 117 | - // Sanitize languages target | |
| 118 | - | |
| 119 | - $languages_target = array(); | |
| 120 | - | |
| 121 | - foreach ( $response['languages_target'] as $id ) { | |
| 122 | - | |
| 123 | - if ( ! wplng_is_valid_language_id( $id ) ) { | |
| 124 | - continue; | |
| 125 | - } | |
| 126 | - | |
| 127 | - $languages_target[] = sanitize_key( $id ); | |
| 128 | - } | |
| 129 | - | |
| 130 | - // Sanitize features list | |
| 131 | - | |
| 132 | - $features = array(); | |
| 133 | - | |
| 134 | - foreach ( $response['features'] as $key => $allow ) { | |
| 135 | - | |
| 136 | - if ( ! is_string( $key ) || ! is_bool( $allow ) ) { | |
| 137 | - continue; | |
| 138 | - } | |
| 139 | - | |
| 140 | - $key = sanitize_key( $key ); | |
| 141 | - $allow = ( true === $allow ); | |
| 142 | - | |
| 143 | - $features[ $key ] = $allow; | |
| 144 | - } | |
| 145 | - | |
| 146 | - // Sanitize status | |
| 147 | - | |
| 148 | - $status = 'FREE'; | |
| 149 | - | |
| 150 | - if ( ! empty( $response['status'] ) | |
| 151 | - && ( | |
| 152 | - 'PREMIUM' === $response['status'] | |
| 153 | - || 'VIP' === $response['status'] | |
| 154 | - ) | |
| 155 | - ) { | |
| 156 | - $status = $response['status']; | |
| 157 | - } | |
| 158 | - | |
| 159 | - // Make the checked response | |
| 160 | - | |
| 161 | - $response_checked = array( | |
| 162 | - 'language_original' => sanitize_key( $response['language_original'] ), | |
| 163 | - 'languages_target' => $languages_target, | |
| 164 | - 'features' => $features, | |
| 165 | - 'status' => $status, | |
| 166 | - ); | |
| 167 | - | |
| 168 | - // Add expiration | |
| 169 | - | |
| 170 | - if ( ! empty( $response['expiration'] ) | |
| 171 | - && is_string( $response['expiration'] ) | |
| 172 | - ) { | |
| 173 | - $response_checked['expiration'] = $response['expiration']; | |
| 174 | - } | |
| 175 | - } elseif ( isset( $response['error'] ) | |
| 176 | - && ( true === $response['error'] ) | |
| 177 | - && isset( $response['code'] ) | |
| 178 | - && is_int( $response['code'] ) | |
| 179 | - && isset( $response['message'] ) | |
| 180 | - && is_string( $response['message'] ) | |
| 181 | - ) { | |
| 182 | - | |
| 183 | - /** | |
| 184 | - * API returning a valid error | |
| 185 | - */ | |
| 186 | - | |
| 187 | - $error_message = __( 'Code', 'wplingua' ) . ' '; | |
| 188 | - $error_message .= $response['code'] . ' - '; | |
| 189 | - $error_message .= $response['message']; | |
| 190 | - | |
| 191 | - set_transient( | |
| 192 | - 'wplng_api_key_error', | |
| 193 | - $error_message, | |
| 194 | - MINUTE_IN_SECONDS * 5 | |
| 195 | - ); | |
| 196 | - | |
| 197 | - if ( isset( $response['disconnect'] ) | |
| 198 | - && true === $response['disconnect'] | |
| 199 | - ) { | |
| 200 | - delete_option( 'wplng_api_key_data' ); | |
| 201 | - delete_option( 'wplng_api_key' ); | |
| 202 | - wplng_clear_translations_cache(); | |
| 203 | - wplng_clear_slugs_cache(); | |
| 204 | - } | |
| 205 | - } else { | |
| 206 | - | |
| 207 | - /** | |
| 208 | - * API returned an unexpected response | |
| 209 | - */ | |
| 210 | - | |
| 211 | - set_transient( | |
| 212 | - 'wplng_api_key_error', | |
| 213 | - __( 'API returned an unexpected response.', 'wplingua' ), | |
| 214 | - MINUTE_IN_SECONDS * 5 | |
| 215 | - ); | |
| 216 | - | |
| 217 | - } | |
| 218 | - | |
| 219 | - return $response_checked; | |
| 220 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +// If this file is called directly, abort. | |
| 4 | +if ( ! defined( 'WPINC' ) ) { | |
| 5 | + die; | |
| 6 | +} | |
| 7 | + | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Get data from wpLingua API : API key validation | |
| 11 | + * | |
| 12 | + * Return an error message with error code | |
| 13 | + * or return the basic information of the API key | |
| 14 | + * | |
| 15 | + * API terms : https://wplingua.com/terms/ | |
| 16 | + * | |
| 17 | + * --------------------------------------------------- | |
| 18 | + * Data sent : | |
| 19 | + * --------------------------------------------------- | |
| 20 | + * - request : 'api_key' | |
| 21 | + * - version : API compatile version | |
| 22 | + * - context : Sends page calling URL | |
| 23 | + * - api_key : the API key of website | |
| 24 | + * | |
| 25 | + * --------------------------------------------------- | |
| 26 | + * Data received if successful : | |
| 27 | + * --------------------------------------------------- | |
| 28 | + * - language_original : A language ID | |
| 29 | + * - languages_target : An array of languages ID | |
| 30 | + * - features : Array of allowed API features | |
| 31 | + * - status : FREE | PREMIUM | VIP | |
| 32 | + * - expiration : A date dd/mm/yyyy | |
| 33 | + * | |
| 34 | + * --------------------------------------------------- | |
| 35 | + * Data received in case of failure | |
| 36 | + * --------------------------------------------------- | |
| 37 | + * - error : true | |
| 38 | + * - code : An integer | |
| 39 | + * - message : Error description | |
| 40 | + * | |
| 41 | + * @param string $api_key | |
| 42 | + * @return array | |
| 43 | + */ | |
| 44 | +function wplng_api_call_validate_api_key( $api_key = '' ) { | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Get and check the API key | |
| 48 | + */ | |
| 49 | + | |
| 50 | + if ( empty( $api_key ) ) { | |
| 51 | + | |
| 52 | + $api_key = wplng_get_api_key(); | |
| 53 | + | |
| 54 | + if ( empty( $api_key ) ) { | |
| 55 | + return array(); | |
| 56 | + } | |
| 57 | + } | |
| 58 | + | |
| 59 | + if ( ! wplng_is_valid_api_key_format( $api_key ) ) { | |
| 60 | + return array(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Get the API call | |
| 65 | + */ | |
| 66 | + | |
| 67 | + $request = wp_remote_post( | |
| 68 | + WPLNG_API_URL . '/app/', | |
| 69 | + array( | |
| 70 | + 'method' => 'POST', | |
| 71 | + 'timeout' => 5, | |
| 72 | + 'sslverify' => WPLNG_API_SSLVERIFY, | |
| 73 | + 'body' => array( | |
| 74 | + 'request' => 'api_key', | |
| 75 | + 'version' => WPLNG_API_VERSION, | |
| 76 | + 'context' => wplng_get_context(), | |
| 77 | + 'api_key' => $api_key, | |
| 78 | + ), | |
| 79 | + ) | |
| 80 | + ); | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Check if the API call worked | |
| 84 | + */ | |
| 85 | + | |
| 86 | + if ( is_wp_error( $request ) | |
| 87 | + || wp_remote_retrieve_response_code( $request ) !== 200 | |
| 88 | + ) { | |
| 89 | + return array(); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Check and sanitize the API response | |
| 94 | + */ | |
| 95 | + | |
| 96 | + $response_checked = array(); | |
| 97 | + $response = json_decode( | |
| 98 | + wp_remote_retrieve_body( $request ), | |
| 99 | + true | |
| 100 | + ); | |
| 101 | + | |
| 102 | + if ( ! empty( $response['language_original'] ) | |
| 103 | + && ( | |
| 104 | + wplng_is_valid_language_id( $response['language_original'] ) | |
| 105 | + || 'all' === $response['language_original'] | |
| 106 | + ) | |
| 107 | + && ! empty( $response['languages_target'] ) | |
| 108 | + && is_array( $response['languages_target'] ) | |
| 109 | + && isset( $response['features'] ) | |
| 110 | + && is_array( $response['features'] ) | |
| 111 | + ) { | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * API returned valid key informations | |
| 115 | + */ | |
| 116 | + | |
| 117 | + // Sanitize languages target | |
| 118 | + | |
| 119 | + $languages_target = array(); | |
| 120 | + | |
| 121 | + foreach ( $response['languages_target'] as $id ) { | |
| 122 | + | |
| 123 | + if ( ! wplng_is_valid_language_id( $id ) ) { | |
| 124 | + continue; | |
| 125 | + } | |
| 126 | + | |
| 127 | + $languages_target[] = sanitize_key( $id ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + // Sanitize features list | |
| 131 | + | |
| 132 | + $features = array(); | |
| 133 | + | |
| 134 | + foreach ( $response['features'] as $key => $allow ) { | |
| 135 | + | |
| 136 | + if ( ! is_string( $key ) || ! is_bool( $allow ) ) { | |
| 137 | + continue; | |
| 138 | + } | |
| 139 | + | |
| 140 | + $key = sanitize_key( $key ); | |
| 141 | + $allow = ( true === $allow ); | |
| 142 | + | |
| 143 | + $features[ $key ] = $allow; | |
| 144 | + } | |
| 145 | + | |
| 146 | + // Sanitize status | |
| 147 | + | |
| 148 | + $status = 'FREE'; | |
| 149 | + | |
| 150 | + if ( ! empty( $response['status'] ) | |
| 151 | + && ( | |
| 152 | + 'PREMIUM' === $response['status'] | |
| 153 | + || 'VIP' === $response['status'] | |
| 154 | + ) | |
| 155 | + ) { | |
| 156 | + $status = $response['status']; | |
| 157 | + } | |
| 158 | + | |
| 159 | + // Make the checked response | |
| 160 | + | |
| 161 | + $response_checked = array( | |
| 162 | + 'language_original' => sanitize_key( $response['language_original'] ), | |
| 163 | + 'languages_target' => $languages_target, | |
| 164 | + 'features' => $features, | |
| 165 | + 'status' => $status, | |
| 166 | + ); | |
| 167 | + | |
| 168 | + // Add expiration | |
| 169 | + | |
| 170 | + if ( ! empty( $response['expiration'] ) | |
| 171 | + && is_string( $response['expiration'] ) | |
| 172 | + ) { | |
| 173 | + $response_checked['expiration'] = sanitize_text_field( $response['expiration'] ); | |
| 174 | + } | |
| 175 | + } elseif ( isset( $response['error'] ) | |
| 176 | + && ( true === $response['error'] ) | |
| 177 | + && isset( $response['code'] ) | |
| 178 | + && is_int( $response['code'] ) | |
| 179 | + && isset( $response['message'] ) | |
| 180 | + && is_string( $response['message'] ) | |
| 181 | + ) { | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * API returning a valid error | |
| 185 | + */ | |
| 186 | + | |
| 187 | + $error_message = __( 'Code', 'wplingua' ) . ' '; | |
| 188 | + $error_message .= $response['code'] . ' - '; | |
| 189 | + $error_message .= sanitize_text_field( $response['message'] ); | |
| 190 | + | |
| 191 | + set_transient( | |
| 192 | + 'wplng_api_key_error', | |
| 193 | + $error_message, | |
| 194 | + MINUTE_IN_SECONDS * 5 | |
| 195 | + ); | |
| 196 | + | |
| 197 | + if ( isset( $response['disconnect'] ) | |
| 198 | + && true === $response['disconnect'] | |
| 199 | + ) { | |
| 200 | + delete_option( 'wplng_api_key_data' ); | |
| 201 | + delete_option( 'wplng_api_key' ); | |
| 202 | + wplng_clear_translations_cache(); | |
| 203 | + wplng_clear_slugs_cache(); | |
| 204 | + } | |
| 205 | + } else { | |
| 206 | + | |
| 207 | + /** | |
| 208 | + * API returned an unexpected response | |
| 209 | + */ | |
| 210 | + | |
| 211 | + set_transient( | |
| 212 | + 'wplng_api_key_error', | |
| 213 | + __( 'API returned an unexpected response.', 'wplingua' ), | |
| 214 | + MINUTE_IN_SECONDS * 5 | |
| 215 | + ); | |
| 216 | + | |
| 217 | + } | |
| 218 | + | |
| 219 | + return $response_checked; | |
| 220 | +} | |