php – Facing problem to implement SSL redirect

I want to add a checkbox button for automatically redirect to HTTPS if SSL is present.
the process I added like below:

this is form area:

<form action="options.php" method="POST" id="<?php echo esc_attr( $data('slug') ); ?>_options">
            <?php settings_fields( $data('slug') ); ?>
            <?php $this->render_form_sections(); ?>
            <?php $this->render_hidden_fields(); ?>
            <input type="submit" class="wpr-button" id="wpr-options-submit" value="<?php echo esc_attr( $data('btn_submit_text') ); ?>">
</form>
        

added new field in the form:

$this->settings->add_settings_fields(
        (
            
            'ssl_redirect'       => (
                'type'              => 'checkbox',
                'label'             => __( 'Enable SSL Redirect', 'rocket' ),
                'section'           => 'enable_ssl_section',
                'page'              => 'hub',
                'default'           => 0,
                'sanitize_callback' => 'sanitize_checkbox',
            ),

added this:

if ( $wp_rocket_requirement_checks->check() ) {

    require WP_ROCKET_INC_PATH . 'main.php';
    require WP_ROCKET_INC_PATH . 'custom.php'; 

}

this custom.php is added for making action function after checkbox is true state.
this is my custom.php

<?PHP

 defined( 'ABSPATH' ) || exit;



?>

I want to add this function code in cutom.php how to add this and how form action will perform please solve this.

function enable_ssl_redirect() {
    if ( isset( $_SERVER('HTTPS') ) ) {
        if ( 'on' === strtolower( $_SERVER('HTTPS') && strpos( home_url(), 'https' ) == false ) ) {
        wp_redirect(str_replace( 'http', 'https', home_url() ));
    }

    if ( '1' == $_SERVER('HTTPS') ) {
        wp_redirect(home_url());
    }
} elseif ( isset( $_SERVER('SERVER_PORT') ) && ( '443' == $_SERVER('SERVER_PORT') ) ) {
     wp_redirect(home_url());
}

return false;
}