| 1 |
jQuery(document).ready(() => { |
| 2 |
let sdevs_enable_subscription = jQuery("input#subscrpt_enable"); |
| 3 |
sdevs_enable_subscription.change(() => { |
| 4 |
if (sdevs_enable_subscription.is(":checked")) { |
| 5 |
jQuery(".show_if_subscription").show(); |
| 6 |
} else { |
| 7 |
jQuery(".show_if_subscription").hide(); |
| 8 |
} |
| 9 |
}); |
| 10 |
if (sdevs_enable_subscription.is(":checked")) { |
| 11 |
jQuery(".show_if_subscription").show(); |
| 12 |
} else { |
| 13 |
jQuery(".show_if_subscription").hide(); |
| 14 |
} |
| 15 |
|
| 16 |
jQuery(document).on("woocommerce_variations_loaded", () => { |
| 17 |
let total_variations = JSON.parse(jQuery(".woocommerce_variations").attr("data-total")); |
| 18 |
for (let index = 0; index < total_variations; index++) { |
| 19 |
let element = document.getElementById("subscrpt_enable[" + index + "]"); |
| 20 |
if (element && element.checked) { |
| 21 |
jQuery(".show_if_subscription_" + index).show(); |
| 22 |
} else { |
| 23 |
jQuery(".show_if_subscription_" + index).hide(); |
| 24 |
} |
| 25 |
} |
| 26 |
}); |
| 27 |
|
| 28 |
let subscrpt_renewal_process = jQuery("#subscrpt_renewal_process"); |
| 29 |
subscrpt_renewal_process.change(() => { |
| 30 |
if (subscrpt_renewal_process.val() === "manual") { |
| 31 |
jQuery("#sdevs_renewal_cart_tr").show(); |
| 32 |
jQuery("#subscrpt_stripe_auto_renew_tr").hide(); |
| 33 |
jQuery("#subscrpt_auto_renewal_toggle_tr").hide(); |
| 34 |
} else { |
| 35 |
jQuery("#sdevs_renewal_cart_tr").hide(); |
| 36 |
jQuery("#subscrpt_stripe_auto_renew_tr").show(); |
| 37 |
jQuery("#subscrpt_auto_renewal_toggle_tr").show(); |
| 38 |
} |
| 39 |
}); |
| 40 |
if (subscrpt_renewal_process.val() === "manual") { |
| 41 |
jQuery("#sdevs_renewal_cart_tr").show(); |
| 42 |
jQuery("#subscrpt_stripe_auto_renew_tr").hide(); |
| 43 |
jQuery("#subscrpt_auto_renewal_toggle_tr").hide(); |
| 44 |
} else { |
| 45 |
jQuery("#sdevs_renewal_cart_tr").hide(); |
| 46 |
jQuery("#subscrpt_stripe_auto_renew_tr").show(); |
| 47 |
jQuery("#subscrpt_auto_renewal_toggle_tr").show(); |
| 48 |
} |
| 49 |
|
| 50 |
// Handle "select all" checkbox for subscription list |
| 51 |
jQuery("#cb-select-all-1").on("change", function () { |
| 52 |
jQuery('input[name="subscription_ids[]"]').prop("checked", this.checked); |
| 53 |
}); |
| 54 |
|
| 55 |
// Update "select all" checkbox when individual checkboxes change |
| 56 |
jQuery('input[name="subscription_ids[]"]').on("change", function () { |
| 57 |
var totalCheckboxes = jQuery('input[name="subscription_ids[]"]').length; |
| 58 |
var checkedCheckboxes = jQuery('input[name="subscription_ids[]"]:checked').length; |
| 59 |
|
| 60 |
if (checkedCheckboxes === 0) { |
| 61 |
jQuery("#cb-select-all-1").prop("indeterminate", false).prop("checked", false); |
| 62 |
} else if (checkedCheckboxes === totalCheckboxes) { |
| 63 |
jQuery("#cb-select-all-1").prop("indeterminate", false).prop("checked", true); |
| 64 |
} else { |
| 65 |
jQuery("#cb-select-all-1").prop("indeterminate", true); |
| 66 |
} |
| 67 |
}); |
| 68 |
|
| 69 |
// Handle bulk action form submission |
| 70 |
jQuery("#subscriptions-form").on("submit", function (e) { |
| 71 |
// Check if this is a bulk action submission by checking the submitter |
| 72 |
var submitter = e.originalEvent ? e.originalEvent.submitter : null; |
| 73 |
var isBulkAction = submitter && (submitter.name === "bulk_action" || submitter.name === "bulk_action2"); |
| 74 |
|
| 75 |
if (!isBulkAction) { |
| 76 |
// This is a filter submission, allow it to proceed |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
// Prevent default form submission for bulk actions |
| 81 |
e.preventDefault(); |
| 82 |
|
| 83 |
var action = jQuery('select[name="action"]').val(); |
| 84 |
var action2 = jQuery('select[name="action2"]').val(); |
| 85 |
var selectedAction = action !== "-1" ? action : action2; |
| 86 |
|
| 87 |
if (selectedAction === "-1") { |
| 88 |
alert("Please select a bulk action."); |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
var checkedBoxes = jQuery('input[name="subscription_ids[]"]:checked'); |
| 93 |
if (checkedBoxes.length === 0) { |
| 94 |
alert("Please select at least one subscription."); |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
// Confirm destructive actions |
| 99 |
if (selectedAction === "delete") { |
| 100 |
if ( |
| 101 |
!confirm( |
| 102 |
"Are you sure you want to permanently delete the selected subscriptions? This action cannot be undone.", |
| 103 |
) |
| 104 |
) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
} else if (selectedAction === "trash") { |
| 108 |
if (!confirm("Are you sure you want to move the selected subscriptions to trash?")) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Handle bulk action via AJAX |
| 114 |
handleBulkAction(selectedAction, checkedBoxes); |
| 115 |
}); |
| 116 |
|
| 117 |
// AJAX function to handle bulk actions |
| 118 |
function handleBulkAction(action, checkedBoxes) { |
| 119 |
var subscriptionIds = []; |
| 120 |
checkedBoxes.each(function () { |
| 121 |
subscriptionIds.push(jQuery(this).val()); |
| 122 |
}); |
| 123 |
|
| 124 |
// Show loading state |
| 125 |
var submitButton = jQuery('input[name="bulk_action"]:focus, input[name="bulk_action2"]:focus'); |
| 126 |
var originalText = submitButton.val(); |
| 127 |
submitButton.val("Processing...").prop("disabled", true); |
| 128 |
|
| 129 |
// Make AJAX request |
| 130 |
jQuery.ajax({ |
| 131 |
url: wp_subscription_ajax.ajaxurl, |
| 132 |
type: "POST", |
| 133 |
data: { |
| 134 |
action: "subscrpt_bulk_action", |
| 135 |
bulk_action: action, |
| 136 |
subscription_ids: subscriptionIds, |
| 137 |
nonce: wp_subscription_ajax.nonce, |
| 138 |
}, |
| 139 |
success: function (response) { |
| 140 |
if (response.success) { |
| 141 |
// Show success message |
| 142 |
showAdminNotice(response.data.message, "success"); |
| 143 |
// Reload the page to reflect changes |
| 144 |
setTimeout(function () { |
| 145 |
window.location.reload(); |
| 146 |
}, 1000); |
| 147 |
} else { |
| 148 |
// Show error message |
| 149 |
showAdminNotice(response.data.message || "An error occurred while processing the bulk action.", "error"); |
| 150 |
} |
| 151 |
}, |
| 152 |
error: function () { |
| 153 |
showAdminNotice("An error occurred while processing the bulk action.", "error"); |
| 154 |
}, |
| 155 |
complete: function () { |
| 156 |
// Reset button state |
| 157 |
submitButton.val(originalText).prop("disabled", false); |
| 158 |
}, |
| 159 |
}); |
| 160 |
} |
| 161 |
|
| 162 |
// Function to show admin notices |
| 163 |
function showAdminNotice(message, type) { |
| 164 |
var noticeClass = type === "success" ? "notice-success" : "notice-error"; |
| 165 |
var notice = jQuery('<div class="notice ' + noticeClass + ' is-dismissible"><p>' + message + "</p></div>"); |
| 166 |
|
| 167 |
// Insert notice at the top of the page |
| 168 |
jQuery(".wp-subscription-admin-content").prepend(notice); |
| 169 |
|
| 170 |
// Auto-dismiss after 5 seconds |
| 171 |
setTimeout(function () { |
| 172 |
notice.fadeOut(function () { |
| 173 |
jQuery(this).remove(); |
| 174 |
}); |
| 175 |
}, 5000); |
| 176 |
} |
| 177 |
}); |
| 178 |
|
| 179 |
function hellochange(index) { |
| 180 |
if (document.getElementById("subscrpt_enable[" + index + "]").checked) { |
| 181 |
jQuery(".show_if_subscription_" + index).show(); |
| 182 |
} else { |
| 183 |
jQuery(".show_if_subscription_" + index).hide(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
let subscrpt_product_type = jQuery("#product-type"); |
| 188 |
let latest_value_of_subscrpt_product_type = subscrpt_product_type.val(); |
| 189 |
|
| 190 |
subscrpt_product_type.change(() => { |
| 191 |
if ( |
| 192 |
"simple" === latest_value_of_subscrpt_product_type && |
| 193 |
"simple" !== subscrpt_product_type.val() && |
| 194 |
"variable" !== subscrpt_product_type.val() |
| 195 |
) { |
| 196 |
const confirmTypeChange = confirm( |
| 197 |
"Are you sure to change the product type ? If product type changed then You'll lose related subscriptions beacuse of they can't be renewed !", |
| 198 |
); |
| 199 |
if (confirmTypeChange) { |
| 200 |
latest_value_of_subscrpt_product_type = subscrpt_product_type.val(); |
| 201 |
} else { |
| 202 |
subscrpt_product_type.val("simple"); |
| 203 |
} |
| 204 |
} |
| 205 |
}); |
| 206 |
|