Plugin code
This commit is contained in:
parent
8819c70d0e
commit
2fde6edfdc
28 changed files with 6338 additions and 0 deletions
186
admin/js/cklph-wdl-admin.js
Normal file
186
admin/js/cklph-wdl-admin.js
Normal file
|
@ -0,0 +1,186 @@
|
|||
/**
|
||||
* 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";
|
||||
}
|
||||
}
|
566
admin/js/cklph-wdl-settings.js
Normal file
566
admin/js/cklph-wdl-settings.js
Normal file
|
@ -0,0 +1,566 @@
|
|||
/**
|
||||
* The JavaScript code for the settings
|
||||
*
|
||||
*/
|
||||
|
||||
// show/hide setup for shortlink.
|
||||
function cklph_wdl_shortlink_setup(shortlink) {
|
||||
var tabBitly = document.getElementById('tab-bitly');
|
||||
var tabYourls = document.getElementById('tab-yourls');
|
||||
var tabjoturl = document.getElementById('tab-joturl');
|
||||
var tabTinyurl = document.getElementById('tab-tinyurl');
|
||||
|
||||
switch (shortlink) {
|
||||
case 'tinyurl':
|
||||
tabTinyurl.classList.toggle('active');
|
||||
tabBitly.classList.remove('active');
|
||||
tabYourls.classList.remove('active');
|
||||
tabjoturl.classList.remove('active');
|
||||
break;
|
||||
case 'bitly':
|
||||
tabBitly.classList.toggle('active');
|
||||
tabYourls.classList.remove('active');
|
||||
tabTinyurl.classList.remove('active');
|
||||
tabjoturl.classList.remove('active');
|
||||
break;
|
||||
case 'yourls':
|
||||
tabYourls.classList.toggle('active');
|
||||
tabBitly.classList.remove('active');
|
||||
tabjoturl.classList.remove('active');
|
||||
tabTinyurl.classList.remove('active');
|
||||
break;
|
||||
case 'joturl':
|
||||
tabjoturl.classList.toggle('active');
|
||||
tabYourls.classList.remove('active');
|
||||
tabTinyurl.classList.remove('active');
|
||||
tabBitly.classList.remove('active');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Bulk refresh checkout links.
|
||||
function cklph_wdl_bulk_rest() {
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
var cklphWdlBtn = document.getElementById('cklph-wdl-bulk-btn');
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
|
||||
cklphWdlBtn.style.pointerEvents = "none";
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Refreshing shortlinks please wait!</strong></p>';
|
||||
|
||||
fetch('/wp-json/cklph-wdl/v1/product/bulk', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(urlResponse => {
|
||||
switch(urlResponse) {
|
||||
case 200:
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Shortlinks Refreshed.</strong></p>';
|
||||
break;
|
||||
case '401-bitly':
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid Bitly API Key.</strong></p>';
|
||||
break;
|
||||
case '401-yourls':
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature.</strong></p>';
|
||||
break;
|
||||
default:
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>No URL Shortener is activated. Please set and activate a shortener to refresh all links.</strong></p>';
|
||||
break;
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
cklphWdlBtn.classList.toggle("cklph-wdl-loading");
|
||||
cklphWdlBtn.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This Section is for tinyurl setup and activate.
|
||||
*
|
||||
* Activate tinyurl in settings page.
|
||||
*/
|
||||
function cklph_wdl_activate_tinyurl() {
|
||||
var loadBitly = document.getElementById('bitly-span');
|
||||
var loadYourls = document.getElementById('yourls-span');
|
||||
var loadJoturl = document.getElementById('joturl-span');
|
||||
var loadTinyurl = document.getElementById('tinyurl-span');
|
||||
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
var onclickTinyurl = document.getElementById('tinyurl_activate_btn');
|
||||
var tinyurlApiKey = document.getElementById('tinyurl_access_token_options');
|
||||
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
onclickTinyurl.classList.toggle("cklph-wdl-loading");
|
||||
onclickTinyurl.style.pointerEvents = "none";
|
||||
|
||||
fetch(`/wp-json/cklph-wdl/v1/shortlink/tinyurl/`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(shortlinkResponse => {
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
if ('tinyurl' === shortlinkResponse) {
|
||||
loadBitly.innerHTML = 'Activate';
|
||||
loadYourls.innerHTML = 'Activate';
|
||||
loadJoturl.innerHTML = 'Activate';
|
||||
loadTinyurl.innerHTML = 'Deactivate';
|
||||
if (tinyurlApiKey.value) {
|
||||
replyDiv.innerHTML = '<p><strong>TinyURL successfully activated using API Key.</strong></p>';
|
||||
return;
|
||||
}
|
||||
replyDiv.innerHTML = '<p><strong>TinyURL successfully activated without using API Key.</strong></p>';
|
||||
} else {
|
||||
loadTinyurl.innerHTML = 'Activate';
|
||||
replyDiv.innerHTML = '<p><strong>TinyURL successfully deactivated.</strong></p>';
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
onclickTinyurl.classList.toggle("cklph-wdl-loading");
|
||||
onclickTinyurl.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
// Save settings for tinyurl setup.
|
||||
function cklph_wdl_setup_tinyurl() {
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
var tinyurlSaveBtn = document.getElementById('tinyurl-save-btn');
|
||||
var tinyurlDomain = document.getElementById('tinyurl_domain');
|
||||
var tinyurlApiKey = document.getElementById('tinyurl_access_token_options');
|
||||
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
tinyurlSaveBtn.classList.toggle("cklph-wdl-loading");
|
||||
tinyurlSaveBtn.style.pointerEvents = "none";
|
||||
|
||||
fetch(`/wp-json/cklph-wdl/v1/tinyurl/?domain=${tinyurlDomain.value}&apikey=${tinyurlApiKey.value}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(tinyurlResponse => {
|
||||
if (200 === tinyurlResponse) {
|
||||
if (tinyurlApiKey.value) {
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Settings Saved.</strong></p>`;
|
||||
return;
|
||||
}
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Unable to save TinyURL settings because Access token is empty.</strong></p>`;
|
||||
} else if ('401-tinyurl' === tinyurlResponse) {
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid TinyURL API Key.</strong></p>';
|
||||
tinyurlDomain.value = '';
|
||||
} else if ('422-tinyurl' === tinyurlResponse) {
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Unable to save TinyURL Domain. An Error has occured. Error code: 422 : Domain not found</strong></p>';
|
||||
tinyurlDomain.value = '';
|
||||
} else {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Unable to save TinyURL settings. ${tinyurlResponse}</strong></p>`;
|
||||
tinyurlDomain.value = '';
|
||||
tinyurlApiKey.value = '';
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
tinyurlSaveBtn.classList.toggle("cklph-wdl-loading");
|
||||
tinyurlSaveBtn.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This Section is for bitly setup and activate.
|
||||
*
|
||||
* Activate bitly in settings page.
|
||||
*/
|
||||
function cklph_wdl_activate_bitly() {
|
||||
var loadBitly = document.getElementById('bitly-span');
|
||||
var loadYourls = document.getElementById('yourls-span');
|
||||
var loadJoturl = document.getElementById('joturl-span');
|
||||
var loadTinyurl = document.getElementById('tinyurl-span');
|
||||
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
var onclickBitly = document.getElementById('bitly_activate_btn');
|
||||
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
onclickBitly.classList.toggle("cklph-wdl-loading");
|
||||
onclickBitly.style.pointerEvents = "none";
|
||||
|
||||
fetch(`/wp-json/cklph-wdl/v1/shortlink/bitly/`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(shortlinkResponse => {
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
if ('bitly' === shortlinkResponse) {
|
||||
loadTinyurl.innerHTML = 'Activate';
|
||||
loadYourls.innerHTML = 'Activate';
|
||||
loadJoturl.innerHTML = 'Activate';
|
||||
loadBitly.innerHTML = 'Deactivate';
|
||||
replyDiv.innerHTML = '<p><strong>Bitly successfully activated</strong></p>';
|
||||
} else if ('401-bitly' === shortlinkResponse) {
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid Bitly API Key to activate Bitly shortener.</strong></p>';
|
||||
} else {
|
||||
loadBitly.innerHTML = 'Activate';
|
||||
replyDiv.innerHTML = '<p><strong>Bitly successfully deactivated.</strong></p>';
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
onclickBitly.classList.toggle("cklph-wdl-loading");
|
||||
onclickBitly.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
// Save settings for bitly setup.
|
||||
function cklph_wdl_setup_bitly() {
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
var bitlySaveBtn = document.getElementById('bitly-save-btn');
|
||||
var bitlyApiKey = document.getElementById('bitly_access_token_options');
|
||||
var loadBitly = document.getElementById('bitly-span');
|
||||
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
bitlySaveBtn.classList.toggle("cklph-wdl-loading");
|
||||
bitlySaveBtn.style.pointerEvents = "none";
|
||||
|
||||
fetch(`/wp-json/cklph-wdl/v1/bitly/?apikey=${bitlyApiKey.value}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(bitlyResponse => {
|
||||
if (200 === bitlyResponse) {
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Settings Saved.</strong></p>`;
|
||||
} else if ('401-bitly' === bitlyResponse) {
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid Bitly API Key.</strong></p>';
|
||||
loadBitly.innerHTML = 'Activate';
|
||||
} else {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Unable to save Bitly settings. ${bitlyResponse}</strong></p>`;
|
||||
loadBitly.innerHTML = 'Activate';
|
||||
bitlyApiKey.value = '';
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
bitlySaveBtn.classList.toggle("cklph-wdl-loading");
|
||||
bitlySaveBtn.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This Section is for yourls setup and activate.
|
||||
*
|
||||
* Activate yourls in settings page.
|
||||
*/
|
||||
function cklph_wdl_activate_yourls() {
|
||||
var loadBitly = document.getElementById('bitly-span');
|
||||
var loadYourls = document.getElementById('yourls-span');
|
||||
var loadJoturl = document.getElementById('joturl-span');
|
||||
var loadTinyurl = document.getElementById('tinyurl-span');
|
||||
|
||||
var onclickYourls = document.getElementById('yourls_activate_btn');
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
onclickYourls.classList.toggle("cklph-wdl-loading");
|
||||
onclickYourls.style.pointerEvents = "none";
|
||||
|
||||
fetch(`/wp-json/cklph-wdl/v1/shortlink/yourls/`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(shortlinkResponse => {
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
if ('yourls' === shortlinkResponse) {
|
||||
loadBitly.innerHTML = 'Activate';
|
||||
loadJoturl.innerHTML = 'Activate';
|
||||
loadTinyurl.innerHTML = 'Activate';
|
||||
loadYourls.innerHTML = 'Deactivate';
|
||||
replyDiv.innerHTML = '<p><strong>YoURLS successfully activated</strong></p>';
|
||||
} else if ('401-yourls' === shortlinkResponse) {
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature to activate YoURLS shortener.</strong></p>';
|
||||
} else {
|
||||
loadYourls.innerHTML = 'Activate';
|
||||
replyDiv.innerHTML = '<p><strong>YoURLS successfully deactivated.</strong></p>';
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
onclickYourls.classList.toggle("cklph-wdl-loading");
|
||||
onclickYourls.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
// Save settings for yourls setup.
|
||||
function cklph_wdl_setup_yourls() {
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
var yourlsSaveBtn = document.getElementById('yourls-save-btn');
|
||||
var yourlsDomain = document.getElementById('yourls_domain');
|
||||
var yourlsApiKey = document.getElementById('yourls_signature');
|
||||
var loadYourls = document.getElementById('yourls-span');
|
||||
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
yourlsSaveBtn.classList.toggle("cklph-wdl-loading");
|
||||
yourlsSaveBtn.style.pointerEvents = "none";
|
||||
|
||||
fetch(`/wp-json/cklph-wdl/v1/yourls/?domain=${yourlsDomain.value}&apikey=${yourlsApiKey.value}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(yourlsResponse => {
|
||||
if ('401-yourls' === yourlsResponse) {
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid YoURLS Site & Signature.</strong></p>';
|
||||
} else if (200 !== yourlsResponse) {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Unable to save YoURLS settings. ${yourlsResponse}</strong></p>`;
|
||||
yourlsDomain.value = '';
|
||||
yourlsApiKey.value = '';
|
||||
loadYourls.innerHTML = 'Activate';
|
||||
} else {
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Settings Saved.</strong></p>`;
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
yourlsSaveBtn.classList.toggle("cklph-wdl-loading");
|
||||
yourlsSaveBtn.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This Section is for JotURL setup and activate.
|
||||
*
|
||||
* Activate JotURL in settings page.
|
||||
*/
|
||||
function cklph_wdl_activate_joturl() {
|
||||
var loadBitly = document.getElementById('bitly-span');
|
||||
var loadYourls = document.getElementById('yourls-span');
|
||||
var loadJoturl = document.getElementById('joturl-span');
|
||||
var loadTinyurl = document.getElementById('tinyurl-span');
|
||||
|
||||
var onclickJoturl = document.getElementById('joturl_activate_btn');
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
onclickJoturl.classList.toggle("cklph-wdl-loading");
|
||||
onclickJoturl.style.pointerEvents = "none";
|
||||
|
||||
fetch(`/wp-json/cklph-wdl/v1/shortlink/joturl/`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(shortlinkResponse => {
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
if ('joturl' === shortlinkResponse) {
|
||||
loadBitly.innerHTML = 'Activate';
|
||||
loadYourls.innerHTML = 'Activate';
|
||||
loadTinyurl.innerHTML = 'Activate';
|
||||
loadJoturl.innerHTML = 'Deactivate';
|
||||
replyDiv.innerHTML = '<p><strong>JotURL successfully activated</strong></p>';
|
||||
} else if ('401-joturl' === shortlinkResponse) {
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid Public & Private API Key to activate JotURL shortener.</strong></p>';
|
||||
} else {
|
||||
loadJoturl.innerHTML = 'Activate';
|
||||
replyDiv.innerHTML = '<p><strong>JotURL successfully deactivated.</strong></p>';
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
onclickJoturl.classList.toggle("cklph-wdl-loading");
|
||||
onclickJoturl.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
function cklph_wdl_setup_joturl() {
|
||||
var loadJoturl = document.getElementById('joturl-span');
|
||||
var ckplhWdlReply = document.getElementById('cklph-wdl-reply');
|
||||
var joturlSaveBtn = document.getElementById('joturl-save-btn');
|
||||
var joturlPublic = document.getElementById('joturl_public_key');
|
||||
var joturlPrivate = document.getElementById('joturl_private_key');
|
||||
|
||||
var replyDiv = (!document.getElementById('cklph-message')) ? document.createElement('div') : document.getElementById('cklph-message');
|
||||
var dismissButton = document.createElement('button');
|
||||
|
||||
replyDiv.setAttribute('id', 'cklph-message');
|
||||
dismissButton.setAttribute('type', 'button');
|
||||
dismissButton.setAttribute('onclick', 'this.closest(\'div\').remove()');
|
||||
dismissButton.className = 'notice-dismiss';
|
||||
ckplhWdlReply.appendChild(replyDiv);
|
||||
|
||||
joturlSaveBtn.classList.toggle("cklph-wdl-loading");
|
||||
joturlSaveBtn.style.pointerEvents = "none";
|
||||
|
||||
fetch(`/wp-json/cklph-wdl/v1/joturl/?public=${joturlPublic.value}&private=${joturlPrivate.value}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-WP-Nonce': cklph_wdl.nonce,
|
||||
}
|
||||
}).then(response => {
|
||||
return response.json();
|
||||
}).then(joturlResponse => {
|
||||
if ('401-joturl' === joturlResponse) {
|
||||
replyDiv.className = "notice notice-warning settings-error is-dismissible";
|
||||
replyDiv.innerHTML = '<p><strong>Please enter a valid JotURL Public & Private API Key.</strong></p>';
|
||||
} else if (200 !== joturlResponse) {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Unable to save JotURL settings. ${joturlResponse}</strong></p>`;
|
||||
joturlPublic.value = '';
|
||||
joturlPrivate.value = '';
|
||||
loadJoturl.innerHTML = 'Activate';
|
||||
} else {
|
||||
replyDiv.className = "notice notice-success settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>Settings Saved.</strong></p>`;
|
||||
}
|
||||
}).catch(error => {
|
||||
replyDiv.className = "notice notice-error settings-error is-dismissible";
|
||||
replyDiv.innerHTML = `<p><strong>An unknown error occured. ${error}</strong></p>`;
|
||||
}).finally(() => {
|
||||
replyDiv.appendChild(dismissButton);
|
||||
joturlSaveBtn.classList.toggle("cklph-wdl-loading");
|
||||
joturlSaveBtn.style.pointerEvents = "auto";
|
||||
});
|
||||
}
|
||||
|
||||
jQuery( function( $ ) {
|
||||
var short_link = $(".shortener");
|
||||
if ( short_link.length ) {
|
||||
short_link.each(function() {
|
||||
var get_active_box = $(this).find('.cklph-wdl-btn-text').html();
|
||||
if (get_active_box == 'Deactivate') {
|
||||
// add a class to active shortlink method
|
||||
$(this).addClass('active-shortener');
|
||||
|
||||
// add a css to active shortlink method
|
||||
$(this).css('background-color','#d3d3d3');
|
||||
$(this).find("[name='activate_btn']").css('background-color','#218838');
|
||||
$(this).find("[name='activate_btn']").css('border-color','#1e7e34');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue