Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active July 22, 2018 12:36
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 wpmudev-sls/945973f074eb97c3a015a6a68ef4f9b0 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/945973f074eb97c3a015a6a68ef4f9b0 to your computer and use it in GitHub Desktop.
This plugin implemented chunk syncing, so syncing not get blocked in low execution time, only implemented user delete module
.usc--status {}
.usc--area {
position: fixed;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
top: 0;
left: 0;
}
.usc--wrapper {
width: 400px;
background: #fff;
border-radius: 6px;
margin: 10% auto;
padding: 50px 35px;
box-shadow: 0px 3px 10px rgba(0, 0, 0, 0.18);
text-align: center;
}
.usc--slider {
margin: 35px 0px 15px;
background: #ddd;
border-radius: 15px;
text-align: center;
padding: 5px;
position: relative;
overflow: hidden;
}
.usc--slider-inner {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 0%;
background: #41d85b;
transition: width .35s ease;
}
.usc--heading {
margin-top: 0px;
font-size: 18px;
}
.usc--slider-percentage {
position: relative;
z-index: 2;
}
.usc--slider-status {
margin-bottom: 0px;
font-weight: 600;
}
(function() {
if ( ! wpus_ajax_settings.queues.length ) return;
var queue_percentage = 100 / wpus_ajax_settings.queues.length;
function request( index ) {
show_logs();
index = index || 0;
if ( ! wpus_ajax_settings.queues[index] ) return;
jQuery.ajax({
url: wpus_ajax_settings.ajax_url,
type: 'GET',
data: wpus_ajax_settings.queues[index],
success: function(data, textStatus, xhr) {
request( ++index );
increase_statistic( index * queue_percentage );
}
});
}
function show_logs() {
if ( !! jQuery('body').find('.usc--status').length ) return;
var markups = '<div class="usc--status">'+
'<div class="usc--area">'+
'<div class="usc--wrapper">'+
'<h3 class="usc--heading">Please Don\'t close this window!</h3>'+
'<div class="usc--slider">'+
'<div class="usc--slider-inner"></div>'+
'<div class="usc--slider-percentage">0%</div>'+
'</div>'+
'<p class="usc--slider-status">User Sync Syncing...</p>'+
'</div>'+
'</div>'+
'</div>';
jQuery('body').append(markups);
}
function increase_statistic( percentage ) {
jQuery('.usc--area .usc--slider-percentage').text( parseInt(percentage) + '%' );
jQuery('.usc--area .usc--slider-inner').css('width', percentage + '%' );
if ( percentage == 100 ) {
jQuery('.usc--area').remove();
}
}
request();
})();
<?php
/**
* Plugin Name: UserSync Custom Sync
* Plugin URI: https://premium.wpmudev.org/
* Description: This plugin implemented chunk syncing, so syncing not get blocked in low execution time, only implemented user delete module
* Author: Ariful Islam @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/itsarifulislam
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'UserSyncCustomSync' ) ) {
class UserSyncCustomSync {
private static $_instance = null;
public $user_sync = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new UserSyncCustomSync();
}
return self::$_instance;
}
private function __construct() {
global $user_sync;
$this->user_sync = $user_sync;
remove_action( 'delete_user', array( $this->user_sync, 'user_delete_data' ), 20 );
add_action( 'delete_user', array( $this, 'user_delete_data' ), 20, 1 );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'wp_ajax_delete_user_sync_users', array( $this, 'delete_user_sync_users' ) );
}
function plugins_loaded() {
}
function admin_enqueue_scripts() {
$queues = array();
$status = get_transient( 'user_sync_status' );
$action = get_transient( 'user_sync_action' );
$users = get_transient( 'user_sync_s_users' );
$sub_urls = $this->user_sync->options['sub_urls'];
if ( $status === false || $action === false || $sub_urls === false || $users === false ) return;
if ( ! empty( $users ) && ! empty( $sub_urls ) ) {
foreach ($users as $user) {
foreach ($sub_urls as $sub_url) {
array_push( $queues , array(
'site' => $sub_url,
'user' => $user,
'action' => $action
));
}
}
}
if ( empty( $queues ) ) return;
wp_enqueue_style( 'wp-user-sync-custom', plugins_url( '/user-sync-custom.css' , __FILE__ ) );
wp_enqueue_script( 'wp-user-sync-custom', plugins_url( '/user-sync-custom.js' , __FILE__ ), array( 'jquery' ) );
wp_localize_script( 'wp-user-sync-custom', 'wpus_ajax_settings', array(
'ajax_url' => admin_url('admin-ajax.php'),
'queues' => $queues
));
delete_transient( 'user_sync_action' );
delete_transient( 'user_sync_status' );
delete_transient( 'user_sync_s_users' );
}
function get_user_data( $user_id ) {
global $wpdb;
$data = get_userdata( $user_id );
if ( !empty( $data->data ) )
$user_data = (array) $data->data;
else
$user_data = (array) $data;
$user_meta = get_user_meta( $user_id );
$keys = array();
// replace empty array on empty string
foreach ( $user_meta as $key => $value ) {
$keys[] = $key;
}
foreach ( $keys as $key ) {
$user_meta[$key] = get_user_meta( $user_id, $key, true );
}
$prefix_fix = array('capabilities', 'user_level');
foreach ($prefix_fix as $key) {
if(isset($user_meta[$wpdb->get_blog_prefix() . $key])) {
$user_meta['wp_' . $key] = $user_meta[$wpdb->get_blog_prefix() . $key];
if($wpdb->get_blog_prefix() != 'wp_')
unset($user_meta[$wpdb->get_blog_prefix() . $key]);
}
}
return array_merge( $user_data, $user_meta );
}
function user_delete_data( $userID ) {
$user_data = $this->get_user_data( $userID );
$status = $this->user_sync->options['status'];
if ( "sub" == $status ) {
//Adding login of user to list of deleted users on Sub site
$deleted_users = (array) $this->user_sync->options['deleted_users'];
if ( false === array_search( $user_data['user_login'], $deleted_users ) ) {
$deleted_users[] = $user_data['user_login'];
$this->user_sync->set_options( 'deleted_users', $deleted_users );
}
} elseif ( "central" == $status ) {
$key = $this->user_sync->options['key'];
$sub_urls = $this->user_sync->options['sub_urls'];
// Return if no sub_urls
if ( false === $sub_urls ) return;
set_transient( 'user_sync_status', 'syncing' );
set_transient( 'user_sync_action', 'delete_user_sync_users' );
$users = get_transient( 'user_sync_s_users' );
if ( $users === false ) {
$users = array( $user_data['user_login'] );
} else {
array_push( $users, $user_data['user_login'] );
}
set_transient( 'user_sync_s_users', $users );
}
}
function delete_user_sync_users() {
$user_login = $_GET['user'];
$site = $_GET['site'];
$key = $this->user_sync->options['key'];
$sub_urls = $this->user_sync->options['sub_urls'];
// Checking key of security from Subsite
if ( $this->user_sync->check_key( $site['url'], $key ) ) {
$p = array( 'user_login' => $user_login, 'overwrite_user' => $site['param']['overwrite_user'] );
$p = base64_encode( serialize ( $p ) );
$hash = md5( $p . $key );
$this->user_sync->send_request( $site['url'], "user_sync_action=sync_user_delete&hash=". $hash . "&p=" . $p );
//Update last Sync date
$sub_urls = $this->user_sync->options['sub_urls'];
$array_id = $this->user_sync->get_index_by_url( $site['url'], $sub_urls );
$sub_urls[$array_id]['last_sync'] = date( "m.d.y G:i:s" );
$this->user_sync->set_options( 'sub_urls', $sub_urls );
}
wp_die();
}
}
function UserSyncCustomSyncRender() {
$GLOBALS['UserSyncCustomSync'] = UserSyncCustomSync::get_instance();
}
}
add_action( 'plugins_loaded', 'UserSyncCustomSyncRender' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment