28 lines
27 KiB
JavaScript
28 lines
27 KiB
JavaScript
|
|
(()=>{"use strict";var e={4962:(e,t,o)=>{o.d(t,{NOTICE_TYPES:()=>r,createDismissibleNotice:()=>n,createNotice:()=>s});var i=o(7723);const r={SUCCESS:"success",ERROR:"error",WARNING:"warning",INFO:"info"};function s(e,t=r.SUCCESS,o=!0,s=null){const n=document.querySelector(".wrap h1")||document.querySelector(".wrap h2")||document.querySelector(".wrap");if(!n)return void console.error("Could not find container for admin notice");let a=e;if(s&&"object"==typeof s){const t=s.message||"",o=s.code||"";t&&(a=`${e}: ${t}`),o&&(a=`${a}<br />Error Code: <code>${o}</code>`)}const c=document.createElement("div");if(c.className=`notice notice-${t}${o?" is-dismissible":""}`,c.style.transition="opacity 0.5s ease-out",c.innerHTML=`\n\t\t<p>${a}</p>\n\t\t${o?`<button type="button" class="notice-dismiss"><span class="screen-reader-text">${(0,i.__)("Dismiss this notice.")}</span></button>`:""}\n\t`,n.insertAdjacentElement("afterend",c),o){const e=c.querySelector(".notice-dismiss");e?.addEventListener("click",()=>{c.remove()})}}function n(e,t=r.SUCCESS,o=null){s(e,t,!1,o);const i=document.querySelector(".wrap h1")||document.querySelector(".wrap h2")||document.querySelector(".wrap");return i?i.nextElementSibling:null}},7723:e=>{e.exports=window.wp.i18n}},t={};function o(i){var r=t[i];if(void 0!==r)return r.exports;var s=t[i]={exports:{}};return e[i](s,s.exports,o),s.exports}o.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return o.d(t,{a:t}),t},o.d=(e,t)=>{for(var i in t)o.o(t,i)&&!o.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var i=o(7723);const r=window.wp.hooks,s=window.wp.data,n=window.wp.url;class a extends Error{url;code;constructor(e,t,o){super(e),this.name="IframeError",this.url=t,this.code=o}}class c extends a{constructor(e){super(`Cross-domain request blocked for ${e}`,e),this.name="CrossDomainError"}}class l extends a{constructor(e,t){super(`Page load timeout after ${t}ms for ${e}`,e),this.name="LoadTimeoutError"}}class d extends a{constructor(e){super(`X-Frame-Options: DENY prevents loading ${e} in iframe`,e),this.name="XFrameDenyError"}}class p{iframe;wrapperDiv;logger;currentUrl=null;currentViewport=null;defaultTimeout;closed=!1;constructor(e,t){this.logger=e,this.defaultTimeout=t||3e4;const{iframe:o,wrapperDiv:i}=this.createIframe();this.iframe=o,this.wrapperDiv=i}createIframe(){const e=document.createElement("div");e.setAttribute("style","position: fixed; z-index: -1000; opacity: 0; top: -10000px; left: -10000px;"),document.body.appendChild(e);const t=document.createElement("iframe");return t.setAttribute("style","max-width: none; max-height: none; border: 0;"),t.setAttribute("aria-hidden","true"),t.setAttribute("sandbox","allow-same-origin allow-scripts"),t.setAttribute("credentialless","true"),e.appendChild(t),{iframe:t,wrapperDiv:e}}async setViewport(e){if(this.closed)throw new Error("Cannot set viewport on closed page");if(this.currentViewport?.width!==e.width||this.currentViewport?.height!==e.height)return new Promise(t=>{this.iframe.width=e.width.toString(),this.iframe.height=e.height.toString(),this.currentViewport=e,setTimeout(t,1)})}setHeaders(e){return Promise.reject(new Error("setHeaders is not supported in iframe adapter - iframes cannot set custom HTTP headers due to browser security restrictions"))}async goto(e,t){if(this.closed)throw new Error("Cannot navigate closed page");if(e===this.currentUrl)return;const o=t?.timeout||this.defaultTimeout;try{if(new URL(e),!this.isSameOrigin(e))throw new c(e);await this.loadPage(e,o,t?.waitUntil),this.currentUrl=e}catch(t){throw this.logger.error({err:t},`Failed to navigate to ${e}`),t}}isSameOrigin(e){try{return new URL(e).origin===window.location.origin}catch{return!1}}async loadPage(e,t,o){return new Promise((i,r)=>{const s=setTimeout(()=>{this.iframe.onload=null,this.iframe.onerror=null,r(new l(e,t))},t),n=()=>{clearTimeout(s),this.iframe.onload=null,this.iframe.onerror=null};this.iframe.onload=async()=>{try{if(n(),!this.iframe.contentDocument||!this.iframe.contentWindow)throw await this.diagnoseLoad
|
||
|
|
// translators: %s: The post title or post ID.
|
||
|
|
// translators: %s: The post title or post ID.
|
||
|
|
(0,i.__)('🎉 "%s" is now Optimized!',"kadence-blocks"),a))}catch(t){console.error("❌ Optimization failed:",t),clearInterval(n),T(e.target,S.OPTIMIZE),(0,R.createNotice)(
|
||
|
|
// translators: %s: The post title or post ID.
|
||
|
|
// translators: %s: The post title or post ID.
|
||
|
|
(0,i.sprintf)((0,i.__)('Optimization failed for "%s"',"kadence-blocks"),a),R.NOTICE_TYPES.ERROR,!0,t)}}(e):e.target.classList.contains("kb-remove-post-optimization")&&await async function(e){e.preventDefault();const t=parseInt(e.target.dataset.postId,10),o=e.target.dataset.postPath;console.log("Removing optimization...",{postId:t,postPath:o});try{const r=await async function(e,t){try{return await N()({path:k,method:"DELETE",data:{post_path:t,post_id:e}})}catch(t){throw console.error(`❌ Failed to remove optimization for post ID ${e}:`,t),t}}(t,o);console.log(r),T(e.target,S.OPTIMIZE);const s=z(e.target,t);(0,R.createNotice)((0,i.sprintf)(
|
||
|
|
// translators: %s: The post title or post ID.
|
||
|
|
// translators: %s: The post title or post ID.
|
||
|
|
(0,i.__)('Optimization data removed for "%s".',"kadence-blocks"),s))}catch(e){console.error("❌ Failed to remove optimization:",e),(0,R.createNotice)((0,i.__)("An error occurred","kadence-blocks"),R.NOTICE_TYPES.ERROR,!0,e)}}(e)})}class ${constructor(){this.intervals=new Map}start(e,t){const o=C(t);this.intervals.set(e,o)}stop(e){const t=this.intervals.get(e);t&&(clearInterval(t),this.intervals.delete(e))}stopAll(){this.intervals.forEach(e=>clearInterval(e)),this.intervals.clear()}}function A(e,t){const o=document.querySelector(`a[data-post-id="${e}"]`);return o&&T(o,t),o}async function F(e){return await N()({path:"/kb-optimizer/v1/optimize/posts-metadata",method:"POST",data:{post_ids:e}})}function D(e,t){e.forEach(e=>{const o=A(e,S.OPTIMIZING);o&&t.start(e,o)})}function M(){const e=document.querySelector("#posts-filter");e&&e.addEventListener("submit",async e=>{if(!e.submitter||"doaction"!==e.submitter.id)return;const t=document.querySelector("#bulk-action-selector-top"),o=t?t.value:null;if("kb_optimize_posts"===o||"kb_optimize_remove"===o){e.preventDefault();const t=function(){const e=document.querySelectorAll('input[name="post[]"]:checked');return Array.from(e).map(e=>parseInt(e.value,10))}();if(0===t.length)return;"kb_optimize_posts"===o?await async function(e){console.log("🚀 Starting bulk optimization for posts:",e);const t=new $;D(e,t);const o=(0,R.createDismissibleNotice)((0,i.sprintf)(
|
||
|
|
// translators: %d: Number of posts being optimized.
|
||
|
|
// translators: %d: Number of posts being optimized.
|
||
|
|
(0,i._n)("Optimizing %d post…","Optimizing %d posts…",e.length,"kadence-blocks"),e.length),R.NOTICE_TYPES.INFO);try{const r=await F(e),{data:s,errors:n}=r;!function(e,t){e&&0!==e.length&&e.forEach(e=>{(0,R.createNotice)(`Post ID ${e.post_id}: ${e.message}`,R.NOTICE_TYPES.ERROR,!0),t.stop(e.post_id),A(e.post_id,S.OPTIMIZE)})}(n,t);const a={successful:[],failed:[]};for(const e of s){console.log(`🎯 Optimizing post ${e.post_id}...`);try{await O(e.url,e.post_id,e.post_path,_.token),a.successful.push(e.post_id),t.stop(e.post_id),A(e.post_id,S.REMOVE)}catch(o){console.error(`❌ Failed to optimize post ${e.post_id}:`,o),a.failed.push(e.post_id),(0,R.createNotice)(`Post ID ${e.post_id}: ${o.message}`,R.NOTICE_TYPES.ERROR,!0),t.stop(e.post_id),A(e.post_id,S.OPTIMIZE)}}a.successful.length>0&&(0,R.createNotice)((0,i.sprintf)(
|
||
|
|
// translators: %d: Number of posts optimized.
|
||
|
|
// translators: %d: Number of posts optimized.
|
||
|
|
(0,i._n)("🎉 %d post optimized successfully!","🎉 %d posts optimized successfully!",a.successful.length,"kadence-blocks"),a.successful.length),R.NOTICE_TYPES.SUCCESS),a.failed.length>0&&(0,R.createNotice)((0,i.sprintf)(
|
||
|
|
// translators: %d: Number of posts that failed.
|
||
|
|
// translators: %d: Number of posts that failed.
|
||
|
|
(0,i._n)("Failed to optimize %d post.","Failed to optimize %d posts.",a.failed.length,"kadence-blocks"),a.failed.length),R.NOTICE_TYPES.ERROR,!0),o?.remove()}catch(e){console.error("❌ Bulk optimization failed:",e),o?.remove(),(0,R.createNotice)((0,i.__)("Failed to fetch post data for bulk optimization.","kadence-blocks"),R.NOTICE_TYPES.ERROR,!0,e)}}(t):"kb_optimize_remove"===o&&await async function(e){console.log("🗑️ Removing optimization for posts:",e);const t=new $;D(e,t);const o=(0,R.createDismissibleNotice)((0,i.sprintf)(
|
||
|
|
// translators: %d: Number of posts having optimization removed.
|
||
|
|
// translators: %d: Number of posts having optimization removed.
|
||
|
|
(0,i._n)("Removing optimization for %d post…","Removing optimization for %d posts…",e.length,"kadence-blocks"),e.length),R.NOTICE_TYPES.INFO);try{const r=await F(e),{data:s,errors:n}=r;if(n&&n.length>0&&n.forEach(e=>{(0,R.createNotice)(`Post ID ${e.post_id}: ${e.message}`,R.NOTICE_TYPES.ERROR,!0),t.stop(e.post_id),A(e.post_id,S.REMOVE)}),s.length>0){const e=s.map(e=>e.post_id),o=s.map(e=>e.post_path);console.log(`Removing optimization for ${e.length} posts...`);const r=await async function(e,t){return await N()({path:"/kb-optimizer/v1/optimize/bulk/delete",method:"DELETE",data:{post_ids:e,post_paths:t}})}(e,o),{data:n}=r;n.successful.forEach(e=>{t.stop(e),A(e,S.OPTIMIZE)}),n.failed&&n.failed.length>0&&n.failed.forEach(e=>{(0,R.createNotice)(`Post ID ${e.post_id}: ${e.message}`,R.NOTICE_TYPES.ERROR,!0),t.stop(e.post_id),A(e.post_id,S.REMOVE)}),n.successful.length>0&&(0,R.createNotice)((0,i.sprintf)(
|
||
|
|
// translators: %d: Number of posts.
|
||
|
|
// translators: %d: Number of posts.
|
||
|
|
(0,i._n)("Removed optimization for %d post.","Removed optimization for %d posts.",n.successful.length,"kadence-blocks"),n.successful.length),R.NOTICE_TYPES.SUCCESS),n.failed&&n.failed.length>0&&(0,R.createNotice)((0,i.sprintf)(
|
||
|
|
// translators: %d: Number of posts that failed.
|
||
|
|
// translators: %d: Number of posts that failed.
|
||
|
|
(0,i._n)("Failed to remove optimization for %d post.","Failed to remove optimization for %d posts.",n.failed.length,"kadence-blocks"),n.failed.length),R.NOTICE_TYPES.ERROR,!0)}o?.remove()}catch(e){console.error("❌ Bulk remove optimization failed:",e),o?.remove(),(0,R.createNotice)((0,i.__)("Failed to fetch post data for bulk operation.","kadence-blocks"),R.NOTICE_TYPES.ERROR,!0,e)}}(t)}})}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>{x(),M()}):(x(),M()),(this.kadence=this.kadence||{})["kadence-optimizer"]={}})();
|