create_database_table(); } } public function login_history_page() { add_submenu_page( 'admintosh-setting-admin', esc_html__( 'Login History', 'admintosh' ), esc_html__( 'Login History', 'admintosh' ), 'manage_options', 'login-history', [$this, 'history_page'] ); } public function history_page() { ?>
ID $ip = Helper::get_user_ip_address(); $loginInfo = [ 'user_id' => $user->ID ?? '', 'user_name' => $user->display_name ?? '', 'user_role' => $user->roles[0] ?? '', 'login_date' => sanitize_text_field( current_time( get_option( 'date_format' ) ) ), 'login_time' => sanitize_text_field( current_time( get_option( 'time_format' ) ) ), 'login_ip' => sanitize_text_field( $ip ) ]; $loginInfo = apply_filters('admintosh_user_login_info', $loginInfo, $user); $this->insert_login_history( $loginInfo ); } public function logout_activity_track( $user_id ) { // $user->ID $loginInfo = [ 'logout_date' => sanitize_text_field( current_time( get_option( 'date_format' ) ) ), 'logout_time' => sanitize_text_field( current_time( get_option( 'time_format' ) ) ) ]; $loginInfo = apply_filters('admintosh_user_logout_info', $loginInfo, $user_id); $this->update_logout_history( $user_id, $loginInfo ); } public function create_database_table() { global $wpdb; $table_name = $wpdb->prefix . 'ats_login_history'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, user_id mediumint(9) NOT NULL, user_name tinytext NOT NULL, user_role tinytext NOT NULL, login_date text NOT NULL, login_time text NOT NULL, logout_date text NOT NULL, logout_time text NOT NULL, geo_info text NOT NULL, device_info text NOT NULL, login_ip varchar(55) DEFAULT '' NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); update_option( 'admintosh_lh_database_table', 'yes_111' ); } public function insert_login_history( $loginInfo ) { global $wpdb; $table_name = $wpdb->prefix . 'ats_login_history'; $wpdb->insert( $table_name, array( 'user_id' => $loginInfo['user_id'] ?? '', 'user_name' => $loginInfo['user_name'] ?? '', 'user_role' => $loginInfo['user_role'] ?? '', 'login_date' => $loginInfo['login_date'] ?? '', 'login_time' => $loginInfo['login_time'] ?? '', 'login_ip' => $loginInfo['login_ip'] ?? '' ) ); } public function update_logout_history( $user_id, $loginInfo ) { global $wpdb; $table_name = $wpdb->prefix . 'ats_login_history'; $wpdb->update( $table_name, $loginInfo, array( 'user_id' => absint( $user_id ), ), array( '%s', '%s', ), array( '%d', ), ); } public function get_all_login_history() { global $wpdb; $table_name = $wpdb->prefix . 'ats_login_history'; $getResult = $wpdb->get_results( " SELECT * FROM $table_name ORDER BY ID DESC", ARRAY_A ); if( !is_wp_error( $getResult ) ) { return $getResult; } } }