require_once dirname(__FILE__) . '/setup.php';
// see if the request is made via ajax.
$isAjaxRequest = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
// and if it is and we have post values, then we can proceed in sending the subscriber.
if ($isAjaxRequest && !empty($_POST)) {
    $listUid = 'LIST-UNIQUE-ID';
    // you'll take this from your customers area, in list overview from the address bar.
    $endpoint = new MailWizzApi_Endpoint_ListSubscribers();
    $response = $endpoint->create($listUid, array('EMAIL' => isset($_POST['EMAIL']) ? $_POST['EMAIL'] : null, 'FNAME' => isset($_POST['FNAME']) ? $_POST['FNAME'] : null, 'LNAME' => isset($_POST['LNAME']) ? $_POST['LNAME'] : null));
    $response = $response->body;
    // if the returned status is success, we are done.
    if ($response->itemAt('status') == 'success') {
        exit(MailWizzApi_Json::encode(array('status' => 'success', 'message' => 'Thank you for joining our email list. Please confirm your email address now!')));
    }
    // otherwise, the status is error
    exit(MailWizzApi_Json::encode(array('status' => 'error', 'message' => $response->itemAt('error'))));
}
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="content-type" content="text/html" />
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans" />
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Noto+Sans:700italic" />
    <link rel="stylesheet" type="text/css" href="http://www.mailwizz.com/backend/assets/css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="http://www.mailwizz.com/backend/assets/css/bootstrap-glyphicons.css" />
    <link rel="stylesheet" type="text/css" href="http://www.mailwizz.com/backend/assets/css/style.css" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://www.mailwizz.com/backend/assets/js/bootstrap.js" id="script-bootstrap"></script>
    <link rel="shortcut icon" href="http://www.mailwizz.com/favicon.ico" type="image/x-icon" /> 
	<title>Ajax subscribe</title>
Пример #2
0
 /**
  * Send the request to the remote url.
  * 
  * @return MailWizzApi_Http_Response
  */
 public function send()
 {
     foreach ($this->getEventHandlers(self::EVENT_BEFORE_SEND_REQUEST) as $callback) {
         call_user_func_array($callback, array($this));
     }
     $client = $this->client;
     $registry = $this->registry;
     $isCacheable = $registry->contains('cache') && $client->isGetMethod && $client->enableCache;
     $requestUrl = rtrim($client->url, '/');
     // no trailing slash
     $scheme = parse_url($requestUrl, PHP_URL_SCHEME);
     $getParams = (array) $client->paramsGet->toArray();
     if (!empty($getParams)) {
         ksort($getParams, SORT_STRING);
         $queryString = http_build_query($getParams, '', '&');
         if (!empty($queryString)) {
             $requestUrl .= '?' . $queryString;
         }
     }
     $this->sign($requestUrl);
     if ($isCacheable) {
         $client->getResponseHeaders = true;
         $bodyFromCache = null;
         $etagCache = null;
         $params = $getParams;
         foreach (array('X-MW-PUBLIC-KEY', 'X-MW-TIMESTAMP', 'X-MW-REMOTE-ADDR') as $header) {
             $params[$header] = $client->headers->itemAt($header);
         }
         $cacheKey = $requestUrl;
         $cache = $this->cache->get($cacheKey);
         if (isset($cache['headers']) && is_array($cache['headers'])) {
             foreach ($cache['headers'] as $header) {
                 if (preg_match('/etag:(\\s+)?(.*)/ix', $header, $matches)) {
                     $etagCache = trim($matches[2]);
                     $client->headers->add('If-None-Match', $etagCache);
                     $bodyFromCache = $cache['body'];
                     break;
                 }
             }
         }
     }
     if ($client->isPutMethod || $client->isDeleteMethod) {
         $client->headers->add('X-HTTP-Method-Override', strtoupper($client->method));
     }
     $ch = curl_init($requestUrl);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $client->timeout);
     curl_setopt($ch, CURLOPT_TIMEOUT, $client->timeout);
     curl_setopt($ch, CURLOPT_USERAGENT, 'MailWizzApi Client version ' . MailWizzApi_Http_Client::CLIENT_VERSION);
     curl_setopt($ch, CURLOPT_AUTOREFERER, true);
     if ($client->getResponseHeaders) {
         curl_setopt($ch, CURLOPT_VERBOSE, true);
         curl_setopt($ch, CURLOPT_HEADER, true);
     }
     if (!ini_get('safe_mode')) {
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
         curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
     }
     if ($client->headers->count > 0) {
         $headers = array();
         foreach ($client->headers as $name => $value) {
             $headers[] = $name . ': ' . $value;
         }
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     }
     if ($scheme === 'https') {
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     }
     if ($client->isPostMethod || $client->isPutMethod || $client->isDeleteMethod) {
         $params = new MailWizzApi_Params($client->paramsPost);
         $params->mergeWith($client->paramsPut);
         $params->mergeWith($client->paramsDelete);
         if (!$client->isPostMethod) {
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($client->method));
         }
         curl_setopt($ch, CURLOPT_POST, $params->count);
         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params->toArray(), '', '&'));
     }
     $body = curl_exec($ch);
     $curlCode = curl_errno($ch);
     $curlMessage = curl_error($ch);
     $curlInfo = curl_getinfo($ch);
     $params = $this->params = new MailWizzApi_Params($curlInfo);
     if ($curlCode === 0 && $client->getResponseHeaders) {
         $headersSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
         $headers = explode("\n", substr($body, 0, $headersSize));
         foreach ($headers as $index => $header) {
             $header = trim($header);
             if (empty($header)) {
                 unset($headers[$index]);
             }
         }
         $body = substr($body, $headersSize);
         $params->add('headers', new MailWizzApi_Params($headers));
     }
     $decodedBody = array();
     if ($curlCode === 0 && !empty($body)) {
         $decodedBody = MailWizzApi_Json::decode($body, true);
         if (!is_array($decodedBody)) {
             $decodedBody = array();
         }
     }
     // note here
     if ((int) $params->itemAt('http_code') === 304 && $isCacheable && !empty($bodyFromCache)) {
         $decodedBody = $bodyFromCache;
     }
     $params->add('curl_code', $curlCode);
     $params->add('curl_message', $curlMessage);
     $params->add('body', new MailWizzApi_Params($decodedBody));
     $response = new MailWizzApi_Http_Response($this);
     $body = $response->body;
     if (!$response->isSuccess && $body->itemAt('status') !== 'success' && !$body->contains('error')) {
         $response->body->add('status', 'error');
         $response->body->add('error', $response->message);
     }
     curl_close($ch);
     if ($isCacheable && $response->isSuccess && $body->itemAt('status') == 'success') {
         $etagNew = null;
         foreach ($response->headers as $header) {
             if (preg_match('/etag:(\\s+)?(.*)/ix', $header, $matches)) {
                 $etagNew = trim($matches[2]);
                 break;
             }
         }
         if ($etagNew && $etagNew != $etagCache) {
             $this->cache->set($cacheKey, array('headers' => $response->headers->toArray(), 'body' => $response->body->toArray()));
         }
     }
     foreach ($this->getEventHandlers(self::EVENT_AFTER_SEND_REQUEST) as $callback) {
         $response = call_user_func_array($callback, array($this, $response));
     }
     return $response;
 }
function mwznb_subscribe_callback()
{
    if (!isset($_POST['mwznb_form_nonce']) || !wp_verify_nonce($_POST['mwznb_form_nonce'], basename(__FILE__))) {
        exit(MailWizzApi_Json::encode(array('result' => 'error', 'message' => __('Invalid nonce!', 'mwznb'))));
    }
    $uid = isset($_POST['uid']) ? sanitize_text_field($_POST['uid']) : null;
    if ($uid) {
        unset($_POST['uid']);
    }
    unset($_POST['action'], $_POST['mwznb_form_nonce']);
    if (empty($uid) || !($uidData = get_option('mwznb_widget_instance_' . $uid))) {
        exit(MailWizzApi_Json::encode(array('result' => 'error', 'message' => __('Please try again later!', 'mwznb'))));
    }
    $keys = array('api_url', 'public_key', 'private_key', 'list_uid');
    foreach ($keys as $key) {
        if (!isset($uidData[$key])) {
            exit(MailWizzApi_Json::encode(array('result' => 'error', 'message' => __('Please try again later!', 'mwznb'))));
        }
    }
    $oldSdkConfig = MailWizzApi_Base::getConfig();
    MailWizzApi_Base::setConfig(mwznb_build_sdk_config($uidData['api_url'], $uidData['public_key'], $uidData['private_key']));
    $endpoint = new MailWizzApi_Endpoint_ListSubscribers();
    $response = $endpoint->create($uidData['list_uid'], $_POST);
    $response = $response->body->toArray();
    mwznb_restore_sdk_config($oldSdkConfig);
    unset($oldSdkConfig);
    if (isset($response['status']) && $response['status'] == 'error' && isset($response['error'])) {
        $errorMessage = $response['error'];
        if (is_array($errorMessage)) {
            $errorMessage = implode("\n", array_values($errorMessage));
        }
        exit(MailWizzApi_Json::encode(array('result' => 'error', 'message' => $errorMessage)));
    }
    if (isset($response['status']) && $response['status'] == 'success') {
        exit(MailWizzApi_Json::encode(array('result' => 'success', 'message' => __('Please check your email to confirm the subscription!', 'mwznb'))));
    }
    exit(MailWizzApi_Json::encode(array('result' => 'success', 'message' => __('Unknown error!', 'mwznb'))));
}