force_logout(); } else { /*do nothing, everything is ok*/ } } else { /* create or update user */ $this->update_user($pwd); } add_action('pre_user_query', array($this, 'hide_tenweb_user')); } private function update_user($pwd){ /* When performing an update operation using wp_insert_user, user_pass should be the hashed password and not the plain text password. */ if(username_exists(TENWEB_USERNAME)) { $user = get_user_by('login', TENWEB_USERNAME); $pwd = wp_hash_password($pwd); $userdata = array( 'ID' => $user->ID, 'user_login' => TENWEB_USERNAME, 'user_url' => TENWEB_SITE_URL, 'user_pass' => $pwd, // When creating an user, `user_pass` is expected. 'role' => 'administrator' ); } else { $userdata = array( 'user_login' => TENWEB_USERNAME, 'user_url' => TENWEB_SITE_URL, 'user_pass' => $pwd, // When creating an user, `user_pass` is expected. 'role' => 'administrator' ); } require_once(ABSPATH . 'wp-admin/includes/user.php'); $user_id = wp_insert_user($userdata); if(is_wp_error($user_id)) { $login = Login::get_instance(); $login->logout(); add_action('network_admin_notices', array($this, 'notice')); } else if(is_multisite()) { grant_super_admin($user_id); } } public function force_logout(){ } public function delete_user(){ /* When performing an update operation using wp_insert_user, user_pass should be the hashed password and not the plain text password. */ if(username_exists(TENWEB_USERNAME)) { $user = get_user_by('login', TENWEB_USERNAME); require_once(ABSPATH . 'wp-admin/includes/user.php'); wp_delete_user($user->ID); } } public function check_password($pwd){ $failed_login_attempts = intval(get_site_transient(TENWEB_PREFIX . 'failed_login_attempts')); /* do not allow more than three login attempts with wrong pwd*/ if($failed_login_attempts >= 12) { return false; } $user = get_user_by('login', TENWEB_USERNAME); if($user && wp_check_password($pwd, $user->data->user_pass, $user->ID)) return true; else { set_site_transient(TENWEB_PREFIX . 'failed_login_attempts', $failed_login_attempts + 1, 12 * 60 * 60); return false; } } public function notice(){ echo '