|
1
|
|
-(function(){"use strict";const w="X-WP-Nonce";function E(e,n){const o=N();if(!o)return n;const s=x(e);if(!s||!S(s))return n;const t=n?.headers??(typeof Request<"u"&&e instanceof Request?e.headers:void 0),r=new Headers(t??{});return r.has(w)?n:(r.set(w,o),{...n??{},headers:r})}function N(){if(typeof window>"u")return;const n=window.openStationConfig?.restNonce;return typeof n=="string"&&n.length>0?n:void 0}function x(e){try{const n=typeof window<"u"&&window.location?window.location.href:void 0;return typeof e=="string"?new URL(e,n):e instanceof URL?e:typeof Request<"u"&&e instanceof Request?new URL(e.url,n):null}catch{return null}}function S(e){return typeof window>"u"||!window.location||e.origin!==window.location.origin?!1:!!(e.pathname.includes("/wp-json/")||e.searchParams.has("rest_route"))}function L(e,n,o={}){const s=window.wp?.os?.fetch;if(typeof s=="function")return s(e,n,o);const t=E(e,n);return fetch(e,t)}function P(e,n){let o=null,s=Date.now();const t=()=>{s=Date.now(),e()},r=()=>{o===null&&(o=setInterval(t,n))},d=()=>{o!==null&&(clearInterval(o),o=null)},a=()=>{if(document.hidden){d();return}Date.now()-s>=n&&t(),r()};return document.addEventListener("visibilitychange",a),document.hidden||r(),{stop(){d(),document.removeEventListener("visibilitychange",a)}}}const H="desktop-mode/post-stats",W=5*6e4,h={published:"#3b82f6",pending:"#fbbf24",draft:"#a5b4fc"};function D(e){const[,n]=e.split("-");return new Date(2e3,parseInt(n,10)-1,1).toLocaleString(void 0,{month:"short"})}async function I(){const e=window.wpApiSettings?.root??"/wp-json/",n=await L(e.replace(/\/$/,"")+"/desktop-mode/v1/post-stats",{credentials:"same-origin"},{source:"desktop-mode/post-stats",silent:!0});if(!n.ok)throw new Error(`post-stats request failed: ${n.status}`);const o=await n.json();return(Array.isArray(o.months)?o.months:[]).slice(-6).map(t=>({ym:t.ym,label:D(t.ym),published:Number(t.publish)||0,pending:Number(t.pending)||0,draft:Number(t.draft)||0}))}function T(e,n,o,s,t,r){typeof e.roundRect=="function"?e.roundRect(n,o,s,t,r):e.rect(n,o,s,t)}function A(e,n){const o=window.devicePixelRatio||1,s=e.getBoundingClientRect();if(s.width===0||s.height===0)return;e.width=Math.round(s.width*o),e.height=Math.round(s.height*o);const t=e.getContext("2d");if(!t)return;t.scale(o,o);const r=s.width,d=s.height,a={top:18,right:10,bottom:22,left:28},u=r-a.left-a.right,l=d-a.top-a.bottom;if(u<=0||l<=0)return;const f=Math.max(1,...n.map(i=>i.published+i.pending+i.draft)),c=u/n.length,y=c*.2,v=Math.max(1,c-y*2);t.strokeStyle="rgba(0,0,0,0.07)",t.lineWidth=1;for(let i=0;i<=3;i++){const p=a.top+l-l*i/3;t.beginPath(),t.moveTo(a.left,p),t.lineTo(a.left+u,p),t.stroke(),t.fillStyle="rgba(0,0,0,0.35)",t.font="9px -apple-system, sans-serif",t.textAlign="right",t.fillText(String(Math.round(f*i/3)),a.left-4,p+3)}for(let i=0;i<n.length;i++){const p=n[i],_=a.left+c*i+y;let R=a.top+l;for(const g of[{value:p.published,color:h.published},{value:p.pending,color:h.pending},{value:p.draft,color:h.draft}]){if(g.value===0)continue;const C=g.value/f*l;R-=C,t.fillStyle=g.color,t.beginPath(),T(t,_,R,v,C,2),t.fill()}t.fillStyle="rgba(0,0,0,0.5)",t.font="9px -apple-system, sans-serif",t.textAlign="center",t.fillText(p.label,_+v/2,a.top+l+13)}}function b(e,n,o){const s=document.createElement("div");s.className="dm-poststats__header";const t=document.createElement("span");t.className="dm-poststats__title",t.textContent="Post Stats";const r=document.createElement("span");r.className="dm-poststats__total",r.textContent=o?"":`${n} post${n!==1?"s":""} in 6 mo`,s.appendChild(t),s.appendChild(r),e.appendChild(s)}function M(e){const n=document.createElement("div");n.className="dm-poststats__legend";for(const[o,s]of[["Published",h.published],["Pending",h.pending],["Draft",h.draft]]){const t=document.createElement("div");t.className="dm-poststats__legend-item";const r=document.createElement("span");r.className="dm-poststats__legend-swatch",r.style.background=s,t.appendChild(r),t.appendChild(document.createTextNode(o)),n.appendChild(t)}e.appendChild(n)}const k=async(e,n)=>{let o=!1,s=null;const t=async()=>{if(!o)try{const d=await I(),a=d.reduce((f,c)=>f+c.published+c.pending+c.draft,0);if(o)return;if(e.innerHTML="",b(e,a,!1),a===0){const f=document.createElement("div");f.className="dm-poststats__empty",f.textContent="No posts in the last 6 months.",e.appendChild(f);return}const u=document.createElement("div");u.className="dm-poststats__canvas-wrap";const l=document.createElement("canvas");l.className="dm-poststats__canvas",u.appendChild(l),e.appendChild(u),M(e),s?.disconnect(),s=new ResizeObserver(f=>{if(o)return;const c=f[0];c&&c.contentRect.width>0&&c.contentRect.height>0&&A(l,d)}),s.observe(u)}catch{if(!o){e.innerHTML="",b(e,0,!0);const d=document.createElement("div");d.className="dm-poststats__error",d.textContent="Could not load post data.",e.appendChild(d)}}};await t();const r=P(t,W);return()=>{o=!0,r.stop(),s?.disconnect()}},m=window;m.openStationWidgets=m.openStationWidgets??{},m.openStationWidgets[H]=k})(); |
|
1
|
+(function(){"use strict";const v="X-WP-Nonce";function A(e,t){const s=D();if(!s)return t;const o=H(e);if(!o||!O(o))return t;const n=t?.headers??(typeof Request<"u"&&e instanceof Request?e.headers:void 0),i=new Headers(n??{});return i.has(v)?t:(i.set(v,s),{...t??{},headers:i})}function D(){if(typeof window>"u")return;const t=window.openStationConfig?.restNonce;return typeof t=="string"&&t.length>0?t:void 0}function H(e){try{const t=typeof window<"u"&&window.location?window.location.href:void 0;return typeof e=="string"?new URL(e,t):e instanceof URL?e:typeof Request<"u"&&e instanceof Request?new URL(e.url,t):null}catch{return null}}function O(e){return typeof window>"u"||!window.location||e.origin!==window.location.origin?!1:!!(e.pathname.includes("/wp-json/")||e.searchParams.has("rest_route"))}function W(e,t,s={}){const o=window.wp?.os?.fetch;if(typeof o=="function")return o(e,t,s);const n=A(e,t);return fetch(e,n)}class y extends Error{constructor(t,s){super(t||s.serverMessage||`HTTP ${s.status}`),this.name="RestError",this.status=s.status,this.code=s.code,this.data=s.data,this.serverMessage=s.serverMessage??""}}function _(e){return e instanceof y}function $(e){const t=typeof e=="object"&&e!==null?e:void 0;return{code:typeof t?.code=="string"?t.code:void 0,data:t?.data,serverMessage:typeof t?.message=="string"?t.message:""}}function F(e,t,s=""){return new y(s,{status:e,...$(t)})}async function j(e,t=""){let s=null;try{s=await e.json()}catch{s=null}return F(e.status,s,t)}const U="desktop-mode";function E(){return window.wp?.i18n}function h(e,t=U){return E()?.__(e,t)??e}function R(e,...t){const s=E()?.sprintf;if(s)return s(e,...t);let o=0;return e.replace(/%(?:(\d+)\$)?[sd]/g,(n,i)=>{const r=i?Number.parseInt(i,10)-1:o++;return String(t[r]??"")})}const q="rest_cookie_invalid_nonce",k=()=>h("Could not reach the site. Check your connection and try again.","desktop-mode");function C(e){return typeof navigator<"u"&&navigator.onLine===!1?!0:e instanceof TypeError}function V(e){return _(e)?e.code===q||e.status===401?"session":e.status>=200&&e.status<300?"unreadable":e.status===404?"gone":e.status>=500?"server":e.status>=400?"refused":"unknown":C(e)?"offline":"unknown"}function B(e){return _(e)?e.serverMessage:C(e)?k():e instanceof Error?e.message:""}function G(e,t){const{fallback:s,lead:o}=t,n=V(e),i=n==="offline"||n==="unreadable"?"warning":"error";let r="",a=!0;switch(n){case"offline":r=k();break;case"session":r=h("Your session has expired. Reload the page and try again.","desktop-mode");break;case"unreadable":r=h("The site sent an unreadable reply. Reload the page to check whether the change went through.","desktop-mode");break;case"gone":case"refused":case"server":{const c=e;c.serverMessage?r=c.serverMessage:n==="gone"?(r=h("It no longer exists. Reload the page to catch up.","desktop-mode"),a=!1):n==="server"?(r=R(h("The server answered with error %d.","desktop-mode"),c.status),a=!1):(r=R(h("The server refused the request (HTTP %d).","desktop-mode"),c.status),a=!1);break}case"unknown":default:r=B(e);break}let l;return o?l=r?`${o}: ${r}`:s:r?a?l=r:l=`${s} ${r}`:l=s,{message:l,reason:r,kind:n,type:i}}function X(e,t){let s=null,o=Date.now();const n=()=>{o=Date.now(),e()},i=()=>{s===null&&(s=setInterval(n,t))},r=()=>{s!==null&&(clearInterval(s),s=null)},a=()=>{if(document.hidden){r();return}Date.now()-o>=t&&n(),i()};return document.addEventListener("visibilitychange",a),document.hidden||i(),{stop(){r(),document.removeEventListener("visibilitychange",a)}}}const z="desktop-mode/post-stats",K=5*6e4,m={published:"#3b82f6",pending:"#fbbf24",draft:"#a5b4fc"};function Y(e){const[,t]=e.split("-");return new Date(2e3,parseInt(t,10)-1,1).toLocaleString(void 0,{month:"short"})}async function J(){const e=window.wpApiSettings?.root??"/wp-json/",t=await W(e.replace(/\/$/,"")+"/desktop-mode/v1/post-stats",{credentials:"same-origin"},{source:"desktop-mode/post-stats",silent:!0});if(!t.ok)throw await j(t);const s=await t.json();return(Array.isArray(s.months)?s.months:[]).slice(-6).map(n=>({ym:n.ym,label:Y(n.ym),published:Number(n.publish)||0,pending:Number(n.pending)||0,draft:Number(n.draft)||0}))}function Q(e,t,s,o,n,i){typeof e.roundRect=="function"?e.roundRect(t,s,o,n,i):e.rect(t,s,o,n)}function Z(e,t){const s=window.devicePixelRatio||1,o=e.getBoundingClientRect();if(o.width===0||o.height===0)return;e.width=Math.round(o.width*s),e.height=Math.round(o.height*s);const n=e.getContext("2d");if(!n)return;n.scale(s,s);const i=o.width,r=o.height,a={top:18,right:10,bottom:22,left:28},l=i-a.left-a.right,c=r-a.top-a.bottom;if(l<=0||c<=0)return;const p=getComputedStyle(e),u=(d,f)=>p.getPropertyValue(d).trim()||f,ne=u("--os-ui-color-border","rgba( 255, 251, 255, 0.12 )"),x=u("--os-ui-color-text-subtle","rgba( 255, 251, 255, 0.7 )"),T=Math.max(1,...t.map(d=>d.published+d.pending+d.draft)),w=l/t.length,S=w*.2,M=Math.max(1,w-S*2);n.strokeStyle=ne,n.lineWidth=1;for(let d=0;d<=3;d++){const f=a.top+c-c*d/3;n.beginPath(),n.moveTo(a.left,f),n.lineTo(a.left+l,f),n.stroke(),n.fillStyle=x,n.font="9px -apple-system, sans-serif",n.textAlign="right",n.fillText(String(Math.round(T*d/3)),a.left-4,f+3)}for(let d=0;d<t.length;d++){const f=t[d],L=a.left+w*d+S;let I=a.top+c;for(const b of[{value:f.published,color:m.published},{value:f.pending,color:m.pending},{value:f.draft,color:m.draft}]){if(b.value===0)continue;const P=b.value/T*c;I-=P,n.fillStyle=b.color,n.beginPath(),Q(n,L,I,M,P,2),n.fill()}n.fillStyle=x,n.font="9px -apple-system, sans-serif",n.textAlign="center",n.fillText(f.label,L+M/2,a.top+c+13)}}function N(e,t,s){const o=document.createElement("div");o.className="dm-poststats__header";const n=document.createElement("span");n.className="dm-poststats__title",n.textContent="Post Stats";const i=document.createElement("span");i.className="dm-poststats__total",i.textContent=s?"":`${t} post${t!==1?"s":""} in 6 mo`,o.appendChild(n),o.appendChild(i),e.appendChild(o)}function ee(e){const t=document.createElement("div");t.className="dm-poststats__legend";for(const[s,o]of[["Published",m.published],["Pending",m.pending],["Draft",m.draft]]){const n=document.createElement("div");n.className="dm-poststats__legend-item";const i=document.createElement("span");i.className="dm-poststats__legend-swatch",i.style.background=o,n.appendChild(i),n.appendChild(document.createTextNode(s)),t.appendChild(n)}e.appendChild(t)}const te=async(e,t)=>{let s=!1,o=null;const n=async()=>{if(!s)try{const r=await J(),a=r.reduce((p,u)=>p+u.published+u.pending+u.draft,0);if(s)return;if(e.innerHTML="",N(e,a,!1),a===0){const p=document.createElement("div");p.className="dm-poststats__empty",p.textContent="No posts in the last 6 months.",e.appendChild(p);return}const l=document.createElement("div");l.className="dm-poststats__canvas-wrap";const c=document.createElement("canvas");c.className="dm-poststats__canvas",l.appendChild(c),e.appendChild(l),ee(e),o?.disconnect(),o=new ResizeObserver(p=>{if(s)return;const u=p[0];u&&u.contentRect.width>0&&u.contentRect.height>0&&Z(c,r)}),o.observe(l)}catch(r){if(!s){e.innerHTML="",N(e,0,!0);const a=document.createElement("div");a.className="dm-poststats__error",a.textContent=G(r,{fallback:"Could not load post data."}).message,e.appendChild(a)}}};await n();const i=X(n,K);return()=>{s=!0,i.stop(),o?.disconnect()}},g=window;g.openStationWidgets=g.openStationWidgets??{},g.openStationWidgets[z]=te})(); |