| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helper class for making http requests |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace Extendify; |
| 7 |
|
| 8 |
use Extendify\Config; |
| 9 |
use Extendify\User; |
| 10 |
|
| 11 |
/** |
| 12 |
* Controller for http communication |
| 13 |
*/ |
| 14 |
class Http |
| 15 |
{ |
| 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 |
* @return void |
| 50 |
*/ |
| 51 |
public function __construct($request) |
| 52 |
{ |
| 53 |
// Redundant, but extra protection! |
| 54 |
if (!\wp_verify_nonce(sanitize_text_field(wp_unslash($request->get_header('x_wp_nonce'))), 'wp_rest')) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Some special cases for library development. |
| 59 |
$this->baseUrl = $this->getBaseUrl($request); |
| 60 |
|
| 61 |
$this->data = [ |
| 62 |
'wp_language' => \get_locale(), |
| 63 |
'wp_theme' => \get_option('template'), |
| 64 |
'mode' => Config::$environment, |
| 65 |
'uuid' => User::data('uuid'), |
| 66 |
'library_version' => Config::$version, |
| 67 |
'wp_active_plugins' => $request->get_method() === 'POST' ? \get_option('active_plugins') : [], |
| 68 |
'sdk_partner' => Config::$sdkPartner, |
| 69 |
]; |
| 70 |
|
| 71 |
if ($request->get_header('x_extendify_dev_mode') !== 'false') { |
| 72 |
$this->data['devmode'] = true; |
| 73 |
} |
| 74 |
|
| 75 |
$this->headers = [ |
| 76 |
'Accept' => 'application/json', |
| 77 |
'referer' => $request->get_header('referer'), |
| 78 |
'user_agent' => $request->get_header('user_agent'), |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Register dynamic routes |
| 84 |
* |
| 85 |
* @param string $endpoint - The endpoint. |
| 86 |
* @param array $data - The data to include. |
| 87 |
* @param array $headers - The headers to include. |
| 88 |
* |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
public function getHandler($endpoint, $data = [], $headers = []) |
| 92 |
{ |
| 93 |
$url = \esc_url_raw( |
| 94 |
\add_query_arg( |
| 95 |
\urlencode_deep(\urldecode_deep(array_merge($this->data, $data))), |
| 96 |
$this->baseUrl . $endpoint |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
$response = \wp_remote_get( |
| 101 |
$url, |
| 102 |
[ |
| 103 |
'headers' => array_merge($this->headers, $headers), |
| 104 |
] |
| 105 |
); |
| 106 |
if (\is_wp_error($response)) { |
| 107 |
return $response; |
| 108 |
} |
| 109 |
|
| 110 |
$responseBody = \wp_remote_retrieve_body($response); |
| 111 |
return json_decode($responseBody, true); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Register dynamic routes |
| 116 |
* |
| 117 |
* @param string $endpoint - The endpoint. |
| 118 |
* @param array $data - The arguments to include. |
| 119 |
* @param array $headers - The headers to include. |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function postHandler($endpoint, $data = [], $headers = []) |
| 124 |
{ |
| 125 |
$response = \wp_remote_post( |
| 126 |
$this->baseUrl . $endpoint, |
| 127 |
[ |
| 128 |
'headers' => array_merge($this->headers, $headers), |
| 129 |
'body' => array_merge($this->data, $data), |
| 130 |
] |
| 131 |
); |
| 132 |
if (\is_wp_error($response)) { |
| 133 |
return $response; |
| 134 |
} |
| 135 |
|
| 136 |
$responseBody = \wp_remote_retrieve_body($response); |
| 137 |
return json_decode($responseBody, true); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* The caller |
| 142 |
* |
| 143 |
* @param string $name - The name of the method to call. |
| 144 |
* @param array $arguments - The arguments to pass in. |
| 145 |
* |
| 146 |
* @return mixed |
| 147 |
*/ |
| 148 |
public static function __callStatic($name, array $arguments) |
| 149 |
{ |
| 150 |
if ($name === 'init') { |
| 151 |
self::$instance = new static($arguments[0]); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
$name = "{$name}Handler"; |
| 156 |
$r = self::$instance; |
| 157 |
|
| 158 |
return $r->$name(...$arguments); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Figure out the base URL to use. |
| 163 |
* |
| 164 |
* @param \WP_REST_Request $request - The request. |
| 165 |
* |
| 166 |
* @return string |
| 167 |
*/ |
| 168 |
public function getBaseUrl($request) |
| 169 |
{ |
| 170 |
// Library Dev request. |
| 171 |
if ($request->get_header('x_extendify_dev_mode') === 'true') { |
| 172 |
return Config::$config['api']['dev']; |
| 173 |
} |
| 174 |
|
| 175 |
// Library Local request. |
| 176 |
if ($request->get_header('x_extendify_local_mode') === 'true') { |
| 177 |
return Config::$config['api']['local']; |
| 178 |
} |
| 179 |
|
| 180 |
// Onboarding dev request. |
| 181 |
if ($request->get_header('x_extendify_onboarding_dev_mode') === 'true') { |
| 182 |
return Config::$config['api']['onboarding-dev']; |
| 183 |
} |
| 184 |
|
| 185 |
// Onborarding local request. |
| 186 |
if ($request->get_header('x_extendify_onboarding_local_mode') === 'true') { |
| 187 |
return Config::$config['api']['onboarding-local']; |
| 188 |
} |
| 189 |
|
| 190 |
// Onboarding request. |
| 191 |
if ($request->get_header('x_extendify_onboarding') === 'true') { |
| 192 |
return Config::$config['api']['onboarding']; |
| 193 |
} |
| 194 |
|
| 195 |
// Assist dev request. |
| 196 |
if ($request->get_header('x_extendify_assist_dev_mode') === 'true') { |
| 197 |
return Config::$config['api']['assist-dev']; |
| 198 |
} |
| 199 |
|
| 200 |
// Assist local request. |
| 201 |
if ($request->get_header('x_extendify_assist_local_mode') === 'true') { |
| 202 |
return Config::$config['api']['assist-local']; |
| 203 |
} |
| 204 |
|
| 205 |
// Assist request. |
| 206 |
if ($request->get_header('x_extendify_assist') === 'true') { |
| 207 |
return Config::$config['api']['assist']; |
| 208 |
} |
| 209 |
|
| 210 |
// Normal Library request. |
| 211 |
return Config::$config['api']['live']; |
| 212 |
} |
| 213 |
} |
| 214 |
|