Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../themes/Divi/core/componen.../api/email
File: MailChimp.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Wrapper for MailChimp's API.
[3] Fix | Delete
*
[4] Fix | Delete
* @since 1.1.0
[5] Fix | Delete
*
[6] Fix | Delete
* @package ET\Core\API\Email
[7] Fix | Delete
*/
[8] Fix | Delete
class ET_Core_API_Email_MailChimp extends ET_Core_API_Email_Provider {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* @inheritDoc
[12] Fix | Delete
*/
[13] Fix | Delete
public $BASE_URL = '';
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Use this variable to hold the pattern and update $BASE_URL dynamically when needed
[17] Fix | Delete
*/
[18] Fix | Delete
public $BASE_URL_PATTERN = 'https://@datacenter@.api.mailchimp.com/3.0';
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* @inheritDoc
[22] Fix | Delete
*/
[23] Fix | Delete
public $http_auth = array(
[24] Fix | Delete
'username' => '-',
[25] Fix | Delete
'password' => 'api_key',
[26] Fix | Delete
);
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* @inheritDoc
[30] Fix | Delete
*/
[31] Fix | Delete
public $name = 'MailChimp';
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* @inheritDoc
[35] Fix | Delete
*/
[36] Fix | Delete
public $slug = 'mailchimp';
[37] Fix | Delete
[38] Fix | Delete
public function __construct( $owner, $account_name, $api_key = '' ) {
[39] Fix | Delete
parent::__construct( $owner, $account_name, $api_key );
[40] Fix | Delete
[41] Fix | Delete
if ( ! empty( $this->data['api_key'] ) ) {
[42] Fix | Delete
$this->_set_base_url();
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
$this->http_auth['username'] = $owner;
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
protected function _add_note_to_subscriber( $email, $url ) {
[49] Fix | Delete
$email = md5( $email );
[50] Fix | Delete
[51] Fix | Delete
$this->prepare_request( "{$url}/$email/notes", 'POST' );
[52] Fix | Delete
[53] Fix | Delete
$this->request->BODY = json_encode( array( 'note' => $this->SUBSCRIBED_VIA ) );
[54] Fix | Delete
[55] Fix | Delete
$this->make_remote_request();
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
protected function _fetch_custom_fields( $list_id = '', $list = array() ) {
[59] Fix | Delete
$this->response_data_key = 'merge_fields';
[60] Fix | Delete
[61] Fix | Delete
$this->prepare_request( "{$this->BASE_URL}/lists/{$list_id}/merge-fields?count={$this->COUNT}" );
[62] Fix | Delete
[63] Fix | Delete
$fields = parent::_fetch_custom_fields( $list_id, $list );
[64] Fix | Delete
[65] Fix | Delete
foreach ( $fields as $id => $field ) {
[66] Fix | Delete
if ( in_array( $id, array( 1, 2 ) ) ) {
[67] Fix | Delete
unset( $fields[ $id ] );
[68] Fix | Delete
}
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
// MailChimp is weird in that they treat checkbox fields as an entirely different concept in their API (Groups)
[72] Fix | Delete
// We'll grab the groups and treat them as checkbox fields in our UI.
[73] Fix | Delete
$groups = $this->_fetch_subscriber_list_groups( $list_id );
[74] Fix | Delete
[75] Fix | Delete
return $fields + $groups;
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
protected function _fetch_subscriber_list_group_options( $list_id, $group_id ) {
[79] Fix | Delete
$this->prepare_request( "{$this->BASE_URL}/lists/{$list_id}/interest-categories/{$group_id}/interests?count={$this->COUNT}" );
[80] Fix | Delete
[81] Fix | Delete
$this->make_remote_request();
[82] Fix | Delete
[83] Fix | Delete
if ( $this->response->ERROR ) {
[84] Fix | Delete
et_debug( $this->get_error_message() );
[85] Fix | Delete
[86] Fix | Delete
return array();
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
$data = $this->response->DATA['interests'];
[90] Fix | Delete
$options = array();
[91] Fix | Delete
[92] Fix | Delete
foreach ( $data as $option ) {
[93] Fix | Delete
$option = $this->transform_data_to_our_format( $option, 'group_option' );
[94] Fix | Delete
$id = $option['id'];
[95] Fix | Delete
[96] Fix | Delete
$options[ $id ] = $option['name'];
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
return $options;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
protected function _fetch_subscriber_list_groups( $list_id ) {
[103] Fix | Delete
$this->response_data_key = 'categories';
[104] Fix | Delete
[105] Fix | Delete
$this->prepare_request( "{$this->BASE_URL}/lists/{$list_id}/interest-categories?count={$this->COUNT}" );
[106] Fix | Delete
$this->make_remote_request();
[107] Fix | Delete
[108] Fix | Delete
$groups = array();
[109] Fix | Delete
[110] Fix | Delete
if ( false !== $this->response_data_key && empty( $this->response_data_key ) ) {
[111] Fix | Delete
// Let child class handle parsing the response data themselves.
[112] Fix | Delete
return $groups;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
if ( $this->response->ERROR ) {
[116] Fix | Delete
et_debug( $this->get_error_message() );
[117] Fix | Delete
[118] Fix | Delete
return $groups;
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
$data = $this->response->DATA[ $this->response_data_key ];
[122] Fix | Delete
[123] Fix | Delete
foreach ( $data as $group ) {
[124] Fix | Delete
$group = $this->transform_data_to_our_format( $group, 'group' );
[125] Fix | Delete
$field_id = $group['field_id'];
[126] Fix | Delete
$type = $group['type'];
[127] Fix | Delete
[128] Fix | Delete
if ( 'hidden' === $type ) {
[129] Fix | Delete
// MailChimp only allows groups of type: 'checkbox' to be hidden.
[130] Fix | Delete
$group['type'] = 'checkbox';
[131] Fix | Delete
$group['hidden'] = true;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
$group['is_group'] = true;
[135] Fix | Delete
$group['options'] = $this->_fetch_subscriber_list_group_options( $list_id, $field_id );
[136] Fix | Delete
$group['type'] = self::$_->array_get( $this->data_keys, "custom_field_type.{$type}", 'text' );
[137] Fix | Delete
[138] Fix | Delete
$groups[ $field_id ] = $group;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
return $groups;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
protected function _process_custom_fields( $args ) {
[145] Fix | Delete
if ( ! isset( $args['custom_fields'] ) ) {
[146] Fix | Delete
return $args;
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
$fields = $args['custom_fields'];
[150] Fix | Delete
$list_id = self::$_->array_get( $args, 'list_id', '' );
[151] Fix | Delete
[152] Fix | Delete
unset( $args['custom_fields'] );
[153] Fix | Delete
unset( $args['list_id'] );
[154] Fix | Delete
[155] Fix | Delete
$custom_fields_data = self::$_->array_get( $this->data, "lists.{$list_id}.custom_fields", array() );
[156] Fix | Delete
[157] Fix | Delete
foreach ( $fields as $field_id => $value ) {
[158] Fix | Delete
$is_group = self::$_->array_get( $custom_fields_data, "{$field_id}.is_group", false );
[159] Fix | Delete
[160] Fix | Delete
if ( is_array( $value ) && $value ) {
[161] Fix | Delete
foreach ( $value as $id => $field_value ) {
[162] Fix | Delete
if ( $is_group ) {
[163] Fix | Delete
// If it is a group custom field, set as `interests` and don't process the `merge_fields`
[164] Fix | Delete
self::$_->array_set( $args, "interests.{$id}", true );
[165] Fix | Delete
$field_id = false;
[166] Fix | Delete
} else {
[167] Fix | Delete
$value = $field_value;
[168] Fix | Delete
}
[169] Fix | Delete
}
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
if ( false === $field_id ) {
[173] Fix | Delete
continue;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
// In previous version of Mailchimp implementation we only supported default field tag, but it can be customized and our code fails.
[177] Fix | Delete
// Added `field_tag` attribute which is actual field tag. Fallback to default field tag if `field_tag` doesn't exist for backward compatibility.
[178] Fix | Delete
$custom_field_tag = self::$_->array_get( $custom_fields_data, "{$field_id}.field_tag", "MMERGE{$field_id}" );
[179] Fix | Delete
[180] Fix | Delete
// Need to strips existing slash chars.
[181] Fix | Delete
self::$_->array_set( $args, "merge_fields.{$custom_field_tag}", stripslashes( $value ) );
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
return $args;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
protected function _set_base_url() {
[188] Fix | Delete
$api_key_pieces = explode( '-', $this->data['api_key'] );
[189] Fix | Delete
$datacenter = empty( $api_key_pieces[1] ) ? '' : $api_key_pieces[1];
[190] Fix | Delete
$this->BASE_URL = str_replace( '@datacenter@', $datacenter, $this->BASE_URL_PATTERN );
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* @inheritDoc
[195] Fix | Delete
*/
[196] Fix | Delete
public function fetch_subscriber_lists() {
[197] Fix | Delete
if ( empty( $this->data['api_key'] ) ) {
[198] Fix | Delete
return $this->API_KEY_REQUIRED;
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
$this->_set_base_url();
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* The maximum number of subscriber lists to request from MailChimp's API.
[205] Fix | Delete
*
[206] Fix | Delete
* @since 2.0.0
[207] Fix | Delete
*
[208] Fix | Delete
* @param int $max_lists
[209] Fix | Delete
*/
[210] Fix | Delete
$max_lists = (int) apply_filters( 'et_core_api_email_mailchimp_max_lists', 250 );
[211] Fix | Delete
[212] Fix | Delete
$url = "{$this->BASE_URL}/lists?count={$max_lists}&fields=lists.name,lists.id,lists.stats,lists.double_optin";
[213] Fix | Delete
[214] Fix | Delete
$this->prepare_request( $url );
[215] Fix | Delete
[216] Fix | Delete
$this->response_data_key = 'lists';
[217] Fix | Delete
[218] Fix | Delete
$result = parent::fetch_subscriber_lists();
[219] Fix | Delete
[220] Fix | Delete
return $result;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* @inheritDoc
[225] Fix | Delete
*/
[226] Fix | Delete
public function get_account_fields() {
[227] Fix | Delete
return array(
[228] Fix | Delete
'api_key' => array(
[229] Fix | Delete
'label' => esc_html__( 'API Key', 'et_core' ),
[230] Fix | Delete
),
[231] Fix | Delete
);
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
/**
[235] Fix | Delete
* @inheritDoc
[236] Fix | Delete
*/
[237] Fix | Delete
public function get_data_keymap( $keymap = array() ) {
[238] Fix | Delete
$keymap = array(
[239] Fix | Delete
'list' => array(
[240] Fix | Delete
'list_id' => 'id',
[241] Fix | Delete
'name' => 'name',
[242] Fix | Delete
'double_optin' => 'double_optin',
[243] Fix | Delete
'subscribers_count' => 'stats.member_count',
[244] Fix | Delete
),
[245] Fix | Delete
'subscriber' => array(
[246] Fix | Delete
'email' => 'email_address',
[247] Fix | Delete
'name' => 'merge_fields.FNAME',
[248] Fix | Delete
'last_name' => 'merge_fields.LNAME',
[249] Fix | Delete
'custom_fields' => 'custom_fields',
[250] Fix | Delete
),
[251] Fix | Delete
'error' => array(
[252] Fix | Delete
'error_message' => 'detail',
[253] Fix | Delete
),
[254] Fix | Delete
'custom_field' => array(
[255] Fix | Delete
'field_id' => 'merge_id',
[256] Fix | Delete
'field_tag' => 'tag',
[257] Fix | Delete
'name' => 'name',
[258] Fix | Delete
'type' => 'type',
[259] Fix | Delete
'hidden' => '!public',
[260] Fix | Delete
'options' => 'options.choices',
[261] Fix | Delete
),
[262] Fix | Delete
'custom_field_type' => array(
[263] Fix | Delete
// Us <=> Them
[264] Fix | Delete
'radio' => 'radio',
[265] Fix | Delete
// Us => Them
[266] Fix | Delete
'input' => 'text',
[267] Fix | Delete
'select' => 'dropdown',
[268] Fix | Delete
'checkbox' => 'checkboxes',
[269] Fix | Delete
// Them => Us
[270] Fix | Delete
'text' => 'input',
[271] Fix | Delete
'dropdown' => 'select',
[272] Fix | Delete
'checkboxes' => 'checkbox',
[273] Fix | Delete
),
[274] Fix | Delete
'group' => array(
[275] Fix | Delete
'field_id' => 'id',
[276] Fix | Delete
'name' => 'title',
[277] Fix | Delete
'type' => 'type',
[278] Fix | Delete
),
[279] Fix | Delete
'group_option' => array(
[280] Fix | Delete
'id' => 'id',
[281] Fix | Delete
'name' => 'name',
[282] Fix | Delete
),
[283] Fix | Delete
);
[284] Fix | Delete
[285] Fix | Delete
return parent::get_data_keymap( $keymap );
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
public function get_subscriber( $list_id, $email ) {
[289] Fix | Delete
$hash = md5( strtolower( $email ) );
[290] Fix | Delete
$this->prepare_request( implode( '/', array( $this->BASE_URL, 'lists', $list_id, 'members', $hash ) ) );
[291] Fix | Delete
$this->make_remote_request();
[292] Fix | Delete
[293] Fix | Delete
return $this->response->STATUS_CODE !== 200 ? null : $this->response->DATA;
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
/**
[297] Fix | Delete
* @inheritDoc
[298] Fix | Delete
*/
[299] Fix | Delete
public function subscribe( $args, $url = '' ) {
[300] Fix | Delete
$list_id = $args['list_id'];
[301] Fix | Delete
$args = $this->transform_data_to_provider_format( $args, 'subscriber' );
[302] Fix | Delete
$url = "{$this->BASE_URL}/lists/{$list_id}/members";
[303] Fix | Delete
$email = $args['email_address'];
[304] Fix | Delete
$err = esc_html__( 'An error occurred, please try later.', 'et_core' );
[305] Fix | Delete
$dbl_optin = self::$_->array_get( $this->data, "lists.{$list_id}.double_optin", true );
[306] Fix | Delete
[307] Fix | Delete
$ip_address = 'true' === self::$_->array_get( $args, 'ip_address', 'true' ) ? et_core_get_ip_address() : '0.0.0.0';
[308] Fix | Delete
[309] Fix | Delete
$args['ip_signup'] = $ip_address;
[310] Fix | Delete
$args['status'] = $dbl_optin ? 'pending' : 'subscribed';
[311] Fix | Delete
$args['list_id'] = $list_id;
[312] Fix | Delete
[313] Fix | Delete
$args = $this->_process_custom_fields( $args );
[314] Fix | Delete
[315] Fix | Delete
$this->prepare_request( $url, 'POST', false, $args, true );
[316] Fix | Delete
$result = parent::subscribe( $args, $url );
[317] Fix | Delete
[318] Fix | Delete
if ( false !== stripos( $result, 'already a list member' ) ) {
[319] Fix | Delete
$result = $err;
[320] Fix | Delete
[321] Fix | Delete
if ( $user = $this->get_subscriber( $list_id, $email ) ) {
[322] Fix | Delete
if ( 'subscribed' === $user['status'] ) {
[323] Fix | Delete
$result = 'success';
[324] Fix | Delete
} else {
[325] Fix | Delete
$this->prepare_request( implode( '/', array( $url, $user['id'] ) ), 'PUT', false, $args, true );
[326] Fix | Delete
[327] Fix | Delete
$result = parent::subscribe( $args, $url );
[328] Fix | Delete
}
[329] Fix | Delete
}
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
if ( 'success' === $result ) {
[333] Fix | Delete
$this->_add_note_to_subscriber( $email, $url );
[334] Fix | Delete
} else if ( false !== stripos( $result, 'has signed up to a lot of lists ' ) ) {
[335] Fix | Delete
// return message which can be translated. Generic Mailchimp messages are not translatable.
[336] Fix | Delete
$result = esc_html__( 'You have signed up to a lot of lists very recently, please try again later', 'et_core' );
[337] Fix | Delete
} else {
[338] Fix | Delete
$result = $err;
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
return $result;
[342] Fix | Delete
}
[343] Fix | Delete
}
[344] Fix | Delete
[345] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function