| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla\Licensing; |
| 4 |
|
| 5 |
use Boxzilla\Admin\Notices; |
| 6 |
use Boxzilla\Plugin; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
class API { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var License |
| 13 |
*/ |
| 14 |
protected $license; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var Notices |
| 18 |
*/ |
| 19 |
protected $notices; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $api_url = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
protected $error_code = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $error_message = ''; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var |
| 38 |
*/ |
| 39 |
protected $last_response; |
| 40 |
|
| 41 |
/** |
| 42 |
* @param string $api_url |
| 43 |
* @param Notices $notices |
| 44 |
*/ |
| 45 |
public function __construct( $api_url, Notices $notices ) { |
| 46 |
$this->api_url = $api_url; |
| 47 |
$this->notices = $notices; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Logs the current site in to the remote API |
| 52 |
* |
| 53 |
* @param License $license |
| 54 |
* @return bool |
| 55 |
*/ |
| 56 |
public function create_license_activation( License $license ) { |
| 57 |
$endpoint = '/license/activations'; |
| 58 |
$data = array( |
| 59 |
'site_url' => $license->site |
| 60 |
); |
| 61 |
$result = $this->request( 'POST', $endpoint, $data ); |
| 62 |
if( $result ) { |
| 63 |
$this->notices->add( $result->message, 'info' ); |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Logs the current site out of the remote API |
| 72 |
* |
| 73 |
* @param License $license |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
public function delete_license_activation( License $license ) { |
| 77 |
$endpoint = '/license/activations'; |
| 78 |
$data = array( |
| 79 |
'site_url' => $license->site |
| 80 |
); |
| 81 |
$result = $this->request( 'DELETE', $endpoint, $data ); |
| 82 |
|
| 83 |
if( $result ) { |
| 84 |
$this->notices->add( $result->message, 'info' ); |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param Plugin $plugin |
| 93 |
* @return object |
| 94 |
*/ |
| 95 |
public function get_plugin( Plugin $plugin ) { |
| 96 |
$endpoint = sprintf( '/plugins/%s?format=wp', $plugin->id() ); |
| 97 |
return $this->request( 'GET', $endpoint ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param Plugin[] $plugins |
| 102 |
* @return object |
| 103 |
*/ |
| 104 |
public function get_plugins( $plugins ) { |
| 105 |
// create array of plugin ID's |
| 106 |
$plugin_slugs = $plugins->map(function( $p ) { return $p->id(); }); |
| 107 |
$endpoint = add_query_arg( array( 'sids' => implode(',', $plugin_slugs ), 'format' => 'wp' ), '/plugins' ); |
| 108 |
return $this->request( 'GET', $endpoint ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @param string $method |
| 113 |
* @param string $endpoint |
| 114 |
* @param array $data |
| 115 |
* @return object |
| 116 |
*/ |
| 117 |
public function request( $method, $endpoint, $data = array() ) { |
| 118 |
|
| 119 |
$url = $this->api_url . $endpoint; |
| 120 |
$args = array( |
| 121 |
'method' => $method |
| 122 |
); |
| 123 |
|
| 124 |
if( in_array( $method, array( 'GET', 'DELETE' ) ) ) { |
| 125 |
$url = add_query_arg( $data, $url ); |
| 126 |
} else { |
| 127 |
$args['body'] = $data; |
| 128 |
} |
| 129 |
|
| 130 |
$request = wp_remote_request( $url, $args ); |
| 131 |
|
| 132 |
// test for wp errors |
| 133 |
if( $request instanceof WP_Error) { |
| 134 |
$this->notices->add( $request->get_error_message(), 'error' ); ; |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
// retrieve response body |
| 139 |
$body = wp_remote_retrieve_body( $request ); |
| 140 |
$response = json_decode( $body ); |
| 141 |
if( ! is_object( $response ) ) { |
| 142 |
$this->notices->add( __( "The Boxzilla server returned an invalid response.", 'boxzilla' ), 'error' ); |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
// store response |
| 147 |
$this->last_response = $response; |
| 148 |
|
| 149 |
// did request return an error response? |
| 150 |
if( isset( $response->error ) ) { |
| 151 |
$this->notices->add( $response->error->message, 'error' ); |
| 152 |
return null; |
| 153 |
} |
| 154 |
|
| 155 |
// return actual response data |
| 156 |
return $response->data; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @return object|null |
| 161 |
*/ |
| 162 |
public function get_last_response() { |
| 163 |
return $this->last_response; |
| 164 |
} |
| 165 |
|
| 166 |
} |