| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The class defines way of connecting this user to the Optimole Dashboard. |
| 5 |
* |
| 6 |
* @package \Optimole\Inc |
| 7 |
* @author Optimole <friends@optimole.com> |
| 8 |
*/ |
| 9 |
final class Optml_Api { |
| 10 |
|
| 11 |
/** |
| 12 |
* Optimole root api url. |
| 13 |
* |
| 14 |
* @var string Api root. |
| 15 |
*/ |
| 16 |
private $api_root = 'https://dashboard.optimole.com/api/optml/v1/'; |
| 17 |
/** |
| 18 |
* Hold the user api key. |
| 19 |
* |
| 20 |
* @var string Api key. |
| 21 |
*/ |
| 22 |
private $api_key; |
| 23 |
|
| 24 |
/** |
| 25 |
* Optml_Api constructor. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$settings = new Optml_Settings(); |
| 29 |
$this->api_key = $settings->get( 'api_key' ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get user data from service. |
| 34 |
* |
| 35 |
* @return array|bool User data. |
| 36 |
*/ |
| 37 |
public function get_user_data( $api_key = '' ) { |
| 38 |
if ( ! empty( $api_key ) ) { |
| 39 |
$this->api_key = $api_key; |
| 40 |
} |
| 41 |
|
| 42 |
return $this->request( '/image/details', 'POST' ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Request constructor. |
| 47 |
* |
| 48 |
* @param string $path The request url. |
| 49 |
* @param string $method The request method type. |
| 50 |
* @param array $params The request method type. |
| 51 |
* |
| 52 |
* @return array|boolean Api data. |
| 53 |
*/ |
| 54 |
private function request( $path, $method = 'GET', $params = array() ) { |
| 55 |
|
| 56 |
// Grab the url to which we'll be making the request. |
| 57 |
$url = $this->api_root; |
| 58 |
$headers = array(); |
| 59 |
if ( ! empty( $this->api_key ) ) { |
| 60 |
$headers = array( |
| 61 |
'Authorization' => 'Bearer ' . $this->api_key, |
| 62 |
); |
| 63 |
} |
| 64 |
// If there is a extra, add that as a url var. |
| 65 |
if ( 'GET' === $method && ! empty( $params ) ) { |
| 66 |
foreach ( $params as $key => $val ) { |
| 67 |
$url = add_query_arg( array( $key => $val ), $url ); |
| 68 |
} |
| 69 |
} |
| 70 |
$url = trailingslashit( $this->api_root ) . ltrim( $path, '/' ); |
| 71 |
$args = array( |
| 72 |
'url' => $url, |
| 73 |
'method' => $method, |
| 74 |
'timeout' => 45, |
| 75 |
'httpversion' => 'Optimle WP (v' . OPTML_VERSION . ') ', |
| 76 |
'sslverify' => false, |
| 77 |
'headers' => $headers, |
| 78 |
); |
| 79 |
if ( $method !== 'GET' ) { |
| 80 |
$args['body'] = $params; |
| 81 |
} |
| 82 |
$response = wp_remote_request( $url, $args ); |
| 83 |
|
| 84 |
if ( is_wp_error( $response ) ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
$response = wp_remote_retrieve_body( $response ); |
| 88 |
|
| 89 |
if ( empty( $response ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
$response = json_decode( $response, true ); |
| 94 |
|
| 95 |
if ( ! $response['code'] ) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
if ( intval( $response['code'] ) !== 200 ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
return $response['data']; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Register user remotely on optimole.com. |
| 108 |
* |
| 109 |
* @param string $email User email. |
| 110 |
* |
| 111 |
* @return array|bool Api response. |
| 112 |
*/ |
| 113 |
public function create_account( $email ) { |
| 114 |
return $this->request( 'user/register-remote', 'POST', array( |
| 115 |
'email' => $email, |
| 116 |
'site' => get_site_url() |
| 117 |
) ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the optimized images from API. |
| 122 |
* |
| 123 |
* @param string $api_key the api key. |
| 124 |
* |
| 125 |
* @return array|bool |
| 126 |
*/ |
| 127 |
public function get_optimized_images( $api_key = '' ) { |
| 128 |
if ( ! empty( $api_key ) ) { |
| 129 |
$this->api_key = $api_key; |
| 130 |
} |
| 131 |
|
| 132 |
return $this->request( '/stats/images' ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Throw error on object clone |
| 137 |
* |
| 138 |
* The whole idea of the singleton design pattern is that there is a single |
| 139 |
* object therefore, we don't want the object to be cloned. |
| 140 |
* |
| 141 |
* @access public |
| 142 |
* @since 1.0.0 |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function __clone() { |
| 146 |
// Cloning instances of the class is forbidden. |
| 147 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'optimole-wp' ), '1.0.0' ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Disable unserializing of the class |
| 152 |
* |
| 153 |
* @access public |
| 154 |
* @since 1.0.0 |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function __wakeup() { |
| 158 |
// Unserializing instances of the class is forbidden. |
| 159 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'optimole-wp' ), '1.0.0' ); |
| 160 |
} |
| 161 |
} |
| 162 |
|