|
1
|
|
-(function(){"use strict";const _="X-WP-Nonce";function E(t,n){const s=M();if(!s)return n;const o=S(t);if(!o||!x(o))return n;const e=n?.headers??(typeof Request<"u"&&t instanceof Request?t.headers:void 0),a=new Headers(e??{});return a.has(_)?n:(a.set(_,s),{...n??{},headers:a})}function M(){if(typeof window>"u")return;const n=window.desktopModeConfig?.restNonce;return typeof n=="string"&&n.length>0?n:void 0}function S(t){try{const n=typeof window<"u"&&window.location?window.location.href:void 0;return typeof t=="string"?new URL(t,n):t instanceof URL?t:typeof Request<"u"&&t instanceof Request?new URL(t.url,n):null}catch{return null}}function x(t){return typeof window>"u"||!window.location||t.origin!==window.location.origin?!1:!!(t.pathname.includes("/wp-json/")||t.searchParams.has("rest_route"))}function k(t,n,s={}){const o=window.wp?.desktop?.fetch;if(typeof o=="function")return o(t,n,s);const e=E(t,n);return fetch(t,e)}const P="desktop-mode/post-stats",H=5*6e4,w=6,m={published:"#3b82f6",pending:"#fbbf24",draft:"#a5b4fc"};function I(t){return t.getFullYear()+"-"+String(t.getMonth()+1).padStart(2,"0")}function T(t){const[,n]=t.split("-");return new Date(2e3,parseInt(n,10)-1,1).toLocaleString(void 0,{month:"short"})}function W(){const t=new Date;return Array.from({length:w},(n,s)=>{const o=new Date(t.getFullYear(),t.getMonth()-(w-1-s),1),e=I(o);return{ym:e,label:T(e),published:0,pending:0,draft:0}})}async function D(){const t=window.wpApiSettings?.root??"/wp-json/",n=new Date;n.setMonth(n.getMonth()-w),n.setDate(1);const s=n.toISOString(),o=["publish","draft","pending"],e=[];for(const a of o){let l=1,r=1/0;for(;e.length<200&&(l-1)*100<r;){const f=await k(t.replace(/\/$/,"")+`/wp/v2/posts?per_page=100&page=${l}&status=${a}&after=${encodeURIComponent(s)}&_fields=id,date,status`,{credentials:"same-origin"},{source:"desktop-mode/post-stats",silent:!0});if(!f.ok)break;r=parseInt(f.headers.get("X-WP-Total")??"0",10);const i=await f.json();if(!Array.isArray(i)||i.length===0)break;e.push(...i),l++}}return e}function A(t,n,s,o,e,a){typeof t.roundRect=="function"?t.roundRect(n,s,o,e,a):t.rect(n,s,o,e)}function L(t,n){const s=window.devicePixelRatio||1,o=t.getBoundingClientRect();if(o.width===0||o.height===0)return;t.width=Math.round(o.width*s),t.height=Math.round(o.height*s);const e=t.getContext("2d");if(!e)return;e.scale(s,s);const a=o.width,l=o.height,r={top:18,right:10,bottom:22,left:28},f=a-r.left-r.right,i=l-r.top-r.bottom;if(f<=0||i<=0)return;const g=Math.max(1,...n.map(c=>c.published+c.pending+c.draft)),d=f/n.length,u=d*.2,h=Math.max(1,d-u*2);e.strokeStyle="rgba(0,0,0,0.07)",e.lineWidth=1;for(let c=0;c<=3;c++){const p=r.top+i-i*c/3;e.beginPath(),e.moveTo(r.left,p),e.lineTo(r.left+f,p),e.stroke(),e.fillStyle="rgba(0,0,0,0.35)",e.font="9px -apple-system, sans-serif",e.textAlign="right",e.fillText(String(Math.round(g*c/3)),r.left-4,p+3)}for(let c=0;c<n.length;c++){const p=n[c],C=r.left+d*c+u;let R=r.top+i;for(const y of[{value:p.published,color:m.published},{value:p.pending,color:m.pending},{value:p.draft,color:m.draft}]){if(y.value===0)continue;const N=y.value/g*i;R-=N,e.fillStyle=y.color,e.beginPath(),A(e,C,R,h,N,2),e.fill()}e.fillStyle="rgba(0,0,0,0.5)",e.font="9px -apple-system, sans-serif",e.textAlign="center",e.fillText(p.label,C+h/2,r.top+i+13)}}function v(t,n,s){const o=document.createElement("div");o.className="dm-poststats__header";const e=document.createElement("span");e.className="dm-poststats__title",e.textContent="Post Stats";const a=document.createElement("span");a.className="dm-poststats__total",a.textContent=s?"":`${n} post${n!==1?"s":""} in 6 mo`,o.appendChild(e),o.appendChild(a),t.appendChild(o)}function O(t){const n=document.createElement("div");n.className="dm-poststats__legend";for(const[s,o]of[["Published",m.published],["Pending",m.pending],["Draft",m.draft]]){const e=document.createElement("div");e.className="dm-poststats__legend-item";const a=document.createElement("span");a.className="dm-poststats__legend-swatch",a.style.background=o,e.appendChild(a),e.appendChild(document.createTextNode(s)),n.appendChild(e)}t.appendChild(n)}const U=async(t,n)=>{let s=!1,o=null,e=null;const a=async()=>{if(!s)try{const l=await D(),r=W();for(const d of l){const u=d.date?d.date.slice(0,7):null,h=u?r.find(c=>c.ym===u):null;h&&(d.status==="publish"?h.published++:d.status==="pending"?h.pending++:d.status==="draft"&&h.draft++)}const f=l.length;if(s)return;if(t.innerHTML="",v(t,f,!1),f===0){const d=document.createElement("div");d.className="dm-poststats__empty",d.textContent="No posts in the last 6 months.",t.appendChild(d);return}const i=document.createElement("div");i.className="dm-poststats__canvas-wrap";const g=document.createElement("canvas");g.className="dm-poststats__canvas",i.appendChild(g),t.appendChild(i),O(t),e?.disconnect(),e=new ResizeObserver(d=>{if(s)return;const u=d[0];u&&u.contentRect.width>0&&u.contentRect.height>0&&L(g,r)}),e.observe(i)}catch{if(!s){t.innerHTML="",v(t,0,!0);const l=document.createElement("div");l.className="dm-poststats__error",l.textContent="Could not load post data.",t.appendChild(l)}}};return await a(),o=setInterval(a,H),()=>{s=!0,o!==null&&clearInterval(o),e?.disconnect()}},b=window;b.desktopModeWidgets=b.desktopModeWidgets??{},b.desktopModeWidgets[P]=U})(); |
|
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})(); |