add-contact.php
2 months ago
add-email-to-sequence.php
10 months ago
add-event.php
10 months ago
add-lists-to-contact.php
8 months ago
add-note-to-company.php
8 months ago
add-note.php
5 months ago
add-tag-to-contact.php
10 months ago
attach-subscribers-company.php
10 months ago
create-campaign.php
4 months ago
create-company.php
10 months ago
create-email-template.php
8 months ago
create-list.php
8 months ago
create-sequence.php
8 months ago
create-tag.php
2 weeks ago
delete-campaign.php
8 months ago
delete-company.php
8 months ago
delete-list.php
8 months ago
delete-sequence.php
8 months ago
delete-tag.php
8 months ago
detach-subscribers-company.php
10 months ago
get-all-lists.php
8 months ago
get-all-tags.php
8 months ago
get-company-notes.php
8 months ago
get-contact-notes.php
8 months ago
get-contacts-who-clicked-email.php
8 months ago
get-contacts-who-did-not-open-email.php
8 months ago
get-contacts-who-opened-email.php
8 months ago
get-contacts-who-unsubscribed-email.php
8 months ago
list-contacts-by-list-id.php
7 months ago
list-email-templates.php
10 months ago
list-sequences.php
10 months ago
remove-contact.php
10 months ago
remove-subscriber-from-sequence.php
8 months ago
remove-tag-from-contact.php
10 months ago
retrieve-contact-by-email.php
10 months ago
retrieve-contact-by-id.php
10 months ago
retrieve-contact-by-list-ids.php
10 months ago
retrieve-contact-by-segment.php
10 months ago
retrieve-contact-by-status.php
10 months ago
retrieve-contact-by-tag-ids.php
10 months ago
retrieve-contact-by-user-id.php
10 months ago
send-email-by-campaign.php
4 months ago
send-email-by-template.php
10 months ago
send-email-to-list-contacts.php
4 months ago
update-contact-status.php
8 months ago
update-template.php
10 months ago
add-contact.php
326 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 | foreach ( $selected_options['field_row_repeater'] as $key => $field ) { |
| 140 | $type = $fcrm_custom_fields[ $key ]['type']; |
| 141 | $label = $fcrm_custom_fields[ $key ]['label']; |
| 142 | $field_name = $field['value']['name']; |
| 143 | $value = trim( $selected_options['field_row'][ $key ][ $field_name ] ); |
| 144 | |
| 145 | if ( empty( $value ) ) { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | if ( in_array( $type, [ 'select-one', 'radio' ], true ) ) { |
| 150 | $field_options = $fcrm_custom_fields[ $key ]['options']; |
| 151 | $field_value = null; |
| 152 | |
| 153 | foreach ( $field_options as $option ) { |
| 154 | if ( strtolower( $value ) === strtolower( $option ) ) { |
| 155 | $field_value = $option; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if ( ! $field_value ) { |
| 160 | return [ |
| 161 | 'status' => 'error', |
| 162 | 'message' => sprintf( __( "The value '%1\$s' is not a valid option in the %2\$s field in FluentCRM.", 'suretriggers' ), $value, $label ), |
| 163 | ]; |
| 164 | } |
| 165 | |
| 166 | $data[ $field_name ] = $field_value; |
| 167 | |
| 168 | } elseif ( in_array( $type, [ 'select-multi', 'checkbox' ], true ) ) { |
| 169 | $option_values = explode( ',', $value ); |
| 170 | $option_values = array_map( 'trim', $option_values ); |
| 171 | $field_options = $fcrm_custom_fields[ $key ]['options']; |
| 172 | |
| 173 | $options = []; |
| 174 | foreach ( $option_values as $option_value ) { |
| 175 | $field_value = null; |
| 176 | |
| 177 | foreach ( $field_options as $option ) { |
| 178 | if ( strtolower( $option_value ) === strtolower( $option ) ) { |
| 179 | $field_value = $option; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if ( ! $field_value ) { |
| 184 | return [ |
| 185 | 'status' => 'error', |
| 186 | 'message' => sprintf( __( "The value '%1\$s' is not a valid option in the %2\$s field in FluentCRM.", 'suretriggers' ), $option_value, $label ), |
| 187 | ]; |
| 188 | } |
| 189 | |
| 190 | $options[] = $field_value; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | $data[ $field_name ] = $options; |
| 195 | |
| 196 | } elseif ( 'date' === $type ) { |
| 197 | $date = DateTime::createFromFormat( 'Y-m-d', $value ); |
| 198 | if ( ! $date ) { |
| 199 | return [ |
| 200 | 'status' => 'error', |
| 201 | 'message' => sprintf( __( "The date format does not conform to the 'yyyy-mm-dd' format in %s field.", 'suretriggers' ), $label ), |
| 202 | ]; |
| 203 | } |
| 204 | |
| 205 | $data[ $field_name ] = $value; |
| 206 | } elseif ( 'date_time' === $type ) { |
| 207 | $date = DateTime::createFromFormat( 'Y-m-d H:i:s', $value ); |
| 208 | if ( ! $date ) { |
| 209 | return [ |
| 210 | 'status' => 'error', |
| 211 | 'message' => sprintf( __( "The datetime format does not conform to the 'yyyy-mm-dd hh:mm:ss' format in %s field.", 'suretriggers' ), $label ), |
| 212 | ]; |
| 213 | } |
| 214 | |
| 215 | $data[ $field_name ] = $value; |
| 216 | } else { |
| 217 | $data[ $field_name ] = $value; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | $contact = $contact_api->createOrUpdate( $data, $forced_update ); |
| 223 | |
| 224 | if ( ! $contact ) { |
| 225 | return [ |
| 226 | 'status' => 'error', |
| 227 | 'message' => __( 'Failed to create or update contact.', 'suretriggers' ), |
| 228 | ]; |
| 229 | } |
| 230 | |
| 231 | if ( 'pending' === $contact->status ) { |
| 232 | $contact->sendDoubleOptinEmail(); |
| 233 | } |
| 234 | |
| 235 | $tag_ids = []; |
| 236 | $tag_names = []; |
| 237 | $selected_tag = $selected_options['tag_id']; |
| 238 | if ( ! empty( $selected_tag ) ) { |
| 239 | if ( is_array( $selected_tag ) ) { |
| 240 | foreach ( $selected_tag as $tag ) { |
| 241 | $tag_ids[] = $tag['value']; |
| 242 | $tag_names[] = esc_html( $tag['label'] ); |
| 243 | } |
| 244 | |
| 245 | $contact->attachTags( $tag_ids ); |
| 246 | } elseif ( is_string( $selected_tag ) ) { |
| 247 | $tags_arr = array_filter( explode( ',', $selected_tag ) ); |
| 248 | if ( ! class_exists( 'FluentCrm\App\Models\Tag' ) ) { |
| 249 | return [ |
| 250 | 'status' => 'error', |
| 251 | 'message' => __( 'Tag model not found.', 'suretriggers' ), |
| 252 | |
| 253 | ]; |
| 254 | } |
| 255 | foreach ( $tags_arr as $tag ) { |
| 256 | $exist = Tag::where( 'title', $tag ) |
| 257 | ->orWhere( 'slug', $tag ) |
| 258 | ->first(); |
| 259 | if ( is_null( $exist ) ) { |
| 260 | $new_tag = Tag::create( |
| 261 | [ |
| 262 | 'title' => $tag, |
| 263 | ] |
| 264 | ); |
| 265 | $tag_ids[] = $new_tag->id; |
| 266 | $tag_names[] = esc_html( $new_tag->title ); |
| 267 | } else { |
| 268 | $tag_ids[] = $exist->id; |
| 269 | $tag_names[] = esc_html( $exist->title ); |
| 270 | } |
| 271 | } |
| 272 | $contact->attachTags( $tag_ids ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | $list_ids = []; |
| 277 | $list_names = []; |
| 278 | if ( isset( $selected_options['list_id'] ) && is_array( $selected_options['list_id'] ) && ! empty( $selected_options['list_id'] ) ) { |
| 279 | foreach ( $selected_options['list_id'] as $list ) { |
| 280 | $list_ids[] = $list['value']; |
| 281 | $list_names[] = esc_html( $list['label'] ); |
| 282 | } |
| 283 | |
| 284 | $contact->attachLists( $list_ids ); |
| 285 | } |
| 286 | |
| 287 | $custom_data = $contact->custom_fields(); |
| 288 | |
| 289 | $context = []; |
| 290 | $context['contact_id'] = $contact->id; |
| 291 | $context['full_name'] = $contact->full_name; |
| 292 | $context['first_name'] = $contact->first_name; |
| 293 | $context['last_name'] = $contact->last_name; |
| 294 | $context['contact_owner'] = $contact->contact_owner; |
| 295 | $context['company_id'] = $contact->company_id; |
| 296 | $context['email'] = $contact->email; |
| 297 | $context['address_line_1'] = $contact->address_line_1; |
| 298 | $context['address_line_2'] = $contact->address_line_2; |
| 299 | $context['postal_code'] = $contact->postal_code; |
| 300 | $context['city'] = $contact->city; |
| 301 | $context['state'] = $contact->state; |
| 302 | $context['country'] = $contact->country; |
| 303 | $context['phone'] = $contact->phone; |
| 304 | $context['status'] = $contact->status; |
| 305 | $context['contact_type'] = $contact->contact_type; |
| 306 | $context['source'] = $contact->source; |
| 307 | $context['date_of_birth'] = $contact->date_of_birth; |
| 308 | $context['list_names'] = implode( ',', $list_names ); |
| 309 | $context['tag_names'] = implode( ',', $tag_names ); |
| 310 | |
| 311 | if ( ! empty( $custom_data ) ) { |
| 312 | foreach ( $custom_data as $key => $field ) { |
| 313 | if ( is_array( $field ) ) { |
| 314 | $context[ $key ] = implode( ',', $field ); |
| 315 | } else { |
| 316 | $context[ $key ] = $field; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | return $context; |
| 321 | } |
| 322 | |
| 323 | } |
| 324 | |
| 325 | AddContact::get_instance(); |
| 326 |