WordPress/wp-includes/js/heartbeat.min.js

1 line
5.2 KiB
JavaScript

!function(f,w){w.wp=w.wp||{},w.wp.heartbeat=new function(){var e,n,t,a,i=f(document),r={suspend:!1,suspendEnabled:!0,screenId:"",url:"",lastTick:0,queue:{},mainInterval:60,tempInterval:0,originalInterval:0,minimalInterval:0,countdown:0,connecting:!1,connectionError:!1,errorcount:0,hasConnected:!1,hasFocus:!0,userActivity:0,userActivityEvents:!1,checkFocusTimer:0,beatTimer:0};function o(){return(new Date).getTime()}function c(e){var n,t=e.src;if(!t||!/^https?:\/\//.test(t)||(n=w.location.origin||w.location.protocol+"//"+w.location.host,0===t.indexOf(n)))try{if(e.contentWindow.document)return 1}catch(e){}}function u(){r.hasFocus&&!document.hasFocus()?m():!r.hasFocus&&document.hasFocus()&&d()}function s(e,n){var t;if(e){switch(e){case"abort":break;case"timeout":t=!0;break;case"error":if(503===n&&r.hasConnected){t=!0;break}case"parsererror":case"empty":case"unknown":r.errorcount++,2<r.errorcount&&r.hasConnected&&(t=!0)}t&&!p()&&(r.connectionError=!0,i.trigger("heartbeat-connection-lost",[e,n]))}}function l(){var e;r.connecting||r.suspend||(r.lastTick=o(),e=f.extend({},r.queue),r.queue={},i.trigger("heartbeat-send",[e]),e={data:e,interval:r.tempInterval?r.tempInterval/1e3:r.mainInterval/1e3,_nonce:"object"==typeof w.heartbeatSettings?w.heartbeatSettings.nonce:"",action:"heartbeat",screen_id:r.screenId,has_focus:r.hasFocus},r.connecting=!0,r.xhr=f.ajax({url:r.url,type:"post",timeout:3e4,data:e,dataType:"json"}).always(function(){r.connecting=!1,v()}).done(function(e,n,t){var a;e?(r.hasConnected=!0,p()&&(r.errorcount=0,r.connectionError=!1,i.trigger("heartbeat-connection-restored")),e.nonces_expired&&i.trigger("heartbeat-nonces-expired"),e.heartbeat_interval&&(a=e.heartbeat_interval,delete e.heartbeat_interval),i.trigger("heartbeat-tick",[e,n,t]),a&&b(a)):s("empty")}).fail(function(e,n,t){s(n||"unknown",e.status),i.trigger("heartbeat-error",[e,n,t])}))}function v(){var e=o()-r.lastTick,n=r.mainInterval;r.suspend||(r.hasFocus?0<r.countdown&&r.tempInterval&&(n=r.tempInterval,r.countdown--,r.countdown<1&&(r.tempInterval=0)):n=12e4,r.minimalInterval&&n<r.minimalInterval&&(n=r.minimalInterval),w.clearTimeout(r.beatTimer),e<n?r.beatTimer=w.setTimeout(function(){l()},n-e):l())}function m(){r.hasFocus=!1}function d(){r.userActivity=o(),r.suspend=!1,r.hasFocus||(r.hasFocus=!0,v())}function h(){r.userActivityEvents=!1,i.off(".wp-heartbeat-active"),f("iframe").each(function(e,n){c(n)&&f(n.contentWindow).off(".wp-heartbeat-active")}),d()}function I(){var e=r.userActivity?o()-r.userActivity:0;3e5<e&&r.hasFocus&&m(),(r.suspendEnabled&&6e5<e||36e5<e)&&(r.suspend=!0),r.userActivityEvents||(i.on("mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active",function(){h()}),f("iframe").each(function(e,n){c(n)&&f(n.contentWindow).on("mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active",function(){h()})}),r.userActivityEvents=!0)}function p(){return r.connectionError}function b(e,n){var t,a=r.tempInterval||r.mainInterval;if(e){switch(e){case"fast":case 5:t=5e3;break;case 15:t=15e3;break;case 30:t=3e4;break;case 60:t=6e4;break;case 120:t=12e4;break;case"long-polling":return r.mainInterval=0;default:t=r.originalInterval}5e3===(t=r.minimalInterval&&t<r.minimalInterval?r.minimalInterval:t)?(n=(n=parseInt(n,10)||30)<1||30<n?30:n,r.countdown=n,r.tempInterval=t):(r.countdown=0,r.tempInterval=0,r.mainInterval=t),t!==a&&v()}return r.tempInterval?r.tempInterval/1e3:r.mainInterval/1e3}return"string"==typeof w.pagenow&&(r.screenId=w.pagenow),"string"==typeof w.ajaxurl&&(r.url=w.ajaxurl),"object"==typeof w.heartbeatSettings&&(e=w.heartbeatSettings,!r.url&&e.ajaxurl&&(r.url=e.ajaxurl),e.interval&&(r.mainInterval=e.interval,r.mainInterval<15?r.mainInterval=15:120<r.mainInterval&&(r.mainInterval=120)),e.minimalInterval&&(e.minimalInterval=parseInt(e.minimalInterval,10),r.minimalInterval=0<e.minimalInterval&&e.minimalInterval<=600?1e3*e.minimalInterval:0),r.minimalInterval&&r.mainInterval<r.minimalInterval&&(r.mainInterval=r.minimalInterval),r.screenId||(r.screenId=e.screenId||"front"),"disable"===e.suspension&&(r.suspendEnabled=!1)),r.mainInterval=1e3*r.mainInterval,r.originalInterval=r.mainInterval,void 0!==document.hidden?(n="hidden",a="visibilitychange",t="visibilityState"):void 0!==document.msHidden?(n="msHidden",a="msvisibilitychange",t="msVisibilityState"):void 0!==document.webkitHidden&&(n="webkitHidden",a="webkitvisibilitychange",t="webkitVisibilityState"),n&&(document[n]&&(r.hasFocus=!1),i.on(a+".wp-heartbeat",function(){"hidden"===document[t]?(m(),w.clearInterval(r.checkFocusTimer)):(d(),document.hasFocus&&(r.checkFocusTimer=w.setInterval(u,1e4)))})),document.hasFocus&&(r.checkFocusTimer=w.setInterval(u,1e4)),f(w).on("unload.wp-heartbeat",function(){r.suspend=!0,r.xhr&&4!==r.xhr.readyState&&r.xhr.abort()}),w.setInterval(I,3e4),i.ready(function(){r.lastTick=o(),v()}),{hasFocus:function(){return r.hasFocus},connectNow:function(){r.lastTick=0,v()},disableSuspend:function(){r.suspendEnabled=!1},interval:b,hasConnectionError:p,enqueue:function(e,n,t){return!!e&&((!t||!this.isQueued(e))&&(r.queue[e]=n,!0))},dequeue:function(e){e&&delete r.queue[e]},isQueued:function(e){if(e)return r.queue.hasOwnProperty(e)},getQueuedItem:function(e){if(e)return this.isQueued(e)?r.queue[e]:void 0}}}}(jQuery,window);