PluginProbe
Extendify / 0.9.2
Extendify v0.9.2
3.2.0 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 All 126 releases
extendify / app / Http.php

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

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