Created
August 12, 2017 14:54
-
-
Save wpmudev-sls/4fcca58cdfce21feda9f960d9ed7ff33 to your computer and use it in GitHub Desktop.
[Membership 2 Pro] - Adds payment notices to members that have not paid for subscriptions
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 - Add payment notices | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Adds payment notices to members that have not paid | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_Payment_Notices' ) ) { | |
class WPMUDEV_MS_Payment_Notices { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MS_Payment_Notices(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'admin_notices', array( $this, 'show_admin_notice' ), 10 ); | |
add_filter( 'ms_model_invoice_changed', array( $this, 'update_memberships_in_debt' ), 10, 1 ); | |
} | |
public function show_admin_notice( $user_id = null ){ | |
if( is_null( $user_id ) || ! is_numeric( $user_id ) ){ | |
if( ! is_user_logged_in() ){ | |
return false; | |
} | |
else{ | |
$user_id = get_current_user_id(); | |
} | |
} | |
if( $this->show_notice_to_member() ){ | |
$memberships_with_debt = get_user_meta( $user_id, 'ms2_unpaid_memberships_ids', true ); | |
$payment_links = ''; | |
if( is_array( $memberships_with_debt ) && ! empty( $memberships_with_debt ) ){ | |
foreach( $memberships_with_debt as $membership_id ){ | |
$membership = MS_Factory::load( 'MS_Model_Membership', intval( $membership_id ) ); | |
$url = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_MEMBERSHIPS ); | |
$url = esc_url_raw( | |
add_query_arg( | |
'membership_id', | |
$membership_id, | |
$url | |
) | |
); | |
$payment_links .= '<div><a href="' . $url . '"><strong>' . $membership->name . '</strong> '. MS_Helper_Billing::format_price( $membership->price ) .'</a></div>'; | |
} | |
} | |
echo ' | |
<div class="notice notice-warning is-dismissible"> | |
<p> | |
<div>There are some pending subscriptions:</div> | |
'. $payment_links .' | |
</p> | |
</div>'; | |
} | |
} | |
public function show_notice_to_member( $user_id = null ){ | |
if( is_null( $user_id ) ){ | |
if( ! is_user_logged_in() ){ | |
return false; | |
} | |
else{ | |
$user_id = get_current_user_id(); | |
} | |
} | |
$user = get_user_by( 'id', $user_id ); | |
//Don't show notice to administrators | |
if( $user->has_cap( 'manage_options' ) ){ | |
return false; | |
} | |
$show_notice = get_user_meta( $user_id, 'ms2_unpaid_memberships_ids', true ); | |
if( empty( $show_notice ) || ! $show_notice ){ | |
$show_notice = $this->check_member_debts(); | |
} | |
else{ | |
$show_notice = true; | |
} | |
return $show_notice; | |
} | |
public function check_member_debts( $user_id = null ){ | |
if( is_null( $user_id ) ){ | |
if( ! is_user_logged_in() ){ | |
return false; | |
} | |
else{ | |
$user_id = get_current_user_id(); | |
} | |
} | |
$global_option = ! is_multisite(); | |
$member = MS_Factory::load( 'MS_Model_Member', $user_id ); | |
$has_unpaid_invoices = false; | |
$memberships_with_debt = array(); | |
$subscriptions = MS_Model_Relationship::get_subscriptions( | |
array( | |
'user_id' => $user_id, | |
'status' => 'all', | |
) | |
); | |
foreach( $subscriptions as $subscription ){ | |
$gateway = $subscription->get_gateway(); | |
if( $gateway->id == 'admin' ){ | |
$membership = $subscription->get_membership(); | |
if( $membership->is_free() ){ | |
continue; | |
} | |
//Create an unpaid invoice with Manual payment gateway | |
$subscription->gateway_id = 'manual'; | |
$subscription->status = MS_Model_Invoice::STATUS_PENDING; | |
$subscription->save(); | |
MS_Model_Invoice::create_invoice( $subscription ); | |
} | |
$invoices = $subscription->get_invoices(); | |
foreach( $invoices as $invoice ){ | |
if( ! $invoice->is_paid() ){ | |
$has_unpaid_invoices = true; | |
$membership = $invoice->get_membership(); | |
$memberships_with_debt[] = $membership->id; | |
} | |
} | |
} | |
if( $has_unpaid_invoices && ! empty( $memberships_with_debt ) ){ | |
update_user_option( $user_id, 'ms2_unpaid_memberships_ids', $memberships_with_debt, $global_option ); | |
} | |
else{ | |
update_user_option( $user_id, 'ms2_unpaid_memberships_ids', false, $global_option ); | |
} | |
return $has_unpaid_invoices; | |
} | |
public function update_memberships_in_debt( $invoice ){ | |
if( $invoice->is_paid() ){ | |
$this->check_member_debts( $invoice->user_id ); | |
} | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_MS_Invoice_Gateways'] = WPMUDEV_MS_Payment_Notices::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment