miled / wordpress-social-login

WordPress Social Login
http://miled.github.io/wordpress-social-login/
MIT License
399 stars 235 forks source link

Disable starting session on page load and add a hook that lets themes and plugins decide if they want sessions to start #304

Closed rxnlabs closed 3 years ago

rxnlabs commented 5 years ago

It would great if there was a way to disable the calling session_start on every page load. This prevents some caching services from working and also adds more load to the server if the user doesn't have a need for sessions. I've also run into issues with session collisions. This can either be a option on the settings page or a filter that runs early on that sites can hook into to determine if they want sessions enabled or not. In https://github.com/miled/wordpress-social-login/blob/791c5f4bca58af94f47cfccaaa6e72839fa0763f/wp-social-login.php#L73

This code can be added to the wp-social-login.php file before sessions are started.

add_action( 'init', function() {
    $start_session = apply_filters( 'wsl_session_start', true );

    if ( true === $start_session ) {
        session_id() or session_start();
    }
}, 1 );

In the theme or plugin, I call the hook to specify if I want to initialize sessions.

add_filter(  'wsl_session_start', function( $session_start ) {
    if (  false === some_conditional_here() ) {
        return false;
    }

    return $session_start;
}, 10 );

function some_conditional_here() {
    // determine if a session cookie already exists for this requests
    if ( !empty( $_COOKIE ) ) {
        foreach ( $_COOKIE as $name => $value ) {
            if ( session_name() === $name ) {
                return true;
            }
        }

    }

    return false;
}