| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Validate a wpLingua API key |
| 11 |
* |
| 12 |
* @param string $api_key |
| 13 |
* @return bool |
| 14 |
*/ |
| 15 |
function wplng_is_valid_api_key_format( $api_key ) { |
| 16 |
|
| 17 |
$regex = '/^[a-zA-Z1-9]{42}$/s'; |
| 18 |
|
| 19 |
if ( |
| 20 |
empty( $api_key ) |
| 21 |
|| ! is_string( $api_key ) |
| 22 |
|| $api_key !== preg_quote( $api_key ) |
| 23 |
|| false === preg_match( $regex, $api_key ) |
| 24 |
) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
return true; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Get the wpLingua registered API key |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
function wplng_get_api_key() { |
| 38 |
|
| 39 |
$api_key = trim( get_option( 'wplng_api_key' ) ); |
| 40 |
|
| 41 |
if ( ! wplng_is_valid_api_key_format( $api_key ) ) { |
| 42 |
$api_key = ''; |
| 43 |
} |
| 44 |
|
| 45 |
return $api_key; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* Get wpLingua API key data |
| 51 |
* - Website language (id or 'all') |
| 52 |
* - Target language(s) (array) |
| 53 |
* - Enaled features |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
function wplng_get_api_data() { |
| 58 |
|
| 59 |
if ( empty( wplng_get_api_key() ) ) { |
| 60 |
return array(); |
| 61 |
} |
| 62 |
|
| 63 |
$data_checked = array(); |
| 64 |
$data = get_transient( 'wplng_api_key_data' ); |
| 65 |
$data = json_decode( $data, true ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Check the API key data |
| 69 |
*/ |
| 70 |
|
| 71 |
if ( ! empty( $data['language_original'] ) |
| 72 |
&& ( |
| 73 |
wplng_is_valid_language_id( $data['language_original'] ) |
| 74 |
|| 'all' === $data['language_original'] |
| 75 |
) |
| 76 |
&& ! empty( $data['languages_target'] ) |
| 77 |
&& wplng_is_valid_language_ids( $data['languages_target'] ) |
| 78 |
&& isset( $data['features'] ) |
| 79 |
&& is_array( $data['features'] ) |
| 80 |
) { |
| 81 |
|
| 82 |
/** |
| 83 |
* Sanitize languages target |
| 84 |
*/ |
| 85 |
|
| 86 |
$languages_target = array(); |
| 87 |
|
| 88 |
foreach ( $data['languages_target'] as $id ) { |
| 89 |
|
| 90 |
if ( ! wplng_is_valid_language_id( $id ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
$languages_target[] = sanitize_key( $id ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Sanitize features list |
| 99 |
*/ |
| 100 |
|
| 101 |
$features = array(); |
| 102 |
|
| 103 |
foreach ( $data['features'] as $key => $allow ) { |
| 104 |
|
| 105 |
if ( ! is_string( $key ) || ! is_bool( $allow ) ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
$key = sanitize_key( $key ); |
| 110 |
$allow = ( true === $allow ); |
| 111 |
|
| 112 |
$features[ $key ] = $allow; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Make the checked response |
| 117 |
*/ |
| 118 |
|
| 119 |
$data_checked = array( |
| 120 |
'language_original' => sanitize_key( $data['language_original'] ), |
| 121 |
'languages_target' => $languages_target, |
| 122 |
'features' => $features, |
| 123 |
); |
| 124 |
|
| 125 |
} else { |
| 126 |
|
| 127 |
/** |
| 128 |
* Get sanitized data from API call |
| 129 |
*/ |
| 130 |
|
| 131 |
$data_checked = wplng_api_call_validate_api_key(); |
| 132 |
|
| 133 |
if ( empty( $data_checked ) ) { |
| 134 |
return array(); |
| 135 |
} |
| 136 |
|
| 137 |
set_transient( |
| 138 |
'wplng_api_key_data', |
| 139 |
wp_json_encode( $data_checked ), |
| 140 |
60 * 60 * 24 |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
return $data_checked; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Get website language from wpLingua API data |
| 150 |
* |
| 151 |
* @return string Language ID, 'all' or '' |
| 152 |
*/ |
| 153 |
function wplng_get_api_language_website() { |
| 154 |
|
| 155 |
$data = wplng_get_api_data(); |
| 156 |
|
| 157 |
if ( |
| 158 |
! empty( $data['language_original'] ) |
| 159 |
&& ( |
| 160 |
wplng_is_valid_language_id( $data['language_original'] ) |
| 161 |
|| 'all' === $data['language_original'] |
| 162 |
) |
| 163 |
) { |
| 164 |
return $data['language_original']; |
| 165 |
} |
| 166 |
|
| 167 |
return ''; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* Get target languages from wpLingua API data |
| 173 |
* |
| 174 |
* @return mixed Language IDs array, 'all' or false |
| 175 |
*/ |
| 176 |
function wplng_get_api_languages_target() { |
| 177 |
|
| 178 |
$data = wplng_get_api_data(); |
| 179 |
|
| 180 |
if ( empty( $data['languages_target'] ) ) { |
| 181 |
return false; |
| 182 |
} elseif ( 'all' === $data['languages_target'] ) { |
| 183 |
return 'all'; |
| 184 |
} elseif ( is_array( $data['languages_target'] ) ) { |
| 185 |
|
| 186 |
$all_languages = wplng_get_languages_all(); |
| 187 |
$languages_id_ordered = array(); |
| 188 |
|
| 189 |
foreach ( $all_languages as $language ) { |
| 190 |
foreach ( $data['languages_target'] as $language_id ) { |
| 191 |
|
| 192 |
if ( |
| 193 |
! wplng_is_valid_language_id( $language_id ) |
| 194 |
|| empty( $language['id'] ) |
| 195 |
|| $language['id'] !== $language_id |
| 196 |
) { |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
$languages_id_ordered[] = $language_id; |
| 201 |
|
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return $languages_id_ordered; |
| 206 |
} |
| 207 |
|
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/** |
| 213 |
* Get enabled features from wpLingua API data |
| 214 |
* ('search', 'woocommerce') |
| 215 |
* |
| 216 |
* @return array |
| 217 |
*/ |
| 218 |
function wplng_get_api_feature() { |
| 219 |
|
| 220 |
$data = wplng_get_api_data(); |
| 221 |
$all = array( 'search', 'woocommerce' ); |
| 222 |
$features = array(); |
| 223 |
|
| 224 |
if ( ! empty( $data['features'] ) && is_array( $data['features'] ) ) { |
| 225 |
foreach ( $data['features'] as $feature_name => $feature_allow ) { |
| 226 |
if ( $feature_allow && in_array( $feature_name, $all ) ) { |
| 227 |
$features[] = $feature_name; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return $features; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* Return true if the feature is enable in wpLingua API key data |
| 238 |
* |
| 239 |
* @param string $feature_name |
| 240 |
* @return bool |
| 241 |
*/ |
| 242 |
function wplng_api_feature_is_allow( $feature_name ) { |
| 243 |
return in_array( $feature_name, wplng_get_api_feature() ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* Clear wpLingua API key data if wpLingua key is changed |
| 249 |
* |
| 250 |
* @param string $old_value JSON |
| 251 |
* @param string $new_value JSON |
| 252 |
* @return void |
| 253 |
*/ |
| 254 |
function wplng_on_update_option_wplng_api_key( $old_value, $new_value ) { |
| 255 |
|
| 256 |
delete_transient( 'wplng_api_key_data' ); |
| 257 |
|
| 258 |
delete_option( 'wplng_website_language' ); |
| 259 |
delete_option( 'wplng_website_flag' ); |
| 260 |
|
| 261 |
wplng_clear_translations_cache(); |
| 262 |
|
| 263 |
} |
| 264 |
|