| @@ -1,0 +1,92 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | + | |
| 5 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 6 | +} | |
| 7 | + | |
| 8 | +if ( ! class_exists( 'SuextDlmyapp' ) ) { | |
| 9 | + | |
| 10 | + class SuextDlmyapp { | |
| 11 | + | |
| 12 | + private $api_key; | |
| 13 | + | |
| 14 | + function __construct( $api_key = null ) { | |
| 15 | + | |
| 16 | + if ( $api_key ) { | |
| 17 | + | |
| 18 | + $this->api_key = $api_key; | |
| 19 | + } | |
| 20 | + } | |
| 21 | + | |
| 22 | + public function shorten( $long_url ) { | |
| 23 | + | |
| 24 | + static $local_cache = array(); | |
| 25 | + | |
| 26 | + if ( ! empty( $local_cache[ $long_url ] ) ) { | |
| 27 | + | |
| 28 | + return $local_cache[ $long_url ]; | |
| 29 | + } | |
| 30 | + | |
| 31 | + $request_url = 'https://dlmy.app/api/?key=' . urlencode( $this->api_key ) . '&url=' . urlencode( $long_url ); | |
| 32 | + | |
| 33 | + $ch = curl_init(); | |
| 34 | + | |
| 35 | + curl_setopt( $ch, CURLOPT_URL, $request_url ); | |
| 36 | + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
| 37 | + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( | |
| 38 | + 'Content-Type: application/json', | |
| 39 | + 'Accept: application/json', | |
| 40 | + ) ); | |
| 41 | + | |
| 42 | + $response = curl_exec( $ch ); | |
| 43 | + $curl_errnum = curl_errno( $ch ); | |
| 44 | + $curl_errmsg = curl_error( $ch ); // See https://curl.se/libcurl/c/libcurl-errors.html. | |
| 45 | + $ssl_verify = (int) curl_getinfo( $ch, CURLINFO_SSL_VERIFYRESULT ); // See https://www.php.net/manual/en/function.curl-getinfo.php. | |
| 46 | + $http_code = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE ); | |
| 47 | + | |
| 48 | + curl_close( $ch ); | |
| 49 | + | |
| 50 | + $http_success_codes = array( 200, 201 ); | |
| 51 | + | |
| 52 | + try { | |
| 53 | + | |
| 54 | + if ( ! empty( $curl_errnum ) ) { | |
| 55 | + | |
| 56 | + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'DLMY.App', $curl_errnum . ' ' . $curl_errmsg ); | |
| 57 | + | |
| 58 | + throw new WpssoErrorException( $except_msg ); | |
| 59 | + | |
| 60 | + } elseif ( ! in_array( $http_code, $http_success_codes ) ) { | |
| 61 | + | |
| 62 | + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'DLMY.App', '' ); | |
| 63 | + | |
| 64 | + throw new WpssoErrorException( $except_msg, $http_code ); | |
| 65 | + | |
| 66 | + } else { | |
| 67 | + | |
| 68 | + $resp_data = json_decode( $response, $assoc = false ); | |
| 69 | + | |
| 70 | + if ( null === $resp_data ) { | |
| 71 | + | |
| 72 | + $json_msg = sprintf( __( 'JSON response decode error code %d.', 'wpsso' ), json_last_error() ); | |
| 73 | + | |
| 74 | + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'DLMY.App', $json_msg ); | |
| 75 | + | |
| 76 | + throw new WpssoErrorException( $except_msg ); | |
| 77 | + | |
| 78 | + } elseif ( ! empty( $resp_data->short ) ) { | |
| 79 | + | |
| 80 | + return $local_cache[ $long_url ] = $resp_data->short; | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | + } catch ( WpssoErrorException $e ) { | |
| 85 | + | |
| 86 | + $e->errorMessage(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + return false; | |
| 90 | + } | |
| 91 | + } | |
| 92 | +} | |