Last active
February 18, 2019 19:52
-
-
Save wpmudev-sls/bd7550cf4d4d7521fe1bddcc2a834754 to your computer and use it in GitHub Desktop.
[Membersship] - Memberships Bulk Invoices. Lists invoices from subscriptions of a specific membership that their total is 0 and their number is higher than 1
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] - Memberships Bulk Invoices | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Lists invoices from subscriptions of a specific membership that their total is 0 and their number is higher than 1 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_Bulk_Invoices' ) ) { | |
class WPMUDEV_MS_Bulk_Invoices { | |
private static $_instance = null; | |
private $limit = 10; | |
public static $memberships; | |
public static $invoice_statuses; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MS_Bulk_Invoices(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
$this->setup_data(); | |
add_action( 'admin_menu', array( $this, 'admin_menu' ), 999 ); | |
add_action( 'admin_footer', array( $this, 'scripts' ) ); | |
add_action( 'wp_ajax_wpmudev_ms_bulk_invoices', array( $this, 'fetch_invoices_ajax' ), 10 ); | |
add_action( 'wp_ajax_wpmudev_ms_bulk_invoices_reset', array( $this, 'reset_invoices_ajax' ), 10 ); | |
} | |
protected function reset_invoices( $invoices = null, $price = null, $status = 'default' ) { | |
if ( is_null( $invoices ) || is_null( $price ) || is_null( $status ) ) { | |
return false; | |
} | |
foreach ( $invoices as $invoice_id => $invoice_data ) { | |
update_post_meta( $invoice_id, 'amount', $price ); | |
update_post_meta( $invoice_id, 'total', $price ); | |
if ( MS_Gateway_Free::ID === $invoice_data['gateway'] ) { | |
update_post_meta( $invoice_id, 'gateway_id', MS_Gateway_Manual::ID ); | |
} | |
if ( 'default' != $status ) { | |
update_post_meta( $invoice_id, 'status', $status ); | |
} | |
} | |
return true; | |
} | |
public function reset_invoices_ajax() { | |
check_ajax_referer( 'wpmudev_ms_bulk_invoices_reset', 'nonce' ); | |
$membership_id = (int) $_POST['membership_id']; | |
$price = MS_Helper_Billing::format_price($_POST['price'] ); | |
$status = filter_input( INPUT_POST, 'status', FILTER_DEFAULT ); | |
$success = false; | |
$content = 'Something went wrong'; | |
$invoices = $this->fetch_invoices( $membership_id ); | |
if ( ! ! $invoices ) { | |
$success = true; | |
$content = $this->reset_invoices( $invoices, $price, $status ); | |
} | |
$return = array( | |
'success' => $success, | |
'content' => $content, | |
'price' => $price | |
); | |
wp_send_json($return); | |
} | |
protected function fetch_invoices( $membership_id = null ) { | |
if ( is_null( $membership_id ) ) { | |
return false; | |
} | |
global $wpdb; | |
$membership_id = (int) $membership_id; | |
$invoices = array(); | |
$gateways = MS_Model_Gateway::get_gateways(); | |
$invoice_ids = $wpdb->get_results( " | |
SELECT ID FROM {$wpdb->posts} i | |
LEFT JOIN {$wpdb->postmeta} a on a.post_id = i.ID | |
LEFT JOIN {$wpdb->postmeta} m on m.post_id = i.ID | |
LEFT JOIN {$wpdb->postmeta} n on n.post_id = i.ID | |
LEFT JOIN {$wpdb->postmeta} d on n.post_id = i.ID | |
WHERE i.post_type = 'ms_invoice' AND | |
a.meta_key='amount' AND a.meta_value='0' AND | |
m.meta_key='membership_id' AND m.meta_value='{$membership_id}' AND | |
n.meta_key='invoice_number' AND CAST( n.meta_value as INT ) >= 2 | |
LIMIT {$this->limit} | |
" ); | |
foreach ( $invoice_ids as $invoice_obj ) { | |
$invoice = MS_Factory::load( 'MS_Model_Invoice', $invoice_obj->ID ); | |
$member = MS_Factory::load( 'MS_Model_Member', $invoice->user_id ); | |
$gateway = isset( $gateways[ $invoice->gateway_id ] ) && isset( $gateways[ $invoice->gateway_id ]->name ) ? $gateways[ $invoice->gateway_id ]->name : $invoice->gateway_id; | |
$invoices[ $invoice->id ] = array( | |
'number' => $invoice->get_invoice_number(), | |
'invoice_url' => $invoice_url = MS_Controller_Plugin::get_admin_url( | |
'billing', | |
array( 'action' => 'edit', 'invoice_id' => $invoice->id ) | |
), | |
'total' => $invoice->total, | |
'status' => $invoice->status, | |
'gateway' => $gateway, | |
'username' => $member->username, | |
'subscription_url' => MS_Controller_Plugin::get_admin_url( 'add-member', array( 'user_id' => $member->id ) ), | |
); | |
} | |
return $invoices; | |
} | |
public function limit_results( $args ) { | |
unset( $args['nopaging'] ); | |
$args[ 'posts_per_page' ] = $this->limit; | |
return $args; | |
} | |
public function fetch_invoices_ajax() { | |
check_ajax_referer( 'wpmudev_ms_bulk_invoices', 'nonce' ); | |
$membership_id = (int) $_POST['membership_id']; | |
$membership = MS_Factory::load( 'MS_Model_Membership', $membership_id ); | |
$success = false; | |
$content = 'No invoices with 0 amount for this memberhip'; | |
$price = $membership->price; | |
$invoices = $this->fetch_invoices( $membership_id ); | |
if ( ! ! $invoices ) { | |
$success = true; | |
$content = $this->to_html( $invoices ); | |
} | |
$return = array( | |
'success' => $success, | |
'content' => $content, | |
'price' => $price | |
); | |
wp_send_json($return); | |
} | |
protected function to_html( $invoices ) { | |
$html = ''; | |
if ( empty( $invoices) ) { | |
return 'No invoices for this memerhip'; | |
} | |
$html .= ' | |
<table class="wp-list-table widefat fixed"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>Total</th> | |
<th>Status</th> | |
<th>Gateway</th> | |
<th>Username</th> | |
</tr> | |
</thead> | |
<tbody> | |
'; | |
foreach ( $invoices as $invoice ) { | |
$invoice = (object) $invoice; | |
ob_start(); | |
$invoice_link = "<a href=\"{$invoice->invoice_url}\">{$invoice->number}</a>"; | |
$subscription_link = "<a href=\"{$invoice->subscription_url}\">{$invoice->username}</a>"; | |
?> | |
<tr> | |
<td><?php echo $invoice_link; ?></td> | |
<td><?php echo $invoice->total; ?></td> | |
<td><?php echo self::$invoice_statuses[ $invoice->status ]; ?></td> | |
<td><?php echo $invoice->gateway; ?></td> | |
<td><?php echo $subscription_link; ?></td> | |
</tr> | |
<?php | |
$html .= ob_get_clean(); | |
} | |
$html .= '</tbody></table>'; | |
return $html; | |
} | |
public function admin_menu() { | |
add_submenu_page( | |
'membership2', | |
'Bulk Invoices', | |
'Bulk Invoices', | |
'manage_options', | |
'ms-bulk-invoices', | |
array( $this, 'admin_page_display' ) | |
); | |
} | |
public function admin_page_display() { | |
?> | |
<div class="ms-wrap wrap"> | |
<h2 class="ms-settings-title">Memberships Bulk invoices</h2> | |
<div style="padding: 20px; background: #fff; color: #555; font-size: 16px;"> | |
<div style="overflow:hidden;"> | |
<div style="float:left;width:300px"> | |
<h3>All Memberships</h3> | |
<div id="memberships-list"><?php $this->list_memberships(); ?></div> | |
</div> | |
<div style="float:left; max-width: 600px;"> | |
<h3 id="invoices-title">Invoices <span></span></h3> | |
<div id="invoices-list">No membership selected. Please choose one</div> | |
</div> | |
</div> | |
</div> | |
<div id="invoice-statuses-box" style="display:none;"> | |
<select> | |
<option value="default">Keep default status</option> | |
<?php foreach ( self::$invoice_statuses as $invoice_type => $invoice_name ) : ?> | |
<option value="<?php echo $invoice_type; ?>"><?php echo $invoice_name ?></option> | |
<?php endforeach; ?> | |
</select> | |
</div> | |
</div> | |
<?php | |
} | |
protected function list_memberships() { | |
if ( ! empty( self::$memberships ) ) { | |
foreach ( self::$memberships as $membership ) { | |
$this->print_part( $membership ); | |
} | |
} | |
} | |
protected function print_part( $membership = null ) { | |
if ( is_null( $membership ) || ! $membership instanceof MS_Model_Membership ) { | |
return; | |
} | |
?> | |
<div> | |
<a style="cursor: pointer;" data-membership-id="<?php echo $membership->id; ?>"><?php echo $membership->name; ?></a> | |
</div> | |
<?php | |
} | |
protected function setup_data() { | |
self::$memberships = MS_Model_Membership::get_public_memberships(); | |
self::$invoice_statuses = MS_Model_Invoice::get_status_types(); | |
} | |
public function scripts() { | |
if ( ! is_callable( 'get_current_screen' ) || 'membership-2_page_ms-bulk-invoices' !== get_current_screen()->id ) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(function($){ | |
$(document).ready(function(){ | |
$( '#memberships-list a' ).on( 'click', ms_fetch_invoices ); | |
$( document ).on( 'click', '#ms-invoices-new-price-button', ms_reset_invoices_prices ); | |
}); | |
function ms_reset_invoices_prices() { | |
data = { | |
action: 'wpmudev_ms_bulk_invoices_reset', | |
nonce: '<?php echo wp_create_nonce( "wpmudev_ms_bulk_invoices_reset" ); ?>', | |
membership_id: $( '#ms-invoices-membership_id' ).val(), | |
price : $( '#ms-invoices-new-price' ).val(), | |
status : $( '#ms-invoices-status' ).val() | |
}; | |
$.post(ajaxurl, data, function(response) { | |
alert( "Invoices have been reset" ); | |
window.location.reload(true); | |
}); | |
} | |
function ms_fetch_invoices() { | |
let membership_id = $(this).data( 'membership-id' ), | |
title = $(this).html(), | |
field = $('<input>',{ | |
type: 'text', | |
id: 'ms-invoices-new-price' | |
}), | |
button = $('<a>', { | |
text : 'Set invoices price', | |
'class' : 'button button-primary', | |
id : 'ms-invoices-new-price-button' | |
}), | |
label = $( '<label>' ), | |
hidden = $( '<input>', { | |
type: 'hidden', | |
value: membership_id, | |
id : 'ms-invoices-membership_id' | |
} ), | |
row = $( '<div>', { | |
id : 'ms-invoices-new-price-wrap' | |
} ), | |
status_options = $( '#invoice-statuses-box select' ).html(), | |
status_label = $( '<label>' ), | |
status_select = $('<select />', { | |
id : 'ms-invoices-status' | |
}); | |
status_select.html( status_options ); | |
//status_select.removeClass( 'select2-hidden-accessible' ); | |
//status_select.attr( 'id', 'thecooldude' ); | |
data = { | |
action: 'wpmudev_ms_bulk_invoices', | |
nonce: '<?php echo wp_create_nonce( "wpmudev_ms_bulk_invoices" ); ?>', | |
membership_id: membership_id | |
}; | |
$.post(ajaxurl, data, function(response) { | |
$( '#invoices-title span' ).html( ' - ' + title ); | |
$( '#invoices-list' ).html( response.content ); | |
if ( response.success ) { | |
field.val( response.price ); | |
label.html( "Set new price for invoices" ); | |
status_label.html( "Set new status for the invoices " ); | |
row.append( label ).append( field ).append( hidden ).append( '<br />' ).append( status_label).append( status_select ).append( '<br />' ).append( button ); | |
$( '#invoices-list' ).append( row ); | |
} | |
}); | |
} | |
})(jQuery); | |
</script> | |
<?php | |
} | |
} | |
if ( ! function_exists( 'wpmudev_ms_bulk_invoices' ) ) { | |
function wpmudev_ms_bulk_invoices(){ | |
return WPMUDEV_MS_Bulk_Invoices::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_ms_bulk_invoices', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment