Http.php (3791B)
1 <?php 2 /** 3 * Helper class for making http requests 4 */ 5 6 namespace Extendify\ExtendifySdk; 7 8 use Extendify\ExtendifySdk\App; 9 use Extendify\ExtendifySdk\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 prodection! 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 development. 59 $this->baseUrl = $request->get_header('x_extendify_dev_mode') !== 'false' ? App::$config['api']['dev'] : App::$config['api']['live']; 60 $this->baseUrl = $request->get_header('x_extendify_local_mode') !== 'false' ? App::$config['api']['local'] : $this->baseUrl; 61 62 $this->data = [ 63 'mode' => App::$environment, 64 'uuid' => User::data('uuid'), 65 'sdk_version' => App::$version, 66 'wp_plugins' => $request->get_method() === 'POST' ? array_keys(\get_plugins()) : [], 67 ]; 68 69 $this->headers = [ 70 'Accept' => 'application/json', 71 'referer' => $request->get_header('referer'), 72 'user_agent' => $request->get_header('user_agent'), 73 ]; 74 } 75 76 /** 77 * Register dynamic routes 78 * 79 * @param string $endpoint - The endpoint. 80 * @param array $data - The data to include. 81 * @param array $headers - The headers to include. 82 * 83 * @return array 84 */ 85 public function getHandler($endpoint, $data = [], $headers = []) 86 { 87 $url = \esc_url_raw( 88 \add_query_arg( 89 \urlencode_deep(\urldecode_deep(array_merge($this->data, $data))), 90 $this->baseUrl . $endpoint 91 ) 92 ); 93 94 $response = \wp_remote_get( 95 $url, 96 [ 97 'headers' => array_merge($this->headers, $headers), 98 ] 99 ); 100 101 $responseBody = \wp_remote_retrieve_body($response); 102 return json_decode($responseBody, true); 103 } 104 105 /** 106 * Register dynamic routes 107 * 108 * @param string $endpoint - The endpoint. 109 * @param array $data - The arguments to include. 110 * @param array $headers - The headers to include. 111 * 112 * @return array 113 */ 114 public function postHandler($endpoint, $data = [], $headers = []) 115 { 116 $response = \wp_remote_post( 117 $this->baseUrl . $endpoint, 118 [ 119 'headers' => array_merge($this->headers, $headers), 120 'body' => array_merge($this->data, $data), 121 ] 122 ); 123 124 $responseBody = \wp_remote_retrieve_body($response); 125 return json_decode($responseBody, true); 126 } 127 128 /** 129 * The caller 130 * 131 * @param string $name - The name of the method to call. 132 * @param array $arguments - The arguments to pass in. 133 * 134 * @return mixed 135 */ 136 public static function __callStatic($name, array $arguments) 137 { 138 if ($name === 'init') { 139 self::$instance = new static($arguments[0]); 140 return; 141 } 142 143 $name = "{$name}Handler"; 144 $r = self::$instance; 145 146 return $r->$name(...$arguments); 147 } 148 }