| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helper class for making http requests |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Extendify; |
| 7 |
|
| 8 |
use Extendify\PartnerData; |
| 9 |
use Extendify\Config; |
| 10 |
use Extendify\User; |
| 11 |
|
| 12 |
/** |
| 13 |
* Controller for http communication |
| 14 |
*/ |
| 15 |
class Http |
| 16 |
{ |
| 17 |
/** |
| 18 |
* The api endpoint |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $baseUrl = ''; |
| 23 |
|
| 24 |
/** |
| 25 |
* Request data sent to the server |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
public $data = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* Any headers required |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
public $headers = []; |
| 37 |
|
| 38 |
/** |
| 39 |
* The class instance. |
| 40 |
* |
| 41 |
* @var $instance |
| 42 |
*/ |
| 43 |
protected static $instance = null; |
| 44 |
|
| 45 |
/** |
| 46 |
* Set up the base object to send with every request |
| 47 |
* |
| 48 |
* @param \WP_REST_Request $request - The request. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function __construct($request) |
| 53 |
{ |
| 54 |
// Redundant, but extra protection! |
| 55 |
if (!\wp_verify_nonce(sanitize_text_field(wp_unslash($request->get_header('x_wp_nonce'))), 'wp_rest')) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// Some special cases for library development. |
| 60 |
$this->baseUrl = $this->getBaseUrl($request); |
| 61 |
|
| 62 |
$this->data = [ |
| 63 |
'wp_language' => \get_locale(), |
| 64 |
'wp_theme' => \get_option('template'), |
| 65 |
'wp_version' => \get_bloginfo('version'), |
| 66 |
'mode' => Config::$environment, |
| 67 |
'uuid' => User::data('uuid'), |
| 68 |
'library_version' => Config::$version, |
| 69 |
'wp_active_plugins' => $request->get_method() === 'POST' ? \get_option('active_plugins') : [], |
| 70 |
'is_block_theme' => function_exists('wp_is_block_theme') ? wp_is_block_theme() : false, |
| 71 |
'sdk_partner' => PartnerData::$id, |
| 72 |
]; |
| 73 |
|
| 74 |
if ($request->get_header('x_extendify_dev_mode') === 'true') { |
| 75 |
$this->data['devmode'] = true; |
| 76 |
} |
| 77 |
|
| 78 |
$this->headers = [ |
| 79 |
'Accept' => 'application/json', |
| 80 |
'referer' => $request->get_header('referer'), |
| 81 |
'user_agent' => $request->get_header('user_agent'), |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Register dynamic routes |
| 87 |
* |
| 88 |
* @param string $endpoint - The endpoint. |
| 89 |
* @param array $data - The data to include. |
| 90 |
* @param array $headers - The headers to include. |
| 91 |
* |
| 92 |
* @return array |
| 93 |
*/ |
| 94 |
public function getHandler($endpoint, $data = [], $headers = []) |
| 95 |
{ |
| 96 |
$url = \esc_url_raw( |
| 97 |
\add_query_arg( |
| 98 |
\urlencode_deep(\urldecode_deep(array_merge($this->data, $data))), |
| 99 |
$this->baseUrl . $endpoint |
| 100 |
) |
| 101 |
); |
| 102 |
|
| 103 |
$response = \wp_remote_get( |
| 104 |
$url, |
| 105 |
[ |
| 106 |
'headers' => array_merge($this->headers, $headers), |
| 107 |
] |
| 108 |
); |
| 109 |
|
| 110 |
if (\is_wp_error($response)) { |
| 111 |
return $response; |
| 112 |
} |
| 113 |
|
| 114 |
if ((int) $response['response']['code'] >= 500) { |
| 115 |
return new \WP_Error($response['response']['code'], $response['response']['message']); |
| 116 |
} |
| 117 |
|
| 118 |
$responseBody = \wp_remote_retrieve_body($response); |
| 119 |
return json_decode($responseBody, true); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Register dynamic routes |
| 124 |
* |
| 125 |
* @param string $endpoint - The endpoint. |
| 126 |
* @param array $data - The arguments to include. |
| 127 |
* @param array $headers - The headers to include. |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
public function postHandler($endpoint, $data = [], $headers = []) |
| 132 |
{ |
| 133 |
$response = \wp_remote_post( |
| 134 |
$this->baseUrl . $endpoint, |
| 135 |
[ |
| 136 |
'headers' => array_merge($this->headers, $headers), |
| 137 |
'body' => array_merge($this->data, $data), |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
if (\is_wp_error($response)) { |
| 142 |
return $response; |
| 143 |
} |
| 144 |
|
| 145 |
if ((int) $response['response']['code'] >= 500) { |
| 146 |
wp_send_json_error($response['response']['message'], $response['response']['code']); |
| 147 |
} |
| 148 |
|
| 149 |
$responseBody = \wp_remote_retrieve_body($response); |
| 150 |
return json_decode($responseBody, true); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* The caller |
| 155 |
* |
| 156 |
* @param string $name - The name of the method to call. |
| 157 |
* @param array $arguments - The arguments to pass in. |
| 158 |
* |
| 159 |
* @return mixed |
| 160 |
*/ |
| 161 |
public static function __callStatic($name, array $arguments) |
| 162 |
{ |
| 163 |
if ($name === 'init') { |
| 164 |
self::$instance = new static($arguments[0]); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$name = "{$name}Handler"; |
| 169 |
$r = self::$instance; |
| 170 |
|
| 171 |
return $r->$name(...$arguments); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Figure out the base URL to use. |
| 176 |
* |
| 177 |
* @param \WP_REST_Request $request - The request. |
| 178 |
* |
| 179 |
* @return string |
| 180 |
*/ |
| 181 |
public function getBaseUrl($request) |
| 182 |
{ |
| 183 |
$headerToConfigKeyMap = [ |
| 184 |
'x_extendify_dev_mode' => 'dev', |
| 185 |
'x_extendify_local_mode' => 'local', |
| 186 |
'x_extendify_onboarding_dev_mode' => 'onboarding-dev', |
| 187 |
'x_extendify_onboarding_local_mode' => 'onboarding-local', |
| 188 |
'x_extendify_onboarding' => 'onboarding', |
| 189 |
'x_extendify_assist_dev_mode' => 'assist-dev', |
| 190 |
'x_extendify_assist_local_mode' => 'assist-local', |
| 191 |
'x_extendify_assist' => 'assist', |
| 192 |
'x_extendify_chat' => 'chat', |
| 193 |
]; |
| 194 |
|
| 195 |
foreach ($headerToConfigKeyMap as $headerKey => $configKey) { |
| 196 |
if ($request->get_header($headerKey) === 'true') { |
| 197 |
return Config::$config['api'][$configKey]; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// Normal Library request. |
| 202 |
return Config::$config['api']['live']; |
| 203 |
} |
| 204 |
} |
| 205 |
|