PluginProbe
Extendify / 0.6.0
Extendify v0.6.0
3.2.1 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 All 127 releases
extendify / app / Http.php

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

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