Example #1
1
function et_pb_submit_subscribe_form()
{
    if (!wp_verify_nonce($_POST['et_load_nonce'], 'et_load_nonce')) {
        die(json_encode(array('error' => __('Configuration error', 'Divi'))));
    }
    $service = sanitize_text_field($_POST['et_service']);
    $list_id = sanitize_text_field($_POST['et_list_id']);
    $email = sanitize_email($_POST['et_email']);
    $firstname = sanitize_text_field($_POST['et_firstname']);
    if ('' === $firstname) {
        die(json_encode(array('error' => __('Please enter first name', 'Divi'))));
    }
    if (!is_email(sanitize_email($_POST['et_email']))) {
        die(json_encode(array('error' => __('Incorrect email', 'Divi'))));
    }
    if ('' == $list_id) {
        die(json_encode(array('error' => __('Configuration error: List is not defined', 'Divi'))));
    }
    $success_message = __('<h2 class="et_pb_subscribed">Subscribed - look for the confirmation email!</h2>', 'Divi');
    switch ($service) {
        case 'mailchimp':
            $lastname = sanitize_text_field($_POST['et_lastname']);
            $email = array('email' => $email);
            if (!class_exists('MailChimp_Divi')) {
                require_once ET_BUILDER_DIR . 'subscription/mailchimp/mailchimp.php';
            }
            if (et_is_builder_plugin_active()) {
                $mailchimp_api_option = get_option('et_pb_builder_options');
                $mailchimp_api_key = isset($mailchimp_api_option['newsletter_main_mailchimp_key']) ? $mailchimp_api_option['newsletter_main_mailchimp_key'] : '';
            } else {
                $mailchimp_api_key = et_get_option('divi_mailchimp_api_key');
            }
            if ('' === $mailchimp_api_key) {
                die(json_encode(array('error' => __('Configuration error: api key is not defined', 'Divi'))));
            }
            $mailchimp = new MailChimp_Divi($mailchimp_api_key);
            $subscribe_args = array('id' => $list_id, 'email' => $email, 'merge_vars' => array('FNAME' => $firstname, 'LNAME' => $lastname));
            $retval = $mailchimp->call('lists/subscribe', $subscribe_args);
            if (200 !== wp_remote_retrieve_response_code($retval)) {
                if ('214' === wp_remote_retrieve_header($retval, 'x-mailchimp-api-error-code')) {
                    $mailchimp_message = json_decode(wp_remote_retrieve_body($retval), true);
                    $error_message = isset($mailchimp_message['error']) ? $mailchimp_message['error'] : wp_remote_retrieve_body($retval);
                    $result = json_encode(array('success' => $error_message));
                } else {
                    $result = json_encode(array('success' => wp_remote_retrieve_response_message($retval)));
                }
            } else {
                $result = json_encode(array('success' => $success_message));
            }
            die($result);
            break;
        case 'aweber':
            if (!class_exists('AWeberAPI')) {
                require_once ET_BUILDER_DIR . 'subscription/aweber/aweber_api.php';
            }
            $account = et_pb_get_aweber_account();
            if (!$account) {
                die(json_encode(array('error' => __('Aweber: Wrong configuration data', 'Divi'))));
            }
            try {
                $list_url = "/accounts/{$account->id}/lists/{$list_id}";
                $list = $account->loadFromUrl($list_url);
                $new_subscriber = $list->subscribers->create(array('email' => $email, 'name' => $firstname));
                die(json_encode(array('success' => $success_message)));
            } catch (Exception $exc) {
                die(json_encode(array('error' => $exc->message)));
            }
            break;
    }
    die;
}
Example #2
0
function et_pb_submit_subscribe_form()
{
    if (!wp_verify_nonce($_POST['et_load_nonce'], 'et_load_nonce')) {
        die(json_encode(array('error' => __('Configuration error', 'Divi'))));
    }
    $service = sanitize_text_field($_POST['et_service']);
    $list_id = sanitize_text_field($_POST['et_list_id']);
    $email = sanitize_email($_POST['et_email']);
    $firstname = sanitize_text_field($_POST['et_firstname']);
    if ('' === $firstname) {
        die(json_encode(array('error' => __('Please enter first name', 'Divi'))));
    }
    if (!is_email(sanitize_email($_POST['et_email']))) {
        die(json_encode(array('error' => __('Incorrect email', 'Divi'))));
    }
    if ('' == $list_id) {
        die(json_encode(array('error' => __('Configuration error: List is not defined', 'Divi'))));
    }
    $success_message = __('<h2 class="et_pb_subscribed">Subscribed - look for the confirmation email!</h2>', 'Divi');
    switch ($service) {
        case 'mailchimp':
            $lastname = sanitize_text_field($_POST['et_lastname']);
            $email = array('email' => $email);
            if (!class_exists('MailChimp_Divi')) {
                require_once ET_BUILDER_DIR . 'subscription/mailchimp/mailchimp.php';
            }
            $mailchimp_api_key = et_get_option('divi_mailchimp_api_key');
            if ('' === $mailchimp_api_key) {
                die(json_encode(array('error' => __('Configuration error: api key is not defined', 'Divi'))));
            }
            $mailchimp = new MailChimp_Divi($mailchimp_api_key);
            $merge_vars = array('FNAME' => $firstname, 'LNAME' => $lastname);
            $retval = $mailchimp->call('lists/subscribe', array('id' => $list_id, 'email' => $email, 'merge_vars' => $merge_vars));
            if (isset($retval['error'])) {
                if ('214' == $retval['code']) {
                    $error_message = str_replace('Click here to update your profile.', '', $retval['error']);
                    $result = json_encode(array('success' => $error_message));
                } else {
                    $result = json_encode(array('success' => $retval['error']));
                }
            } else {
                $result = json_encode(array('success' => $success_message));
            }
            die($result);
            break;
        case 'aweber':
            if (!class_exists('AWeberAPI')) {
                require_once ET_BUILDER_DIR . 'subscription/aweber/aweber_api.php';
            }
            $account = et_pb_get_aweber_account();
            if (!$account) {
                die(json_encode(array('error' => __('Aweber: Wrong configuration data', 'Divi'))));
            }
            try {
                $list_url = "/accounts/{$account->id}/lists/{$list_id}";
                $list = $account->loadFromUrl($list_url);
                $new_subscriber = $list->subscribers->create(array('email' => $email, 'name' => $firstname));
                die(json_encode(array('success' => $success_message)));
            } catch (Exception $exc) {
                die(json_encode(array('error' => $exc->message)));
            }
            break;
    }
    die;
}
Example #3
0
 function et_pb_get_mailchimp_lists($regenerate_mailchimp_list = 'off')
 {
     $lists = array();
     if (et_is_builder_plugin_active()) {
         $mailchimp_api_option = get_option('et_pb_builder_options');
         $mailchimp_api_key = isset($mailchimp_api_option['newsletter_main_mailchimp_key']) ? $mailchimp_api_option['newsletter_main_mailchimp_key'] : '';
     } else {
         $mailchimp_api_key = et_get_option('divi_mailchimp_api_key');
         $regenerate_mailchimp_list = et_get_option('divi_regenerate_mailchimp_lists', 'false');
     }
     if (empty($mailchimp_api_key) || false === strpos($mailchimp_api_key, '-')) {
         return false;
     }
     $et_pb_mailchimp_lists = get_transient('et_pb_mailchimp_lists');
     if ('on' === $regenerate_mailchimp_list || false === $et_pb_mailchimp_lists) {
         if (!class_exists('MailChimp_Divi')) {
             require_once ET_BUILDER_DIR . 'subscription/mailchimp/mailchimp.php';
         }
         try {
             $mailchimp = new MailChimp_Divi($mailchimp_api_key);
             $retval = $mailchimp->call('lists/list', array('limit' => 100));
             $retval_body = json_decode(wp_remote_retrieve_body($retval), true);
             $retrieved_lists = isset($retval_body['data']) ? $retval_body['data'] : array();
             if (200 !== wp_remote_retrieve_response_code($retval) || empty($retval_body['data']) || !is_array($retval_body['data'])) {
                 return $et_pb_mailchimp_lists;
             }
             // if there is more than 100 lists in account, then perform additional calls to retrieve all the lists.
             if (!empty($retval_body['total']) && 100 < $retval_body['total']) {
                 // determine how many requests we need to retrieve all the lists
                 $total_pages = ceil($retval_body['total'] / 100);
                 for ($i = 1; $i <= $total_pages; $i++) {
                     $retval_additional = $mailchimp->call('lists/list', array('limit' => 100, 'start' => $i));
                     if (!empty($retval_additional) && empty($retval_additional['errors'])) {
                         if (!empty($retval_additional['data'])) {
                             $retrieved_lists = array_merge($retrieved_lists, $retval_additional['data']);
                         }
                     }
                 }
             }
             if (!empty($retrieved_lists)) {
                 foreach ($retrieved_lists as $list) {
                     $lists[$list['id']] = $list['name'];
                 }
             }
             set_transient('et_pb_mailchimp_lists', $lists, 60 * 60 * 24);
         } catch (Exception $exc) {
             $lists = $et_pb_mailchimp_lists;
         }
         return $lists;
     } else {
         return $et_pb_mailchimp_lists;
     }
 }