Plugin Directory

Changeset 2328396


Ignore:
Timestamp:
06/22/2020 04:45:32 AM (6 years ago)
Author:
bhartlenn
Message:

updated plugin to version 1.3: includes bug fix for script loading, removal of an option for performance increase, and other minor changes - 1.0

Location:
whos-logged-in
Files:
4 added
1 deleted
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • whos-logged-in/tags/1.3/readme.txt

    r2324058 r2328396  
    66Tested up to: 5.4.2
    77Requires PHP: 5.6
    8 Stable tag: 1.2
     8Stable tag: 1.3
    99License: GPLv3
    1010License URI: https://www.gnu.org/licenses/gpl-3.0.html
    1111
    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 out
     12The 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
    1313
    1414== Description ==
    1515
    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"
     16Install and activate the Who's Logged In plugin, and you can view a list of currently logged in users on your Dashboard Home page.
    2217
    2318== Installation ==
     
    4439== Changelog ==
    4540
     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
    4645= 1.2 =
    4746* 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.
     
    5251== Upgrade Notice ==
    5352
    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  
    33/*
    44  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 out
    6   Version: 1.2
     5  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
    77  Author: Ben HartLenn
    88  Author URI: bhartlenn@gmail.com
     
    1111  License URI: https://www.gnu.org/licenses/gpl-3.0.html
    1212
    13   The Who's Logged In plugin shows you a list of logged in users on your dashboard 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!
    1414  Copyright (C) 2020  Ben HartLenn
    1515
     
    2727
    2828// Plugin helper function(s)
    29 // Display the list of users stored in plugin option
     29// Function to get and display a list of currently logged in users
    3030function 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   
    3334    // 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([
    3536        'meta_key' => 'session_tokens',
    3637        'meta_compare' => 'EXISTS',
    37         'fields' => ['user_login']
     38        'fields' => ['id', 'user_login']
    3839    ]);
    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');
    4842   
    4943    // initialize output variable
    5044    $wli_logged_in_users_output = "";
    5145   
    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>";
    5956  }
    6057    else {
    6158        $wli_logged_in_users_output .= "No users to display.";
    6259    }
    63    
     60    // return the output, do not echo
    6461    return $wli_logged_in_users_output;
    65    
    66     update_option( 'wli_logged_in_users', $wli_logged_in_users, '', 'no' );
    6762}
    6863// ***** End plugin helper functions section *****
    6964
    70 // ***** Plugin Functionality Section *****
    71 
     65// ***** Main Plugin Functionality Section *****
    7266// Setup a settings page for Who's Logged In available at Dashboard >> Settings >> Whos Logged In
    7367add_action( 'admin_menu', 'wli_add_settings_page' );
     
    8781function wli_settings_init() {
    8882    // 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 section
    98     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 off
    106     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  );       
    113107}
    114108
    115109// settings section intro text
    116110function 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>';
    117112}
    118113
    119114// render checkbox field for kicking users automatically
    120115function 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
    128122}
    129123   
     
    131125// render the settings page html/php
    132126function wli_render_plugin_settings_page() {
    133   // check user capabilities
     127  // check user has capability to manage options before rendering settings page
    134128  if ( !current_user_can( 'manage_options' ) ) {
    135129      return;
     
    158152}
    159153
    160 
    161 
     154// when plugin is activated...
    162155register_activation_hook( __FILE__, 'wli_plugin_activation' );
    163156function 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] );
    169159}
    170160
     
    172162register_deactivation_hook( __FILE__, 'wli_plugin_deactivation' );
    173163function 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' );
    184166}
    185167
     
    187169add_action( 'wp_dashboard_setup', 'wli_add_metabox' );
    188170function 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
     176function 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
     183add_action( 'wp_enqueue_scripts', 'wli_load_scripts' );
     184add_action( 'admin_enqueue_scripts', 'wli_load_scripts' );
     185function 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', [
    227194      'ajax_url' => admin_url( 'admin-ajax.php' ), // make sure proper url is used for WordPress Ajax requests
    228195      '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
    232216
    233217// action called by ajax that updates list of logged in users shown on dashboard
     
    238222        die( 'Permission denied.' );
    239223       
    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
    241225    echo wli_output_users();
    242226
     
    244228}
    245229
    246 // action called by ajax to log out user
     230// action called by ajax to automatically log out inactive users
    247231add_action( 'wp_ajax_wli_ajax_logout_users', 'wli_ajax_logout_users' );
    248232function wli_ajax_logout_users() {
    249     // check if auto kick users setting is on or off
     233    // check if autologout users setting is on or off
    250234  $general_settings = get_option( 'wli_settings_general' );
    251   $value = $general_settings['wli_auto_kick_inactive_users'];
    252     // if auto kick is 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) { 
    254238        // ...get the current user that triggered this action
    255239        $current_user = wp_get_current_user();
     
    258242        $sessions = WP_Session_Tokens::get_instance($current_user->ID);
    259243
    260         // ...destroy their sessions, and all of them!
     244        // ...destroy all of their login sessions to log them out
    261245        $sessions->destroy_all();
    262246    }
    263     // or else send response to let javascript know not to refresh page on ajax success
     247    // or else send response to let javascript know not to refresh the page on ajax success
    264248    else {
    265249        echo "auto-kick-is-turned-off";
  • whos-logged-in/trunk/readme.txt

    r2324058 r2328396  
    66Tested up to: 5.4.2
    77Requires PHP: 5.6
    8 Stable tag: 1.2
     8Stable tag: 1.3
    99License: GPLv3
    1010License URI: https://www.gnu.org/licenses/gpl-3.0.html
    1111
    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 out
     12The 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
    1313
    1414== Description ==
    1515
    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"
     16Install and activate the Who's Logged In plugin, and you can view a list of currently logged in users on your Dashboard Home page.
    2217
    2318== Installation ==
     
    4439== Changelog ==
    4540
     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
    4645= 1.2 =
    4746* 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.
     
    5251== Upgrade Notice ==
    5352
    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  
    33/*
    44  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 out
    6   Version: 1.2
     5  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
    77  Author: Ben HartLenn
    88  Author URI: bhartlenn@gmail.com
     
    1111  License URI: https://www.gnu.org/licenses/gpl-3.0.html
    1212
    13   The Who's Logged In plugin shows you a list of logged in users on your dashboard 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!
    1414  Copyright (C) 2020  Ben HartLenn
    1515
     
    2727
    2828// Plugin helper function(s)
    29 // Display the list of users stored in plugin option
     29// Function to get and display a list of currently logged in users
    3030function 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   
    3334    // 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([
    3536        'meta_key' => 'session_tokens',
    3637        'meta_compare' => 'EXISTS',
    37         'fields' => ['user_login']
     38        'fields' => ['id', 'user_login']
    3839    ]);
    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');
    4842   
    4943    // initialize output variable
    5044    $wli_logged_in_users_output = "";
    5145   
    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>";
    5956  }
    6057    else {
    6158        $wli_logged_in_users_output .= "No users to display.";
    6259    }
    63    
     60    // return the output, do not echo
    6461    return $wli_logged_in_users_output;
    65    
    66     update_option( 'wli_logged_in_users', $wli_logged_in_users, '', 'no' );
    6762}
    6863// ***** End plugin helper functions section *****
    6964
    70 // ***** Plugin Functionality Section *****
    71 
     65// ***** Main Plugin Functionality Section *****
    7266// Setup a settings page for Who's Logged In available at Dashboard >> Settings >> Whos Logged In
    7367add_action( 'admin_menu', 'wli_add_settings_page' );
     
    8781function wli_settings_init() {
    8882    // 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 section
    98     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 off
    106     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  );       
    113107}
    114108
    115109// settings section intro text
    116110function 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>';
    117112}
    118113
    119114// render checkbox field for kicking users automatically
    120115function 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
    128122}
    129123   
     
    131125// render the settings page html/php
    132126function wli_render_plugin_settings_page() {
    133   // check user capabilities
     127  // check user has capability to manage options before rendering settings page
    134128  if ( !current_user_can( 'manage_options' ) ) {
    135129      return;
     
    158152}
    159153
    160 
    161 
     154// when plugin is activated...
    162155register_activation_hook( __FILE__, 'wli_plugin_activation' );
    163156function 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] );
    169159}
    170160
     
    172162register_deactivation_hook( __FILE__, 'wli_plugin_deactivation' );
    173163function 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' );
    184166}
    185167
     
    187169add_action( 'wp_dashboard_setup', 'wli_add_metabox' );
    188170function 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
     176function 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
     183add_action( 'wp_enqueue_scripts', 'wli_load_scripts' );
     184add_action( 'admin_enqueue_scripts', 'wli_load_scripts' );
     185function 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', [
    227194      'ajax_url' => admin_url( 'admin-ajax.php' ), // make sure proper url is used for WordPress Ajax requests
    228195      '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
    232216
    233217// action called by ajax that updates list of logged in users shown on dashboard
     
    238222        die( 'Permission denied.' );
    239223       
    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
    241225    echo wli_output_users();
    242226
     
    244228}
    245229
    246 // action called by ajax to log out user
     230// action called by ajax to automatically log out inactive users
    247231add_action( 'wp_ajax_wli_ajax_logout_users', 'wli_ajax_logout_users' );
    248232function wli_ajax_logout_users() {
    249     // check if auto kick users setting is on or off
     233    // check if autologout users setting is on or off
    250234  $general_settings = get_option( 'wli_settings_general' );
    251   $value = $general_settings['wli_auto_kick_inactive_users'];
    252     // if auto kick is 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) { 
    254238        // ...get the current user that triggered this action
    255239        $current_user = wp_get_current_user();
     
    258242        $sessions = WP_Session_Tokens::get_instance($current_user->ID);
    259243
    260         // ...destroy their sessions, and all of them!
     244        // ...destroy all of their login sessions to log them out
    261245        $sessions->destroy_all();
    262246    }
    263     // or else send response to let javascript know not to refresh page on ajax success
     247    // or else send response to let javascript know not to refresh the page on ajax success
    264248    else {
    265249        echo "auto-kick-is-turned-off";
Note: See TracChangeset for help on using the changeset viewer.