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

/* セクションID haumenu_define_section_id_03_security() */
/* タブのスラグ haumenu_define_tab_slug_03_security() */
/* $option_name 設定名 haumenu_define_option_name_03_security() */
/* get_option GetOP03_security() */

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

function haup_child_setting_view_control_03_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_03_security();

	// 連動マップ（ID => セレクター）
	// 1つのIDに対して複数のCSSクラスを指定する（1つなら1行）
	$dependency_map = array(
		//$set_name . 'enable-set-revisions' => array(
			//'.set-revisions-num-wrapper', // リビジョン数
			//'.set-revisions-posttype-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_03_security', 20 );

/*** コメントスパム対策 ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_comment_anti_spam(){
//項目の追加
	add_settings_field(
		'comment-anti-spam', // 設定名
		__('Eliminate spam comments','hima-art-utility'), // 設定タイトル
		'haumenu_comment_anti_spam', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_03_security(), //表示させるタブのスラグ
		haumenu_define_section_id_03_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

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

/* 入力フォーム */
function haumenu_comment_anti_spam() {
	$set_name = haumenu_define_option_name_03_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'comment-anti-spam'; //個別配列名

	//テーブルに値がなかった場合の措置（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('Add a dummy item (honeypot) to the comment input item and return an error to comments that are posted mechanically','hima-art-utility'); ?></p>
</label>
<?php
}

/*** コメントの自動削除 ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_auto_delete_comments_with_url(){
//項目の追加
	add_settings_field(
		'auto-delete-comments-with-url', // 設定名
		__('Automatic deletion of comments','hima-art-utility'), // 設定タイトル
		'haumenu_auto_delete_comments_with_url', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_03_security(), //表示させるタブのスラグ
		haumenu_define_section_id_03_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

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

/* 入力フォーム */
function haumenu_auto_delete_comments_with_url() {
	$set_name = haumenu_define_option_name_03_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'auto-delete-comments-with-url'; //個別配列名

	//テーブルに値がなかった場合の措置（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('Comments containing URLs or links will be automatically deleted.','hima-art-utility'); ?></p>
</label>
<p><?php esc_html_e('*Enabling this feature will display a message below the comment text stating, "Comments containing links or URLs will be ignored."','hima-art-utility'); ?></p>
<p class="haup-admin-bk">
	<span><?php esc_html_e('WordPress standard functionality will put a comment awaiting approval if it contains more than a specified number of URLs or links in the body of the text. However, this feature will automatically delete a comment completely if it contains even one URL or link, while still making it appear to the sender that the comment was submitted as usual.','hima-art-utility'); ?></span>
	<br>
	<span class="haup-admin-font-red" style="font-size:0.8rem;"><?php esc_html_e('*This will be processed with higher priority than WordPress standard URL spam protection feature.','hima-art-utility'); ?></span>
	<br>
	<span><?php esc_html_e('This can save you some of the trouble of having to check and manually delete unwanted spam comments that are awaiting approval.','hima-art-utility'); ?></span>
</p>
<?php
}


/*** ユーザー登録スパム対策 ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_user_register_anti_spam(){
//項目の追加
	add_settings_field(
		'user-register-anti-spam', // 設定名
		__('Eliminate user registration spam','hima-art-utility'), // 設定タイトル
		'haumenu_user_register_anti_spam', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_03_security(), //表示させるタブのスラグ
		haumenu_define_section_id_03_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

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

/* 入力フォーム */
function haumenu_user_register_anti_spam() {
	$set_name = haumenu_define_option_name_03_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'user-register-anti-spam'; //個別配列名

	//テーブルに値がなかった場合の措置（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('Dummy items (honeypots) are provided in the user registration form to prevent nuisances involving mechanical user registration.','hima-art-utility'); ?></p>
</label>
<?php
}

/*** ブルートフォースアタック対策 ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_brute_force_proof(){
//項目の追加
	add_settings_field(
		'brute-force-proof', // 設定名
		__('Avoiding brute force attacks','hima-art-utility'), // 設定タイトル
		'haumenu_brute_force_proof', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_03_security(), //表示させるタブのスラグ
		haumenu_define_section_id_03_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

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

/* 入力フォーム */
function haumenu_brute_force_proof() {
	$set_name = haumenu_define_option_name_03_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'brute-force-proof'; //個別配列名

	//テーブルに値がなかった場合の措置（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('Eliminates login attempts without opening the login form, reducing server load from brute force attacks','hima-art-utility'); ?></p>
</label>
<?php
}

/*** IPアドレスによるアクセスのブロック ***/
/* 項目の追加とデータの更新 */
function haumenu_init_field_use_ip_block(){
//項目の追加
	add_settings_field(
		'use-ip-block', // 設定名
		__('Block unauthorized access.','hima-art-utility'), // 設定タイトル
		'haumenu_use_ip_block', // 設定項目のHTMLを出力する関数名
		haumenu_define_tab_slug_03_security(), //表示させるタブのスラグ
		haumenu_define_section_id_03_security(), //セクションID
		array( 'class' => 'ha-settings-separate' )
	);

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

/* 入力フォーム */
function haumenu_use_ip_block() {
	$set_name = haumenu_define_option_name_03_security(); //  $option_name 設定名
	$options = get_option($set_name);
	$option_name = 'use-ip-block'; //個別配列名

	//テーブルに値がなかった場合の措置（PHP8.x以降への対応）
	if(isset($options[$option_name])){
		$checked = $options[$option_name];
	}else{
		$checked = '';
	}

	//機能が使えるかどうかの判定
	$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' : '';

	$stats = get_option('haup_ip_block_stats');
	$blocked_ips_list_title_text = __('Display the list of IP ranges currently being rejected.','hima-art-utility');
	$manual_update_text = __('Get the latest blocked IP list now','hima-art-utility');
	$moddate_title_text = __('Last updated:','hima-art-utility');
?>
<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('We will add a line of code to the .htaccess file to deny access, primarily to prevent unauthorized access from China.','hima-art-utility'); ?></p>
</label>
<?php echo wp_kses_post( $enabled_message ); ?>
<details style="background: #f6f7f7; border: 1px solid #ccd0d4; padding: 10px; border-radius: 4px; margin-bottom: 15px;">
	<summary style="cursor: pointer;"><?php echo esc_html( $blocked_ips_list_title_text ); ?></summary>
	<div style="margin-top: 10px; max-height: 150px; overflow-y: auto; font-family: monospace; font-size: 12px; line-height: 1.5; background: #fff; padding: 5px; border: 1px solid #ddd;">
<?php
			if ( ! empty($stats['ips']) ) {
				echo implode('<br>', array_map('esc_html', $stats['ips']));
			}
?>
</div>
</details>
<?php
if( isset( GetOP03_security()['use-ip-block'] ) && GetOP03_security()['use-ip-block'] == 1 ){
	echo '<form method="post" action="">';
	wp_nonce_field('haup_sync_action', 'haup_sync_nonce');
	echo '<input type="submit" name="haup_manual_sync" class="haup-update-ban-ip-button" value="'.esc_html( $manual_update_text ).'">';
	echo '</form>';
	echo '<p><strong>'.esc_html( $moddate_title_text ).'</strong> ' . esc_html($stats['last_sync']) . '</p>';
}
?>
<div class="haup-admin-bk">
	<h4><?php esc_html_e('How to verify after activation and how to disable the feature','hima-art-utility'); ?></h4>
	<p>
		<span><?php esc_html_e('Once you enable the feature and click "Save Changes," a button that says "Get the latest blocked IP list now" will appear in the field. Clicking this button will allow you to receive the latest blocked list and begin denying access.','hima-art-utility'); ?></span>
		<br>
		<span class="haup-admin-font-red" style="font-size:0.8rem;"><?php esc_html_e('Disabling this feature will not delete any entries you have added to the .htaccess file. To completely disable the feature, disable this setting, click "Save Changes," then deactivate the Hima Art Utility plugin and re-enable it.','hima-art-utility'); ?></span>
	</p>
</div>
<div class="haup-admin-bk">
	<h4><?php esc_html_e('Regarding the scope of application of this function','hima-art-utility'); ?></h4>
	<p>
		<span><?php esc_html_e('Enabling this feature will apply the same rules to other directories and files within the servers subdirectories of the site where it was enabled.','hima-art-utility'); ?></span>
		<br>
		<span><?php esc_html_e('If you are running another WordPress site in a subdirectory, rather than the site you are currently viewing this settings screen on, and you only want to apply this feature to that site, please install Hima Art Utility on the subdirectory site and enable the feature there.','hima-art-utility'); ?></span>
	</p>
</div>
<div style="text-align:right;margin-top:1rem;"><a href="<?php echo esc_url(HAUP_MANUAL_DIR); ?>/howtouse-ip-blocker" target="_blank"><?php echo esc_html(haup_manual_text()); ?></a></div>
<?php
}