<?php
/**
 * NewsSync Settings Section
 * Plugin configuration and settings management
 *
 * @package NewsSync
 * @since 1.0.0
 */

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

// Get settings from tabs file — wrapped in error handling so a broken
// extension filter cannot crash the entire admin page (WSOD).
$newssync_settings_file = dirname( __DIR__ ) . '/tabs/newssync-settings.php';
$newssync_fields = array();
if ( file_exists( $newssync_settings_file ) ) {
    try {
        $newssync_fields = include $newssync_settings_file;
    } catch ( \Throwable $newssync_e ) {
        $newssync_fields = array();
        if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
            error_log( '[newssync] Settings fields error: ' . $newssync_e->getMessage() . ' in ' . $newssync_e->getFile() . ':' . $newssync_e->getLine() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- guarded by WP_DEBUG
        }
    }
    if ( ! is_array( $newssync_fields ) ) {
        $newssync_fields = array();
    }
}

// Backwards compatibility: the tabs file may return either a flat numeric
// array of fields (legacy) or an associative array with keys 'free'/'pro'.
if ( is_array( $newssync_fields ) && ( isset( $newssync_fields['free'] ) || isset( $newssync_fields['pro'] ) ) ) {
    $newssync_free_fields = isset( $newssync_fields['free'] ) ? (array) $newssync_fields['free'] : array();
    $newssync_pro_fields  = isset( $newssync_fields['pro'] ) ? (array) $newssync_fields['pro'] : array();
    $newssync_legacy_all  = isset( $newssync_fields['all'] ) ? (array) $newssync_fields['all'] : array_merge( $newssync_free_fields, $newssync_pro_fields );
    $newssync_is_structured = true;
} else {
    $newssync_legacy_all = is_array( $newssync_fields ) ? $newssync_fields : array();
    $newssync_free_fields = $newssync_legacy_all;
    $newssync_pro_fields  = array();
    $newssync_is_structured = false;
}
?>

<?php
/**
 * Extended kses allowlist for admin UI blocks that include form elements.
 *
 * wp_kses_post() strips <input>, <select>, <button> etc. because those tags are
 * not permitted in post content. Admin pages are a different context — they are
 * only visible to administrators and the HTML is constructed by trusted plugin
 * code with all dynamic values already escaped (esc_attr / esc_html / esc_url).
 *
 * Using wp_kses() with this explicit allowlist satisfies WordPress.org's
 * escaping requirements while preserving the form elements the PRO UI needs.
 */
$newssync_admin_html_tags = array_merge(
    wp_kses_allowed_html( 'post' ),
    array(
        // Form elements
        'form'     => array( 'method' => true, 'action' => true, 'class' => true, 'id' => true, 'enctype' => true ),
        'input'    => array(
            'type' => true, 'name' => true, 'id' => true, 'value' => true,
            'class' => true, 'checked' => true, 'disabled' => true, 'readonly' => true,
            'min' => true, 'max' => true, 'step' => true, 'placeholder' => true,
            'title' => true, 'required' => true, 'aria-label' => true,
            // data-* attributes used by PRO toggle labels
            'data-label-on' => true, 'data-label-off' => true,
        ),
        'select'   => array( 'name' => true, 'id' => true, 'class' => true, 'title' => true, 'aria-label' => true, 'disabled' => true ),
        'option'   => array( 'value' => true, 'selected' => true, 'disabled' => true ),
        'optgroup' => array( 'label' => true ),
        'textarea' => array( 'name' => true, 'id' => true, 'class' => true, 'rows' => true, 'cols' => true, 'placeholder' => true, 'disabled' => true, 'readonly' => true ),
        'button'   => array(
            'type' => true, 'id' => true, 'class' => true, 'disabled' => true,
            'title' => true, 'aria-label' => true,
            'data-tab' => true, 'data-target' => true, 'data-provider' => true,
            'role' => true, 'aria-selected' => true, 'aria-controls' => true,
        ),
        'label'    => array( 'for' => true, 'class' => true, 'title' => true, 'id' => true ),
        // Semantic / layout elements used in PRO panels
        'nav'      => array( 'class' => true, 'role' => true, 'aria-label' => true ),
        'section'  => array( 'class' => true, 'id' => true, 'aria-label' => true ),
        'article'  => array( 'class' => true, 'id' => true ),
        'header'   => array( 'class' => true ),
        'footer'   => array( 'class' => true ),
        'main'     => array( 'class' => true ),
        'details'  => array( 'class' => true, 'open' => true ),
        'summary'  => array( 'class' => true ),
        // Allow role / aria on all block elements (merged into existing tags)
        'div'      => array( 'class' => true, 'id' => true, 'style' => true, 'role' => true, 'aria-label' => true, 'data-section' => true, 'data-tab' => true, 'data-target' => true, 'data-provider' => true, 'aria-live' => true ),
        'span'     => array( 'class' => true, 'id' => true, 'style' => true, 'role' => true, 'aria-label' => true, 'aria-hidden' => true ),
        'p'        => array( 'class' => true, 'id' => true, 'style' => true ),
        'pre'      => array( 'class' => true, 'id' => true, 'style' => true ),
        'code'     => array( 'class' => true ),
    )
);
?>

