Created
March 8, 2018 18:22
-
-
Save wpmudev-sls/079b1470d81e35d4c3d57cfdad4a9bb4 to your computer and use it in GitHub Desktop.
[Membership 2] - Forces Pending status to new subscription made from admin
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] - Force Pending status | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Forces Pending status to new subscription made from admin | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_Force_Pending' ) ) { | |
class WPMUDEV_MS_Force_Pending { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MS_Force_Pending(); | |
} | |
return self::$_instance; | |
} | |
public function __construct() { | |
add_action( 'ms_model_member_add_membership', array( $this, 'update_subscription_status' ), 10, 5 ); | |
add_action( 'ms_model_relationship_handle_status_change_after', array( $this, 'send_notification' ), 10, 2 ); | |
// Use a custom page to list pending subscriptions from where we can activate or remove completelly | |
add_action( 'admin_menu', array(&$this, 'register_sub_menu'), 20 ); | |
add_action( 'load-membership-2_page_manage-pending-memberships', array( $this, 'manage_status_action' ) ); | |
//add_filter( 'ms_model_member_get_subscription', array( $this, 'member_get_subscription_filter' ), 10, 3 ); | |
} | |
public function update_subscription_status( $subscription, $membership_id, $gateway_id, $move_from_id, $MS_Model_Member ){ | |
global $pagenow; | |
if( ! is_admin() || $pagenow != 'admin.php' || ! isset( $_GET['page'] ) || $_GET['page'] != 'membership2-add-member' ){ | |
return $subscription; | |
} | |
$data = $_POST; | |
if( empty( $data ) || | |
! isset( $data['action'] ) || $data['action'] != 'member_subscription' || | |
! isset( $data['user_id'] ) || ! is_numeric( $data['user_id'] ) ){ | |
return $subscription; | |
} | |
if( ! $data['subscribe'] || empty( $data['subscribe'] ) ){ | |
return $subscription; | |
} | |
$invoices = $subscription->get_invoices(); | |
if( empty( $invoices ) ){ | |
$invoice = $subscription->get_next_invoice(); | |
$invoice->status = MS_Model_Invoice::STATUS_PENDING; | |
$invoice->changed(); | |
$subscription->handle_status_change( MS_Model_Relationship::STATUS_PENDING ); | |
} | |
//$subscription->handle_status_change( MS_Model_Relationship::STATUS_PENDING ); | |
return $subscription; | |
} | |
public static function send_notification( $status, $subscription ){ | |
global $pagenow; | |
if( ! is_admin() || $pagenow != 'admin.php' || ! isset( $_GET['page'] ) || $_GET['page'] != 'membership2-add-member' ){ | |
return; | |
} | |
$data = $_POST; | |
if( empty( $data ) || | |
! isset( $data['action'] ) || $data['action'] != 'member_subscription' || | |
! isset( $data['user_id'] ) || ! is_numeric( $data['user_id'] ) ){ | |
return; | |
} | |
if( ! isset( $data['subscribe'] ) || empty( $data['subscribe'] ) ){ | |
return; | |
} | |
$membership = MS_Factory::load( 'MS_Model_Membership', intval( $subscription->membership_id ) ); | |
$user_id = absint( $data['user_id'] ); | |
$user = MS_Factory::load( 'MS_Model_Member', $user_id ); | |
$blog_title = get_bloginfo( 'name' ); | |
$blog_email = get_bloginfo( 'admin_email' ); | |
$checkout_url = add_query_arg( array( | |
'step' => 'payment_table', | |
'membership_id' => $subscription->membership_id, | |
), MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_MEMBERSHIPS ) ); | |
$notification_subject = "You have a new Pending membership!"; | |
$notification_body = "<p>Hi {$user->name}!</p>"; | |
$notification_body .= "<p>You have a new subscription on {$blog_title} for membership <srong>{$membership->name}</strong> which is currently pending</p>"; | |
$notification_body .= "<p>Please follow this <a href='{$checkout_url}'>activation link</a> in order to activate the subscription</p>"; | |
$notification_body .= "<p>The {$blog_title} team</p>"; | |
//$headers = array("Content-Type: text/html; charset=UTF-8","From: {$blog_title} <{$blog_email}>"); | |
wp_mail( $user->email, $notification_subject, $notification_body ); | |
} | |
public function register_sub_menu(){ | |
add_submenu_page( | |
'membership2', | |
__( 'Pending Memberships', 'membership2' ), | |
__( 'Manage Pending Memberships', 'membership2' ), | |
'manage_options', | |
'manage-pending-memberships', | |
array( $this, 'pending_page' ) | |
); | |
} | |
public function pending_page(){ | |
global $wpdb; | |
$q = "SELECT * FROM {$wpdb->posts} as subscription | |
LEFT JOIN {$wpdb->postmeta} as meta | |
on subscription.ID = meta.post_id | |
WHERE | |
subscription.post_type = 'ms_relationship' AND | |
meta.meta_key = 'status' AND | |
meta.meta_value IN ( 'pending', 'expired' ) | |
"; | |
$pending_subscriptions = $wpdb->get_results( $q ); | |
?> | |
<h1>Pending Memberships</h1> | |
<?php | |
$table_rows = ''; | |
$_users = array(); | |
$_memberships = array(); | |
foreach ($pending_subscriptions as $key => $subscription) { | |
$user_id = $subscription->post_author; | |
$membership_id = get_post_meta( $subscription->ID, 'membership_id', true ); | |
if( ! isset( $_users[$user_id] ) ){ | |
$_users[$user_id] = (array) get_user_by( 'id', $user_id )->data; | |
} | |
if( ! isset( $_memberships[ $membership_id ] ) ){ | |
$_membership = get_post( $membership_id ); | |
$_memberships[ $membership_id ] = array( | |
'name' => $_membership->post_title | |
); | |
} | |
$user = $_users[$user_id]; | |
$membership = $_memberships[ $membership_id ]; | |
$table_rows .= '<tr>'; | |
//User info | |
$table_rows .= '<td>'; | |
$table_rows .= $user['display_name'] . '<br />( ' . $user['user_email'] . ' )'; | |
$table_rows .= '</td>'; | |
//Membership info | |
$table_rows .= '<td>'; | |
$table_rows .= '<strong>' . $membership['name'] . '</strong>'; | |
$table_rows .= '</td>'; | |
//Buttons | |
$table_rows .= '<td>'; | |
$table_rows .= '<a class="ms_membership_status_btn button" | |
data-action="activate" | |
data-member="' . $user_id . '" | |
data-membership="' . $membership_id . '" | |
><i class="dashicons dashicons-yes" style="color:green;"></i></a>'; | |
$table_rows .= '<a class="ms_membership_status_btn button" | |
data-action="cancel" | |
data-member="' . $user_id . '" | |
data-membership="' . $membership_id . '" | |
><i class="dashicons dashicons-no" style="color:red;"></i></a>'; | |
$table_rows .= '</td>'; | |
$table_rows .= '</tr>'; | |
} | |
?> | |
<table class="widefat"> | |
<tr> | |
<th>User</th> | |
<th>Membership</th> | |
<th>Actions</th> | |
</tr> | |
<?php echo $table_rows; ?> | |
</table> | |
<form method='post' id="wpmudev_ms_force_sub_status_frm"> | |
<?php wp_nonce_field( 'wpmudev_ms_force_sub_status_nonce', 'wpmudev_ms_force_sub_status' ); ?> | |
<input type="hidden" name="action" /> | |
<input type="hidden" name="member_id" /> | |
<input type="hidden" name="membership_id" /> | |
</form> | |
<script type="text/javascript"> | |
(function($){ | |
$(document).ready(function(){ | |
$( '.ms_membership_status_btn' ).on('click', function(e){ | |
e.preventDefault(); | |
let form = $( '#wpmudev_ms_force_sub_status_frm' ), | |
me = $( this ) | |
action = me.data( 'action' ) | |
action_str = (action == 'activate' ? 'Activate' : 'Remove' ); | |
if( ! confirm("Are you sure you want to " + action_str + " this membership? ") ){ | |
return; | |
} | |
form.find( 'input[name=action]' ).val( action ); | |
form.find( 'input[name=member_id]' ).val( me.data( 'member' ) ); | |
form.find( 'input[name=membership_id]' ).val( me.data( 'membership' ) ); | |
form.submit(); | |
}); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
public function manage_status_action(){ | |
if ( | |
! isset( $_POST['wpmudev_ms_force_sub_status'] ) | |
|| ! wp_verify_nonce( $_POST['wpmudev_ms_force_sub_status'], 'wpmudev_ms_force_sub_status_nonce' ) | |
) { | |
return; | |
} | |
$membership_id = intval( $_POST['membership_id'] ); | |
$member_id = intval( $_POST['member_id'] ); | |
$action = $_POST['action']; | |
$subscription = MS_Model_Relationship::get_subscription( $member_id, $membership_id ); | |
$subscription->status = MS_Model_Relationship::STATUS_ACTIVE; | |
if( $action == 'activate' ){ | |
$invoice = MS_Model_Invoice::get_current_invoice( $subscription ); | |
update_post_meta( $invoice->id, 'status', MS_Model_Invoice::STATUS_PAID ); | |
update_post_meta( $subscription->id, 'status', MS_Model_Relationship::STATUS_ACTIVE ); | |
} | |
elseif( $action == 'cancel' ){ | |
wp_delete_post( $subscription->id, true); | |
} | |
} | |
/* | |
public function member_get_subscription_filter( $subscription, $membership_id, $member ){ | |
global $pagenow; | |
if( ! is_admin() || | |
$pagenow != 'admin.php' || | |
! isset( $_GET['page'] ) || | |
$_GET['page'] != 'membership2-add-member' || | |
! isset( $_POST['mem_' . $membership_id]['status'] ) || | |
'active' != $_POST['mem_' . $membership_id]['status'] | |
){ | |
return $subscription; | |
} | |
if( ! $subscription instanceof MS_Model_Relationship ){ | |
$subscription = MS_Model_Relationship::get_subscription( $member->id, $membership_id ); | |
$subscription->status = MS_Model_Relationship::STATUS_ACTIVE; | |
$invoice = MS_Model_Invoice::get_current_invoice( $subscription ); | |
if( ! $invoice instanceof MS_Model_Invoice ){ | |
$invoice = MS_Model_Invoice::create_invoice( $subscription ); | |
} | |
update_post_meta( $invoice->id, 'status', MS_Model_Invoice::STATUS_PAID ); | |
update_post_meta( $subscription->id, 'status', MS_Model_Relationship::STATUS_ACTIVE ); | |
} | |
return $subscription; | |
} | |
*/ | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_MS_Force_Pending'] = WPMUDEV_MS_Force_Pending::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment