bux23
11/29/2016 - 1:09 PM

JS COOKIE FOR USER NOTICES

JS COOKIE FOR USER NOTICES

/* LOGGED NOTICE MESSAGE */
.logged_notice {
    position: fixed;
    width: 100%;
    background: #f2f2f2;
    border: 1px solid #333;
    box-shadow: 0 0 4px rgba(0,0,0,0.2);
    top: 0;
    text-align: center;
    z-index: 99999;
    padding: 5px;
    display: none;
}
<div class="logged_notice">
    <div class="logged_notice_int">
        Dear user, your profile is still uncompleted.
    </div>
    <div class="btn btn-danger logged_notice_close">
        close
    </div>
</div>
//////////////////////////////////////////////////////////////
///////// ENQUEUE LOGGED IN SCRIPTS
//////////////////////////////////////////////////////////////
if(is_user_logged_in()) {
    function enqueue_logged_js() {
       wp_enqueue_script( 'loggedjs', get_theme_root_uri().'/woptimizr/js/logged_js.js' );
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_logged_js', 10 );
    
    function insert_logged_notice() {
      get_template_part('/template-parts/logged_notice');
    }
    add_action( '__before_footer', 'insert_logged_notice', 10 );
}

//////////////////////////////////////////////////////////////
///////// KILL JS COOKIES
//////////////////////////////////////////////////////////////

if(!is_user_logged_in()) {
    function kill_js_cookies() {
        echo '<script type="text/javascript">Cookies.remove('notice_1');</script>';
    }
    add_action( '__after_footer', 'kill_js_cookies', 99 );
}
////////////////////////////////////////////////////////////////////////////////
//SHOW HIDE DIV COOKIE PERSISTENT 
////////////////////////////////////////////////////////////////////////////////
// INCLUDE https://github.com/js-cookie/js-cookie
function show_hide_notice(cook, element, btnClose) {
    if(Cookies.get(cook) != "closed") {
        Cookies.set(cook, 'open');
        jQuery(element).slideDown();
    }
    jQuery(document).on('click', btnClose, function () {
        jQuery(element).slideUp(); 
        Cookies.set(cook, 'closed');
    });
}

jQuery(document).ready(function($) {
    show_hide_notice("notice_1",".logged_notice",".logged_notice_close");
});