PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.8.14
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.8.14
7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / inc / providers / getresponse / hustle-get-response-api.php
wordpress-popup / inc / providers / getresponse Last commit date
images 7 years ago getresponse.php 5 months ago hustle-get-response-api.php 3 years ago hustle-get-response-form-hooks.php 5 months ago hustle-get-response-form-settings.php 5 months ago hustle-get-response.php 3 years ago
hustle-get-response-api.php
263 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Hustle_Get_Response_Api class
4 *
5 * @package Hustle
6 */
7
8 /**
9 * GetResponse API implementation
10 *
11 * Class Hustle_Get_Response_Api
12 */
13 class Hustle_Get_Response_Api {
14
15 /**
16 * Api key
17 *
18 * @var string
19 */
20 private $api_key;
21
22 /**
23 * Endpoint
24 *
25 * @var string
26 */
27 private $endpoint = 'https://api.getresponse.com/v3/';
28
29 /**
30 * Constructs class with required data
31 *
32 * Hustle_Get_Response_Api constructor.
33 *
34 * @param string $api_key Api key.
35 * @param array $args Args.
36 */
37 public function __construct( $api_key, $args = array() ) {
38 $this->api_key = $api_key;
39
40 if ( isset( $args['endpoint'] ) ) {
41 $this->endpoint = $args['endpoint'];
42 }
43 }
44
45
46 /**
47 * Sends request to the endpoint url with the provided $action
48 *
49 * @param string $action rest action.
50 * @param string $verb Verb.
51 * @param array $args Args.
52 * @return object|WP_Error
53 */
54 private function request( $action, $verb = 'GET', $args = array() ) {
55 $url = trailingslashit( $this->endpoint ) . $action;
56
57 $_args = array(
58 'method' => $verb,
59 'headers' => array(
60 'X-Auth-Token' => 'api-key ' . $this->api_key,
61 'Content-Type' => 'application/json;charset=utf-8',
62 ),
63 );
64
65 if ( 'GET' === $verb ) {
66 $url .= ( '?' . http_build_query( $args ) );
67
68 if ( 'contacts' === $action ) {
69 $url = rawurldecode( $url );
70 }
71 } elseif ( ! empty( $args['body'] ) ) {
72 $_args['body'] = wp_json_encode( $args['body'] );
73 }
74
75 $res = wp_remote_request( $url, $_args );
76
77 // logging data.
78 $utils = Hustle_Provider_Utils::get_instance();
79 $utils->last_url_request = $url;
80 $utils->last_data_sent = $_args;
81 $utils->last_data_received = $res;
82
83 if ( ! is_wp_error( $res ) && is_array( $res ) && $res['response']['code'] <= 204 ) {
84 return json_decode( wp_remote_retrieve_body( $res ) );
85 }
86
87 if ( is_wp_error( $res ) ) {
88 return $res;
89 }
90
91 $err = new WP_Error();
92 $message = $res['response']['message'];
93 $message .= wp_remote_retrieve_body( $res );
94
95 $err->add( $res['response']['code'], $message );
96 return $err;
97 }
98
99 /**
100 * Sends rest GET request
101 *
102 * @param string $action Actions.
103 * @param array $args Args.
104 * @return array|mixed|object|WP_Error
105 */
106 private function get( $action, $args = array() ) {
107 return $this->request( $action, 'GET', $args );
108 }
109
110 /**
111 * Sends rest POST request
112 *
113 * @param string $action Actions.
114 * @param array $args Args.
115 * @return array|mixed|object|WP_Error
116 */
117 private function post( $action, $args = array() ) {
118 return $this->request( $action, 'POST', $args );
119 }
120
121 /**
122 * Sends rest DELETE request
123 *
124 * @param string $action Actions.
125 * @param array $args Args.
126 * @return array|mixed|object|WP_Error
127 */
128 private function delete( $action, $args = array() ) {
129 return $this->request( $action, 'DELETE', $args );
130 }
131
132 /**
133 * Retrieves campaigns as array of objects
134 *
135 * @return array|WP_Error
136 */
137 public function get_campaigns() {
138 return $this->get(
139 'campaigns',
140 array(
141 'name' => array( 'CONTAINS' => '%' ),
142 'perPage' => 1000,
143 )
144 );
145 }
146
147 /**
148 * Retrieves contactID
149 *
150 * @since 4.0
151 * @param array $data Data.
152 * @return string
153 */
154 public function get_contact( $data ) {
155 $res = $this->get(
156 'contacts',
157 array(
158 'query[email]' => rawurlencode( $data['email'] ),
159 'query[campaignId]' => $data['list_id'],
160 )
161 );
162 $contact_id = '';
163
164 if ( ! empty( $res[0] ) && ! empty( $res[0]->contactId ) ) {
165 $contact_id = $res[0]->contactId;
166 }
167
168 return $contact_id;
169 }
170
171 /**
172 * Add new contact
173 *
174 * @param array $data Data.
175 * @return array|mixed|object|WP_Error
176 */
177 public function subscribe( $data ) {
178 $url = 'contacts';
179 $args = array(
180 'body' => $data,
181 );
182 $res = $this->post( $url, $args );
183
184 return empty( $res ) ? __( 'Successful subscription', 'hustle' ) : $res;
185 }
186
187 /**
188 * Update contact
189 *
190 * @param string $contact_id Contact ID.
191 * @param array $data New data.
192 * @return array|mixed|object|WP_Error
193 */
194 public function update_contact( $contact_id, $data ) {
195 $url = 'contacts/' . $contact_id;
196 $args = array(
197 'body' => $data,
198 );
199 $res = $this->post( $url, $args );
200
201 return empty( $res ) ? __( 'Successful subscription', 'hustle' ) : $res;
202 }
203
204 /**
205 * Delete subscriber from the list
206 *
207 * @param string $list_id List ID.
208 * @param string $email Email.
209 *
210 * @return bool
211 */
212 public function delete_email( $list_id, $email ) {
213 $args = array(
214 'email' => $email,
215 'list_id' => $list_id,
216 );
217 $contact_id = $this->get_contact( $args );
218
219 if ( empty( $contact_id ) ) {
220 return false;
221 }
222 // They don't have the ability to unsubscribe.
223 // To remove is their official reply from their dev.
224 $res = $this->delete( 'contacts/' . $contact_id );
225
226 return ! is_wp_error( $res );
227 }
228
229 /**
230 * Get custom fields
231 *
232 * @return array
233 */
234 public function get_custom_fields() {
235 $args = array( 'fields' => 'name, type' );
236 $res = $this->get( 'custom-fields', $args );
237
238 return $res;
239 }
240
241 /**
242 * Add custom field
243 *
244 * @param array $custom_field Custom field.
245 **/
246 public function add_custom_field( $custom_field ) {
247 $url = 'custom-fields';
248 $args = array(
249 'body' => $custom_field,
250 );
251 $res = $this->post( $url, $args );
252
253 if ( is_wp_error( $res ) ) {
254 return $res;
255 }
256 if ( ! empty( $res ) && ! empty( $res->customFieldId ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
257 return $res->customFieldId;// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
258 }
259
260 return false;
261 }
262 }
263