commit 6d4b4c813d3f33f0fb9442d8b3278ea8e295f263 Author: nobu Date: Tue Jun 29 15:49:09 2021 +0900 WP_PLUGIN 自動ログアウト拡張 初回コミット diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85e7c1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ diff --git a/al_ext_config.php b/al_ext_config.php new file mode 100644 index 0000000..83769d8 --- /dev/null +++ b/al_ext_config.php @@ -0,0 +1,26 @@ +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, '/' ); + } + } + + } + +} + + diff --git a/class/class.al_ext_setting.php b/class/class.al_ext_setting.php new file mode 100644 index 0000000..29b54b9 --- /dev/null +++ b/class/class.al_ext_setting.php @@ -0,0 +1,56 @@ +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; + } + } + + } + +} \ No newline at end of file diff --git a/css/al_ext.css b/css/al_ext.css new file mode 100644 index 0000000..0a1acdb --- /dev/null +++ b/css/al_ext.css @@ -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; +} \ No newline at end of file diff --git a/js/al_ext.js b/js/al_ext.js new file mode 100644 index 0000000..94a6840 --- /dev/null +++ b/js/al_ext.js @@ -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); + } + }) + } +}); \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..8b7b5ef --- /dev/null +++ b/readme.txt @@ -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 \ No newline at end of file diff --git a/template/al_ext_form.php b/template/al_ext_form.php new file mode 100644 index 0000000..249820c --- /dev/null +++ b/template/al_ext_form.php @@ -0,0 +1,70 @@ + +
+

ログアウト拡張

+

「ログイン状態を保存する」がチェック時のログアウト機能を拡張します。

+
+ + + + + + + + + + + + + + + + + + +
+
+ +
+

「自動ログアウト時間」内にログアウトせずブラウザ終了した場合 +
⇒ログイン状態は「ログイン状態の保存期間」に応じた日数で保存します +

+
+
+
+ +
+

ログイン後の無操作時間が「自動ログアウト時間」を超えた場合
⇒自動ログアウトします

+
+
+
+ +
+

ログイン後の無操作時間が「自動ログアウト時間」を超えた場合
⇒自動ログアウトします

+

「自動ログアウト時間」内にログアウトせずブラウザ終了した場合 +
⇒ログイン状態は「ログイン状態の保存期間」に応じた日数で保存します +

+
+
+
ログイン状態の保存期間 + 日 +
自動ログアウト時間 + 分 +
ログアウトのリダイレクトURL + +
+

+
+ \ No newline at end of file diff --git a/template/success.php b/template/success.php new file mode 100644 index 0000000..008ce97 --- /dev/null +++ b/template/success.php @@ -0,0 +1 @@ +

設定を保存しました。(新しいログイン設定はログアウト後に有効になります)

diff --git a/trait/al_ext_force_trait.php b/trait/al_ext_force_trait.php new file mode 100644 index 0000000..93858d2 --- /dev/null +++ b/trait/al_ext_force_trait.php @@ -0,0 +1,36 @@ +create_tmp_cookie(); + } + } + + /** + * 自動ログアウトのログイン:remembermeを元に一時Cookieを作成 + */ + public function my_force_login() { + if ( isset( $_POST['rememberme'] ) ) { + //一時ログインCookieを生成 + $this->create_tmp_cookie(); + } + } + + } + +} \ No newline at end of file diff --git a/trait/al_ext_trait.php b/trait/al_ext_trait.php new file mode 100644 index 0000000..91128b7 --- /dev/null +++ b/trait/al_ext_trait.php @@ -0,0 +1,53 @@ +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; + } + + } +} \ No newline at end of file diff --git a/uninstall.php b/uninstall.php new file mode 100644 index 0000000..f3b2d1c --- /dev/null +++ b/uninstall.php @@ -0,0 +1,8 @@ +