PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / 22.7.0
WPSSO Core – Complete Schema Markup and Meta Tags v22.7.0
22.7.0 22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
wpsso / lib / ext / dlmyapp.php

dlmyapp.php in WPSSO Core – Complete Schema Markup and Meta Tags 22.7.0, at lib/ext/dlmyapp.php

93 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
93