/** * 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 = '
Refreshing shortlinks please wait!
'; 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 = 'Shortlinks Refreshed.
'; break; case '401-bitly': replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'Please enter a valid Bitly API Key.
'; break; case '401-yourls': replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'Please enter a valid YoURLS Site & Signature.
'; break; default: replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'No URL Shortener is activated. Please set and activate a shortener to refresh all links.
'; break; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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 = 'TinyURL successfully activated using API Key.
'; return; } replyDiv.innerHTML = 'TinyURL successfully activated without using API Key.
'; } else { loadTinyurl.innerHTML = 'Activate'; replyDiv.innerHTML = 'TinyURL successfully deactivated.
'; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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 = `Settings Saved.
`; return; } replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `Unable to save TinyURL settings because Access token is empty.
`; } else if ('401-tinyurl' === tinyurlResponse) { replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'Please enter a valid TinyURL API Key.
'; tinyurlDomain.value = ''; } else if ('422-tinyurl' === tinyurlResponse) { replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'Unable to save TinyURL Domain. An Error has occured. Error code: 422 : Domain not found
'; tinyurlDomain.value = ''; } else { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `Unable to save TinyURL settings. ${tinyurlResponse}
`; tinyurlDomain.value = ''; tinyurlApiKey.value = ''; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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 = 'Bitly successfully activated
'; } else if ('401-bitly' === shortlinkResponse) { replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'Please enter a valid Bitly API Key to activate Bitly shortener.
'; } else { loadBitly.innerHTML = 'Activate'; replyDiv.innerHTML = 'Bitly successfully deactivated.
'; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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 = `Settings Saved.
`; } else if ('401-bitly' === bitlyResponse) { replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'Please enter a valid Bitly API Key.
'; loadBitly.innerHTML = 'Activate'; } else { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `Unable to save Bitly settings. ${bitlyResponse}
`; loadBitly.innerHTML = 'Activate'; bitlyApiKey.value = ''; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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 = 'YoURLS successfully activated
'; } else if ('401-yourls' === shortlinkResponse) { replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'Please enter a valid YoURLS Site & Signature to activate YoURLS shortener.
'; } else { loadYourls.innerHTML = 'Activate'; replyDiv.innerHTML = 'YoURLS successfully deactivated.
'; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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 = 'Please enter a valid YoURLS Site & Signature.
'; } else if (200 !== yourlsResponse) { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `Unable to save YoURLS settings. ${yourlsResponse}
`; yourlsDomain.value = ''; yourlsApiKey.value = ''; loadYourls.innerHTML = 'Activate'; } else { replyDiv.className = "notice notice-success settings-error is-dismissible"; replyDiv.innerHTML = `Settings Saved.
`; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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 = 'JotURL successfully activated
'; } else if ('401-joturl' === shortlinkResponse) { replyDiv.className = "notice notice-warning settings-error is-dismissible"; replyDiv.innerHTML = 'Please enter a valid Public & Private API Key to activate JotURL shortener.
'; } else { loadJoturl.innerHTML = 'Activate'; replyDiv.innerHTML = 'JotURL successfully deactivated.
'; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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 = 'Please enter a valid JotURL Public & Private API Key.
'; } else if (200 !== joturlResponse) { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `Unable to save JotURL settings. ${joturlResponse}
`; joturlPublic.value = ''; joturlPrivate.value = ''; loadJoturl.innerHTML = 'Activate'; } else { replyDiv.className = "notice notice-success settings-error is-dismissible"; replyDiv.innerHTML = `Settings Saved.
`; } }).catch(error => { replyDiv.className = "notice notice-error settings-error is-dismissible"; replyDiv.innerHTML = `An unknown error occured. ${error}
`; }).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'); } }); } });