<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否

/* セクションID haumenu_define_section_id_01_security() */
/* タブのスラグ haumenu_define_tab_slug_01_security() */
/* $option_name 設定名 haumenu_define_option_name_01_security() */
/* get_option GetOP01_security() */

/***** 親設定の有効／無効に合わせて子設定の表示方法を変更する *****/
/* 関数名・設定名は画面ごとに変えること（関数重複を防ぐ） */
/* add_action('admin_notices', 'current_pagehook');を追加してページ名を取得し、$page_fooksuffixの値を変える */
/* 連動マップ（ID => セレクター）の項目を編集する */

function haup_child_setting_view_control_01_security( $hook_suffix ) {
	//ページフックにより出力を判断
	$page_fooksuffix = 'ha-utilitiy_page_haumenu_page_security'; //セキュリティとスパム対策設定

	if ( $page_fooksuffix !== $hook_suffix ) {
		return;
	}

	//メインスクリプトのハンドル
	$handle = 'haup-child-setting-control-js';

	// メインスクリプトが登録されていなければ終了(スクリプトの読み込みはmain-menu.phpで記述)
	if ( ! wp_script_is( $handle, 'registered' ) ) {
		return;
	}

	//設定名
	$set_name = haumenu_define_option_name_01_security();

	// 連動マップ（ID => セレクター）
	// 1つのIDに対して複数のCSSクラスを指定する（1つなら1行）
	$dependency_map = array(
		$set_name . 'banned-registration-username' => array(
			'.username-blacklist-wrapper', // ユーザー名の制限
		),

	);

	$inner_js = "";
	foreach ( $dependency_map as $id => $selectors ) {
		foreach ( (array) $selectors as $selector ) {
			// translators: %s stands for the CSS Class & ID.
			$inner_js .= sprintf(
				"haup_apply_dependency('%s', '%s');\n",
				esc_js( $id ),
				esc_js( $selector )
			);
		}
	}


	if ( $inner_js ) {
		$js_code = "if (window.haup_apply_dependency) {\n" . $inner_js . "}";
		// 第3引数を 'after' にすることで、本体JSの直後に出力
		wp_add_inline_script( $handle, $js_code, 'after' );
	}
}
add_action( 'admin_enqueue_scripts', 'haup_child_setting_view_control_01_security', 20 );

/*** ログインにユーザー名を使用しない ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_is_use_login_forced_email(){
//項目の追加
	add_settings_field(
		'use-login-forced-email', // 設定名
		__('Disable Username Login','hima-art-utility'), // 設定タイトル
		'haumenu_is_use_login_forced_email', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}

add_action( 'admin_init', 'haumenu_init_field_is_use_login_forced_email' );

/* 入力フォーム */
function haumenu_is_use_login_forced_email() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'use-login-forced-email'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Restricting logins to email addresses and disabling usernames reduces the risk of guessing usernames from slugs in user posting lists.','hima-art-utility'); ?></p>
</label>
<?php
	$user_info = get_userdata(1);
	$usermail = $user_info->user_email;
	$blog_mail = get_bloginfo('admin_email');

	$alert_message = __('A simple check of the registered email address information revealed that the sites email address and the email address of the initial registered administrative user are the same, so it can be said that the site is in a dangerous state regardless of whether this function is enabled or disabled. We recommend changing one of your email addresses to increase login security.','hima-art-utility');
	$secure_message = __('A simple check of the email address registration information revealed that the sites email address and the email address of the initial registered administrative user are different. Enabling this function will improve login security.','hima-art-utility');

	if($usermail == $blog_mail){
		echo '<p class="haup-admin-bk haup-admin-font-red">'.esc_html($alert_message).'</p>';
	}else{
		echo '<p class="haup-admin-bk">'.esc_html($secure_message).'</p>';
	}
}

/*** ログインヒントを変更する ***/
function haumenu_init_field_change_login_hints(){
//項目の追加
	add_settings_field(
		'change-login-hints', // 設定名
		__('Change Login Hint Text','hima-art-utility'), // 設定タイトル
		'haumenu_change_login_hints', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_change_login_hints' );

/* 入力フォーム */
function haumenu_change_login_hints() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'change-login-hints'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Change the following login error message to "Login failed"','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk">
	<span>
		<?php esc_html_e('ERROR: Invalid username. Lost your password?','hima-art-utility'); ?>
	</span>
	<br>
	<span>
		<?php esc_html_e('ERROR: The password you entered for the username ___ is incorrect. Lost your password?','hima-art-utility'); ?>
	</span>
	<br>
	<span>
		<?php esc_html_e('By enabling it, it will be difficult to guess which of the user name, email address, and password is wrong.','hima-art-utility'); ?>
	</span>
</p>
<?php
}

/*** ログイン時に計算問題を出す ***/
function haumenu_init_field_login_math_check(){
//項目の追加
	add_settings_field(
		'login-math-check', // 設定名
		__('Authentication by calculation','hima-art-utility'), // 設定タイトル
		'haumenu_login_math_check', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_login_math_check' );

/* 入力フォーム */
function haumenu_login_math_check() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'login-math-check'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('A simple addition problem is displayed in the login form, and login is permitted only if the answer is correct.','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk"><?php esc_html_e('If authentication fails, the site administrator will be notified by email.','hima-art-utility'); ?></p>
<?php echo wp_kses_post(haup_usage_info_login_math_check()); ?>
<?php
}

/*** ログイン試行回数制限 ***/
function haumenu_init_field_login_attemps(){
//項目の追加
	add_settings_field(
		'login-attemps', // 設定名
		__('Login Attemps','hima-art-utility'), // 設定タイトル
		'haumenu_login_attemps', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_login_attemps' );

/* 入力フォーム */
function haumenu_login_attemps() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'login-attemps'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('If a user fails to log in multiple times, the login screen will be blocked and the user will not be able to log in for a certain period of time.','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk"><?php esc_html_e('By default, the number of attempts is 3, the lockout time is 5 minutes, and after a failed login, the message "Login failed. Are you a spammer?" is displayed.','hima-art-utility'); ?></p>
<?php echo wp_kses_post(haup_usage_limit_login_attempt_filters()); ?>
<div style="text-align:right;margin-top:1rem;"><a href="<?php echo esc_url(HAUP_MANUAL_DIR); ?>/howtouse-login-attemps" target="_blank"><?php echo esc_html(haup_manual_text()); ?></a></div>
<?php
}

/*** 「ログイン状態を保存する」を無効にする ***/
function haumenu_init_field_disable_rememberme(){
//項目の追加
	add_settings_field(
		'disable-rememberme', // 設定名
		__('Disable "Remember Me"','hima-art-utility'), // 設定タイトル
		'haumenu_disable_rememberme', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_disable_rememberme' );

/* 入力フォーム */
function haumenu_disable_rememberme() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'disable-rememberme'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Hides the "Keep me logged in" checkbox when logging in and disables the function','hima-art-utility'); ?></p>
</label>
<?php
}

/*** ログイン履歴 ***/
function haumenu_init_field_record_login_history(){
//項目の追加
	add_settings_field(
		'record-login-history', // 設定名
		__('Record login history','hima-art-utility'), // 設定タイトル
		'haumenu_record_login_history', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_record_login_history' );

/* 入力フォーム */
function haumenu_record_login_history() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'record-login-history'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Records login history to the site and allows you to check it from the administration screen','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk"><?php esc_html_e('You can view the records of past successful and unsuccessful logins from the "Login History" menu in the "HA Utility" on the administration screen.','hima-art-utility'); ?></p>
 <div style="text-align:right;margin-top:1rem;"><a href="<?php echo esc_url(HAUP_MANUAL_DIR); ?>/howtouse-login-history" target="_blank"><?php echo esc_html(haup_manual_text()); ?></a></div>
<?php
}

/*** 最終ログイン日時 ***/
function haumenu_init_field_user_last_login_datetime(){
//項目の追加
	add_settings_field(
		'user-last-login-datetime', // 設定名
		__('Last Login Datetime','hima-art-utility'), // 設定タイトル
		'haumenu_user_last_login_datetime', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_user_last_login_datetime' );

/* 入力フォーム */
function haumenu_user_last_login_datetime() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'user-last-login-datetime'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Displays the last login date and time for each user in the user list.','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk">
	<span><?php esc_html_e('After enabling this function, it will be displayed from the next login onwards.','hima-art-utility'); ?></span>
	<br>
	<span><?php esc_html_e('After enabling the feature, we recommend that you operate the system for a while and then delete accounts that have not logged in for a long period of time, especially accounts with high privileges.','hima-art-utility'); ?></span>
</p>
<?php
}

/*** ユーザースラグの変更 ***/
function haumenu_init_field_is_use_change_author_slug(){
//項目の追加
	add_settings_field(
		'use-change-author-slug', // 設定名
		__('Change User Slug','hima-art-utility'), // 設定タイトル
		'haumenu_is_use_change_author_slug', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_is_use_change_author_slug' );

/* 入力フォーム */
function haumenu_is_use_change_author_slug() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'use-change-author-slug'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('By changing the user slug to an arbitrary string, the user name cannot be guessed','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk">
	<span class="haup-admin-font-red">
		<?php esc_html_e('After activation, be sure to change the user slug on each users profile setting screen.','hima-art-utility'); ?>
	</span>
	<br>
	<span>
		<?php esc_html_e('By changing the user slug, even when accessing the user list using the REST API, the rewritten user slug will be output, improving security.','hima-art-utility'); ?>
	</span>
</p>
<?php
}

/*** ユーザー名を6文字以上にする ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_minimun_username_length(){
//項目の追加
	add_settings_field(
		'minimun-username-length', // 設定名
		__('Minimum username length','hima-art-utility'), // 設定タイトル
		'haumenu_minimun_username_length', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_minimun_username_length' );

/* 入力フォーム */
function haumenu_minimun_username_length() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'minimun-username-length'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Minimum username length of 6 characters to prevent short usernames','hima-art-utility'); ?></p>
</label>
<?php
}

/*** 特定のユーザー名で登録できなくする ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_banned_registration_username(){
//項目の追加
	add_settings_field(
		'banned-registration-username', // 設定名
		__('Restrict Username','hima-art-utility'), // 設定タイトル
		'haumenu_banned_registration_username', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_banned_registration_username' );

/* 入力フォーム */
function haumenu_banned_registration_username() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'banned-registration-username'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('Prevent registration with a username added to the blacklist','hima-art-utility'); ?></p>
</label>
<?php
}

/*** ユーザー名のブラックリスト ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_username_blacklist(){
//項目の追加
	add_settings_field(
		'username-blacklist', // 設定名
		__('Prohibited Usernames','hima-art-utility'), // 設定タイトル
		'haumenu_username_blacklist', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-convine' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_username_blacklist' );

/* 入力フォーム */
function haumenu_username_blacklist() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'username-blacklist'; //個別配列名
?>
<div class="username-blacklist-wrapper">
<label>
	<input name="<?php echo esc_html($set_name . '['.$option_name.']'); ?>" type="text" size="100" id="<?php echo esc_html($set_name . $option_name ); ?>" value="<?php echo esc_attr($options[ $option_name ] ?? ''); ?>">
	<p><?php esc_html_e('Specify the user names whose use is prohibited (separate each word with a "," (comma))','hima-art-utility'); ?></p>
</label>
<p  class="haup-admin-bk">
	<span>
		<?php esc_html_e('The following user names cannot be used even if they are not specified','hima-art-utility'); ?>
	</span>
<br>
	<span>
		<?php esc_html_e('www,web,root,admin,main,invite,administrator','hima-art-utility'); ?>
	</span>
<br>
	<span class="haup-admin-font-red">
		<?php esc_html_e('Only single-byte alphanumeric characters and " (space)", "- (hyphen)", "_ (underscore)", ". (period)", and "@" can be entered as the user name.','hima-art-utility'); ?>
	</span>
</p>
</div>
<?php
}

/*** ログイン時に管理者メールアドレス（サイトのメールアドレス）へ通知する ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_is_use_login_notice_for_admin(){
//項目の追加
	add_settings_field(
		'is-use-login-notice-for-admin', // 設定名
		__('Login Notice for Site Admin','hima-art-utility'), // 設定タイトル
		'haumenu_is_use_login_notice_for_admin', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_is_use_login_notice_for_admin' );

/* 入力フォーム */
function haumenu_is_use_login_notice_for_admin() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'is-use-login-notice-for-admin'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('When someone logs in to the site, an email will be sent to the administrator email address (the email address specified in the "Settings" panel of the administration screen).','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk">
	<span>
		<?php esc_html_e('This is not only a way to find out about unauthorized logins, but also helps to avoid issues such as not receiving the recovery email sent when an error occurs.','hima-art-utility'); ?>
	</span>
<br>
	<span class="haup-admin-font-red" style="font-size:0.8rem">
		<?php esc_html_e('If you are not receiving emails even after enabling this function, or if they are treated as spam, there may be a problem with the standard WordPress email sending function.','hima-art-utility'); ?>
	</span>
	<br>
	<span class="haup-admin-font-red" style="font-size:0.8rem;">
		<?php esc_html_e('To avoid the trouble of not receiving emails when you need them, we recommend that you try out the SMTP email sending function, which can be set up in the plugins "Site Optimization" → "Encrypt Outgoing Email" section, to ensure that your emails are always delivered.','hima-art-utility'); ?>
	</span>
</p>
<?php
}

/*** ログイン時にユーザーへ通知する ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_is_use_login_notice_for_user(){
//項目の追加
	add_settings_field(
		'is-use-login-notice-for-user', // 設定名
		__('Login Notice for User','hima-art-utility'), // 設定タイトル
		'haumenu_is_use_login_notice_for_user', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_01_security(), //表示させるタブのスラグ
		haumenu_define_section_id_01_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

}
add_action( 'admin_init', 'haumenu_init_field_is_use_login_notice_for_user' );

/* 入力フォーム */
function haumenu_is_use_login_notice_for_user() {
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'is-use-login-notice-for-user'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}
?>
<label>
	<input type="checkbox" id="<?php echo esc_html($set_name . $option_name ); ?>" name="<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>" class="wppd-ui-toggle" <?php checked( $checked , 1 ); ?> value="1">
	<p><?php esc_html_e('After logging in, send the user an email to confirm that they have logged in.','hima-art-utility'); ?></p>
</label>
<p class="haup-admin-bk">
	<span>
		<?php esc_html_e('This is effective not only for detecting if someone else has logged in under an identifiable name, but also as a way to ensure that emails are sent properly when resetting your password, etc.','hima-art-utility'); ?>
	</span>
<br>
	<span class="haup-admin-font-red" style="font-size:0.8rem">
		<?php esc_html_e('If you are not receiving emails even after enabling this function, or if they are treated as spam, there may be a problem with the standard WordPress email sending function.','hima-art-utility'); ?>
	</span>
<br>
	<span class="haup-admin-font-red" style="font-size:0.8rem;">
		<?php esc_html_e('To avoid the trouble of not receiving emails when you need them, we recommend that you try out the SMTP email sending function, which can be set up in the plugins "Site Optimization" → "Encrypt Outgoing Email" section, to ensure that your emails are always delivered.','hima-art-utility'); ?>
	</span>
</p>
<?php
}


/*** 特定のIP以外から管理画面へのアクセスを禁止する ***/
function haumenu_init_field_ip_restrict() {
	add_settings_field(
		'setting-restrict-access-ip', // 個別キー
		__('Restricting access to the administration screen and login screen', 'hima-art-utility'), // 項目名
		'haumenu_ip_restrict_field_render', // 出力関数
		haumenu_define_tab_slug_01_security(), // タブスラグ
		haumenu_define_section_id_01_security(), // セクションID
		array( 'class' => 'ha-settings-separate' )
	);
}
add_action( 'admin_init', 'haumenu_init_field_ip_restrict' );

/* IP制限入力フォーム */
function haumenu_ip_restrict_field_render() {
	$set_name = haumenu_define_option_name_01_security();
	$options = get_option($set_name);
	$option_name = 'setting-restrict-access-ip';
	$user_ip = $_SERVER['REMOTE_ADDR'];
	$value = $options[$option_name] ?? '';

	//機能が使えるかどうかの判定
	$htaccess_file = ABSPATH . '.htaccess';
	$is_writable = ( file_exists( $htaccess_file ) && is_writable( $htaccess_file ) );
	if ( $is_writable ) {
		$enabled_message_text = __('This feature is available on this site.','hima-art-utility');
		$enabled_message = '<p style="color:green;font-weight:700;">'.$enabled_message_text.'</p>';
	} else {
		$enabled_message_text = __('You are either using a server where the .htaccess file does not exist, or you do not have write permissions to the .htaccess file, so you cannot use this feature.','hima-art-utility');
		$enabled_message = '<p style="color:red;font-weight:700;">'.$enabled_message_text.'</p>';
	}

	//機能が使えない場合には選択できないようにする
	$disabled_attr = ! $is_writable ? 'disabled' : '';
?>
<div class="haup-ip-restrict-wrapper">
	<label>
		<p><?php esc_html_e('This function prevents the login screen and administration screen from being displayed or operated from any IP address other than a specific one.', 'hima-art-utility'); ?></p>
		<?php echo wp_kses_post( $enabled_message ); ?>
		<p class="haup-admin-font-red" style="font-size:0.8rem;"><?php esc_html_e('*It will be automatically enabled by entering the IP addresses that you want to allow access to the login screen and administration screen, separated by ",".', 'hima-art-utility'); ?></p>
		<textarea name="<?php echo esc_attr($set_name . '[' . $option_name . ']'); ?>"  id="haup-ip-restrict-input" rows="3" cols="100"  class="large-text"><?php echo esc_textarea($value); ?></textarea>
	</label>
	<div id="haup-ip-warning" style="display:none; margin-top: 10px; padding: 10px; border-left: 4px solid #d63638; background: #fff;">
		<p style="color: #d63638; font-weight: bold; margin: 0;">
			<?php esc_html_e('Error: The IP address you are currently accessing must be in the list', 'hima-art-utility'); ?>
		</p>
	</div>
	<div style="margin-top: 10px; padding: 10px; background: #f0f0f1; border-left: 4px solid #72aee6;">
	<?php 
		echo sprintf( 
			/* translators: %s: User IP address */
			esc_html__( 'Your current IP address: %s', 'hima-art-utility' ), 
			'<strong id="haup-current-ip">' . esc_html( $user_ip ) . '</strong>' 
		); 
	?>
	</div>
	<div style="text-align:right;margin-top:1rem;"><a href="<?php echo esc_url(HAUP_MANUAL_DIR); ?>/howtouse-restrict-admin-access-by-ip" target="_blank"><?php echo esc_html(haup_manual_text()); ?></a></div>

<script>
	(function() {
		document.addEventListener('DOMContentLoaded', function() {
			const input = document.getElementById('haup-ip-restrict-input');
			const warning = document.getElementById('haup-ip-warning');
			const myIp = "<?php echo esc_js($user_ip); ?>";
			const form = input.form;

		function checkIp() {
			const allowedIps = input.value;
			// 未入力（制限なし）は許可、入力がある場合は自分のIPが必須
			if (allowedIps.trim() !== "" && allowedIps.indexOf(myIp) === -1) {
				warning.style.display = 'block';
				return false;
			} else {
				warning.style.display = 'none';
				return true;
			}
		}

		// 入力中もリアルタイムにチェック
		input.addEventListener('input', checkIp);

		// 送信時に最終チェック
		form.addEventListener('submit', function(e) {
			if (!checkIp()) {
				e.preventDefault();
				alert("<?php echo esc_js(__('Settings could not be saved because your current IP is missing from the list.', 'hima-art-utility')); ?>");
				input.focus();
			}
		});
	});
})();
</script>
</div>
<?php
}

/*** アクセス拒否時の遷移先 ***/
function haumenu_init_field_ip_vanned_redirect_page() {
	add_settings_field(
		'ip-vanned-redirect-page', // 個別キー
		__('Page to display when access is denied', 'hima-art-utility'), // 項目名
		'haumenu_ip_vanned_redirect_page', // 出力関数
		haumenu_define_tab_slug_01_security(), // タブスラグ
		haumenu_define_section_id_01_security(), // セクションID
		array( 'class' => 'ha-settings-convine' )
	);
}
add_action( 'admin_init', 'haumenu_init_field_ip_vanned_redirect_page' );


/* 入力フォーム */
function haumenu_ip_vanned_redirect_page(){
	$set_name = haumenu_define_option_name_01_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'ip-vanned-redirect-page'; //個別配列名

	if(isset($options[$option_name])){
		$selected = $options[$option_name];
	}else{
		$selected = '';
	}
?>
<p><?php esc_html_e('Choose the page to display to denied visitors', 'hima-art-utility'); ?></p>
<select name= '<?php echo esc_html($set_name . '[' .$option_name. ']'); ?>' >
<option value='1' <?php selected($selected , '1' ); ?>><?php esc_html_e('403 Forbidden', 'hima-art-utility'); ?></option>
<option value='2' <?php selected($selected , '2' ); ?> ><?php esc_html_e('"404 Not Found" plain message', 'hima-art-utility'); ?></option>
<option value='3' <?php selected($selected , '3' ); ?> ><?php esc_html_e('Site 404 Page', 'hima-art-utility'); ?></option>
<option value='4' <?php selected($selected , '4' ); ?>><?php esc_html_e('HomePage', 'hima-art-utility'); ?></option>
</select>
<h4><?php esc_html_e('About the destination page', 'hima-art-utility'); ?></h4>
<ol>
	<li><?php esc_html_e('403 Forbidden = Shows the standard access denied page', 'hima-art-utility'); ?></li>
	<li><?php esc_html_e('"404 Not Found" plain message = Only the simple "404 Not Found" message is output. This makes it difficult to tell from the outside that the site is running on WordPress.', 'hima-art-utility'); ?></li>
	<li><?php esc_html_e('Site 404 Page = Transition to the 404 page output by the site', 'hima-art-utility'); ?></li>
	<li><?php esc_html_e('HomePage = Go to the homepage of the site. This is common in membership plugins, but it may be difficult to determine whether access is being denied by this function.', 'hima-art-utility'); ?></li>
	</ol>
<?php
}