<?php
/**
 * Settings tab - global (non-per-rule) plugin options.
 *
 * Skip HTML tags is available to all users. The default is empty - the engine
 * will replace anything, anywhere. Users who want to protect <script>, <style>,
 * <code>, or other tags can add them here.
 *
 * @package EasySearchReplace
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Settings tab POST handler.
 *
 * @return array|null
 */
function esrn_settings_handle_submit() {
	// phpcs:disable WordPress.Security.NonceVerification.Missing
	if ( ! isset( $_POST['esrn_settings_submit'] ) ) {
		return null;
	}
	if ( ! current_user_can( 'manage_options' ) ) {
		return null;
	}
	check_admin_referer( 'esrn_search_replace_settings' );

	$skip_raw = isset( $_POST['esrn_skip_tags'] ) && is_array( $_POST['esrn_skip_tags'] )
		? (array) $_POST['esrn_skip_tags']
		: array();
	// phpcs:enable WordPress.Security.NonceVerification.Missing

	$skip = array();
	foreach ( $skip_raw as $tag ) {
		$tag = strtolower( preg_replace( '/[^a-z0-9]/i', '', (string) wp_unslash( $tag ) ) );
		if ( '' !== $tag ) {
			$skip[] = $tag;
		}
	}

	// Custom additions from the free-text field (comma-separated).
	// phpcs:ignore WordPress.Security.NonceVerification.Missing
	if ( isset( $_POST['esrn_skip_tags_custom'] ) ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Missing
		$custom_raw = (string) wp_unslash( $_POST['esrn_skip_tags_custom'] );
		$pieces     = array_filter( array_map( 'trim', explode( ',', $custom_raw ) ) );
		foreach ( $pieces as $p ) {
			$p = strtolower( preg_replace( '/[^a-z0-9]/i', '', $p ) );
			if ( '' !== $p ) {
				$skip[] = $p;
			}
		}
	}

	$skip = array_values( array_unique( $skip ) );

	$options              = get_option( ESRN_OPTION_KEY, array() );
	$options['skip_tags'] = $skip;
	update_option( ESRN_OPTION_KEY, $options );

	return array( 'skip_tags' => $skip );
}

/**
 * Render the Settings tab.
 */
function esrn_render_tab_settings() {
	$saved = esrn_settings_handle_submit();
	if ( null !== $saved ) {
		esrn_flash( 'success', __( 'Settings saved.', 'easy-search-replace' ) );
	}

	$options     = get_option( ESRN_OPTION_KEY, array() );
	$saved_skip  = isset( $options['skip_tags'] ) && is_array( $options['skip_tags'] ) ? $options['skip_tags'] : array();
	$common_tags = array( 'script', 'style', 'code', 'pre', 'noscript', 'textarea' );
	$custom_tags = array_diff( $saved_skip, $common_tags );
	?>

	<div class="esr-panel">
		<div class="esr-panel-head">
			<h2><?php esc_html_e( 'Skip these HTML tags', 'easy-search-replace' ); ?></h2>
			<p class="esr-muted">
				<?php esc_html_e( 'By default, replacements run across your entire page HTML, including scripts and styles. Check the tags below to protect their contents from being touched.', 'easy-search-replace' ); ?>
			</p>
		</div>
		<div class="esr-panel-body">
			<div class="esr-notice esr-notice-tip">
				<span class="dashicons dashicons-info" aria-hidden="true"></span>
				<div>
					<strong><?php esc_html_e( 'Recommended for most sites:', 'easy-search-replace' ); ?></strong>
					<?php esc_html_e( 'Check ', 'easy-search-replace' ); ?><code>&lt;script&gt;</code><?php esc_html_e( ' and ', 'easy-search-replace' ); ?><code>&lt;style&gt;</code><?php esc_html_e( ' to avoid accidentally breaking JavaScript or CSS. Leave them unchecked only if you specifically need to replace text inside scripts or stylesheets.', 'easy-search-replace' ); ?>
				</div>
			</div>

			<form method="post" action="">
				<?php wp_nonce_field( 'esrn_search_replace_settings' ); ?>

				<div class="esr-field">
					<label><?php esc_html_e( 'Common tags', 'easy-search-replace' ); ?></label>
					<div class="esr-checkbox-grid">
						<?php foreach ( $common_tags as $tag ) : ?>
							<label class="esr-checkbox">
								<input
									type="checkbox"
									name="esrn_skip_tags[]"
									value="<?php echo esc_attr( $tag ); ?>"
									<?php checked( in_array( $tag, $saved_skip, true ) ); ?>
								/>
								<code>&lt;<?php echo esc_html( $tag ); ?>&gt;</code>
							</label>
						<?php endforeach; ?>
					</div>
				</div>

				<div class="esr-field">
					<label for="esrn_skip_tags_custom">
						<?php esc_html_e( 'Custom tags (comma-separated)', 'easy-search-replace' ); ?>
					</label>
					<input
						type="text"
						id="esrn_skip_tags_custom"
						name="esrn_skip_tags_custom"
						value="<?php echo esc_attr( implode( ', ', $custom_tags ) ); ?>"
						placeholder="iframe, svg"
					/>
					<p class="esr-help">
						<?php esc_html_e( 'Only letters and numbers. Any matching tag and its contents will be left alone.', 'easy-search-replace' ); ?>
					</p>
				</div>

				<div class="esr-inline-actions">
					<button type="submit" name="esrn_settings_submit" class="esr-btn esr-btn-primary">
						<span class="dashicons dashicons-yes" aria-hidden="true"></span>
						<?php esc_html_e( 'Save settings', 'easy-search-replace' ); ?>
					</button>
				</div>
			</form>
		</div>
	</div>
	<?php
}
