186 lines
7.1 KiB
JavaScript
186 lines
7.1 KiB
JavaScript
/**
|
|
* The JavaScript code for the admin
|
|
*
|
|
*/
|
|
|
|
// Copy the checkout url of product.
|
|
function cklph_wdl_copyurl( id ){
|
|
copyText = document.getElementById(`cklph-wdl-url-${id}`);
|
|
copyText.select();
|
|
copyText.setSelectionRange(0, 99999);
|
|
document.execCommand("copy");
|
|
|
|
var tooltip = document.getElementById(`myTooltip-${id}`);
|
|
tooltip.innerHTML = "Link has been copied";
|
|
}
|
|
|
|
// Copy tooltip.
|
|
function cklph_wdl_out( id ) {
|
|
var tooltip = document.getElementById(`myTooltip-${id}`);
|
|
tooltip.innerHTML = "Copy";
|
|
}
|
|
|
|
// Refresh checkout links.
|
|
function cklph_wdl_rest( id ) {
|
|
var cklphWdlBtn = document.getElementById(`cklph-wdl-btn-${id}`);
|
|
var cklphWdlHidden = document.getElementById('cklph-wdl-hidden');
|
|
var countClick = 0;
|
|
|
|
cklphWdlHidden.value = 'clicked';
|
|
cklph_wdl_refresh(id);
|
|
|
|
cklphWdlBtn.onclick = () => {
|
|
if('created' === cklphWdlHidden.value) {
|
|
cklph_wdl_refresh(id);
|
|
cklphWdlHidden.value = 'exists';
|
|
countClick += 2;
|
|
} else {
|
|
countClick += 1;
|
|
if(1 === countClick) {
|
|
cklph_wdl_force_joturl_refresh(id);
|
|
cklphWdlHidden.value = 'exists';
|
|
} else {
|
|
cklph_wdl_refresh(id);
|
|
cklphWdlHidden.value = 'exists';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function cklph_wdl_refresh(id) {
|
|
var cklphWdlHidden = document.getElementById('cklph-wdl-hidden');
|
|
var replyDiv = document.getElementById("cklph-wdl-reply");
|
|
var cklphWdlBtn = document.getElementById(`cklph-wdl-btn-${id}`);
|
|
var cklphWdlInput = document.getElementById(`cklph-wdl-url-${id}`);
|
|
var ckplhWdlReply = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
|
var dismissButton = document.createElement('button');
|
|
|
|
ckplhWdlReply.setAttribute('id', 'cklph-message');
|
|
dismissButton.setAttribute('type', 'button');
|
|
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
|
dismissButton.className = 'notice-dismiss';
|
|
replyDiv.appendChild(ckplhWdlReply);
|
|
|
|
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
|
|
cklphWdlBtn.style.pointerEvents = "none";
|
|
|
|
// run wp rest api to convert links and return success or error.
|
|
try {
|
|
try {
|
|
const response = await fetch(`/wp-json/cklph-wdl/v1/product/${id}/`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-WP-Nonce': cklph_wdl.nonce,
|
|
}
|
|
});
|
|
const urlResponse = await response.json();
|
|
cklphWdlInput.value = urlResponse[0];
|
|
switch (urlResponse[1]) {
|
|
case 200:
|
|
ckplhWdlReply.className = "notice notice-success settings-error is-dismissible";
|
|
if('clicked' === cklphWdlHidden.value) {
|
|
ckplhWdlReply.innerHTML = `<p><strong>Link already exists. Please press Refresh Link again to force refresh shortlink. ${urlResponse[0]}</strong></p>`;
|
|
} else {
|
|
ckplhWdlReply.innerHTML = `<p><strong>Link already refreshed. ${urlResponse[0]}</strong></p>`;
|
|
}
|
|
break;
|
|
case 201:
|
|
ckplhWdlReply.className = "notice notice-success settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = `<p><strong>Link successfully created. ${urlResponse[0]}</strong></p>`;
|
|
cklphWdlHidden.value = 'created';
|
|
break;
|
|
case '401-bitly':
|
|
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid Bitly API Key.</strong></p>';
|
|
break;
|
|
case '401-yourls':
|
|
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature.</strong></p>';
|
|
break;
|
|
case '401-joturl':
|
|
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid JotURL Public & Private API Key.</strong></p>';
|
|
break;
|
|
default:
|
|
ckplhWdlReply.className = "notice notice-error settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = `<p><strong>${urlResponse[1]}</strong></p>`;
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
ckplhWdlReply.className = "notice notice-error settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
|
}
|
|
} finally {
|
|
ckplhWdlReply.appendChild(dismissButton);
|
|
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
|
|
cklphWdlBtn.style.pointerEvents = "auto";
|
|
}
|
|
}
|
|
|
|
async function cklph_wdl_force_joturl_refresh(id) {
|
|
var cklphWdlHidden = document.getElementById('cklph-wdl-hidden');
|
|
var replyDiv = document.getElementById("cklph-wdl-reply");
|
|
var cklphWdlBtn = document.getElementById(`cklph-wdl-btn-${id}`);
|
|
var cklphWdlInput = document.getElementById(`cklph-wdl-url-${id}`);
|
|
var ckplhWdlReply = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
|
var dismissButton = document.createElement('button');
|
|
|
|
dismissButton.setAttribute('type', 'button');
|
|
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
|
dismissButton.className = 'notice-dismiss';
|
|
replyDiv.appendChild(ckplhWdlReply);
|
|
|
|
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
|
|
cklphWdlBtn.style.pointerEvents = "none";
|
|
|
|
|
|
// run wp rest api to convert links and return success or error.
|
|
try {
|
|
try {
|
|
const response = await fetch(`/wp-json/cklph-wdl/v1/product-joturl/${id}/`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-WP-Nonce': cklph_wdl.nonce,
|
|
}
|
|
});
|
|
const urlResponse = await response.json();
|
|
cklphWdlInput.value = urlResponse[0];
|
|
switch (urlResponse[1]) {
|
|
case 200:
|
|
ckplhWdlReply.className = "notice notice-success settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = `<p><strong>Link successfully refreshed. ${urlResponse[0]}</strong></p>`;
|
|
break;
|
|
case 201:
|
|
ckplhWdlReply.className = "notice notice-success settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = `<p><strong>Link successfully created. ${urlResponse[0]}</strong></p>`;
|
|
cklphWdlHidden.value = 'created';
|
|
break;
|
|
case '401-bitly':
|
|
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid Bitly API Key.</strong></p>';
|
|
break;
|
|
case '401-yourls':
|
|
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature.</strong></p>';
|
|
break;
|
|
case '401-joturl':
|
|
ckplhWdlReply.className = "notice notice-warning settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = '<p><strong>Please enter a valid JotURL Public & Private API Key.</strong></p>';
|
|
break;
|
|
default:
|
|
ckplhWdlReply.className = "notice notice-error settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = `<p><strong>${urlResponse[1]}</strong></p>`;
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
ckplhWdlReply.className = "notice notice-error settings-error is-dismissible";
|
|
ckplhWdlReply.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
|
}
|
|
} finally {
|
|
ckplhWdlReply.appendChild(dismissButton);
|
|
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
|
|
cklphWdlBtn.style.pointerEvents = "auto";
|
|
}
|
|
} |