(function(){
'use strict';
/* ---- helpers ---- */
function g(s){return document.querySelector(s);}
function gid(s){return document.getElementById(s);}
function st(h){return(h||'').replace(/]*>/g,'').replace(/&[^;]+;/g,' ').trim();}
function esc(s){return String(s||'').replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"');}
function setH(id,url){var el=gid(id);if(el)el.href=url;}
/* ---- detect post ID from body class ---- */
var postId=0;
document.body.classList.forEach(function(c){
var m=c.match(/^postid-(d+)$/);
if(m)postId=parseInt(m[1],10);
});
/*
* ---- WPML API configuration ----
*
* WPML exposes translation info via a REST endpoint.
* wpApiSettings is injected by WordPress (wp_localize_script).
* We always request lang=en to ensure English content.
*/
var BASE=(typeof wpApiSettings!=='undefined'&&wpApiSettings.root)
?wpApiSettings.root.replace(//$/,'')
:location.origin+'/wp-json';
var LANG_PARAM='&lang=en';
var LANG_PARAM_Q='?lang=en';
/* ---- public copy helper ---- */
window.ECL={
copy:function(){
navigator.clipboard.writeText(location.href).then(function(){
var i=document.querySelector('.ecl-sh-btn .fa-link');
if(i){
i.className=i.className.replace('fa-link','fa-check');
setTimeout(function(){i.className=i.className.replace('fa-check','fa-link');},2000);
}
});
}
};
/* ---- abort if no post ID ---- */
if(!postId){
gid('ecl-loading').style.display='none';
return;
}
/* ---- fetch post + adjacent posts in parallel ---- */
Promise.all([
fetch(BASE+'/wp/v2/posts/'+postId+'?_embed=wp:featuredmedia,wp:term'+LANG_PARAM)
.then(function(r){return r.json();}),
fetch(BASE+'/wp/v2/posts/'+(postId-1)+'?_fields=id,title,link'+LANG_PARAM)
.then(function(r){return r.ok?r.json():null;}).catch(function(){return null;}),
fetch(BASE+'/wp/v2/posts/'+(postId+1)+'?_fields=id,title,link'+LANG_PARAM)
.then(function(r){return r.ok?r.json():null;}).catch(function(){return null;})
]).then(function(res){
var post=res[0],prev=res[1],next=res[2];
if(post&&!post.code){
render(post);
buildNav(prev,next);
loadRelated(post);
loadWpmlTranslations(postId);
} else {
gid('ecl-loading').style.display='none';
}
}).catch(function(){
gid('ecl-loading').style.display='none';
});
/* ---- render post ---- */
function render(post){
var url=location.href;
var title=st(post.title.rendered);
var enc=encodeURIComponent(url);
var encT=encodeURIComponent(title);
/* category */
var cat='',catUrl='/blog';
try{
var t0=post._embedded['wp:term'][0][0];
cat=t0.name;catUrl=t0.link;
}catch(e){}
/* reading time (avg 238 wpm English) */
var words=st(post.content.rendered).split(/s+/).length;
var mins=Math.max(1,Math.ceil(words/238));
/* tags */
var tags=[];
try{tags=post._embedded['wp:term'][1]||[];}catch(e){}
/* excerpt */
var exc=st(post.excerpt.rendered);
/* --- fill hero --- */
gid('ecl-bc-cat').textContent=cat||'Article';
gid('ecl-cat-badge-text').textContent=cat||'Article';
gid('ecl-cat-badge').href=catUrl;
gid('ecl-title').textContent=title;
gid('ecl-read-time').textContent=mins+' min read';
gid('ecl-cat-meta').textContent=cat||'—';
if(exc){
var excEl=gid('ecl-excerpt');
excEl.textContent=exc;
excEl.style.display='block';
}
/* --- article content --- */
gid('ecl-content').innerHTML=post.content.rendered;
/* --- tags --- */
if(tags.length){
var tel=gid('ecl-tags');
tel.style.display='flex';
tags.forEach(function(t){
var a=document.createElement('a');
a.href=t.link||'#';
a.textContent=t.name;
tel.appendChild(a);
});
}
/* --- share links --- */
[
['ecl-sh-fb','ecl-sb-fb','https://www.facebook.com/sharer/sharer.php?u='+enc],
['ecl-sh-tw','ecl-sb-tw','https://x.com/share?url='+enc+'&text='+encT],
['ecl-sh-li','ecl-sb-li','https://www.linkedin.com/shareArticle?mini=true&url='+enc+'&title='+encT],
['ecl-sh-wa','ecl-sb-wa','https://wa.me/?text='+encT+'%20'+enc],
['ecl-sh-tg',null,'https://t.me/share/url?url='+enc+'&text='+encT]
].forEach(function(s){
setH(s[0],s[2]);
if(s[1])setH(s[1],s[2]);
});
/* --- build TOC & show page --- */
buildTOC();
gid('ecl-loading').style.display='none';
gid('ecl-main').style.display='block';
initScroll();
document.title=title+' — ECL Report';
}
/* ---- build table of contents ---- */
function buildTOC(){
var hs=document.querySelectorAll('#ecl-content h2,#ecl-content h3');
var list=gid('ecl-toc-list');
if(!hs.length){
list.innerHTML='
No headings found';
return;
}
var idx=0,html='';
hs.forEach(function(h){
var id=h.id||('ech'+(idx++));
h.id=id;h.style.scrollMarginTop='80px';
var cls=h.tagName==='H3'?' class="tl3"':'';
html+='
'+h.textContent+'';
});
list.innerHTML=html;
var links=list.querySelectorAll('a');
var io=new IntersectionObserver(function(es){
es.forEach(function(e){
if(e.isIntersecting){
links.forEach(function(l){l.classList.remove('on');});
var a=list.querySelector('a[href="#'+e.target.id+'"]');
if(a)a.classList.add('on');
}
});
},{rootMargin:'-20% 0px -74% 0px'});
hs.forEach(function(h){io.observe(h);});
links.forEach(function(a){
a.addEventListener('click',function(e){
e.preventDefault();
var t=document.querySelector(a.getAttribute('href'));
if(t)t.scrollIntoView({behavior:'smooth',block:'start'});
});
});
}
/* ---- load related posts (English only via lang=en) ---- */
function loadRelated(post){
var catId=0;
try{catId=post._embedded['wp:term'][0][0].id;}catch(e){}
if(!catId){gid('ecl-related').style.display='none';return;}
var FB=[
'https://images.unsplash.com/photo-1554224155-6726b3ff858f?w=640&q=75',
'https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=640&q=75',
'https://images.unsplash.com/photo-1460925895917-afdab827c52f?w=640&q=75'
];
fetch(BASE+'/wp/v2/posts?_embed=wp:featuredmedia,wp:term&per_page=3&categories='+catId+'&exclude='+post.id+LANG_PARAM)
.then(function(r){return r.json();})
.then(function(posts){
if(!posts||!posts.length){gid('ecl-related').style.display='none';return;}
gid('ecl-rel-grid').innerHTML=posts.map(function(p){
var img='',rcat='',rt=st(p.title.rendered);
try{img=p._embedded['wp:featuredmedia'][0].source_url;}catch(e){}
if(!img)img=FB[p.id%3];
try{rcat=p._embedded['wp:term'][0][0].name;}catch(e){}
return '
'
+''
+''
+'
'+esc(rcat)+'
'
+'
'+esc(rt)+'
'
+'
Read article
'
+'
';
}).join('');
})
.catch(function(){gid('ecl-related').style.display='none';});
}
/* ---- WPML: fetch Arabic translation link ---- */
function loadWpmlTranslations(id){
/*
* WPML REST API endpoint for translation info.
* Endpoint: /wp-json/wpml/v1/post_translations?post_id=ID
* Returns: { ar: { link: "...", post_id: N }, ... }
*
* If WPML's REST API is enabled (WPML REST API add-on or WPML 4.3+),
* this call will return the Arabic equivalent URL.
*/
fetch(BASE+'/wpml/v1/post_translations?post_id='+id)
.then(function(r){return r.ok?r.json():null;})
.then(function(data){
if(!data)return;
/* try common WPML response shapes */
var arLink=null;
if(data.ar&&data.ar.link)arLink=data.ar.link;
else if(Array.isArray(data)){
var ar=data.find(function(t){return t.language_code==='ar'||t.lang==='ar';});
if(ar)arLink=ar.permalink||ar.link||ar.url;
}
if(arLink){
var card=gid('ecl-wpml-card');
var langLink=gid('ecl-wpml-ar');
var heroBadge=gid('ecl-lang-switch');
if(card)card.style.display='block';
if(langLink)langLink.href=arLink;
if(heroBadge){heroBadge.href=arLink;heroBadge.style.display='inline-flex';}
}
})
.catch(function(){
/* WPML REST API not available — silently skip */
});
}
/* ---- prev / next navigation ---- */
function buildNav(prev,next){
if(prev&&!prev.code){
var el=gid('ecl-prev');
el.href=prev.link;
gid('ecl-prev-t').textContent=st(prev.title.rendered);
el.style.display='flex';
}
if(next&&!next.code){
var el2=gid('ecl-next');
el2.href=next.link;
gid('ecl-next-t').textContent=st(next.title.rendered);
el2.style.display='flex';
}
}
/* ---- scroll: reading progress + progress bar ---- */
function initScroll(){
var prog=gid('ecl-prog');
var fill=gid('ecl-rp-fill');
var pct=gid('ecl-rp-pct');
var art=gid('ecl-content');
window.addEventListener('scroll',function(){
var top=art.getBoundingClientRect().top+scrollY;
var h=art.offsetHeight;
var sc=Math.max(0,scrollY-top);
var p=Math.min(100,Math.round(sc/h*100));
if(prog)prog.style.width=p+'%';
if(fill)fill.style.width=p+'%';
if(pct)pct.textContent=p+'%';
},{passive:true});
}
})();