jQuery(document).ready(function() {
const fel = jQuery("#follow");
const is_btm_bar = (jQuery("body").data("style") == "bottombar");
const set_unfollow = function() {
const follow_text = is_btm_bar ? ' Følg Blog' : "Følg blog +";
fel.html(follow_text);
fel.addClass("unfollow");
fel.removeClass("follow");
fel.attr("data-action", "add");
}
const set_follow = function() {
const follow_text = is_btm_bar ? ' Følg Blog' : "Følger";
fel.html(follow_text);
fel.addClass("follow");
fel.removeClass("unfollow");
fel.attr("data-action", "delete");
}
const load_user_state = function() {
jQuery.ajax({
type: "POST",
url: BdSet.config.wp_home + "/wp-content/themes/bloggersdelight/follow/get_cookie_user_id.php",
xhrFields: {
"withCredentials": true
},
async: true,
crossDomain: true,
complete: function(user_data) {
if (!user_data ||
!user_data.responseText ||
!user_data.responseText.match(/\d+/) ||
!user_data.responseText.match(/\d+/)[0] ) {
set_unfollow()
return false;
}
BdSet.bd_user_id = user_data.responseText.match(/\d+/)[0];
var blog_id = jQuery("body").data("blog-id");
if (BdSet.bd_user_id) {
//check if user is following blog
jQuery.post(
BdSet.config.wp_home + "/wp-content/themes/bloggersdelight/follow/check_if_user_follows_blog.php",
{ "user_id": BdSet.bd_user_id, "blog_id": blog_id },
function(follow_data) {
if (follow_data == 'true') {
set_follow()
} else {
set_unfollow()
}
}
);
}
return false;
}
});
}
window.addEventListener('message', function(event) {
if(parseInt(event.data.user_id)) {
BdSet.bd_user_id = event.data.user_id
set_follow();
}
}, false);
jQuery("#follow").on("click", function() {
const blog_id = jQuery("body").data("blog-id");
const action = jQuery(this).attr("data-action");
//if not logged in show overlay to logged in else save
if (BdSet.bd_user_id) {
if (action == 'add') {
jQuery.post(
BdSet.config.wp_home + "/wp-content/themes/bloggersdelight/follow/follow_create.php", { "blog_id": blog_id, "user_id": BdSet.bd_user_id, "action": "add" }
);
set_follow()
} else if (action == 'delete') {
jQuery.post(
"/wp-content/themes/bloggersdelight/follow/follow_create.php", { "blog_id": blog_id, "user_id": BdSet.bd_user_id, "action": "delete" }
);
set_unfollow()
}
} else {
window.parent.postMessage({ message: "show_follow_overlay" }, "*");
}
return false;
});
load_user_state();
})