PluginProbe
Extendify / 0.8.2
Extendify v0.8.2
3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 0.8.0 All 125 releases
extendify / app / Http.php

Http.php in Extendify 0.8.2, at app/Http.php

165 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = $request->get_header('x_extendify_dev_mode') !== 'false' ? Config::$config['api']['dev'] : Config::$config['api']['live'];
60 $this->baseUrl = $request->get_header('x_extendify_local_mode') !== 'false' ? Config::$config['api']['local'] : $this->baseUrl;
61
62 // Onboarding request.
63 $this->baseUrl = $request->get_header('x_extendify_onboarding') === 'true' ? Config::$config['api']['onboarding'] : $this->baseUrl;
64
65 $this->data = [
66 'wp_language' => \get_locale(),
67 'wp_theme' => \get_option('template'),
68 'mode' => Config::$environment,
69 'uuid' => User::data('uuid'),
70 'library_version' => Config::$version,
71 'wp_active_plugins' => $request->get_method() === 'POST' ? \get_option('active_plugins') : [],
72 'sdk_partner' => Config::$sdkPartner,
73 ];
74
75 if ($request->get_header('x_extendify_dev_mode') !== 'false') {
76 $this->data['devmode'] = true;
77 }
78
79 $this->headers = [
80 'Accept' => 'application/json',
81 'referer' => $request->get_header('referer'),
82 'user_agent' => $request->get_header('user_agent'),
83 ];
84 }
85
86 /**
87 * Register dynamic routes
88 *
89 * @param string $endpoint - The endpoint.
90 * @param array $data - The data to include.
91 * @param array $headers - The headers to include.
92 *
93 * @return array
94 */
95 public function getHandler($endpoint, $data = [], $headers = [])
96 {
97 $url = \esc_url_raw(
98 \add_query_arg(
99 \urlencode_deep(\urldecode_deep(array_merge($this->data, $data))),
100 $this->baseUrl . $endpoint
101 )
102 );
103
104 $response = \wp_remote_get(
105 $url,
106 [
107 'headers' => array_merge($this->headers, $headers),
108 ]
109 );
110 if (\is_wp_error($response)) {
111 return $response;
112 }
113
114 $responseBody = \wp_remote_retrieve_body($response);
115 return json_decode($responseBody, true);
116 }
117
118 /**
119 * Register dynamic routes
120 *
121 * @param string $endpoint - The endpoint.
122 * @param array $data - The arguments to include.
123 * @param array $headers - The headers to include.
124 *
125 * @return array
126 */
127 public function postHandler($endpoint, $data = [], $headers = [])
128 {
129 $response = \wp_remote_post(
130 $this->baseUrl . $endpoint,
131 [
132 'headers' => array_merge($this->headers, $headers),
133 'body' => array_merge($this->data, $data),
134 ]
135 );
136 if (\is_wp_error($response)) {
137 return $response;
138 }
139
140 $responseBody = \wp_remote_retrieve_body($response);
141 return json_decode($responseBody, true);
142 }
143
144 /**
145 * The caller
146 *
147 * @param string $name - The name of the method to call.
148 * @param array $arguments - The arguments to pass in.
149 *
150 * @return mixed
151 */
152 public static function __callStatic($name, array $arguments)
153 {
154 if ($name === 'init') {
155 self::$instance = new static($arguments[0]);
156 return;
157 }
158
159 $name = "{$name}Handler";
160 $r = self::$instance;
161
162 return $r->$name(...$arguments);
163 }
164 }
165