Created
June 23, 2018 05:48
-
-
Save wpmudev-sls/5d0adaa3bf567a86ca1860ae1dc6e045 to your computer and use it in GitHub Desktop.
[Membership] - Set Manual Gateway. Allows to switch subscription gateway from Admin to Manual
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] - Set Manual Gateway | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Allows to switch subscription gateway from Admin to Manual | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_Set_Manual_Gateway' ) ) { | |
class WPMUDEV_MS_Set_Manual_Gateway { | |
private static $_instance = null; | |
private $gdpr_settings_args = array(); | |
private $items_titles = array() ; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_MS_Set_Manual_Gateway(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'wp_ajax_wpmudev_ms_change_sub_gw', array( $this, 'change_gateway' ), 20 ); | |
add_action( 'admin_footer', array( $this, 'admin_footer' ), 20 ); | |
add_filter( 'ms_view_member_editor_fields_edit', array( $this, 'filter_member_fields' ), 20 ); | |
} | |
public function change_gateway() { | |
check_ajax_referer( 'wpmudev_ms_change_sub_gw', 'security' ); | |
$return = array( | |
'success' => false, | |
); | |
$subscription_id = isset( $_POST['subscription_id'] ) ? (int) $_POST['subscription_id'] : null; | |
if( ! is_null( $subscription_id ) ){ | |
$subscription = MS_Factory::load( 'MS_Model_Relationship', $subscription_id ); | |
$subscription->gateway_id = 'manual'; | |
$subscription->save(); | |
$return = array( | |
'success' => true, | |
); | |
} | |
wp_send_json($return); | |
} | |
public function admin_footer() { | |
$screen = get_current_screen(); | |
if( ! isset( $screen->base ) || 'membership-2_page_membership2-add-member' != $screen->base ) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(function(d,$){ | |
$(d).ready(function(){ | |
$('.ms-change-sub-gateway').on('click', function(e){ | |
let me = $(this), | |
subscription_id = me.data('subscription-id'), | |
conf = confirm("Are you sure you want to change gateway to Manual?"); | |
e.preventDefault(); | |
if ( ! conf ) { | |
return; | |
} | |
let data = { | |
action: 'wpmudev_ms_change_sub_gw', | |
security: '<?php echo wp_create_nonce( "wpmudev_ms_change_sub_gw" ); ?>', | |
subscription_id: subscription_id | |
}; | |
$.post(ajaxurl, data, function(response) { | |
if( response.success ){ | |
location.reload(); | |
} | |
else{ | |
alert( 'Error in request' ); | |
} | |
}); | |
}); | |
}); | |
})(document,jQuery); | |
</script> | |
<?php | |
} | |
public function filter_member_fields( $fields ) { | |
foreach ( $fields as $field_key => $field_items ) { | |
foreach ( $field_items as $item_key => $field_item ) { | |
if ( ! isset( $field_item['value'] ) || ! is_array( $field_item['value'] ) || ! isset( $field_item['value'][0][1] ) ) { | |
continue; | |
} | |
$subscription_id = (int) $field_item['value'][0][1]; | |
foreach ( $field_item['value'] as $pair_key => $pair_value ) { | |
if ( 'Payment Gateway' == $pair_value[0] && 'None (Admin)' == $pair_value[1] ) { | |
$set_manual_btn = "<a class=\"ms-change-sub-gateway\" data-subscription-id=\"{$subscription_id}\">Set to manual</a>"; | |
$pair_value[1] .= " | " . $set_manual_btn; | |
$field_item['value'][$pair_key] = $pair_value; | |
$fields[$field_key][$item_key] = $field_item; | |
} | |
} | |
} | |
} | |
return $fields; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_ms_set_manual_gateway' ) ) { | |
function wpmudev_ms_set_manual_gateway() { | |
return WPMUDEV_MS_Set_Manual_Gateway::get_instance(); | |
} | |
add_action( 'plugins_loaded', 'wpmudev_ms_set_manual_gateway', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment