| 2 |
(()=>{"use strict";const{__:e,sprintf:t}=wp.i18n,o=(document.querySelector("[name='squeeze_bulk']"),document.querySelector("[name='squeeze_bulk_again']"),document.querySelector("[name='squeeze_bulk_path_button']"),document.querySelectorAll(".squeeze-path-list__remove"),document.querySelector("[name='squeeze_select_path_button']"),document.querySelector("[name='squeeze_save_path_button']"),document.getElementById("squeeze-path-dialog"),document.querySelector("[name='squeeze_close_path_dialog_button']"),document.querySelector("[name='squeeze_bulk_path']"),document.querySelector(".squeeze-path-list"),document.querySelector("#posts-filter"),document.getElementById("squeeze-bulk-directory-list"),document.querySelector("#squeeze-log-data"),document.querySelector("[name='squeeze_pause_page_bulk']"),document.getElementById("squeeze-bulk-paused-banner"),document.getElementById("squeeze-bulk-paused-banner-text"),document.querySelector("[name='squeeze_bulk_total_images']"),document.querySelector("[name='squeeze_bulk_uncompressed_images']"),(e,t,o=null)=>{const n=o?.auto_compress??!0;return"image"===e&&["jpeg","png","webp","avif"].includes(t)&&n}),{__:n,sprintf:i}=wp.i18n,a=()=>{const e=new URLSearchParams(window.location.search),t=e.get("page"),o=window.location.href.includes("upload.php"),i=window.location.href.includes("media-new.php"),a=window.location.href.includes("post.php")&&"edit"===e.get("action");return"squeeze-bulk"===t||i||a?n("Are you sure you want to leave this page? The squeezing process will be terminated!","squeeze"):o?n("Are you sure you want to leave this page? The settings will not be saved!","squeeze"):void 0},{sprintf:r,__:s}=wp.i18n,u=JSON.parse(squeezeOptions.options),c=wp.hooks.addFilter,l=wp.compose.createHigherOrderComponent,{createElement:d,Fragment:p}=wp.element,{Spinner:m}=wp.components;wp.hooks.addFilter("blocks.registerBlockType","squeeze/add-compressing-attribute",(e,t)=>"core/image"===t||"generateblocks/media"===t?{...e,attributes:{...e.attributes,isSqueezing:{type:"boolean",default:!1}}}:e),c("editor.BlockEdit","squeeze/with-image-onselect",l(function(e){return function(t){const{attributes:o}=t;if("core/image"!==t.name&&"generateblocks/media"!==t.name)return d(e,t);const{isSqueezing:n}=o;return n?d(p,{},d("div",{style:{position:"relative"}},d(e,t),d("div",{style:{position:"absolute",backgroundColor:"rgba(255,255,255,0.5)",pointerEvents:"none",width:"100%",height:"100%",left:"0%",top:"0%"}}),d("div",{style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",pointerEvents:"none"}},d(m,{size:50}),s("Squeezing image…","squeeze")))):d(e,t)}},"withImageOnSelect")),wp.domReady(function(){wp.apiFetch.use(async(e,t)=>{if(e&&e._squeezeProcessed)return await t(e);if("POST"===e.method&&function(e){if(!e||"string"!=typeof e)return!1;const t=e.split("?")[0].replace(/\/+$/,"");return/(?:^|\/)wp\/v2\/media$/.test(t)}(e.path)){const n=e.body;if(!(n instanceof FormData&&n.has("file")))return await t(e);const i=wp?.data?.select?.("core/editor")?.getCurrentPostId?.(),c=n.get("post");if(!c||Number(c)!==Number(i))return await t(e);const l=n.get("file");if(l&&!l.type.startsWith("image/"))return await t(e);const d=l.type.split("/")[0],p=l.type.split("/")[1];let m=l;if(!o(d,p,u))return await t(e);if(await Squeeze.isExcludedByName(l.name))return console.warn("File is excluded from compression:",l.name),await t(e);const b="compression-notice";wp.data.dispatch("core/notices").createNotice("info",s("Squeezing the image...","squeeze"),{id:b,isDismissible:!1}),e.headers&&(delete e.headers["Content-Type"],delete e.headers["content-type"]),e._squeezeProcessed=!0;try{window.onbeforeunload=a;const n=await Squeeze.compressBeforeUpload(l);if(!n||!n.base64)return console.warn("Compression skipped or failed for:",l.name,n),await t(e);if(u.backup_original){const e=l instanceof Blob?l:new Blob([l],{type:l.type});m=u?.direct_webp&&"image/webp"!==l.type?await Squeeze.convertFileToWebp(e):e}const i=new FormData,c=((e,t="image/jpeg")=>{const o=e.split(",")[1]||e,n=atob(o),i=new Array(n.length);for(let e=0;e<n.length;e++)i[e]=n.charCodeAt(e);const a=new Uint8Array(i);return new Blob([a],{type:t})})(n.base64,l.type);i.append("file",c,l.name);for(const[t,o]of e.body.entries())"file"!==t&&i.append(t,o);e.body=i;const d=await t(e);return d&&d.id&&"image"===d.media_type&&(d.base64=n.base64,d.base64Webp=n?.base64Webp,u.backup_original&&(d.originalFile=m),function(e,t){return new Promise(n=>{setTimeout(()=>{const i=wp.data.select("core/block-editor").getBlocks().map(n=>{const i=n.attributes.id===e||n.attributes.mediaId===e,c=function t(o){return!(!o||!o.length)&&o.some(o=>o.attributes.id===e||o.attributes.mediaId===e||t(o.innerBlocks))}(n.innerBlocks);return i||c?(c?n.innerBlocks.forEach(t=>{t.attributes.id!==e&&t.attributes.mediaId!==e||wp.data.dispatch("core/block-editor").updateBlockAttributes(t.clientId,{isSqueezing:!0})}):wp.data.dispatch("core/block-editor").updateBlockAttributes(n.clientId,{isSqueezing:!0}),async function(e){const t=e.mime_type.split("/")[0],n=e.mime_type.split("/")[1];if(o(t,n,u)){window.onbeforeunload=a;try{const t=await Squeeze.getAttachment(e.id);if(!1===t.success)return window.onbeforeunload=null,void console.error(r(s("Error fetching attachment: %s","squeeze"),t.data));const o=t.data;if(o.is_squeezed)return console.warn("Image already compressed:",o.id),void(window.onbeforeunload=null);const n={attributes:{url:o.url,mime:o.mime,name:o.name,filename:o.filename,id:o.id,sizes:o.sizes}};(async(e,t)=>{if("object"!=typeof e||!e.attributes)return void console.warn("Invalid attachment object:",e);const o=e.attributes;t?.base64&&(o.base64Compressed=t.base64),t?.base64Webp&&(o.base64WebpCompressed=t.base64Webp),t?.originalFile&&(o.originalFile=t.originalFile)})(n,e);const i=await Squeeze.handleCompress(n),a=await Squeeze.handleUpload({attachment:n,base64:i});a.success?window.onbeforeunload=null:(window.onbeforeunload=null,console.error(r(s("Error uploading image: %s","squeeze"),a.data)))}catch(e){console.error(e),window.onbeforeunload=null}}}(t).then(()=>{c?n.innerBlocks.forEach(t=>{t.attributes.id!==e&&t.attributes.mediaId!==e||wp.data.dispatch("core/block-editor").updateBlockAttributes(t.clientId,{isSqueezing:!1})}):wp.data.dispatch("core/block-editor").updateBlockAttributes(n.clientId,{isSqueezing:!1})})):null}).filter(e=>e);Promise.all(i).then(()=>{n()})},100)})}(d.id,d).then(()=>{wp.data.dispatch("core/notices").removeNotice("compression-notice"),window.onbeforeunload=null}).catch(e=>{console.error("Error during squeeze and update:",e),wp.data.dispatch("core/notices").createNotice("error",r(s("Error squeezing the image: %s","squeeze"),e.message),{id:b,isDismissible:!0}),window.onbeforeunload=null})),d}catch(o){return wp.data.dispatch("core/notices").removeNotice("compression-notice"),console.error("Error during image compression:",o),wp.data.dispatch("core/notices").createNotice("error",r(s("Error squeezing the image: %s","squeeze"),o?.message||o),{id:b,isDismissible:!0}),window.onbeforeunload=null,await t(e)}}return await t(e)})})})(); |