Plugin Directory

Changeset 3066190


Ignore:
Timestamp:
04/07/2024 08:04:53 AM (2 years ago)
Author:
addonspress
Message:

1.2.4

Location:
only-one-device-login-limit
Files:
10 added
4 edited

Legend:

Unmodified
Added
Removed
  • only-one-device-login-limit/trunk/assets/js/coder-limit-login.js

    r1196579 r3066190  
    11/** @since 1.0*/
    2 
    3 jQuery(document).ready(function($){
    4     $( '#destroy-sessions' ).on( 'click', function( e ) {
    5         $.ajax({
    6             type: "POST",
    7             url: ajaxurl,
    8             data: {
    9                 action: 'coder_destroy_sessions_ajax',
    10                 user_id: $( '#user_id' ).val()
    11             },
    12             beforeSend: function(){
    13 
    14             },
    15             success:function(response){
    16                 $( '#coder-login-logout-status').html(response)
    17             }
    18         })
    19     })
    20 })
    21        
     2jQuery(document).ready(function ($) {
     3    $('#destroy-sessions').on('click', function (e) {
     4        $.ajax({
     5            type: 'POST',
     6            url: ajaxurl,
     7            data: {
     8                action: 'coder_destroy_sessions_ajax',
     9                user_id: $('#user_id').val(),
     10            },
     11            beforeSend: function () {},
     12            success: function (response) {
     13                $('#coder-login-logout-status').html(response);
     14            },
     15        });
     16    });
     17});
  • only-one-device-login-limit/trunk/inc/coder-admin-menu.php

    r1195134 r3066190  
    11<?php
    2 if ( ! class_exists( 'Coder_Limit_Login_Admin_Menu_Setting' ) ){
    3     /**
    4      * Class for Limit login Admin Menu and Setting
    5      *
    6      * @package Coder Limit Login Framework
    7      * @subpackage Coder Limit Login Admin Menu Setting
    8      * @since 1.0
    9      */
    10     class Coder_Limit_Login_Admin_Menu_Setting{
    11         /*Basic variables for class*/
    12 
    13         /**
    14          * Option saved value.
    15          *
    16          * @var array
    17          * @access protected
    18          * @since 1.0
    19          *
    20          */
    21         protected $coder_limit_login_options = array();
    22 
    23         /**
    24          * Coder_Limit_Login_Admin_Menu_Setting instance.
    25          *
    26          * @see coder_get_instance()
    27          * @var object
    28          * @access protected
    29          * @since 1.0
    30          *
    31          */
    32         protected static $coder_instance = NULL;
    33 
    34 
    35         /**
    36          * Access Coder_Limit_Login_Admin_Menu_Setting working coder_instance
    37          *
    38          * @access public
    39          * @since 1.0
    40          * @return object of this class
    41          */
    42         public static function coder_get_instance() {
    43             NULL === self::$coder_instance and self::$coder_instance = new self;
    44             return self::$coder_instance;
    45         }
    46 
    47         /**
    48          * Used for regular plugin work.
    49          *
    50          * @access public
    51          * @since 1.0
    52          *
    53          * @return void
    54          *
    55          */
    56         public function coder_admin_menu_init() {
    57 
    58             /*Hook before any function of class start */
    59             do_action( 'coder_admin_menu_before');
    60 
    61             $this->coder_limit_login_options = get_option( 'coder-limit-login-options' );
    62 
    63             /*Adding menu page*/
    64             add_action( 'admin_menu', array($this,'coder_admin_submenu') ,12 );
    65 
    66             /*Adding coder register setting*/
    67             add_action( 'admin_init', array($this,'coder_register_setting') ,12 );
    68 
    69             /*Hook before any function of class end */
    70             do_action( 'coder_admin_menu_after');
    71         }
    72 
    73         /**
    74          * Constructor. Intentionally left empty and public.
    75          *
    76          * @access public
    77          * @since 1.0
    78          *
    79          */
    80         public function __construct(){ }
    81 
    82         /**
    83          * Add submenu in general options
    84          *
    85          * @access public
    86          * @since 1.0
    87          *
    88          * @return void
    89          *
    90          */
    91         public function coder_admin_submenu() {
    92             add_submenu_page(
    93                 "options-general.php",
    94                 __('Coder limit login','coder_limit_login'),
    95                 __('Coder limit login','coder_limit_login'),
    96                 'manage_options',
    97                 'coder-limit-login-setting',
    98                 array($this, 'coder_submenu_page' )
    99             );
    100             /*add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );*/
    101         }
    102         /**
    103          * Add form fields in Coder limit login page
    104          *
    105          * @access public
    106          * @since 1.0
    107          *
    108          * @return void
    109          *
    110          */
    111         public function coder_submenu_page() {
    112             ?>
    113             <div class="wrap">
    114                 <h2><?php _e('Coder limit login Settings','coder_limit_login');?></h2>
    115                 <br />
    116                 <form method="post" enctype="multipart/form-data" action="options.php">
    117                     <?php
    118                     settings_fields( 'coder-limit-login-optiongroup' );
    119                     do_settings_sections( 'coder_limit_login_page' );
    120                     submit_button();
    121                     ?>
    122                 </form>
    123             </div>
    124         <?php
    125         }
    126 
    127         /**
    128          * Add setting sections and fields
    129          *
    130          * @access public
    131          * @since 1.0
    132          *
    133          * @return void
    134          *
    135          */
    136         public function coder_register_setting(){
    137             register_setting(
    138                 'coder-limit-login-optiongroup',
    139                 'coder-limit-login-options',
    140                 array($this, 'coder_sanitize_callback' )
    141             );
    142             /*register_setting( $option_group, $option_name, $sanitize_callback )*/
    143 
    144             add_settings_section(
    145                 'coder-limit-login-general',
    146                 __('Coder limit login general setting','coder_limit_login'),
    147                 array($this, 'coder_print_section_info' ),
    148                 'coder_limit_login_page'
    149             );
    150             /*add_settings_section( $id, $title, $callback, $page )*/
    151 
    152             add_settings_field(
    153                 'coder_logout_duration',
    154                 '<label for="coder_logout_duration">'.__('Auto Logout Duration','coder_limit_login').'</label>',
    155                 array($this, 'coder_logout_duration' ),
    156                 'coder_limit_login_page',
    157                 'coder-limit-login-general'
    158             );
    159             // add_settings_field ( id , field title , callback function , menu page slug , settings section , [arguments] )
    160            
    161             add_settings_field(
    162                 'coder_enable_admin',
    163                 '<label for="coder_enable_admin">'.__('Enable for Admin','coder_limit_login').'</label>',
    164                 array($this, 'coder_enable_admin'),
    165                 'coder_limit_login_page',
    166                 'coder-limit-login-general'
    167             );
    168 
    169             add_settings_field(
    170                 'coder_force_logout_message',
    171                 '<label for="coder_force_logout_message">'.__('Already logout message','coder_limit_login').'</label>',
    172                 array($this, 'coder_force_logout_message'),
    173                 'coder_limit_login_page',
    174                 'coder-limit-login-general'
    175             );
    176 
    177             add_settings_field(
    178                 'coder_already_login_message',
    179                 '<label for="coder_already_login_message">'.__('Already login message','coder_limit_login').'</label>',
    180                 array($this, 'coder_already_login_message'),
    181                 'coder_limit_login_page',
    182                 'coder-limit-login-general'
    183             );
    184         }
    185 
    186         /**
    187          * Validate options values
    188          *
    189          * @access public
    190          * @since 1.0
    191          *
    192          * @param object $coder_input
    193          * @return Array
    194          *
    195          */
    196         public function coder_sanitize_callback($coder_input){
    197 //            $coder_valid_input = array();
    198 //            $coder_valid_input['coder_logout_duration'] = intval($coder_input['coder_logout_duration']);
    199             return $coder_input;
    200         }
    201 
    202         /**
    203          * Display section info
    204          *
    205          * @access public
    206          * @since 1.0
    207          *
    208          * @param null
    209          * @return void
    210          *
    211          */
    212         public function coder_print_section_info(){
    213             echo '<hr />';
    214         }
    215 
    216         /**
    217          * Display coder logout duration fields
    218          *
    219          * @access public
    220          * @since 1.0
    221          *
    222          * @param null
    223          * @return void
    224          *
    225          */
    226         public function coder_logout_duration(){
    227             $coder_value = isset( $this->coder_limit_login_options['coder_logout_duration'] ) ? esc_attr( $this->coder_limit_login_options['coder_logout_duration']) : '';
    228             echo '<input type="text" id="coder_logout_duration" name="coder-limit-login-options[coder_logout_duration]" value="'.$coder_value.'" /> minutes';
    229         }
    230 
    231         /**
    232          * Display coder enable admin fields
    233          *
    234          * @access public
    235          * @since 1.0
    236          *
    237          * @param null
    238          * @return void
    239          *
    240          */
    241         public function coder_enable_admin(){
    242             $coder_value = isset( $this->coder_limit_login_options['coder_enable_admin'] ) ? esc_attr( $this->coder_limit_login_options['coder_enable_admin']) : '';
    243             echo '<input type="checkbox" id="coder_enable_admin" name="coder-limit-login-options[coder_enable_admin]" '.checked(!empty($coder_value), true, false).' />';
    244         }
    245 
    246         /**
    247          * Display coder force logout info fields
    248          *
    249          * @access public
    250          * @since 1.0
    251          *
    252          * @param null
    253          * @return void
    254          *
    255          */
    256         public function coder_force_logout_message(){
    257             $coder_value = isset( $this->coder_limit_login_options['coder_force_logout_message'] ) ? esc_attr( $this->coder_limit_login_options['coder_force_logout_message']) : '';
    258             echo "<textarea name='coder-limit-login-options[coder_force_logout_message]' id='coder-limit-login-options[coder_force_logout_message]' cols='50' rows='8'>{$coder_value}</textarea>";
    259         }
    260 
    261         /**
    262          * Display coder force logout info fields
    263          *
    264          * @access public
    265          * @since 1.0
    266          *
    267          * @param null
    268          * @return void
    269          *
    270          */
    271         public function coder_already_login_message(){
    272             $coder_value = isset( $this->coder_limit_login_options['coder_already_login_message'] ) ? esc_attr( $this->coder_limit_login_options['coder_already_login_message']) : '';
    273             echo "<textarea name='coder-limit-login-options[coder_already_login_message]' id='coder-limit-login-options[coder_already_login_message]' cols='50' rows='8'>{$coder_value}</textarea>";
    274         }
    275 
    276 
    277 
    278     } /*END class Coder_Limit_Login_Admin_Menu_Setting*/
    279 
    280     /*Initialize class after admin_init*/
    281     $coder_admin_menu =  new Coder_Limit_Login_Admin_Menu_Setting();
    282     $coder_admin_menu->coder_admin_menu_init();
    283 //    add_action( 'admin_init', array ( Coder_Limit_Login_Admin_Menu_Setting::coder_get_instance(), 'coder_admin_menu_init' ));
     2if ( ! class_exists( 'Coder_Limit_Login_Admin_Menu_Setting' ) ) {
     3    /**
     4     * Class for Limit login Admin Menu and Setting
     5     *
     6     * @package Coder Limit Login Framework
     7     * @subpackage Coder Limit Login Admin Menu Setting
     8     * @since 1.0
     9     */
     10    class Coder_Limit_Login_Admin_Menu_Setting {
     11        /*Basic variables for class*/
     12
     13        /**
     14         * Option saved value.
     15         *
     16         * @var array
     17         * @access protected
     18         * @since 1.0
     19         */
     20        protected $coder_limit_login_options = array();
     21
     22        /**
     23         * Coder_Limit_Login_Admin_Menu_Setting instance.
     24         *
     25         * @see coder_get_instance()
     26         * @var object
     27         * @access protected
     28         * @since 1.0
     29         */
     30        protected static $coder_instance = null;
     31
     32
     33        /**
     34         * Access Coder_Limit_Login_Admin_Menu_Setting working coder_instance
     35         *
     36         * @access public
     37         * @since 1.0
     38         * @return object of this class
     39         */
     40        public static function coder_get_instance() {
     41            null === self::$coder_instance and self::$coder_instance = new self();
     42            return self::$coder_instance;
     43        }
     44
     45        /**
     46         * Used for regular plugin work.
     47         *
     48         * @access public
     49         * @since 1.0
     50         *
     51         * @return void
     52         */
     53        public function coder_admin_menu_init() {
     54
     55            /*Hook before any function of class start */
     56            do_action( 'coder_admin_menu_before' );
     57
     58            $this->coder_limit_login_options = get_option( 'coder-limit-login-options' );
     59
     60            /*Adding menu page*/
     61            add_action( 'admin_menu', array( $this, 'coder_admin_submenu' ), 12 );
     62
     63            /*Adding coder register setting*/
     64            add_action( 'admin_init', array( $this, 'coder_register_setting' ), 12 );
     65
     66            /*Hook before any function of class end */
     67            do_action( 'coder_admin_menu_after' );
     68        }
     69
     70        /**
     71         * Constructor. Intentionally left empty and public.
     72         *
     73         * @access public
     74         * @since 1.0
     75         */
     76        public function __construct() { }
     77
     78        /**
     79         * Add submenu in general options
     80         *
     81         * @access public
     82         * @since 1.0
     83         *
     84         * @return void
     85         */
     86        public function coder_admin_submenu() {
     87            add_submenu_page(
     88                'options-general.php',
     89                __( 'Coder limit login', 'coder_limit_login' ),
     90                __( 'Coder limit login', 'coder_limit_login' ),
     91                'manage_options',
     92                'coder-limit-login-setting',
     93                array( $this, 'coder_submenu_page' )
     94            );
     95            /*add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );*/
     96        }
     97        /**
     98         * Add form fields in Coder limit login page
     99         *
     100         * @access public
     101         * @since 1.0
     102         *
     103         * @return void
     104         */
     105        public function coder_submenu_page() {
     106            ?>
     107            <div class="wrap">
     108                <h2><?php _e( 'Coder limit login Settings', 'coder_limit_login' ); ?></h2>
     109                <br />
     110                <form method="post" enctype="multipart/form-data" action="options.php">
     111                    <?php
     112                    settings_fields( 'coder-limit-login-optiongroup' );
     113                    do_settings_sections( 'coder_limit_login_page' );
     114                    submit_button();
     115                    ?>
     116                </form>
     117            </div>
     118            <?php
     119        }
     120
     121        /**
     122         * Add setting sections and fields
     123         *
     124         * @access public
     125         * @since 1.0
     126         *
     127         * @return void
     128         */
     129        public function coder_register_setting() {
     130            register_setting(
     131                'coder-limit-login-optiongroup',
     132                'coder-limit-login-options',
     133                array( $this, 'coder_sanitize_callback' )
     134            );
     135            /*register_setting( $option_group, $option_name, $sanitize_callback )*/
     136
     137            add_settings_section(
     138                'coder-limit-login-general',
     139                __( 'Coder limit login general setting', 'coder_limit_login' ),
     140                array( $this, 'coder_print_section_info' ),
     141                'coder_limit_login_page'
     142            );
     143            /*add_settings_section( $id, $title, $callback, $page )*/
     144
     145            add_settings_field(
     146                'coder_logout_duration',
     147                '<label for="coder_logout_duration">' . __( 'Auto Logout Duration', 'coder_limit_login' ) . '</label>',
     148                array( $this, 'coder_logout_duration' ),
     149                'coder_limit_login_page',
     150                'coder-limit-login-general'
     151            );
     152            // add_settings_field ( id , field title , callback function , menu page slug , settings section , [arguments] )
     153
     154            add_settings_field(
     155                'coder_enable_admin',
     156                '<label for="coder_enable_admin">' . __( 'Enable for Admin', 'coder_limit_login' ) . '</label>',
     157                array( $this, 'coder_enable_admin' ),
     158                'coder_limit_login_page',
     159                'coder-limit-login-general'
     160            );
     161
     162            add_settings_field(
     163                'coder_force_logout_message',
     164                '<label for="coder_force_logout_message">' . __( 'Already logout message', 'coder_limit_login' ) . '</label>',
     165                array( $this, 'coder_force_logout_message' ),
     166                'coder_limit_login_page',
     167                'coder-limit-login-general'
     168            );
     169
     170            add_settings_field(
     171                'coder_already_login_message',
     172                '<label for="coder_already_login_message">' . __( 'Already login message', 'coder_limit_login' ) . '</label>',
     173                array( $this, 'coder_already_login_message' ),
     174                'coder_limit_login_page',
     175                'coder-limit-login-general'
     176            );
     177        }
     178
     179        /**
     180         * Validate options values
     181         *
     182         * @access public
     183         * @since 1.0
     184         *
     185         * @param object $coder_input
     186         * @return Array
     187         */
     188        public function coder_sanitize_callback( $coder_input ) {
     189            // $coder_valid_input = array();
     190            // $coder_valid_input['coder_logout_duration'] = intval($coder_input['coder_logout_duration']);
     191            return $coder_input;
     192        }
     193
     194        /**
     195         * Display section info
     196         *
     197         * @access public
     198         * @since 1.0
     199         *
     200         * @param null
     201         * @return void
     202         */
     203        public function coder_print_section_info() {
     204            echo '<hr />';
     205        }
     206
     207        /**
     208         * Display coder logout duration fields
     209         *
     210         * @access public
     211         * @since 1.0
     212         *
     213         * @param null
     214         * @return void
     215         */
     216        public function coder_logout_duration() {
     217            $coder_value = isset( $this->coder_limit_login_options['coder_logout_duration'] ) ? esc_attr( $this->coder_limit_login_options['coder_logout_duration'] ) : '';
     218            echo '<input type="text" id="coder_logout_duration" name="coder-limit-login-options[coder_logout_duration]" value="' . $coder_value . '" /> minutes';
     219        }
     220
     221        /**
     222         * Display coder enable admin fields
     223         *
     224         * @access public
     225         * @since 1.0
     226         *
     227         * @param null
     228         * @return void
     229         */
     230        public function coder_enable_admin() {
     231            $coder_value = isset( $this->coder_limit_login_options['coder_enable_admin'] ) ? esc_attr( $this->coder_limit_login_options['coder_enable_admin'] ) : '';
     232            echo '<input type="checkbox" id="coder_enable_admin" name="coder-limit-login-options[coder_enable_admin]" ' . checked( ! empty( $coder_value ), true, false ) . ' />';
     233        }
     234
     235        /**
     236         * Display coder force logout info fields
     237         *
     238         * @access public
     239         * @since 1.0
     240         *
     241         * @param null
     242         * @return void
     243         */
     244        public function coder_force_logout_message() {
     245            $coder_value = isset( $this->coder_limit_login_options['coder_force_logout_message'] ) ? esc_attr( $this->coder_limit_login_options['coder_force_logout_message'] ) : '';
     246            echo "<textarea name='coder-limit-login-options[coder_force_logout_message]' id='coder-limit-login-options[coder_force_logout_message]' cols='50' rows='8'>{$coder_value}</textarea>";
     247        }
     248
     249        /**
     250         * Display coder force logout info fields
     251         *
     252         * @access public
     253         * @since 1.0
     254         *
     255         * @param null
     256         * @return void
     257         */
     258        public function coder_already_login_message() {
     259            $coder_value = isset( $this->coder_limit_login_options['coder_already_login_message'] ) ? esc_attr( $this->coder_limit_login_options['coder_already_login_message'] ) : '';
     260            echo "<textarea name='coder-limit-login-options[coder_already_login_message]' id='coder-limit-login-options[coder_already_login_message]' cols='50' rows='8'>{$coder_value}</textarea>";
     261        }
     262    } /*END class Coder_Limit_Login_Admin_Menu_Setting*/
     263
     264    /*Initialize class after admin_init*/
     265    $coder_admin_menu = new Coder_Limit_Login_Admin_Menu_Setting();
     266    $coder_admin_menu->coder_admin_menu_init();
     267    // add_action( 'admin_init', array ( Coder_Limit_Login_Admin_Menu_Setting::coder_get_instance(), 'coder_admin_menu_init' ));
    284268
    285269}/*END if(!class_exists('Coder_Limit_Login_Admin_Menu_Setting'))*/
  • only-one-device-login-limit/trunk/only-one-device-login-limit.php

    r2672961 r3066190  
    44Plugin URI: https://www.addonspress.com/
    55Description: Limit login to one device at a time for a user
    6 Version: 1.2.3
     6Version: 1.2.4
    77Author: addonspress
    88Author URI: https://www.addonspress.com/
     
    1212
    1313/*Make sure we don't expose any info if called directly*/
    14 if ( !function_exists( 'add_action' ) ) {
    15     echo 'Hi there!  I\'m just a plugin, not much I can do when called directly.';
    16     exit;
     14if ( ! function_exists( 'add_action' ) ) {
     15    echo 'Hi there!  I\'m just a plugin, not much I can do when called directly.';
     16    exit;
    1717}
    1818
    19 if ( ! class_exists( 'Coder_Limit_Login' ) ){
    20     /**
    21      * Class for Limit login to one device at a time for a user
    22      *
    23      * @package Coder Limit Framework
    24      * @since 1.0
    25      */
    26     class Coder_Limit_Login{
    27         /*Basic variables for class*/
    28 
    29         /**
    30          * Variable to hold this plugin version
    31          *
    32          * @var string
    33          * @access protected
    34          * @since 1.0
    35          *
    36          */
    37         private  $coder_limit_login_version = '1.2.3';
    38 
    39         /**
    40          * Variable to hold this plugin minimum wp version
    41          *
    42          * @var string
    43          * @access protected
    44          * @since 1.0
    45          *
    46          */
    47         protected $coder_limit_login_minimum_wp_version = '3.1';
    48 
    49         /**
    50          * Coder_Limit_Login Plugin instance.
    51          *
    52          * @see coder_get_instance()
    53          * @var object
    54          * @access protected
    55          * @since 1.0
    56          *
    57          */
    58         protected static $coder_instance = NULL;
    59 
    60         /**
    61          * Variable to hold this framework url
    62          *
    63          * @var string
    64          * @access protected
    65          * @since 1.0
    66          *
    67          */
    68         protected $coder_limit_login_url = '';
    69 
    70         /**
    71          * Variable to hold this framework path
    72          *
    73          * @var string
    74          * @access protected
    75          * @since 1.0
    76          *
    77          */
    78         protected $coder_limit_login_path = '';
    79 
    80         /**
    81          * Option saved value.
    82          *
    83          * @var array
    84          * @access protected
    85          * @since 1.0
    86          *
    87          */
    88         protected $coder_limit_login_options = array();
    89 
    90         /**
    91          * Stored logout in minutes
    92          *
    93          * @var int
    94          * @access protected
    95          * @since 1.0
    96          *
    97          */
    98         protected $coder_logout_duration = 30;
    99 
    100         /**
    101          * Stored logout in seconds
    102          *
    103          * @var int
    104          * @access protected
    105          * @since 1.0
    106          *
    107          */
    108         protected $coder_logout_duration_seconds = 1800;
    109 
    110         /**
    111          * Stored already login message
    112          *
    113          * @var string
    114          * @access protected
    115          * @since 1.0
    116          *
    117          */
    118         protected $coder_already_login_message = '<h1>The User with this username already login! </h1>';
    119 
    120         /**
    121          * Stored logout message
    122          *
    123          * @var string
    124          * @access protected
    125          * @since 1.0
    126          *
    127          */
    128         protected $coder_force_logout_message = '<h1>The User with this username already logout ! Please Login to Continue. </h1>';
    129 
    130         /**
    131          * check if admin is enable
    132          *
    133          * @var boolean
    134          * @access protected
    135          * @since 1.0
    136          *
    137          */
    138         protected $coder_enable_admin = false;
    139 
    140         /**
    141          * Access this plugin’s working coder_instance
    142          *
    143          * @access public
    144          * @since 1.0
    145          * @return object of this class
    146          */
    147         public static function coder_get_instance() {
    148             NULL === self::$coder_instance and self::$coder_instance = new self;
    149             return self::$coder_instance;
    150         }
    151 
    152         /**
    153          * Used for regular plugin work
    154          * @access public
    155          * @since 1.0
    156          *
    157          * @return void
    158          *
    159          */
    160         public function coder_limit_login_init() {
    161 
    162             /*Basic variables initialization with filter*/
    163             $this->coder_limit_login_url = plugin_dir_url( __FILE__ ) ;
    164             $this->coder_limit_login_path = plugin_dir_path( __FILE__ );
    165             $this->coder_limit_login_url = apply_filters( 'coder_limit_login_url', $this->coder_limit_login_url );
    166             $this->coder_limit_login_path = apply_filters( 'coder_limit_login_path', $this->coder_limit_login_path );
    167             $this->coder_limit_login_options =  apply_filters( 'coder_limit_login_options', get_option( 'coder-limit-login-options' ));
    168             if(isset($this->coder_limit_login_options['coder_logout_duration'])){
    169                 $this->coder_logout_duration =  apply_filters( 'coder_logout_duration', $this->coder_limit_login_options['coder_logout_duration'] );
    170             }
    171             if(isset($this->coder_limit_login_options['coder_already_login_message'])){
    172                 $this->coder_already_login_message =  apply_filters( 'coder_logout_duration', $this->coder_limit_login_options['coder_already_login_message'] );
    173             }
    174             if(isset($this->coder_limit_login_options['coder_force_logout_message'])){
    175                 $this->coder_force_logout_message =  apply_filters( 'coder_logout_duration', $this->coder_limit_login_options['coder_force_logout_message'] );
    176             }
    177 
    178             $this->coder_logout_duration_seconds =  (int)$this->coder_logout_duration * 60;
    179             if(isset($this->coder_limit_login_options['coder_enable_admin'])){
    180                 $this->coder_enable_admin =  apply_filters( 'coder_enable_admin', $this->coder_limit_login_options['coder_enable_admin']);
    181             }
    182             /*load translation*/
    183             add_action('init', array($this,'coder_load_textdomain') , 12);
    184 
    185             /*Hook before any function of class start */
    186             do_action( 'coder_limit_login_before');
    187 
    188             /*Enqueue necessary styles and scripts.*/
    189             add_action( 'wp_enqueue_scripts', array($this,'coder_limit_login_enqueue_scripts') ,12 );
    190             add_action( 'admin_enqueue_scripts', array($this,'coder_limit_login_enqueue_scripts') ,12 );
    191 
    192             /*check if user is already login*/
    193             add_action( 'wp_login', array($this,'coder_check_if_already_user_active') ,0 ,1 );
    194 
    195 
    196             /*set current user active time*/
    197             add_action( 'init', array($this,'coder_set_current_user_active_time') ,0 );
    198 
    199             /*make sure user is logout*/
    200             add_action( 'wp_logout', array($this,'coder_set_logout') ,0 ,1 );
    201 
    202             /*user profile show/edit*/
    203             add_action( 'show_user_profile', array($this,'coder_add_custom_user_profile_fields') ,12 ,1 );
    204             add_action( 'edit_user_profile', array($this,'coder_add_custom_user_profile_fields') ,12 ,1 );
    205 
    206             /*user profile save*/
    207             add_action( 'personal_options_update', array($this,'coder_save_custom_user_profile_fields') ,12  );
    208             add_action( 'edit_user_profile_update', array($this,'coder_save_custom_user_profile_fields') ,12 );
    209 
    210             /*adding column to user listing*/
    211             add_filter( 'manage_users_columns', array($this,'coder_modify_user_columns') ,12 );
    212 
    213             /*adding content to new custom column*/
    214             add_filter( 'manage_users_custom_column', array($this,'coder_modify_user_column_content') ,12 ,3 );
    215 
    216             /*make column sortable*/
    217             add_filter( 'manage_users_sortable_columns', array($this,'coder_make_sortable_column') ,12 );
    218 
    219             /*Actual function to make column sortable*/
    220             add_filter( 'pre_user_query', array($this,'coder_sortable_column_query') ,12 );
    221 
    222             /*wp ajax handling*/
    223             add_action( 'wp_ajax_coder_destroy_sessions_ajax', array($this,'coder_destroy_sessions_ajax_callback') ,12 );
    224 
    225             /*Setting admin menu*/
    226             require_once trailingslashit( $this->coder_limit_login_path ) . 'inc/coder-admin-menu.php';
    227 
    228             /*Hook before any function of class end */
    229             do_action( 'coder_limit_login_after');
    230         }
    231 
    232         /**
    233          * Constructor. Intentionally left empty and public.
    234          *
    235          * @access public
    236          * @since 1.0
    237          *
    238          */
    239         public function __construct(){ }
    240 
    241         /**
    242          * Load_textdomain
    243          *
    244          * @access public
    245          * @since 1.0
    246          *
    247          * @return void
    248          *
    249          */
    250         public function coder_load_textdomain(){
    251             /*Added filter for text domain path*/
    252             $coder_limit_login_textdomain_path = apply_filters( 'coder_limit_login_textdomain_path', $this->coder_limit_login_path );
    253             load_textdomain( 'coder_limit_login', $coder_limit_login_textdomain_path . '/languages/' . get_locale() .'.mo' );
    254         }
    255 
    256 
    257         /**
    258          * Enqueue style and scripts at Theme Limit
    259          *
    260          * @access public
    261          * @since 1.0
    262          *
    263          * @return void
    264          *
    265          */
    266         public function coder_limit_login_enqueue_scripts(){
    267 
    268             wp_register_style( 'coder-limit-login-style', $this->coder_limit_login_url . '/assets/css/coder-limit-login.css', false, $this->coder_limit_login_version );
    269             wp_enqueue_style( 'coder-limit-login-style' );
    270 
    271             /*localizing the script start*/
    272             /*Register the script*/
    273             wp_register_script( 'coder-limit-login', $this->coder_limit_login_url . '/assets/js/coder-limit-login.js', array( 'jquery' ), $this->coder_limit_login_version, true );
    274             /*Localize the script with new data*/
    275             $coder_customizer_localization_array = array(
    276                 'coder_limit_login_url' => $this->coder_limit_login_url
    277             );
    278             wp_localize_script( 'coder-limit-login', 'coder_limit_login', $coder_customizer_localization_array );
    279             /*enqueue script with localized data.*/
    280 
    281             wp_enqueue_script( 'coder-limit-login' );
    282             /*localizing the script end*/
    283         }
    284 
    285         /**
    286          * Logout function
    287          *
    288          * @access public
    289          * @since 1.0
    290          *
    291          * @return void
    292          *
    293          */
    294         public function coder_force_logout($coder_logout_message ){
    295             nocache_headers();
    296             wp_clear_auth_cookie();
    297             do_action('wp_logout');
    298             wp_die($coder_logout_message, '', array( 'back_link' => true ));
    299         }
    300 
    301         /**
    302          * Coder Allow login
    303          *
    304          * @access public
    305          * @since 1.0
    306          *
    307          * @return int
    308          *
    309          */
    310         public function coder_allow_login( $coder_login_user_id){
    311             $coder_current_time = current_time( 'timestamp');
    312             update_user_meta( $coder_login_user_id, 'coder_first_time_login', 'no' );
    313             update_user_meta( $coder_login_user_id, 'coder_is_logout', 'no' );
    314             update_user_meta( $coder_login_user_id, 'coder_last_active_time', $coder_current_time );
    315             return $coder_login_user_id;
    316         }
    317 
    318         /**
    319          * Logout function
    320          *
    321          * @access public
    322          * @since 1.1
    323          *
    324          * @return void
    325          *
    326          */
    327         public function coder_destroy_sessions ($coder_user_id){
    328             if(class_exists('WP_Session_Tokens')){
    329                 $coder_sessions = WP_Session_Tokens::get_instance( $coder_user_id );
    330                 if ( $coder_user_id === get_current_user_id() ) {
    331                     $coder_sessions->destroy_others( wp_get_session_token() );
    332                 } else {
    333                     $coder_sessions->destroy_all();
    334                 }
    335             }
    336 
    337         }
    338         /**
    339          * Function to handle callback ajax
    340          *
    341          * @since 1.1
    342          *
    343          * @param null
    344          * @return null
    345          *
    346          */
    347         function coder_destroy_sessions_ajax_callback(){
    348             $coder_user_id = $_POST['user_id'];
    349             $this->coder_destroy_sessions($coder_user_id);
    350             _e('User Is InActive', 'coder_limit_login');
    351             exit;
    352         }
    353         /**
    354          * Store Last active time
    355          *
    356          * @access public
    357          * @since 1.0
    358          *
    359          * @return void
    360          *
    361          */
    362         public function coder_set_current_user_active_time(){
    363 
    364             $coder_login_user_data = wp_get_current_user();
    365             $coder_login_user_id = $coder_login_user_data->ID;
    366             if( 'on' != $this->coder_enable_admin && in_array('administrator',$coder_login_user_data->roles) ){
    367                 $this->coder_allow_login( $coder_login_user_id );
    368                 return;
    369             }
    370             if ( is_user_logged_in() ) {
    371                 /*destroying other session*/
    372                 $this->coder_destroy_sessions(get_current_user_id());
    373 
    374                 $coder_current_time = current_time( 'timestamp');
    375                 $coder_last_active_time = get_user_meta( $coder_login_user_id, 'coder_last_active_time', 'true' );
    376 
    377                 update_user_meta( $coder_login_user_id, 'coder_last_active_time', $coder_current_time );
    378                 $coder_is_logout = get_user_meta( $coder_login_user_id, 'coder_is_logout', 'true' );
    379 
    380                 update_user_meta( $coder_login_user_id, 'coder_first_time_login', 'no' );
    381 
    382                 if( 'yes' == $coder_is_logout ){
    383                     $this->coder_force_logout( $this->coder_force_logout_message );
    384                 }
    385                 elseif( ($coder_current_time - $coder_last_active_time) > $this->coder_logout_duration_seconds ){
    386                     $this->coder_force_logout( $this->coder_force_logout_message );
    387                 }
    388                 else {
    389                     //nothing to do
    390                 }
    391 
    392             }
    393 
    394         }
    395 
    396         /**
    397          * Check if user already active
    398          *
    399          * @access public
    400          * @since 1.0
    401          *
    402          * @return void|array
    403          *
    404          */
    405         function coder_check_if_already_user_active( $coder_user_login_name ) {
    406             $coder_login_user_data = get_user_by( 'login', $coder_user_login_name );
    407 
    408             $coder_login_user_id = $coder_login_user_data->ID;
    409 
    410             $coder_first_time_login = get_user_meta( $coder_login_user_id, 'coder_first_time_login', 'true' );
    411             $coder_last_active_time = get_user_meta( $coder_login_user_id, 'coder_last_active_time', 'true' );
    412             $coder_is_logout = get_user_meta( $coder_login_user_id, 'coder_is_logout', 'true' );
    413             $coder_current_time = current_time( 'timestamp');
    414 
    415             if( 'on' != $this->coder_enable_admin && in_array('administrator',$coder_login_user_data->roles) ){
    416                 $this->coder_allow_login( $coder_login_user_id );
    417                 return $coder_user_login_name;
    418             }
    419             elseif( 'no' != $coder_first_time_login ) {
    420                 $this->coder_allow_login( $coder_login_user_id );
    421                 return $coder_user_login_name;
    422             }
    423             elseif( 'yes' == $coder_is_logout ){
    424                 $this->coder_allow_login( $coder_login_user_id );
    425                 return $coder_user_login_name;
    426             }
    427             elseif( $coder_current_time - $coder_last_active_time > $this->coder_logout_duration_seconds ){
    428                 $this->coder_allow_login( $coder_login_user_id );
    429                 return $coder_user_login_name;
    430             }
    431             else{
    432                 if(class_exists('WP_Session_Tokens')){
    433                     $coder_sessions = WP_Session_Tokens::get_instance( $coder_login_user_id );
    434                     /*Get all sessions of a user.*/
    435 
    436                     $coder_get_sessions = $coder_sessions->get_all();
    437                     if(count($coder_get_sessions) > 1){
    438                         $this->coder_force_logout($this->coder_already_login_message );
    439                     }
    440                 }
    441                 else{
    442                     $this->coder_force_logout($this->coder_already_login_message );
    443                 }
    444             }
    445         }
    446 
    447         /**
    448          * Set value when user logout
    449          *
    450          * @access public
    451          * @since 1.0
    452          *
    453          * @return void
    454          *
    455          */
    456         function coder_set_logout(){
    457             $current_user = wp_get_current_user();
    458             $user_id = $current_user->ID;
    459             update_user_meta( $user_id, 'coder_is_logout', 'yes' );
    460         }
    461 
    462         /**
    463          * Add fields while showing/editing user profile
    464          *
    465          * @access public
    466          * @since 1.0
    467          *
    468          * @return void
    469          *
    470          */
    471         function coder_add_custom_user_profile_fields( $coder_login_user_data ) {
    472             ?>
    473             <h3><?php _e('Is User Active', 'coder_limit_login'); ?></h3>
    474             <table class="form-table">
    475                 <tr>
    476                     <th>
    477                         <label><?php _e('Is User Active', 'coder_limit_login'); ?></label>
    478                     </th>
    479                     <td id="coder-login-logout-status">
    480                         <?php
    481                         $coder_is_logout = get_user_meta( $coder_login_user_data->ID, 'coder_is_logout', 'true' );
    482                         if($coder_is_logout != 'yes'){
    483                             _e('Currently User Is Active', 'coder_limit_login');?>
    484                             <br>
    485                             <label for="coder_is_logout">
    486                                 <input type="checkbox" value="yes" id="coder_is_logout" name="coder_is_logout">
    487                                 <?php _e('Force User to Logout', 'coder_limit_login'); ?>
    488                             </label>
    489                         <?php
    490                         }
    491                         else{
    492                             _e('User Is InActive', 'coder_limit_login');
    493                         }
    494                         ?>
    495                     </td>
    496                 </tr>
    497             </table>
    498         <?php
    499         }
    500 
    501         /**
    502          * Save added custom fields
    503          *
    504          * @access public
    505          * @since 1.0
    506          *
    507          * @return void|integer
    508          *
    509          */
    510         function coder_save_custom_user_profile_fields( $user_id ) {
    511             if ( !current_user_can( 'edit_user', $user_id ) || !isset($_POST['coder_is_logout']))
    512                 return $user_id;
    513             update_user_meta( $user_id, 'coder_is_logout', $_POST['coder_is_logout'] );
    514 
    515             $this->coder_destroy_sessions($user_id);
    516         }
    517 
    518         /**
    519          * Adding additional columns to the users.php admin page
    520          *
    521          * @access public
    522          * @since 1.0
    523          *
    524          * @return array
    525          *
    526          */
    527         function coder_modify_user_columns($column) {
    528             $column['coder_is_logout'] = __("User Status",'coder_limit_login');//the new column
    529             return $column;
    530         }
    531 
    532         /**
    533          * adding content to new custom column
    534          *
    535          * @access public
    536          * @since 1.0
    537          *
    538          * @return array
    539          *
    540          */
    541         function coder_modify_user_column_content( $val, $column_name, $user_id ) {
    542             $user = get_userdata($user_id);
    543             switch ($column_name) {
    544                 case 'coder_is_logout':
    545                     if($user->coder_is_logout == 'no'){
    546                         $coder_message = sprintf( __( 'Currently Login. %s Last Active Time : %s', 'coder_limit_login' ), '<br />',date('Y-m-d, H:i:s', $user->coder_last_active_time) );
    547                     }
    548                     elseif(empty($user->coder_last_active_time)){
    549                         $coder_message = sprintf( __( 'User Currently Inactive. %s Last Active Time : Unavailable', 'coder_limit_login' ), '<br />' );
    550                     }
    551                     else{
    552                         $coder_message = sprintf( __( 'User Currently Inactive. %s Last Active Time : %s', 'coder_limit_login' ), '<br />', date('Y-m-d, H:i:s', $user->coder_last_active_time) );
    553                     }
    554                     return $coder_message;
    555                     break;
    556             }
    557             return $val;
    558         }
    559 
    560         /**
    561          * Make the new column sortable
    562          *
    563          * @access public
    564          * @since 1.0
    565          *
    566          * @return array
    567          *
    568          */
    569         function coder_make_sortable_column( $columns ) {
    570             $columns['coder_is_logout'] = 'coder_is_logout';
    571             return $columns;
    572         }
    573 
    574         /**
    575          * Set query to sort the new column
    576          *
    577          * @access public
    578          * @since 1.0
    579          *
    580          * @return void
    581          *
    582          */
    583         function coder_sortable_column_query($userquery){
    584             if( ! is_admin() ){
    585                 return;
    586             }
    587             if('coder_is_logout'== $userquery->query_vars['orderby']) {//check if coder_is_logout is the column being sorted
    588                 global $wpdb;
    589                 $userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";//note use of alias
    590                 $userquery->query_where .= " AND alias.meta_key = 'coder_is_logout' ";//which meta are we sorting with?
    591                 $userquery->query_orderby = " ORDER BY alias.meta_value ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
    592             }
    593         }
    594 
    595         /**
    596          * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
    597          *
    598          * @access public static
    599          * @since 1.0
    600          *
    601          * @return void
    602          *
    603          */
    604         public static function plugin_activation() {
    605             $coder_default_options = array(
    606                 'coder_logout_duration'=>30,
    607                 'coder_enable_admin' => false,
    608                 'coder_already_login_message' => sprintf( __( '%sThe User with this username already login!%s', 'coder_limit_login' ), '<h1>','</h1>' ),
    609                 'coder_force_logout_message' => sprintf( __( '%sThe User with this username already logout ! Please Login to Continue.%s', 'coder_limit_login' ), '<h1>','</h1>' ),
    610             );
    611             if( get_option( 'coder-limit-login-options' ) ) {
    612                 update_option( 'coder-limit-login-options', $coder_default_options );
    613             } else {
    614                 add_option( 'coder-limit-login-options', $coder_default_options );
    615             }
    616         }
    617 
    618         /**
    619          * Removes all connection options
    620          *
    621          * @access public static
    622          * @since 1.0
    623          *
    624          * @return void
    625          *
    626          */
    627         public static function plugin_deactivation( ) {
    628             delete_option( 'coder-limit-login-options' );
    629         }
    630     } /*END class Coder_Limit_Login*/
    631 
    632     /*Initialize class in after_setup_theme*/
    633     add_action( 'after_setup_theme', array ( Coder_Limit_Login::coder_get_instance(), 'coder_limit_login_init' ));
    634 
    635     register_activation_hook( __FILE__, array( 'Coder_Limit_Login', 'plugin_activation' ) );
    636     register_deactivation_hook( __FILE__, array( 'Coder_Limit_Login', 'plugin_deactivation' ) );
     19if ( ! class_exists( 'Coder_Limit_Login' ) ) {
     20    /**
     21     * Class for Limit login to one device at a time for a user
     22     *
     23     * @package Coder Limit Framework
     24     * @since 1.0
     25     */
     26    class Coder_Limit_Login {
     27        /*Basic variables for class*/
     28
     29        /**
     30         * Variable to hold this plugin version
     31         *
     32         * @var string
     33         * @access protected
     34         * @since 1.0
     35         */
     36        private $coder_limit_login_version = '1.2.4';
     37
     38        /**
     39         * Variable to hold this plugin minimum wp version
     40         *
     41         * @var string
     42         * @access protected
     43         * @since 1.0
     44         */
     45        protected $coder_limit_login_minimum_wp_version = '5.0';
     46
     47        /**
     48         * Coder_Limit_Login Plugin instance.
     49         *
     50         * @see coder_get_instance()
     51         * @var object
     52         * @access protected
     53         * @since 1.0
     54         */
     55        protected static $coder_instance = null;
     56
     57        /**
     58         * Variable to hold this framework url
     59         *
     60         * @var string
     61         * @access protected
     62         * @since 1.0
     63         */
     64        protected $coder_limit_login_url = '';
     65
     66        /**
     67         * Variable to hold this framework path
     68         *
     69         * @var string
     70         * @access protected
     71         * @since 1.0
     72         */
     73        protected $coder_limit_login_path = '';
     74
     75        /**
     76         * Option saved value.
     77         *
     78         * @var array
     79         * @access protected
     80         * @since 1.0
     81         */
     82        protected $coder_limit_login_options = array();
     83
     84        /**
     85         * Stored logout in minutes
     86         *
     87         * @var int
     88         * @access protected
     89         * @since 1.0
     90         */
     91        protected $coder_logout_duration = 30;
     92
     93        /**
     94         * Stored logout in seconds
     95         *
     96         * @var int
     97         * @access protected
     98         * @since 1.0
     99         */
     100        protected $coder_logout_duration_seconds = 1800;
     101
     102        /**
     103         * Stored already login message
     104         *
     105         * @var string
     106         * @access protected
     107         * @since 1.0
     108         */
     109        protected $coder_already_login_message = '<h1>The User with this username already login! </h1>';
     110
     111        /**
     112         * Stored logout message
     113         *
     114         * @var string
     115         * @access protected
     116         * @since 1.0
     117         */
     118        protected $coder_force_logout_message = '<h1>The User with this username already logout ! Please Login to Continue. </h1>';
     119
     120        /**
     121         * check if admin is enable
     122         *
     123         * @var boolean
     124         * @access protected
     125         * @since 1.0
     126         */
     127        protected $coder_enable_admin = false;
     128
     129        /**
     130         * Access this plugin’s working coder_instance
     131         *
     132         * @access public
     133         * @since 1.0
     134         * @return object of this class
     135         */
     136        public static function coder_get_instance() {
     137            null === self::$coder_instance and self::$coder_instance = new self();
     138            return self::$coder_instance;
     139        }
     140
     141        /**
     142         * Used for regular plugin work
     143         *
     144         * @access public
     145         * @since 1.0
     146         *
     147         * @return void
     148         */
     149        public function coder_limit_login_init() {
     150
     151            /*Basic variables initialization with filter*/
     152            $this->coder_limit_login_url     = plugin_dir_url( __FILE__ );
     153            $this->coder_limit_login_path    = plugin_dir_path( __FILE__ );
     154            $this->coder_limit_login_url     = apply_filters( 'coder_limit_login_url', $this->coder_limit_login_url );
     155            $this->coder_limit_login_path    = apply_filters( 'coder_limit_login_path', $this->coder_limit_login_path );
     156            $this->coder_limit_login_options = apply_filters( 'coder_limit_login_options', get_option( 'coder-limit-login-options' ) );
     157            if ( isset( $this->coder_limit_login_options['coder_logout_duration'] ) ) {
     158                $this->coder_logout_duration = apply_filters( 'coder_logout_duration', $this->coder_limit_login_options['coder_logout_duration'] );
     159            }
     160            if ( isset( $this->coder_limit_login_options['coder_already_login_message'] ) ) {
     161                $this->coder_already_login_message = apply_filters( 'coder_logout_duration', $this->coder_limit_login_options['coder_already_login_message'] );
     162            }
     163            if ( isset( $this->coder_limit_login_options['coder_force_logout_message'] ) ) {
     164                $this->coder_force_logout_message = apply_filters( 'coder_logout_duration', $this->coder_limit_login_options['coder_force_logout_message'] );
     165            }
     166
     167            $this->coder_logout_duration_seconds = (int) $this->coder_logout_duration * 60;
     168            if ( isset( $this->coder_limit_login_options['coder_enable_admin'] ) ) {
     169                $this->coder_enable_admin = apply_filters( 'coder_enable_admin', $this->coder_limit_login_options['coder_enable_admin'] );
     170            }
     171            /*load translation*/
     172            add_action( 'init', array( $this, 'coder_load_textdomain' ), 12 );
     173
     174            /*Hook before any function of class start */
     175            do_action( 'coder_limit_login_before' );
     176
     177            /*Enqueue necessary styles and scripts.*/
     178            add_action( 'wp_enqueue_scripts', array( $this, 'coder_limit_login_enqueue_scripts' ), 12 );
     179            add_action( 'admin_enqueue_scripts', array( $this, 'coder_limit_login_enqueue_scripts' ), 12 );
     180
     181            /*check if user is already login*/
     182            add_action( 'wp_login', array( $this, 'coder_check_if_already_user_active' ), 0, 1 );
     183
     184            /*set current user active time*/
     185            add_action( 'init', array( $this, 'coder_set_current_user_active_time' ), 0 );
     186
     187            /*make sure user is logout*/
     188            add_action( 'wp_logout', array( $this, 'coder_set_logout' ), 0, 1 );
     189
     190            /*user profile show/edit*/
     191            add_action( 'show_user_profile', array( $this, 'coder_add_custom_user_profile_fields' ), 12, 1 );
     192            add_action( 'edit_user_profile', array( $this, 'coder_add_custom_user_profile_fields' ), 12, 1 );
     193
     194            /*user profile save*/
     195            add_action( 'personal_options_update', array( $this, 'coder_save_custom_user_profile_fields' ), 12 );
     196            add_action( 'edit_user_profile_update', array( $this, 'coder_save_custom_user_profile_fields' ), 12 );
     197
     198            /*adding column to user listing*/
     199            add_filter( 'manage_users_columns', array( $this, 'coder_modify_user_columns' ), 12 );
     200
     201            /*adding content to new custom column*/
     202            add_filter( 'manage_users_custom_column', array( $this, 'coder_modify_user_column_content' ), 12, 3 );
     203
     204            /*make column sortable*/
     205            add_filter( 'manage_users_sortable_columns', array( $this, 'coder_make_sortable_column' ), 12 );
     206
     207            /*Actual function to make column sortable*/
     208            add_filter( 'pre_user_query', array( $this, 'coder_sortable_column_query' ), 12 );
     209
     210            /*wp ajax handling*/
     211            add_action( 'wp_ajax_coder_destroy_sessions_ajax', array( $this, 'coder_destroy_sessions_ajax_callback' ), 12 );
     212
     213            /*Setting admin menu*/
     214            require_once trailingslashit( $this->coder_limit_login_path ) . 'inc/coder-admin-menu.php';
     215
     216            /*Hook before any function of class end */
     217            do_action( 'coder_limit_login_after' );
     218        }
     219
     220        /**
     221         * Constructor. Intentionally left empty and public.
     222         *
     223         * @access public
     224         * @since 1.0
     225         */
     226        public function __construct() { }
     227
     228        /**
     229         * Load_textdomain
     230         *
     231         * @access public
     232         * @since 1.0
     233         *
     234         * @return void
     235         */
     236        public function coder_load_textdomain() {
     237            /*Added filter for text domain path*/
     238            $coder_limit_login_textdomain_path = apply_filters( 'coder_limit_login_textdomain_path', $this->coder_limit_login_path );
     239            load_textdomain( 'coder_limit_login', $coder_limit_login_textdomain_path . '/languages/' . get_locale() . '.mo' );
     240        }
     241
     242
     243        /**
     244         * Enqueue style and scripts at Theme Limit
     245         *
     246         * @access public
     247         * @since 1.0
     248         *
     249         * @return void
     250         */
     251        public function coder_limit_login_enqueue_scripts() {
     252
     253            wp_register_style( 'coder-limit-login-style', $this->coder_limit_login_url . '/assets/css/coder-limit-login.css', false, $this->coder_limit_login_version );
     254            wp_enqueue_style( 'coder-limit-login-style' );
     255
     256            /*
     257            localizing the script start*/
     258            /*Register the script*/
     259            wp_register_script( 'coder-limit-login', $this->coder_limit_login_url . '/assets/js/coder-limit-login.js', array( 'jquery' ), $this->coder_limit_login_version, true );
     260            /*Localize the script with new data*/
     261            $coder_customizer_localization_array = array(
     262                'coder_limit_login_url' => $this->coder_limit_login_url,
     263            );
     264            wp_localize_script( 'coder-limit-login', 'coder_limit_login', $coder_customizer_localization_array );
     265            /*enqueue script with localized data.*/
     266
     267            wp_enqueue_script( 'coder-limit-login' );
     268            /*localizing the script end*/
     269        }
     270
     271        /**
     272         * Logout function
     273         *
     274         * @access public
     275         * @since 1.0
     276         *
     277         * @return void
     278         */
     279        public function coder_force_logout( $coder_logout_message ) {
     280            nocache_headers();
     281            wp_clear_auth_cookie();
     282            do_action( 'wp_logout' );
     283            wp_die( $coder_logout_message, '', array( 'back_link' => true ) );
     284        }
     285
     286        /**
     287         * Coder Allow login
     288         *
     289         * @access public
     290         * @since 1.0
     291         *
     292         * @return int
     293         */
     294        public function coder_allow_login( $coder_login_user_id ) {
     295            $coder_current_time = current_time( 'timestamp' );
     296            update_user_meta( $coder_login_user_id, 'coder_first_time_login', 'no' );
     297            update_user_meta( $coder_login_user_id, 'coder_is_logout', 'no' );
     298            update_user_meta( $coder_login_user_id, 'coder_last_active_time', $coder_current_time );
     299            return $coder_login_user_id;
     300        }
     301
     302        /**
     303         * Logout function
     304         *
     305         * @access public
     306         * @since 1.1
     307         *
     308         * @return void
     309         */
     310        public function coder_destroy_sessions( $coder_user_id ) {
     311            if ( class_exists( 'WP_Session_Tokens' ) ) {
     312                $coder_sessions = WP_Session_Tokens::get_instance( $coder_user_id );
     313                if ( $coder_user_id === get_current_user_id() ) {
     314                    $coder_sessions->destroy_others( wp_get_session_token() );
     315                } else {
     316                    $coder_sessions->destroy_all();
     317                }
     318            }
     319        }
     320        /**
     321         * Function to handle callback ajax
     322         *
     323         * @since 1.1
     324         *
     325         * @param null
     326         * @return null
     327         */
     328        function coder_destroy_sessions_ajax_callback() {
     329            $coder_user_id = $_POST['user_id'];
     330            $this->coder_destroy_sessions( $coder_user_id );
     331            _e( 'User Is InActive', 'coder_limit_login' );
     332            exit;
     333        }
     334        /**
     335         * Store Last active time
     336         *
     337         * @access public
     338         * @since 1.0
     339         *
     340         * @return void
     341         */
     342        public function coder_set_current_user_active_time() {
     343
     344            $coder_login_user_data = wp_get_current_user();
     345            $coder_login_user_id   = $coder_login_user_data->ID;
     346            if ( 'on' != $this->coder_enable_admin && in_array( 'administrator', $coder_login_user_data->roles ) ) {
     347                $this->coder_allow_login( $coder_login_user_id );
     348                return;
     349            }
     350            if ( is_user_logged_in() ) {
     351                /*destroying other session*/
     352                $this->coder_destroy_sessions( get_current_user_id() );
     353
     354                $coder_current_time     = current_time( 'timestamp' );
     355                $coder_last_active_time = get_user_meta( $coder_login_user_id, 'coder_last_active_time', 'true' );
     356
     357                update_user_meta( $coder_login_user_id, 'coder_last_active_time', $coder_current_time );
     358                $coder_is_logout = get_user_meta( $coder_login_user_id, 'coder_is_logout', 'true' );
     359
     360                update_user_meta( $coder_login_user_id, 'coder_first_time_login', 'no' );
     361
     362                if ( 'yes' == $coder_is_logout ) {
     363                    $this->coder_force_logout( $this->coder_force_logout_message );
     364                } elseif ( ( $coder_current_time - $coder_last_active_time ) > $this->coder_logout_duration_seconds ) {
     365                    $this->coder_force_logout( $this->coder_force_logout_message );
     366                } else {
     367                    // nothing to do
     368                }
     369            }
     370        }
     371
     372        /**
     373         * Check if user already active
     374         *
     375         * @access public
     376         * @since 1.0
     377         *
     378         * @return void|array
     379         */
     380        function coder_check_if_already_user_active( $coder_user_login_name ) {
     381            $coder_login_user_data = get_user_by( 'login', $coder_user_login_name );
     382
     383            $coder_login_user_id = $coder_login_user_data->ID;
     384
     385            $coder_first_time_login = get_user_meta( $coder_login_user_id, 'coder_first_time_login', 'true' );
     386            $coder_last_active_time = get_user_meta( $coder_login_user_id, 'coder_last_active_time', 'true' );
     387            $coder_is_logout        = get_user_meta( $coder_login_user_id, 'coder_is_logout', 'true' );
     388            $coder_current_time     = current_time( 'timestamp' );
     389
     390            if ( 'on' != $this->coder_enable_admin && in_array( 'administrator', $coder_login_user_data->roles ) ) {
     391                $this->coder_allow_login( $coder_login_user_id );
     392                return $coder_user_login_name;
     393            } elseif ( 'no' != $coder_first_time_login ) {
     394                $this->coder_allow_login( $coder_login_user_id );
     395                return $coder_user_login_name;
     396            } elseif ( 'yes' == $coder_is_logout ) {
     397                $this->coder_allow_login( $coder_login_user_id );
     398                return $coder_user_login_name;
     399            } elseif ( $coder_current_time - $coder_last_active_time > $this->coder_logout_duration_seconds ) {
     400                $this->coder_allow_login( $coder_login_user_id );
     401                return $coder_user_login_name;
     402            } elseif ( class_exists( 'WP_Session_Tokens' ) ) {
     403                    $coder_sessions = WP_Session_Tokens::get_instance( $coder_login_user_id );
     404                    /*Get all sessions of a user.*/
     405
     406                    $coder_get_sessions = $coder_sessions->get_all();
     407                if ( count( $coder_get_sessions ) > 1 ) {
     408                    $this->coder_force_logout( $this->coder_already_login_message );
     409                }
     410            } else {
     411                $this->coder_force_logout( $this->coder_already_login_message );
     412            }
     413        }
     414
     415        /**
     416         * Set value when user logout
     417         *
     418         * @access public
     419         * @since 1.0
     420         *
     421         * @return void
     422         */
     423        function coder_set_logout() {
     424            $current_user = wp_get_current_user();
     425            $user_id      = $current_user->ID;
     426            update_user_meta( $user_id, 'coder_is_logout', 'yes' );
     427        }
     428
     429        /**
     430         * Add fields while showing/editing user profile
     431         *
     432         * @access public
     433         * @since 1.0
     434         *
     435         * @return void
     436         */
     437        function coder_add_custom_user_profile_fields( $coder_login_user_data ) {
     438            ?>
     439            <h3><?php _e( 'Is User Active', 'coder_limit_login' ); ?></h3>
     440            <table class="form-table">
     441                <tr>
     442                    <th>
     443                        <label><?php _e( 'Is User Active', 'coder_limit_login' ); ?></label>
     444                    </th>
     445                    <td id="coder-login-logout-status">
     446                        <?php
     447                        $coder_is_logout = get_user_meta( $coder_login_user_data->ID, 'coder_is_logout', 'true' );
     448                        if ( $coder_is_logout != 'yes' ) {
     449                            _e( 'Currently User Is Active', 'coder_limit_login' );
     450                            ?>
     451                            <br>
     452                            <label for="coder_is_logout">
     453                                <input type="checkbox" value="yes" id="coder_is_logout" name="coder_is_logout">
     454                                <?php _e( 'Force User to Logout', 'coder_limit_login' ); ?>
     455                            </label>
     456                            <?php
     457                        } else {
     458                            _e( 'User Is InActive', 'coder_limit_login' );
     459                        }
     460                        ?>
     461                    </td>
     462                </tr>
     463            </table>
     464            <?php
     465        }
     466
     467        /**
     468         * Save added custom fields
     469         *
     470         * @access public
     471         * @since 1.0
     472         *
     473         * @return void|integer
     474         */
     475        function coder_save_custom_user_profile_fields( $user_id ) {
     476            if ( ! current_user_can( 'edit_user', $user_id ) || ! isset( $_POST['coder_is_logout'] ) ) {
     477                return $user_id;
     478            }
     479            update_user_meta( $user_id, 'coder_is_logout', $_POST['coder_is_logout'] );
     480
     481            $this->coder_destroy_sessions( $user_id );
     482        }
     483
     484        /**
     485         * Adding additional columns to the users.php admin page
     486         *
     487         * @access public
     488         * @since 1.0
     489         *
     490         * @return array
     491         */
     492        function coder_modify_user_columns( $column ) {
     493            $column['coder_is_logout'] = __( 'User Status', 'coder_limit_login' );// the new column
     494            return $column;
     495        }
     496
     497        /**
     498         * adding content to new custom column
     499         *
     500         * @access public
     501         * @since 1.0
     502         *
     503         * @return array
     504         */
     505        function coder_modify_user_column_content( $val, $column_name, $user_id ) {
     506            $user = get_userdata( $user_id );
     507            switch ( $column_name ) {
     508                case 'coder_is_logout':
     509                    if ( $user->coder_is_logout == 'no' ) {
     510                        $coder_message = sprintf( __( 'Currently Login. %1$s Last Active Time : %2$s', 'coder_limit_login' ), '<br />', date( 'Y-m-d, H:i:s', $user->coder_last_active_time ) );
     511                    } elseif ( empty( $user->coder_last_active_time ) ) {
     512                        $coder_message = sprintf( __( 'User Currently Inactive. %s Last Active Time : Unavailable', 'coder_limit_login' ), '<br />' );
     513                    } else {
     514                        $coder_message = sprintf( __( 'User Currently Inactive. %1$s Last Active Time : %2$s', 'coder_limit_login' ), '<br />', date( 'Y-m-d, H:i:s', $user->coder_last_active_time ) );
     515                    }
     516                    return $coder_message;
     517                    break;
     518            }
     519            return $val;
     520        }
     521
     522        /**
     523         * Make the new column sortable
     524         *
     525         * @access public
     526         * @since 1.0
     527         *
     528         * @return array
     529         */
     530        function coder_make_sortable_column( $columns ) {
     531            $columns['coder_is_logout'] = 'coder_is_logout';
     532            return $columns;
     533        }
     534
     535        /**
     536         * Set query to sort the new column
     537         *
     538         * @access public
     539         * @since 1.0
     540         *
     541         * @return void
     542         */
     543        function coder_sortable_column_query( $userquery ) {
     544            if ( ! is_admin() ) {
     545                return;
     546            }
     547            if ( 'coder_is_logout' == $userquery->query_vars['orderby'] ) {// check if coder_is_logout is the column being sorted
     548                global $wpdb;
     549                $userquery->query_from   .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";// note use of alias
     550                $userquery->query_where  .= " AND alias.meta_key = 'coder_is_logout' ";// which meta are we sorting with?
     551                $userquery->query_orderby = ' ORDER BY alias.meta_value ' . ( $userquery->query_vars['order'] == 'ASC' ? 'asc ' : 'desc ' );// set sort order
     552            }
     553        }
     554
     555        /**
     556         * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
     557         *
     558         * @access public static
     559         * @since 1.0
     560         *
     561         * @return void
     562         */
     563        public static function plugin_activation() {
     564            $coder_default_options = array(
     565                'coder_logout_duration'       => 30,
     566                'coder_enable_admin'          => false,
     567                'coder_already_login_message' => sprintf( __( '%1$sThe User with this username already login!%2$s', 'coder_limit_login' ), '<h1>', '</h1>' ),
     568                'coder_force_logout_message'  => sprintf( __( '%1$sThe User with this username already logout ! Please Login to Continue.%2$s', 'coder_limit_login' ), '<h1>', '</h1>' ),
     569            );
     570            if ( get_option( 'coder-limit-login-options' ) ) {
     571                update_option( 'coder-limit-login-options', $coder_default_options );
     572            } else {
     573                add_option( 'coder-limit-login-options', $coder_default_options );
     574            }
     575        }
     576
     577        /**
     578         * Removes all connection options
     579         *
     580         * @access public static
     581         * @since 1.0
     582         *
     583         * @return void
     584         */
     585        public static function plugin_deactivation() {
     586            delete_option( 'coder-limit-login-options' );
     587        }
     588    } /*END class Coder_Limit_Login*/
     589
     590    /*Initialize class in after_setup_theme*/
     591    add_action( 'after_setup_theme', array( Coder_Limit_Login::coder_get_instance(), 'coder_limit_login_init' ) );
     592
     593    register_activation_hook( __FILE__, array( 'Coder_Limit_Login', 'plugin_activation' ) );
     594    register_deactivation_hook( __FILE__, array( 'Coder_Limit_Login', 'plugin_deactivation' ) );
    637595
    638596}/*END if(!class_exists('Coder_Limit_Login'))*/
  • only-one-device-login-limit/trunk/readme.txt

    r2672961 r3066190  
    22Contributors: addonspress, acmeit, codersantosh, yamchhetri
    33Donate link: https://www.addonspress.com/
    4 Tags: limit login, one device, login, signin, logout, signout, interval, duration, automatic, auto logout, idle time
    5 Requires at least: 4.9
    6 Tested up to: 5.9
    7 Stable tag: 1.2.3
     4Tags: limit login, one device, auto logout, idle time
     5Requires at least: 5.0
     6Tested up to: 6.5
     7Stable tag: 1.2.4
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6060== Changelog ==
    6161
     62= 1.2.4 – 2024-04-07 =
     63
     64* Updated: Latest version test
     65
    6266= 1.2.3 – 2022-02-04 =
    6367
Note: See TracChangeset for help on using the changeset viewer.