File Editor
Directories:
.. (Back)
options
scripts
Files:
options.min.js
scripts.min.js
Create New File
Create
Edit File: scripts.min.js
class PhoenixMediaRename{constructor(){this.form=null,this.fields=null,this.type=null,this.isMediaSingle=!1,this.fieldsCount=0,this.currentField=0,this.doRename=this.doRename.bind(this),this.processFormSubmit=this.processFormSubmit.bind(this),document.addEventListener("DOMContentLoaded",()=>this.init())}init(){this.form=document.getElementById("posts-filter"),this.checkMediaPage(),this.isMediaSingle||document.querySelectorAll(".tablenav select[name^=action]").forEach(e=>{for(let t in MRSettings.labels){const i=document.createElement("option");i.value=t,i.textContent=decodeURIComponent(MRSettings.labels[t].replace(/\+/g,"%20")),e.insertBefore(i,e.lastElementChild)}}),document.getElementById("post")&&document.getElementById("post").addEventListener("submit",this.processFormSubmit),document.querySelectorAll(".tablenav .button.action").forEach(e=>{e.addEventListener("click",this.processFormSubmit)}),this.initTextboxListeners()}checkMediaPage(){const e=window.location.href;e.includes("/wp-admin/upload.php")?this.isMediaSingle=!1:this.isMediaSingle=!0}initTextboxListeners(){const e=document.querySelectorAll(".phoenix-media-rename-filename");e.forEach(e=>{e.addEventListener("input",function(){const e=this.closest("tr"),t=e.querySelector('input[type="checkbox"]');t&&(t.checked=!0)})})}async doRename(e){const t=new FormData;t.append("action","phoenix_media_rename"),t.append("type",this.type),t.append("_wpnonce",this.form.querySelector("input[name=_mr_wp_nonce]").value),t.append("new_filename",e.querySelector("input").value),t.append("post_id",e.querySelector("input").dataset.postId);const i=e.querySelector(".loader"),n=e.querySelector(".error"),s=e.querySelector(".success");try{const o=await fetch(ajaxurl,{method:"POST",body:t}),r=await o.text();if(i.style.display="none","1"!==r)n.textContent=r,n.style.display="inline-block";else{const t=e.querySelector("input[type=text]");t.title=t.value,s.style.display="inline-block"}if(this.currentField++,this.currentField===this.fieldsCount){this.currentField=0,this.form.querySelector('.error[style*="display: inline-block"]')||this.form.submit();const e=document.querySelectorAll('input[type="submit"]');e.forEach(e=>e.disabled=!1)}else this.doRename(this.fields[this.currentField])}catch(e){console.error("Error during rename:",e),n.textContent="An error occurred during rename",n.style.display="inline-block",i.style.display="none"}}processFormSubmit(e){if(this.isMediaSingle?this.type="rename":this.type=document.querySelector("#bulk-action-selector-top").value,"-1"==this.type)return void alert(phoenix_media_rename_strings.no_action_warning);const t=["rename","rename_retitle","retitle","retitle_from_post_title","rename_from_post_title","rename_retitle_from_post_title"];if(!this.isMediaSingle&&!t.includes(this.type))return;const i=document.querySelectorAll('input[type="submit"]');if(i.forEach(e=>e.disabled=!0),this.form=this.isMediaSingle?document.getElementById("post"):document.getElementById("posts-filter"),this.isMediaSingle)this.fields=Array.from(this.form.querySelectorAll(".phoenix-media-rename")).filter(e=>{const t=e.querySelector("input[type=text]");return t.value!==t.title});else{const e=document.querySelectorAll("#the-list input:checked");this.fields=Array.from(e).map(e=>e.closest("tr").querySelector(".phoenix-media-rename"))}this.fieldsCount=this.fields.length,this.fieldsCount&&(this.fields.forEach(e=>{const t=e.querySelectorAll(".loader, .error, .success");t.forEach(e=>e.style.display="none"),e.querySelector(".loader").style.display="inline-block"}),this.doRename(this.fields[0]),e.preventDefault())}}const phoenixMediaRename=new PhoenixMediaRename;
Save Changes
Rename File
Rename