<!-- Settings Section -->
<div class="newssync-content-section<?php echo ( isset( $newssync_active_tab ) && $newssync_active_tab === 'settings' ) ? ' active' : ''; ?>" data-section="settings" style="display:<?php echo ( isset( $newssync_active_tab ) && $newssync_active_tab === 'settings' ) ? 'block' : 'none'; ?>;">
    <!-- Inline debug script removed for WordPress.org compliance -->

    <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="newssync-settings-form">
        <input type="hidden" name="action" value="newssync_save_settings">
        <input type="hidden" name="newssync_current_tab" value="settings">
        <input type="hidden" name="newssync_current_subtab" value="default">
        <?php wp_nonce_field( 'newssync_save_settings', 'newssync_nonce' ); ?>

        <?php if ( $newssync_is_structured ) : ?>

            <div class="ns-settings-tabs">
                <nav class="ns-settings-tabs-nav" role="tablist">
                    <button type="button" class="ns-tab-button ns-tab-active" data-tab="default" role="tab" aria-selected="true"><?php esc_html_e( 'Default', 'daazsync' ); ?></button>
                    <button type="button" class="ns-tab-button" data-tab="pro" role="tab" aria-selected="false"><?php esc_html_e( 'PRO', 'daazsync' ); ?></button>
                </nav>

                <div class="ns-settings-tabs-content">
                    <div class="ns-settings-tab-pane ns-settings-tab-active" data-tab="default">
                        <table class="form-table" role="presentation"><tbody>
                        <?php foreach ( $newssync_free_fields as $newssync_field ) : ?>
                            <?php
                            $newssync_field_id    = isset( $newssync_field['id'] ) ? $newssync_field['id'] : '';
                            $newssync_field_name  = isset( $newssync_field['name'] ) ? $newssync_field['name'] : '';
                            $newssync_field_desc  = isset( $newssync_field['desc'] ) ? $newssync_field['desc'] : '';
                            $newssync_field_type  = isset( $newssync_field['type'] ) ? $newssync_field['type'] : 'text';
                            $newssync_field_value = get_option( $newssync_field_id, isset( $newssync_field['default'] ) ? $newssync_field['default'] : '' );
                            ?>

                            <tr>
                                <th scope="row"><label for="<?php echo esc_attr( $newssync_field_id ); ?>"><?php echo esc_html( $newssync_field_name ); ?></label></th>
                                <td>
                                    <?php if ( $newssync_field_type === 'select' && isset( $newssync_field['options'] ) ) : ?>
                                        <?php
                                        $newssync_field_attrs = isset( $newssync_field['attrs'] ) && is_array( $newssync_field['attrs'] ) ? $newssync_field['attrs'] : array();
                                        $newssync_attr_str = '';
                                        foreach ( $newssync_field_attrs as $newssync_ak => $newssync_av ) {
                                            $newssync_attr_str .= ' ' . esc_attr( $newssync_ak ) . '="' . esc_attr( $newssync_av ) . '"';
                                        }
                                        ?>
                                        <select name="<?php echo esc_attr( $newssync_field_id ); ?>" id="<?php echo esc_attr( $newssync_field_id ); ?>" class="regular-text"<?php echo $newssync_attr_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attributes already escaped during string construction ?>>
                                            <?php foreach ( $newssync_field['options'] as $newssync_opt_value => $newssync_opt_label ) : ?>
                                                <option value="<?php echo esc_attr( $newssync_opt_value ); ?>" <?php selected( $newssync_field_value, $newssync_opt_value ); ?>><?php echo esc_html( $newssync_opt_label ); ?></option>
                                            <?php endforeach; ?>
                                        </select>

                                    <?php elseif ( $newssync_field_type === 'number' ) : ?>
                                        <?php
                                        $newssync_field_attrs = isset( $newssync_field['attrs'] ) && is_array( $newssync_field['attrs'] ) ? $newssync_field['attrs'] : array();
                                        $newssync_attr_str = '';
                                        foreach ( $newssync_field_attrs as $newssync_ak => $newssync_av ) {
                                            $newssync_attr_str .= ' ' . esc_attr( $newssync_ak ) . '="' . esc_attr( $newssync_av ) . '"';
                                        }
                                        ?>
                                        <input type="number" name="<?php echo esc_attr( $newssync_field_id ); ?>" id="<?php echo esc_attr( $newssync_field_id ); ?>" value="<?php echo esc_attr( $newssync_field_value ); ?>" class="small-text" <?php if ( isset( $newssync_field['min'] ) ) echo 'min="' . esc_attr( $newssync_field['min'] ) . '"'; ?> <?php if ( isset( $newssync_field['max'] ) ) echo 'max="' . esc_attr( $newssync_field['max'] ) . '"'; ?> <?php if ( isset( $newssync_field['step'] ) ) echo 'step="' . esc_attr( $newssync_field['step'] ) . '"'; ?><?php echo $newssync_attr_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attributes already escaped during string construction ?> >

                                    <?php elseif ( $newssync_field_type === 'html' ) : ?>
                                        <?php
                                        $newssync_html_block = isset( $newssync_field['html'] ) ? $newssync_field['html'] : ( $newssync_field['content'] ?? '' );
                                        $newssync_is_auto_import_card = ( strpos( $newssync_html_block, 'rsp-force-import-btn' ) !== false );
                                        $newssync_save_as_posts       = get_option( 'newssync_save_as_posts', '0' );
                                        if ( ! $newssync_is_auto_import_card || '1' === (string) $newssync_save_as_posts ) {
                                            echo wp_kses( $newssync_html_block, $newssync_admin_html_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses allowlist defined above; HTML built by trusted plugin code with escaped values.
                                        }
                                        ?>

                                    <?php else : ?>
                                        <input type="text" name="<?php echo esc_attr( $newssync_field_id ); ?>" id="<?php echo esc_attr( $newssync_field_id ); ?>" value="<?php echo esc_attr( $newssync_field_value ); ?>" class="regular-text">
                                    <?php endif; ?>

                                    <?php if ( ! empty( $newssync_field_desc ) ) : ?>
                                        <?php
                                        $newssync_allowed_desc_tags = array(
                                            'br'   => array(),
                                            'code' => array(),
                                            'span' => array( 'style' => array() ),
                                            'a'    => array( 'href' => array(), 'target' => array(), 'rel' => array() ),
                                            'strong' => array(),
                                            'em'     => array(),
                                        );

                                        if ( is_array( $newssync_field_desc ) ) {
                                            $newssync_line1 = isset( $newssync_field_desc['line1'] ) ? $newssync_field_desc['line1'] : '';
                                            $newssync_line2 = isset( $newssync_field_desc['line2'] ) ? $newssync_field_desc['line2'] : '';
                                            $newssync_note  = isset( $newssync_field_desc['note'] ) ? $newssync_field_desc['note'] : '';

                                            $newssync_desc_html = esc_html( $newssync_line1 );
                                            if ( $newssync_line2 ) {
                                                $newssync_desc_html .= '<br>' . '<code>' . esc_html( $newssync_line2 ) . '</code>';
                                            }
                                            if ( $newssync_note ) {
                                                $newssync_desc_html .= '<br>' . '<span style="color:#d63638;">' . esc_html( $newssync_note ) . '</span>';
                                            }

                                            echo wp_kses( $newssync_desc_html, $newssync_allowed_desc_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                        } else {
                                            echo wp_kses( $newssync_field_desc, $newssync_allowed_desc_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                        }
                                        ?>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody></table>
                    </div><!-- .ns-settings-tab-pane -->

                    <div class="ns-settings-tab-pane" data-tab="pro">
                        <?php if ( ! defined( 'NEWSSYNC_PRO_DIR' ) || empty( $newssync_pro_fields ) ) : ?>
                            <div style="background:linear-gradient(135deg,#FFF7ED,#FFFBEB);border:1px solid #FCD34D;border-radius:12px;padding:28px 32px;max-width:620px;">
                                <h3 style="margin:0 0 6px;font-size:18px;color:#92400E;"><?php esc_html_e( 'Unlock NewsSync PRO', 'daazsync' ); ?></h3>
                                <p style="margin:0 0 20px;color:#78350F;font-size:13px;"><?php esc_html_e( 'Everything in FREE, plus powerful features for serious publishers:', 'daazsync' ); ?></p>
                                <ul style="margin:0 0 24px;padding:0;list-style:none;display:flex;flex-direction:column;gap:10px;">
                                    <li style="display:flex;align-items:flex-start;gap:10px;"><span style="color:#D97706;font-size:16px;">&#9733;</span><span><strong><?php esc_html_e( 'Advanced keyword filtering', 'daazsync' ); ?></strong> &mdash; <?php esc_html_e( 'AND/OR group logic to include or exclude exactly the news you want', 'daazsync' ); ?></span></li>
                                    <li style="display:flex;align-items:flex-start;gap:10px;"><span style="color:#D97706;font-size:16px;">&#9733;</span><span><strong><?php esc_html_e( 'Full-text extraction', 'daazsync' ); ?></strong> &mdash; <?php esc_html_e( 'Fetch the complete article body, not just the RSS excerpt', 'daazsync' ); ?></span></li>
                                    <li style="display:flex;align-items:flex-start;gap:10px;"><span style="color:#D97706;font-size:16px;">&#9733;</span><span><strong><?php esc_html_e( 'AI-powered summaries', 'daazsync' ); ?></strong> &mdash; <?php esc_html_e( 'OpenAI, Claude, Gemini or your own custom API — bring your own key', 'daazsync' ); ?></span></li>
                                    <li style="display:flex;align-items:flex-start;gap:10px;"><span style="color:#D97706;font-size:16px;">&#9733;</span><span><strong><?php esc_html_e( 'Detailed import logs', 'daazsync' ); ?></strong> &mdash; <?php esc_html_e( 'See exactly what was imported, skipped or failed for every feed', 'daazsync' ); ?></span></li>
                                    <li style="display:flex;align-items:flex-start;gap:10px;"><span style="color:#D97706;font-size:16px;">&#9733;</span><span><strong><?php esc_html_e( 'Cache control', 'daazsync' ); ?></strong> &mdash; <?php esc_html_e( 'Import every 30min, 1h, 2h, 3h and more — not just 6h or 24h', 'daazsync' ); ?></span></li>
                                </ul>
                                <div style="display:flex;align-items:center;gap:20px;flex-wrap:wrap;">
                                    <?php if ( function_exists( 'newssync_freemius' ) ) : ?>
                                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=daazsync-pricing' ) ); ?>" class="button newssync-btn-upgrade" style="text-decoration:none;">
                                            <span class="dashicons dashicons-star-filled"></span>
                                            <?php esc_html_e( 'Get NewsSync PRO', 'daazsync' ); ?>
                                        </a>
                                    <?php endif; ?>
                                    <a href="https://daazlabs.com/newssync/" target="_blank" rel="noopener noreferrer" style="font-size:13px;color:#92400E;text-decoration:underline;"><?php esc_html_e( 'See all PRO features &rarr;', 'daazsync' ); ?></a>
                                </div>
                            </div>
                        <?php else : ?>
                            <table class="form-table" role="presentation"><tbody>
                            <?php foreach ( $newssync_pro_fields as $newssync_field ) : ?>
                                <?php
                                $newssync_field_id    = isset( $newssync_field['id'] ) ? $newssync_field['id'] : '';
                                $newssync_field_name  = isset( $newssync_field['name'] ) ? $newssync_field['name'] : '';
                                $newssync_field_desc  = isset( $newssync_field['desc'] ) ? $newssync_field['desc'] : '';
                                $newssync_field_type  = isset( $newssync_field['type'] ) ? $newssync_field['type'] : 'text';
                                $newssync_field_value = get_option( $newssync_field_id, isset( $newssync_field['default'] ) ? $newssync_field['default'] : '' );
                                ?>

                                <tr>
                                    <th scope="row"><label for="<?php echo esc_attr( $newssync_field_id ); ?>"><?php echo esc_html( $newssync_field_name ); ?></label></th>
                                    <td>
                                        <?php if ( $newssync_field_type === 'select' && isset( $newssync_field['options'] ) ) : ?>
                                            <?php
                                            $newssync_field_attrs = isset( $newssync_field['attrs'] ) && is_array( $newssync_field['attrs'] ) ? $newssync_field['attrs'] : array();
                                            $newssync_attr_str = '';
                                            foreach ( $newssync_field_attrs as $newssync_ak => $newssync_av ) {
                                                $newssync_attr_str .= ' ' . esc_attr( $newssync_ak ) . '="' . esc_attr( $newssync_av ) . '"';
                                            }
                                            ?>
                                            <select name="<?php echo esc_attr( $newssync_field_id ); ?>" id="<?php echo esc_attr( $newssync_field_id ); ?>" class="regular-text"<?php echo $newssync_attr_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attributes already escaped during string construction ?>>
                                                <?php foreach ( $newssync_field['options'] as $newssync_opt_value => $newssync_opt_label ) : ?>
                                                    <option value="<?php echo esc_attr( $newssync_opt_value ); ?>" <?php selected( $newssync_field_value, $newssync_opt_value ); ?>><?php echo esc_html( $newssync_opt_label ); ?></option>
                                                <?php endforeach; ?>
                                            </select>

                                        <?php elseif ( $newssync_field_type === 'number' ) : ?>
                                            <?php
                                            $newssync_field_attrs = isset( $newssync_field['attrs'] ) && is_array( $newssync_field['attrs'] ) ? $newssync_field['attrs'] : array();
                                            $newssync_attr_str = '';
                                            foreach ( $newssync_field_attrs as $newssync_ak => $newssync_av ) {
                                                $newssync_attr_str .= ' ' . esc_attr( $newssync_ak ) . '="' . esc_attr( $newssync_av ) . '"';
                                            }
                                            ?>
                                            <input type="number" name="<?php echo esc_attr( $newssync_field_id ); ?>" id="<?php echo esc_attr( $newssync_field_id ); ?>" value="<?php echo esc_attr( $newssync_field_value ); ?>" class="small-text" <?php if ( isset( $newssync_field['min'] ) ) echo 'min="' . esc_attr( $newssync_field['min'] ) . '"'; ?> <?php if ( isset( $newssync_field['max'] ) ) echo 'max="' . esc_attr( $newssync_field['max'] ) . '"'; ?> <?php if ( isset( $newssync_field['step'] ) ) echo 'step="' . esc_attr( $newssync_field['step'] ) . '"'; ?><?php echo $newssync_attr_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attributes already escaped during string construction ?> >

                                        <?php elseif ( $newssync_field_type === 'html' ) : ?>
                                            <?php
                                        $newssync_html_block = isset( $newssync_field['html'] ) ? $newssync_field['html'] : ( $newssync_field['content'] ?? '' );
                                        $newssync_is_auto_import_card = ( strpos( $newssync_html_block, 'rsp-force-import-btn' ) !== false );
                                        $newssync_save_as_posts       = get_option( 'newssync_save_as_posts', '0' );
                                        if ( ! $newssync_is_auto_import_card || '1' === (string) $newssync_save_as_posts ) {
                                            echo wp_kses( $newssync_html_block, $newssync_admin_html_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses allowlist defined above; HTML built by trusted plugin code with escaped values.
                                        }
                                        ?>
                                        <?php else : ?>
                                            <input type="text" name="<?php echo esc_attr( $newssync_field_id ); ?>" id="<?php echo esc_attr( $newssync_field_id ); ?>" value="<?php echo esc_attr( $newssync_field_value ); ?>" class="regular-text">
                                        <?php endif; ?>

                                        <?php if ( ! empty( $newssync_field_desc ) ) : ?>
                                            <?php
                                            $newssync_allowed_desc_tags = array(
                                                'br'   => array(),
                                                'code' => array(),
                                                'span' => array( 'style' => array() ),
                                                'a'    => array( 'href' => array(), 'target' => array(), 'rel' => array() ),
                                                'strong' => array(),
                                                'em'     => array(),
                                            );

                                                if ( is_array( $newssync_field_desc ) ) {
                                                    $newssync_line1 = isset( $newssync_field_desc['line1'] ) ? $newssync_field_desc['line1'] : '';
                                                    $newssync_line2 = isset( $newssync_field_desc['line2'] ) ? $newssync_field_desc['line2'] : '';
                                                    $newssync_note  = isset( $newssync_field_desc['note'] ) ? $newssync_field_desc['note'] : '';

                                                    $newssync_desc_html = esc_html( $newssync_line1 );
                                                    if ( $newssync_line2 ) {
                                                        $newssync_desc_html .= '<br>' . '<code>' . esc_html( $newssync_line2 ) . '</code>';
                                                    }
                                                    if ( $newssync_note ) {
                                                        $newssync_desc_html .= '<br>' . '<span style="color:#d63638;">' . esc_html( $newssync_note ) . '</span>';
                                                    }

                                                    echo wp_kses( $newssync_desc_html, $newssync_allowed_desc_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                                } else {
                                                    echo wp_kses( $newssync_field_desc, $newssync_allowed_desc_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                                }
                                            ?>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            </tbody></table>
                        <?php endif; ?>
                    </div><!-- .ns-settings-tab-pane -->

                </div><!-- .ns-settings-tabs-content -->
            </div><!-- .ns-settings-tabs -->

        <?php else : ?>

            <table class="form-table" role="presentation">
                <tbody>
                    <?php foreach ( $newssync_legacy_all as $newssync_field ) : ?>
                        <?php
                        $newssync_field_id    = isset( $newssync_field['id'] ) ? $newssync_field['id'] : '';
                        $newssync_field_name  = isset( $newssync_field['name'] ) ? $newssync_field['name'] : '';
                        $newssync_field_desc  = isset( $newssync_field['desc'] ) ? $newssync_field['desc'] : '';
                        $newssync_field_type  = isset( $newssync_field['type'] ) ? $newssync_field['type'] : 'text';
                        $newssync_field_value = get_option( $newssync_field_id, isset( $newssync_field['default'] ) ? $newssync_field['default'] : '' );
                        ?>

                        <tr>
                            <th scope="row">
                                <label for="<?php echo esc_attr( $newssync_field_id ); ?>">
                                    <?php echo esc_html( $newssync_field_name ); ?>
                                </label>
                            </th>
                            <td>
                                <?php if ( $newssync_field_type === 'select' && isset( $newssync_field['options'] ) ) : ?>
                                    <?php
                                    $newssync_field_attrs = isset( $newssync_field['attrs'] ) && is_array( $newssync_field['attrs'] ) ? $newssync_field['attrs'] : array();
                                    $newssync_attr_str = '';
                                    foreach ( $newssync_field_attrs as $newssync_ak => $newssync_av ) {
                                        $newssync_attr_str .= ' ' . esc_attr( $newssync_ak ) . '="' . esc_attr( $newssync_av ) . '"';
                                    }
                                    ?>
                                    <select
                                        name="<?php echo esc_attr( $newssync_field_id ); ?>"
                                        id="<?php echo esc_attr( $newssync_field_id ); ?>"
                                        class="regular-text"<?php echo $newssync_attr_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attributes already escaped during string construction ?>>
                                        <?php foreach ( $newssync_field['options'] as $newssync_opt_value => $newssync_opt_label ) : ?>
                                            <option
                                                value="<?php echo esc_attr( $newssync_opt_value ); ?>"
                                                <?php selected( $newssync_field_value, $newssync_opt_value ); ?> >
                                                <?php echo esc_html( $newssync_opt_label ); ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>

                                <?php elseif ( $newssync_field_type === 'number' ) : ?>
                                    <?php
                                    $newssync_field_attrs = isset( $newssync_field['attrs'] ) && is_array( $newssync_field['attrs'] ) ? $newssync_field['attrs'] : array();
                                    $newssync_attr_str = '';
                                    foreach ( $newssync_field_attrs as $newssync_ak => $newssync_av ) {
                                        $newssync_attr_str .= ' ' . esc_attr( $newssync_ak ) . '="' . esc_attr( $newssync_av ) . '"';
                                    }
                                    ?>
                                    <input
                                        type="number"
                                        name="<?php echo esc_attr( $newssync_field_id ); ?>"
                                        id="<?php echo esc_attr( $newssync_field_id ); ?>"
                                        value="<?php echo esc_attr( $newssync_field_value ); ?>"
                                        class="small-text"
                                        <?php if ( isset( $newssync_field['min'] ) ) echo 'min="' . esc_attr( $newssync_field['min'] ) . '"'; ?>
                                        <?php if ( isset( $newssync_field['max'] ) ) echo 'max="' . esc_attr( $newssync_field['max'] ) . '"'; ?>
                                        <?php if ( isset( $newssync_field['step'] ) ) echo 'step="' . esc_attr( $newssync_field['step'] ) . '"'; ?><?php echo $newssync_attr_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attributes already escaped during string construction ?> >

                                <?php elseif ( $newssync_field_type === 'html' ) : ?>
                                    <?php
                                    $newssync_html_block = isset( $newssync_field['html'] ) ? $newssync_field['html'] : ( $newssync_field['content'] ?? '' );
                                    $newssync_is_auto_import_card = ( strpos( $newssync_html_block, 'rsp-force-import-btn' ) !== false );
                                    $newssync_save_as_posts       = get_option( 'newssync_save_as_posts', '0' );
                                    if ( ! $newssync_is_auto_import_card || '1' === (string) $newssync_save_as_posts ) {
                                        echo wp_kses( $newssync_html_block, $newssync_admin_html_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses allowlist defined above; HTML built by trusted plugin code with escaped values.
                                    }
                                    ?>

                                <?php else : ?>
                                    <input
                                        type="text"
                                        name="<?php echo esc_attr( $newssync_field_id ); ?>"
                                        id="<?php echo esc_attr( $newssync_field_id ); ?>"
                                        value="<?php echo esc_attr( $newssync_field_value ); ?>"
                                        class="regular-text">
                                <?php endif; ?>

                                <?php if ( ! empty( $newssync_field_desc ) ) : ?>
                                    <?php
                                    $newssync_allowed_desc_tags = array(
                                        'br'   => array(),
                                        'code' => array(),
                                        'span' => array( 'style' => array() ),
                                        'a'    => array( 'href' => array(), 'target' => array(), 'rel' => array() ),
                                        'strong' => array(),
                                        'em'     => array(),
                                    );

                                    if ( is_array( $newssync_field_desc ) ) {
                                        $newssync_line1 = isset( $newssync_field_desc['line1'] ) ? $newssync_field_desc['line1'] : '';
                                        $newssync_line2 = isset( $newssync_field_desc['line2'] ) ? $newssync_field_desc['line2'] : '';
                                        $newssync_note  = isset( $newssync_field_desc['note'] ) ? $newssync_field_desc['note'] : '';

                                        $newssync_desc_html = esc_html( $newssync_line1 );
                                        if ( $newssync_line2 ) {
                                            $newssync_desc_html .= '<br>' . '<code>' . esc_html( $newssync_line2 ) . '</code>';
                                        }
                                        if ( $newssync_note ) {
                                            $newssync_desc_html .= '<br>' . '<span style="color:#d63638;">' . esc_html( $newssync_note ) . '</span>';
                                        }

                                        echo wp_kses( $newssync_desc_html, $newssync_allowed_desc_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                    } else {
                                        echo wp_kses( $newssync_field_desc, $newssync_allowed_desc_tags ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                    }
                                    ?>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

        <?php endif; ?>

        <?php submit_button( __( 'Save Settings', 'daazsync' ) ); ?>
    </form>
</div>
