/* 
Theme Name: Honeycomb Child 
Version: 1.0 
Description: Child theme for Honeycomb. 
Author: Property Hive
Author URI: http://wp-property-hive.com 
Template: honeycomb 
*/  

add_action('init', 'my_start_session', 1);
function my_start_session() 
{
    if (!session_id()) 
    {
        session_start();
    }
}
 
add_action('wp_logout', 'my_end_session');
add_action('wp_login', 'my_end_session');
function my_end_session() 
{
    session_destroy();
}

add_action( 'init', 'set_last_search' );
function set_last_search()
{
    if ( !function_exists('ph_get_page_id') )
    {
        // Prevent fatal error, just in case Property Hive isn't active
        return false;
    }
    if ( !isset($_SESSION['last_search']) )
    {
        $_SESSION['last_search'] = '';
    }
    if ( 
        (strpos(
            "http://" . strtolower($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']), 
            strtolower(get_permalink(ph_get_page_id( 'search_results' )))
        )
        !==
        FALSE)
        ||
        (strpos(
            "https://" . strtolower($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']), 
            strtolower(get_permalink(ph_get_page_id( 'search_results' )))
        )
        !==
        FALSE)
    )
    {
        // if on the property search pages and a query string set
        $_SESSION['last_search'] = $_SERVER['REQUEST_URI'];
        if (strpos($_SERVER['REQUEST_URI'], '?') === FALSE)
        {
            $_SESSION['last_search'] = $_SERVER['REQUEST_URI'] . '?' . $_SERVER['QUERY_STRING'];
        }
    }
    session_write_close();
}

if ( isset($_SESSION['last_search']) && $_SESSION['last_search'] != '' ) 
{
    echo '<a href="' . $_SESSION['last_search'] . '">' . __( 'Back To Search', 'propertyhive' ) . '</a>';
}
else
{
    // Cater for when user might've visited URL direct
    // In which case just link back to search page
    echo '<a href="' . get_permalink(ph_get_page_id( 'search_results' )) . '">' . __( 'Back To Search', 'propertyhive' ) . '</a>';
}