PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.36
OttoKit: All-in-One Automation Platform v1.1.36
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 / fluentcrm / actions / add-contact.php
suretriggers / src / Integrations / fluentcrm / actions Last commit date
add-contact.php 2 weeks ago add-email-to-sequence.php 11 months ago add-event.php 11 months ago add-lists-to-contact.php 9 months ago add-note-to-company.php 9 months ago add-note.php 6 months ago add-tag-to-contact.php 11 months ago attach-subscribers-company.php 11 months ago create-campaign.php 5 months ago create-company.php 2 weeks ago create-email-template.php 9 months ago create-list.php 9 months ago create-sequence.php 9 months ago create-tag.php 1 month ago delete-campaign.php 9 months ago delete-company.php 9 months ago delete-list.php 9 months ago delete-sequence.php 9 months ago delete-tag.php 9 months ago detach-subscribers-company.php 11 months ago get-all-lists.php 9 months ago get-all-tags.php 9 months ago get-company-notes.php 9 months ago get-contact-notes.php 9 months ago get-contacts-who-clicked-email.php 9 months ago get-contacts-who-did-not-open-email.php 9 months ago get-contacts-who-opened-email.php 9 months ago get-contacts-who-unsubscribed-email.php 9 months ago list-contacts-by-list-id.php 7 months ago list-email-templates.php 11 months ago list-sequences.php 11 months ago remove-contact.php 11 months ago remove-subscriber-from-sequence.php 9 months ago remove-tag-from-contact.php 11 months ago retrieve-contact-by-email.php 11 months ago retrieve-contact-by-id.php 11 months ago retrieve-contact-by-list-ids.php 11 months ago retrieve-contact-by-segment.php 11 months ago retrieve-contact-by-status.php 11 months ago retrieve-contact-by-tag-ids.php 11 months ago retrieve-contact-by-user-id.php 11 months ago send-email-by-campaign.php 5 months ago send-email-by-template.php 11 months ago send-email-to-list-contacts.php 5 months ago update-contact-status.php 9 months ago update-template.php 11 months ago
add-contact.php
341 lines
1 <?php
2 /**
3 * AddContact.
4 * php version 5.6
5 *
6 * @category AddContact
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\FluentCRM\Actions;
15
16 use DateTime;
17 use Exception;
18 use SureTriggers\Integrations\AutomateAction;
19 use SureTriggers\Traits\SingletonLoader;
20 use FluentCrm\App\Models\Tag;
21
22 /**
23 * AddContact
24 *
25 * @category AddContact
26 * @package SureTriggers
27 * @author BSF <username@example.com>
28 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
29 * @link https://www.brainstormforce.com/
30 * @since 1.0.0
31 */
32 class AddContact extends AutomateAction {
33
34
35 /**
36 * Integration type.
37 *
38 * @var string
39 */
40 public $integration = 'FluentCRM';
41
42 /**
43 * Action name.
44 *
45 * @var string
46 */
47 public $action = 'fluentcrm_add_contact';
48
49 use SingletonLoader;
50
51 /**
52 * Register a action.
53 *
54 * @param array $actions actions.
55 * @return array
56 */
57 public function register( $actions ) {
58
59 $actions[ $this->integration ][ $this->action ] = [
60 'label' => __( 'Add/Update Contact', 'suretriggers' ),
61 'action' => $this->action,
62 'function' => [ $this, 'action_listener' ],
63 ];
64 return $actions;
65 }
66
67 /**
68 * Action listener.
69 *
70 * @param int $user_id user_id.
71 * @param int $automation_id automation_id.
72 * @param array $fields fields.
73 * @param array $selected_options selectedOptions.
74 *
75 * @return array|void
76 *
77 * @throws Exception Exception.
78 */
79 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
80 if ( ! function_exists( 'FluentCrmApi' ) || ! function_exists( 'fluentcrm_get_custom_contact_fields' ) ) {
81 return [
82 'status' => 'error',
83 'message' => __( 'FluentCRM functions not found.', 'suretriggers' ),
84
85 ];
86 }
87 if ( empty( $selected_options['contact_email'] ) || ! is_email( $selected_options['contact_email'] ) ) {
88 return [
89 'status' => 'error',
90 'message' => __( 'Email address is invalid.', 'suretriggers' ),
91
92 ];
93 }
94 $forced_update = false;
95
96 $contact_api = FluentCrmApi( 'contacts' );
97 $contact = $contact_api->getContact( trim( $selected_options['contact_email'] ) );
98
99 if ( ! is_null( $contact ) ) {
100 $forced_update = true;
101 }
102
103
104 $data = [
105 'email' => trim( $selected_options['contact_email'] ),
106 ];
107
108 $data['prefix'] = ( isset( $selected_options['prefix'] ) ) ? $selected_options['prefix'] : '';
109 $data['first_name'] = ( isset( $selected_options['first_name'] ) ) ? $selected_options['first_name'] : '';
110 $data['last_name'] = ( isset( $selected_options['last_name'] ) ) ? $selected_options['last_name'] : '';
111 $data['address_line_1'] = ( isset( $selected_options['address_line_1'] ) ) ? $selected_options['address_line_1'] : '';
112 $data['address_line_2'] = ( isset( $selected_options['address_line_2'] ) ) ? $selected_options['address_line_2'] : '';
113 $data['city'] = ( isset( $selected_options['city'] ) ) ? $selected_options['city'] : '';
114 $data['state'] = ( isset( $selected_options['state'] ) ) ? $selected_options['state'] : '';
115 $data['postal_code'] = ( isset( $selected_options['postal_code'] ) ) ? $selected_options['postal_code'] : '';
116 $data['country'] = ( isset( $selected_options['country'] ) ) ? $selected_options['country'] : '';
117 $data['phone'] = ( isset( $selected_options['phone'] ) ) ? $selected_options['phone'] : '';
118 $dob = ( isset( $selected_options['date_of_birth'] ) ) ? $selected_options['date_of_birth'] : '';
119
120 if ( '' !== $dob ) {
121 $date_of_birth = DateTime::createFromFormat( 'Y-m-d', $dob );
122 if ( ! $date_of_birth ) {
123 return [
124 'status' => 'error',
125 'message' => __( "The date format does not conform to the 'yyyy-mm-dd' format in Date of Birth field.", 'suretriggers' ),
126
127 ];
128 }
129 $data['date_of_birth'] = $dob;
130 }
131
132 if ( ! empty( $selected_options['contact_status'] ) ) {
133 $data['status'] = $selected_options['contact_status'];
134 }
135
136 if ( isset( $selected_options['show_custom_fields'] )
137 && in_array( $selected_options['show_custom_fields'], [ true, 1, 'true', '1' ], true ) ) {
138 $fcrm_custom_fields = fluentcrm_get_custom_contact_fields();
139
140 $fcrm_custom_fields_by_slug = [];
141 if ( is_array( $fcrm_custom_fields ) ) {
142 foreach ( $fcrm_custom_fields as $fcrm_custom_field ) {
143 if ( isset( $fcrm_custom_field['slug'] ) ) {
144 $fcrm_custom_fields_by_slug[ $fcrm_custom_field['slug'] ] = $fcrm_custom_field;
145 }
146 }
147 }
148
149 foreach ( $selected_options['field_row_repeater'] as $key => $field ) {
150 $field_name = ( isset( $field['value']['name'] ) && is_string( $field['value']['name'] ) ) ? $field['value']['name'] : '';
151
152 if ( '' === $field_name || ! isset( $fcrm_custom_fields_by_slug[ $field_name ] ) ) {
153 continue;
154 }
155
156 $type = $fcrm_custom_fields_by_slug[ $field_name ]['type'];
157 $label = $fcrm_custom_fields_by_slug[ $field_name ]['label'];
158 $value = isset( $selected_options['field_row'][ $key ][ $field_name ] ) ? trim( $selected_options['field_row'][ $key ][ $field_name ] ) : '';
159
160 if ( empty( $value ) ) {
161 continue;
162 }
163
164 if ( in_array( $type, [ 'select-one', 'radio' ], true ) ) {
165 $field_options = $fcrm_custom_fields_by_slug[ $field_name ]['options'];
166 $field_value = null;
167
168 foreach ( $field_options as $option ) {
169 if ( strtolower( $value ) === strtolower( $option ) ) {
170 $field_value = $option;
171 }
172 }
173
174 if ( ! $field_value ) {
175 return [
176 'status' => 'error',
177 'message' => sprintf( __( "The value '%1\$s' is not a valid option in the %2\$s field in FluentCRM.", 'suretriggers' ), $value, $label ),
178 ];
179 }
180
181 $data['custom_values'][ $field_name ] = $field_value;
182
183 } elseif ( in_array( $type, [ 'select-multi', 'checkbox' ], true ) ) {
184 $option_values = explode( ',', $value );
185 $option_values = array_map( 'trim', $option_values );
186 $field_options = $fcrm_custom_fields_by_slug[ $field_name ]['options'];
187
188 $options = [];
189 foreach ( $option_values as $option_value ) {
190 $field_value = null;
191
192 foreach ( $field_options as $option ) {
193 if ( strtolower( $option_value ) === strtolower( $option ) ) {
194 $field_value = $option;
195 }
196 }
197
198 if ( ! $field_value ) {
199 return [
200 'status' => 'error',
201 'message' => sprintf( __( "The value '%1\$s' is not a valid option in the %2\$s field in FluentCRM.", 'suretriggers' ), $option_value, $label ),
202 ];
203 }
204
205 $options[] = $field_value;
206 }
207
208
209 $data['custom_values'][ $field_name ] = $options;
210
211 } elseif ( 'date' === $type ) {
212 $date = DateTime::createFromFormat( 'Y-m-d', $value );
213 if ( ! $date ) {
214 return [
215 'status' => 'error',
216 'message' => sprintf( __( "The date format does not conform to the 'yyyy-mm-dd' format in %s field.", 'suretriggers' ), $label ),
217 ];
218 }
219
220 $data['custom_values'][ $field_name ] = $value;
221 } elseif ( 'date_time' === $type ) {
222 $date = DateTime::createFromFormat( 'Y-m-d H:i:s', $value );
223 if ( ! $date ) {
224 return [
225 'status' => 'error',
226 'message' => sprintf( __( "The datetime format does not conform to the 'yyyy-mm-dd hh:mm:ss' format in %s field.", 'suretriggers' ), $label ),
227 ];
228 }
229
230 $data['custom_values'][ $field_name ] = $value;
231 } else {
232 $data['custom_values'][ $field_name ] = $value;
233 }
234 }
235 }
236
237 $contact = $contact_api->createOrUpdate( $data, $forced_update );
238
239 if ( ! $contact ) {
240 return [
241 'status' => 'error',
242 'message' => __( 'Failed to create or update contact.', 'suretriggers' ),
243 ];
244 }
245
246 if ( 'pending' === $contact->status ) {
247 $contact->sendDoubleOptinEmail();
248 }
249
250 $tag_ids = [];
251 $tag_names = [];
252 $selected_tag = $selected_options['tag_id'];
253 if ( ! empty( $selected_tag ) ) {
254 if ( is_array( $selected_tag ) ) {
255 foreach ( $selected_tag as $tag ) {
256 $tag_ids[] = $tag['value'];
257 $tag_names[] = esc_html( $tag['label'] );
258 }
259
260 $contact->attachTags( $tag_ids );
261 } elseif ( is_string( $selected_tag ) ) {
262 $tags_arr = array_filter( explode( ',', $selected_tag ) );
263 if ( ! class_exists( 'FluentCrm\App\Models\Tag' ) ) {
264 return [
265 'status' => 'error',
266 'message' => __( 'Tag model not found.', 'suretriggers' ),
267
268 ];
269 }
270 foreach ( $tags_arr as $tag ) {
271 $exist = Tag::where( 'title', $tag )
272 ->orWhere( 'slug', $tag )
273 ->first();
274 if ( is_null( $exist ) ) {
275 $new_tag = Tag::create(
276 [
277 'title' => $tag,
278 ]
279 );
280 $tag_ids[] = $new_tag->id;
281 $tag_names[] = esc_html( $new_tag->title );
282 } else {
283 $tag_ids[] = $exist->id;
284 $tag_names[] = esc_html( $exist->title );
285 }
286 }
287 $contact->attachTags( $tag_ids );
288 }
289 }
290
291 $list_ids = [];
292 $list_names = [];
293 if ( isset( $selected_options['list_id'] ) && is_array( $selected_options['list_id'] ) && ! empty( $selected_options['list_id'] ) ) {
294 foreach ( $selected_options['list_id'] as $list ) {
295 $list_ids[] = $list['value'];
296 $list_names[] = esc_html( $list['label'] );
297 }
298
299 $contact->attachLists( $list_ids );
300 }
301
302 $custom_data = $contact->custom_fields();
303
304 $context = [];
305 $context['contact_id'] = $contact->id;
306 $context['full_name'] = $contact->full_name;
307 $context['first_name'] = $contact->first_name;
308 $context['last_name'] = $contact->last_name;
309 $context['contact_owner'] = $contact->contact_owner;
310 $context['company_id'] = $contact->company_id;
311 $context['email'] = $contact->email;
312 $context['address_line_1'] = $contact->address_line_1;
313 $context['address_line_2'] = $contact->address_line_2;
314 $context['postal_code'] = $contact->postal_code;
315 $context['city'] = $contact->city;
316 $context['state'] = $contact->state;
317 $context['country'] = $contact->country;
318 $context['phone'] = $contact->phone;
319 $context['status'] = $contact->status;
320 $context['contact_type'] = $contact->contact_type;
321 $context['source'] = $contact->source;
322 $context['date_of_birth'] = $contact->date_of_birth;
323 $context['list_names'] = implode( ',', $list_names );
324 $context['tag_names'] = implode( ',', $tag_names );
325
326 if ( ! empty( $custom_data ) ) {
327 foreach ( $custom_data as $key => $field ) {
328 if ( is_array( $field ) ) {
329 $context[ $key ] = implode( ',', $field );
330 } else {
331 $context[ $key ] = $field;
332 }
333 }
334 }
335 return $context;
336 }
337
338 }
339
340 AddContact::get_instance();
341