static function process_submission($obj) { $cf7_ctct = get_option('cf7_ctct_' . $obj->id); // Let the shortcode functionality work with the data using a filter. $cf7_ctct = apply_filters('ctctcf7_push', apply_filters('ctctcf7_push_form_' . $obj->id, $cf7_ctct, $obj), $obj); if (empty($cf7_ctct)) { return $obj; } if (empty($cf7_ctct['active']) || empty($cf7_ctct['fields']) || empty($cf7_ctct['lists'])) { return $obj; } self::get_includes(); // If it doesn't load for some reason.... if (!class_exists('CTCT_SuperClass')) { return $obj; } // If there are no fields defined. if (empty($cf7_ctct['fields'])) { return $obj; } $contact = array(); foreach ($cf7_ctct['fields'] as $key => $field) { if (empty($key) || empty($field)) { continue; } $value = self::get_submitted_value($field, $obj); $contact[$key] = self::process_field($key, $value); } $contact = self::process_contact($contact); // We need an email address to continue. if (empty($contact['email_address']) || !is_email($contact['email_address'])) { return $obj; } // For debug only # $contact['email_address'] = rand(0,10000).$contact['email_address']; // REMOVE!!!!! $CTCT_SuperClass = new CTCT_SuperClass(); $contact_exists = $CTCT_SuperClass->CC_ContactsCollection()->searchByEmail($contact['email_address']); // If there's a field to opt in, and the opt-in field is empty, return. if (!empty($cf7_ctct['accept'])) { $accept = self::get_submitted_value($cf7_ctct['accept'], $obj); if (empty($accept)) { return $obj; } $contact['opt_in_source'] = 'ACTION_BY_CONTACT'; } else { // Don't send them a welcome email $contact['opt_in_source'] = 'ACTION_BY_CUSTOMER'; } // Create a new contact. if (!$contact_exists) { $expected_response = 201; $Contact = $CTCT_SuperClass->CC_Contact($contact); $ExistingContact = false; foreach ((array) $cf7_ctct['lists'] as $list) { $Contact->setLists($list); } $Contact->setOptInSource($contact['opt_in_source']); $response = $CTCT_SuperClass->CC_ContactsCollection()->createContact($Contact, false); } else { $expected_response = 204; $Contact = false; $ExistingContact = $CTCT_SuperClass->CC_ContactsCollection()->listContactDetails($contact_exists[0][0]); $ExistingContact->setOptInSource($contact['opt_in_source']); // Update the existing contact with the new data self::mapMergeVars($contact, $ExistingContact); // Update Lists $lists = $ExistingContact->getLists(); foreach ((array) $cf7_ctct['lists'] as $list) { $lists[] = 'https://api.constantcontact.com' . $list; } // Remove dupe lists $set_lists = array_unique($lists); // Update contact lists foreach ($set_lists as $list) { $ExistingContact->setLists($list); } $response = $CTCT_SuperClass->CC_ContactsCollection()->updateContact($ExistingContact->getId(), $ExistingContact, false); } if (floatval($response['info']['http_code']) !== floatval($expected_response)) { do_action('cf7_ctct_failed', $response, $Contact, $ExistingContact); } else { do_action('cf7_ctct_succeeded', $response, $Contact, $ExistingContact); } return $obj; }
/** * Handle the form submission * * If no CTCT configurations exist or the `CTCT_SuperClass` class doesn't load, * or there's no mapped `email_address` value, then nothing happens. * * @param WPCF7_ContactForm $obj * * @return WPCF7_ContactForm */ public function process_submission($obj) { $cf_id = method_exists($obj, 'id') ? $obj->id() : $obj->id; $cf7_ctct = get_option('cf7_ctct_' . $cf_id); // Let the shortcode functionality work with the data using a filter. $cf7_ctct = apply_filters('ctctcf7_push', apply_filters('ctctcf7_push_form_' . $cf_id, $cf7_ctct, $obj), $obj); if (empty($cf7_ctct)) { return $obj; } if (empty($cf7_ctct['active']) || empty($cf7_ctct['fields'])) { return $obj; } self::get_includes(); // If it doesn't load for some reason.... if (!class_exists('CTCT_SuperClass')) { return $obj; } $contact = array(); foreach ($cf7_ctct['fields'] as $key => $field) { if (empty($key) || empty($field)) { continue; } $value = self::get_submitted_value($field, $obj); $contact[$key] = self::process_field($key, $value); } $contact = self::process_contact($contact); // We need an email address to continue. if (empty($contact['email_address']) || !is_email($contact['email_address'])) { return $obj; } // For debug only #$contact['email_address'] = str_replace( '@', rand( 0, 10000 ) . '@', $contact['email_address'] ); // REMOVE!!!!! $CTCT_SuperClass = new CTCT_SuperClass(); $contact_exists = $CTCT_SuperClass->CC_ContactsCollection()->searchByEmail($contact['email_address']); // If there's a field to opt in, and the opt-in field is empty, return. if (!empty($cf7_ctct['accept'])) { $accept = self::get_submitted_value($cf7_ctct['accept'], $obj); if (empty($accept)) { return $obj; } $contact['opt_in_source'] = 'ACTION_BY_CONTACT'; } else { // Don't send them a welcome email $contact['opt_in_source'] = 'ACTION_BY_CUSTOMER'; } $response = NULL; // calculate subscribed lists $requested_lists = self::get_submitted_lists(); // Create a new contact. if (!$contact_exists) { $expected_response = 201; $Contact = $CTCT_SuperClass->CC_Contact($contact); $ExistingContact = false; foreach ($requested_lists as $list) { $Contact->setLists($list); } $Contact->setOptInSource($contact['opt_in_source']); // Only create contact if there were requested lists if ($Contact->getLists()) { $response = $CTCT_SuperClass->CC_ContactsCollection()->createContact($Contact, false); } } else { $expected_response = 204; $Contact = false; $ExistingContact = $CTCT_SuperClass->CC_ContactsCollection()->listContactDetails($contact_exists[0][0]); $ExistingContact->setOptInSource($contact['opt_in_source']); // Update the existing contact with the new data self::mapMergeVars($contact, $ExistingContact); /** * If you want to replace contact lists instead of updating them, use `__return_false` * * @since 2.1 * * @param boolean $update True: Only add lists to existing contacts; False: Replace lists with submission selections */ if (apply_filters('ctctcf7_update_contact_lists', true)) { $lists = $ExistingContact->getLists(); } else { $lists = array(); } foreach ($requested_lists as $list) { $list_to_add = 'https://api.constantcontact.com' . $list; if (!in_array($list_to_add, $lists)) { $lists[] = $list_to_add; } } // Remove existing lists then re-add them $ExistingContact->removeLists(); // Add contact lists foreach ($lists as $list) { $ExistingContact->setLists($list); } // Only update contact if there were requested lists if ($ExistingContact->getLists()) { $response = $CTCT_SuperClass->CC_ContactsCollection()->updateContact($ExistingContact->getId(), $ExistingContact, false); } } if (empty($response['info']) || intval($response['info']['http_code']) !== intval($expected_response)) { do_action('cf7_ctct_failed', $response, $Contact, $ExistingContact); } else { do_action('cf7_ctct_succeeded', $response, $Contact, $ExistingContact); } return $obj; }