: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
if ( ! defined( 'ABSPATH' ) ) {
* Constant Contact integration.
class WPForms_Constant_Contact extends WPForms_Provider {
public $api_key = 'c58xq3r27udz59h9rrq7qnvf';
public $sign_up = 'https://constant-contact.evyy.net/c/11535/341874/3411?sharedid=wpforms';
$this->version = '1.3.6';
$this->name = 'Constant Contact';
$this->slug = 'constant-contact';
$this->icon = WPFORMS_PLUGIN_URL . 'assets/images/icon-provider-constant-contact.png';
// Admin notice requesting connecting.
$this->connect_request();
add_action( 'wpforms_admin_notice_dismiss_ajax', [ $this, 'connect_dismiss' ] );
add_filter( "wpforms_providers_provider_settings_formbuilder_display_content_default_screen_{$this->slug}", [ $this, 'builder_settings_default_content' ] );
// Provide option to override sign up link.
$sign_up = get_option( 'wpforms_constant_contact_signup', false );
$this->sign_up = esc_html( $sign_up );
* Process and submit entry to provider.
* @param array $fields List of fields with their data and settings.
* @param array $entry Submitted entry values.
* @param array $form_data Form data and settings.
* @param int $entry_id Saved entry ID.
public function process_entry( $fields, $entry, $form_data, $entry_id = 0 ) {
// Only run if this form has a connections for this provider.
if ( empty( $form_data['providers'][ $this->slug ] ) ) {
* Fire for each connection.
foreach ( $form_data['providers'][ $this->slug ] as $connection ) :
// Before proceeding make sure required fields are configured.
if ( empty( $connection['fields']['email'] ) ) {
$list_id = $connection['list_id'];
$account_id = $connection['account_id'];
$email_data = explode( '.', $connection['fields']['email'] );
$email_id = $email_data[0];
$email = $fields[ $email_id ]['value'];
$this->api_connect( $account_id );
// Email is required and Access token are required.
if ( empty( $email ) || empty( $this->access_token ) ) {
// Check for conditionals.
$pass = $this->process_conditionals( $fields, $entry, $form_data, $connection );
'Constant Contact Subscription stopped by conditional logic',
'type' => [ 'provider', 'conditional_logic' ],
'form_id' => $form_data['id'],
// Check to see if the lead already exists in Constant Contact.
$response = wp_remote_get( 'https://api.constantcontact.com/v2/contacts?api_key=' . $this->api_key . '&access_token=' . $this->access_token . '&email=' . $email );
$contact = json_decode( wp_remote_retrieve_body( $response ), true );
// Return early if there was a problem.
if ( isset( $contact['error_key'] ) ) {
'Constant Contact API Error',
$contact->get_error_message(),
'type' => [ 'provider', 'error' ],
'form_id' => $form_data['id'],
foreach ( $connection['fields'] as $name => $merge_var ) {
// Don't include Email or Full name fields.
if ( 'email' === $name ) {
// Check if merge var is mapped.
if ( empty( $merge_var ) ) {
$merge_var = explode( '.', $merge_var );
$key = ! empty( $merge_var[1] ) ? $merge_var[1] : 'value';
// Check if mapped form field has a value.
if ( empty( $fields[ $id ][ $key ] ) ) {
$value = $fields[ $id ][ $key ];
// Constant Contact doesn't native URL field so it has to be
// stored in a custom field.
$merge_vars['custom_fields'] = [
'name' => 'custom_field_1',
// Constant Contact stores name in two fields, so we have to
if ( $name === 'full_name' ) {
$names = explode( ' ', $value );
if ( ! empty( $names[0] ) ) {
$merge_vars['first_name'] = $names[0];
if ( ! empty( $names[1] ) ) {
$merge_vars['last_name'] = $names[1];
// Constant Contact stores address in multiple fields, so we
if ( $name === 'address' ) {
// Only support Address fields.
if ( $fields[ $id ]['type'] !== 'address' ) {
// Postal code may be in extended US format.
if ( ! empty( $fields[ $id ]['postal'] ) ) {
$p = explode( '-', $fields[ $id ]['postal'] );
$postal['code'] = ! empty( $p[0] ) ? $p[0] : '';
$postal['subcode'] = ! empty( $p[1] ) ? $p[1] : '';
$merge_vars['addresses'] = [
'address_type' => 'BUSINESS',
'city' => ! empty( $fields[ $id ]['city'] ) ? $fields[ $id ]['city'] : '',
'country_code' => ! empty( $fields[ $id ]['country'] ) ? $fields[ $id ]['country'] : '',
'line1' => ! empty( $fields[ $id ]['address1'] ) ? $fields[ $id ]['address1'] : '',
'line2' => ! empty( $fields[ $id ]['address2'] ) ? $fields[ $id ]['address2'] : '',
'postal_code' => $postal['code'],
'state' => ! empty( $fields[ $id ]['state'] ) ? $fields[ $id ]['state'] : '',
'sub_postal_code' => $postal['subcode'],
$merge_vars[ $name ] = $value;
// If we have a previous contact, only update the list association.
if ( ! empty( $contact['results'] ) ) {
$data = $contact['results'][0];
// Check if they are already assigned to lists.
if ( ! empty( $data['lists'] ) ) {
foreach ( $data['lists'] as $list ) {
// If they are already assigned to this list, return early.
if ( isset( $list['id'] ) && (string) $list_id === (string) $list['id'] ) {
// Otherwise, add them to the list.
$data['lists'][ count( $data['lists'] ) ] = [
// Add the contact to the list.
// Combine merge vars into data before sending.
$data = array_merge( $data, $merge_vars );
'body' => wp_json_encode( $data ),
'Content-Type' => 'application/json',
$update = wp_remote_request( 'https://api.constantcontact.com/v2/contacts/' . $data['id'] . '?api_key=' . $this->api_key . '&access_token=' . $this->access_token . '&action_by=ACTION_BY_VISITOR', $args );
$res = json_decode( wp_remote_retrieve_body( $update ), true );
'email_addresses' => [ [ 'email_address' => $email ] ],
'lists' => [ [ 'id' => $list_id ] ],
// Combine merge vars into data before sending.
$data = array_merge( $data, $merge_vars );
'body' => wp_json_encode( $data ),
'Content-Type' => 'application/json',
$add = wp_remote_post( 'https://api.constantcontact.com/v2/contacts?api_key=' . $this->api_key . '&access_token=' . $this->access_token . '&action_by=ACTION_BY_VISITOR', $args );
$res = json_decode( wp_remote_retrieve_body( $add ), true );
if ( isset( $res['error_key'] ) ) {
'Constant Contact API Error',
$res->get_error_message(),
'type' => [ 'provider', 'error' ],
'form_id' => $form_data['id'],
/************************************************************************
* API methods - these methods interact directly with the provider API. *
************************************************************************/
* Authenticate with the API.
* @param array $data Contact data.
* @param string $form_id Form ID.
* @return WP_Error|string Unique ID or error object
public function api_auth( $data = [], $form_id = '' ) {
$this->access_token = isset( $data['authcode'] ) ? $data['authcode'] : '';
$user = $this->get_account_information();
if ( is_wp_error( $user ) ) {
wpforms_update_providers_options(
'access_token' => sanitize_text_field( $data['authcode'] ),
'label' => sanitize_text_field( $data['label'] ),
* Get account information.
public function get_account_information() {
$response = wp_remote_get( 'https://api.constantcontact.com/v2/account/info?api_key=' . $this->api_key . '&access_token=' . $this->access_token );
if ( is_wp_error( $response ) ) {
$user = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! empty( $user[0]['error_key'] ) ) {
$message = ! empty( $user[0]['error_message'] ) ? $user[0]['error_message'] : '';
return new WP_Error( $this->slug . '_error', $message );
* Establish connection object to API.
* @param string $account_id
* @return mixed array or error object.
public function api_connect( $account_id ) {
if ( ! empty( $this->api[ $account_id ] ) ) {
return $this->api[ $account_id ];
$providers = wpforms_get_providers_options();
if ( ! empty( $providers[ $this->slug ][ $account_id ] ) ) {
$this->api[ $account_id ] = true;
$this->access_token = $providers[ $this->slug ][ $account_id ]['access_token'];
return $this->error( 'API error' );
* Retrieve provider account lists.
* @param string $connection_id
* @param string $account_id
* @return mixed array or error object
public function api_lists( $connection_id = '', $account_id = '' ) {
$this->api_connect( $account_id );
$request = wp_remote_get( 'https://api.constantcontact.com/v2/lists?api_key=' . $this->api_key . '&access_token=' . $this->access_token );
$lists = json_decode( wp_remote_retrieve_body( $request ), true );
'Constant Contact API Error',
'type' => [ 'provider', 'error' ],
return $this->error( esc_html__( 'API list error: Constant API error', 'wpforms-lite' ) );
* Retrieve provider account list fields.
* @param string $connection_id
* @param string $account_id
* @return mixed array or error object
public function api_fields( $connection_id = '', $account_id = '', $list_id = '' ) {