| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCartQuickWebP\Licensing; |
| 4 |
|
| 5 |
/** |
| 6 |
* Activation model |
| 7 |
*/ |
| 8 |
class Activation { |
| 9 |
/** |
| 10 |
* The endpoint for the activations. |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
protected $endpoint = 'v1/public/activations'; |
| 15 |
|
| 16 |
/** |
| 17 |
* SureCartQuickWebP\Licensing\Client |
| 18 |
* |
| 19 |
* @var object |
| 20 |
*/ |
| 21 |
protected $client; |
| 22 |
|
| 23 |
/** |
| 24 |
* `option_name` of `wp_options` table |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $option_key; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initialize the class. |
| 32 |
* |
| 33 |
* @param SureCartQuickWebP\Licensing\Client $client The client. |
| 34 |
*/ |
| 35 |
public function __construct( Client $client ) { |
| 36 |
$this->client = $client; |
| 37 |
$this->option_key = 'surecart_' . md5( $this->client->slug ) . '_license_activation_id'; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Create an activation for the license. |
| 42 |
* |
| 43 |
* @param string $license_id The license id. |
| 44 |
* |
| 45 |
* @return object|\WP_Error |
| 46 |
*/ |
| 47 |
public function create( $license_id ) { |
| 48 |
if ( empty( $license_id ) ) { |
| 49 |
return new \WP_Error( 'missing_key', __( 'Please enter a license key', 'quickwebp' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
// send the activation request. |
| 53 |
$activation = $this->client->send_request( |
| 54 |
'POST', |
| 55 |
trailingslashit( $this->endpoint ), |
| 56 |
array( |
| 57 |
'activation' => array( |
| 58 |
'fingerprint' => esc_url_raw( get_site_url() ), |
| 59 |
'name' => get_bloginfo(), |
| 60 |
'license' => $license_id, |
| 61 |
), |
| 62 |
) |
| 63 |
); |
| 64 |
|
| 65 |
// error. |
| 66 |
if ( is_wp_error( $activation ) ) { |
| 67 |
return $activation; |
| 68 |
} |
| 69 |
|
| 70 |
// no id. |
| 71 |
if ( empty( $activation->id ) ) { |
| 72 |
return new \WP_Error( 'could_not_activate', __( 'Could not activate the license.', 'quickwebp' ) ); |
| 73 |
} |
| 74 |
|
| 75 |
// return the activation. |
| 76 |
return $activation; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Retrieves details of a specific activation. |
| 81 |
* |
| 82 |
* @param string $id The id of the activation. |
| 83 |
* |
| 84 |
* @return object|\WP_Error |
| 85 |
*/ |
| 86 |
public function get( $id = '' ) { |
| 87 |
return $this->client->send_request( |
| 88 |
'GET', |
| 89 |
trailingslashit( $this->endpoint ) . $id |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Update an activation for the license. |
| 95 |
* |
| 96 |
* @param string $id The id of the activation. |
| 97 |
* |
| 98 |
* @return object|\WP_Error |
| 99 |
*/ |
| 100 |
public function update( $id = '' ) { |
| 101 |
$license_key = $this->client->license()->get_id(); |
| 102 |
if ( empty( $license_key ) ) { |
| 103 |
return new \WP_Error( 'missing_key', __( 'Please enter a license key', 'quickwebp' ) ); |
| 104 |
} |
| 105 |
|
| 106 |
return $this->client->send_request( |
| 107 |
'PATCH', |
| 108 |
trailingslashit( $this->endpoint ) . $id, |
| 109 |
array( |
| 110 |
'fingerprint' => esc_url_raw( get_site_url() ), |
| 111 |
'name' => get_bloginfo(), |
| 112 |
'license' => $license_key, |
| 113 |
) |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Deletes a specific activation. |
| 119 |
* |
| 120 |
* @param string $id The id of the activation. |
| 121 |
* |
| 122 |
* @return object|\WP_Error |
| 123 |
*/ |
| 124 |
public function delete( $id = '' ) { |
| 125 |
return $this->client->send_request( |
| 126 |
'DELETE', |
| 127 |
trailingslashit( $this->endpoint ) . $id |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|