Last active
November 7, 2019 12:13
-
-
Save wpmudev-sls/e0760ed6436acc300e699b9e06e57f33 to your computer and use it in GitHub Desktop.
[Membership] - Expiration Dates. Recheck Expiration dates on cron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Membership] - Expiration Dates | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Recheck Expiration dates on cron | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_Expire_Date_Check' ) ) { | |
class WPMUDEV_MS_Expire_Date_Check { | |
private static $_instance = null; | |
private $transaction_type = 'ms_transaction_log'; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MS_Expire_Date_Check(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_filter( 'ms_model_relationship_check_membership_status_after', array( $this, 'subscription_expire_date_check' ), 20, 2 ); | |
} | |
public function subscription_expire_date_check( $subscription ) { | |
if ( | |
empty( $subscription->expire_date ) || | |
( $subscription->is_trial_eligible() && strtotime( $subscription->trial_expire_date ) >= strtotime( MS_Helper_Period::current_date() ) ) | |
) | |
{ | |
return; | |
} | |
//$last_payment_date = $subscription->expire_date; | |
//if ( 'admin' != $subscription->gateway_id && 'free' != $subscription->gateway_id ) { | |
$payment = $this->get_subscription_last_payment( $subscription ); | |
$subscription_expiration_date = $subscription->expire_date; | |
if ( ! ! $payment && ! empty( $payment ) ) { | |
$last_payment_date = $payment->date; | |
$subscription_expiration_date = $this->get_subscription_expiration_date( $subscription, $last_payment_date ); | |
} | |
if ( strtotime( $subscription->expire_date ) > strtotime( $subscription_expiration_date ) ) { | |
update_post_meta( $subscription->id, 'expire_date', $subscription_expiration_date ); | |
$this->log( "#{$subscription->id} SET EXPIRE DATE TO {$subscription_expiration_date}" ); | |
// Maybe update the last invoice date too? | |
$invoice = $subscription->get_current_invoice(); | |
if ( $invoice->is_paid() ) { | |
update_post_meta( $invoice->id, 'due_date', $subscription_expiration_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; | |
if ( | |
$subscription->status != $subscription_next_status && | |
$subscription->status != MS_Model_Relationship::STATUS_DEACTIVATED && | |
$subscription->status != MS_Model_Relationship::STATUS_CANCELED | |
) | |
{ | |
$this->set_status( $subscription, $subscription_next_status ); | |
$this->log( "#{$subscription->id} STATUS SET TO {$subscription_next_status}" ); | |
} | |
if ( strtotime( $subscription->expire_date ) < strtotime( $subscription_expiration_date ) ) { | |
update_post_meta( $subscription->id, 'expire_date', $subscription_expiration_date ); | |
$this->log( "#{$subscription->id} SET EXPIRE DATE TO {$subscription_expiration_date}" ); | |
} | |
} | |
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; | |
$this->set_status( $subscription, $subscription_next_status ); | |
$this->log( "#{$subscription->id} STATUS SET TO {$subscription_next_status}" ); | |
} | |
} | |
// } | |
} | |
private function log( $message ) { | |
if ( ! defined( "WP_DEBUG_LOG" ) || ! WP_DEBUG_LOG ) { | |
return; | |
} | |
$time = date( 'd-M-Y H:i:s' ); | |
error_log( | |
"[ {$time} ] \n{$message}\n\n\n", | |
3, | |
WP_CONTENT_DIR . "/ms-crondebug-" . date( 'd-m-Y' ) . ".log" | |
); | |
} | |
private function set_status( $subscription, $status ) { | |
update_post_meta( $subscription->id, 'status', $status ); | |
if ( MS_Model_Relationship::STATUS_EXPIRED == $status ) { | |
// Since this subscription is expired, lets check if we need to move membership: | |
$membership = $subscription->get_membership(); | |
if ( ! empty( $membership->on_end_membership_id ) ) { | |
$this->maybe_move_membership( $subscription, $membership ); | |
} | |
} | |
} | |
private function maybe_move_membership( $subscription, $membership ) { | |
// Do not continue if on_end_membership_id is empty. | |
if ( empty( $membership->on_end_membership_id ) ) { | |
return; | |
} | |
// Deactivate the current membership. | |
$subscription->deactivate_membership(); | |
// Move membership to configured membership. | |
$new_membership = MS_Factory::load( | |
'MS_Model_Membership', | |
$membership->on_end_membership_id | |
); | |
if ( $new_membership->is_valid() ) { | |
$member = MS_Factory::load( 'MS_Model_Member', $subscription->user_id ); | |
$new_subscription = $member->add_membership( | |
$membership->on_end_membership_id, | |
$subscription->gateway_id | |
); | |
MS_Model_Event::save_event( | |
MS_Model_Event::TYPE_MS_MOVED, | |
$new_subscription | |
); | |
/* | |
* If the new membership is paid we want that the user | |
* confirms the payment in his account. So we set it | |
* to "Pending" first. If its free we set it as active | |
*/ | |
if ( ! $new_membership->is_free() ) { | |
$new_subscription->status = MS_Model_Relationship::STATUS_PENDING; | |
} else { | |
$new_subscription->status = MS_Model_Relationship::STATUS_ACTIVE; | |
} | |
// Save new membership. | |
$new_subscription->save(); | |
} | |
} | |
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', strtotime( $expiration_date . "+{$grace_period} days" ) ); | |
// We must not add the grace period here. The grace period is added on top of the expiration date of the subscription | |
return date( 'Y-m-d', strtotime( $expiration_date ) ); | |
} | |
public function get_subscription_duration( $subscription ) { | |
if ( ! $subscription instanceof MS_Model_Relationship ) { | |
return false; | |
} | |
return (object) get_post_meta( $subscription->membership_id, 'pay_cycle_period', true ); | |
//return (object) MS_Factory::load( | |
// 'MS_Model_Membership', | |
// $subscription->membership_id | |
//)->pay_cycle_period; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_ms_expire_date_check' ) ) { | |
function wpmudev_ms_expire_date_check(){ | |
return WPMUDEV_MS_Expire_Date_Check::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_ms_expire_date_check', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment