Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Subscribe an email address to a AWeber list
- *
- * @since 1.0
- * @return bool
- */
- public function subscribe_email( $subscriber = array(), $list_id = '' ) {
- $defaults = array ( 'consumer_key' => false, 'consumer_secret' => false, 'access_key' => false, 'access_secret' => false );
- $options = get_option( 'ninja_forms_aweber_options', $defaults );
- $options = wp_parse_args( (array) $options, $defaults );
- try {
- $aweber = new AWeberAPI( $options['consumer_key'], $options['consumer_secret'] );
- $account = $aweber->getAccount( $options['access_key'], $options['access_secret'] );
- // direct API URL
- $account_id = $account->id;
- $url = "/accounts/{$account_id}/lists/{$list_id}/subscribers";
- $name = isset( $subscriber['first_name'] ) ? $subscriber['first_name'] : '';
- // add space between first and last names if both exist
- if( isset( $subscriber['first_name'] ) && isset( $subscriber['last_name'] ) ){
- $name .= ' ' . $subscriber['last_name'];
- } else if ( isset( $subscriber['last_name'] ) ){
- $name .= $subscriber['last_name'];
- }
- // get custom fields
- $custom_fields = isset( $subscriber['custom_fields'] ) ? (array) $subscriber['custom_fields'] : array();
- // get IP address
- $ip_address = isset( $subscriber['ip_address'] ) ? $subscriber['ip_address'] : '';
- # create a subscriber
- $params = array(
- 'ws.op' => 'create',
- 'email' => $subscriber['email'],
- 'name' => $name,
- 'ip_address' => $ip_address,
- 'ad_tracking' => sprintf( __( 'Ninja Forms Form #%s', 'ninja-forms-aweber' ), $subscriber['form_id'] )
- );
- if( ! empty( $custom_fields ) ){
- $params['custom_fields'] = $custom_fields;
- }
- // single POST to API
- $data = $aweber->adapter->request( 'POST', $url, $params, array('return' => 'headers') );
- } catch( AWeberException $e ) {
- $error_message = $e->getMessage() ? $e->getMessage() : '';
- $error_code = null; // AWeber doesn't have codes yet, but this holds a place for when they do
- // process the AWeber message and return something i18n-ready and somewhat more friendly
- $new_message = $this->error_message( $error_message, $error_code, $subscriber, $list_id );
- // if we get an exception, generally the user can't be added to the list
- if( $new_message && apply_filters( 'ninja_forms_aweber_show_errors', true, $error_message, $error_code, $subscriber, $list_id ) ){
- global $ninja_forms_processing;
- $ninja_forms_processing->add_error( 'aweber_error', $new_message );
- }
- }
- }
- /************* truncated *****************/
- /**
- * Create better error messages for AWeber
- *
- * @param $error_message Message from AWeber
- * @param $error_code placeholder, does not exist yet in AWeber
- * @param $subscriber array of form data submission
- * @param $list_id string AWeber list id
- * @return string
- * @since 1.3.1
- */
- public function error_message( $error_message = '', $error_code = '', $subscriber = array(), $list_id = '' ) {
- // if in debug mode, return the error from AWeber without modification
- if( ! defined( 'NF_AWEBER_DEBUG' ) || ! NF_AWEBER_DEBUG ){
- switch( $error_message ){
- case strpos( $error_message, 'Consumer key is invalid' ) !== false :
- $new_message = __( 'The form could not be submitted because this form is not connected to AWeber. Please enter your authorization code in the plugin settings.', 'ninja-forms-aweber' );
- break;
- case strpos( $error_message, 'Subscriber already subscribed' ) !== false :
- $new_message = __( 'The form could not be submitted because you have already subscribed to this mailing list.', 'ninja-forms-aweber' );
- break;
- case strpos( $error_message, 'Email address blocked' ) !== false :
- $new_message = __( 'The form could not be submitted because you are using an unsupported email address. Please try another.', 'ninja-forms-aweber' );
- break;
- default:
- $new_message = __( 'We\'re sorry, but there has been an error in submitting your form. Please try again later.', 'ninja-forms-aweber' );
- }
- $error_message = apply_filters( 'ninja_forms_aweber_subscribe_error_message', $new_message, $error_code, $error_message, $subscriber, $list_id );
- }
- return $error_message;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement