1: <?php
2:
3: /**
4: * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
5: */
6: class Quform_Api
7: {
8: /**
9: * @var string
10: */
11: const API_URL = 'https://api.quform.com/wp-json/quform/v1';
12:
13: /**
14: * @var string
15: */
16: const API_URL_INSECURE = 'http://api.quform.com/wp-json/quform/v1';
17:
18: /**
19: * @var Quform_Options
20: */
21: protected $options;
22:
23: /**
24: * @param Quform_Options $options
25: */
26: public function __construct(Quform_Options $options)
27: {
28: $this->options = $options;
29: }
30:
31: /**
32: * Send a request to the Quform API
33: *
34: * @param string $endpoint The API endpoint to send the request to
35: * @param array $data The request data
36: * @param string $method The HTTP method to use
37: * @return array|bool The response array or false on failure
38: */
39: public function request($endpoint, $data, $method = 'GET')
40: {
41: $url = $this->options->get('secureApiRequests') ? self::API_URL : self::API_URL_INSECURE;
42: $url .= '/' . trim($endpoint, '/');
43:
44: $response = wp_remote_request($url, array(
45: 'method' => $method,
46: 'body' => $data,
47: 'timeout' => 10
48: ));
49:
50: if (is_wp_error($response) || ! strlen($body = wp_remote_retrieve_body($response))) {
51: return false;
52: }
53:
54: $response = json_decode($body, true);
55:
56: if ( ! is_array($response)) {
57: return false;
58: }
59:
60: return $response;
61: }
62:
63: /**
64: * Send a GET request to the Quform API
65: *
66: * @param string $endpoint The API endpoint to send the request to
67: * @param array $data The request data
68: * @return array|bool The response array or false on failure
69: */
70: public function get($endpoint, $data)
71: {
72: return $this->request($endpoint, $data, 'GET');
73: }
74:
75: /**
76: * Send a POST request to the Quform API
77: *
78: * @param string $endpoint The API endpoint to send the request to
79: * @param array $data The request data
80: * @return array|bool The response array or false on failure
81: */
82: public function post($endpoint, $data)
83: {
84: return $this->request($endpoint, $data, 'POST');
85: }
86: }
87: