Last active
June 28, 2017 08:54
-
-
Save panoslyrakis/e5d5daf1d8fb85f7c3995a2bd7570c79 to your computer and use it in GitHub Desktop.
Membership - Edit invoice gateways
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 - Edit invoice gateways | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Adds a new field for gateway option in billing info | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_Invoice_Gateways' ) ) { | |
class WPMUDEV_MS_Invoice_Gateways { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MS_Invoice_Gateways(); | |
} | |
return self::$_instance; | |
} | |
public function __construct() { | |
add_filter( 'ms_view_billing_edit_prepare_fields', array( $this, 'add_gateway_field' ), 10, 2 ); | |
add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ), 20 ); | |
add_action( 'admin_init', array( $this, 'update_subscription_gateway' ) ); | |
add_filter( 'ms_model_relationship_get_payment_description', array( $this, 'subscription_payment_description' ), 10, 6 ); | |
} | |
public function add_gateway_field( $fields, $billing_obj ){ | |
$invoice = $billing_obj->data['invoice']; | |
$gateway_field = array( | |
'gateway_id' => array( | |
'id' => 'gateway_id', | |
'title' => __( 'Invoice gateway', 'membership2' ), | |
'type' => MS_Helper_Html::INPUT_TYPE_SELECT, | |
'field_options' => self::get_available_gateways(), | |
'value' => $invoice->gateway_id | |
) | |
); | |
return $this->push_array( 'status', $fields, $gateway_field ); | |
} | |
public function push_array( $key = false, $array = array(), $new_array = array() ){ | |
if( ! $key ){ | |
return; | |
} | |
$offset = array_search($key, array_keys($array)) + 1; | |
return array_merge( | |
array_slice($array, 0, $offset), | |
$new_array, | |
array_slice($array, $offset, null) | |
); | |
} | |
public static function get_available_gateways(){ | |
$gateways = MS_Model_Gateway::get_gateways( true ); | |
$result = array(); | |
foreach( $gateways as $gateway_key => $gateway_data ){ | |
$result[$gateway_key] = $gateway_data->name; | |
} | |
return $result; | |
} | |
public function admin_print_scripts(){ | |
global $pagenow; | |
if( $pagenow != 'admin.php' || ! isset( $_GET['page'] ) || $_GET['page'] != 'membership2-add-member' ){ | |
return; | |
} | |
if( ! isset( $_GET['user_id'] ) || ! is_numeric( $_GET['user_id'] ) ){ | |
return; | |
} | |
$user_id = (int)$_GET['user_id']; | |
$user = MS_Factory::load( 'MS_Model_Member', $user_id ); | |
$gateways = ''; | |
$gateways_options = array(); | |
if ( $user->subscriptions ) { | |
$gateways = MS_Model_Gateway::get_gateway_names( false, true ); | |
foreach ( $user->subscriptions as $subscription ) { | |
$select_field_name = 'mem_' . $subscription->membership_id . '[gateway_id]'; | |
$gateways_options[] = $this->create_gateway_select( $gateways, $subscription->gateway_id, $select_field_name ); | |
if ( MS_Model_Relationship::STATUS_DEACTIVATED == $subscription->status ) { | |
continue; | |
} | |
$the_membership = $subscription->get_membership(); | |
} | |
} | |
?> | |
<script type="text/javascript"> | |
(function($){ | |
$(document).ready(function(){ | |
<?php | |
foreach( $gateways_options as $key => $gateways_option ){ | |
?> | |
$gateway_field = $('.ms-group-subscriptions form .wpmui-html-table:eq( <?php echo $key ?> ) tr:eq( 1 ) > td' ); | |
$gateway_field.html( '<?php echo $gateways_option; ?>' ); | |
<?php | |
} | |
?> | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
public function create_gateway_select( $gateways, $selected_val, $name ){ | |
$return = '<select name="' . $name . '">'; | |
foreach( $gateways as $gateway_id => $gateway ){ | |
$selected = $gateway_id == $selected_val ? ' selected' : ''; | |
$return .= '<option value="' . $gateway_id . '" '. $selected .'>' . $gateway . '</option>'; | |
} | |
$return .= '</select>'; | |
return $return; | |
} | |
public function update_subscription_gateway(){ | |
global $pagenow; | |
if( $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; | |
} | |
$user_id = $data['user_id']; | |
$user = MS_Factory::load( 'MS_Model_Member', $user_id ); | |
$memberships = isset( $data['memberships'] ) ? $data['memberships'] : array(); | |
foreach ( $memberships as $membership_id ) { | |
if ( empty( $_POST['mem_' . $membership_id] ) ) { continue; } | |
$subscription = $user->get_subscription( $membership_id ); | |
$data = $_POST['mem_' . $membership_id]; | |
//$subscription->start_date = $data['start']; | |
//$subscription->expire_date = $data['expire']; | |
//$subscription->status = $data['status']; | |
$subscription->gateway_id = $data['gateway_id']; | |
//get_payment_description | |
$subscription->save(); | |
} | |
} | |
public function subscription_payment_description( $desc, $membership, $payment_type, $MS_Model_Relationship, $invoice, $short ){ | |
global $pagenow; | |
if( ! is_admin() || $pagenow != 'admin.php' || | |
! isset( $_GET['page'] ) || $_GET['page'] != 'membership2-add-member' || | |
! isset( $_GET['user_id'] ) || ! is_numeric( $_GET['user_id'] ) ){ | |
return $desc; | |
} | |
if( $invoice != null ){ | |
return $desc; | |
} | |
$user_id = (int)$_GET['user_id']; | |
$currency = MS_Plugin::instance()->settings->currency; | |
$membership = $MS_Model_Relationship->get_membership(); | |
$desc = ''; | |
if ( null !== $invoice ) { | |
$total_price = $invoice->total; // Includes Tax | |
$trial_price = $invoice->trial_price; // Includes Tax | |
} else { | |
$total_price = $membership->total_price; // Excludes Tax | |
$trial_price = $membership->trial_price; // Excludes Tax | |
} | |
$total_price = MS_Helper_Billing::format_price( $total_price ); | |
$trial_price = MS_Helper_Billing::format_price( $trial_price ); | |
$payment_type = $MS_Model_Relationship->payment_type; | |
if ( ! $payment_type ) { | |
$payment_type = $membership->payment_type; | |
} | |
switch ( $payment_type ) { | |
case MS_Model_Membership::PAYMENT_TYPE_PERMANENT: | |
if ( 0 == $total_price ) { | |
if ( $short ) { | |
$lbl = __( 'Nothing (for ever)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will pay nothing for permanent access.', 'membership2' ); | |
} | |
} else { | |
if ( $short ) { | |
$lbl = __( '<span class="price">%1$s %2$s</span> (for ever)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will pay <span class="price">%1$s %2$s</span> for permanent access.', 'membership2' ); | |
} | |
} | |
if ( MS_Model_Member::is_admin_user( $user_id ) ) { | |
$desc = __( 'Admin has no fees!', 'membership' ); | |
} else { | |
$desc = sprintf( | |
$lbl, | |
$currency, | |
$total_price | |
); | |
} | |
break; | |
case MS_Model_Membership::PAYMENT_TYPE_FINITE: | |
if ( 0 == $total_price ) { | |
if ( $short ) { | |
$lbl = __( 'Nothing (until %4$s)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will pay nothing for access until %3$s.', 'membership2' ); | |
} | |
} else { | |
if ( $short ) { | |
$lbl = __( '<span class="price">%1$s %2$s</span> (until %4$s)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will pay <span class="price">%1$s %2$s</span> for access until %3$s.', 'membership2' ); | |
} | |
} | |
$desc .= sprintf( | |
$lbl, | |
$currency, | |
$total_price, | |
MS_Helper_Period::format_date( $MS_Model_Relationship->calc_expire_date( MS_Helper_Period::current_time() ) ), | |
$MS_Model_Relationship->calc_expire_date( MS_Helper_Period::current_time() ) | |
); | |
break; | |
case MS_Model_Membership::PAYMENT_TYPE_DATE_RANGE: | |
if ( 0 == $total_price ) { | |
if ( $short ) { | |
$lbl = __( 'Nothing (%5$s - %6$s)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will pay nothing for access from %3$s until %4$s.', 'membership2' ); | |
} | |
} else { | |
if ( $short ) { | |
$lbl = __( '<span class="price">%1$s %2$s</span> (%5$s - %6$s)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will pay <span class="price">%1$s %2$s</span> to access from %3$s until %4$s.', 'membership2' ); | |
} | |
} | |
$desc .= sprintf( | |
$lbl, | |
$currency, | |
$total_price, | |
MS_Helper_Period::format_date( $membership->period_date_start ), | |
MS_Helper_Period::format_date( $membership->period_date_end ), | |
$membership->period_date_start, | |
$membership->period_date_end | |
); | |
break; | |
case MS_Model_Membership::PAYMENT_TYPE_RECURRING: | |
if ( 1 == $membership->pay_cycle_repetitions ) { | |
// Exactly 1 payment. Actually same as the "finite" type. | |
if ( $short ) { | |
$lbl = __( '<span class="price">%1$s %2$s</span> (once)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will pay <span class="price">%1$s %2$s</span> once.', 'membership2' ); | |
} | |
} else { | |
if ( $membership->pay_cycle_repetitions > 1 ) { | |
// Fixed number of payments (more than 1) | |
if ( $short ) { | |
$lbl = __( '%4$s times <span class="price">%1$s %2$s</span> (each %3$s)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will make %4$s payments of <span class="price">%1$s %2$s</span>, one each %3$s.', 'membership2' ); | |
} | |
} else { | |
// Indefinite number of payments | |
if ( $short ) { | |
$lbl = __( '<span class="price">%1$s %2$s</span> (each %3$s)', 'membership2' ); | |
} else { | |
$lbl = __( 'You will pay <span class="price">%1$s %2$s</span> each %3$s.', 'membership2' ); | |
} | |
} | |
} | |
$desc = apply_filters( | |
'ms_model_relationship_get_payment_description/recurring', | |
sprintf( | |
$lbl, | |
$currency, | |
$total_price, | |
MS_Helper_Period::get_period_desc( $membership->pay_cycle_period ), | |
$membership->pay_cycle_repetitions | |
), | |
$short, | |
$currency, | |
$total_price, | |
$membership, | |
$invoice | |
); | |
break; | |
} | |
if ( $MS_Model_Relationship->is_trial_eligible() && 0 != $total_price ) { | |
if ( 0 == absint( $trial_price ) ) { | |
if ( $short ) { | |
if ( MS_Model_Membership::PAYMENT_TYPE_RECURRING == $payment_type ) { | |
$lbl = __( 'after %4$s', 'membership2' ); | |
} else { | |
$lbl = __( 'on %4$s', 'membership2' ); | |
} | |
} else { | |
$trial_price = __( 'nothing', 'membership2' ); | |
$lbl = __( 'Your %1$s free trial ends on %5$s and then you will be billed.', 'membership2' ); | |
} | |
} else { | |
$trial_price = MS_Helper_Billing::format_price( $trial_price ); | |
$lbl = __( 'For the trial period of %1$s you only pay <span class="price">%2$s %3$s</span>.', 'membership2' ); | |
} | |
$desc .= sprintf( | |
' <br />' . $lbl, | |
MS_Helper_Period::get_period_desc( $membership->trial_period, true ), | |
$currency, | |
$trial_price, | |
MS_Helper_Period::format_date( | |
$invoice->due_date, | |
__( 'M j', 'membership2' ) | |
), | |
MS_Helper_Period::format_date( $invoice->trial_ends ) | |
); | |
} | |
return $desc; | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_MS_Invoice_Gateways'] = WPMUDEV_MS_Invoice_Gateways::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment