<?php

/**
 * This file contains examples for using the MailWizzApi PHP-SDK.
 *
 * @author Serban George Cristian <*****@*****.**>
 * @link http://www.mailwizz.com/
 * @copyright 2013 http://www.mailwizz.com/
 */
// require the setup which has registered the autoloader
require_once dirname(__FILE__) . '/setup.php';
// CREATE THE ENDPOINT
$endpoint = new MailWizzApi_Endpoint_ListSubscribers();
/*===================================================================================*/
// GET ALL ITEMS
$response = $endpoint->getSubscribers('LIST-UNIQUE-ID', $pageNumber = 1, $perPage = 10);
// DISPLAY RESPONSE
echo '<pre>';
print_r($response->body);
echo '</pre>';
/*===================================================================================*/
// GET ONE ITEM
$response = $endpoint->getSubscriber('LIST-UNIQUE-ID', 'SUBSCRIBER-UNIQUE-ID');
// DISPLAY RESPONSE
echo '<hr /><pre>';
print_r($response->body);
echo '</pre>';
/*===================================================================================*/
// SEARCH BY EMAIL
$response = $endpoint->emailSearch('LIST-UNIQUE-ID', '*****@*****.**');
// DISPLAY RESPONSE
<?php

/**
 * This file contains examples for using the SendReach PHP-SDK.
 */
// require the setup which has registered the autoloader
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" />
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'))));
}