PluginProbe
WP Multisite User Sync/Unsync / 1.5.0
WP Multisite User Sync/Unsync v1.5.0
trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0
wp-multisite-user-sync / wp-multisite-user-sync.php

wp-multisite-user-sync.php in WP Multisite User Sync/Unsync 1.5.0, at wp-multisite-user-sync.php

245 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP Multisite User Sync/Unsync
4 Description: Sync/Unsync users from one site (blog) to the other sites (blogs) in your WordPress Multisite Network.
5 Version: 1.5.0
6 Author: Obtain Infotech
7 Author URI: http://www.obtaininfotech.com/
8 License: GPL2
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit( 'restricted access' );
13 }
14
15 if ( ! function_exists( 'wmus_admin_include_css_and_js' ) ) {
16 add_action( 'admin_enqueue_scripts', 'wmus_admin_include_css_and_js' );
17 function wmus_admin_include_css_and_js() {
18
19 /* admin script */
20 wp_register_script( 'wmus-script', plugin_dir_url( __FILE__ ) . 'assets/js/wmus-script.js', array( 'jquery' ) );
21 wp_enqueue_script( 'wmus-script' );
22 }
23 }
24
25 if ( ! function_exists( 'wmus_user_sync_unsync' ) ) {
26 add_action( 'show_user_profile', 'wmus_user_sync_unsync' );
27 add_action( 'edit_user_profile', 'wmus_user_sync_unsync' );
28 function wmus_user_sync_unsync() {
29
30 global $wpdb;
31
32 $current_user = wp_get_current_user();
33 if ( $current_user != null ) {
34 $current_user_role = reset( $current_user->roles );
35 }
36
37 $wmus_user_roles = get_site_option( 'wmus_user_roles' );
38 if ( ! $wmus_user_roles ) {
39 $wmus_user_roles = array();
40 }
41
42 if ( is_super_admin() || ( in_array( $current_user_role, $wmus_user_roles ) ) ) {
43 ?>
44 <h2><?php esc_html_e( 'WordPress Multisite User Sync/Unsync' ); ?></h2>
45 <table class="form-table">
46 <tbody>
47 <tr>
48 <th><label><?php esc_html_e( 'Sync/Unsync?' ); ?></label></th>
49 <td>
50 <fieldset>
51 <label>
52 <input type="radio" name="wmus_sync_unsync" value="<?php echo esc_attr( '1' ); ?>" checked="checked" /><?php esc_attr_e( 'Sync' ); ?>
53 </label>
54 <label>
55 <input type="radio" name="wmus_sync_unsync" value="<?php echo esc_attr( '0' ); ?>" /><?php esc_attr_e( 'Unsync' ); ?>
56 </label>
57 </fieldset>
58 <p class="description"><?php esc_html_e( 'Select sync/unsync.' ); ?></p>
59 </td>
60 </tr>
61 <tr>
62 <th><label><?php esc_html_e( 'Sites' ); ?></label></th>
63 <td>
64 <label><input class="wmus-check-uncheck" type="checkbox" /><?php esc_html_e( 'All' ); ?></label>
65 <p class="description"><?php esc_html_e( 'Select/Deselect all sites.' ); ?></p>
66 <br>
67 <fieldset class="wmus-sites">
68 <?php
69 $sites = $wpdb->get_results( "SELECT * FROM ".$wpdb->base_prefix."blogs" );
70 if ( $sites != null ) {
71 $user_id = intval( $_REQUEST['user_id'] );
72 foreach ( $sites as $key => $value ) {
73 $checked = '';
74 if ( is_user_member_of_blog( $user_id, $value->blog_id ) ) {
75 $checked = ' checked="checked"';
76 }
77
78 $blog_details = get_blog_details( $value->blog_id );
79 if ( ( $value->blog_id != get_current_blog_id() ) || ( is_network_admin() ) ) {
80 ?>
81 <label><input name="wmus_blogs[]" type="checkbox" value="<?php echo esc_attr( $value->blog_id ); ?>"<?php echo esc_attr( $checked ); ?>><?php echo esc_html( $value->domain ); echo esc_html( $value->path ); echo ' ('.esc_html( $blog_details->blogname ).')'; ?></label><br>
82 <?php
83 }
84 }
85 }
86 ?>
87 </fieldset>
88 <p class="description"><?php esc_html_e( 'Select destination sites you want to sync/unsync.' ); ?></p>
89 </td>
90 </tr>
91 </tbody>
92 </table>
93 <?php
94 }
95 }
96 }
97
98 if ( ! function_exists( 'wmus_user_sync_unsync_update' ) ) {
99 add_action( 'edit_user_profile_update', 'wmus_user_sync_unsync_update' );
100 add_action( 'profile_update', 'wmus_user_sync_unsync_update' );
101 function wmus_user_sync_unsync_update( $user ) {
102
103 $current_user = wp_get_current_user();
104 if ( $current_user != null ) {
105 $current_user_role = reset( $current_user->roles );
106 }
107
108 $wmus_user_roles = get_site_option( 'wmus_user_roles' );
109 if ( ! $wmus_user_roles ) {
110 $wmus_user_roles = array();
111 }
112
113 if ( is_super_admin() || ( in_array( $current_user_role, $wmus_user_roles ) ) ) {
114 $wmus_blogs = ( isset( $_POST['wmus_blogs'] ) ? $_POST['wmus_blogs'] : array() );
115 $wmus_sync_unsync = ( isset( $_POST['wmus_sync_unsync'] ) ? intval( $_POST['wmus_sync_unsync'] ) : 1 );
116 if ( $wmus_blogs != null ) {
117 $user_info = get_userdata( $user );
118 $user_id = $user;
119 $role = $user_info->roles;
120 if ( $role != null ) {
121 $role = reset( $user_info->roles );
122 } else {
123 $role = 'subscriber';
124 }
125
126 if ( isset( $_POST['role'] ) && $_POST['role'] != null ) {
127 $role = sanitize_text_field( $_POST['role'] );
128 }
129
130 $roles = get_editable_roles();
131 if ( array_key_exists( $role, $roles ) ) {
132 foreach ( $wmus_blogs as $wmus_blog ) {
133 $blog_id = (int) $wmus_blog;
134 if ( $wmus_sync_unsync ) {
135 add_user_to_blog( $blog_id, $user_id, $role );
136 } else {
137 remove_user_from_blog( $user_id, $blog_id );
138 }
139 }
140 }
141 }
142 }
143 }
144 }
145
146 if ( ! function_exists( 'wmus_add_network_admin_menu' ) ) {
147 add_action( 'network_admin_menu', 'wmus_add_network_admin_menu' );
148 function wmus_add_network_admin_menu() {
149
150 add_menu_page( 'WordPress Multisite User Sync/Unsync', 'User Sync', 'manage_options', 'wordpress-multisite-user-sync', 'wmus_settings', 'dashicons-update' );
151 }
152 }
153
154 if ( ! function_exists( 'wmus_settings' ) ) {
155 function wmus_settings() {
156
157 global $wpdb;
158
159 if ( isset( $_POST['wmus_submit'] ) ) {
160 if ( isset( $_POST['wmus_user_roles'] ) ) {
161 if ( is_array( $_POST['wmus_user_roles'] ) && $_POST['wmus_user_roles'] != null ) {
162 foreach ( $_POST['wmus_user_roles'] as $key => $value ) {
163 $_POST['wmus_user_roles'][$key] = sanitize_text_field( $value );
164 }
165
166 update_site_option( 'wmus_user_roles', $_POST['wmus_user_roles'] );
167 } else {
168 update_site_option( 'wmus_user_roles', (int) $_POST['wmus_user_roles'] );
169 }
170 }
171
172 ?>
173 <div class="notice notice-success is-dismissible">
174 <p><?php esc_html_e( 'Settings saved.' ); ?></p>
175 </div>
176 <?php
177 }
178
179 $wmus_user_roles = get_site_option( 'wmus_user_roles' );
180 ?>
181 <div class="wrap">
182 <h2><?php esc_html_e( 'Settings' ); ?></h2>
183 <hr>
184 <form method="post">
185 <table class="form-table">
186 <tbody>
187 <tr>
188 <th scope="row"><?php esc_html_e( 'User Roles' ); ?></th>
189 <td>
190 <fieldset>
191 <?php
192 $checked = '';
193 if ( ! $wmus_user_roles ) {
194 $checked = ' checked="checked"';
195 }
196 ?>
197 <label><input name="wmus_user_roles" type="checkbox" value="0"<?php echo esc_attr( $checked ); ?>><?php esc_attr_e( 'None' ); ?></label><br>
198 <?php
199 $roles = get_editable_roles();
200 if ( $roles != null ) {
201 foreach ( $roles as $key => $value ) {
202 if ( $key != 'subscriber' ) {
203 $checked = '';
204 if ( $wmus_user_roles && in_array( $key, $wmus_user_roles ) ) {
205 $checked = ' checked="checked"';
206 }
207 ?>
208 <label><input name="wmus_user_roles[]" type="checkbox" value="<?php echo esc_attr( $key ); ?>"<?php echo esc_attr( $checked ); ?>><?php echo esc_html( $value['name'] ); ?></label><br>
209 <?php
210 }
211 }
212 }
213 ?>
214 </fieldset>
215 </td>
216 </tr>
217 </tbody>
218 </table>
219 <script type="text/javascript">
220 jQuery( document ).ready( function( $ ) {
221 $( 'input[type="checkbox"]' ).on( 'change', function() {
222 if ( $( this ).val() != 0 ) {
223 var fieldset = $( this ).closest( 'fieldset' );
224 $( 'input[type="checkbox"]', fieldset ).each( function() {
225 if ( $( this ).val() == 0 ) {
226 $( this ).prop('checked', false);
227 }
228 });
229 } else {
230 var fieldset = $( this ).closest( 'fieldset' );
231 $( 'input[type="checkbox"]', fieldset ).each( function() {
232 if ( $( this ).val() != 0 ) {
233 $( this ).prop('checked', false);
234 }
235 });
236 }
237 });
238 });
239 </script>
240 <p class="submit"><input name="wmus_submit" class="button button-primary" value="<?php esc_attr_e( 'Save Changes' ); ?>" type="submit"></p>
241 </form>
242 </div>
243 <?php
244 }
245 }