| 1 |
document.addEventListener('DOMContentLoaded', function () { |
| 2 |
// Create the modal HTML |
| 3 |
const phModalHTML = ` |
| 4 |
<div id="propertyhive_survey_modal"> |
| 5 |
<div class="modal-container"> |
| 6 |
<div class="modal-header"> |
| 7 |
${deactivation_survey.modalHeader} |
| 8 |
</div> |
| 9 |
<div class="modal-content"> |
| 10 |
<h3>${deactivation_survey.modalTitle}</h3> |
| 11 |
<form id="deactivationSurvey"> |
| 12 |
<label><input type="radio" name="reason" value="Broke site"> ${deactivation_survey.brokeSite}</label> |
| 13 |
<label><input type="radio" name="reason" value="Confusing"> ${deactivation_survey.confusing}</label> |
| 14 |
<label><input type="radio" name="reason" value="Poor performance"> ${deactivation_survey.poorPerformance}</label> |
| 15 |
<label><input type="radio" name="reason" value="Inadequate support documentation"> ${deactivation_survey.inadequateSupport}</label> |
| 16 |
<label><input type="radio" name="reason" value="Not needed"> ${deactivation_survey.notNeeded}</label> |
| 17 |
<label><input type="radio" name="reason" value="Found a better plugin"> ${deactivation_survey.betterPlugin}</label> |
| 18 |
<label><input type="radio" name="reason" value="Temporary"> ${deactivation_survey.temporary}</label> |
| 19 |
<label><input type="radio" name="reason" value="Other"> ${deactivation_survey.other}</label> |
| 20 |
|
| 21 |
<label id="otherLabel" style="margin-top:25px;">${deactivation_survey.otherLabel}</label> |
| 22 |
<textarea id="phOtherReasonBox" name="otherReason" rows="4" cols="50"></textarea> |
| 23 |
</form> |
| 24 |
</div> |
| 25 |
<div class="modal-footer"> |
| 26 |
<div class="anonymous" id="anonymous"> |
| 27 |
<label><input type="checkbox" name="anonymous" value="yes"> ${deactivation_survey.anonymous}</label> |
| 28 |
</div> |
| 29 |
<button type="button" id="deactivateButton" class="button">${deactivation_survey.skipDeactivate}</button> |
| 30 |
<button type="button" id="cancelButton" class="button">${deactivation_survey.cancel}</button> |
| 31 |
</div> |
| 32 |
</div> |
| 33 |
</div> |
| 34 |
`; |
| 35 |
|
| 36 |
// Add modal to body |
| 37 |
document.body.insertAdjacentHTML('beforeend', phModalHTML); |
| 38 |
|
| 39 |
// References to modal and buttons |
| 40 |
const modal = document.getElementById('propertyhive_survey_modal'); |
| 41 |
const deactivateButton = document.getElementById('deactivateButton'); |
| 42 |
const cancelButton = document.getElementById('cancelButton'); |
| 43 |
const otherLabel = document.getElementById('otherLabel'); |
| 44 |
const anonymous = document.getElementById('anonymous'); |
| 45 |
|
| 46 |
// Show/hide "Other" text box and change button text |
| 47 |
document.querySelectorAll('input[name="reason"]').forEach((input) => { |
| 48 |
input.addEventListener('change', function () { |
| 49 |
|
| 50 |
switch (this.value) |
| 51 |
{ |
| 52 |
case "Broke site": { otherLabel.innerHTML = deactivation_survey.brokeSiteLabel; break; } |
| 53 |
case "Found a better plugin": { otherLabel.innerHTML = deactivation_survey.betterPluginLabel; break; } |
| 54 |
default: { otherLabel.innerHTML = deactivation_survey.otherLabel; break; } |
| 55 |
} |
| 56 |
|
| 57 |
// Change button text to "Deactivate" |
| 58 |
deactivateButton.textContent = deactivation_survey.deactivate; |
| 59 |
deactivateButton.className = "button button-primary"; |
| 60 |
|
| 61 |
anonymous.style.display = 'block'; |
| 62 |
}); |
| 63 |
}); |
| 64 |
|
| 65 |
// Close modal when clicking outside modal content |
| 66 |
window.addEventListener('click', function (event) { |
| 67 |
if (event.target === modal) { |
| 68 |
ph_deactivate_close_modal(); |
| 69 |
} |
| 70 |
}); |
| 71 |
|
| 72 |
// Cancel button closes the modal |
| 73 |
cancelButton.addEventListener('click', ph_deactivate_close_modal); |
| 74 |
|
| 75 |
// Deactivate button functionality |
| 76 |
deactivateButton.addEventListener('click', function () { |
| 77 |
const selectedReason = document.querySelector('input[name="reason"]:checked'); |
| 78 |
const otherReason = phOtherReasonBox.value; |
| 79 |
const anonymousCheckbox = document.querySelector('input[name="anonymous"]'); |
| 80 |
const isAnonymous = anonymousCheckbox && anonymousCheckbox.checked; |
| 81 |
|
| 82 |
// If a reason is selected, send data to the third-party URL |
| 83 |
if (selectedReason) { |
| 84 |
const reasonData = { |
| 85 |
action: 'propertyhive_deactivate_survey', |
| 86 |
nonce: deactivation_survey.nonce, |
| 87 |
reason: selectedReason.value, |
| 88 |
comments: otherReason, |
| 89 |
anonymous: isAnonymous ? 'yes' : 'no', |
| 90 |
}; |
| 91 |
|
| 92 |
jQuery.ajax({ |
| 93 |
url: ajaxurl, // Provided by WordPress |
| 94 |
method: 'POST', |
| 95 |
data: reasonData, // jQuery automatically converts this to x-www-form-urlencoded |
| 96 |
dataType: 'json', // Expect JSON response from server |
| 97 |
success: function (response) { |
| 98 |
if (response.success) { |
| 99 |
console.log('Success:', response.data); |
| 100 |
} else { |
| 101 |
console.error('Error:', response.data); |
| 102 |
} |
| 103 |
ph_deactivate_plugin(); |
| 104 |
}, |
| 105 |
error: function (jqXHR, textStatus, errorThrown) { |
| 106 |
console.error('AJAX Error:', textStatus, errorThrown); |
| 107 |
ph_deactivate_plugin(); |
| 108 |
}, |
| 109 |
}); |
| 110 |
} else { |
| 111 |
// No reason selected, deactivate immediately |
| 112 |
ph_deactivate_plugin(); |
| 113 |
} |
| 114 |
}); |
| 115 |
|
| 116 |
document.getElementById('phOtherReasonBox').addEventListener('input', function () { |
| 117 |
|
| 118 |
const radioButtons = document.querySelectorAll('input[name="reason"]'); |
| 119 |
const otherReasonRadio = document.querySelector('input[name="reason"][value="Other"]'); |
| 120 |
|
| 121 |
// Check if any radio button is selected |
| 122 |
const isAnyRadioSelected = Array.from(radioButtons).some(radio => radio.checked); |
| 123 |
|
| 124 |
// If none are selected, select the 'Other' radio button |
| 125 |
if (!isAnyRadioSelected) { |
| 126 |
otherReasonRadio.checked = true; |
| 127 |
|
| 128 |
const event = new Event('change', { bubbles: true }); |
| 129 |
otherReasonRadio.dispatchEvent(event); |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
// Function to deactivate plugin |
| 134 |
function ph_deactivate_plugin() { |
| 135 |
const originalDeactivateLink = document.getElementById('deactivate-propertyhive'); |
| 136 |
if (originalDeactivateLink) { |
| 137 |
window.location.href = originalDeactivateLink.href; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// Function to close modal |
| 142 |
function ph_deactivate_close_modal() { |
| 143 |
modal.style.display = 'none'; |
| 144 |
ph_deactivate_reset_modal(); // Reset modal when closed |
| 145 |
} |
| 146 |
|
| 147 |
// Function to reset modal |
| 148 |
function ph_deactivate_reset_modal() { |
| 149 |
// Clear selected options |
| 150 |
const selectedOptions = document.querySelectorAll('input[name="reason"]:checked'); |
| 151 |
selectedOptions.forEach((option) => (option.checked = false)); |
| 152 |
|
| 153 |
// Hide the "Other" text box |
| 154 |
otherLabel.innerHTML = deactivation_survey.otherLabel; |
| 155 |
|
| 156 |
// Reset the button text |
| 157 |
deactivateButton.textContent = deactivation_survey.skipDeactivate; |
| 158 |
deactivateButton.className = 'button'; |
| 159 |
|
| 160 |
anonymous.style.display = 'none'; |
| 161 |
} |
| 162 |
|
| 163 |
// Attach event listener to your specified button |
| 164 |
const deactivateButtonTrigger = document.getElementById('deactivate-propertyhive'); |
| 165 |
if (deactivateButtonTrigger) { |
| 166 |
deactivateButtonTrigger.addEventListener('click', function (event) { |
| 167 |
event.preventDefault(); // Prevent default deactivation |
| 168 |
modal.style.display = 'flex'; // Show the modal |
| 169 |
}); |
| 170 |
} |
| 171 |
}); |
| 172 |
|