Advertisement
helgatheviki

subscribe email

May 25th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.37 KB | None | 0 0
  1.     /**
  2.      * Subscribe an email address to a AWeber list
  3.      *
  4.      * @since       1.0
  5.      * @return      bool
  6.      */
  7.     public function subscribe_email( $subscriber = array(), $list_id = '' ) {
  8.  
  9.         $defaults = array ( 'consumer_key' => false, 'consumer_secret' => false, 'access_key' => false, 'access_secret' => false );
  10.         $options = get_option( 'ninja_forms_aweber_options', $defaults );
  11.         $options = wp_parse_args( (array) $options, $defaults );
  12.  
  13.         try {
  14.             $aweber = new AWeberAPI( $options['consumer_key'], $options['consumer_secret'] );
  15.             $account = $aweber->getAccount( $options['access_key'], $options['access_secret'] );
  16.  
  17.             // direct API URL
  18.             $account_id = $account->id;
  19.             $url = "/accounts/{$account_id}/lists/{$list_id}/subscribers";
  20.  
  21.             $name = isset( $subscriber['first_name'] ) ? $subscriber['first_name'] : '';
  22.  
  23.             // add space between first and last names if both exist
  24.             if( isset( $subscriber['first_name'] ) && isset( $subscriber['last_name'] ) ){
  25.                 $name .= ' ' . $subscriber['last_name'];
  26.             } else if ( isset( $subscriber['last_name'] ) ){
  27.                 $name .= $subscriber['last_name'];
  28.             }
  29.  
  30.             // get custom fields
  31.             $custom_fields = isset( $subscriber['custom_fields'] ) ? (array) $subscriber['custom_fields'] : array();
  32.  
  33.             // get IP address
  34.             $ip_address = isset( $subscriber['ip_address'] ) ? $subscriber['ip_address'] : '';
  35.  
  36.             # create a subscriber
  37.             $params = array(
  38.                 'ws.op' => 'create',
  39.                 'email' => $subscriber['email'],
  40.                 'name' => $name,
  41.                 'ip_address' => $ip_address,
  42.                 'ad_tracking' => sprintf( __( 'Ninja Forms Form #%s', 'ninja-forms-aweber' ), $subscriber['form_id'] )
  43.             );
  44.  
  45.             if( ! empty( $custom_fields ) ){
  46.                 $params['custom_fields'] = $custom_fields;
  47.             }
  48.  
  49.             // single POST to API
  50.             $data = $aweber->adapter->request( 'POST', $url, $params, array('return' => 'headers') );
  51.  
  52.         } catch( AWeberException $e ) {
  53.            
  54.             $error_message = $e->getMessage() ? $e->getMessage() : '';
  55.             $error_code = null; // AWeber doesn't have codes yet, but this holds a place for when they do
  56.            
  57.             // process the AWeber message and return something i18n-ready and somewhat more friendly
  58.             $new_message = $this->error_message( $error_message, $error_code, $subscriber, $list_id );
  59.            
  60.             // if we get an exception, generally the user can't be added to the list
  61.             if( $new_message && apply_filters( 'ninja_forms_aweber_show_errors', true, $error_message, $error_code, $subscriber, $list_id ) ){
  62.                 global $ninja_forms_processing;
  63.                 $ninja_forms_processing->add_error( 'aweber_error', $new_message );
  64.             }
  65.            
  66.         }
  67.  
  68.     }
  69.  
  70.  
  71.  
  72. /************* truncated *****************/
  73.  
  74.    
  75.     /**
  76.      * Create better error messages for AWeber
  77.      *
  78.      * @param       $error_message Message from AWeber
  79.      * @param       $error_code placeholder, does not exist yet in AWeber
  80.      * @param       $subscriber array of form data submission
  81.      * @param       $list_id string AWeber list id
  82.      * @return      string
  83.      * @since       1.3.1
  84.      */
  85.     public function error_message( $error_message = '', $error_code = '', $subscriber = array(), $list_id = '' ) {
  86.        
  87.         // if in debug mode, return the error from AWeber without modification
  88.         if( ! defined( 'NF_AWEBER_DEBUG' ) || ! NF_AWEBER_DEBUG ){
  89.  
  90.             switch( $error_message ){
  91.                 case strpos( $error_message, 'Consumer key is invalid' ) !== false :
  92.                     $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' );
  93.                     break;
  94.                 case strpos( $error_message, 'Subscriber already subscribed' ) !== false :
  95.                     $new_message = __( 'The form could not be submitted because you have already subscribed to this mailing list.', 'ninja-forms-aweber' );
  96.                     break;
  97.                 case strpos( $error_message, 'Email address blocked' ) !== false :
  98.                     $new_message = __( 'The form could not be submitted because you are using an unsupported email address. Please try another.', 'ninja-forms-aweber' );
  99.                     break;
  100.                 default:
  101.                     $new_message = __( 'We\'re sorry, but there has been an error in submitting your form. Please try again later.', 'ninja-forms-aweber' );
  102.                
  103.             }
  104.             $error_message = apply_filters( 'ninja_forms_aweber_subscribe_error_message', $new_message, $error_code, $error_message, $subscriber, $list_id );
  105.         }
  106.         return $error_message;
  107.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement