| 1 |
/** |
| 2 |
* MLS Import — deactivation exit survey. |
| 3 |
* |
| 4 |
* Intercepts the "Deactivate" link for the MLS Import plugin on the Plugins |
| 5 |
* screen, shows a short survey modal, sends the chosen reason to the server, |
| 6 |
* then proceeds with the original deactivation. Submitting or skipping both |
| 7 |
* complete the deactivation — the user is never trapped. |
| 8 |
*/ |
| 9 |
( function ( $ ) { |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
// Server-provided config: plugin basename, options, i18n strings, ajax url, nonce. |
| 13 |
var cfg = window.mlsimport_deact_survey || {}; |
| 14 |
|
| 15 |
$( function () { |
| 16 |
|
| 17 |
// The Deactivate link inside the MLS Import plugin's row. |
| 18 |
var $link = $( 'tr[data-plugin="' + cfg.plugin_basename + '"] span.deactivate a' ); |
| 19 |
// If the plugin's row/link isn't on this page, there's nothing to intercept. |
| 20 |
if ( ! $link.length ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
// Holds the original deactivation URL captured when the link is clicked. |
| 25 |
var deactivateUrl = ''; |
| 26 |
|
| 27 |
// --- build the modal once ------------------------------------------ |
| 28 |
// Build one radio option per configured reason. |
| 29 |
var optionsHtml = ''; |
| 30 |
$.each( cfg.options || {}, function ( value, label ) { |
| 31 |
optionsHtml += |
| 32 |
'<label class="mlsimport-es-option">' + |
| 33 |
'<input type="radio" name="mlsimport_exit_reason" value="' + value + '"> ' + |
| 34 |
'<span>' + label + '</span>' + |
| 35 |
'</label>'; |
| 36 |
} ); |
| 37 |
|
| 38 |
// Assemble the (initially hidden) survey modal markup. |
| 39 |
var $modal = $( |
| 40 |
'<div id="mlsimport-exit-survey-modal" class="mlsimport-es-hidden">' + |
| 41 |
'<div class="mlsimport-es-backdrop"></div>' + |
| 42 |
'<div class="mlsimport-es-card" role="dialog" aria-modal="true">' + |
| 43 |
'<h2 class="mlsimport-es-title">' + ( cfg.i18n.title || '' ) + '</h2>' + |
| 44 |
'<p class="mlsimport-es-intro">' + ( cfg.i18n.intro || '' ) + '</p>' + |
| 45 |
'<div class="mlsimport-es-options">' + optionsHtml + '</div>' + |
| 46 |
'<textarea class="mlsimport-es-other mlsimport-es-hidden" rows="3" ' + |
| 47 |
'placeholder="' + ( cfg.i18n.other_placeholder || '' ) + '"></textarea>' + |
| 48 |
'<div class="mlsimport-es-actions">' + |
| 49 |
'<button type="button" class="button button-primary mlsimport-es-submit" disabled>' + |
| 50 |
( cfg.i18n.submit || '' ) + |
| 51 |
'</button>' + |
| 52 |
'<a href="#" class="mlsimport-es-skip">' + ( cfg.i18n.skip || '' ) + '</a>' + |
| 53 |
'</div>' + |
| 54 |
'</div>' + |
| 55 |
'</div>' |
| 56 |
); |
| 57 |
$( 'body' ).append( $modal ); |
| 58 |
|
| 59 |
// Cache the Submit button and the free-text "other" field. |
| 60 |
var $submit = $modal.find( '.mlsimport-es-submit' ); |
| 61 |
var $other = $modal.find( '.mlsimport-es-other' ); |
| 62 |
|
| 63 |
// --- intercept the Deactivate click -------------------------------- |
| 64 |
// Prevent the immediate navigation, remember its URL, and open the modal. |
| 65 |
$link.on( 'click', function ( e ) { |
| 66 |
e.preventDefault(); |
| 67 |
deactivateUrl = $( this ).attr( 'href' ); |
| 68 |
$modal.removeClass( 'mlsimport-es-hidden' ); |
| 69 |
} ); |
| 70 |
|
| 71 |
// Selecting a reason enables Submit; "other" reveals the text field. |
| 72 |
$modal.on( 'change', 'input[name="mlsimport_exit_reason"]', function () { |
| 73 |
$submit.prop( 'disabled', false ); |
| 74 |
$other.toggleClass( 'mlsimport-es-hidden', this.value !== 'other' ); |
| 75 |
} ); |
| 76 |
|
| 77 |
/** |
| 78 |
* Navigate to the original deactivation URL, completing the deactivation. |
| 79 |
*/ |
| 80 |
function goDeactivate() { |
| 81 |
if ( deactivateUrl ) { |
| 82 |
window.location.href = deactivateUrl; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Submit: send the answer, then deactivate regardless of the result. |
| 87 |
$submit.on( 'click', function () { |
| 88 |
// Disable to prevent double submits, then POST the chosen reason + details. |
| 89 |
$submit.prop( 'disabled', true ); |
| 90 |
$.post( cfg.ajax_url, { |
| 91 |
action: 'mlsimport_exit_survey_submit', |
| 92 |
security: cfg.nonce, |
| 93 |
reason: $modal.find( 'input[name="mlsimport_exit_reason"]:checked' ).val() || '', |
| 94 |
details: $other.val() || '' |
| 95 |
} ).always( goDeactivate ); |
| 96 |
} ); |
| 97 |
|
| 98 |
// Skip: no data sent, deactivate immediately. |
| 99 |
$modal.on( 'click', '.mlsimport-es-skip', function ( e ) { |
| 100 |
e.preventDefault(); |
| 101 |
goDeactivate(); |
| 102 |
} ); |
| 103 |
|
| 104 |
} ); |
| 105 |
|
| 106 |
}( jQuery ) ); |
| 107 |
|