Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created July 12, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/02ec5b583432259b837382aff16d01f3 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/02ec5b583432259b837382aff16d01f3 to your computer and use it in GitHub Desktop.
[Membership] - Mailchimp Segment Vars. Override Cron Status Check to set Mailchimp Segment Vars
<?php
/**
* Plugin Name: [Membership] - Mailchimp Segment Vars
* Plugin URI: https://premium.wpmudev.org/
* Description: Override Cron Status Check to set Mailchimp Segment Vars
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_MS_Cron_Mailchimp_Segment_Vars' ) ) {
class WPMUDEV_MS_Cron_Mailchimp_Segment_Vars {
private static $_instance = null;
private static $merge_fields = array();
private static $settings = null;
protected static $mailchimp_api = null;
private $transaction_type = 'ms_transaction_log';
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_MS_Cron_Mailchimp_Segment_Vars();
}
return self::$_instance;
}
private function __construct() {
self::$merge_fields = [
//'FNAME' => 'Jack',
//'LNAME' => 'White',
'MMERGE5' => 'The new value'
];
add_action( 'ms_model_relationship_check_membership_status_before', array( $this, 'subscription_status_check' ), 20, 1 );
}
/*
* This method checks purely the expiration date based on last transaction log.
* It does not check the invoice dates
* It creates an invoice only if subscription gets expired and there is no current invoice
*/
public function subscription_status_check( $subscription ) {
self::$settings = get_option( 'ms_model_settings' );
if ( empty( $subscription->expire_date ) ) {
// If there is no expiration date, subscription should be set to active.
// This can be handled by the Cron check
return;
}
$merge_vars = array();
$subscription_current_status = get_post_meta( $subscription->id, 'status', true );
$subscription_next_status = '';
$last_payment_date = $subscription->expire_date;
if ( 'admin' != $subscription->gateway_id && 'free' != $subscription->gateway_id ) {
$payment = $this->get_subscription_last_payment( $subscription );
if ( ! $payment || empty( $payment ) ){
return;
}
$last_payment_date = $payment->date;
//$last_payment_date = $subscription->expire_date;
}
$subscription_expiration_date = $this->get_subscription_expiration_date( $subscription, $last_payment_date );
if ( strtotime( $subscription_expiration_date ) >= strtotime( MS_Helper_Period::current_date() ) ) {
//Should be set to active
$subscription_next_status = MS_Model_Relationship::STATUS_ACTIVE;
}
else {
// if current status is MS_Model_Relationship::STATUS_PENDING, it should ermain like that else
//should be set to expired
if ( MS_Model_Relationship::STATUS_PENDING == $subscription->status ) {
$subscription_next_status = MS_Model_Relationship::STATUS_PENDING;
}
else {
$subscription_next_status = MS_Model_Relationship::STATUS_EXPIRED;
if ( ! empty( self::$merge_fields ) ) {
$member = $subscription->get_member();
$api_resp = self::update_mailchimp_subscriber( $member->email, self::$merge_fields );
}
}
}
}
/**
*
* Update the Subscriber info on Mailchimp based on member's email
* @param string $email Member's email address
* @param array $merge_fields The merge fields that we want updated
*/
public static function update_mailchimp_subscriber( $email, $merge_fields ) {
$verb = 'PATCH'; // Use PATCH to Update Subscriber on Mailchimp
//$list_id_registered = isset( self::$settings[ 'custom' ][ 'mailchimp' ][ 'mail_list_registered' ] ) ? self::$settings[ 'custom' ][ 'mailchimp' ][ 'mail_list_registered' ] : false;
$api_key = isset( self::$settings[ 'custom' ][ 'mailchimp' ][ 'api_key' ] ) ? self::$settings[ 'custom' ][ 'mailchimp' ][ 'api_key' ] : '';
$list_id_subscribed = isset( self::$settings[ 'custom' ][ 'mailchimp' ][ 'mail_list_members' ] ) ? self::$settings[ 'custom' ][ 'mailchimp' ][ 'mail_list_members' ] : false;
$data = array(
'merge_fields' => $merge_fields,
);
$md5_email = md5( strtolower( $email ) );
$action = "lists/{$list_id_subscribed}/members/{$md5_email}";
$data_center = explode( '-', $api_key )[1];
$endpoint = 'https://<dc>.api.mailchimp.com/3.0/';
$endpoint = str_replace( '<dc>', $data_center, $endpoint );
$url = trailingslashit( $endpoint ) . $action;
$args = array(
"method" => $verb,
"headers" => array(
'Authorization' => 'apikey '. $api_key,
'Content-Type' => 'application/json;charset=utf-8'
//'X-Trigger-Error' => 'APIKeyMissing',
),
"body" => wp_json_encode( $data )
);
return wp_remote_request( $url, $args );
}
public function get_subscription_last_payment( $subscription ) {
$transaction = $this->get_subscription_last_transaction( $subscription->id );
if ( ! $transaction || empty( $transaction ) ) {
return false;
}
return $this->payment_from_transaction( $transaction );
}
public function get_subscription_last_transaction( $subscription_id ) {
if ( is_object( $subscription_id ) ) {
if ( ! isset( $subscription_id->id ) ) {
return false;
}
$subscription_id = $subscription_id->id;
}
$transaction = array();
$args = array(
'numberposts' => 1,
'offset' => 0,
'post_status' => 'any', //'private', // transaction logs have private status in posts table
'post_type' => 'ms_transaction_log',
'meta_query' => array(
array(
'key' => 'subscription_id',
'value' => $subscription_id,
'compare' => '=',
),
array(
'key' => 'success',
'value' => 'ok',
'compare' => '='
)
)
);
$transactions = get_posts( $args );
if ( ! empty( $transactions ) ) {
return $transactions[0];
}
else {
return false;
}
}
// This returns the required fields of the transaction.
public function payment_from_transaction( $transaction ) {
$payment = array();
if ( ! $transaction instanceof WP_Post || 'ms_transaction_log' != $transaction->post_type ) {
return $payment;
}
$payment = array(
'id' => $transaction->ID,
'date' => $transaction->post_date
);
/*
// Currently we don't need to use transaction meta
$meta = get_post_meta( $transaction->ID );
foreach ( $meta as $meta_key => $meta_value ) {
if ( isset( $meta_value[0] ) && ! isset( $payment[ $meta_key ] ) ) {
$payment[ $meta_key ] = $meta_value[0];
}
}
*/
return (object) $payment;
}
public function get_subscription_expiration_date( $subscription, $last_payment_date ) {
$grace_period = apply_filters(
'ms_subscription_expiration_grace_period',
1,
$subscription
);
$period_duration = $this->get_subscription_duration( $subscription );
$expiration_date = date( 'Y-m-d H:i:s', strtotime( $last_payment_date . "+{$period_duration->period_unit} {$period_duration->period_type}" ) );
return date( 'Y-m-d H:i:s', strtotime( $expiration_date . "+{$grace_period} days" ) );
}
public function get_subscription_duration( $subscription ) {
if ( ! $subscription instanceof MS_Model_Relationship ) {
return false;
}
return (object) MS_Factory::load(
'MS_Model_Membership',
$subscription->membership_id
)->period;
}
}
if ( ! function_exists( 'wpmudev_ms_cron_mailchimp_segment_vars' ) ) {
function wpmudev_ms_cron_mailchimp_segment_vars(){
return WPMUDEV_MS_Cron_Mailchimp_Segment_Vars::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_ms_cron_mailchimp_segment_vars', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment