Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Last active January 11, 2017 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/a2b2520890a08704b478924319aa3610 to your computer and use it in GitHub Desktop.
Save panoslyrakis/a2b2520890a08704b478924319aa3610 to your computer and use it in GitHub Desktop.
Activate or delete inactive signups on a WordPress Multisiet installation
<?php
/**
* Plugin Name: Activate Multisite Signups
* Plugin URI: https://premium.wpmudev.org/
* Description: Activate or delete inactive signups on a WordPress Multisite installation
* Version: 1.0.0
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/panoskatws
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wpmudev-edit-signups
*/
/*
* Quick instructions
* 1. Install as a mu-plugin or normal plugin
* 2. Activate or delete signups from Network > Sites > Inactive Sites
*/
if( !class_exists( 'WPMUDEV_Activate_Signups' ) ){
class WPMUDEV_Activate_Signups{
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_Activate_Signups();
}
return self::$_instance;
}
private function __construct() {
add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ) );
add_action( 'admin_footer', array( $this, 'scripts' ) );
add_action( 'wp_ajax_wpmudev_signups_action', array( $this, 'modify_signup' ) );
}
public function network_admin_menu() {
add_submenu_page( 'sites.php', esc_html__( 'Inactive Sites', 'wpmudev-edit-signups' ), esc_html__( 'Inactive Sites', 'wpmudev-edit-signups' ), 'list_signups', 'inactive-signups', array( $this, 'list_signups_page' ) );
}
public function list_signups_page(){
//TODO:
// Add filter if users or sites (signup contains domain or not)
if ( ! is_super_admin() ) {
wp_die( esc_html__( 'You do not have permission to access this page.', 'wpmudev-edit-signups' ) );
}
?>
<div class="wrap">
<h1><?php esc_html_e( 'Inactive Sites', 'wpmudev-edit-signups' ); ?></h1>
<?php $this->list_signups_display( true ); ?>
</div>
<?php
}
public function list_signups_display( $echo = false ){
$signups = $this->get_signups();
ob_start();
?>
<table class="widefat">
<thead>
<tr>
<th><?php _e( 'ID', 'wpmudev-edit-signups' ); ?></th>
<th><?php _e( 'Site title', 'wpmudev-edit-signups' ); ?></th>
<th><?php _e( 'Domain', 'wpmudev-edit-signups' ); ?></th>
<th><?php _e( 'Path', 'wpmudev-edit-signups' ); ?></th>
<th><?php _e( 'Username', 'wpmudev-edit-signups' ); ?></th>
<th><?php _e( 'Email', 'wpmudev-edit-signups' ); ?></th>
<th><?php _e( 'Action', 'wpmudev-edit-signups' ); ?></th>
</tr>
</thead>
<tbody>
<?php if( $signups ): ?>
<?php foreach( $signups as $signup ){ ?>
<tr>
<td><?php echo $signup->signup_id; ?></td>
<td><?php echo $signup->title; ?></td>
<td><?php echo $signup->domain; ?></td>
<td><?php echo $signup->path; ?></td>
<td><?php echo $signup->user_login; ?></td>
<td><?php echo $signup->user_email; ?></td>
<td>
<div class="signup-actions">
<a class="button signup-action activate-signup" data-action='activate' data-signup-key="<?php echo $signup->activation_key; ?>" title="<?php _e( 'Activate site', 'wpmudev-edit-signups' ); ?>"><i class="dashicons dashicons-yes"></i></a>
<a class="button signup-action delete-signup" data-action='delete' data-signup-key="<?php echo $signup->activation_key; ?>" title="<?php _e( 'Delete site', 'wpmudev-edit-signups' ); ?>"><i class="dashicons dashicons-no"></i></a>
</div>
</td>
</tr>
<?php } ?>
<?php else: ?>
<tr>
<td colspan="7">
<center><?php _e( 'There are no inactive sites', 'wpmudev-edit-signups' ); ?></center>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<?php
$out = ob_get_clean();
if( ! $echo ){
return $out;
}
echo $out;
}
public function scripts(){
?>
<script type="text/javascript">
(function($){
$( document ).ready(function(){
var wpmudev_signups_nonce = '<?php echo wp_create_nonce("wpmudev_signups_nonce"); ?>';
$( '.signup-actions .signup-action' ).on('click', function(e){
e.preventDefault();
var key = $(this).data( 'signup-key' );
var action = $(this).data( 'action' );
if( action == 'delete' ){
var conf = confirm( 'Are you sure you want to delete this Site?' );
if( ! conf ) return;
}
var ajax_data = {
action: 'wpmudev_signups_action',
nonce: wpmudev_signups_nonce,
key: key,
signup_action: action
};
$.ajax({
type: "POST",
url: ajaxurl,
data: ajax_data,
dataType: "json",
success: function(resp){
if( resp.data.message == "_SUCCESS_" ) location.reload();
},
failure: function(errMsg) {
console.log(errMsg);
}
});
});
});
})(jQuery);
</script>
<?php
}
public function modify_signup(){
check_ajax_referer( 'wpmudev_signups_nonce', 'nonce' );
global $wpdb;
$key = filter_input( INPUT_POST, 'key', FILTER_DEFAULT );
$signup_action = filter_input( INPUT_POST, 'signup_action', FILTER_DEFAULT );
if( $signup_action == 'activate' ){
$resp = wpmu_activate_signup( $key );
}
else{
$wpdb->delete(
$wpdb->signups,
array( 'activation_key' => $key, ),
array( '%s', )
);
}
$resp = array(
'message' => '_SUCCESS_',
'action' => $signup_action
);
wp_send_json_success( $resp );
exit;
}
public function get_signups(){
global $wpdb;
$signups = $wpdb->get_results( "SELECT * FROM {$wpdb->signups} WHERE active=0 " );
return ! empty( $signups ) ? $signups : false;
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_Activate_Signups'] = WPMUDEV_Activate_Signups::get_instance();
}, 10 );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment