Changeset 2328396
- Timestamp:
- 06/22/2020 04:45:32 AM (6 years ago)
- Location:
- whos-logged-in
- Files:
-
- 4 added
- 1 deleted
- 4 edited
- 1 copied
-
tags/1.3 (copied) (copied from whos-logged-in/trunk)
-
tags/1.3/js/wli-logout-script.js (added)
-
tags/1.3/js/wli-update-user-list-script.js (added)
-
tags/1.3/readme.txt (modified) (3 diffs)
-
tags/1.3/whos-logged-in.php (modified) (11 diffs)
-
trunk/js/wli-logout-script.js (added)
-
trunk/js/wli-script.js (deleted)
-
trunk/js/wli-update-user-list-script.js (added)
-
trunk/readme.txt (modified) (3 diffs)
-
trunk/whos-logged-in.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
whos-logged-in/tags/1.3/readme.txt
r2324058 r2328396 6 6 Tested up to: 5.4.2 7 7 Requires PHP: 5.6 8 Stable tag: 1. 28 Stable tag: 1.3 9 9 License: GPLv3 10 10 License URI: https://www.gnu.org/licenses/gpl-3.0.html 11 11 12 The Who's Logged In Plugin provides a simple meta box on your dashboard home page that shows a list of users as they log in and log out12 The Who's Logged In Plugin provides a simple meta box on your dashboard home page that shows a list of users that are logged in 13 13 14 14 == Description == 15 15 16 Install and activate the Who's Logged In plugin, and it will start collecting a list of users logged into your website. When a user logs in they are added to the list, and when a user logs out they are removed from the list: and it's automatic! The list of logged in users is displayed to administrator users only on their dashboard homepage, in a simple metabox. The list displayed is automatically updated every 15 seconds to ensure you know who's logged on to your website asap. 17 18 == New Feature! == 19 * Added a settings page that, for now, has a single option to turn the automatic logout of inactive users functionality on and off. 20 21 Current development is underway for a handful of other new settings page options, and I would love to hear any and all suggestions for new features you would like to see in the Who's Logged In plugin. Please visit the support forum to leave your ideas in the thread titled "New Feature Requests Thread" 16 Install and activate the Who's Logged In plugin, and you can view a list of currently logged in users on your Dashboard Home page. 22 17 23 18 == Installation == … … 44 39 == Changelog == 45 40 41 = 1.3 = 42 * Fixed a bug where scripts weren't loading properly in all cases 43 * Removed an extra option to improve performance 44 46 45 = 1.2 = 47 46 * Added a requested settings page that, for now, has one setting for turning the automatic logout of inactive users functionality that was added in version 1.1 on and off. Also changed the automatic logout timer to be 20 minutes instead of 15 minutes to make it a little more friendly. … … 52 51 == Upgrade Notice == 53 52 54 = 1.2 = 55 * Adds new settings page with an option for turning the automatic logging out of inactive users functionality on and off. 53 = 1.3 = 54 * Fixed a bug where scripts weren't loading properly in all cases 55 * Removed an extra option to improve performance -
whos-logged-in/tags/1.3/whos-logged-in.php
r2324049 r2328396 3 3 /* 4 4 Plugin Name: Who's Logged In 5 Description: Adds a metabox in all Administrator users Dashboard page that shows a list of users that updates regularly as they log in and log out6 Version: 1. 25 Description: Adds a metabox on the Dashboard Home page that displays a list of all users that are currently logged in for Administrators 6 Version: 1.3 7 7 Author: Ben HartLenn 8 8 Author URI: bhartlenn@gmail.com … … 11 11 License URI: https://www.gnu.org/licenses/gpl-3.0.html 12 12 13 The Who's Logged In plugin shows you a list of logged in users on yourdashboard home page, and it updates the list every 15 seconds!13 The Who's Logged In plugin shows Administrators a list of currently logged in users on the dashboard home page, and it updates the list every 15 seconds! 14 14 Copyright (C) 2020 Ben HartLenn 15 15 … … 27 27 28 28 // Plugin helper function(s) 29 // Display the list of users stored in plugin option29 // Function to get and display a list of currently logged in users 30 30 function wli_output_users() { 31 // get list of users from option 32 $wli_logged_in_users = get_option( 'wli_logged_in_users' ); 31 // NOTE: version 1.3 update is no longer using option to display logged in users. 32 // now making one query to db for all users with an active session token, which are logged in users, and displaying the results of that 33 33 34 // get list of users from wordpress database that have an active session token 34 $wli_ token_user_objects = get_users([35 $wli_logged_in_user_objects = get_users([ 35 36 'meta_key' => 'session_tokens', 36 37 'meta_compare' => 'EXISTS', 37 'fields' => [' user_login']38 'fields' => ['id', 'user_login'] 38 39 ]); 39 // create array from array of WordPress User objects 40 $wli_token_users_array = wp_list_pluck($wli_token_user_objects, 'user_login'); 41 42 // loop through the array of users stored in the plugin option, and remove them if they do not have an active session token in database 43 foreach( $wli_logged_in_users as $wli_user_id => $wli_user_login ) { 44 if( !in_array( $wli_user_login, $wli_token_users_array ) ) { 45 unset( $wli_logged_in_users[$wli_user_id] ); 46 } 47 } 40 // create array of logged in user info from array of WordPress user objects 41 $wli_logged_in_users_array = wp_list_pluck($wli_logged_in_user_objects, 'user_login', 'id'); 48 42 49 43 // initialize output variable 50 44 $wli_logged_in_users_output = ""; 51 45 52 // build user list output for display 53 if ( !empty( $wli_logged_in_users ) ) { 54 $wli_logged_in_users_output .= "<ul id='wli-logged-in-users'>"; 55 foreach ( $wli_logged_in_users as $wli_user_id => $wli_user_login ) { 56 $wli_logged_in_users_output .= "<li><a href='" . get_edit_user_link( $wli_user_id ) . "'>" . __( $wli_user_login, 'whos-logged-in' ) . "</a></li>"; 57 } 58 $wli_logged_in_users_output .= "</ul>"; 46 // if users array is not empty... 47 if ( !empty( $wli_logged_in_users_array ) ) { 48 // ... start unordered list 49 $wli_logged_in_users_output .= "<ul id='wli-logged-in-users'>"; 50 // // add an li and a link for each user in the array 51 foreach ( $wli_logged_in_users_array as $wli_user_id => $wli_user_login ) { 52 $wli_logged_in_users_output .= "<li><a href='" . get_edit_user_link( $wli_user_id ) . "'>" . __( $wli_user_login, 'whos-logged-in' ) . "</a></li>"; 53 } 54 // ... end unordered list 55 $wli_logged_in_users_output .= "</ul>"; 59 56 } 60 57 else { 61 58 $wli_logged_in_users_output .= "No users to display."; 62 59 } 63 60 // return the output, do not echo 64 61 return $wli_logged_in_users_output; 65 66 update_option( 'wli_logged_in_users', $wli_logged_in_users, '', 'no' );67 62 } 68 63 // ***** End plugin helper functions section ***** 69 64 70 // ***** Plugin Functionality Section ***** 71 65 // ***** Main Plugin Functionality Section ***** 72 66 // Setup a settings page for Who's Logged In available at Dashboard >> Settings >> Whos Logged In 73 67 add_action( 'admin_menu', 'wli_add_settings_page' ); … … 87 81 function wli_settings_init() { 88 82 // register plugin setting that will have an array of option-like key:value pairs stored in the one option 89 register_setting(90 'wli_settings',91 'wli_settings_general',92 [93 'type' => 'array'94 ]95 );96 97 // add plugin settings section98 add_settings_section(99 'wli_settings_page_general_section',100 __( 'General Settings', 'whos_logged_in' ),101 'wli_settings_general_section_callback',102 'whos-logged-in'103 );104 105 // add checkbox field for turning auto kick users functionality on and off106 add_settings_field(107 'wli_auto_kick_inactive_users',108 __( 'Auto logout inactive users', 'whos_logged_in' ),109 'wli_auto_kick_users_field_render',110 'whos-logged-in',111 'wli_settings_page_general_section'112 );83 register_setting( 84 'wli_settings', 85 'wli_settings_general', 86 [ 87 'type' => 'array' 88 ] 89 ); 90 91 // add plugin settings section 92 add_settings_section( 93 'wli_settings_page_general_section', 94 __( 'General Settings', 'whos_logged_in' ), 95 'wli_settings_general_section_callback', 96 'whos-logged-in' 97 ); 98 99 // add checkbox field for turning auto kick users functionality on and off 100 add_settings_field( 101 'wli_auto_kick_inactive_users', 102 __( 'Auto logout inactive users', 'whos_logged_in' ), 103 'wli_auto_kick_users_field_render', 104 'whos-logged-in', 105 'wli_settings_page_general_section' 106 ); 113 107 } 114 108 115 109 // settings section intro text 116 110 function wli_settings_general_section_callback() { 111 echo '<i>Turning on this option will make the plugin automatically logout <b>ANY</b> users that have left your site, and have not returned for 20 minutes.</i>'; 117 112 } 118 113 119 114 // render checkbox field for kicking users automatically 120 115 function wli_auto_kick_users_field_render() { 121 122 $general_settings = get_option( 'wli_settings_general' ); 123 $value = $general_settings['wli_auto_kick_inactive_users']; 124 $checked = checked( $value, 1, false ); 125 ?> 126 <input id="wli_auto_kick_inactive_users" type="checkbox" name="wli_settings_general[wli_auto_kick_inactive_users]" value="1" <?= $checked; ?> ><label for="wli_auto_kick_inactive_users">Turning on this option will make the plugin automatically logout <b>ANY</b> inactive users after 20 minutes of continous inactivity on your website.</label> 127 <?php 116 $general_settings = get_option( 'wli_settings_general' ); 117 $value = $general_settings['wli_auto_kick_inactive_users']; 118 $checked = checked( $value, 1, false ); 119 ?> 120 <input id="wli_auto_kick_inactive_users" type="checkbox" name="wli_settings_general[wli_auto_kick_inactive_users]" value="1" <?= $checked; ?> > 121 <?php 128 122 } 129 123 … … 131 125 // render the settings page html/php 132 126 function wli_render_plugin_settings_page() { 133 // check user capabilities127 // check user has capability to manage options before rendering settings page 134 128 if ( !current_user_can( 'manage_options' ) ) { 135 129 return; … … 158 152 } 159 153 160 161 154 // when plugin is activated... 162 155 register_activation_hook( __FILE__, 'wli_plugin_activation' ); 163 156 function wli_plugin_activation() { 164 // get current user and add to logged in users list on plugin activation 165 $wli_current_user = wp_get_current_user(); 166 update_option( 'wli_logged_in_users', [ $wli_current_user->ID => $wli_current_user->user_login ], '', 'no' ); 167 // default to not automatically kicking users off when plugin is activated 168 update_option( 'wli_settings_general', ['wli_auto_kick_inactive_users' => 0] ); 157 // ...default to *NOT* automatically kicking users off the website when plugin is activated 158 update_option( 'wli_settings_general', ['wli_auto_kick_inactive_users' => 0] ); 169 159 } 170 160 … … 172 162 register_deactivation_hook( __FILE__, 'wli_plugin_deactivation' ); 173 163 function wli_plugin_deactivation() { 174 delete_option( 'wli_logged_in_users' ); 175 delete_option( 'wli_settings_general' ); 176 } 177 178 // output metabox content which is our list of logged in users stored in our option 179 function wli_metabox_ouput() { 180 181 echo "<div id='wli-logged-in-users-wrapper'>"; 182 echo wli_output_users(); 183 echo "</div>"; 164 delete_option( 'wli_logged_in_users' ); // delete old unused option from version 1.2 and older 165 delete_option( 'wli_settings_general' ); 184 166 } 185 167 … … 187 169 add_action( 'wp_dashboard_setup', 'wli_add_metabox' ); 188 170 function wli_add_metabox() { 189 if ( current_user_can( 'administrator' ) ) { 190 add_meta_box( 'wli-metabox', __( 'Who\'s Logged In', 'whos-logged-in' ), 'wli_metabox_ouput', 'dashboard', 'side', 'high' ); 191 } 192 } 193 194 // when a user logs in, add them to our list of logged in users 195 add_action( 'wp_login', 'wli_add_user', 10, 2 ); 196 function wli_add_user( $user_login, $user ) { 197 $wli_logged_in_users = get_option( 'wli_logged_in_users' ); 198 if ( !in_array( $user->user_login, $wli_logged_in_users ) ) { 199 $wli_logged_in_users[ $user->ID ] = $user->user_login; 200 update_option( 'wli_logged_in_users', $wli_logged_in_users, '', 'no' ); 201 } 202 } 203 204 // when a user logs out, remove them from our list of logged in users 205 add_action( 'wp_logout', 'wli_remove_user' ); 206 function wli_remove_user() { 207 $wli_current_user = wp_get_current_user(); 208 $wli_logged_in_users = get_option( 'wli_logged_in_users' ); 209 if ( in_array( $wli_current_user->user_login, $wli_logged_in_users ) ) { 210 unset( $wli_logged_in_users[ $wli_current_user->ID ] ); 211 update_option( 'wli_logged_in_users', $wli_logged_in_users, '', 'no' ); 212 } 213 } 214 215 // check if on dashboard home page, and that user is an administrator then load scripts 216 add_action( 'admin_enqueue_scripts', 'wli_remove_meta_boxes' ); 217 function wli_remove_meta_boxes( $hook ) { 218 if ( 'index.php' != $hook && !current_user_can( 'administrator' ) ) { 219 return; 220 } 221 222 // add script that runs ajax call at 5 second intervals to keep list of logged in users updated 223 wp_enqueue_script( 'wli-script', plugin_dir_url( __FILE__ ) . 'js/wli-script.js', [ 'jquery' ] ); 224 225 // add js variables for our ajax call url, and a nonce variable for security 226 wp_localize_script( 'wli-script', 'wli_js_vars', [ 171 if ( current_user_can( 'administrator' ) ) { 172 add_meta_box( 'wli-metabox', __( 'Who\'s Logged In', 'whos-logged-in' ), 'wli_metabox_ouput', 'dashboard', 'side', 'high' ); 173 } 174 } 175 // output metabox content using helper function for getting and displaying logged in user list 176 function wli_metabox_ouput() { 177 echo "<div id='wli-logged-in-users-wrapper'>"; 178 echo wli_output_users(); 179 echo "</div>"; 180 } 181 182 // Who's Logged In javascript loading 183 add_action( 'wp_enqueue_scripts', 'wli_load_scripts' ); 184 add_action( 'admin_enqueue_scripts', 'wli_load_scripts' ); 185 function wli_load_scripts( $hook ) { 186 187 // only load script that updates the metabox list of users on the dashboard homepage where the Whos Logged In metabox is displayed 188 if( $hook == 'index.php' ) { 189 // add script that runs ajax call at 15 second intervals to keep list of logged in users updated in the metabox 190 wp_enqueue_script( 'wli-update-user-list-script', plugin_dir_url( __FILE__ ) . 'js/wli-update-user-list-script.js', [ 'jquery' ] ); 191 192 // add js variables for our ajax call url, and a nonce variable for security 193 wp_localize_script( 'wli-update-user-list-script', 'wli_user_list_js_vars', [ 227 194 'ajax_url' => admin_url( 'admin-ajax.php' ), // make sure proper url is used for WordPress Ajax requests 228 195 'secret_nonce' => wp_create_nonce( 'wli_secret_nonce_sauce' ), // Create nonce to secure ajax requests 229 ] 230 ); 231 } 196 ]); 197 } 198 199 // only load script for autologout of inactive users if the setting is turned on and saved 200 // get autologout users setting 201 $general_settings = get_option( 'wli_settings_general' ); 202 $autologout_setting = $general_settings['wli_auto_kick_inactive_users']; 203 // check if autologout setting is turned on, then proceed to... 204 if($autologout_setting == 1) { 205 // add script that handles autologout functionality 206 wp_enqueue_script( 'wli-logout-script', plugin_dir_url( __FILE__ ) . 'js/wli-logout-script.js', [ 'jquery' ] ); 207 208 // add js variables for our ajax call url, and a nonce variable for security 209 wp_localize_script( 'wli-logout-script', 'wli_logout_js_vars', [ 210 'ajax_url' => admin_url( 'admin-ajax.php' ), // make sure proper url is used for WordPress Ajax requests 211 'secret_nonce' => wp_create_nonce( 'wli_secret_nonce_sauce' ), // Create nonce to secure ajax requests 212 ]); 213 } 214 } 215 232 216 233 217 // action called by ajax that updates list of logged in users shown on dashboard … … 238 222 die( 'Permission denied.' ); 239 223 240 // jQuery puts output response into div#wli-logged-in-users-wrapper 224 // jQuery puts output response into div#wli-logged-in-users-wrapper when ajax successful 241 225 echo wli_output_users(); 242 226 … … 244 228 } 245 229 246 // action called by ajax to log out user230 // action called by ajax to automatically log out inactive users 247 231 add_action( 'wp_ajax_wli_ajax_logout_users', 'wli_ajax_logout_users' ); 248 232 function wli_ajax_logout_users() { 249 // check if auto kickusers setting is on or off233 // check if autologout users setting is on or off 250 234 $general_settings = get_option( 'wli_settings_general' ); 251 $ value= $general_settings['wli_auto_kick_inactive_users'];252 // if auto kickis turned on, then proceed to...253 if($ value== 1) {235 $autologout_setting = $general_settings['wli_auto_kick_inactive_users']; 236 // if autologout is turned on, then proceed to... 237 if($autologout_setting == 1) { 254 238 // ...get the current user that triggered this action 255 239 $current_user = wp_get_current_user(); … … 258 242 $sessions = WP_Session_Tokens::get_instance($current_user->ID); 259 243 260 // ...destroy their sessions, and all of them!244 // ...destroy all of their login sessions to log them out 261 245 $sessions->destroy_all(); 262 246 } 263 // or else send response to let javascript know not to refresh page on ajax success247 // or else send response to let javascript know not to refresh the page on ajax success 264 248 else { 265 249 echo "auto-kick-is-turned-off"; -
whos-logged-in/trunk/readme.txt
r2324058 r2328396 6 6 Tested up to: 5.4.2 7 7 Requires PHP: 5.6 8 Stable tag: 1. 28 Stable tag: 1.3 9 9 License: GPLv3 10 10 License URI: https://www.gnu.org/licenses/gpl-3.0.html 11 11 12 The Who's Logged In Plugin provides a simple meta box on your dashboard home page that shows a list of users as they log in and log out12 The Who's Logged In Plugin provides a simple meta box on your dashboard home page that shows a list of users that are logged in 13 13 14 14 == Description == 15 15 16 Install and activate the Who's Logged In plugin, and it will start collecting a list of users logged into your website. When a user logs in they are added to the list, and when a user logs out they are removed from the list: and it's automatic! The list of logged in users is displayed to administrator users only on their dashboard homepage, in a simple metabox. The list displayed is automatically updated every 15 seconds to ensure you know who's logged on to your website asap. 17 18 == New Feature! == 19 * Added a settings page that, for now, has a single option to turn the automatic logout of inactive users functionality on and off. 20 21 Current development is underway for a handful of other new settings page options, and I would love to hear any and all suggestions for new features you would like to see in the Who's Logged In plugin. Please visit the support forum to leave your ideas in the thread titled "New Feature Requests Thread" 16 Install and activate the Who's Logged In plugin, and you can view a list of currently logged in users on your Dashboard Home page. 22 17 23 18 == Installation == … … 44 39 == Changelog == 45 40 41 = 1.3 = 42 * Fixed a bug where scripts weren't loading properly in all cases 43 * Removed an extra option to improve performance 44 46 45 = 1.2 = 47 46 * Added a requested settings page that, for now, has one setting for turning the automatic logout of inactive users functionality that was added in version 1.1 on and off. Also changed the automatic logout timer to be 20 minutes instead of 15 minutes to make it a little more friendly. … … 52 51 == Upgrade Notice == 53 52 54 = 1.2 = 55 * Adds new settings page with an option for turning the automatic logging out of inactive users functionality on and off. 53 = 1.3 = 54 * Fixed a bug where scripts weren't loading properly in all cases 55 * Removed an extra option to improve performance -
whos-logged-in/trunk/whos-logged-in.php
r2324049 r2328396 3 3 /* 4 4 Plugin Name: Who's Logged In 5 Description: Adds a metabox in all Administrator users Dashboard page that shows a list of users that updates regularly as they log in and log out6 Version: 1. 25 Description: Adds a metabox on the Dashboard Home page that displays a list of all users that are currently logged in for Administrators 6 Version: 1.3 7 7 Author: Ben HartLenn 8 8 Author URI: bhartlenn@gmail.com … … 11 11 License URI: https://www.gnu.org/licenses/gpl-3.0.html 12 12 13 The Who's Logged In plugin shows you a list of logged in users on yourdashboard home page, and it updates the list every 15 seconds!13 The Who's Logged In plugin shows Administrators a list of currently logged in users on the dashboard home page, and it updates the list every 15 seconds! 14 14 Copyright (C) 2020 Ben HartLenn 15 15 … … 27 27 28 28 // Plugin helper function(s) 29 // Display the list of users stored in plugin option29 // Function to get and display a list of currently logged in users 30 30 function wli_output_users() { 31 // get list of users from option 32 $wli_logged_in_users = get_option( 'wli_logged_in_users' ); 31 // NOTE: version 1.3 update is no longer using option to display logged in users. 32 // now making one query to db for all users with an active session token, which are logged in users, and displaying the results of that 33 33 34 // get list of users from wordpress database that have an active session token 34 $wli_ token_user_objects = get_users([35 $wli_logged_in_user_objects = get_users([ 35 36 'meta_key' => 'session_tokens', 36 37 'meta_compare' => 'EXISTS', 37 'fields' => [' user_login']38 'fields' => ['id', 'user_login'] 38 39 ]); 39 // create array from array of WordPress User objects 40 $wli_token_users_array = wp_list_pluck($wli_token_user_objects, 'user_login'); 41 42 // loop through the array of users stored in the plugin option, and remove them if they do not have an active session token in database 43 foreach( $wli_logged_in_users as $wli_user_id => $wli_user_login ) { 44 if( !in_array( $wli_user_login, $wli_token_users_array ) ) { 45 unset( $wli_logged_in_users[$wli_user_id] ); 46 } 47 } 40 // create array of logged in user info from array of WordPress user objects 41 $wli_logged_in_users_array = wp_list_pluck($wli_logged_in_user_objects, 'user_login', 'id'); 48 42 49 43 // initialize output variable 50 44 $wli_logged_in_users_output = ""; 51 45 52 // build user list output for display 53 if ( !empty( $wli_logged_in_users ) ) { 54 $wli_logged_in_users_output .= "<ul id='wli-logged-in-users'>"; 55 foreach ( $wli_logged_in_users as $wli_user_id => $wli_user_login ) { 56 $wli_logged_in_users_output .= "<li><a href='" . get_edit_user_link( $wli_user_id ) . "'>" . __( $wli_user_login, 'whos-logged-in' ) . "</a></li>"; 57 } 58 $wli_logged_in_users_output .= "</ul>"; 46 // if users array is not empty... 47 if ( !empty( $wli_logged_in_users_array ) ) { 48 // ... start unordered list 49 $wli_logged_in_users_output .= "<ul id='wli-logged-in-users'>"; 50 // // add an li and a link for each user in the array 51 foreach ( $wli_logged_in_users_array as $wli_user_id => $wli_user_login ) { 52 $wli_logged_in_users_output .= "<li><a href='" . get_edit_user_link( $wli_user_id ) . "'>" . __( $wli_user_login, 'whos-logged-in' ) . "</a></li>"; 53 } 54 // ... end unordered list 55 $wli_logged_in_users_output .= "</ul>"; 59 56 } 60 57 else { 61 58 $wli_logged_in_users_output .= "No users to display."; 62 59 } 63 60 // return the output, do not echo 64 61 return $wli_logged_in_users_output; 65 66 update_option( 'wli_logged_in_users', $wli_logged_in_users, '', 'no' );67 62 } 68 63 // ***** End plugin helper functions section ***** 69 64 70 // ***** Plugin Functionality Section ***** 71 65 // ***** Main Plugin Functionality Section ***** 72 66 // Setup a settings page for Who's Logged In available at Dashboard >> Settings >> Whos Logged In 73 67 add_action( 'admin_menu', 'wli_add_settings_page' ); … … 87 81 function wli_settings_init() { 88 82 // register plugin setting that will have an array of option-like key:value pairs stored in the one option 89 register_setting(90 'wli_settings',91 'wli_settings_general',92 [93 'type' => 'array'94 ]95 );96 97 // add plugin settings section98 add_settings_section(99 'wli_settings_page_general_section',100 __( 'General Settings', 'whos_logged_in' ),101 'wli_settings_general_section_callback',102 'whos-logged-in'103 );104 105 // add checkbox field for turning auto kick users functionality on and off106 add_settings_field(107 'wli_auto_kick_inactive_users',108 __( 'Auto logout inactive users', 'whos_logged_in' ),109 'wli_auto_kick_users_field_render',110 'whos-logged-in',111 'wli_settings_page_general_section'112 );83 register_setting( 84 'wli_settings', 85 'wli_settings_general', 86 [ 87 'type' => 'array' 88 ] 89 ); 90 91 // add plugin settings section 92 add_settings_section( 93 'wli_settings_page_general_section', 94 __( 'General Settings', 'whos_logged_in' ), 95 'wli_settings_general_section_callback', 96 'whos-logged-in' 97 ); 98 99 // add checkbox field for turning auto kick users functionality on and off 100 add_settings_field( 101 'wli_auto_kick_inactive_users', 102 __( 'Auto logout inactive users', 'whos_logged_in' ), 103 'wli_auto_kick_users_field_render', 104 'whos-logged-in', 105 'wli_settings_page_general_section' 106 ); 113 107 } 114 108 115 109 // settings section intro text 116 110 function wli_settings_general_section_callback() { 111 echo '<i>Turning on this option will make the plugin automatically logout <b>ANY</b> users that have left your site, and have not returned for 20 minutes.</i>'; 117 112 } 118 113 119 114 // render checkbox field for kicking users automatically 120 115 function wli_auto_kick_users_field_render() { 121 122 $general_settings = get_option( 'wli_settings_general' ); 123 $value = $general_settings['wli_auto_kick_inactive_users']; 124 $checked = checked( $value, 1, false ); 125 ?> 126 <input id="wli_auto_kick_inactive_users" type="checkbox" name="wli_settings_general[wli_auto_kick_inactive_users]" value="1" <?= $checked; ?> ><label for="wli_auto_kick_inactive_users">Turning on this option will make the plugin automatically logout <b>ANY</b> inactive users after 20 minutes of continous inactivity on your website.</label> 127 <?php 116 $general_settings = get_option( 'wli_settings_general' ); 117 $value = $general_settings['wli_auto_kick_inactive_users']; 118 $checked = checked( $value, 1, false ); 119 ?> 120 <input id="wli_auto_kick_inactive_users" type="checkbox" name="wli_settings_general[wli_auto_kick_inactive_users]" value="1" <?= $checked; ?> > 121 <?php 128 122 } 129 123 … … 131 125 // render the settings page html/php 132 126 function wli_render_plugin_settings_page() { 133 // check user capabilities127 // check user has capability to manage options before rendering settings page 134 128 if ( !current_user_can( 'manage_options' ) ) { 135 129 return; … … 158 152 } 159 153 160 161 154 // when plugin is activated... 162 155 register_activation_hook( __FILE__, 'wli_plugin_activation' ); 163 156 function wli_plugin_activation() { 164 // get current user and add to logged in users list on plugin activation 165 $wli_current_user = wp_get_current_user(); 166 update_option( 'wli_logged_in_users', [ $wli_current_user->ID => $wli_current_user->user_login ], '', 'no' ); 167 // default to not automatically kicking users off when plugin is activated 168 update_option( 'wli_settings_general', ['wli_auto_kick_inactive_users' => 0] ); 157 // ...default to *NOT* automatically kicking users off the website when plugin is activated 158 update_option( 'wli_settings_general', ['wli_auto_kick_inactive_users' => 0] ); 169 159 } 170 160 … … 172 162 register_deactivation_hook( __FILE__, 'wli_plugin_deactivation' ); 173 163 function wli_plugin_deactivation() { 174 delete_option( 'wli_logged_in_users' ); 175 delete_option( 'wli_settings_general' ); 176 } 177 178 // output metabox content which is our list of logged in users stored in our option 179 function wli_metabox_ouput() { 180 181 echo "<div id='wli-logged-in-users-wrapper'>"; 182 echo wli_output_users(); 183 echo "</div>"; 164 delete_option( 'wli_logged_in_users' ); // delete old unused option from version 1.2 and older 165 delete_option( 'wli_settings_general' ); 184 166 } 185 167 … … 187 169 add_action( 'wp_dashboard_setup', 'wli_add_metabox' ); 188 170 function wli_add_metabox() { 189 if ( current_user_can( 'administrator' ) ) { 190 add_meta_box( 'wli-metabox', __( 'Who\'s Logged In', 'whos-logged-in' ), 'wli_metabox_ouput', 'dashboard', 'side', 'high' ); 191 } 192 } 193 194 // when a user logs in, add them to our list of logged in users 195 add_action( 'wp_login', 'wli_add_user', 10, 2 ); 196 function wli_add_user( $user_login, $user ) { 197 $wli_logged_in_users = get_option( 'wli_logged_in_users' ); 198 if ( !in_array( $user->user_login, $wli_logged_in_users ) ) { 199 $wli_logged_in_users[ $user->ID ] = $user->user_login; 200 update_option( 'wli_logged_in_users', $wli_logged_in_users, '', 'no' ); 201 } 202 } 203 204 // when a user logs out, remove them from our list of logged in users 205 add_action( 'wp_logout', 'wli_remove_user' ); 206 function wli_remove_user() { 207 $wli_current_user = wp_get_current_user(); 208 $wli_logged_in_users = get_option( 'wli_logged_in_users' ); 209 if ( in_array( $wli_current_user->user_login, $wli_logged_in_users ) ) { 210 unset( $wli_logged_in_users[ $wli_current_user->ID ] ); 211 update_option( 'wli_logged_in_users', $wli_logged_in_users, '', 'no' ); 212 } 213 } 214 215 // check if on dashboard home page, and that user is an administrator then load scripts 216 add_action( 'admin_enqueue_scripts', 'wli_remove_meta_boxes' ); 217 function wli_remove_meta_boxes( $hook ) { 218 if ( 'index.php' != $hook && !current_user_can( 'administrator' ) ) { 219 return; 220 } 221 222 // add script that runs ajax call at 5 second intervals to keep list of logged in users updated 223 wp_enqueue_script( 'wli-script', plugin_dir_url( __FILE__ ) . 'js/wli-script.js', [ 'jquery' ] ); 224 225 // add js variables for our ajax call url, and a nonce variable for security 226 wp_localize_script( 'wli-script', 'wli_js_vars', [ 171 if ( current_user_can( 'administrator' ) ) { 172 add_meta_box( 'wli-metabox', __( 'Who\'s Logged In', 'whos-logged-in' ), 'wli_metabox_ouput', 'dashboard', 'side', 'high' ); 173 } 174 } 175 // output metabox content using helper function for getting and displaying logged in user list 176 function wli_metabox_ouput() { 177 echo "<div id='wli-logged-in-users-wrapper'>"; 178 echo wli_output_users(); 179 echo "</div>"; 180 } 181 182 // Who's Logged In javascript loading 183 add_action( 'wp_enqueue_scripts', 'wli_load_scripts' ); 184 add_action( 'admin_enqueue_scripts', 'wli_load_scripts' ); 185 function wli_load_scripts( $hook ) { 186 187 // only load script that updates the metabox list of users on the dashboard homepage where the Whos Logged In metabox is displayed 188 if( $hook == 'index.php' ) { 189 // add script that runs ajax call at 15 second intervals to keep list of logged in users updated in the metabox 190 wp_enqueue_script( 'wli-update-user-list-script', plugin_dir_url( __FILE__ ) . 'js/wli-update-user-list-script.js', [ 'jquery' ] ); 191 192 // add js variables for our ajax call url, and a nonce variable for security 193 wp_localize_script( 'wli-update-user-list-script', 'wli_user_list_js_vars', [ 227 194 'ajax_url' => admin_url( 'admin-ajax.php' ), // make sure proper url is used for WordPress Ajax requests 228 195 'secret_nonce' => wp_create_nonce( 'wli_secret_nonce_sauce' ), // Create nonce to secure ajax requests 229 ] 230 ); 231 } 196 ]); 197 } 198 199 // only load script for autologout of inactive users if the setting is turned on and saved 200 // get autologout users setting 201 $general_settings = get_option( 'wli_settings_general' ); 202 $autologout_setting = $general_settings['wli_auto_kick_inactive_users']; 203 // check if autologout setting is turned on, then proceed to... 204 if($autologout_setting == 1) { 205 // add script that handles autologout functionality 206 wp_enqueue_script( 'wli-logout-script', plugin_dir_url( __FILE__ ) . 'js/wli-logout-script.js', [ 'jquery' ] ); 207 208 // add js variables for our ajax call url, and a nonce variable for security 209 wp_localize_script( 'wli-logout-script', 'wli_logout_js_vars', [ 210 'ajax_url' => admin_url( 'admin-ajax.php' ), // make sure proper url is used for WordPress Ajax requests 211 'secret_nonce' => wp_create_nonce( 'wli_secret_nonce_sauce' ), // Create nonce to secure ajax requests 212 ]); 213 } 214 } 215 232 216 233 217 // action called by ajax that updates list of logged in users shown on dashboard … … 238 222 die( 'Permission denied.' ); 239 223 240 // jQuery puts output response into div#wli-logged-in-users-wrapper 224 // jQuery puts output response into div#wli-logged-in-users-wrapper when ajax successful 241 225 echo wli_output_users(); 242 226 … … 244 228 } 245 229 246 // action called by ajax to log out user230 // action called by ajax to automatically log out inactive users 247 231 add_action( 'wp_ajax_wli_ajax_logout_users', 'wli_ajax_logout_users' ); 248 232 function wli_ajax_logout_users() { 249 // check if auto kickusers setting is on or off233 // check if autologout users setting is on or off 250 234 $general_settings = get_option( 'wli_settings_general' ); 251 $ value= $general_settings['wli_auto_kick_inactive_users'];252 // if auto kickis turned on, then proceed to...253 if($ value== 1) {235 $autologout_setting = $general_settings['wli_auto_kick_inactive_users']; 236 // if autologout is turned on, then proceed to... 237 if($autologout_setting == 1) { 254 238 // ...get the current user that triggered this action 255 239 $current_user = wp_get_current_user(); … … 258 242 $sessions = WP_Session_Tokens::get_instance($current_user->ID); 259 243 260 // ...destroy their sessions, and all of them!244 // ...destroy all of their login sessions to log them out 261 245 $sessions->destroy_all(); 262 246 } 263 // or else send response to let javascript know not to refresh page on ajax success247 // or else send response to let javascript know not to refresh the page on ajax success 264 248 else { 265 249 echo "auto-kick-is-turned-off";
Note: See TracChangeset
for help on using the changeset viewer.