PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.36
OttoKit: All-in-One Automation Platform v1.1.36
1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / groundhogg / actions / gh-create-contact.php
suretriggers / src / Integrations / groundhogg / actions Last commit date
gh-add-tag-to-contact.php 11 months ago gh-create-contact.php 9 months ago gh-remove-tag-from-contact.php 11 months ago
gh-create-contact.php
330 lines
1 <?php
2 /**
3 * GhCreateContact.
4 * php version 5.6
5 *
6 * @category GhCreateContact
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\Groundhogg\Actions;
15
16 use Exception;
17 use SureTriggers\Integrations\AutomateAction;
18 use SureTriggers\Traits\SingletonLoader;
19
20 /**
21 * GhCreateContact
22 *
23 * @category GhCreateContact
24 * @package SureTriggers
25 * @author BSF <username@example.com>
26 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
27 * @link https://www.brainstormforce.com/
28 * @since 1.0.0
29 */
30 class GhCreateContact extends AutomateAction {
31
32
33 /**
34 * Integration type.
35 *
36 * @var string
37 */
38 public $integration = 'Groundhogg';
39
40 /**
41 * Action name.
42 *
43 * @var string
44 */
45 public $action = 'gh_create_contact';
46
47 use SingletonLoader;
48
49 /**
50 * Register a action.
51 *
52 * @param array $actions actions.
53 * @return array
54 */
55 public function register( $actions ) {
56
57 $actions[ $this->integration ][ $this->action ] = [
58 'label' => __( 'Create/Update Contact', 'suretriggers' ),
59 'action' => $this->action,
60 'function' => [ $this, 'action_listener' ],
61 ];
62 return $actions;
63 }
64
65 /**
66 * Action listener.
67 *
68 * @param int $user_id user_id.
69 * @param int $automation_id automation_id.
70 * @param array $fields fields.
71 * @param array $selected_options selectedOptions.
72 * @return array
73 * @throws Exception Exception.
74 */
75 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
76 $email = sanitize_email( $selected_options['email'] );
77 $api_key = $selected_options['token'];
78 $public_key = $selected_options['public_key'];
79 $first_name = $selected_options['first_name'];
80 $last_name = $selected_options['last_name'];
81 $optin_status = $selected_options['optin_status'];
82 $custom_fields = $selected_options['custom_fields'];
83 // Extract new fields from selected_options or meta object.
84 $primary_phone = isset( $selected_options['primary_phone'] ) ? $selected_options['primary_phone'] : '';
85 $primary_phone_extension = isset( $selected_options['primary_phone_extension'] ) ? $selected_options['primary_phone_extension'] : '';
86 $street_address_1 = isset( $selected_options['street_address_1'] ) ? $selected_options['street_address_1'] : '';
87 $street_address_2 = isset( $selected_options['street_address_2'] ) ? $selected_options['street_address_2'] : '';
88 $city = isset( $selected_options['city'] ) ? $selected_options['city'] : '';
89 $postal_zip = isset( $selected_options['postal_zip'] ) ? $selected_options['postal_zip'] : '';
90 $region = isset( $selected_options['region'] ) ? $selected_options['region'] : '';
91 $country = isset( $selected_options['country'] ) ? $selected_options['country'] : '';
92 $lead_source = isset( $selected_options['lead_source'] ) ? $selected_options['lead_source'] : '';
93 $source_page = isset( $selected_options['source_page'] ) ? $selected_options['source_page'] : '';
94 $custom_meta = isset( $selected_options['custom_meta'] ) ? $selected_options['custom_meta'] : '';
95 $birthday = isset( $selected_options['birthday'] ) ? $selected_options['birthday'] : '';
96 $ip_address = isset( $selected_options['ip_address'] ) ? $selected_options['ip_address'] : '';
97 $mobile_phone = isset( $selected_options['mobile_phone'] ) ? $selected_options['mobile_phone'] : '';
98 $gravatar = isset( $selected_options['gravatar'] ) ? $selected_options['gravatar'] : '';
99 $age = isset( $selected_options['age'] ) ? $selected_options['age'] : '';
100
101
102 // Check if fields exist in meta object from custom_fields if not found directly.
103 if ( ! empty( $custom_fields ) ) {
104 foreach ( $custom_fields as $field ) {
105 if ( isset( $field['custom_field_key'] ) && isset( $field['custom_field_value'] ) ) {
106 $key = $field['custom_field_key'];
107 $value = $field['custom_field_value'];
108
109 // Map meta fields to direct fields if direct fields are empty.
110 switch ( $key ) {
111 case 'primary_phone':
112 if ( empty( $primary_phone ) ) {
113 $primary_phone = $value;
114 }
115 break;
116 case 'primary_phone_extension':
117 if ( empty( $primary_phone_extension ) ) {
118 $primary_phone_extension = $value;
119 }
120 break;
121 case 'street_address_1':
122 if ( empty( $street_address_1 ) ) {
123 $street_address_1 = $value;
124 }
125 break;
126 case 'street_address_2':
127 if ( empty( $street_address_2 ) ) {
128 $street_address_2 = $value;
129 }
130 break;
131 case 'city':
132 if ( empty( $city ) ) {
133 $city = $value;
134 }
135 break;
136 case 'postal_zip':
137 if ( empty( $postal_zip ) ) {
138 $postal_zip = $value;
139 }
140 break;
141 case 'region':
142 if ( empty( $region ) ) {
143 $region = $value;
144 }
145 break;
146 case 'country':
147 if ( empty( $country ) ) {
148 $country = $value;
149 }
150 break;
151 case 'lead_source':
152 if ( empty( $lead_source ) ) {
153 $lead_source = $value;
154 }
155 break;
156 case 'source_page':
157 if ( empty( $source_page ) ) {
158 $source_page = $value;
159 }
160 break;
161 case 'custom_meta':
162 if ( empty( $custom_meta ) ) {
163 $custom_meta = $value;
164 }
165 break;
166 case 'birthday':
167 if ( empty( $birthday ) ) {
168 $birthday = $value;
169 }
170 break;
171 case 'ip_address':
172 if ( empty( $ip_address ) ) {
173 $ip_address = $value;
174 }
175 break;
176 case 'mobile_phone':
177 if ( empty( $mobile_phone ) ) {
178 $mobile_phone = $value;
179 }
180 break;
181 case 'gravatar':
182 if ( empty( $gravatar ) ) {
183 $gravatar = $value;
184 }
185 break;
186 case 'age':
187 if ( empty( $age ) ) {
188 $age = $value;
189 }
190 break;
191 }
192 }
193 }
194 }
195
196 if ( is_email( $email ) ) {
197 // Make a single response array.
198 $response_array = [];
199
200 // Build http request param.
201 $request_args = [
202 'data' => [
203 'email' => $email,
204 'first_name' => $first_name,
205 'last_name' => $last_name,
206 'optin_status' => $optin_status,
207 ],
208 ];
209
210 // Add gravatar and age to data section if provided.
211 if ( ! empty( $gravatar ) ) {
212 $request_args['data']['gravatar'] = $gravatar;
213 }
214 if ( ! empty( $age ) ) {
215 $request_args['data']['age'] = $age;
216 }
217
218 // Prepare meta fields array.
219 $meta_fields = [];
220
221 // Add custom_fields to meta.
222 if ( ! empty( $custom_fields ) ) {
223 foreach ( $custom_fields as $key => $value ) {
224 $meta_fields[ $value['custom_field_key'] ] = $value['custom_field_value'];
225 }
226 }
227
228 // Add direct fields to meta if provided.
229 if ( ! empty( $primary_phone ) ) {
230 $meta_fields['primary_phone'] = $primary_phone;
231 }
232 if ( ! empty( $primary_phone_extension ) ) {
233 $meta_fields['primary_phone_extension'] = $primary_phone_extension;
234 }
235 if ( ! empty( $street_address_1 ) ) {
236 $meta_fields['street_address_1'] = $street_address_1;
237 }
238 if ( ! empty( $street_address_2 ) ) {
239 $meta_fields['street_address_2'] = $street_address_2;
240 }
241 if ( ! empty( $city ) ) {
242 $meta_fields['city'] = $city;
243 }
244 if ( ! empty( $postal_zip ) ) {
245 $meta_fields['postal_zip'] = $postal_zip;
246 }
247 if ( ! empty( $region ) ) {
248 $meta_fields['region'] = $region;
249 }
250 if ( ! empty( $country ) ) {
251 $meta_fields['country'] = $country;
252 }
253 if ( ! empty( $lead_source ) ) {
254 $meta_fields['lead_source'] = $lead_source;
255 }
256 if ( ! empty( $source_page ) ) {
257 $meta_fields['source_page'] = $source_page;
258 }
259 if ( ! empty( $custom_meta ) ) {
260 $meta_fields['custom_meta'] = $custom_meta;
261 }
262 if ( ! empty( $birthday ) ) {
263 $meta_fields['birthday'] = $birthday;
264 }
265 if ( ! empty( $ip_address ) ) {
266 $meta_fields['ip_address'] = $ip_address;
267 }
268 if ( ! empty( $mobile_phone ) ) {
269 $meta_fields['mobile_phone'] = $mobile_phone;
270 }
271
272 // Add meta to request if we have any fields.
273 if ( ! empty( $meta_fields ) ) {
274 $request_args['meta'] = $meta_fields;
275 }
276
277
278 $args = [
279 'headers' => [
280 'Content-Type' => 'application/json',
281 'Gh-Token' => $api_key,
282 'Gh-Public-Key' => $public_key,
283 ],
284 'sslverify' => false,
285 'data_format' => 'body',
286 'body' => wp_json_encode( $request_args ),
287 ];
288
289 /**
290 *
291 * Ignore line
292 *
293 * @phpstan-ignore-next-line
294 */
295 $request = wp_remote_post( get_rest_url() . 'gh/v4/contacts/', $args );
296 $response_code = wp_remote_retrieve_response_code( $request );
297 $response_body = wp_remote_retrieve_body( $request );
298 $response = $response_body;
299
300 if ( 200 !== $response_code ) {
301 $response = json_decode( $response_body );
302 if ( is_object( $response ) ) {
303 if ( property_exists( $response, 'code' ) && property_exists( $response, 'message' ) ) {
304 $error_code = $response->code;
305 $error_message = $response->message;
306 $response_array = [
307 'status' => 'error',
308 'code' => $error_code,
309 'message' => $error_message,
310 ];
311 }
312 }
313 } else {
314 $response = json_decode( $response, true );
315 $response_array = (array) $response;
316 }
317
318 return $response_array;
319 } else {
320 return [
321 'status' => 'error',
322 'message' => 'Enter valid email',
323 ];
324 }
325 }
326
327 }
328
329 GhCreateContact::get_instance();
330