Files
Jacek Pyziak cd264483f8 Add PSR HTTP Message Interfaces and Dependencies
- Implemented StreamInterface, UploadedFileInterface, and UriInterface as per PSR standards.
- Added getallheaders function to retrieve HTTP headers in a compatible manner.
- Included LICENSE files for ralouphie/getallheaders and symfony/deprecation-contracts.
- Introduced function for triggering deprecation notices in Symfony.
2025-12-28 12:44:00 +01:00

1 line
7.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const{parse:parse}=wp.blocks,{select:select,subscribe:subscribe}=wp.data;class blockDataReterive{constructor(){this.blockLists=[],this.customBlockTranslateData={},this.customBlocksData=[],this.loaderContainer=null,this.init()}init=()=>{this.fetchCustomBlocks(),this.loaderContainer=document.createElement("div"),this.loaderContainer.className="atfp-overlay",this.loaderContainer.setAttribute("role","status"),this.loaderContainer.setAttribute("aria-live","polite"),this.loaderContainer.innerHTML=this.getOverlayTemplate(),document.body.appendChild(this.loaderContainer),document.body.classList.add("atfp-overlay-open")};getBlocks=blocks=>{const innerBlocks=block=>{const innerBlock=block.innerBlocks;innerBlock.length>0&&innerBlock.forEach(innerBlock=>{this.customBlocksData.push(innerBlock),innerBlocks(innerBlock)})},blockLists=blocks;blockLists.forEach(block=>{innerBlocks(block)}),this.customBlocksData=[...this.customBlocksData,...blockLists],this.getBlockData()};fetchCustomBlocks=()=>{const apiSendData={atfp_nonce:atfp_block_update_object.ajax_nonce,action:atfp_block_update_object.action_get_content},apiUrl=atfp_block_update_object.ajax_url;fetch(apiUrl,{method:"POST",headers:{"content-type":"application/x-www-form-urlencoded; charset=UTF-8"},body:new URLSearchParams(apiSendData)}).then(response=>response.json()).then(data=>{if("No custom blocks found."===data.message)return void(this.loaderContainer&&this.loaderContainer.remove());const customBlocks=parse(data.data.block_data);this.getBlocks(customBlocks),this.saveBlockData()}).catch(error=>{this.loaderContainer&&this.loaderContainer.remove(),console.error("Error fetching block rules:",error)})};saveBlockData=()=>{if(Object.keys(this.customBlockTranslateData).length<1)return void(this.loaderContainer&&this.loaderContainer.remove());const apiSendData={atfp_nonce:atfp_block_update_object.ajax_nonce,action:atfp_block_update_object.action_update_content,save_block_data:JSON.stringify(this.customBlockTranslateData)},apiUrl=atfp_block_update_object.ajax_url;fetch(apiUrl,{method:"POST",headers:{"content-type":"application/x-www-form-urlencoded; charset=UTF-8"},body:new URLSearchParams(apiSendData)}).then(response=>response.json()).then(data=>{this.setOverlayState("success"),this.teardownOverlay(),data.success&&data.data.message&&console.log(data.data.message)}).catch(error=>{this.setOverlayState("error"),this.teardownOverlay(),console.error("Error fetching block rules:",error)})};nestedAttrValue=idsArr=>{const convertToArrays=obj=>{if("object"!=typeof obj||null===obj)return obj;for(let key in obj)obj.hasOwnProperty(key)&&("object"==typeof obj[key]&&null!==obj[key]&&obj[key].hasOwnProperty("atfp_array_key_replace")?(obj[key]=Object.values(obj[key]),obj[key]=convertToArrays(obj[key])):obj[key]=convertToArrays(obj[key]));return obj},deepMerge=(target,source)=>{for(const key in source)source[key]instanceof Object&&key in target&&Object.assign(source[key],deepMerge(target[key],source[key]));return Object.assign(target||{},source),target};let currentElement={},tempObj=currentElement,lastKey=idsArr[idsArr.length-1];idsArr.slice(0,-1).forEach(key=>{tempObj[key]=tempObj[key]||{},tempObj=tempObj[key]}),tempObj[lastKey]=!0;const obj=convertToArrays(currentElement);deepMerge(this.customBlockTranslateData,obj)};filterAttr=(idsArray,value)=>{null!=value&&(Object.getPrototypeOf(value)===Array.prototype?this.filterBlockArrayAttr(idsArray,value):Object.getPrototypeOf(value)===Object.prototype?this.filterBlockObjectAttr(idsArray,value):"string"==typeof value&&/Make This Content Available for Translation/i.test(value)?this.nestedAttrValue(idsArray,value):value instanceof wp.richText.RichTextData&&/Make This Content Available for Translation/i.test(value.originalHTML)&&this.nestedAttrValue(idsArray,value.originalHTML))};filterBlockArrayAttr=(idsArr,blockData)=>{const newIdArr=new Array(...idsArr);newIdArr.push("atfp_array_key_replace"),blockData.forEach((value,key)=>{("string"==typeof value&&/Make This Content Available for Translation/i.test(value)||![null,void 0].includes(value)&&[Array.prototype,Object.prototype].includes(Object.getPrototypeOf(value)))&&this.filterAttr(newIdArr,value)})};filterBlockObjectAttr=(idsArr,blockData)=>{Object.keys(blockData).forEach(key=>{const newIdArr=new Array(...idsArr),value=blockData[key];null!=value&&("string"==typeof value&&/Make This Content Available for Translation/i.test(value)||[Array.prototype,Object.prototype].includes(Object.getPrototypeOf(value)))&&(newIdArr.push(key),this.filterAttr(newIdArr,blockData[key]))})};filterBlockAttribute=blockData=>{Object.keys(blockData).map(clientId=>{const blockName=Object.keys(blockData[clientId])[0],attributes=blockData[clientId][blockName];Object.keys(attributes).forEach(keytwo=>{const value=attributes[keytwo],idsArray=new Array(blockName,"attributes",keytwo);this.filterAttr(idsArray,value)})})};getBlockData=()=>{if("object"!=typeof this.customBlocksData||0===Object.keys(this.customBlocksData).length)return;const blockData=this.customBlocksData,blockAttributes={};Object.values(blockData).forEach(block=>{Object.values(block.attributes).length>0&&(blockAttributes[block.clientId]={},blockAttributes[block.clientId][block.name]=block.attributes)}),Object.values(blockAttributes).length>0&&this.filterBlockAttribute(blockAttributes)};setOverlayState=state=>{if(!this.loaderContainer)return;const panel=this.loaderContainer.querySelector(".atfp-overlay .atfp-box");panel&&panel.setAttribute("data-state",state)};teardownOverlay=(delayMs=3e3)=>{this.loaderContainer&&setTimeout(()=>{this.loaderContainer.classList.add("atfp-overlay--closing"),setTimeout(()=>{this.loaderContainer.remove(),this.loaderContainer=null,document.body.classList.remove("atfp-overlay-open")},300)},delayMs)};getOverlayTemplate=()=>'\n <div class="atfp-overlay" role="status" aria-live="polite">\n <div class="atfp-backdrop"></div>\n <div class="atfp-box" data-state="loading">\n <div class="atfp-row">\n <span class="atfp-spinner" aria-hidden="true"></span>\n <span class="atfp-icon atfp-icon--ok" aria-hidden="true">✓</span>\n <span class="atfp-icon atfp-icon--err" aria-hidden="true">!</span>\n\n <div class="atfp-text">\n <div class="atfp-title" data-label="loading">Saving block content</div>\n <div class="atfp-title" data-label="success">Supported block content has been updated</div>\n <div class="atfp-title" data-label="error">Update failed</div>\n\n <div class="atfp-desc" data-label="loading">\n Please dont close or refresh this window until the update is complete.\n </div>\n <div class="atfp-desc" data-label="success">\n Supported block content has been updated. You may continue.\n </div>\n <div class="atfp-desc" data-label="error">\n Something went wrong. You can retry without closing this window.\n </div>\n </div>\n </div>\n\n <div class="atfp-bar"><span></span></div>\n </div>\n </div>\n '}const debounce=(func,delay)=>{let timeoutId;return function(...args){clearTimeout(timeoutId),timeoutId=setTimeout(()=>func.apply(this,args),delay)}};let isBlockContentUpdating=!1;const saveBlockContent=debounce(()=>{new blockDataReterive,isBlockContentUpdating=!1},300);select&&select("core/editor")&&subscribe&&subscribe(()=>{const{isCurrentPostPublished:isCurrentPostPublished,isSavingPost:isSavingPost,isPublishingPost:isPublishingPost,isAutosavingPost:isAutosavingPost}=select("core/editor"),isAutoSaving=isAutosavingPost(),isPublishing=isPublishingPost(),isSaving=isSavingPost(),postPublished=isCurrentPostPublished();!(isPublishing||postPublished&&isSaving)||isAutoSaving||isBlockContentUpdating||(isBlockContentUpdating=!0,saveBlockContent())});