<?php
/**
 * ST_ProQyz_Admin_Settings
 * Adds ProQyz permalink settings into WP core Permalink settings page.
 *
 * @package ST_ProQyz
 * @since   1.2.0
 */

defined( 'ABSPATH' ) || exit;

class ST_ProQyz_Admin_Settings {

    public function __construct() {
        // Add fields to the Permalinks screen
        add_action( 'load-options-permalink.php', [ $this, 'add_fields_to_permalink_settings' ] );

        // Handle saving on the same screen
        add_action( 'load-options-permalink.php', [ $this, 'maybe_save_fields' ] );
    }

    /**
     * Add custom fields to WP permalink settings page.
     */
    public function add_fields_to_permalink_settings() {
        add_settings_section(
            'proqyz_permalink_section',
            __( 'ProQyz Permalinks', 'st-proqyz' ),
            function () {
                echo '<p>' . esc_html__( 'Controls the URL slugs for the custom posts used by ProQyz.', 'st-proqyz' ) . '</p>';
            },
            'permalink'
        );

        add_settings_field(
            'proqyz_permalink_base',
            __( 'ProQyz', 'st-proqyz' ),
            [ $this, 'render_field_base' ],
            'permalink',
            'proqyz_permalink_section'
        );

        add_settings_field(
            'proqyz_permalink_course',
            __( 'ProQyz Course', 'st-proqyz' ),
            [ $this, 'render_field_course' ],
            'permalink',
            'proqyz_permalink_section'
        );
    }

    /**
     * Save custom fields when permalinks are updated.
     */
    public function maybe_save_fields() {
        if ( ! current_user_can( 'manage_options' ) ) {
            return;
        }

        // Make sure this is actually the permalink form submission
        if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
            check_admin_referer( 'update-permalink' );

            // Save ProQyz Base
            $base = isset( $_POST['proqyz_permalink_base'] )
                ? sanitize_title( wp_unslash( $_POST['proqyz_permalink_base'] ) )
                : '';
            if ( empty( $base ) ) {
                $base = 'proqyz';
            }
            update_option( 'proqyz_permalink_base', $base );

            // Save ProQyz Course
            $course = isset( $_POST['proqyz_permalink_course'] )
                ? sanitize_title( wp_unslash( $_POST['proqyz_permalink_course'] ) )
                : '';
            if ( empty( $course ) ) {
                $course = 'proqyz-course';
            }
            update_option( 'proqyz_permalink_course', $course );

            // Flush rewrites so changes take effect
            flush_rewrite_rules();
        }
    }

    /**
     * Render Base Slug field.
     */
    public function render_field_base() {
        $value = get_option( 'proqyz_permalink_base', 'proqyz' );
        ?>
        <input name="proqyz_permalink_base" type="text" id="proqyz_permalink_base"
            value="<?php echo esc_attr( $value ); ?>" class="regular-text">
        
        <?php
    }

    /**
     * Render Course Slug field.
     */
    public function render_field_course() {
        $value = get_option( 'proqyz_permalink_course', 'proqyz-course' );
        ?>
        <input name="proqyz_permalink_course" type="text" id="proqyz_permalink_course"
            value="<?php echo esc_attr( $value ); ?>" class="regular-text">
        
        <?php
    }
}

new ST_ProQyz_Admin_Settings();