|
7 | 7 | */ |
8 | 8 | /* jshint unused:false */ |
9 | 9 | jQuery(function ($) { |
| 10 | + |
| 11 | + /** |
| 12 | + * Check if the user is in live preview mode. |
| 13 | + * Live Preview is used in WordPress Playground to test the plugin. |
| 14 | + * Use this check to deactivate any process that does not showcase the plugin features (e.g. subscription to the newsletter, etc.) |
| 15 | + * |
| 16 | + * @type {boolean} |
| 17 | + */ |
| 18 | + const isLivePreview = window.location.search.includes('env=preview'); |
| 19 | + |
| 20 | + /** |
| 21 | + * Redirect to draft page after subscribe (optional). |
| 22 | + * |
| 23 | + * @param {Object} postData - Post data. |
| 24 | + */ |
| 25 | + const goToDraftPage = function ( postData = {} ) { |
| 26 | + postData ??= {}; |
| 27 | + postData.action ??= 'visualizer_wizard_step_process'; |
| 28 | + postData.security ??= visualizerSetupWizardData.ajax.security; |
| 29 | + postData.step ??= 'step_subscribe'; |
| 30 | + |
| 31 | + // Subscribe the user using the email if provided. Redirect to the draft page. |
| 32 | + $.post(visualizerSetupWizardData.ajax.url, postData, function (res) { |
| 33 | + |
| 34 | + // Toggle the redirect popup. |
| 35 | + $('.redirect-popup').find('h3.popup-title').html(res.message); |
| 36 | + $('.redirect-popup').show(); |
| 37 | + |
| 38 | + if (1 === res.status) { |
| 39 | + setTimeout(function () { |
| 40 | + window.location.href = res.redirect_to; |
| 41 | + }, 5000); |
| 42 | + } else { |
| 43 | + $('.redirect-popup').hide(); |
| 44 | + } |
| 45 | + currentStep.find('.spinner').removeClass('is-active'); |
| 46 | + }).fail(function () { |
| 47 | + $('.redirect-popup').hide(); |
| 48 | + currentStep.find('.spinner').removeClass('is-active'); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
10 | 52 | var provideContent = function(id, stepDirection, stepPosition, selStep, callback) { |
11 | 53 | // Import chart data. |
12 | 54 | if ( 1 == id ) { |
@@ -119,17 +161,22 @@ jQuery(function ($) { |
119 | 161 | ".btn-primary:not(.next-btn,.vz-create-page,.vz-subscribe)", |
120 | 162 | function (e) { |
121 | 163 | var stepNumber = $(this).data("step_number"); |
| 164 | + |
122 | 165 | switch (stepNumber) { |
123 | 166 | case 1: |
124 | 167 | if ($(".vz-radio-btn").is(":checked")) { |
125 | 168 | $('#smartwizard').smartWizard('next'); |
126 | 169 | } |
127 | 170 | break; |
128 | 171 | case 3: |
129 | | - var urlParams = new URLSearchParams(window.location.search); |
130 | | - urlParams.set('preview_chart', Date.now()); |
131 | | - window.location.hash = "#step-3"; |
132 | | - window.location.search = urlParams; |
| 172 | + if ( isLivePreview ) { |
| 173 | + $('#smartwizard').smartWizard('next'); |
| 174 | + } else { |
| 175 | + var urlParams = new URLSearchParams(window.location.search); |
| 176 | + urlParams.set('preview_chart', Date.now()); |
| 177 | + window.location.hash = "#step-3"; |
| 178 | + window.location.search = urlParams; |
| 179 | + } |
133 | 180 | break; |
134 | 181 | case 4: |
135 | 182 | $('#step-4').find('.spinner').addClass('is-active'); |
@@ -181,7 +228,11 @@ jQuery(function ($) { |
181 | 228 | }, |
182 | 229 | function (res) { |
183 | 230 | if (res.status > 0) { |
184 | | - $("#smartwizard").smartWizard("next"); |
| 231 | + if ( isLivePreview ) { |
| 232 | + goToDraftPage(); |
| 233 | + } else { |
| 234 | + $("#smartwizard").smartWizard("next"); |
| 235 | + } |
185 | 236 | } |
186 | 237 | _this.next(".spinner").removeClass("is-active"); |
187 | 238 | } |
@@ -235,21 +286,7 @@ jQuery(function ($) { |
235 | 286 | var currentStep = $( '.vz-wizard-wrap .tab-pane:last-child' ); |
236 | 287 | currentStep.find(".spinner").addClass("is-active"); |
237 | 288 |
|
238 | | - $.post(visualizerSetupWizardData.ajax.url, postData, function (res) { |
239 | | - $('.redirect-popup').find('h3.popup-title').html(res.message); |
240 | | - $('.redirect-popup').show(); |
241 | | - if (1 === res.status) { |
242 | | - setTimeout(function () { |
243 | | - window.location.href = res.redirect_to; |
244 | | - }, 5000); |
245 | | - } else { |
246 | | - $('.redirect-popup').hide(); |
247 | | - } |
248 | | - currentStep.find('.spinner').removeClass('is-active'); |
249 | | - }).fail(function () { |
250 | | - $('.redirect-popup').hide(); |
251 | | - currentStep.find('.spinner').removeClass('is-active'); |
252 | | - }); |
| 289 | + goToDraftPage( postData ); |
253 | 290 | e.preventDefault(); |
254 | 291 | }); |
255 | 292 |
|
|
0 commit comments