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/wp-conte.../themes/Divi/core/componen.../api/email
File: Infusionsoft.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Wrapper for Infusionsoft'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_Infusionsoft extends ET_Core_API_Email_Provider {
[9] Fix | Delete
[10] Fix | Delete
private static $_data_keys = array(
[11] Fix | Delete
'app_name' => 'client_id',
[12] Fix | Delete
'api_key' => 'api_key',
[13] Fix | Delete
);
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* @inheritDoc
[17] Fix | Delete
*/
[18] Fix | Delete
public $ACCESS_TOKEN_URL = 'https://api.infusionsoft.com/token';
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* @inheritDoc
[22] Fix | Delete
*/
[23] Fix | Delete
public $AUTHORIZATION_URL = 'https://signin.infusionsoft.com/app/oauth/authorize';
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* @inheritDoc
[27] Fix | Delete
*/
[28] Fix | Delete
public $BASE_URL = '';
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* @inheritDoc
[32] Fix | Delete
* Use this variable to hold the pattern and update $BASE_URL dynamically when needed
[33] Fix | Delete
*/
[34] Fix | Delete
public $BASE_URL_PATTERN = 'https://@app_name@.infusionsoft.com/api/xmlrpc';
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* @inheritDoc
[38] Fix | Delete
*/
[39] Fix | Delete
public $custom_fields_scope = 'account';
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* @inheritDoc
[43] Fix | Delete
*/
[44] Fix | Delete
public $name = 'Infusionsoft';
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* @inheritDoc
[48] Fix | Delete
*/
[49] Fix | Delete
public $oauth_version = '2.0';
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* @inheritDoc
[53] Fix | Delete
*/
[54] Fix | Delete
public $slug = 'infusionsoft';
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* @inheritDoc
[58] Fix | Delete
* @internal If true, oauth endpoints properties must also be defined.
[59] Fix | Delete
*/
[60] Fix | Delete
public $uses_oauth = false;
[61] Fix | Delete
[62] Fix | Delete
public function __construct( $owner = '', $account_name = '', $api_key = '' ) {
[63] Fix | Delete
parent::__construct( $owner, $account_name, $api_key );
[64] Fix | Delete
[65] Fix | Delete
$this->http->expects_json = false;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
protected function _add_contact_to_list( $contact_id, $list_id ) {
[69] Fix | Delete
$params = array( $this->data[ self::$_data_keys['api_key'] ], (int) $contact_id, (int) $list_id );
[70] Fix | Delete
$data = self::$_->prepare_xmlrpc_method_call( 'ContactService.addToGroup', $params );
[71] Fix | Delete
[72] Fix | Delete
$this->_do_request( $data );
[73] Fix | Delete
[74] Fix | Delete
if ( $this->response->ERROR ) {
[75] Fix | Delete
return false;
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
return self::$_->process_xmlrpc_response( $this->response->DATA );
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
protected function _create_contact( $contact_details ) {
[82] Fix | Delete
$params = array( $this->data[ self::$_data_keys['api_key'] ], $contact_details );
[83] Fix | Delete
$data = self::$_->prepare_xmlrpc_method_call( 'ContactService.add', $params );
[84] Fix | Delete
[85] Fix | Delete
$this->_do_request( $data );
[86] Fix | Delete
[87] Fix | Delete
if ( $this->response->ERROR ) {
[88] Fix | Delete
return false;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
$result = self::$_->process_xmlrpc_response( $this->response->DATA );
[92] Fix | Delete
[93] Fix | Delete
return $result;
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
protected function _do_request( $data ) {
[97] Fix | Delete
$this->prepare_request( $this->_get_base_url(), 'POST', false, $data );
[98] Fix | Delete
$this->request->HEADERS = array( 'Content-Type' => 'application/xml', 'Accept-Charset' => 'UTF-8' );
[99] Fix | Delete
$this->make_remote_request();
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
protected function _fetch_custom_fields( $list_id = '', $list = array() ) {
[103] Fix | Delete
$params = array( $this->data[ self::$_data_keys['api_key'] ], 'DataFormField', 100, 0, array( 'FormId' => -1 ), array( 'Name', 'Label', 'DataType', 'Values' ) );
[104] Fix | Delete
$data = self::$_->prepare_xmlrpc_method_call( 'DataService.query', $params );
[105] Fix | Delete
[106] Fix | Delete
$this->_do_request( $data );
[107] Fix | Delete
[108] Fix | Delete
if ( $this->response->ERROR ) {
[109] Fix | Delete
return array();
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
$data = self::$_->process_xmlrpc_response( $this->response->DATA );
[113] Fix | Delete
[114] Fix | Delete
foreach ( $data as &$custom_field ) {
[115] Fix | Delete
$custom_field = (array) $custom_field;
[116] Fix | Delete
$custom_field = $this->transform_data_to_our_format( $custom_field, 'custom_field' );
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
$fields = array();
[120] Fix | Delete
[121] Fix | Delete
foreach ( $data as $field ) {
[122] Fix | Delete
$field_id = $field['field_id'];
[123] Fix | Delete
$type = self::$_->array_get( $field, 'type', 'any' );
[124] Fix | Delete
[125] Fix | Delete
$field['type'] = self::$_->array_get( $this->data_keys, "custom_field_type.{$type}", 'any' );
[126] Fix | Delete
[127] Fix | Delete
if ( isset( $field['options'] ) ) {
[128] Fix | Delete
$field['options'] = explode( "\n", $field['options'] );
[129] Fix | Delete
$field['options'] = array_filter( $field['options'] );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
$fields[ $field_id ] = $field;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
return $fields;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
protected function _get_base_url() {
[139] Fix | Delete
$this->BASE_URL = str_replace( '@app_name@', $this->data[ self::$_data_keys['app_name'] ], $this->BASE_URL_PATTERN );
[140] Fix | Delete
return $this->BASE_URL;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
protected function _get_contact_by_email( $email ) {
[144] Fix | Delete
$params = array( $this->data[ self::$_data_keys['api_key'] ], 'Contact', 1, 0, array( 'Email' => $email ), array( 'Id', 'Groups' ) );
[145] Fix | Delete
$data = self::$_->prepare_xmlrpc_method_call( 'DataService.query', $params );
[146] Fix | Delete
[147] Fix | Delete
$this->_do_request( $data );
[148] Fix | Delete
[149] Fix | Delete
if ( $this->response->ERROR ) {
[150] Fix | Delete
return false;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
return self::$_->process_xmlrpc_response( $this->response->DATA );
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
protected function _optin_email_address( $email ) {
[157] Fix | Delete
$params = array( $this->data[ self::$_data_keys['api_key'] ], $email, $this->SUBSCRIBED_VIA );
[158] Fix | Delete
$data = self::$_->prepare_xmlrpc_method_call('APIEmailService.optIn', $params );
[159] Fix | Delete
[160] Fix | Delete
$this->_do_request( $data );
[161] Fix | Delete
[162] Fix | Delete
if ( $this->response->ERROR ) {
[163] Fix | Delete
return false;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
return self::$_->process_xmlrpc_response( $this->response->DATA );
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
protected function _process_custom_fields( $args ) {
[170] Fix | Delete
if ( ! isset( $args['custom_fields'] ) ) {
[171] Fix | Delete
return array();
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
$fields = array();
[175] Fix | Delete
[176] Fix | Delete
foreach ( $args['custom_fields'] as $field_id => $value ) {
[177] Fix | Delete
if ( is_array( $value ) && $value ) {
[178] Fix | Delete
// This is a multiple choice field (eg. checkbox, radio, select)
[179] Fix | Delete
$value = array_values( $value );
[180] Fix | Delete
$value = implode( ',', $value );
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
$fields[ "_{$field_id}" ] = $value;
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
return $fields;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
public function retrieve_subscribers_count() {
[190] Fix | Delete
$existing_lists = $this->data['lists'];
[191] Fix | Delete
[192] Fix | Delete
if ( empty( $existing_lists ) ) {
[193] Fix | Delete
return;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
foreach( $existing_lists as $list_id => $list_data ) {
[197] Fix | Delete
$params = array( $this->data[ self::$_data_keys['api_key'] ], 'Contact', array( 'Groups' => "%{$list_id}%" ) );
[198] Fix | Delete
$data = self::$_->prepare_xmlrpc_method_call( 'DataService.count', $params );
[199] Fix | Delete
[200] Fix | Delete
$this->_do_request( $data );
[201] Fix | Delete
[202] Fix | Delete
if ( $this->response->ERROR ) {
[203] Fix | Delete
continue;
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
$subscribers_count = self::$_->process_xmlrpc_response( $this->response->DATA );
[207] Fix | Delete
[208] Fix | Delete
if ( empty( $subscribers_count ) || self::$_->is_xmlrpc_error( $subscribers_count ) ) {
[209] Fix | Delete
$subscribers_count = 0;
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
$existing_lists[ $list_id ]['subscribers_count'] = $subscribers_count;
[213] Fix | Delete
[214] Fix | Delete
$this->data['lists'] = $existing_lists;
[215] Fix | Delete
[216] Fix | Delete
$this->save_data();
[217] Fix | Delete
}
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
/**
[221] Fix | Delete
* @inheritDoc
[222] Fix | Delete
*/
[223] Fix | Delete
protected function _process_subscriber_lists( $lists ) {
[224] Fix | Delete
$result = array();
[225] Fix | Delete
[226] Fix | Delete
if ( empty( $lists ) || ! is_array( $lists ) ) {
[227] Fix | Delete
return $result;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
foreach( $lists as $list ) {
[231] Fix | Delete
$list_id = (string) $list->Id;
[232] Fix | Delete
$list_name = (string) $list->GroupName;
[233] Fix | Delete
[234] Fix | Delete
$result[ $list_id ]['list_id'] = $list_id;
[235] Fix | Delete
$result[ $list_id ]['name'] = $list_name;
[236] Fix | Delete
$result[ $list_id ]['subscribers_count'] = 0;
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
return $result;
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
/**
[243] Fix | Delete
* @inheritDoc
[244] Fix | Delete
*/
[245] Fix | Delete
public function get_account_fields() {
[246] Fix | Delete
return array(
[247] Fix | Delete
self::$_data_keys['api_key'] => array(
[248] Fix | Delete
'label' => esc_html__( 'API Key', 'et_core' ),
[249] Fix | Delete
),
[250] Fix | Delete
self::$_data_keys['app_name'] => array(
[251] Fix | Delete
'label' => esc_html__( 'App Name', 'et_core' ),
[252] Fix | Delete
),
[253] Fix | Delete
);
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* @inheritDoc
[258] Fix | Delete
*/
[259] Fix | Delete
public function get_data_keymap( $keymap = array() ) {
[260] Fix | Delete
$keymap = array(
[261] Fix | Delete
'subscriber' => array(
[262] Fix | Delete
'name' => 'FirstName',
[263] Fix | Delete
'last_name' => 'LastName',
[264] Fix | Delete
'email' => 'Email',
[265] Fix | Delete
'custom_fields' => 'custom_fields',
[266] Fix | Delete
),
[267] Fix | Delete
'custom_field' => array(
[268] Fix | Delete
'field_id' => 'Name',
[269] Fix | Delete
'name' => 'Label',
[270] Fix | Delete
'type' => 'DataType',
[271] Fix | Delete
'options' => 'Values',
[272] Fix | Delete
),
[273] Fix | Delete
'custom_field_type' => array(
[274] Fix | Delete
// Us => Them
[275] Fix | Delete
'input' => 15,
[276] Fix | Delete
'textarea' => 16,
[277] Fix | Delete
'checkbox' => 17,
[278] Fix | Delete
'radio' => 20,
[279] Fix | Delete
'select' => 21,
[280] Fix | Delete
// Them => Us
[281] Fix | Delete
15 => 'input',
[282] Fix | Delete
16 => 'textarea',
[283] Fix | Delete
17 => 'checkbox',
[284] Fix | Delete
20 => 'radio',
[285] Fix | Delete
21 => 'select',
[286] Fix | Delete
),
[287] Fix | Delete
);
[288] Fix | Delete
[289] Fix | Delete
return parent::get_data_keymap( $keymap );
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
/**
[293] Fix | Delete
* @inheritDoc
[294] Fix | Delete
*/
[295] Fix | Delete
public function fetch_subscriber_lists() {
[296] Fix | Delete
if ( empty( $this->data[ self::$_data_keys['api_key'] ] ) || empty( $this->data[ self::$_data_keys['app_name'] ] ) ) {
[297] Fix | Delete
return $this->API_KEY_REQUIRED;
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
$this->response_data_key = false;
[301] Fix | Delete
[302] Fix | Delete
$params_count = array( $this->data[ self::$_data_keys['api_key'] ], 'ContactGroup', array( 'Id' => '%' ) );
[303] Fix | Delete
$data_count = self::$_->prepare_xmlrpc_method_call( 'DataService.count', $params_count );
[304] Fix | Delete
[305] Fix | Delete
$this->_do_request( $data_count );
[306] Fix | Delete
[307] Fix | Delete
if ( $this->response->ERROR ) {
[308] Fix | Delete
return $this->response->ERROR_MESSAGE;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
$records_count = (int) self::$_->process_xmlrpc_response( $this->http->response->DATA );
[312] Fix | Delete
[313] Fix | Delete
if ( 0 === $records_count ) {
[314] Fix | Delete
return 'success';
[315] Fix | Delete
}
[316] Fix | Delete
[317] Fix | Delete
// determine how many requests we need to retrieve all lists
[318] Fix | Delete
$number_of_additional_requests = floor( $records_count / 1000 );
[319] Fix | Delete
[320] Fix | Delete
$list_data = array();
[321] Fix | Delete
[322] Fix | Delete
for ( $i = 0; $i <= $number_of_additional_requests; $i++ ) {
[323] Fix | Delete
$params = array( $this->data[ self::$_data_keys['api_key'] ], 'ContactGroup', 1000, $i, array( 'Id' => '%' ), array( 'Id', 'GroupName' ) );
[324] Fix | Delete
$data = self::$_->prepare_xmlrpc_method_call( 'DataService.query', $params );
[325] Fix | Delete
[326] Fix | Delete
$this->_do_request( $data );
[327] Fix | Delete
[328] Fix | Delete
if ( $this->http->response->ERROR ) {
[329] Fix | Delete
return $this->http->response->ERROR_MESSAGE;
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
$response = self::$_->process_xmlrpc_response( $this->http->response->DATA );
[333] Fix | Delete
[334] Fix | Delete
if ( self::$_->is_xmlrpc_error( $response ) ) {
[335] Fix | Delete
return $response->faultString;
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
$list_data = array_merge( $list_data, $response );
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
$lists = $this->_process_subscriber_lists( $list_data );
[342] Fix | Delete
[343] Fix | Delete
if ( false === $lists ) {
[344] Fix | Delete
return $this->response->ERROR_MESSAGE;
[345] Fix | Delete
} else if ( self::$_->is_xmlrpc_error( $lists ) ) {
[346] Fix | Delete
return $lists->faultString;
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
$result = 'success';
[350] Fix | Delete
[351] Fix | Delete
$this->data['lists'] = $lists;
[352] Fix | Delete
$this->data['custom_fields'] = $this->_fetch_custom_fields();
[353] Fix | Delete
$this->data['is_authorized'] = true;
[354] Fix | Delete
[355] Fix | Delete
// retrieve counts right away if it can be done in reasonable time ( in 20 seconds )
[356] Fix | Delete
if ( 20 >= count( $lists ) ) {
[357] Fix | Delete
$this->retrieve_subscribers_count();
[358] Fix | Delete
} else {
[359] Fix | Delete
// estimate the time for all lists update assuming that one list can be updated in 1 second
[360] Fix | Delete
$estimated_time = ceil( count( $lists ) / 60 );
[361] Fix | Delete
$result = array(
[362] Fix | Delete
'need_counts_update' => true,
[363] Fix | Delete
'message' => sprintf(
[364] Fix | Delete
esc_html__( 'Successfully authorized. Subscribers count will be updated in background, please check back in %1$s %2$s', 'et_core' ),
[365] Fix | Delete
$estimated_time,
[366] Fix | Delete
1 === (int) $estimated_time ? esc_html__( 'minute', 'et_core' ) : esc_html__( 'minutes', 'et_core' )
[367] Fix | Delete
),
[368] Fix | Delete
);
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
$this->save_data();
[372] Fix | Delete
[373] Fix | Delete
return $result;
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
/**
[377] Fix | Delete
* @inheritDoc
[378] Fix | Delete
*/
[379] Fix | Delete
public function subscribe( $args, $url = '' ) {
[380] Fix | Delete
if ( empty( $this->data[ self::$_data_keys['api_key'] ] ) || empty( $this->data[ self::$_data_keys['app_name'] ] ) ) {
[381] Fix | Delete
return $this->API_KEY_REQUIRED;
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
$message = '';
[385] Fix | Delete
$search_result = $this->_get_contact_by_email( $args['email'] );
[386] Fix | Delete
[387] Fix | Delete
if ( false === $search_result ) {
[388] Fix | Delete
return $this->response->ERROR_MESSAGE;
[389] Fix | Delete
} else if ( self::$_->is_xmlrpc_error( $search_result ) ) {
[390] Fix | Delete
return $search_result->faultString;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
if ( ! empty( $search_result ) ) {
[394] Fix | Delete
$message = esc_html__( 'Already subscribed', 'bloom' );
[395] Fix | Delete
[396] Fix | Delete
if ( false === strpos( $search_result[0]->Groups, $args['list_id'] ) ) {
[397] Fix | Delete
$result = $this->_add_contact_to_list( $search_result[0]->Id, $args['list_id'] );
[398] Fix | Delete
$message = 'success';
[399] Fix | Delete
[400] Fix | Delete
if ( false === $result ) {
[401] Fix | Delete
return $this->response->ERROR_MESSAGE;
[402] Fix | Delete
} else if ( self::$_->is_xmlrpc_error( $result ) ) {
[403] Fix | Delete
return $result->faultString;
[404] Fix | Delete
}
[405] Fix | Delete
}
[406] Fix | Delete
} else {
[407] Fix | Delete
$custom_fields = $this->_process_custom_fields( $args );
[408] Fix | Delete
$contact_details = array(
[409] Fix | Delete
'FirstName' => $args['name'],
[410] Fix | Delete
'LastName' => $args['last_name'],
[411] Fix | Delete
'Email' => $args['email'],
[412] Fix | Delete
);
[413] Fix | Delete
[414] Fix | Delete
$new_contact_id = $this->_create_contact( array_merge( $contact_details, $custom_fields ) );
[415] Fix | Delete
[416] Fix | Delete
if ( false === $new_contact_id ) {
[417] Fix | Delete
return $this->response->ERROR_MESSAGE;
[418] Fix | Delete
} else if ( self::$_->is_xmlrpc_error( $new_contact_id ) ) {
[419] Fix | Delete
return $new_contact_id->faultString;
[420] Fix | Delete
}
[421] Fix | Delete
[422] Fix | Delete
$result = $this->_add_contact_to_list( $new_contact_id, $args['list_id'] );
[423] Fix | Delete
[424] Fix | Delete
if ( false === $result ) {
[425] Fix | Delete
return $this->response->ERROR_MESSAGE;
[426] Fix | Delete
} else if ( self::$_->is_xmlrpc_error( $result ) ) {
[427] Fix | Delete
return $search_result->faultString;
[428] Fix | Delete
}
[429] Fix | Delete
[430] Fix | Delete
if ( $this->_optin_email_address( $args['email'] ) ) {
[431] Fix | Delete
$message = 'success';
[432] Fix | Delete
}
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
return ( '' !== $message ) ? $message : $this->FAILURE_MESSAGE;
[436] Fix | Delete
}
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function