Last active
April 16, 2018 08:22
-
-
Save wpmudev-sls/b27bf5b2215461cda4562c7a324e03b7 to your computer and use it in GitHub Desktop.
[Pro Sites] - Set Blogs Gateway
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: [Pro Sites] - Set Blogs Gateway | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Sets gateway for blogs | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_PS_Set_Blog_Gateway' ) ) { | |
class WPMUDEV_PS_Set_Blog_Gateway { | |
private static $_instance = null; | |
private $gateways = array( | |
'stripe' => 'Stripe', | |
'paypal' => 'PayPal', | |
'manual' => 'Manual', | |
'trial' => 'Trial', | |
'none' => 'None' | |
); | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_PS_Set_Blog_Gateway(); | |
} | |
return self::$_instance; | |
} | |
public function __construct() { | |
if( ! class_exists( 'ProSites' ) ) { | |
return; | |
} | |
add_action( 'admin_footer', array( $this, 'blog_gateway_opts' ) ); | |
add_action( 'wp_ajax_wpmudev-ps-set-gateway', array( $this, 'set_gateway_ajax' ) ); | |
add_action( 'wp_ajax_wpmudev-ps-set-stripe-info', array( $this, 'set_stripe_info_ajax' ) ); | |
} | |
public function set_stripe_info_ajax() { | |
check_ajax_referer( 'ps-set-gateway', 'nonce' ); | |
global $wpdb, $psts; | |
$blog_id = (int) $_POST[ 'blog_id' ]; | |
$customer_id = sanitize_text_field( $_POST[ 'customer_id' ] ); | |
$subscription_id = sanitize_text_field( $_POST[ 'subscription_id' ] ); | |
if ( empty( $blog_id ) ) { | |
$return = array( | |
'success' => false, | |
'message' => "No blog id was set" | |
); | |
wp_send_json($return); | |
} | |
if ( is_main_site( $blog_id ) ) { | |
$return = array( | |
'success' => false, | |
'message' => "Sorry, not allowed to change main site" | |
); | |
wp_send_json($return); | |
} | |
if ( is_main_site( $blog_id ) ) { | |
$return = array( | |
'success' => false, | |
'message' => "Sorry, not allowed to change main site" | |
); | |
wp_send_json($return); | |
} | |
$sql = "UPDATE {$wpdb->base_prefix}pro_sites_stripe_customers set customer_id='%s', subscription_id='%s' WHERE blog_id=%d"; | |
$sql = $wpdb->prepare( $sql, $customer_id, $subscription_id, $blog_id ); | |
if ( ! $wpdb->query( $sql ) ) { | |
$return = array( | |
'success' => false, | |
'message' => "Someting went wrong. Probably there is no record of this blog in pro_sites Stripe table." | |
); | |
} | |
else { | |
$return = array( | |
'success' => true, | |
'message' => "New Stripe info have been successfully set for blog {$blog_id}" | |
); | |
} | |
wp_send_json($return); | |
} | |
public function set_gateway_ajax() { | |
check_ajax_referer( 'ps-set-gateway', 'nonce' ); | |
global $wpdb, $psts; | |
$blog_id = (int) $_POST[ 'blog_id' ]; | |
$gateway = sanitize_text_field( $_POST[ 'gateway' ] ); | |
$return = array(); | |
if ( empty( $blog_id ) ) { | |
$return = array( | |
'success' => false, | |
'message' => "No blog id was set" | |
); | |
wp_send_json($return); | |
} | |
/* | |
if ( ! is_pro_site( $blog_id ) || ! is_pro_trial( $blog_id ) ) { | |
$return = array( | |
'success' => false, | |
'message' => "Blog id {$blog_id} is not a Pro Site" | |
); | |
wp_send_json($return); | |
} | |
*/ | |
if ( is_main_site( $blog_id ) ) { | |
$return = array( | |
'success' => false, | |
'message' => "Sorry, not allowed to change main site" | |
); | |
wp_send_json($return); | |
} | |
$sql = "UPDATE {$wpdb->base_prefix}pro_sites set gateway='%s' WHERE blog_id=%d"; | |
$sql = $wpdb->prepare( $sql, $gateway, $blog_id ); | |
if ( ! $wpdb->query( $sql ) ) { | |
$return = array( | |
'success' => false, | |
'message' => "Someting went wrong. Probably there is no record of this blog in pro_sites table." | |
); | |
} | |
else { | |
$return = array( | |
'success' => true, | |
'message' => "New gateway {$gateway} was successfully set for blog {$blog_id}" | |
); | |
} | |
wp_send_json($return); | |
} | |
public function blog_gateway_opts() { | |
$screen = get_current_screen(); | |
if ( 'toplevel_page_psts-network' != $screen->id || ! isset( $_GET[ 'bid' ] ) ) { | |
return; | |
} | |
global $psts; | |
$blog_id = (int) $_GET[ 'bid' ]; | |
if( is_main_site( $blog_id ) ){ | |
return; | |
} | |
$blog_gateway = ProSites_Helper_ProSite::get_site_gateway( $blog_id ); | |
if( '' == $blog_gateway ){ | |
$blog_gateway = 'none'; | |
} | |
$out = "<div id='ps-blog-gateways-list' style='display:none'>"; | |
$out .= "<label>"; | |
$out .= "<strong>Set/Change gateway</strong>"; | |
$out .= "</label> "; | |
$out .= "<select id='ps-new-gateway'>"; | |
foreach ( $this->gateways as $gateway_key => $gateway_title ) { | |
$selected = selected( $blog_gateway, $gateway_key ); | |
$out .= "<option value='{$gateway_key}' {$selected}>{$gateway_title}</option>"; | |
} | |
$out .= "</select>"; | |
$out .= " <a id='ps-set-blog-gateway' class='button button-primary'>Go</a>"; | |
if( 'stripe' == $blog_gateway ){ | |
$stripe_info = $this->get_stripe_info( $blog_id ); | |
$customer_id = isset( $stripe_info->customer_id ) ? $stripe_info->customer_id : ''; | |
$subscription_id = isset( $stripe_info->subscription_id ) ? $stripe_info->subscription_id : ''; | |
$out .= '<div style="margin-top: 30px;">'; | |
$out .= '<strong>Set/Change Stripe custom info</strong>'; | |
$out .= '<p>'; | |
$out .= '<label>Stripe customer id</label>'; | |
$out .= "<input type='text' id='ps-stripe-customer-id' value='{$customer_id}' />"; | |
$out .= '</p>'; | |
$out .= '<p>'; | |
$out .= '<label>Stripe subscription id</label>'; | |
$out .= "<input type='text' id='ps-stripe-subscription-id' value='{$subscription_id}' />"; | |
$out .= '</p>'; | |
$out .= " <a id='ps-set-blog-stripe-info' class='button button-primary'>Change Stripe info</a>"; | |
$out .= '</div>'; | |
} | |
$out .= wp_nonce_field( 'ps-set-gateway', 'ps-set-gateway' ); | |
$out .= "</div>"; | |
ob_start(); | |
?> | |
<script type="text/javascript"> | |
(function($){ | |
$(document).ready(function(){ | |
var gateways_list_wrap = $( '#ps-blog-gateways-list' ); | |
var gateway_selector = $( '#ps-set-blog-gateway' ); | |
var stripe_setter = $( '#ps-set-blog-stripe-info' ); | |
var conf_msg = "Are you sure you want to change the gateway ofthis blog?\n\nThis is usefull only when switching from one gateway to another. In any other case please do not use this" ; | |
var nonce = $( '#ps-set-gateway' ).val(); | |
gateways_list_wrap.prependTo( '.metabox-holder:first' ).show(300); | |
gateway_selector.on( 'click', function(){ | |
if( ! confirm( conf_msg) ){ | |
return; | |
} | |
var data = { | |
action : 'wpmudev-ps-set-gateway', | |
blog_id : '<?php echo $blog_id; ?>', | |
gateway : $( 'select#ps-new-gateway' ).val(), | |
nonce : nonce | |
}; | |
$.post( ajaxurl, data, function( response ) { | |
if( response.success ){ | |
let success_msg = $('<div>/', { | |
'class' : 'notice notice-success is-dismissible', | |
text : response.message | |
}); | |
gateway_selector.after( success_msg ); | |
location.reload(); | |
} | |
else{ | |
alert( response.message ); | |
} | |
}); | |
} ); | |
stripe_setter.on( 'click', function(){ | |
conf_msg = "Are you sure you want to change the Stripe info for this blog?\n\nThis is usefull only if you have created a custom plan directly from Stripe dashboard.\n\nIf you are not sure what you are doing please click \"Cancel\" to not continue"; | |
if( ! confirm( conf_msg) ){ | |
return; | |
} | |
var data = { | |
action : 'wpmudev-ps-set-stripe-info', | |
blog_id : '<?php echo $blog_id; ?>', | |
customer_id : $( '#ps-stripe-customer-id' ).val(), | |
subscription_id : $( '#ps-stripe-subscription-id' ).val(), | |
nonce : nonce | |
}; | |
$.post( ajaxurl, data, function( response ) { | |
if( response.success ){ | |
let success_msg = $('<div>/', { | |
'class' : 'notice notice-success is-dismissible', | |
text : response.message | |
}); | |
gateway_selector.after( success_msg ); | |
setTimeout(function() { | |
success_msg.hide(400) | |
}, 5000); | |
//location.reload(); | |
} | |
else{ | |
alert( response.message ); | |
} | |
}); | |
}); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
$out .= ob_get_clean(); | |
echo $out; | |
} | |
public function get_stripe_info( $blog_id = false ) { | |
global $wpdb; | |
if ( ! $blog_id ) { | |
return; | |
} | |
return $wpdb->get_row( | |
$wpdb->prepare( | |
"SELECT customer_id,subscription_id FROM {$wpdb->base_prefix}pro_sites_stripe_customers WHERE blog_id=%d" | |
, $blog_id | |
) | |
); | |
} | |
} | |
add_action( 'plugins_loaded', function() { | |
$GLOBALS['WPMUDEV_PS_Set_Blog_Gateway'] = WPMUDEV_PS_Set_Blog_Gateway::get_instance(); | |
}, 20 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment