<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'google-recaptcha';
}

$settings     = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $settings ) ) {
	$settings = [];
}

$site_key           = trim( $settings['site_key'] ?? '' );
// Never render the stored secret in HTML (even as a password field — browser "reveal" exposes it, and it may be encrypted).
// Show a mask when a secret is saved; the save handler preserves the existing value if the mask is submitted unchanged.
$secret_mask        = class_exists( 'GoogleRecaptcha\\Module' ) && method_exists( 'GoogleRecaptcha\\Module', 'getSecretMask' )
	? \GoogleRecaptcha\Module::getSecretMask()
	: '••••••••••••';
$has_secret_stored  = isset( $settings['secret_key'] ) && trim( (string) $settings['secret_key'] ) !== '';
$secret_key         = $has_secret_stored ? $secret_mask : '';
$version            = in_array( $settings['version'] ?? 'v2', [ 'v2', 'v3' ], true ) ? ( $settings['version'] ?? 'v2' ) : 'v2';
$theme              = in_array( $settings['theme'] ?? 'light', [ 'light', 'dark' ], true ) ? ( $settings['theme'] ?? 'light' ) : 'light';
$v3_score_threshold = (float) ( $settings['v3_score_threshold'] ?? 0.5 );

$protect_login          = ! empty( $settings['protect_login'] );
$protect_registration   = ! empty( $settings['protect_registration'] );
$protect_password_reset = ! empty( $settings['protect_password_reset'] );
$protect_comments       = ! empty( $settings['protect_comments'] );

$woo_login          = ! empty( $settings['woo_login'] );
$woo_registration   = ! empty( $settings['woo_registration'] );
$woo_lost_password  = ! empty( $settings['woo_lost_password'] );
$woo_checkout       = ! empty( $settings['woo_checkout'] );
$woo_reviews        = ! empty( $settings['woo_reviews'] );

$basic_forms     = ! empty( $settings['basic_forms'] );
$cf7             = ! empty( $settings['cf7'] );
$wpforms         = ! empty( $settings['wpforms'] );
$formidable      = ! empty( $settings['formidable'] );
$fluent_forms    = ! empty( $settings['fluent_forms'] );
$elementor_forms = ! empty( $settings['elementor_forms'] );

$elementor_pro_active = defined( 'ELEMENTOR_PRO_VERSION' );

$ajax_comment_reset       = ! empty( $settings['ajax_comment_reset'] );
$login_canonical_only     = ! empty( $settings['login_canonical_only'] );
$register_canonical_only  = ! empty( $settings['register_canonical_only'] );
$woo_checkout_guest_only  = ! empty( $settings['woo_checkout_guest_only'] );
$disable_submit           = ! empty( $settings['disable_submit'] );
$perf_compat              = ! empty( $settings['perf_compat'] );
$resource_hints           = ! empty( $settings['resource_hints'] );
$blocksy_compat           = ! empty( $settings['blocksy_compat'] );
$widget_language          = isset( $settings['widget_language'] ) ? trim( (string) $settings['widget_language'] ) : '';

$skip_rules        = isset( $settings['skip_rules'] ) && is_array( $settings['skip_rules'] ) ? $settings['skip_rules'] : [];
$skip_enabled      = ! empty( $skip_rules['enabled'] );
$skip_roles_saved  = isset( $skip_rules['roles'] ) && is_array( $skip_rules['roles'] ) ? $skip_rules['roles'] : [];
$skip_ips_saved    = isset( $skip_rules['ips'] ) && is_array( $skip_rules['ips'] ) ? $skip_rules['ips'] : [];
$skip_ips_textarea = implode( "\n", array_map( 'strval', $skip_ips_saved ) );
$skip_uas_saved    = isset( $skip_rules['user_agents'] ) && is_array( $skip_rules['user_agents'] ) ? $skip_rules['user_agents'] : [];
$skip_uas_textarea = implode( "\n", array_map( 'strval', $skip_uas_saved ) );
$all_roles         = function_exists( 'wp_roles' ) ? wp_roles()->roles : [];

$premium_unlocked = function_exists( 'wp_powersuite_is_license_valid' ) && wp_powersuite_is_license_valid();
$license_page_url   = admin_url( 'admin.php?page=powersuite-license' );

$premium_integrations_on = $basic_forms || $cf7 || $wpforms || $formidable || $fluent_forms || $elementor_forms
	|| ! empty( $settings['integration_gravity'] ) || ! empty( $settings['integration_jetpack'] )
	|| ! empty( $settings['integration_bbpress'] ) || ! empty( $settings['integration_buddypress'] )
	|| ! empty( $settings['integration_ultimate_member'] ) || ! empty( $settings['integration_memberpress'] )
	|| ! empty( $settings['integration_edd'] ) || ! empty( $settings['integration_pmp'] )
	|| ! empty( $settings['integration_mc4wp'] );

$protection_on = $protect_login || $protect_registration || $protect_password_reset || $protect_comments
	|| $woo_login || $woo_registration || $woo_lost_password || $woo_checkout || $woo_reviews
	|| ( $premium_unlocked && $premium_integrations_on );
$keys_missing = ( $site_key === '' || ! $has_secret_stored );

$woo_active = class_exists( 'WooCommerce', false );

$wpps_gr_integrations = [
	[ 'key' => 'cf7', 'label' => __( 'Contact Form 7', 'wp-powersuite' ), 'available' => class_exists( 'WPCF7' ) ],
	[ 'key' => 'wpforms', 'label' => __( 'WPForms', 'wp-powersuite' ), 'available' => function_exists( 'wpforms' ) ],
	[ 'key' => 'formidable', 'label' => __( 'Formidable Forms', 'wp-powersuite' ), 'available' => class_exists( 'FrmFormsController' ) ],
	[ 'key' => 'fluent_forms', 'label' => __( 'Fluent Forms', 'wp-powersuite' ), 'available' => defined( 'FLUENTFORM' ) ],
	[ 'key' => 'elementor_forms', 'label' => __( 'Elementor Pro Forms', 'wp-powersuite' ), 'available' => $elementor_pro_active ],
	[ 'key' => 'integration_gravity', 'label' => __( 'Gravity Forms', 'wp-powersuite' ), 'available' => class_exists( 'GFForms' ) ],
	[ 'key' => 'integration_jetpack', 'label' => __( 'Jetpack forms', 'wp-powersuite' ), 'available' => defined( 'JETPACK__VERSION' ) ],
	[ 'key' => 'integration_bbpress', 'label' => __( 'bbPress', 'wp-powersuite' ), 'available' => function_exists( 'bbpress' ) ],
	[ 'key' => 'integration_buddypress', 'label' => __( 'BuddyPress', 'wp-powersuite' ), 'available' => class_exists( 'BuddyPress' ) ],
	[ 'key' => 'integration_ultimate_member', 'label' => __( 'Ultimate Member', 'wp-powersuite' ), 'available' => class_exists( 'UM' ) ],
	[ 'key' => 'integration_memberpress', 'label' => __( 'MemberPress', 'wp-powersuite' ), 'available' => class_exists( 'MeprAppCtrl' ) ],
	[ 'key' => 'integration_edd', 'label' => __( 'Easy Digital Downloads', 'wp-powersuite' ), 'available' => function_exists( 'EDD' ) ],
	[ 'key' => 'integration_pmp', 'label' => __( 'Paid Memberships Pro', 'wp-powersuite' ), 'available' => defined( 'PMPRO_VERSION' ) ],
	[ 'key' => 'integration_mc4wp', 'label' => __( 'MC4WP (Mailchimp)', 'wp-powersuite' ), 'available' => defined( 'MC4WP_VERSION' ) ],
];

$gr_console_url = 'https://www.google.com/recaptcha/admin';

$uid = preg_replace( '/[^a-z0-9_-]/i', '', $module_id );

$wpps_gr_render_premium_notice = static function () use ( $premium_unlocked, $license_page_url ): void {
	if ( $premium_unlocked ) {
		return;
	}
	?>
	<div class="notice notice-info inline" style="margin:0 0 12px; padding:8px 12px;">
		<p style="margin:0;">
			<?php
			echo wp_kses_post(
				sprintf(
					/* translators: %s: URL to license page */
					__( 'This section is a Premium feature. <a href="%s">Activate your license</a> to configure form integrations, advanced options, and skip rules.', 'wp-powersuite' ),
					esc_url( $license_page_url )
				)
			);
			?>
		</p>
	</div>
	<?php
};
?>

<?php if ( $protection_on && $keys_missing ) : ?>
	<div class="wpps-settings-alert wpps-settings-alert--warning" role="alert">
		<p>
			<strong><?php esc_html_e( 'Keys required', 'wp-powersuite' ); ?></strong>
			<?php esc_html_e( 'Protection is on but Site Key or Secret Key is missing — verification will fail.', 'wp-powersuite' ); ?>
			<?php
			wpps_render_modal_settings_tip(
				__( 'Help: API keys', 'wp-powersuite' ),
				__( 'Create keys in the Google reCAPTCHA admin console, then paste the site key and secret key here. Save before running the connection test.', 'wp-powersuite' )
			);
			?>
		</p>
	</div>
<?php endif; ?>

<?php if ( is_multisite() && ( $site_key === '' || ! $has_secret_stored ) ) : ?>
	<div class="wpps-settings-alert wpps-settings-alert--info">
		<p>
			<?php esc_html_e( 'Multisite: empty keys can fall back to network defaults set by the super admin.', 'wp-powersuite' ); ?>
			<?php
			wpps_render_modal_settings_tip(
				__( 'Help: multisite keys', 'wp-powersuite' ),
				__( 'Network option keys: wpps_network_google-recaptcha_site_key and wpps_network_google-recaptcha_secret_key. Store the network secret with ENC2 encryption (same as site-level saves) when possible.', 'wp-powersuite' )
			);
			?>
		</p>
	</div>
<?php endif; ?>

<?php wpps_render_captcha_overlap_notices( $module_id ); ?>

<div class="wp-powersuite-module-settings wpps-settings-panel wpps-settings-panel--accordion-stack" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<div class="wpps-ts-accordions">
			<div class="wpps-ts-accordion is-open">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_api" aria-expanded="true" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_api">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'API & appearance', 'wp-powersuite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_api" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_api">
					<div class="wpps-ts-field wpps-ts-field--keys">
						<div class="wpps-ts-inline-keys">
							<div class="wpps-ts-inline-keys__cell">
								<div class="wpps-ts-field__label-row">
									<label for="<?php echo esc_attr( $module_id ); ?>_site_key"><?php esc_html_e( 'Site Key', 'wp-powersuite' ); ?></label>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: Site Key', 'wp-powersuite' ),
										__( 'Public key from the Google reCAPTCHA admin console. Pair it with the secret key below.', 'wp-powersuite' )
									);
									?>
								</div>
								<input
									type="text"
									id="<?php echo esc_attr( $module_id ); ?>_site_key"
									name="<?php echo esc_attr( $module_id ); ?>[site_key]"
									value="<?php echo esc_attr( $site_key ); ?>"
									class="wp-powersuite-advanced-setting"
									data-setting-key="site_key"
									placeholder="6Lc..."
									autocomplete="off"
									spellcheck="false"
								/>
							</div>
							<div class="wpps-ts-inline-keys__cell">
								<div class="wpps-ts-field__label-row">
									<label for="<?php echo esc_attr( $module_id ); ?>_secret_key"><?php esc_html_e( 'Secret Key', 'wp-powersuite' ); ?></label>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: Secret Key', 'wp-powersuite' ),
										$has_secret_stored
											? __( 'A secret is already stored. Leave the dots unchanged to keep it, or replace with a new secret from Google.', 'wp-powersuite' )
											: __( 'Private key for server-side verification. Never expose it in the browser or logs.', 'wp-powersuite' )
									);
									?>
								</div>
								<input
									type="password"
									id="<?php echo esc_attr( $module_id ); ?>_secret_key"
									name="<?php echo esc_attr( $module_id ); ?>[secret_key]"
									value="<?php echo esc_attr( $secret_key ); ?>"
									class="wp-powersuite-advanced-setting"
									data-setting-key="secret_key"
									placeholder="6Lc..."
									autocomplete="new-password"
									spellcheck="false"
								/>
							</div>
						</div>
						<p class="wpps-settings-subtle-link wpps-ts-dashboard-link">
							<a href="<?php echo esc_url( $gr_console_url ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Open Google reCAPTCHA admin', 'wp-powersuite' ); ?></a>
						</p>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__control">
							<div class="wpps-ts-controls-row wpps-ts-controls-row--502525">
								<div class="wpps-ts-controls-row__cell">
									<div class="wpps-ts-field__label-row">
										<span><?php esc_html_e( 'Test connection', 'wp-powersuite' ); ?></span>
										<?php
										wpps_render_modal_settings_tip(
											__( 'Help: connection test', 'wp-powersuite' ),
											__( 'Save your keys first. Run test loads reCAPTCHA in the background, obtains a token, and verifies it with Google — no manual copying.', 'wp-powersuite' )
										);
										?>
									</div>
									<div class="wpps-captcha-test-row">
										<div
											class="wpps-captcha-test-widget"
											data-provider="recaptcha"
											data-version="<?php echo esc_attr( $version ); ?>"
											data-module-id="<?php echo esc_attr( $module_id ); ?>"
											data-nonce="<?php echo esc_attr( wp_create_nonce( 'wpps_captcha_test_' . $module_id ) ); ?>"
											data-sitekey-field="<?php echo esc_attr( $module_id ); ?>_site_key"
											data-version-field="<?php echo esc_attr( $module_id ); ?>_version"
											data-theme-field="<?php echo esc_attr( $module_id ); ?>_theme"
										></div>
										<button
											type="button"
											class="button button-secondary wpps-captcha-test-connection"
											data-module-id="<?php echo esc_attr( $module_id ); ?>"
											data-provider="recaptcha"
											data-nonce="<?php echo esc_attr( wp_create_nonce( 'wpps_captcha_test_' . $module_id ) ); ?>"
										>
											<?php esc_html_e( 'Run test', 'wp-powersuite' ); ?>
										</button>
									</div>
								</div>
								<div class="wpps-ts-controls-row__cell">
									<div class="wpps-ts-field__label-row">
										<label for="<?php echo esc_attr( $module_id ); ?>_version"><?php esc_html_e( 'reCAPTCHA version', 'wp-powersuite' ); ?></label>
										<?php
										wpps_render_modal_settings_tip(
											__( 'Help: version', 'wp-powersuite' ),
											__( 'v2 shows a checkbox challenge. v3 is invisible and uses a risk score (0.0–1.0).', 'wp-powersuite' )
										);
										?>
									</div>
									<select
										id="<?php echo esc_attr( $module_id ); ?>_version"
										name="<?php echo esc_attr( $module_id ); ?>[version]"
										class="wp-powersuite-advanced-setting"
										data-setting-key="version"
									>
										<option value="v2" <?php selected( $version, 'v2' ); ?>><?php esc_html_e( 'v2 (checkbox)', 'wp-powersuite' ); ?></option>
										<option value="v3" <?php selected( $version, 'v3' ); ?>><?php esc_html_e( 'v3 (invisible, score-based)', 'wp-powersuite' ); ?></option>
									</select>
								</div>
								<div class="wpps-ts-controls-row__cell wpps-gr-v2-only"<?php echo $version === 'v3' ? ' hidden' : ''; ?>>
									<div class="wpps-ts-field__label-row">
										<label for="<?php echo esc_attr( $module_id ); ?>_theme"><?php esc_html_e( 'Theme', 'wp-powersuite' ); ?></label>
										<?php
										wpps_render_modal_settings_tip(
											__( 'Help: theme', 'wp-powersuite' ),
											__( 'Light or dark styling for the v2 checkbox widget.', 'wp-powersuite' )
										);
										?>
									</div>
									<select
										id="<?php echo esc_attr( $module_id ); ?>_theme"
										name="<?php echo esc_attr( $module_id ); ?>[theme]"
										class="wp-powersuite-advanced-setting"
										data-setting-key="theme"
									>
										<option value="light" <?php selected( $theme, 'light' ); ?>><?php esc_html_e( 'Light', 'wp-powersuite' ); ?></option>
										<option value="dark" <?php selected( $theme, 'dark' ); ?>><?php esc_html_e( 'Dark', 'wp-powersuite' ); ?></option>
									</select>
								</div>
							</div>
							<div class="wpps-ts-field wpps-gr-v3-only"<?php echo $version === 'v2' ? ' hidden' : ''; ?> style="margin-top:12px;">
								<div class="wpps-ts-field__label-row">
									<label for="<?php echo esc_attr( $module_id ); ?>_v3_score_threshold"><?php esc_html_e( 'Score threshold (v3)', 'wp-powersuite' ); ?></label>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: score threshold', 'wp-powersuite' ),
										__( 'Minimum score (0.0–1.0) to pass. 0.5 is typical; lower is stricter.', 'wp-powersuite' )
									);
									?>
								</div>
								<input
									type="number"
									id="<?php echo esc_attr( $module_id ); ?>_v3_score_threshold"
									name="<?php echo esc_attr( $module_id ); ?>[v3_score_threshold]"
									value="<?php echo esc_attr( (string) $v3_score_threshold ); ?>"
									min="0"
									max="1"
									step="0.1"
									class="small-text wp-powersuite-advanced-setting"
									data-setting-key="v3_score_threshold"
								/>
							</div>
							<p class="description wpps-captcha-test-status" role="status" aria-live="polite" id="<?php echo esc_attr( $module_id ); ?>_test_status"></p>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_wp" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_wp">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'WordPress core', 'wp-powersuite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_wp" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_wp">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Protected forms', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: WordPress forms', 'wp-powersuite' ),
								__( 'Adds reCAPTCHA to core WordPress flows. Enable only what you use — each adds a verification step for visitors.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[protect_login]" value="1" <?php checked( $protect_login ); ?> class="wp-powersuite-advanced-setting" data-setting-key="protect_login" />
									<?php esc_html_e( 'Login', 'wp-powersuite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[protect_registration]" value="1" <?php checked( $protect_registration ); ?> class="wp-powersuite-advanced-setting" data-setting-key="protect_registration" />
									<?php esc_html_e( 'Registration', 'wp-powersuite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[protect_password_reset]" value="1" <?php checked( $protect_password_reset ); ?> class="wp-powersuite-advanced-setting" data-setting-key="protect_password_reset" />
									<?php esc_html_e( 'Password reset', 'wp-powersuite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[protect_comments]" value="1" <?php checked( $protect_comments ); ?> class="wp-powersuite-advanced-setting" data-setting-key="protect_comments" />
									<?php esc_html_e( 'Comments', 'wp-powersuite' ); ?>
								</label>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_woo" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_woo">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'WooCommerce', 'wp-powersuite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_woo" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_woo">
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Protected forms', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: WooCommerce', 'wp-powersuite' ),
								__( 'Adds reCAPTCHA to storefront forms. Product reviews only applies when Comments protection above is off.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label<?php echo $woo_active ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_login]" value="1" <?php checked( $woo_login ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_login" <?php disabled( ! $woo_active ); ?> />
									<?php esc_html_e( 'Login', 'wp-powersuite' ); ?>
								</label>
								<label<?php echo $woo_active ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_registration]" value="1" <?php checked( $woo_registration ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_registration" <?php disabled( ! $woo_active ); ?> />
									<?php esc_html_e( 'Registration', 'wp-powersuite' ); ?>
								</label>
								<label<?php echo $woo_active ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_lost_password]" value="1" <?php checked( $woo_lost_password ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_lost_password" <?php disabled( ! $woo_active ); ?> />
									<?php esc_html_e( 'Lost password', 'wp-powersuite' ); ?>
								</label>
								<label<?php echo $woo_active ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_checkout]" value="1" <?php checked( $woo_checkout ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_checkout" <?php disabled( ! $woo_active ); ?> />
									<?php esc_html_e( 'Checkout', 'wp-powersuite' ); ?>
								</label>
								<label class="wpps-check-grid__label-with-tip<?php echo $woo_active ? '' : ' wpps-ts-integration--unavailable'; ?>">
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_reviews]" value="1" <?php checked( $woo_reviews ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_reviews" <?php disabled( ! $woo_active ); ?> />
									<span><?php esc_html_e( 'Product reviews', 'wp-powersuite' ); ?></span>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: product reviews', 'wp-powersuite' ),
										__( 'Use when Comments protection is disabled but you still want reCAPTCHA on WooCommerce product reviews only.', 'wp-powersuite' )
									);
									?>
								</label>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_forms" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_forms">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Forms & integrations', 'wp-powersuite' ); ?><?php if ( ! $premium_unlocked ) : ?> <span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></span><?php endif; ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_forms" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_forms">
					<?php $wpps_gr_render_premium_notice(); ?>
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Integrations', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: form plugins', 'wp-powersuite' ),
								__( 'Supported integrations for Google reCAPTCHA in this plugin. Install and activate a plugin to enable its checkbox. Basic Forms is built into WP PowerSuite.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[basic_forms]" value="1" <?php checked( $basic_forms ); ?> class="wp-powersuite-advanced-setting" data-setting-key="basic_forms" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'Basic Forms (WP PowerSuite)', 'wp-powersuite' ); ?>
								</label>
								<?php foreach ( $wpps_gr_integrations as $wpps_gr_int ) : ?>
									<?php
									$ik = $wpps_gr_int['key'];
									$ia = $wpps_gr_int['available'];
									$iv = ! empty( $settings[ $ik ] );
									?>
								<label<?php echo $ia ? '' : ' class="wpps-ts-integration--unavailable"'; ?>>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[<?php echo esc_attr( $ik ); ?>]" value="1" <?php checked( $iv ); ?> class="wp-powersuite-advanced-setting" data-setting-key="<?php echo esc_attr( $ik ); ?>" <?php disabled( ! $ia || ! $premium_unlocked ); ?> />
									<?php echo esc_html( $wpps_gr_int['label'] ); ?>
								</label>
								<?php endforeach; ?>
							</div>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_adv" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_adv">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Advanced options', 'wp-powersuite' ); ?><?php if ( ! $premium_unlocked ) : ?> <span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></span><?php endif; ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_adv" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_adv">
					<?php $wpps_gr_render_premium_notice(); ?>
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row"><?php esc_html_e( 'Behavior', 'wp-powersuite' ); ?></div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[disable_submit]" value="1" <?php checked( $disable_submit ); ?> class="wp-powersuite-advanced-setting" data-setting-key="disable_submit" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'Disable submit until solved', 'wp-powersuite' ); ?>
									<?php
									wpps_render_modal_settings_tip(
										__( 'Help: disable submit', 'wp-powersuite' ),
										__( 'Dims submit buttons until the visitor completes reCAPTCHA v2. Invisible v3 obtains the token when the form is submitted instead.', 'wp-powersuite' )
									);
									?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[ajax_comment_reset]" value="1" <?php checked( $ajax_comment_reset ); ?> class="wp-powersuite-advanced-setting" data-setting-key="ajax_comment_reset" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'Reset reCAPTCHA when replying to comments (AJAX)', 'wp-powersuite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[login_canonical_only]" value="1" <?php checked( $login_canonical_only ); ?> class="wp-powersuite-advanced-setting" data-setting-key="login_canonical_only" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'Protect wp-login.php login only', 'wp-powersuite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[register_canonical_only]" value="1" <?php checked( $register_canonical_only ); ?> class="wp-powersuite-advanced-setting" data-setting-key="register_canonical_only" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'Protect wp-login.php registration only', 'wp-powersuite' ); ?>
								</label>
								<?php if ( class_exists( 'WooCommerce' ) ) : ?>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[woo_checkout_guest_only]" value="1" <?php checked( $woo_checkout_guest_only ); ?> class="wp-powersuite-advanced-setting" data-setting-key="woo_checkout_guest_only" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'WooCommerce checkout: guests only', 'wp-powersuite' ); ?>
								</label>
								<?php endif; ?>
							</div>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row"><?php esc_html_e( 'Performance & theme', 'wp-powersuite' ); ?></div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid">
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[perf_compat]" value="1" <?php checked( $perf_compat ); ?> class="wp-powersuite-advanced-setting" data-setting-key="perf_compat" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'Compatibility tweaks for optimizers', 'wp-powersuite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[resource_hints]" value="1" <?php checked( $resource_hints ); ?> class="wp-powersuite-advanced-setting" data-setting-key="resource_hints" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'DNS prefetch / preconnect hints', 'wp-powersuite' ); ?>
								</label>
								<label>
									<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[blocksy_compat]" value="1" <?php checked( $blocksy_compat ); ?> class="wp-powersuite-advanced-setting" data-setting-key="blocksy_compat" <?php disabled( ! $premium_unlocked ); ?> />
									<?php esc_html_e( 'Blocksy account modal reset', 'wp-powersuite' ); ?>
								</label>
							</div>
							<p class="description" style="margin:12px 0 0;">
								<label for="<?php echo esc_attr( $module_id ); ?>_widget_language"><?php esc_html_e( 'Widget language (optional)', 'wp-powersuite' ); ?></label>
							</p>
							<input
								type="text"
								id="<?php echo esc_attr( $module_id ); ?>_widget_language"
								name="<?php echo esc_attr( $module_id ); ?>[widget_language]"
								value="<?php echo esc_attr( $widget_language ); ?>"
								class="wp-powersuite-advanced-setting widefat"
								data-setting-key="widget_language"
								placeholder="<?php esc_attr_e( 'auto', 'wp-powersuite' ); ?>"
								autocomplete="off"
								<?php disabled( ! $premium_unlocked ); ?>
							/>
							<p class="description"><?php esc_html_e( 'ISO code passed to Google as hl (e.g. en, de). Leave blank for browser default.', 'wp-powersuite' ); ?></p>
						</div>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_skip" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_skip">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Skip rules', 'wp-powersuite' ); ?><?php if ( ! $premium_unlocked ) : ?> <span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></span><?php endif; ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_skip" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_skip">
					<?php $wpps_gr_render_premium_notice(); ?>
					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<span><?php esc_html_e( 'Trusted visitors', 'wp-powersuite' ); ?></span>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: skip rules', 'wp-powersuite' ),
								__( 'Matching roles, IPs, or User-Agent substrings bypass the widget (no challenge and no server verification for matching visitors). IP rules require correct proxy/Cloudflare configuration — User-Agent matches are spoofable. Leave lists empty when skip is enabled to effectively disable skipping.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<label class="wpps-ts-inline-check">
								<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[skip_rules][enabled]" value="1" <?php checked( $skip_enabled ); ?> class="wp-powersuite-advanced-setting" data-setting-key="skip_rules.enabled" <?php disabled( ! $premium_unlocked ); ?> />
								<?php esc_html_e( 'Enable skip rules', 'wp-powersuite' ); ?>
							</label>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row"><?php esc_html_e( 'Allowed roles', 'wp-powersuite' ); ?></div>
						<div class="wpps-ts-field__control">
							<div class="wpps-check-grid wpps-check-grid--roles">
								<?php if ( ! empty( $all_roles ) ) : ?>
									<?php foreach ( $all_roles as $slug => $role_data ) : ?>
										<label>
											<input type="checkbox" name="<?php echo esc_attr( $module_id ); ?>[skip_rules][roles][]" value="<?php echo esc_attr( $slug ); ?>" <?php checked( in_array( $slug, $skip_roles_saved, true ) ); ?> class="wp-powersuite-advanced-setting" data-setting-key="skip_rules.roles" <?php disabled( ! $premium_unlocked ); ?> />
											<?php echo esc_html( is_array( $role_data ) && isset( $role_data['name'] ) ? $role_data['name'] : $slug ); ?>
										</label>
									<?php endforeach; ?>
								<?php endif; ?>
							</div>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<label for="<?php echo esc_attr( $module_id ); ?>_skip_ips"><?php esc_html_e( 'Allowed IPs / CIDR', 'wp-powersuite' ); ?></label>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: IP allowlist', 'wp-powersuite' ),
								__( 'One IP or CIDR per line. Supports IPv4 and IPv6. Useful for office networks; misconfiguration can weaken protection.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<textarea
								id="<?php echo esc_attr( $module_id ); ?>_skip_ips"
								name="<?php echo esc_attr( $module_id ); ?>[skip_rules][ips]"
								class="large-text code wp-powersuite-advanced-setting"
								data-setting-key="skip_rules.ips"
								rows="4"
								placeholder="203.0.113.5&#10;10.0.0.0/24"
								spellcheck="false"
								<?php disabled( ! $premium_unlocked ); ?>
							><?php echo esc_textarea( $skip_ips_textarea ); ?></textarea>
						</div>
					</div>

					<div class="wpps-ts-field">
						<div class="wpps-ts-field__label-row">
							<label for="<?php echo esc_attr( $module_id ); ?>_skip_user_agents"><?php esc_html_e( 'User-Agent substrings', 'wp-powersuite' ); ?></label>
							<?php
							wpps_render_modal_settings_tip(
								__( 'Help: User-Agent skip list', 'wp-powersuite' ),
								__( 'One substring per line matched against the visitor User-Agent (case-sensitive). Useful for trusted bots or internal tools.', 'wp-powersuite' )
							);
							?>
						</div>
						<div class="wpps-ts-field__control">
							<textarea
								id="<?php echo esc_attr( $module_id ); ?>_skip_user_agents"
								name="<?php echo esc_attr( $module_id ); ?>[skip_rules][user_agents]"
								class="large-text code wp-powersuite-advanced-setting"
								data-setting-key="skip_rules.user_agents"
								rows="3"
								placeholder="SomeBot/1.0"
								spellcheck="false"
								<?php disabled( ! $premium_unlocked ); ?>
							><?php echo esc_textarea( $skip_uas_textarea ); ?></textarea>
						</div>
					</div>
				</div>
			</div>
		</div>
	</form>
	<script>
	(function () {
		var panel = document.querySelector('.wpps-settings-panel--accordion-stack[data-module-id="<?php echo esc_js( $module_id ); ?>"]');
		if (!panel) {
			return;
		}
		var versionSelect = panel.querySelector('#<?php echo esc_js( $module_id ); ?>_version');
		var v2Cells = panel.querySelectorAll('.wpps-gr-v2-only');
		var v3Blocks = panel.querySelectorAll('.wpps-gr-v3-only');
		function syncRecaptchaVersionUi() {
			if (!versionSelect) {
				return;
			}
			var isV3 = versionSelect.value === 'v3';
			v2Cells.forEach(function (el) { el.hidden = isV3; });
			v3Blocks.forEach(function (el) { el.hidden = !isV3; });
			var testWidget = panel.querySelector('.wpps-captcha-test-widget[data-provider="recaptcha"]');
			if (testWidget) {
				testWidget.setAttribute('data-version', versionSelect.value);
			}
		}
		if (versionSelect) {
			versionSelect.addEventListener('change', syncRecaptchaVersionUi);
			syncRecaptchaVersionUi();
		}
	})();
	</script>
</div>
