var recheck_sefave_btns; /* * Create a global function called sefave_login_request to replace the default login alert box. */ jQuery(document).ready(function() { var $ = jQuery; var check = {}; var handle_fave_click = function(e) { if(e && e.preventDefault) { e.preventDefault(); } var postid; var me = this; postid = parseInt(this.getAttribute('data-postid')); if(postid) { jQuery(me).addClass('fave-saving'); $.ajax({ "url": ajaxurl, "method": "post", "dataType": "json", "data": "action=sefavebtn&postid="+postid, "error": function(xhr, textStatus, error) { jQuery(me).removeClass('fave-saving'); if(window.console && window.console.log) { window.console.log('FAVE ERROR: '+textStatus); } }, "success": function(data) { jQuery(me).removeClass('fave-saving'); if(data.error) { if(data.error == 'notloggedin') { if(window.sefave_login_request) { window.sefave_login_request(); } else { var performlog = confirm('Login to use the favorites buttons.'); if(performlog) { window.location = ajaxurl + '?action=favelogin&returl='+encodeURIComponent(window.location)+'&post='+postid; } } } else { window.console.log('FAVE ERROR: '+data.error); return false; } } if(data.newstatus) { $(me).addClass('active-favorite'); } else { $(me).removeClass('active-favorite'); } } }); } } $('.sefave-button').click(handle_fave_click); $('.sefave-button').addClass('evt-bound'); $('.sefave-button').each(function() { check[this.getAttribute('data-postid')] = null; }); var check_unchecked = function() { var x; var postid = ''; for(x in check) { if(check[x] === null) { postid += ('postid[]='+x+'&'); } } if(postid) { $.ajax({ "url": ajaxurl, "method": "get", "dataType": "json", "data": "action=sefavestate&"+postid, "complete": function() { window.setTimeout(recheck_sefave_btns, 3000); }, "success": function(data) { if(data.posts) { var y, u; for(y in data.posts) { check[u = y.replace('P','')] = data.posts[y]; if(check[u]) { $('.sefave-button[data-postid="'+parseInt(u)+'"]').addClass('active-favorite'); } else { $('.sefave-button[data-postid="'+parseInt(u)+'"]').removeClass('active-favorite'); } } } } }); } else { window.setTimeout(recheck_sefave_btns, 1000); } } recheck_sefave_btns = function() { $('.sefave-button').each(function() { if(check[this.getAttribute('data-postid')] === undefined) { check[this.getAttribute('data-postid')] = null; } if(jQuery(this).hasClass('evt-bound') == false) { $(this).click(handle_fave_click); $(this).addClass('evt-bound'); } }); check_unchecked(); } check_unchecked(); });