jQuery(document).ready(function() { // Load notification count when the page loads wppm_loadNotifications(); // Refresh every 30 seconds setInterval(function () { if (!document.hidden) { wppm_loadNotifications(); } }, 60000); }); jQuery(document).on('click', function (e) { if ( !jQuery(e.target).closest('#wppm_notification_bell').length && !jQuery(e.target).closest('#wppm-notification-container').length ) { jQuery('#wppm-notification-container').hide(); } }); function wppm_loadNotifications() { jQuery.ajax({ url: wppm_notification.ajax_url, type: "POST", data: { action: "wppm_get_notifications" }, dataType: "json", success: function(response) { if(response.length > 0){ jQuery("#wppm_notification_bell").addClass("wppm-has-notification"); jQuery(".wppm-badge").text(response.length).show(); }else{ jQuery("#wppm_notification_bell").removeClass("wppm-has-notification"); jQuery(".wppm-badge").text("0").show(); } var html = ""; if(response.length > 0){ jQuery.each(response, function(i, notification){ html += '
'; html += '
'+notification.message+'
'; html += ''+notification.created_at+''; html += '
'; }); }else{ html = '
No new notifications
'; } jQuery("#wppm-notification-container").html(html); } }); } jQuery(document).on("click", ".wppm-notification-item", function () { var id = jQuery(this).data("id"); jQuery.ajax({ url: wppm_notification.ajax_url, type: "POST", data: { action: "wppm_mark_notification_read", id: id } }); // Then open the task }); jQuery(document).on('click', '#wppm_notification_bell', function () { wppm_loadNotifications(); jQuery('#wppm-notification-container').toggle(); });