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