WP_PLUGIN 自動ログアウト拡張
初回コミット
This commit is contained in:
@@ -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, '/' );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user