| 1 |
/** |
| 2 |
* Plugin Name: Visualizer: Tables and Charts for WordPress |
| 3 |
* Plugin URI: https://themeisle.com/plugins/visualizer-charts-and-graphs/ |
| 4 |
* Author: Themeisle |
| 5 |
* |
| 6 |
* @package Visualizer |
| 7 |
*/ |
| 8 |
/* jshint unused:false */ |
| 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 |
const emailRegex = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; |
| 20 |
|
| 21 |
var showNewsletterError = function ($input, message) { |
| 22 |
var $wrap = $input.closest('.vz-option-input'); |
| 23 |
$wrap.next('.vz-field-error').remove(); |
| 24 |
if (message) { |
| 25 |
$wrap.after('<span class="vz-field-error">' + message + '</span>'); |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
/** |
| 30 |
* Redirect to draft page after subscribe (optional). |
| 31 |
* |
| 32 |
* @param {Object} postData - Post data. |
| 33 |
*/ |
| 34 |
const goToDraftPage = function ( postData = {} ) { |
| 35 |
postData ??= {}; |
| 36 |
postData.action ??= 'visualizer_wizard_step_process'; |
| 37 |
postData.security ??= visualizerSetupWizardData.ajax.security; |
| 38 |
postData.step ??= 'step_subscribe'; |
| 39 |
|
| 40 |
// Subscribe the user using the email if provided. Redirect to the draft page. |
| 41 |
var $currentStep = $('#step-3'); |
| 42 |
|
| 43 |
$.post(visualizerSetupWizardData.ajax.url, postData, function (res) { |
| 44 |
|
| 45 |
// Toggle the redirect popup. |
| 46 |
$('.redirect-popup').find('h3.popup-title').html(res.message); |
| 47 |
$('.redirect-popup').show(); |
| 48 |
|
| 49 |
if (1 === res.status) { |
| 50 |
setTimeout(function () { |
| 51 |
window.location.href = res.redirect_to; |
| 52 |
}, 5000); |
| 53 |
} else { |
| 54 |
$('.redirect-popup').hide(); |
| 55 |
} |
| 56 |
$currentStep.find('.spinner').removeClass('is-active'); |
| 57 |
}).fail(function () { |
| 58 |
$('.redirect-popup').hide(); |
| 59 |
$currentStep.find('.spinner').removeClass('is-active'); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
var runImport = function () { |
| 64 |
var chartType = $(".vz-radio-btn:checked").val(); |
| 65 |
var percentBarWidth = 0; |
| 66 |
var loaderDelay; |
| 67 |
var $step = $('#step-1'); |
| 68 |
var $status = $step.find('.vz-import-status'); |
| 69 |
var $progress = $status.find('.vz-progress-bar'); |
| 70 |
var $message = $status.find('[data-import_message]'); |
| 71 |
var $error = $status.find('.vz-error-notice'); |
| 72 |
var $spinner = $step.find('.spinner'); |
| 73 |
var $button = $step.find('[data-step_number="1"]'); |
| 74 |
|
| 75 |
$status.show(); |
| 76 |
$progress.css({ width: '0' }); |
| 77 |
if ($message.length) { |
| 78 |
$message |
| 79 |
.text($message.data('import_initial')) |
| 80 |
.removeClass('import_message'); |
| 81 |
} |
| 82 |
$error.addClass('hidden').empty(); |
| 83 |
$spinner.addClass('is-active'); |
| 84 |
$button.addClass('disabled'); |
| 85 |
|
| 86 |
loaderDelay = setInterval(function () { |
| 87 |
percentBarWidth = Math.min(percentBarWidth + 4, 90); |
| 88 |
$progress.css({ width: percentBarWidth + '%' }); |
| 89 |
}, 120); |
| 90 |
|
| 91 |
$.ajax({ |
| 92 |
type: 'POST', |
| 93 |
url: visualizerSetupWizardData.ajax.url, |
| 94 |
dataType: 'json', |
| 95 |
data: { |
| 96 |
action: 'visualizer_wizard_step_process', |
| 97 |
security: visualizerSetupWizardData.ajax.security, |
| 98 |
chart_type: chartType, |
| 99 |
step: 'step_2', |
| 100 |
}, |
| 101 |
success: function (data) { |
| 102 |
clearInterval(loaderDelay); |
| 103 |
$progress.css({ width: '100%' }); |
| 104 |
|
| 105 |
if (1 === data.success) { |
| 106 |
if (data.chart_id) { |
| 107 |
var $shortcode = $('#basic_shortcode'); |
| 108 |
if ($shortcode.length) { |
| 109 |
$shortcode.val( |
| 110 |
$shortcode.val().replace('{{chart_id}}', data.chart_id) |
| 111 |
); |
| 112 |
} |
| 113 |
} |
| 114 |
if ($message.length) { |
| 115 |
$message |
| 116 |
.text($message.data('import_message')) |
| 117 |
.addClass('import_message'); |
| 118 |
} |
| 119 |
|
| 120 |
setTimeout(function () { |
| 121 |
$spinner.removeClass('is-active'); |
| 122 |
$('#smartwizard').smartWizard('next'); |
| 123 |
}, 200); |
| 124 |
} else if (2 === data.status && '' !== data.message) { |
| 125 |
$error.html('<p>' + data.message + '</p>').removeClass('hidden'); |
| 126 |
$spinner.removeClass('is-active'); |
| 127 |
$button.removeClass('disabled'); |
| 128 |
} else { |
| 129 |
$spinner.removeClass('is-active'); |
| 130 |
$button.removeClass('disabled'); |
| 131 |
$('#smartwizard').smartWizard('reset'); |
| 132 |
} |
| 133 |
}, |
| 134 |
error: function () { |
| 135 |
clearInterval(loaderDelay); |
| 136 |
$progress.css({ width: '0' }); |
| 137 |
$spinner.removeClass('is-active'); |
| 138 |
$button.removeClass('disabled'); |
| 139 |
} |
| 140 |
}); |
| 141 |
}; |
| 142 |
|
| 143 |
var collectInstallSlugs = function () { |
| 144 |
var slugs = []; |
| 145 |
var $optimole = $('#enable_performance'); |
| 146 |
if ($optimole.length && $optimole.is(':checked') && !$optimole.is(':disabled')) { |
| 147 |
slugs.push('optimole-wp'); |
| 148 |
} |
| 149 |
if ($('#enable_otter_blocks').is(':checked') && !$('#enable_otter_blocks').is(':disabled')) { |
| 150 |
slugs.push('otter-blocks'); |
| 151 |
} |
| 152 |
if ($('#enable_page_cache').is(':checked') && !$('#enable_page_cache').is(':disabled')) { |
| 153 |
slugs.push('wp-cloudflare-page-cache'); |
| 154 |
} |
| 155 |
return slugs; |
| 156 |
}; |
| 157 |
|
| 158 |
var setInstallStatus = function (slug, statusClass) { |
| 159 |
var $status = $('[data-install-status="' + slug + '"]'); |
| 160 |
$status.removeClass('is-installing is-done is-error').addClass(statusClass); |
| 161 |
}; |
| 162 |
|
| 163 |
var installPluginsSequentially = function (slugs, onDone) { |
| 164 |
if (!slugs.length) { |
| 165 |
onDone(true); |
| 166 |
return; |
| 167 |
} |
| 168 |
var slug = slugs.shift(); |
| 169 |
setInstallStatus(slug, 'is-installing'); |
| 170 |
$.post( |
| 171 |
visualizerSetupWizardData.ajax.url, |
| 172 |
{ |
| 173 |
action: 'visualizer_wizard_step_process', |
| 174 |
security: visualizerSetupWizardData.ajax.security, |
| 175 |
slug: slug, |
| 176 |
step: 'step_4', |
| 177 |
}, |
| 178 |
function (response) { |
| 179 |
if (1 === response.status) { |
| 180 |
setInstallStatus(slug, 'is-done'); |
| 181 |
installPluginsSequentially(slugs, onDone); |
| 182 |
} else { |
| 183 |
setInstallStatus(slug, 'is-error'); |
| 184 |
onDone(false, response.message); |
| 185 |
} |
| 186 |
} |
| 187 |
).fail(function () { |
| 188 |
setInstallStatus(slug, 'is-error'); |
| 189 |
onDone(false, ''); |
| 190 |
}); |
| 191 |
}; |
| 192 |
$("#smartwizard").smartWizard({ |
| 193 |
transition: { |
| 194 |
animation: "fade", // Animation effect on navigation, none|fade|slideHorizontal|slideVertical|slideSwing|css(Animation CSS class also need to specify) |
| 195 |
speed: "400", // Animation speed. Not used if animation is 'css' |
| 196 |
}, |
| 197 |
lang: { |
| 198 |
// Language variables for button |
| 199 |
next: visualizerSetupWizardData.nextButtonText, |
| 200 |
previous: visualizerSetupWizardData.backButtonText, |
| 201 |
}, |
| 202 |
keyboard: { |
| 203 |
keyNavigation: false, // Enable/Disable keyboard navigation(left and right keys are used if enabled) |
| 204 |
}, |
| 205 |
anchor: { |
| 206 |
enableNavigation: false, // Enable/Disable anchor navigation |
| 207 |
enableNavigationAlways: false, // Activates all anchors clickable always |
| 208 |
}, |
| 209 |
style: { |
| 210 |
// CSS Class settings |
| 211 |
btnCss: "", |
| 212 |
btnNextCss: "btn-primary next-btn", |
| 213 |
btnPrevCss: "btn-light", |
| 214 |
} |
| 215 |
}); |
| 216 |
|
| 217 |
// click to open accordion. |
| 218 |
$(".vz-accordion .vz-accordion-item .vz-accordion-item__button").on( |
| 219 |
"click", |
| 220 |
function () { |
| 221 |
var current_item = $(this).parents(); |
| 222 |
$(".vz-accordion .vz-accordion-item .vz-accordion-item__content").each( |
| 223 |
function (i, el) { |
| 224 |
if ($(el).parent().is(current_item)) { |
| 225 |
$(el).prev().toggleClass("is-active"); |
| 226 |
$(el).slideToggle(); |
| 227 |
$(this).toggleClass("is-active"); |
| 228 |
} else { |
| 229 |
$(el).prev().removeClass("is-active"); |
| 230 |
$(el).slideUp(); |
| 231 |
$(this).removeClass("is-active"); |
| 232 |
} |
| 233 |
} |
| 234 |
); |
| 235 |
} |
| 236 |
); |
| 237 |
|
| 238 |
// Click to next step. |
| 239 |
$(document).on( |
| 240 |
"click", |
| 241 |
".btn-primary:not(.next-btn,.vz-create-page,.vz-subscribe)", |
| 242 |
function (e) { |
| 243 |
var stepNumber = $(this).data("step_number"); |
| 244 |
|
| 245 |
switch (stepNumber) { |
| 246 |
case 1: |
| 247 |
if ($(".vz-radio-btn").is(":checked")) { |
| 248 |
runImport(); |
| 249 |
} |
| 250 |
break; |
| 251 |
case 2: |
| 252 |
if ( isLivePreview ) { |
| 253 |
$('#smartwizard').smartWizard('next'); |
| 254 |
} else { |
| 255 |
var urlParams = new URLSearchParams(window.location.search); |
| 256 |
urlParams.set('preview_chart', Date.now()); |
| 257 |
window.location.hash = "#step-2"; |
| 258 |
window.location.search = urlParams; |
| 259 |
} |
| 260 |
break; |
| 261 |
default: |
| 262 |
e.preventDefault(); |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
); |
| 267 |
|
| 268 |
// Create draft page. |
| 269 |
$(document).on("click", ".vz-create-page", function (e) { |
| 270 |
var _this = $(this); |
| 271 |
_this.next(".spinner").addClass("is-active"); |
| 272 |
$.post( |
| 273 |
visualizerSetupWizardData.ajax.url, |
| 274 |
{ |
| 275 |
action: "visualizer_wizard_step_process", |
| 276 |
security: visualizerSetupWizardData.ajax.security, |
| 277 |
step: "create_draft_page", |
| 278 |
basic_shortcode: $("#basic_shortcode").val(), |
| 279 |
add_basic_shortcode: $("#insert_shortcode").is(":checked"), |
| 280 |
}, |
| 281 |
function (res) { |
| 282 |
if (res.status > 0) { |
| 283 |
if ( isLivePreview ) { |
| 284 |
goToDraftPage(); |
| 285 |
} else { |
| 286 |
$("#smartwizard").smartWizard("next"); |
| 287 |
} |
| 288 |
} |
| 289 |
_this.next(".spinner").removeClass("is-active"); |
| 290 |
} |
| 291 |
).fail(function () { |
| 292 |
_this.next(".spinner").removeClass("is-active"); |
| 293 |
}); |
| 294 |
e.preventDefault(); |
| 295 |
}); |
| 296 |
|
| 297 |
// Enable performance feature. |
| 298 |
// Step: 4 Skip and subscribe process. |
| 299 |
$(document).on( 'click', '.vz-subscribe', function (e) { |
| 300 |
var $btn = $(this); |
| 301 |
|
| 302 |
// Prevent double-clicks while the async flow is running. |
| 303 |
if ( $btn.prop('disabled') ) { |
| 304 |
e.preventDefault(); |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
var withSubscribe = $btn.data("vz_subscribe"); |
| 309 |
var postData = { |
| 310 |
action: "visualizer_wizard_step_process", |
| 311 |
security: visualizerSetupWizardData.ajax.security, |
| 312 |
step: "step_subscribe", |
| 313 |
}; |
| 314 |
var emailElement = $("#vz_subscribe_email"); |
| 315 |
// Remove error message. |
| 316 |
showNewsletterError(emailElement, ''); |
| 317 |
var newsletterEnabled = $("#enable_newsletter").is(":checked"); |
| 318 |
|
| 319 |
if (withSubscribe && newsletterEnabled) { |
| 320 |
var subscribeEmail = emailElement.val().trim(); |
| 321 |
var errorMessage = ""; |
| 322 |
|
| 323 |
if ("" === subscribeEmail) { |
| 324 |
errorMessage = visualizerSetupWizardData.errorMessages.requiredEmail; |
| 325 |
} else if (!emailRegex.test(subscribeEmail)) { |
| 326 |
errorMessage = visualizerSetupWizardData.errorMessages.invalidEmail; |
| 327 |
} |
| 328 |
if ("" !== errorMessage) { |
| 329 |
showNewsletterError(emailElement, errorMessage); |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
postData.email = subscribeEmail; |
| 334 |
postData.with_subscribe = withSubscribe; |
| 335 |
} else { |
| 336 |
postData.with_subscribe = false; |
| 337 |
} |
| 338 |
|
| 339 |
$btn.prop('disabled', true); |
| 340 |
var $step = $('#step-3'); |
| 341 |
$step.find(".spinner").addClass("is-active"); |
| 342 |
var $error = $step.find('.vz-error-notice').first(); |
| 343 |
$error.addClass('hidden').empty(); |
| 344 |
|
| 345 |
var slugs = collectInstallSlugs(); |
| 346 |
installPluginsSequentially(slugs.slice(), function (ok, message) { |
| 347 |
if (!ok) { |
| 348 |
if (message) { |
| 349 |
$error.html('<p>' + message + '</p>').removeClass('hidden'); |
| 350 |
} |
| 351 |
$step.find(".spinner").removeClass("is-active"); |
| 352 |
$btn.prop('disabled', false); |
| 353 |
return; |
| 354 |
} |
| 355 |
goToDraftPage(postData); |
| 356 |
}); |
| 357 |
e.preventDefault(); |
| 358 |
}); |
| 359 |
|
| 360 |
$(document).on('click', '.vz-change-email', function () { |
| 361 |
var $card = $(this).closest('.vz-option-card--newsletter'); |
| 362 |
var $inputWrap = $card.find('.vz-option-input'); |
| 363 |
$inputWrap.show(); |
| 364 |
$card.find('#vz_subscribe_email').focus(); |
| 365 |
validateNewsletterEmail(); |
| 366 |
}); |
| 367 |
|
| 368 |
var finalizeNewsletterEmail = function ($card) { |
| 369 |
var $input = $card.find('#vz_subscribe_email'); |
| 370 |
var $text = $card.find('.vz-email-text'); |
| 371 |
var email = $input.val(); |
| 372 |
if (email) { |
| 373 |
$text.text(email); |
| 374 |
} |
| 375 |
$card.find('.vz-option-input').hide(); |
| 376 |
}; |
| 377 |
|
| 378 |
$(document).on('click', '.vz-save-email', function () { |
| 379 |
var $card = $(this).closest('.vz-option-card--newsletter'); |
| 380 |
if (!validateNewsletterEmail()) { |
| 381 |
return; |
| 382 |
} |
| 383 |
finalizeNewsletterEmail($card); |
| 384 |
}); |
| 385 |
|
| 386 |
var validateNewsletterEmail = function () { |
| 387 |
var $input = $("#vz_subscribe_email"); |
| 388 |
if (!$input.length) { |
| 389 |
return true; |
| 390 |
} |
| 391 |
var $card = $input.closest('.vz-option-card--newsletter'); |
| 392 |
var $saveButton = $card.find('.vz-save-email'); |
| 393 |
var email = $input.val().trim(); |
| 394 |
showNewsletterError($input, ''); |
| 395 |
if (!email) { |
| 396 |
$saveButton.prop('disabled', false); |
| 397 |
return true; |
| 398 |
} |
| 399 |
if (!emailRegex.test(email)) { |
| 400 |
showNewsletterError($input, visualizerSetupWizardData.errorMessages.invalidEmail); |
| 401 |
$saveButton.prop('disabled', true); |
| 402 |
return false; |
| 403 |
} |
| 404 |
$saveButton.prop('disabled', false); |
| 405 |
return true; |
| 406 |
}; |
| 407 |
|
| 408 |
$(document).on('input blur', '#vz_subscribe_email', function () { |
| 409 |
validateNewsletterEmail(); |
| 410 |
}); |
| 411 |
|
| 412 |
// Click to copy. |
| 413 |
var clipboard = new ClipboardJS(".vz-copy-code-btn"); |
| 414 |
clipboard.on("success", function (e) { |
| 415 |
var inputElement = $(e.trigger).prev("input:text"); |
| 416 |
}); |
| 417 |
|
| 418 |
// Remove disabled class from get started button. |
| 419 |
$("#step-1").on("change", "input:radio", function () { |
| 420 |
$("#step-1").find('[data-step_number="1"]').removeClass("disabled"); |
| 421 |
}); |
| 422 |
if ( $('#step-1 .vz-radio-btn:checked').length ) { |
| 423 |
$('#step-1').find('[data-step_number="1"]').removeClass('disabled'); |
| 424 |
} |
| 425 |
|
| 426 |
// Change button text. |
| 427 |
$(document).on("change", "#insert_shortcode", function () { |
| 428 |
if ($(this).is(":checked")) { |
| 429 |
$(".vz-create-page").html( |
| 430 |
visualizerSetupWizardData.draftPageButtonText.firstButtonText + |
| 431 |
' <span class="dashicons dashicons-arrow-right-alt"></span>' |
| 432 |
); |
| 433 |
} else { |
| 434 |
$(".vz-create-page").html( |
| 435 |
visualizerSetupWizardData.draftPageButtonText.secondButtonText + |
| 436 |
' <span class="dashicons dashicons-arrow-right-alt"></span>' |
| 437 |
); |
| 438 |
} |
| 439 |
}); |
| 440 |
|
| 441 |
$(window).bind('pageshow', function() { |
| 442 |
if ( jQuery('.vz-chart-option input').is(':checked') ) { |
| 443 |
$('#step-1').find('button.disabled').removeClass('disabled'); |
| 444 |
} |
| 445 |
}); |
| 446 |
}); |
| 447 |
|