/home/techb158/public_html/wp-content/plugins/kirki/assets/js/admin
NameSizeModeActions
custom-link-in-toolbar.js84280644editdlrm
Edit: /home/techb158/public_html/wp-content/plugins/kirki/assets/js/admin/custom-link-in-toolbar.js (8428B)
// custom-link-in-toolbar.js // wrapped into IIFE - to leave global space clean. (function (window, wp) { let timeID = null; const editWithBtnHTML = ` Design with Kirki xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="none" viewBox="0 0 14 14"> `; const backToWordpressEditBtnHTML = ` xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="none" viewBox="0 0 14 14"> Back to WordPress Editor `; const backtoWPandEditKirkiBtn = `
${backToWordpressEditBtnHTML}${editWithBtnHTML}
`; const createTextToHtmlElement = (text) => { const div = document.createElement('div'); div.innerHTML = text; return div.firstChild; }; const handleClickEditWithBtn = (e) => { e.preventDefault(); let title = document.querySelector('[name="post_title"]'); if (title) { title = title.value; } else { // for gutenberg editor title = document.getElementsByClassName('wp-block-post-title')?.[0]?.innerText || ''; } title = (title || '').replace(/^\uFEFF/, '').trim(); if (!title) { title = 'Untitled'; } const formData = new FormData(); // formData.append('action', 'kirki_post_apis'); // formData.append('endpoint', 'back-to-kirki-editor'); formData.append('postId', kirki_admin.postId); formData.append('title', title); // fetch(kirki_admin.ajaxUrl, { fetch(`${kirki_admin.restUrl}kirki/v1/back-to-kirki-editor`, { method: 'POST', // or 'PUT' body: formData, headers: { 'X-WP-Nonce': kirki_admin.nonce, }, }) .then((response) => response.json()) .then((data) => { window.location.href = kirki_admin.postEditURL; }) .catch((error) => { console.error('Error:', error); }); }; const handleClickBackToWordpressBtn = () => { const formData = new FormData(); // formData.append('action', 'kirki_post_apis'); // formData.append('endpoint', 'back-to-wordpress-editor'); formData.append('postId', kirki_admin.postId); // fetch(kirki_admin.ajaxUrl, { fetch(`${kirki_admin.restUrl}kirki/v1/back-to-wordpress-editor`, { method: 'POST', // or 'PUT' body: formData, headers: { 'X-WP-Nonce': kirki_admin.nonce, // Add nonce as a custom header }, }) .then((response) => response.json()) .then((data) => { if (data) { } }) .catch((error) => { console.error('Error:', error); }); }; const classicAddEditWithSingleButton = () => { const editButtonWrapper = document.querySelector('#wp-content-media-buttons'); if (!editButtonWrapper) return; editButtonWrapper.append(createTextToHtmlElement(editWithBtnHTML)); const kirkiBtns = document.querySelectorAll(`.kirki-edit-with-btn`); kirkiBtns.forEach((kirkiBtn) => { kirkiBtn.addEventListener('click', handleClickEditWithBtn); }); }; const gutenbergAddEditWithSingleButton = () => { const editButtonWrapper = document.querySelector('.edit-post-header-toolbar'); if (!editButtonWrapper) return; editButtonWrapper.append(createTextToHtmlElement(editWithBtnHTML)); const kirkiBtns = document.querySelectorAll(`.kirki-edit-with-btn`); kirkiBtns.forEach((kirkiBtn) => { kirkiBtn.addEventListener('click', handleClickEditWithBtn); }); clearInterval(timeID); }; const classicAddKirkiButtonsInsideContentWrapper = () => { const contentDiv = document.querySelector('#postdivrich'); if (!contentDiv) return; contentDiv.style.display = 'none'; contentDiv.insertAdjacentHTML('afterend', backtoWPandEditKirkiBtn); const backToWPbtns = document.querySelectorAll(`.kirki-back-dash-btn`); backToWPbtns.forEach((backToWPbtn) => { backToWPbtn.addEventListener('click', () => { handleClickBackToWordpressBtn(); removeButtonsFromContent(contentDiv); }); }); const kirkiBtns = document.querySelectorAll(`.kirki-edit-with-btn`); kirkiBtns.forEach((kirkiBtn) => { kirkiBtn.addEventListener('click', handleClickEditWithBtn); }); }; const gutenbergAddKirkiButtonsInsideContentWrapper = () => { let document = window.document; let contentDiv = document.querySelector('.is-root-container'); if (!contentDiv) { const editorCanvasIframe = document.querySelector('[name="editor-canvas"]'); if (!editorCanvasIframe) return; // Access the iframe's content window let iframeWindow = editorCanvasIframe.contentWindow; // Access the document inside the iframe let iframeDocument = iframeWindow.document; // Find the element inside the iframe using its class name contentDiv = iframeDocument.querySelector('.is-root-container'); if (!contentDiv) { return; } document = iframeDocument; } contentDiv.style.display = 'none'; contentDiv.insertAdjacentHTML('afterend', backtoWPandEditKirkiBtn); const backToWPbtns = document.querySelectorAll(`.kirki-back-dash-btn`); backToWPbtns.forEach((backToWPbtn) => { backToWPbtn.addEventListener('click', () => { handleClickBackToWordpressBtn(); removeButtonsFromContent(contentDiv, document); }); }); const kirkiBtns = document.querySelectorAll(`.kirki-edit-with-btn`); kirkiBtns.forEach((kirkiBtn) => { kirkiBtn.addEventListener('click', handleClickEditWithBtn); }); clearInterval(timeID); }; const removeButtonsFromContent = (contentDiv, document = window.document) => { const buttons = document.querySelector('.kirki-editor-button-wrapper'); buttons?.remove(); contentDiv.style.display = 'block'; classicAddEditWithSingleButton(); gutenbergAddEditWithSingleButton(); }; const enableForClassicEditor = () => { if (kirki_admin.isEditorModeIsKirki == 1) { classicAddKirkiButtonsInsideContentWrapper(); } else { classicAddEditWithSingleButton(); } }; const enableForGutenbergEditor = () => { timeID = setInterval(() => { if (kirki_admin.isEditorModeIsKirki == 1) { gutenbergAddKirkiButtonsInsideContentWrapper(); } else { gutenbergAddEditWithSingleButton(); } }, 1000); }; enableForClassicEditor(); enableForGutenbergEditor(); })(window, wp);