class-subscription-response.php
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* The Genesis Pro subscription response.
*
* @package Block_Lab
* @copyright Copyright(c) 2020, Block Lab
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Block_Lab\Admin\Migration;
use stdClass;
/**
* Class Subscription_Response
*/
class Subscription_Response {
/**
* Endpoint to validate the Genesis Pro subscription key.
*
* @var string
*/
const ENDPOINT = 'https://wp-product-info.wpesvc.net/v1/plugins/genesis-custom-blocks-pro/subscriptions/';
/**
* The code expected in a success response.
*
* @var string
*/
const SUCCESS_CODE = 200;
/**
* Whether the subscription key is valid.
*
* @var bool
*/
private $is_valid = false;
/**
* The error code, if any.
*
* @var string|null
*/
private $error_code;
/**
* The product info.
*
* @var stdClass|null
*/
private $product_info;
/**
* Constructs the class.
*
* @param string $subscription_key The subscription key to check.
*/
public function __construct( $subscription_key ) {
$this->evaluate( $subscription_key );
}
/**
* Evaluates the response, storing the response body and a possible error message.
*
* @param string $subscription_key The subscription key to check.
*/
public function evaluate( $subscription_key ) {
$response = wp_remote_get(
self::ENDPOINT . $subscription_key,
[
'timeout' => defined( 'DOING_CRON' ) && DOING_CRON ? 30 : 3,
'user-agent' => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ),
'body' => [
'version' => block_lab()->get_version(),
],
]
);
if ( is_wp_error( $response ) || self::SUCCESS_CODE !== wp_remote_retrieve_response_code( $response ) ) {
if ( is_wp_error( $response ) ) {
$this->error_code = $response->get_error_code();
} else {
$response_body = json_decode( wp_remote_retrieve_body( $response ), false );
$this->error_code = ! empty( $response_body->error_code ) ? $response_body->error_code : 'unknown';
}
return;
}
$this->is_valid = true;
$this->product_info = new stdClass();
$response_body = json_decode( wp_remote_retrieve_body( $response ) );
if ( ! is_object( $response_body ) ) {
$response_body = new stdClass();
}
$this->product_info = $response_body;
}
/**
* Gets whether the subscription key is valid.
*
* @return bool
*/
public function is_valid() {
return $this->is_valid;
}
/**
* Gets the error code, if any.
*
* @return string|null
*/
public function get_error_code() {
return $this->error_code;
}
/**
* Gets the product info, or null if there isn't any.
*
* @return stdClass|null
*/
public function get_product_info() {
return $this->product_info;
}
}