Added login request
This commit is contained in:
@@ -0,0 +1,395 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 7/14/16
|
||||
* Time: 11:54 AM
|
||||
*/
|
||||
abstract class MailChimp_WooCommerce_Abstract_Sync extends WP_Job
|
||||
{
|
||||
/**
|
||||
* @var MailChimp_WooCommerce_Api
|
||||
*/
|
||||
private $api;
|
||||
|
||||
/**
|
||||
* @var MailChimp_WooCommerce_MailChimpApi
|
||||
*/
|
||||
private $mc;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_name = 'mailchimp-woocommerce';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $store_id = '';
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function getResourceType();
|
||||
|
||||
/**
|
||||
* @param $item
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function iterate($item);
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function complete();
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function go()
|
||||
{
|
||||
return $this->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getStoreID()
|
||||
{
|
||||
return mailchimp_get_store_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Task
|
||||
*
|
||||
* Override this method to perform any actions required on each
|
||||
* queue item. Return the modified item for further processing
|
||||
* in the next pass through. Or, return false to remove the
|
||||
* item from the queue.
|
||||
*
|
||||
* @param mixed $item Queue item to iterate over
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
global $wpdb;
|
||||
|
||||
if (!mailchimp_is_configured()) {
|
||||
mailchimp_debug(get_called_class(), 'mailchimp is not configured properly');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!($this->store_id = $this->getStoreID())) {
|
||||
mailchimp_debug(get_called_class().'@handle', 'store id not loaded');
|
||||
$this->delete();
|
||||
return false;
|
||||
}
|
||||
|
||||
// don't let recursion happen.
|
||||
if ($this->getResourceType() === 'orders' && $this->getResourceCompleteTime()) {
|
||||
mailchimp_log('sync.stop', "halting the sync for :: {$this->getResourceType()}");
|
||||
$this->delete();
|
||||
return false;
|
||||
}
|
||||
|
||||
$page = $this->getResources();
|
||||
|
||||
if (empty($page)) {
|
||||
mailchimp_debug(get_called_class().'@handle', 'could not find any more '.$this->getResourceType().' records ending on page '.$this->getResourcePagePointer());
|
||||
// call the completed event to process further
|
||||
$this->resourceComplete($this->getResourceType());
|
||||
$this->complete();
|
||||
$this->delete();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->setResourcePagePointer(($page->page + 1), $this->getResourceType());
|
||||
|
||||
// if we've got a 0 count, that means we're done.
|
||||
if ($page->count <= 0) {
|
||||
|
||||
mailchimp_debug(get_called_class().'@handle', $this->getResourceType().' :: completing now!');
|
||||
|
||||
// reset the resource page back to 1
|
||||
$this->resourceComplete($this->getResourceType());
|
||||
|
||||
// call the completed event to process further
|
||||
$this->complete();
|
||||
|
||||
$this->delete();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// iterate through the items and send each one through the pipeline based on this class.
|
||||
foreach ($page->items as $resource) {
|
||||
$this->iterate($resource);
|
||||
}
|
||||
|
||||
$this->delete();
|
||||
|
||||
$class_name = get_called_class();
|
||||
$wpdb->query("DELETE FROM {$wpdb->prefix}queue WHERE job LIKE '%{$class_name}%'");
|
||||
|
||||
// this will paginate through all records for the resource type until they return no records.
|
||||
wp_queue(new static());
|
||||
mailchimp_debug(get_called_class().'@handle', 'queuing up the next job');
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function flagStartSync()
|
||||
{
|
||||
$job = new MailChimp_Service();
|
||||
|
||||
$job->removeSyncPointers();
|
||||
|
||||
$this->setData('sync.config.resync', false);
|
||||
$this->setData('sync.orders.current_page', 1);
|
||||
$this->setData('sync.products.current_page', 1);
|
||||
$this->setData('sync.syncing', true);
|
||||
$this->setData('sync.started_at', time());
|
||||
|
||||
global $wpdb;
|
||||
try {
|
||||
$wpdb->show_errors(false);
|
||||
$wpdb->query("DELETE FROM {$wpdb->prefix}queue");
|
||||
$wpdb->show_errors(true);
|
||||
} catch (\Exception $e) {}
|
||||
|
||||
mailchimp_log('sync.started', "Starting Sync :: ".date('D, M j, Y g:i A'));
|
||||
|
||||
// flag the store as syncing
|
||||
mailchimp_get_api()->flagStoreSync(mailchimp_get_store_id(), true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function flagStopSync()
|
||||
{
|
||||
// this is the last thing we're doing so it's complete as of now.
|
||||
$this->setData('sync.syncing', false);
|
||||
$this->setData('sync.completed_at', time());
|
||||
|
||||
// set the current sync pages back to 1 if the user hits resync.
|
||||
$this->setData('sync.orders.current_page', 1);
|
||||
$this->setData('sync.products.current_page', 1);
|
||||
|
||||
mailchimp_log('sync.completed', "Finished Sync :: ".date('D, M j, Y g:i A'));
|
||||
|
||||
// flag the store as sync_finished
|
||||
mailchimp_get_api()->flagStoreSync(mailchimp_get_store_id(), false);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|object|stdClass
|
||||
*/
|
||||
public function getResources()
|
||||
{
|
||||
$current_page = $this->getResourcePagePointer($this->getResourceType());
|
||||
|
||||
if ($current_page === 'complete') {
|
||||
if (!$this->getData('sync.config.resync', false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$current_page = 1;
|
||||
$this->setResourcePagePointer($current_page);
|
||||
$this->setData('sync.config.resync', false);
|
||||
}
|
||||
|
||||
return $this->api()->paginate($this->getResourceType(), $current_page, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|string $resource
|
||||
* @return $this
|
||||
*/
|
||||
public function resetResourcePagePointer($resource = null)
|
||||
{
|
||||
if (empty($resource)) $resource = $this->getResourceType();
|
||||
|
||||
$this->setData('sync.'.$resource.'.current_page', 1);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|string $resource
|
||||
* @return null
|
||||
*/
|
||||
public function getResourcePagePointer($resource = null)
|
||||
{
|
||||
if (empty($resource)) $resource = $this->getResourceType();
|
||||
|
||||
return $this->getData('sync.'.$resource.'.current_page', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page
|
||||
* @param null $resource
|
||||
* @return MailChimp_WooCommerce_Abstract_Sync
|
||||
*/
|
||||
public function setResourcePagePointer($page, $resource = null)
|
||||
{
|
||||
if (empty($resource)) $resource = $this->getResourceType();
|
||||
|
||||
return $this->setData('sync.'.$resource.'.current_page', $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|string $resource
|
||||
* @return $this
|
||||
*/
|
||||
protected function resourceComplete($resource = null)
|
||||
{
|
||||
if (empty($resource)) $resource = $this->getResourceType();
|
||||
|
||||
$this->setData('sync.'.$resource.'.current_page', 'complete');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
protected function setResourceCompleteTime($resource = null)
|
||||
{
|
||||
if (empty($resource)) $resource = $this->getResourceType();
|
||||
|
||||
return $this->setData('sync.'.$resource.'.completed_at', time());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $resource
|
||||
* @return bool|DateTime
|
||||
*/
|
||||
protected function getResourceCompleteTime($resource = null)
|
||||
{
|
||||
if (empty($resource)) $resource = $this->getResourceType();
|
||||
|
||||
$time = $this->getData('sync.'.$resource.'.completed_at', false);
|
||||
|
||||
if ($time > 0) {
|
||||
try {
|
||||
$date = new \DateTime();
|
||||
$date->setTimestamp($time);
|
||||
return $date;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param null $default
|
||||
* @return null
|
||||
*/
|
||||
public function getOption($key, $default = null)
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
if (isset($options[$key])) {
|
||||
return $options[$key];
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function setOption($key, $value)
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$options[$key] = $value;
|
||||
update_option($this->plugin_name, $options);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param bool $default
|
||||
* @return bool
|
||||
*/
|
||||
public function hasOption($key, $default = false)
|
||||
{
|
||||
return (bool) $this->getOption($key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
$options = get_option($this->plugin_name);
|
||||
return is_array($options) ? $options : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function setData($key, $value)
|
||||
{
|
||||
update_option($this->plugin_name.'-'.$key, $value, 'yes');
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param null $default
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function getData($key, $default = null)
|
||||
{
|
||||
return get_option($this->plugin_name.'-'.$key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function removeData($key)
|
||||
{
|
||||
return delete_option($this->plugin_name.'-'.$key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MailChimp_WooCommerce_Api
|
||||
*/
|
||||
protected function api()
|
||||
{
|
||||
if (empty($this->api)) {
|
||||
$this->api = new MailChimp_WooCommerce_Api();
|
||||
}
|
||||
return $this->api;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MailChimp_WooCommerce_MailChimpApi
|
||||
*/
|
||||
protected function mailchimp()
|
||||
{
|
||||
if (empty($this->mc)) {
|
||||
$this->mc = new MailChimp_WooCommerce_MailChimpApi($this->getOption('mailchimp_api_key'));
|
||||
}
|
||||
return $this->mc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 7/15/16
|
||||
* Time: 11:42 AM
|
||||
*/
|
||||
class MailChimp_WooCommerce_Cart_Update extends WP_Job
|
||||
{
|
||||
public $unique_id;
|
||||
public $email;
|
||||
public $previous_email;
|
||||
public $campaign_id;
|
||||
public $cart_data;
|
||||
public $ip_address;
|
||||
|
||||
/**
|
||||
* MailChimp_WooCommerce_Cart_Update constructor.
|
||||
* @param null $uid
|
||||
* @param null $email
|
||||
* @param null $campaign_id
|
||||
* @param array $cart_data
|
||||
*/
|
||||
public function __construct($uid = null, $email = null, $campaign_id = null, array $cart_data = array())
|
||||
{
|
||||
if ($uid) {
|
||||
$this->unique_id = $uid;
|
||||
}
|
||||
if ($email) {
|
||||
$this->email = $email;
|
||||
}
|
||||
if (!empty($cart_data)) {
|
||||
$this->cart_data = json_encode($cart_data);
|
||||
}
|
||||
|
||||
if ($campaign_id) {
|
||||
$this->campaign_id = $campaign_id;
|
||||
}
|
||||
|
||||
$this->ip_address = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (($result = $this->process())) {
|
||||
mailchimp_log('ac.success', 'Added', array('api_response' => $result->toArray()));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|MailChimp_WooCommerce_Cart
|
||||
*/
|
||||
public function process()
|
||||
{
|
||||
try {
|
||||
|
||||
if (!mailchimp_is_configured() || !($api = mailchimp_get_api())) {
|
||||
mailchimp_debug(get_called_class(), 'mailchimp is not configured properly');
|
||||
return false;
|
||||
}
|
||||
|
||||
$options = get_option('mailchimp-woocommerce', array());
|
||||
$store_id = mailchimp_get_store_id();
|
||||
|
||||
$this->cart_data = json_decode($this->cart_data, true);
|
||||
|
||||
// delete it and the add it back.
|
||||
$api->deleteCartByID($store_id, $this->unique_id);
|
||||
|
||||
// if they emptied the cart ignore it.
|
||||
if (!is_array($this->cart_data) || empty($this->cart_data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$checkout_url = wc_get_checkout_url();
|
||||
|
||||
if (mailchimp_string_contains($checkout_url, '?')) {
|
||||
$checkout_url .= '&mc_cart_id='.$this->unique_id;
|
||||
} else {
|
||||
$checkout_url .= '?mc_cart_id='.$this->unique_id;
|
||||
}
|
||||
|
||||
$customer = new MailChimp_WooCommerce_Customer();
|
||||
$customer->setId($this->unique_id);
|
||||
$customer->setEmailAddress($this->email);
|
||||
$customer->setOptInStatus(false);
|
||||
|
||||
$cart = new MailChimp_WooCommerce_Cart();
|
||||
$cart->setId($this->unique_id);
|
||||
$cart->setCampaignID($this->campaign_id);
|
||||
$cart->setCheckoutUrl($checkout_url);
|
||||
$cart->setCurrencyCode(isset($options['store_currency_code']) ? $options['store_currency_code'] : 'USD');
|
||||
|
||||
$cart->setCustomer($customer);
|
||||
|
||||
$order_total = 0;
|
||||
$products = array();
|
||||
|
||||
foreach ($this->cart_data as $hash => $item) {
|
||||
try {
|
||||
$line = $this->transformLineItem($hash, $item);
|
||||
$cart->addItem($line);
|
||||
$order_total += ($item['quantity'] * $line->getPrice());
|
||||
$products[] = $line;
|
||||
} catch (\Exception $e) {}
|
||||
}
|
||||
|
||||
if (empty($products)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cart->setOrderTotal($order_total);
|
||||
|
||||
try {
|
||||
// if the post is successful we're all good.
|
||||
$api->addCart($store_id, $cart, false);
|
||||
|
||||
mailchimp_log('abandoned_cart.success', "email: {$customer->getEmailAddress()} :: checkout_url: $checkout_url");
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
mailchimp_error('abandoned_cart.error', "email: {$customer->getEmailAddress()} :: attempting product update :: {$e->getMessage()}");
|
||||
|
||||
// if we have an error it's most likely due to a product not being found.
|
||||
// let's loop through each item, verify that we have the product or not.
|
||||
// if not, we will add it.
|
||||
foreach ($products as $item) {
|
||||
/** @var MailChimp_WooCommerce_LineItem $item */
|
||||
$transformer = new MailChimp_WooCommerce_Single_Product($item->getProductID());
|
||||
if (!$transformer->api()->getStoreProduct($store_id, $item->getProductId())) {
|
||||
$transformer->handle();
|
||||
}
|
||||
}
|
||||
|
||||
// if the post is successful we're all good.
|
||||
$api->addCart($store_id, $cart, false);
|
||||
|
||||
mailchimp_log('abandoned_cart.success', "email: {$customer->getEmailAddress()}");
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
update_option('mailchimp-woocommerce-cart-error', $e->getMessage());
|
||||
mailchimp_error('abandoned_cart.error', $e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $hash
|
||||
* @param $item
|
||||
* @return MailChimp_WooCommerce_LineItem
|
||||
*/
|
||||
protected function transformLineItem($hash, $item)
|
||||
{
|
||||
$product = wc_get_product($item['product_id']);
|
||||
$price = $product ? $product->get_price() : 0;
|
||||
|
||||
$line = new MailChimp_WooCommerce_LineItem();
|
||||
$line->setId($hash);
|
||||
$line->setProductId($item['product_id']);
|
||||
|
||||
if (isset($item['variation_id']) && $item['variation_id'] > 0) {
|
||||
$line->setProductVariantId($item['variation_id']);
|
||||
} else {
|
||||
$line->setProductVariantId($item['product_id']);
|
||||
}
|
||||
|
||||
$line->setQuantity($item['quantity']);
|
||||
$line->setPrice($price);
|
||||
|
||||
return $line;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 10/06/17
|
||||
* Time: 10:57 AM
|
||||
*/
|
||||
class MailChimp_WooCommerce_Process_Coupons extends MailChimp_WooCommerce_Abstract_Sync
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $action = 'mailchimp_woocommerce_process_coupons';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getResourceType()
|
||||
{
|
||||
return 'coupons';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WC_Coupon $item
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function iterate($item)
|
||||
{
|
||||
if ($item instanceof MailChimp_WooCommerce_PromoCode) {
|
||||
|
||||
mailchimp_debug('promo_code_sync', "#{$item->getId()}", $item->toArray());
|
||||
|
||||
try {
|
||||
$this->mailchimp()->addPromoRule($this->store_id, $item->getAttachedPromoRule(), true);
|
||||
$response = $this->mailchimp()->addPromoCodeForRule($this->store_id, $item->getAttachedPromoRule(), $item, true);
|
||||
mailchimp_log('coupon_sync.success', "update promo rule :: #{$item->getCode()}");
|
||||
return $response;
|
||||
} catch (MailChimp_WooCommerce_ServerError $e) {
|
||||
mailchimp_error('order_submit.error', mailchimp_error_trace($e, "update promo rule :: {$item->getCode()}"));
|
||||
return false;
|
||||
} catch (MailChimp_WooCommerce_Error $e) {
|
||||
mailchimp_error('order_submit.error', mailchimp_error_trace($e, "update promo rule :: {$item->getCode()}"));
|
||||
return false;
|
||||
} catch (Exception $e) {
|
||||
mailchimp_error('order_submit.error', mailchimp_error_trace($e, "update promo rule :: {$item->getCode()}"));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
mailchimp_debug('coupon_sync', 'no coupon found', $item);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* After the resources have been loaded and pushed
|
||||
*/
|
||||
protected function complete()
|
||||
{
|
||||
mailchimp_log('coupon_sync.completed', 'Done with the coupon sync.');
|
||||
|
||||
// add a timestamp for the orders sync completion
|
||||
$this->setResourceCompleteTime();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 7/14/16
|
||||
* Time: 10:57 AM
|
||||
*/
|
||||
class MailChimp_WooCommerce_Process_Orders extends MailChimp_WooCommerce_Abstract_Sync
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $action = 'mailchimp_woocommerce_process_orders';
|
||||
public $items = array();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getResourceType()
|
||||
{
|
||||
return 'orders';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MailChimp_WooCommerce_Order $item
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function iterate($item)
|
||||
{
|
||||
if ($item instanceof MailChimp_WooCommerce_Order) {
|
||||
|
||||
// since we're syncing the customer for the first time, this is where we need to add the override
|
||||
// for subscriber status. We don't get the checkbox until this plugin is actually installed and working!
|
||||
if (!($status = $item->getCustomer()->getOptInStatus())) {
|
||||
try {
|
||||
$subscriber = $this->mailchimp()->member(mailchimp_get_list_id(), $item->getCustomer()->getEmailAddress());
|
||||
$status = !in_array($subscriber['status'], array('unsubscribed', 'transactional'));
|
||||
} catch (\Exception $e) {
|
||||
$status = (bool) $this->getOption('mailchimp_auto_subscribe', true);
|
||||
}
|
||||
$item->getCustomer()->setOptInStatus($status);
|
||||
}
|
||||
|
||||
mailchimp_debug('order_sync', "#{$item->getId()}", $item->toArray());
|
||||
|
||||
$type = $this->mailchimp()->getStoreOrder($this->store_id, $item->getId()) ? 'update' : 'create';
|
||||
$call = $type === 'create' ? 'addStoreOrder' : 'updateStoreOrder';
|
||||
|
||||
try {
|
||||
|
||||
// if the order is in failed or cancelled status - and it's brand new, we shouldn't submit it.
|
||||
if ($call === 'addStoreOrder' && in_array($item->getFinancialStatus(), array('failed', 'cancelled'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// make the call
|
||||
$response = $this->mailchimp()->$call($this->store_id, $item, false);
|
||||
|
||||
if (empty($response)) {
|
||||
mailchimp_error('order_submit.failure', "$call :: #{$item->getId()} :: email: {$item->getCustomer()->getEmailAddress()} produced a blank response from MailChimp");
|
||||
return $response;
|
||||
}
|
||||
|
||||
mailchimp_log('order_submit.success', "$call :: #{$item->getId()} :: email: {$item->getCustomer()->getEmailAddress()}");
|
||||
|
||||
$this->items[] = array('response' => $response, 'item' => $item);
|
||||
|
||||
return $response;
|
||||
|
||||
} catch (MailChimp_WooCommerce_ServerError $e) {
|
||||
mailchimp_error('order_submit.error', mailchimp_error_trace($e, "$call :: {$item->getId()}"));
|
||||
return false;
|
||||
} catch (MailChimp_WooCommerce_Error $e) {
|
||||
mailchimp_error('order_submit.error', mailchimp_error_trace($e, "$call :: {$item->getId()}"));
|
||||
return false;
|
||||
} catch (Exception $e) {
|
||||
mailchimp_error('order_submit.error', mailchimp_error_trace($e, "$call :: {$item->getId()}"));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
mailchimp_debug('order_submit', 'no order found', $item);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* After the resources have been loaded and pushed
|
||||
*/
|
||||
protected function complete()
|
||||
{
|
||||
mailchimp_log('order_submit.completed', 'Done with the order sync.');
|
||||
|
||||
// add a timestamp for the orders sync completion
|
||||
$this->setResourceCompleteTime();
|
||||
|
||||
// this is the last thing we're doing so it's complete as of now.
|
||||
$this->flagStopSync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 7/14/16
|
||||
* Time: 10:57 AM
|
||||
*/
|
||||
class MailChimp_WooCommerce_Process_Products extends MailChimp_WooCommerce_Abstract_Sync
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $action = 'mailchimp_woocommerce_process_products';
|
||||
|
||||
|
||||
public static function push()
|
||||
{
|
||||
$job = new MailChimp_WooCommerce_Process_Products();
|
||||
$job->flagStartSync();
|
||||
wp_queue($job);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getResourceType()
|
||||
{
|
||||
return 'products';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MailChimp_WooCommerce_Product $item
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function iterate($item) {
|
||||
|
||||
if ($item instanceof MailChimp_WooCommerce_Product) {
|
||||
|
||||
mailchimp_debug('product_sync', "#{$item->getId()}", $item->toArray());
|
||||
|
||||
// need to run the delete option on this before submitting because the API does not support PATCH yet.
|
||||
$this->mailchimp()->deleteStoreProduct($this->store_id, $item->getId());
|
||||
|
||||
// add the product.
|
||||
try {
|
||||
// make the call
|
||||
$response = $this->mailchimp()->addStoreProduct($this->store_id, $item, false);
|
||||
|
||||
mailchimp_log('product_sync.success', "addStoreProduct :: #{$response->getId()}");
|
||||
|
||||
return $response;
|
||||
|
||||
} catch (MailChimp_WooCommerce_ServerError $e) {
|
||||
mailchimp_error('product_sync.error', mailchimp_error_trace($e, "addStoreProduct :: {$item->getId()}"));
|
||||
} catch (MailChimp_WooCommerce_Error $e) {
|
||||
mailchimp_error('product_sync.error', mailchimp_error_trace($e, "addStoreProduct :: {$item->getId()}"));
|
||||
} catch (Exception $e) {
|
||||
mailchimp_error('product_sync.error', mailchimp_error_trace($e, "addStoreProduct :: {$item->getId()}"));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after all the products have been iterated and processed into MailChimp
|
||||
*/
|
||||
protected function complete()
|
||||
{
|
||||
mailchimp_log('product_sync.completed', 'Done with the product sync :: queuing up the orders next!');
|
||||
|
||||
// add a timestamp for the product sync completion
|
||||
$this->setResourceCompleteTime();
|
||||
|
||||
$prevent_order_sync = get_option('mailchimp-woocommerce-sync.orders.prevent', false);
|
||||
|
||||
// only do this if we're not strictly syncing products ( which is the default ).
|
||||
if (!$prevent_order_sync) {
|
||||
// since the products are all good, let's sync up the orders now.
|
||||
wp_queue(new MailChimp_WooCommerce_Process_Orders());
|
||||
}
|
||||
|
||||
// since we skipped the orders feed we can delete this option.
|
||||
delete_option('mailchimp-woocommerce-sync.orders.prevent');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 10/6/17
|
||||
* Time: 11:14 AM
|
||||
*/
|
||||
class MailChimp_WooCommerce_SingleCoupon extends WP_Job
|
||||
{
|
||||
public $coupon_data;
|
||||
public $post_id;
|
||||
|
||||
/**
|
||||
* MailChimp_WooCommerce_Coupon_Sync constructor.
|
||||
* @param $post_id
|
||||
*/
|
||||
public function __construct($post_id = null)
|
||||
{
|
||||
$this->post_id = $post_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
try {
|
||||
|
||||
if (!mailchimp_is_configured()) {
|
||||
mailchimp_debug(get_called_class(), 'mailchimp is not configured properly');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($this->post_id)) {
|
||||
mailchimp_error('promo_code.failure', "could not process coupon {$this->post_id}");
|
||||
return;
|
||||
}
|
||||
|
||||
$api = mailchimp_get_api();
|
||||
$store_id = mailchimp_get_store_id();
|
||||
|
||||
$transformer = new MailChimp_WooCommerce_Transform_Coupons();
|
||||
$code = $transformer->transform($this->post_id);
|
||||
|
||||
$api->addPromoRule($store_id, $code->getAttachedPromoRule(), true);
|
||||
$api->addPromoCodeForRule($store_id, $code->getAttachedPromoRule(), $code, true);
|
||||
|
||||
mailchimp_log('promo_code.update', "updated promo code {$code->getCode()}");
|
||||
} catch (\Exception $e) {
|
||||
$promo_code = isset($code) ? "code {$code->getCode()}" : "id {$this->post_id}";
|
||||
mailchimp_error('promo_code.error', mailchimp_error_trace($e, "error updating promo {$promo_code}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 7/15/16
|
||||
* Time: 11:42 AM
|
||||
*/
|
||||
class MailChimp_WooCommerce_Single_Order extends WP_Job
|
||||
{
|
||||
public $order_id;
|
||||
public $cart_session_id;
|
||||
public $campaign_id;
|
||||
public $landing_site;
|
||||
public $is_update = false;
|
||||
public $is_admin_save = false;
|
||||
public $partially_refunded = false;
|
||||
protected $woo_order_number = false;
|
||||
|
||||
/**
|
||||
* MailChimp_WooCommerce_Single_Order constructor.
|
||||
* @param null $order_id
|
||||
* @param null $cart_session_id
|
||||
* @param null $campaign_id
|
||||
* @param null $landing_site
|
||||
*/
|
||||
public function __construct($order_id = null, $cart_session_id = null, $campaign_id = null, $landing_site = null)
|
||||
{
|
||||
if (!empty($order_id)) $this->order_id = $order_id;
|
||||
if (!empty($cart_session_id)) $this->cart_session_id = $cart_session_id;
|
||||
if (!empty($campaign_id)) $this->campaign_id = $campaign_id;
|
||||
if (!empty($landing_site)) $this->landing_site = $landing_site;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->process();
|
||||
return false;
|
||||
}
|
||||
|
||||
public function process()
|
||||
{
|
||||
if (!mailchimp_is_configured() || !($api = mailchimp_get_api())) {
|
||||
mailchimp_debug(get_called_class(), 'mailchimp is not configured properly');
|
||||
return false;
|
||||
}
|
||||
|
||||
$store_id = mailchimp_get_store_id();
|
||||
|
||||
if (!($woo_order_number = $this->getRealOrderNumber())) {
|
||||
mailchimp_log('order_submit.failure', "There is no real order number to use.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// skip amazon orders
|
||||
if ($this->isAmazonOrder()) {
|
||||
mailchimp_log('validation.amazon', "Order #{$woo_order_number} was placed through Amazon. Skipping!");
|
||||
return false;
|
||||
}
|
||||
|
||||
$job = new MailChimp_WooCommerce_Transform_Orders();
|
||||
|
||||
// set the campaign ID
|
||||
$job->campaign_id = $this->campaign_id;
|
||||
|
||||
$call = ($api_response = $api->getStoreOrder($store_id, $woo_order_number)) ? 'updateStoreOrder' : 'addStoreOrder';
|
||||
|
||||
if (!$this->is_admin_save && $call === 'addStoreOrder' && $this->is_update === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if we already pushed this order into the system, we need to unset it now just in case there
|
||||
// was another campaign that had been sent and this was only an order update.
|
||||
if ($call === 'updateStoreOrder') {
|
||||
$job->campaign_id = null;
|
||||
$this->campaign_id = null;
|
||||
$this->landing_site = null;
|
||||
}
|
||||
|
||||
// will either add or update the order
|
||||
try {
|
||||
|
||||
if (!($order_post = get_post($this->order_id))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// transform the order
|
||||
$order = $job->transform($order_post);
|
||||
|
||||
// will be the same as the customer id. an md5'd hash of a lowercased email.
|
||||
$this->cart_session_id = $order->getCustomer()->getId();
|
||||
|
||||
// delete the AC cart record.
|
||||
$deleted_abandoned_cart = !empty($this->cart_session_id) && $api->deleteCartByID($store_id, $this->cart_session_id);
|
||||
|
||||
// skip amazon orders
|
||||
if ($order->isFlaggedAsAmazonOrder()) {
|
||||
mailchimp_log('validation.amazon', "Order #{$woo_order_number} was placed through Amazon. Skipping!");
|
||||
return false;
|
||||
}
|
||||
|
||||
// if the order is in failed or cancelled status - and it's brand new, we shouldn't submit it.
|
||||
if ($call === 'addStoreOrder' && in_array($order->getFinancialStatus(), array('failed', 'cancelled'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mailchimp_debug('order_submit', "#{$woo_order_number}", $order->toArray());
|
||||
|
||||
// if we're overriding this we need to set it here.
|
||||
if ($this->partially_refunded) {
|
||||
$order->setFinancialStatus('partially_refunded');
|
||||
}
|
||||
|
||||
$log = "$call :: #{$order->getId()} :: email: {$order->getCustomer()->getEmailAddress()}";
|
||||
|
||||
// only do this stuff on new orders
|
||||
if ($call === 'addStoreOrder') {
|
||||
|
||||
// apply a campaign id if we have one.
|
||||
if (!empty($this->campaign_id)) {
|
||||
$log .= ' :: campaign id ' . $this->campaign_id;
|
||||
$order->setCampaignId($this->campaign_id);
|
||||
}
|
||||
|
||||
// apply the landing site if we have one.
|
||||
if (!empty($this->landing_site)) {
|
||||
$log .= ' :: landing site ' . $this->landing_site;
|
||||
$order->setLandingSite($this->landing_site);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// update or create
|
||||
$api_response = $api->$call($store_id, $order, false);
|
||||
|
||||
if (empty($api_response)) {
|
||||
mailchimp_error('order_submit.failure', "$call :: #{$order->getId()} :: email: {$order->getCustomer()->getEmailAddress()} produced a blank response from MailChimp");
|
||||
return $api_response;
|
||||
}
|
||||
|
||||
if ($deleted_abandoned_cart) {
|
||||
$log .= " :: abandoned cart deleted [{$this->cart_session_id}]";
|
||||
}
|
||||
|
||||
mailchimp_log('order_submit.success', $log);
|
||||
|
||||
return $api_response;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
$message = strtolower($e->getMessage());
|
||||
|
||||
mailchimp_error('order_submit.tracing_error', $e);
|
||||
|
||||
if (!isset($order)) {
|
||||
// transform the order
|
||||
$order = $job->transform(get_post($this->order_id));
|
||||
$this->cart_session_id = $order->getCustomer()->getId();
|
||||
}
|
||||
|
||||
// this can happen when a customer changes their email.
|
||||
if (isset($order) && strpos($message, 'not be changed')) {
|
||||
|
||||
try {
|
||||
|
||||
mailchimp_log('order_submit.deleting_customer', "#{$order->getId()} :: email: {$order->getCustomer()->getEmailAddress()}");
|
||||
|
||||
// delete the customer before adding it again.
|
||||
$api->deleteCustomer($store_id, $order->getCustomer()->getId());
|
||||
|
||||
// update or create
|
||||
$api_response = $api->$call($store_id, $order, false);
|
||||
|
||||
$log = "Deleted Customer :: $call :: #{$order->getId()} :: email: {$order->getCustomer()->getEmailAddress()}";
|
||||
|
||||
if (!empty($job->campaign_id)) {
|
||||
$log .= ' :: campaign id '.$job->campaign_id;
|
||||
}
|
||||
|
||||
mailchimp_log('order_submit.success', $log);
|
||||
|
||||
// if we're adding a new order and the session id is here, we need to delete the AC cart record.
|
||||
if (!empty($this->cart_session_id)) {
|
||||
$api->deleteCartByID($store_id, $this->cart_session_id);
|
||||
}
|
||||
|
||||
return $api_response;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
mailchimp_error('order_submit.error', mailchimp_error_trace($e, 'deleting-customer-re-add :: #'.$this->order_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getRealOrderNumber()
|
||||
{
|
||||
try {
|
||||
if (empty($this->order_id) || !($order_post = get_post($this->order_id))) {
|
||||
return false;
|
||||
}
|
||||
$woo = new WC_Order($order_post);
|
||||
return $this->woo_order_number = $woo->get_order_number();
|
||||
} catch (\Exception $e) {
|
||||
$this->woo_order_number = false;
|
||||
mailchimp_error('order_sync.failure', mailchimp_error_trace($e, "{$this->order_id} could not be loaded"));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isAmazonOrder()
|
||||
{
|
||||
try {
|
||||
if (empty($this->order_id) || !($order_post = get_post($this->order_id))) {
|
||||
return false;
|
||||
}
|
||||
$woo = new WC_Order($order_post);
|
||||
// just skip these altogether because we can't submit any amazon orders anyway.
|
||||
return mailchimp_string_contains($woo->get_billing_email(), '@marketplace.amazon.com');
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 7/15/16
|
||||
* Time: 11:42 AM
|
||||
*/
|
||||
class MailChimp_WooCommerce_Single_Product extends WP_Job
|
||||
{
|
||||
public $product_id;
|
||||
protected $store_id;
|
||||
protected $api;
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* MailChimp_WooCommerce_Single_Order constructor.
|
||||
* @param null|int $product_id
|
||||
*/
|
||||
public function __construct($product_id = null)
|
||||
{
|
||||
if (!empty($product_id)) {
|
||||
$this->product_id = $product_id instanceof WP_Post ? $product_id->ID : $product_id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->process();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|MailChimp_WooCommerce_Product
|
||||
*/
|
||||
public function process()
|
||||
{
|
||||
if (empty($this->product_id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mailchimp_is_configured()) {
|
||||
mailchimp_debug(get_called_class(), 'mailchimp is not configured properly');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->api()->getStoreProduct($this->store_id, $this->product_id)) {
|
||||
$this->api()->deleteStoreProduct($this->store_id, $this->product_id);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
if (!($product_post = get_post($this->product_id))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$product = $this->transformer()->transform($product_post);
|
||||
|
||||
mailchimp_debug('product_submit.debug', "#{$this->product_id}", $product->toArray());
|
||||
|
||||
$this->api()->addStoreProduct($this->store_id, $product, false);
|
||||
|
||||
mailchimp_log('product_submit.success', "addStoreProduct :: #{$product->getId()}");
|
||||
|
||||
update_option('mailchimp-woocommerce-last_product_updated', $product->getId());
|
||||
|
||||
return $product;
|
||||
|
||||
} catch (MailChimp_WooCommerce_ServerError $e) {
|
||||
mailchimp_error('product_submit.error', mailchimp_error_trace($e, "addStoreProduct :: #{$this->product_id}"));
|
||||
} catch (MailChimp_WooCommerce_Error $e) {
|
||||
mailchimp_log('product_submit.error', mailchimp_error_trace($e, "addStoreProduct :: #{$this->product_id}"));
|
||||
} catch (Exception $e) {
|
||||
mailchimp_log('product_submit.error', mailchimp_error_trace($e, "addStoreProduct :: #{$this->product_id}"));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MailChimp_WooCommerce_MailChimpApi
|
||||
*/
|
||||
public function api()
|
||||
{
|
||||
if (is_null($this->api)) {
|
||||
|
||||
$this->store_id = mailchimp_get_store_id();
|
||||
$options = get_option('mailchimp-woocommerce', array());
|
||||
|
||||
if (!empty($this->store_id) && is_array($options) && isset($options['mailchimp_api_key'])) {
|
||||
return $this->api = new MailChimp_WooCommerce_MailChimpApi($options['mailchimp_api_key']);
|
||||
}
|
||||
|
||||
throw new \RuntimeException('The MailChimp API is not currently configured!');
|
||||
}
|
||||
|
||||
return $this->api;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MailChimp_WooCommerce_Transform_Products
|
||||
*/
|
||||
public function transformer()
|
||||
{
|
||||
if (is_null($this->service)) {
|
||||
return $this->service = new MailChimp_WooCommerce_Transform_Products();
|
||||
}
|
||||
|
||||
return $this->service;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Vextras.
|
||||
*
|
||||
* Name: Ryan Hungate
|
||||
* Email: ryan@vextras.com
|
||||
* Date: 11/14/16
|
||||
* Time: 9:38 AM
|
||||
*/
|
||||
class MailChimp_WooCommerce_User_Submit extends WP_Job
|
||||
{
|
||||
public $user_id;
|
||||
public $subscribed;
|
||||
public $updated_data;
|
||||
|
||||
/**
|
||||
* MailChimp_WooCommerce_User_Submit constructor.
|
||||
* @param null $user_id
|
||||
* @param null $subscribed
|
||||
* @param WP_User|null $updated_data
|
||||
*/
|
||||
public function __construct($user_id = null, $subscribed = null, $updated_data = null)
|
||||
{
|
||||
if (!empty($user_id)) {
|
||||
$this->user_id = $user_id;
|
||||
}
|
||||
|
||||
if (is_bool($subscribed)) {
|
||||
$this->subscribed = $subscribed;
|
||||
}
|
||||
|
||||
if (!empty($updated_data)) {
|
||||
$this->updated_data = $updated_data->to_array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if (!mailchimp_is_configured()) {
|
||||
mailchimp_debug(get_called_class(), 'mailchimp is not configured properly');
|
||||
return false;
|
||||
}
|
||||
|
||||
$options = get_option('mailchimp-woocommerce', array());
|
||||
$store_id = mailchimp_get_store_id();
|
||||
|
||||
// load up the user.
|
||||
$user = new WP_User($this->user_id);
|
||||
|
||||
// we need a valid user, a valid store id and options to continue
|
||||
if ($user->ID <= 0 || empty($store_id) || !is_array($options)) {
|
||||
|
||||
// seems as if the database records are not being set by the time this queue job is fired,
|
||||
// just a precautionary to make sure it's available during
|
||||
sleep(3);
|
||||
|
||||
$options = get_option('mailchimp-woocommerce', array());
|
||||
$store_id = mailchimp_get_store_id();
|
||||
|
||||
// load up the user.
|
||||
$user = new WP_User($this->user_id);
|
||||
|
||||
if ($user->ID <= 0 || empty($store_id) || !is_array($options)) {
|
||||
mailchimp_log('member.sync', "Invalid Data For Submission :: {$user->user_email}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// if we have a null value, we need to grab the correct user meta for is_subscribed
|
||||
if (is_null($this->subscribed)) {
|
||||
$user_subscribed = get_user_meta($this->user_id, 'mailchimp_woocommerce_is_subscribed', true);
|
||||
if ($user_subscribed === '' || $user_subscribed === null) {
|
||||
mailchimp_log('member.sync', "Skipping sync for {$user->user_email} because no subscriber status has been set");
|
||||
return false;
|
||||
}
|
||||
$this->subscribed = (bool) $user_subscribed;
|
||||
}
|
||||
|
||||
$api_key = isset($options['mailchimp_api_key']) ? $options['mailchimp_api_key'] : false;
|
||||
$list_id = isset($options['mailchimp_list']) ? $options['mailchimp_list'] : false;
|
||||
|
||||
// we need a valid api key and list id to continue
|
||||
if (empty($api_key) || empty($list_id)) {
|
||||
mailchimp_log('member.sync', "Invalid Api Key or ListID :: {$user->user_email}");
|
||||
return false;
|
||||
}
|
||||
|
||||
// don't let anyone be unsubscribed from the list - that should only happen on email campaigns
|
||||
// and someone clicking the unsubscribe linkage.
|
||||
if (!$this->subscribed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$api = new MailChimp_WooCommerce_MailChimpApi($api_key);
|
||||
|
||||
$merge_vars = array();
|
||||
|
||||
$fn = trim($user->first_name);
|
||||
$ln = trim($user->last_name);
|
||||
|
||||
if (!empty($fn)) $merge_vars['FNAME'] = $fn;
|
||||
if (!empty($ln)) $merge_vars['LNAME'] = $ln;
|
||||
|
||||
try {
|
||||
|
||||
// see if we have a member.
|
||||
$api->member($list_id, $user->user_email);
|
||||
|
||||
// if we're updating a member and the email is different, we need to delete the old person
|
||||
if (is_array($this->updated_data) && isset($this->updated_data['user_email'])) {
|
||||
|
||||
if ($this->updated_data['user_email'] !== $user->user_email) {
|
||||
|
||||
// delete the old
|
||||
$api->deleteMember($list_id, $this->updated_data['user_email']);
|
||||
|
||||
// subscribe the new
|
||||
$api->subscribe($list_id, $user->user_email, $this->subscribed, $merge_vars);
|
||||
|
||||
mailchimp_log('member.sync', 'Subscriber Swap '.$this->updated_data['user_email'].' to '.$user->user_email, $merge_vars);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ok let's update this member
|
||||
$api->update($list_id, $user->user_email, $this->subscribed, $merge_vars);
|
||||
|
||||
mailchimp_log('member.sync', "Updated Member {$user->user_email}", $merge_vars);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
// if we have a 404 not found, we can create the member
|
||||
if ($e->getCode() == 404) {
|
||||
|
||||
try {
|
||||
$api->subscribe($list_id, $user->user_email, $this->subscribed, $merge_vars);
|
||||
mailchimp_log('member.sync', "Subscribed Member {$user->user_email}", $merge_vars);
|
||||
} catch (\Exception $e) {
|
||||
mailchimp_log('member.sync', $e->getMessage());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
mailchimp_error('member.sync', mailchimp_error_trace($e, $user->user_email));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user