<?php
/**
 * Rule card renderer.
 *
 * For free users, Pro sections show the real form fields disabled + a lock
 * overlay with an inline upgrade link. This is more transparent than a
 * feature-list teaser and lets users see exactly what they'd unlock.
 *
 * Technical notes:
 *  - Field names use __disabled_ prefix for free users so values don't POST.
 *    This avoids accidentally persisting values the user couldn't actually set.
 *  - The `disabled` attribute prevents interaction; the overlay adds the
 *    visual lock + upgrade CTA on top.
 *  - Pro CSS selector + regex fields can hold existing values (from a past
 *    Pro subscription) - we preserve them in hidden inputs.
 *
 * @package EasySearchReplace
 */

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

/**
 * Render one rule card.
 *
 * @param int   $index             Zero-based rule index.
 * @param array $field             Normalized rule.
 * @param array $public_post_types WP_Post_Type objects keyed by slug.
 * @param array $roles             wp_roles()->roles array.
 */
function esrn_render_rule_card( $index, $field, $public_post_types, $roles ) {
	$is_pro = ESRN_Freemius_Gate::is_pro();
	$idx    = (string) $index;

	$enabled     = ! empty( $field['enabled'] );
	$search      = (string) $field['search'];
	$replace     = (string) $field['replace'];
	$ignore_case = ! empty( $field['ignore_case'] );
	$is_regex    = ! empty( $field['is_regex'] );
	$selector    = (string) $field['selector'];
	$post_types  = (array) $field['post_types'];
	$post_ids    = (string) $field['post_ids'];
	$urls        = (string) $field['urls'];

	$exclude_post_types = (array) $field['exclude_post_types'];
	$exclude_post_ids   = (string) $field['exclude_post_ids'];
	$exclude_urls       = (string) $field['exclude_urls'];

	$schedule     = $field['schedule'];
	$sched_on     = ! empty( $schedule['enabled'] );
	$sched_days   = (array) $schedule['days'];
	$rule_roles   = (array) $field['roles'];
	$logged_in    = (string) $field['logged_in'];
	$device       = (string) $field['device'];

	$has_targeting = ! empty( $post_types ) || '' !== $post_ids || '' !== $urls
		|| ! empty( $exclude_post_types ) || '' !== $exclude_post_ids || '' !== $exclude_urls;
	$has_matching  = $is_regex || '' !== $selector || ! empty( $field['between_active'] );
	$has_schedule  = $sched_on;
	$has_audience  = ! empty( $rule_roles ) || 'any' !== $logged_in || 'any' !== $device;

	// Attribute used throughout to disable Pro fields for free users.
	$pro_disabled = $is_pro ? '' : 'disabled';
	// Name prefix that prevents non-Pro fields from submitting.
	$nm = $is_pro ? '' : '__disabled_';

	// CSS selector is also unlocked for grandfathered (pre-1.2) installs.
	$css_allowed    = ESRN_Freemius_Gate::is_css_selector_allowed();
	$css_disabled   = $css_allowed ? '' : 'disabled';
	$css_nm         = $css_allowed ? '' : '__disabled_';
	?>

	<div class="esr-rule<?php echo $enabled ? '' : ' esr-rule-disabled'; ?>" data-rule-index="<?php echo esc_attr( $idx ); ?>">

		<div class="esr-rule-header">
			<div class="esr-rule-header-left">
				<button type="button" class="esr-rule-collapse" aria-label="<?php esc_attr_e( 'Collapse rule', 'easy-search-replace' ); ?>">
					<span class="dashicons dashicons-arrow-down-alt2" aria-hidden="true"></span>
				</button>
				<span class="esr-rule-badge"><?php echo esc_html( sprintf( __( 'Rule %d', 'easy-search-replace' ), $index + 1 ) ); ?></span>
				<span class="esr-rule-preview" aria-hidden="true">
					<?php if ( '' !== $search ) : ?>
						<code><?php echo esc_html( wp_trim_words( $search, 6, '…' ) ); ?></code>
						<span class="esr-rule-arrow">→</span>
						<code><?php echo '' !== $replace ? esc_html( wp_trim_words( $replace, 6, '…' ) ) : '<em>' . esc_html__( 'remove', 'easy-search-replace' ) . '</em>'; ?></code>
					<?php endif; ?>
				</span>
			</div>

			<div class="esr-rule-header-right">
				<label class="esr-rule-toggle" title="<?php esc_attr_e( 'Enable or disable this rule', 'easy-search-replace' ); ?>">
					<input
						type="checkbox"
						name="esrn_search_replace_enabled[<?php echo esc_attr( $idx ); ?>]"
						value="1"
						class="esr-rule-toggle-input"
						<?php checked( $enabled ); ?>
					/>
					<span class="esr-rule-toggle-track" aria-hidden="true"></span>
					<span class="esr-rule-toggle-label"><?php echo $enabled ? esc_html__( 'Enabled', 'easy-search-replace' ) : esc_html__( 'Disabled', 'easy-search-replace' ); ?></span>
				</label>
				<button type="button" class="esr-rule-delete" aria-label="<?php esc_attr_e( 'Delete rule', 'easy-search-replace' ); ?>">
					<span class="dashicons dashicons-trash" aria-hidden="true"></span>
				</button>
			</div>
		</div>

		<div class="esr-rule-body">

			<!-- Main (free): Find / Replace / Case -->
			<div class="esr-section esr-section-main">
				<div class="esr-grid-2">
					<div class="esr-field">
						<label for="esrn_search_<?php echo esc_attr( $idx ); ?>">
							<?php esc_html_e( 'Find', 'easy-search-replace' ); ?>
						</label>
						<textarea
							id="esrn_search_<?php echo esc_attr( $idx ); ?>"
							name="esrn_search_replace_search[]"
							rows="3"
							spellcheck="false"
							placeholder="<?php esc_attr_e( 'Text, HTML, or URL to find…', 'easy-search-replace' ); ?>"
						><?php echo esc_textarea( $search ); ?></textarea>
					</div>
					<div class="esr-field">
						<label for="esrn_replace_<?php echo esc_attr( $idx ); ?>">
							<?php esc_html_e( 'Replace with', 'easy-search-replace' ); ?>
						</label>
						<textarea
							id="esrn_replace_<?php echo esc_attr( $idx ); ?>"
							name="esrn_search_replace_replace[]"
							rows="3"
							spellcheck="false"
							placeholder="<?php esc_attr_e( 'Leave empty to remove the match.', 'easy-search-replace' ); ?>"
						><?php echo esc_textarea( $replace ); ?></textarea>
					</div>
				</div>

				<div class="esr-inline-opts">
					<label class="esr-checkbox">
						<input
							type="checkbox"
							name="esrn_search_replace_ignore_case[<?php echo esc_attr( $idx ); ?>]"
							value="1"
							<?php checked( $ignore_case ); ?>
						/>
						<span><?php esc_html_e( 'Case-insensitive (match any capitalization)', 'easy-search-replace' ); ?></span>
					</label>
				</div>
			</div>

			<!-- Section: Where to apply (include filters are free; exclusions are Pro) -->
			<?php esrn_render_section_open(
				'targeting',
				__( 'Where to apply', 'easy-search-replace' ),
				__( 'Limit or exclude by post type, post ID, or URL.', 'easy-search-replace' ),
				$has_targeting,
				'dashicons-location-alt'
			); ?>

				<div class="esr-subsection">
					<h5 class="esr-sub-title"><?php esc_html_e( 'Only apply on', 'easy-search-replace' ); ?></h5>

					<div class="esr-grid-2">
						<div class="esr-field">
							<label><?php esc_html_e( 'Post types', 'easy-search-replace' ); ?></label>
							<select name="esrn_search_replace_post_type[<?php echo esc_attr( $idx ); ?>][]" class="esr-post-type-select" multiple>
								<?php foreach ( $public_post_types as $pt ) : ?>
									<option value="<?php echo esc_attr( $pt->name ); ?>" <?php selected( in_array( $pt->name, $post_types, true ) ); ?>>
										<?php echo esc_html( $pt->labels->singular_name ); ?>
									</option>
								<?php endforeach; ?>
							</select>
						</div>
						<div class="esr-field">
							<label for="esrn_post_ids_<?php echo esc_attr( $idx ); ?>">
								<?php esc_html_e( 'Post IDs (comma-separated)', 'easy-search-replace' ); ?>
							</label>
							<input
								type="text"
								id="esrn_post_ids_<?php echo esc_attr( $idx ); ?>"
								name="esrn_search_replace_post_ids[<?php echo esc_attr( $idx ); ?>]"
								value="<?php echo esc_attr( $post_ids ); ?>"
								placeholder="12, 15, 22"
							/>
						</div>
					</div>

					<div class="esr-field">
						<label for="esrn_urls_<?php echo esc_attr( $idx ); ?>">
							<?php esc_html_e( 'URLs (one per line)', 'easy-search-replace' ); ?>
						</label>
						<textarea
							id="esrn_urls_<?php echo esc_attr( $idx ); ?>"
							name="esrn_search_replace_urls[<?php echo esc_attr( $idx ); ?>]"
							rows="2"
							placeholder="https://yoursite.com/page/  or  /page/"
						><?php echo esc_textarea( $urls ); ?></textarea>
					</div>
				</div>

				<?php esrn_render_pro_fieldset_open( __( 'Except on', 'easy-search-replace' ), 'exclusions', $is_pro ); ?>
					<div class="esr-grid-2">
						<div class="esr-field">
							<label><?php esc_html_e( 'Exclude post types', 'easy-search-replace' ); ?></label>
							<select name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_exclude_post_type[<?php echo esc_attr( $idx ); ?>][]" class="esr-post-type-select" multiple <?php echo esc_attr( $pro_disabled ); ?>>
								<?php foreach ( $public_post_types as $pt ) : ?>
									<option value="<?php echo esc_attr( $pt->name ); ?>" <?php selected( in_array( $pt->name, $exclude_post_types, true ) ); ?>>
										<?php echo esc_html( $pt->labels->singular_name ); ?>
									</option>
								<?php endforeach; ?>
							</select>
						</div>
						<div class="esr-field">
							<label for="esrn_excl_pid_<?php echo esc_attr( $idx ); ?>">
								<?php esc_html_e( 'Exclude post IDs', 'easy-search-replace' ); ?>
							</label>
							<input
								type="text"
								id="esrn_excl_pid_<?php echo esc_attr( $idx ); ?>"
								name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_exclude_post_ids[<?php echo esc_attr( $idx ); ?>]"
								value="<?php echo esc_attr( $exclude_post_ids ); ?>"
								placeholder="12, 15"
								<?php echo esc_attr( $pro_disabled ); ?>
							/>
						</div>
					</div>
					<div class="esr-field">
						<label for="esrn_excl_urls_<?php echo esc_attr( $idx ); ?>">
							<?php esc_html_e( 'Exclude URLs (one per line)', 'easy-search-replace' ); ?>
						</label>
						<textarea
							id="esrn_excl_urls_<?php echo esc_attr( $idx ); ?>"
							name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_exclude_urls[<?php echo esc_attr( $idx ); ?>]"
							rows="2"
							placeholder="/about/"
							<?php echo esc_attr( $pro_disabled ); ?>
						><?php echo esc_textarea( $exclude_urls ); ?></textarea>
					</div>
				<?php esrn_render_pro_fieldset_close( $is_pro ); ?>

			<?php esrn_render_section_close(); ?>

			<!-- Section: Advanced matching -->
			<?php esrn_render_section_open(
				'matching',
				__( 'Advanced matching', 'easy-search-replace' ),
				__( 'CSS selectors, plain-text "between" matching, and regex.', 'easy-search-replace' ),
				$has_matching,
				'dashicons-filter',
				true
			); ?>

				<?php esrn_render_pro_fieldset_open( '', 'matching', $css_allowed ); ?>

					<!-- 1. CSS selector (most user-friendly) — also unlocked for grandfathered installs -->
					<div class="esr-field">
						<label for="esrn_selector_<?php echo esc_attr( $idx ); ?>">
							<?php esc_html_e( 'CSS selector', 'easy-search-replace' ); ?>
						</label>
						<input
							type="text"
							id="esrn_selector_<?php echo esc_attr( $idx ); ?>"
							name="<?php echo esc_attr( $css_nm ); ?>esrn_search_replace_selector[<?php echo esc_attr( $idx ); ?>]"
							value="<?php echo esc_attr( $selector ); ?>"
							placeholder=".site-footer, #main, .elementor-heading-title"
							<?php echo esc_attr( $css_disabled ); ?>
						/>
						<p class="esr-help">
							<?php esc_html_e( 'Restrict replacements to elements matching this CSS selector. Easiest way to target a specific area without regex. Inspect the element in your browser to find its class or ID. Leave empty to replace everywhere.', 'easy-search-replace' ); ?>
						</p>
					</div>

				<?php esrn_render_pro_fieldset_close( $css_allowed ); ?>

				<?php esrn_render_pro_fieldset_open( '', 'matching', $is_pro ); ?>

					<!-- 2. Match text between two markers (no regex) -->
					<div class="esr-field esr-between-block" data-rule-index="<?php echo esc_attr( $idx ); ?>">
						<label class="esr-checkbox">
							<input
								type="checkbox"
								class="esr-between-toggle"
								name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_between_active[<?php echo esc_attr( $idx ); ?>]"
								value="1"
								<?php checked( ! empty( $field['between_active'] ) ); ?>
								<?php echo esc_attr( $pro_disabled ); ?>
							/>
							<span><?php esc_html_e( 'Match text between two markers (no regex needed)', 'easy-search-replace' ); ?></span>
						</label>
						<p class="esr-help">
							<?php esc_html_e( 'Replaces everything between a "starts with" and "ends with" snippet. Safer than regex because it stops at the first end marker, so it can\'t accidentally swallow large chunks of HTML.', 'easy-search-replace' ); ?>
						</p>

						<div class="esr-between-fields" <?php echo empty( $field['between_active'] ) ? 'style="display:none"' : ''; ?>>
							<div class="esr-grid-2">
								<div class="esr-field">
									<label for="esrn_between_start_<?php echo esc_attr( $idx ); ?>">
										<?php esc_html_e( 'Starts with', 'easy-search-replace' ); ?>
									</label>
									<input
										type="text"
										id="esrn_between_start_<?php echo esc_attr( $idx ); ?>"
										name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_between_start[<?php echo esc_attr( $idx ); ?>]"
										value="<?php echo esc_attr( (string) $field['between_start'] ); ?>"
										placeholder='<a href="https://example.com">'
										<?php echo esc_attr( $pro_disabled ); ?>
									/>
								</div>
								<div class="esr-field">
									<label for="esrn_between_end_<?php echo esc_attr( $idx ); ?>">
										<?php esc_html_e( 'Ends with', 'easy-search-replace' ); ?>
									</label>
									<input
										type="text"
										id="esrn_between_end_<?php echo esc_attr( $idx ); ?>"
										name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_between_end[<?php echo esc_attr( $idx ); ?>]"
										value="<?php echo esc_attr( (string) $field['between_end'] ); ?>"
										placeholder='</a>'
										<?php echo esc_attr( $pro_disabled ); ?>
									/>
								</div>
							</div>
							<p class="esr-help">
								<?php
								echo wp_kses(
									__( 'When this is on, the "Find" box above is ignored. The matched text (including markers) is replaced with whatever you type in "Replace with" — leave it empty to remove the whole block.', 'easy-search-replace' ),
									array()
								);
								?>
							</p>
						</div>
					</div>

					<!-- 3. Regex (advanced) -->
					<div class="esr-field">
						<label class="esr-checkbox">
							<input
								type="checkbox"
								class="esr-regex-toggle"
								name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_is_regex[<?php echo esc_attr( $idx ); ?>]"
								value="1"
								<?php checked( $is_regex ); ?>
								<?php echo esc_attr( $pro_disabled ); ?>
							/>
							<span><?php esc_html_e( 'Treat "Find" as a regex pattern', 'easy-search-replace' ); ?></span>
						</label>
						<p class="esr-help">
							<?php
							echo wp_kses(
								__( 'Standard PHP regex. <code>.</code> already matches newlines (no need for the <code>s</code> flag) and patterns are UTF-8 aware.', 'easy-search-replace' ),
								array( 'code' => array() )
							);
							?>
						</p>
					</div>

				<?php esrn_render_pro_fieldset_close( $is_pro ); ?>

			<?php esrn_render_section_close(); ?>

			<!-- Section: Schedule -->
			<?php esrn_render_section_open(
				'schedule',
				__( 'Schedule', 'easy-search-replace' ),
				__( 'Run this rule only during specific dates, times, or weekdays.', 'easy-search-replace' ),
				$has_schedule,
				'dashicons-calendar-alt',
				true
			); ?>

				<?php esrn_render_pro_fieldset_open( '', 'schedule', $is_pro ); ?>
					<div class="esr-field">
						<label class="esr-checkbox">
							<input
								type="checkbox"
								name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_schedule[<?php echo esc_attr( $idx ); ?>][enabled]"
								value="1"
								<?php checked( $sched_on ); ?>
								<?php echo esc_attr( $pro_disabled ); ?>
							/>
							<span><?php esc_html_e( 'Enable schedule', 'easy-search-replace' ); ?></span>
						</label>
					</div>
					<div class="esr-grid-2">
						<div class="esr-field">
							<label><?php esc_html_e( 'Start date', 'easy-search-replace' ); ?></label>
							<input type="date" name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_schedule[<?php echo esc_attr( $idx ); ?>][start_date]" value="<?php echo esc_attr( $schedule['start_date'] ); ?>" <?php echo esc_attr( $pro_disabled ); ?> />
						</div>
						<div class="esr-field">
							<label><?php esc_html_e( 'End date', 'easy-search-replace' ); ?></label>
							<input type="date" name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_schedule[<?php echo esc_attr( $idx ); ?>][end_date]" value="<?php echo esc_attr( $schedule['end_date'] ); ?>" <?php echo esc_attr( $pro_disabled ); ?> />
						</div>
						<div class="esr-field">
							<label><?php esc_html_e( 'Start time', 'easy-search-replace' ); ?></label>
							<input type="time" name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_schedule[<?php echo esc_attr( $idx ); ?>][start_time]" value="<?php echo esc_attr( $schedule['start_time'] ); ?>" <?php echo esc_attr( $pro_disabled ); ?> />
						</div>
						<div class="esr-field">
							<label><?php esc_html_e( 'End time', 'easy-search-replace' ); ?></label>
							<input type="time" name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_schedule[<?php echo esc_attr( $idx ); ?>][end_time]" value="<?php echo esc_attr( $schedule['end_time'] ); ?>" <?php echo esc_attr( $pro_disabled ); ?> />
						</div>
					</div>
					<div class="esr-field">
						<label><?php esc_html_e( 'Active on days (leave all unchecked for every day)', 'easy-search-replace' ); ?></label>
						<div class="esr-days">
							<?php
							$day_labels = array(
								0 => __( 'Sun', 'easy-search-replace' ),
								1 => __( 'Mon', 'easy-search-replace' ),
								2 => __( 'Tue', 'easy-search-replace' ),
								3 => __( 'Wed', 'easy-search-replace' ),
								4 => __( 'Thu', 'easy-search-replace' ),
								5 => __( 'Fri', 'easy-search-replace' ),
								6 => __( 'Sat', 'easy-search-replace' ),
							);
							foreach ( $day_labels as $day_num => $label ) : ?>
								<label class="esr-day">
									<input type="checkbox"
										name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_schedule[<?php echo esc_attr( $idx ); ?>][days][]"
										value="<?php echo esc_attr( (string) $day_num ); ?>"
										<?php checked( in_array( $day_num, $sched_days, true ) ); ?>
										<?php echo esc_attr( $pro_disabled ); ?>
									/>
									<span><?php echo esc_html( $label ); ?></span>
								</label>
							<?php endforeach; ?>
						</div>
					</div>
				<?php esrn_render_pro_fieldset_close( $is_pro ); ?>

			<?php esrn_render_section_close(); ?>

			<!-- Section: Audience & device -->
			<?php esrn_render_section_open(
				'audience',
				__( 'Audience & device', 'easy-search-replace' ),
				__( 'Show different replacements to different visitors.', 'easy-search-replace' ),
				$has_audience,
				'dashicons-groups',
				true
			); ?>

				<?php esrn_render_pro_fieldset_open( '', 'audience', $is_pro ); ?>
					<div class="esr-grid-2">
						<div class="esr-field">
							<label><?php esc_html_e( 'Logged-in state', 'easy-search-replace' ); ?></label>
							<select name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_logged_in[<?php echo esc_attr( $idx ); ?>]" <?php echo esc_attr( $pro_disabled ); ?>>
								<option value="any" <?php selected( 'any', $logged_in ); ?>><?php esc_html_e( 'Any visitor', 'easy-search-replace' ); ?></option>
								<option value="in" <?php selected( 'in', $logged_in ); ?>><?php esc_html_e( 'Only logged-in users', 'easy-search-replace' ); ?></option>
								<option value="out" <?php selected( 'out', $logged_in ); ?>><?php esc_html_e( 'Only guests', 'easy-search-replace' ); ?></option>
							</select>
						</div>
						<div class="esr-field">
							<label><?php esc_html_e( 'Device', 'easy-search-replace' ); ?></label>
							<select name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_device[<?php echo esc_attr( $idx ); ?>]" <?php echo esc_attr( $pro_disabled ); ?>>
								<option value="any" <?php selected( 'any', $device ); ?>><?php esc_html_e( 'Any device', 'easy-search-replace' ); ?></option>
								<option value="desktop" <?php selected( 'desktop', $device ); ?>><?php esc_html_e( 'Desktop only', 'easy-search-replace' ); ?></option>
								<option value="mobile" <?php selected( 'mobile', $device ); ?>><?php esc_html_e( 'Mobile only', 'easy-search-replace' ); ?></option>
								<option value="tablet" <?php selected( 'tablet', $device ); ?>><?php esc_html_e( 'Tablet only', 'easy-search-replace' ); ?></option>
							</select>
						</div>
					</div>
					<div class="esr-field">
						<label><?php esc_html_e( 'Roles (leave empty for any role)', 'easy-search-replace' ); ?></label>
						<select name="<?php echo esc_attr( $nm ); ?>esrn_search_replace_roles[<?php echo esc_attr( $idx ); ?>][]" class="esr-role-select" multiple <?php echo esc_attr( $pro_disabled ); ?>>
							<?php foreach ( $roles as $role_slug => $role_data ) : ?>
								<option value="<?php echo esc_attr( $role_slug ); ?>" <?php selected( in_array( $role_slug, $rule_roles, true ) ); ?>>
									<?php echo esc_html( $role_data['name'] ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</div>
				<?php esrn_render_pro_fieldset_close( $is_pro ); ?>

			<?php esrn_render_section_close(); ?>

		</div><!-- .esr-rule-body -->
	</div>
	<?php
}

/* ---------------------------------------------------------------------------
 * Section + Pro-fieldset helpers
 * ------------------------------------------------------------------------- */

/**
 * Open a collapsible section.
 *
 * @param string $slug           Internal slug.
 * @param string $title          Visible title.
 * @param string $description    Short description.
 * @param bool   $initially_open Start expanded?
 * @param string $icon           Dashicon class.
 * @param bool   $is_pro         Show Pro badge in the header.
 */
function esrn_render_section_open( $slug, $title, $description, $initially_open, $icon = 'dashicons-admin-generic', $is_pro = false ) {
	$open_class  = $initially_open ? ' esr-section-open' : '';
	$is_user_pro = ESRN_Freemius_Gate::is_pro();
	?>
	<div class="esr-section esr-section-<?php echo esc_attr( $slug ); ?><?php echo esc_attr( $open_class ); ?>">
		<button type="button" class="esr-section-head">
			<span class="dashicons <?php echo esc_attr( $icon ); ?>" aria-hidden="true"></span>
			<span class="esr-section-title"><?php echo esc_html( $title ); ?></span>
			<?php if ( $is_pro && ! $is_user_pro ) : ?>
				<?php echo ESRN_Freemius_Gate::pro_badge(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			<?php endif; ?>
			<span class="esr-section-desc"><?php echo esc_html( $description ); ?></span>
			<span class="esr-section-chevron dashicons dashicons-arrow-down-alt2" aria-hidden="true"></span>
		</button>
		<div class="esr-section-body">
	<?php
}

/**
 * Close a collapsible section.
 */
function esrn_render_section_close() {
	echo '</div></div>';
}

/**
 * Open a Pro-locked fieldset. For Pro users: renders as a normal wrapper.
 * For free users: adds the .esr-pro-locked class (overlay via CSS) and an
 * inline upgrade link in the corner.
 *
 * @param string $sub_title   Optional subsection title. Pass '' for no header.
 * @param string $feature_tag Attribution slug for upgrade URL.
 * @param bool   $is_pro      Whether current user is Pro.
 */
function esrn_render_pro_fieldset_open( $sub_title, $feature_tag, $is_pro ) {
	$class = $is_pro ? 'esr-subsection' : 'esr-subsection esr-pro-locked';
	?>
	<div class="<?php echo esc_attr( $class ); ?>">
		<?php if ( '' !== $sub_title || ! $is_pro ) : ?>
			<div class="esr-pro-locked-head">
				<?php if ( '' !== $sub_title ) : ?>
					<h5 class="esr-sub-title">
						<?php echo esc_html( $sub_title ); ?>
						<?php if ( ! $is_pro ) : ?>
							<?php echo ESRN_Freemius_Gate::pro_badge(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<?php endif; ?>
					</h5>
				<?php else : ?>
					<span></span>
				<?php endif; ?>

				<?php if ( ! $is_pro ) : ?>
					<a
						href="<?php echo esc_url( ESRN_Freemius_Gate::upgrade_url( $feature_tag ) ); ?>"
						class="esr-lock-cta"
					>
						<span class="dashicons dashicons-lock" aria-hidden="true"></span>
						<?php esc_html_e( 'Unlock with Pro', 'easy-search-replace' ); ?>
					</a>
				<?php endif; ?>
			</div>
		<?php endif; ?>
		<div class="esr-pro-locked-body">
	<?php
}

/**
 * Close a Pro-locked fieldset.
 *
 * @param bool $is_pro Whether current user is Pro (passed for symmetry).
 */
function esrn_render_pro_fieldset_close( $is_pro ) {
	unset( $is_pro );
	echo '</div></div>';
}

/* ---------------------------------------------------------------------------
 * Backward-compat aliases.
 *
 * Earlier iterations used esrn_open_locked_block / esrn_close_locked_block
 * before we renamed to the more descriptive fieldset pair. Some tab files
 * (import-export) still call the old names. Keep thin wrappers so we don't
 * have to edit the callers.
 * ------------------------------------------------------------------------- */

if ( ! function_exists( 'esrn_open_locked_block' ) ) {
	function esrn_open_locked_block( $is_pro, $feature_tag = '' ) {
		// New signature: ($sub_title, $feature_tag, $is_pro). The legacy
		// callers (tab-import-export) didn't pass a sub-title, so we use ''
		// which the renderer handles by skipping the header row.
		esrn_render_pro_fieldset_open( '', $feature_tag, $is_pro );
	}
}

if ( ! function_exists( 'esrn_close_locked_block' ) ) {
	function esrn_close_locked_block( $is_pro, $feature_tag = '' ) {
		esrn_render_pro_fieldset_close( $is_pro );
	}
}
