| 1 |
jQuery(document).ready(function ($) { |
| 2 |
function addPasswordToggle(inputId) { |
| 3 |
const $input = $("#" + inputId); |
| 4 |
if (!$input.length) return; |
| 5 |
|
| 6 |
// Default value. |
| 7 |
let isPassword = $input.attr("type") === "password"; |
| 8 |
|
| 9 |
// Create Dashicon toggle button |
| 10 |
const $toggleBtn = $("<button>") |
| 11 |
.attr("type", "button") |
| 12 |
.addClass("dashicons") |
| 13 |
.addClass(isPassword ? "dashicons-visibility" : "dashicons-hidden") |
| 14 |
.css({ |
| 15 |
border: $input.css("border"), |
| 16 |
borderRadius: $input.css("border-radius"), |
| 17 |
background: $input.css("background-color"), |
| 18 |
height: $input.height(), |
| 19 |
minHeight: "30px", |
| 20 |
width: "35px", |
| 21 |
margin: "0px 8px", |
| 22 |
fontSize: "18px", |
| 23 |
cursor: "pointer", |
| 24 |
lineHeight: 1, |
| 25 |
}) |
| 26 |
.attr("title", "Show password"); |
| 27 |
|
| 28 |
// Toggle functionality |
| 29 |
$toggleBtn.on("click", function () { |
| 30 |
isPassword = $input.attr("type") === "password"; |
| 31 |
$input.attr("type", isPassword ? "text" : "password"); |
| 32 |
$toggleBtn |
| 33 |
.removeClass("dashicons-visibility dashicons-hidden") |
| 34 |
.addClass(isPassword ? "dashicons-hidden" : "dashicons-visibility") |
| 35 |
.attr("title", isPassword ? "Hide password" : "Show password"); |
| 36 |
}); |
| 37 |
|
| 38 |
// Insert button |
| 39 |
$input.after($toggleBtn); |
| 40 |
} |
| 41 |
|
| 42 |
// Add copy button |
| 43 |
function addCopyButton(inputId) { |
| 44 |
const $input = $("#" + inputId); |
| 45 |
if (!$input.length) return; |
| 46 |
|
| 47 |
// Create Dashicon copy button |
| 48 |
const $copyBtn = $("<button>") |
| 49 |
.attr("type", "button") |
| 50 |
.addClass("dashicons dashicons-admin-page") |
| 51 |
.css({ |
| 52 |
border: $input.css("border"), |
| 53 |
borderRadius: $input.css("border-radius"), |
| 54 |
background: $input.css("background-color"), |
| 55 |
height: $input.height(), |
| 56 |
minHeight: "30px", |
| 57 |
width: "35px", |
| 58 |
margin: "0px 8px", |
| 59 |
fontSize: "18px", |
| 60 |
cursor: "pointer", |
| 61 |
lineHeight: 1, |
| 62 |
}) |
| 63 |
.attr("title", "Copy to clipboard"); |
| 64 |
|
| 65 |
// Copy functionality |
| 66 |
$copyBtn.on("click", function (event) { |
| 67 |
event.preventDefault(); |
| 68 |
const text = $input.val(); |
| 69 |
|
| 70 |
try { |
| 71 |
if (navigator.clipboard && navigator.clipboard.writeText) { |
| 72 |
navigator.clipboard.writeText(text); |
| 73 |
} else { |
| 74 |
const tempInput = document.createElement("textarea"); |
| 75 |
tempInput.value = text; |
| 76 |
document.body.appendChild(tempInput); |
| 77 |
tempInput.select(); |
| 78 |
document.execCommand("copy"); |
| 79 |
document.body.removeChild(tempInput); |
| 80 |
} |
| 81 |
alert("Data successfully copied to clipboard."); |
| 82 |
} catch (err) { |
| 83 |
alert("Failed to the data. Please try to copy manually."); |
| 84 |
console.error("Failed to the data. ", err); |
| 85 |
} |
| 86 |
}); |
| 87 |
|
| 88 |
// Insert button |
| 89 |
$input.after($copyBtn); |
| 90 |
} |
| 91 |
|
| 92 |
// Add toggles |
| 93 |
addPasswordToggle("woocommerce_wp_subscription_paypal_client_id"); |
| 94 |
addPasswordToggle("woocommerce_wp_subscription_paypal_client_secret"); |
| 95 |
addPasswordToggle("woocommerce_wp_subscription_paypal_webhook_id"); |
| 96 |
|
| 97 |
addPasswordToggle("woocommerce_wp_subscription_paypal_sandbox_client_id"); |
| 98 |
addPasswordToggle("woocommerce_wp_subscription_paypal_sandbox_client_secret"); |
| 99 |
addPasswordToggle("woocommerce_wp_subscription_paypal_sandbox_webhook_id"); |
| 100 |
|
| 101 |
// Add copy buttons |
| 102 |
addCopyButton("woocommerce_wp_subscription_paypal_webhook_url"); |
| 103 |
}); |
| 104 |
|