: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Wrapper for ActiveCampaign's API.
* @package ET\Core\API\Email
class ET_Core_API_Email_ActiveCampaign extends ET_Core_API_Email_Provider {
public $name = 'ActiveCampaign';
public $slug = 'activecampaign';
public $uses_oauth = false;
public $custom_fields_scope = 'account';
protected function _fetch_custom_fields( $list_id = '', $list = array() ) {
foreach ( $list['fields'] as $field ) {
$field = $this->transform_data_to_our_format( $field, 'custom_field' );
$field_id = $field['field_id'];
$field['type'] = self::$_->array_get( $this->data_keys, "custom_field_type.{$type}", 'text' );
if ( isset( $field['options'] ) ) {
foreach ( $field['options'] as $option ) {
$option = $this->transform_data_to_our_format( $option, 'custom_field_option' );
$options[ $id ] = $option['name'];
$field['options'] = $options;
$fields[ $field_id ] = $field;
protected function _process_custom_fields( $args ) {
if ( ! isset( $args['custom_fields'] ) ) {
$fields = $args['custom_fields'];
unset( $args['custom_fields'] );
foreach ( $fields as $field_id => $value ) {
if ( is_array( $value ) && $value ) {
// This is a multiple choice field (eg. checkbox, radio, select)
$value = array_values( $value );
if ( 'checkbox' === $this->data['custom_fields'][ $field_id ]['type'] ) {
$value = implode( '||', $value );
$value = array_pop( $value );
self::$_->array_set( $args, "field[{$field_id},0]", $value );
* Returns the requests URL for the account assigned to this class instance.
protected function _get_requests_url() {
$base_url = untrailingslashit( $this->data['api_url'] );
return "{$base_url}/admin/api.php";
public function get_account_fields() {
'label' => esc_html__( 'API Key', 'et_core' ),
'label' => esc_html__( 'API URL', 'et_core' ),
'apply_password_mask' => false,
'label' => esc_html__( 'Form ID', 'et_core' ),
'apply_password_mask' => false,
public function get_data_keymap( $keymap = array() ) {
'subscribers_count' => 'subscriber_count',
'last_name' => 'last_name',
'custom_fields' => 'custom_fields',
'error_message' => 'result_message',
'custom_field_option' => array(
'custom_field_type' => array(
'checkbox' => 'checkbox',
'textarea' => 'textarea',
return parent::get_data_keymap( $keymap );
public function fetch_subscriber_lists() {
if ( empty( $this->data['api_key'] ) || empty( $this->data['api_url'] ) ) {
return $this->API_KEY_REQUIRED;
'api_key' => $this->data['api_key'],
'api_action' => 'list_list',
$request_url = add_query_arg( $query_args, $this->_get_requests_url() );
$request_url = esc_url_raw( $request_url, array( 'https' ) );
$this->prepare_request( $request_url );
$this->request->HEADERS['Content-Type'] = 'application/x-www-form-urlencoded';
parent::fetch_subscriber_lists();
if ( $this->response->ERROR ) {
return $this->get_error_message();
foreach ( $this->response->DATA as $key => $list_data ) {
if ( ! is_numeric( $key ) ) {
if ( ! empty( $list_data ) ) {
$this->data['lists'] = $this->_process_subscriber_lists( $lists );
$this->data['custom_fields'] = $this->_fetch_custom_fields( '', array_shift( $this->response->DATA ) );
$this->data['is_authorized'] = 'true';
* Get ActiveCampaign subscriber info by email.
public function get_subscriber( $email ) {
'api_key' => $this->data['api_key'],
'api_action' => 'subscriber_view_email',
// Build request URL. This action only accept GET method.
$request_url = add_query_arg( $query_args, $this->_get_requests_url() );
$request_url = esc_url_raw( $request_url, array( 'https' ) );
// Prepare and send the request.
$this->prepare_request( $request_url );
$this->request->HEADERS['Content-Type'] = 'application/x-www-form-urlencoded';
$this->make_remote_request();
// Ensure no error happen and it's included in one of the lists.
$list_id = self::$_->array_get( $this->response->DATA, 'listid', false );
$result_code = self::$_->array_get( $this->response->DATA, 'result_code', false );
if ( $this->response->ERROR || ! $list_id || ! $result_code ) {
return $this->response->DATA;
public function subscribe( $args, $url= '' ) {
// Ensure to skip subscribe action if current email already subscribed.
$subscriber_data = $this->get_subscriber( $args['email'] );
$subscriber_lists = self::$_->array_get( $subscriber_data, 'lists', array() );
$subscriber_list = self::$_->array_get( $subscriber_lists, $args['list_id'], false );
if ( $subscriber_list ) {
$list_id_key = 'p[' . $args['list_id'] . ']';
$status_key = 'status[' . $args['list_id'] . ']';
$responders_key = 'instantresponders[' . $args['list_id'] . ']';
$list_id = $args['list_id'];
$args = $this->transform_data_to_provider_format( $args, 'subscriber', array( 'list_id' ) );
$args = $this->_process_custom_fields( $args );
'api_key' => $this->data['api_key'],
'api_action' => 'contact_sync',
$url = esc_url_raw( add_query_arg( $query_args, $this->_get_requests_url() ) );
$args[ $list_id_key ] = $list_id;
$args[ $status_key ] = 1;
$args[ $responders_key ] = 1;
if ( ! empty( $this->data['form_id'] ) ) {
$args['form'] = (int) $this->data['form_id'];
$this->prepare_request( $url, 'POST', false, $args );
$this->request->HEADERS['Content-Type'] = 'application/x-www-form-urlencoded';
return parent::subscribe( $args, $url );