/** * Native WordPress post-lock coordinator for live FrontEdit sessions. * * Reads: SFE.Api.apiCall, SFE.ManagerData.postId * Exposes: SFE.PostLockManager { beginLockClaim, ensureLock, handleLockedError } */ (function() { 'use strict'; window.MWP = window.MWP || {}; window.MWP.SFE = window.MWP.SFE || {}; const SFE = window.MWP.SFE; let ownsLock = false; let refreshTimer = null; let pendingClaim = null; /** * Prevent editor and draft-preview Escape handlers from closing an active * editing session while the post-lock decision is on screen. * * Window capture runs before the document-level editor lifecycle handlers. * * @param {KeyboardEvent} event Keyboard event. * @returns {void} */ window.addEventListener('keydown', (event) => { if ( event.key === 'Escape' && document.querySelector('.mwp-sfe-post-lock-modal.is-open') ) { event.preventDefault(); event.stopImmediatePropagation(); } }, true); function escapeHtml(value) { return String(value).replace(/[&<>"']/g, (char) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[char]); } function showLockModal(owner = {}) { return new Promise((resolve) => { const modal = document.createElement('div'); modal.className = 'mwp-sfe-post-lock-modal is-open'; modal.setAttribute('data-mwp-sfe-control', 'post-lock-modal'); modal.innerHTML = `
`; document.body.appendChild(modal); modal.querySelector('.mwp-sfe-post-lock-cancel').addEventListener('click', () => { modal.remove(); resolve(false); }); modal.querySelector('.mwp-sfe-post-lock-takeover').addEventListener('click', () => { modal.remove(); resolve(true); }); }); } function beginRefresh() { if (refreshTimer) return; refreshTimer = window.setInterval(async () => { if (!ownsLock) return; try { await SFE.Api.apiCall('/post-lock/claim', { post_id: SFE.ManagerData.postId }); } catch (error) { ownsLock = false; clearInterval(refreshTimer); refreshTimer = null; } }, 60000); } /** * Start one shared lock-claim operation for the current post. * * Opening an editor does not need to wait for this request, but every * published-content mutation must await the returned promise before it * reaches the server. Sharing the promise keeps concurrent editor opens and * saves from issuing competing claims or bypassing the pending decision. * * @returns {Promise} Whether this user holds the post lock. */ function beginLockClaim() { if (ownsLock) return Promise.resolve(true); if (pendingClaim) return pendingClaim; pendingClaim = (async () => { try { await SFE.Api.apiCall('/post-lock/claim', { post_id: SFE.ManagerData.postId }); } catch (error) { if (error.message !== 'POST_LOCKED' || !await showLockModal(error?.payload?.lock?.owner)) return false; try { await SFE.Api.apiCall('/post-lock/claim', { post_id: SFE.ManagerData.postId, take_over: true }); } catch (_) { return false; } } ownsLock = true; beginRefresh(); return true; })(); pendingClaim.finally(() => { pendingClaim = null; }); return pendingClaim; } /** * Wait until the current user's native WordPress post lock is held. * * @returns {Promise} Whether this user holds the post lock. */ async function ensureLock() { return beginLockClaim(); } async function handleLockedError(error) { ownsLock = false; if (error?.message !== 'POST_LOCKED') return false; if (!await showLockModal(error?.payload?.lock?.owner)) return false; try { await SFE.Api.apiCall('/post-lock/claim', { post_id: SFE.ManagerData.postId, take_over: true }); ownsLock = true; beginRefresh(); return true; } catch (_) { return false; } } SFE.PostLockManager = { beginLockClaim, ensureLock, handleLockedError }; })();

About the author


Popular Categories


Useful Links

Links I found useful and wanted to share.


Search the website

Aventle clientele Pakistan UK USA Saudia UAE Australia UAE Gulf

Clients

Canlı karşılaşmalara yüksek oranlarla bahis yapmak için bettilt kategorisi kullanılıyor.

Disclaimer: AVENTLE does not own any of the companies and their content displayed. The display in Portfolio and Clientele is only because AVENTLE must have designed/developed complete or some of their required content/designs/solutions. No copyright infringement intended. Incase of any issues email us at info@aventle.com
street mark
Crew Hire
Atlas Battery
Atlas Honda
Keryana
Learning Minds Group
Sohail Zindani
Offer Lagao
Kausar Medicos
Abgurt
Pakistan Airforce
US AID
Soapy cars
Buksh Industries
Ashabi Textiles
IKEA Saudi Arabia
TAN Pan Asian Cuisine
Siemens
Print 3D Pakistan
Wah Brands
Rahaa by Nini
OVOLO
Pizza Track 5
Ali Mart
Peach Consulting
Jeddah Gheir
Stella Shoes
Karachi Dairy Farms
Landmark Investments
K G Traders
IoBM
Inspectorate of Army Stores & Clothing
Building Hardware Stores
Al Hadi Industries
Ameen Motors
inspire communication
iNow Marketing
Areve Event Production
Food Aroma
Greeniche
Al Yafi Family
Collaborative Architects
Al Jaddah
Sairas Collection
i3 Pathfinder Solutions
Hateem Foods
Nissan
Ghandara Motors
CSM Pakistan
Exventure
911 GFix
Tifzee
Handicap International
Rubix Digital
Marie Stopes International
Amrop
Rubber Side Down
Wormhole Productions
Maerz Business Consulting
Kauf Hous
Wecc
Value Chain Magazine
Stimulus Productions
Sofia Naveed Laari
Proactive Sourcings
Preone 0
Patients Helping Hands
Pearl International
Opinion Meter Pakistan
MYF Jeddah
KAFCUIA JEDDAH
JeeFoo
Inspectorate of Army Stores & Clothing
Gon Pacci
Pakistan Army
GMC
Valufacture Concepts
Pak Super Store