WP_PLUGIN 自動ログアウト拡張

初回コミット
This commit is contained in:
2021-06-29 15:49:09 +09:00
commit 6d4b4c813d
13 changed files with 535 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
/.idea/
+26
View File
@@ -0,0 +1,26 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'al_ext_config' ) ) {
class al_ext_config {
const PLUGIN_NAME = 'auto-logout-extended';
const AL_EXT_CLASS_DIR = __DIR__ . '/class/';
const AL_EXT_TRAIT_DIR = __DIR__ . '/trait/';
const AL_EXT_DEFAULT_LOGOUT_TYPE = 1;
const AL_EXT_DEFAULT_EXPIRE_DATE = 14;
const AL_EXT_DEFAULT_EXPIRE_TIME = 60;
const AL_EXT_TEMPLATE_DIR = __DIR__ . '/template/';
const AL_EXT_REMEMBER_COOKIE = 'al_ext_remember';
const AL_EXT_TMP_COOKIE = 'al_ext_logged_in';
public static function get_default_logout_url() {
return home_url( 'wp-admin' );
}
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
/*
Plugin Name: Auto Logout Extended
Plugin URI: https://develop.n-k-y.net/wordpress/wp_plugin/auto-logout-extended
Author: NBK45
Author URI: https://develop.n-k-y.net
Description: 自動ログアウトとログイン状態保存を拡張します。一定時間無操作時に自動ログアウトする機能と(ログアウトせず)ブラウザ終了した場合にログイン状態の保存も可能にするプラグインです。
Version: 1.0.0
License: GPLv2
*/
/* Copyright 2021 NBK45 (email : nbk.develop@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once __DIR__ . '/al_ext_config.php';
require_once al_ext_config::AL_EXT_CLASS_DIR . 'class.al_ext_setting.php';
require_once al_ext_config::AL_EXT_CLASS_DIR . 'class.al_ext.php';
add_action( 'admin_enqueue_scripts', function () {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'al_ext_style', $plugin_url . 'css/al_ext.css' );
wp_enqueue_script( 'al_ext_js', $plugin_url . 'js/al_ext.js', 'jquery' );
} );
new AL_EXT_SETTING;
if ( strpos( filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ), al_ext_config::PLUGIN_NAME ) === false ) {
new AL_EXT;
}
+119
View File
@@ -0,0 +1,119 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'AL_EXT' ) ) {
include_once al_ext_config::AL_EXT_TRAIT_DIR . 'al_ext_trait.php';
include_once al_ext_config::AL_EXT_TRAIT_DIR . 'al_ext_force_trait.php';
class AL_EXT {
use al_ext_trait, al_ext_force_trait;
private $options;
public function __construct() {
$this->options = get_option( '_al_ext' );
if ( $this->options['type'] == 1 ) {
$this->normal_expire();
} elseif ( $this->options['type'] == 2 ) {
$this->force_logout();
} elseif ( $this->options['type'] = 3 ) {
$this->set_extend_expire();
}
}
public function normal_expire() {
add_filter( 'auth_cookie_expiration', array( $this, 'login_expire_change' ) );
add_action( 'wp_logout', array( $this, 'delete_al_ext_cookie' ) );
}
public function force_logout() {
add_filter( 'auth_cookie_expiration', array( $this, 'login_expire_change' ) );
add_action( 'after_setup_theme', array( $this, 'set_force_al_ext_cookie' ) );
add_action( 'init', array( $this, 'can_logged_in_extend' ) );
add_action( 'wp_login', array( $this, 'my_force_login' ) );
add_action( 'wp_logout', array( $this, 'delete_al_ext_cookie' ) );
}
public function set_extend_expire() {
add_filter( 'auth_cookie_expiration', array( $this, 'login_expire_change' ) );
add_action( 'after_setup_theme', array( $this, 'set_al_ext_cookie' ) );
add_action( 'init', array( $this, 'can_logged_in_extend' ) );
add_action( 'wp_login', array( $this, 'my_login' ) );
add_action( 'wp_logout', array( $this, 'delete_al_ext_cookie' ) );
}
/**
*
* ログイン期間の設定
*
* @param $expire
*
* @return float|int
*/
public function login_expire_change( $expire ) {
if ( isset( $this->options['expire_date'] ) ) {
return 60 * 60 * 24 * $this->options['expire_date'];
}
return 60 * 60 * 24 * al_ext_config::AL_EXT_DEFAULT_EXPIRE_DATE;
}
/**
* ログインチェック
* @throws Exception
*/
public function can_logged_in_extend() {
if ( isset( $_COOKIE[ al_ext_config::AL_EXT_TMP_COOKIE ] ) && is_user_logged_in() ) {
$prev_time = new DateTime( date( 'Y-m-d H:i:s', $_COOKIE[ al_ext_config::AL_EXT_TMP_COOKIE ] ) );
$current_time = new DateTime( date( 'Y-m-d H:i:s' ) );
$diff = $prev_time->diff( $current_time );
$time_diff = $diff->days * 24 * 60 + $diff->h * 60 + $diff->i * 60 + $diff->s;
$expire = $this->set_force_expire_time();
if ( $time_diff > $expire ) {
wp_logout();
exit();
} else {
$this->create_tmp_cookie();
}
}
}
/**
* 拡張ログアウトの一時Cookieと確認用Cookieを削除
*/
public function delete_al_ext_cookie() {
setcookie( al_ext_config::AL_EXT_REMEMBER_COOKIE, '', time() - 30 );
setcookie( al_ext_config::AL_EXT_TMP_COOKIE, '', time() - 30 );
if ( ! empty( $this->options['force_logout_url'] ) ) {
wp_redirect( esc_url( $this->options['force_logout_url'] ) );
} else {
wp_redirect( esc_url( al_ext_config::get_default_logout_url() ) );
}
}
private function set_force_expire_time() {
if ( isset( $this->options['force_logout'] ) ) {
return $this->options['force_logout'] * 60;
}
return 60 * al_ext_config::AL_EXT_DEFAULT_EXPIRE_TIME;
}
private function create_tmp_cookie() {
//バックグラウンドのHeartbeat APIが実行されたときは一時Cookieを更新しない
if ( ! wp_doing_ajax() ) {
$current_time = strtotime( date( 'Y/m/d H:i:s' ) );
setcookie( al_ext_config::AL_EXT_TMP_COOKIE, $current_time, 0, '/' );
}
}
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'AL_EXT_SETTING' ) ) {
class AL_EXT_SETTING {
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_pages' ) );
}
public function add_pages() {
add_menu_page(
'ログアウト拡張',
'Auto Logout Extended',
'level_8',
__FILE__,
array(
$this,
'show_option_page'
),
''
);
}
public function show_option_page() {
$update_option = filter_input( INPUT_POST, '_al_ext', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
if ( ! empty( $update_option ) ) {
check_admin_referer( 'al_ext_options', '_al_ext_nonce' );
update_option( '_al_ext', $update_option );
include al_ext_config::AL_EXT_TEMPLATE_DIR . 'success.php';
}
$opt = get_option( '_al_ext' );
$al_ext_type = $this->set_al_ext_parameter( $opt, 'type', al_ext_config::AL_EXT_DEFAULT_LOGOUT_TYPE );
$expire_date = $this->set_al_ext_parameter( $opt, 'expire_date', al_ext_config::AL_EXT_DEFAULT_EXPIRE_DATE );
$force_logout = $this->set_al_ext_parameter( $opt, 'force_logout', al_ext_config::AL_EXT_DEFAULT_EXPIRE_TIME );
$force_logout_url = $this->set_al_ext_parameter( $opt, 'force_logout_url', '' );
$default_logout_url = al_ext_config::get_default_logout_url();
include al_ext_config::AL_EXT_TEMPLATE_DIR . 'al_ext_form.php';
}
private function set_al_ext_parameter( $opt, $target, $default ) {
if ( isset( $opt[ $target ] ) && ! empty( $opt[ $target ] ) ) {
return $opt[ $target ];
} else {
return $default;
}
}
}
}
+34
View File
@@ -0,0 +1,34 @@
.logout-type {
display: flex;
padding: 1em 0;
border-bottom: 1px solid #ccc;
}
.logout-type:first-of-type {
padding-top: 5px;
}
.logout-type:last-of-type {
border-bottom: none;
}
.logout-type label {
width: 200px;
}
.logout-type .logout-exp {
width: calc(100% - 200px);
}
.logout-exp-alert {
margin-left: 200px;
padding-top: .5em;
}
.logout-redirect-url {
width: 60%;
}
.logout-redirect-url::placeholder {
color: #999;
}
+35
View File
@@ -0,0 +1,35 @@
jQuery(function ($) {
change_al_ext_expire();
function change_al_ext_expire() {
let my_type = $('.logout-type_radio');
let expire_date = $('.expire-date');
let force_logout = $('.force-logout');
my_type.each(function () {
if ($(this).prop('checked') == true) {
if ($(this).val() == 2) {
expire_date.prop('disabled', true);
}
if ($(this).val() == 1) {
force_logout.prop('disabled', true);
}
}
});
my_type.click(function () {
if ($(this).val() == 1) {
force_logout.prop('disabled', true);
} else {
force_logout.prop('disabled', false);
}
if ($(this).val() == 2) {
expire_date.prop('disabled', true);
} else {
expire_date.prop('disabled', false);
}
})
}
});
+57
View File
@@ -0,0 +1,57 @@
=== Auto Logout Extended ===
Contributors: NBK45
Tags: logout,rememberme
Requires at least: 4.9
Tested up to: 5.7.2
Requires PHP: 7.0
Stable tag: 1.1.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
自動ログアウトとログイン状態保存を拡張するプラグイン。
== Description ==
自動ログアウトとログイン状態保存を拡張します。
一定時間無操作のときに自動でログアウトする設定と(ログアウトせず)ブラウザ終了した場合のログイン状態の保存期間変更が設定可能です。
= 仕様 =
自動ログアウトの時間設定:分単位で設定
ログイン状態保存期間設定;日単位で設定
ログアウトタイプ設定
1)標準
ログイン画面の「ログイン状態を保存」をチェックした場合、「ログイン状態の保存期間」で設定した期間ログイン状態が保存されます。
2)自動ログアウト
ログイン画面の「ログイン状態を保存」をチェックした場合、操作(画面遷移)後「自動ログアウト時間」で設定分間ログイン状態が保存されます。
「自動ログアウト時間」を超えて操作(画面遷移)が無い場合、自動ログアウトします。
3)拡張ログアウト
ログイン画面の「ログイン状態を保存」をチェックした場合、「ログイン状態の保存期間」で設定した期間ログイン状態が保存されます。
「自動ログアウト時間」を超えて操作(画面遷移)が無い場合、自動ログアウトします。
「自動ログアウト時間」内にブラウザ終了した場合、「ログイン状態の保存期間」で設定した期間ログイン状態が保存されます。
== Installation ==
= 自動インストール =
1. プラグインの検索フィールドより「Auto Logout Extended」と入力し、"プラグインの検索"をクリックします。
2. 当プラグインを見つけたら、"今すぐインストール"をクリックしてインストールし、プラグインを有効化してください。
= 手動インストール =
1. プラグインをダウンロードします。
2. プラグインフォルダ内にアップロードし、管理画面よりプラグインを有効化してください。
== Frequently Asked Questions ==
== Screenshots ==
== Changelog ==
== Upgrade Notice ==
No information
+70
View File
@@ -0,0 +1,70 @@
<?php if ( isset( $al_ext_type, $expire_date, $force_logout, $force_logout_url, $default_logout_url ) ): ?>
<div class="wrap">
<h2>ログアウト拡張</h2>
<p>「ログイン状態を保存する」がチェック時のログアウト機能を拡張します。</p>
<form action="" method="post">
<?php wp_nonce_field( 'al_ext_options', '_al_ext_nonce' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="inputtext">ログアウトタイプ</label></th>
<td>
<div class="logout-type">
<label>
<input type="radio" class="logout-type_radio" name="_al_ext[type]"
value="1" <?php checked( $al_ext_type, 1 ); ?>>標準ログアウト
</label>
<div class="logout-exp">
<p>&#9679;<strong>「自動ログアウト時間」内にログアウトせずブラウザ終了した場合</strong>
<br>&#8658;ログイン状態は「ログイン状態の保存期間」に応じた日数で保存します
</p>
</div>
</div>
<div class="logout-type">
<label>
<input type="radio" class="logout-type_radio" name="_al_ext[type]"
value="2" <?php checked( $al_ext_type, 2 ); ?>>自動ログアウト
</label>
<div class="logout-exp">
<p>&#9679;<strong>ログイン後の無操作時間が「自動ログアウト時間」を超えた場合</strong><br>&#8658;自動ログアウトします</p>
</div>
</div>
<div class="logout-type">
<label>
<input type="radio" class="logout-type_radio" name="_al_ext[type]"
value="3" <?php checked( $al_ext_type, 3 ); ?>>拡張ログアウト
</label>
<div class="logout-exp">
<p>&#9679;<strong>ログイン後の無操作時間が「自動ログアウト時間」を超えた場合</strong><br>&#8658;自動ログアウトします</p>
<p>&#9679;<strong>「自動ログアウト時間」内にログアウトせずブラウザ終了した場合</strong>
<br>&#8658;ログイン状態は「ログイン状態の保存期間」に応じた日数で保存します
</p>
</div>
</div>
</td>
</tr>
<tr valign="top">
<th scope="row">ログイン状態の保存期間</th>
<td>
<input type="number" class="expire-date" name="_al_ext[expire_date]"
value="<?php echo esc_attr( $expire_date ); ?>"> 日
</td>
</tr>
<tr valign="top">
<th scope="row">自動ログアウト時間</th>
<td>
<input type="number" class="force-logout" name="_al_ext[force_logout]"
value="<?php echo esc_attr( $force_logout ); ?>"> 分
</td>
</tr>
<tr valign="top">
<th scope="row">ログアウトのリダイレクトURL</th>
<td>
<input type="url" class="logout-redirect-url" name="_al_ext[force_logout_url]"
value="<?php echo esc_attr( $force_logout_url ); ?>"
placeholder="<?php echo esc_attr( $default_logout_url ); ?>">
</td>
</tr>
</table>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存"/></p>
</form>
<?php endif; ?>
+1
View File
@@ -0,0 +1 @@
<div id="settings_updated" class="updated notice is-dismissible"><p><strong>設定を保存しました。(新しいログイン設定はログアウト後に有効になります)</strong></p></div>
+36
View File
@@ -0,0 +1,36 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! trait_exists( 'al_ext_force_trait' ) ) {
/**
* 自動ログアウト
* Trait al_ext_force_trait
*/
trait al_ext_force_trait {
/**
* 自動ログアウトの一時Cookie作成
*/
public function set_force_al_ext_cookie() {
//ログイン+一時Cookieが存在する場合、一時Cookieを更新する
if ( isset( $_COOKIE[ al_ext_config::AL_EXT_TMP_COOKIE ] ) && is_user_logged_in() ) {
$this->create_tmp_cookie();
}
}
/**
* 自動ログアウトのログイン:remembermeを元に一時Cookieを作成
*/
public function my_force_login() {
if ( isset( $_POST['rememberme'] ) ) {
//一時ログインCookieを生成
$this->create_tmp_cookie();
}
}
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! trait_exists( 'al_ext_trait' ) ) {
/**
* 拡張ログアウト
* Trait al_ext_trait
*/
trait al_ext_trait {
/**
* 拡張ログアウトの一時Cookie作成
*/
public function set_al_ext_cookie() {
if ( is_user_logged_in() ) {
//rememberme cookieが存在する場合は一時Cookieを作成する
if ( isset( $_COOKIE[ al_ext_config::AL_EXT_REMEMBER_COOKIE ] )
&& ! isset( $_COOKIE[ al_ext_config::AL_EXT_TMP_COOKIE ] ) ) {
$this->create_tmp_cookie();
}
}
}
/**
* 拡張ログアウト時のログイン:remembermeを元に一時Cookieを作成
*/
public function my_login() {
//ログイン継続のチェックがあれば、Cookieに保存する
if ( isset( $_POST['rememberme'] ) ) {
$this->create_remember_cookie();
//一時ログインCookieを生成
$this->create_tmp_cookie();
} else {
setcookie( al_ext_config::AL_EXT_REMEMBER_COOKIE, '', time() - 30 );
}
}
private function create_remember_cookie() {
$expire = $this->set_remember_cookie_expire_time();
setcookie( al_ext_config::AL_EXT_REMEMBER_COOKIE, 'al_ext_remember', $expire, '/' );
}
private function set_remember_cookie_expire_time() {
if ( isset( $this->options['expire_date'] ) ) {
return time() + 60 * 60 * 24 * $this->options['expire_date'];
}
return time() + 60 * 60 * 24 * al_ext_config::AL_EXT_DEFAULT_EXPIRE_DATE;
}
}
}
+8
View File
@@ -0,0 +1,8 @@
<?php
// WP_UNINSTALL_PLUGINが定義されているかチェック
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
die;
}
// オプション設定の削除
delete_option( '_al_ext' );