| 1 |
/** |
| 2 |
* AJAX Request Queue |
| 3 |
* |
| 4 |
* - add() |
| 5 |
* - remove() |
| 6 |
* - run() |
| 7 |
* - stop() |
| 8 |
* |
| 9 |
* @since x.x.x |
| 10 |
*/ |
| 11 |
|
| 12 |
const CartFlowsAjaxQueue = ( function () { |
| 13 |
let requests = []; |
| 14 |
|
| 15 |
return { |
| 16 |
/** |
| 17 |
* Add AJAX request |
| 18 |
* |
| 19 |
* @param {string} opt selected opt. |
| 20 |
* @since x.x.x |
| 21 |
*/ |
| 22 |
add( opt ) { |
| 23 |
requests.push( opt ); |
| 24 |
}, |
| 25 |
|
| 26 |
/** |
| 27 |
* Remove AJAX request |
| 28 |
* |
| 29 |
* @param {string} opt selected opt. |
| 30 |
* @since x.x.x |
| 31 |
*/ |
| 32 |
remove( opt ) { |
| 33 |
if ( jQuery.inArray( opt, requests ) > -1 ) { |
| 34 |
requests.splice( jQuery.inArray( opt, requests ), 1 ); |
| 35 |
} |
| 36 |
}, |
| 37 |
|
| 38 |
/** |
| 39 |
* Run / Process AJAX request |
| 40 |
* |
| 41 |
* @since x.x.x |
| 42 |
*/ |
| 43 |
run() { |
| 44 |
const self = this; |
| 45 |
let oriSuc; |
| 46 |
|
| 47 |
if ( requests.length ) { |
| 48 |
oriSuc = requests[ 0 ].complete; |
| 49 |
|
| 50 |
requests[ 0 ].complete = function () { |
| 51 |
if ( typeof oriSuc === 'function' ) { |
| 52 |
oriSuc(); |
| 53 |
} |
| 54 |
requests.shift(); |
| 55 |
self.run.apply( self, [] ); |
| 56 |
}; |
| 57 |
|
| 58 |
jQuery.ajax( requests[ 0 ] ); |
| 59 |
} else { |
| 60 |
self.tid = setTimeout( function () { |
| 61 |
self.run.apply( self, [] ); |
| 62 |
}, 1000 ); |
| 63 |
} |
| 64 |
}, |
| 65 |
|
| 66 |
/** |
| 67 |
* Stop AJAX request |
| 68 |
* |
| 69 |
* @since x.x.x |
| 70 |
*/ |
| 71 |
stop() { |
| 72 |
requests = []; |
| 73 |
clearTimeout( this.tid ); |
| 74 |
}, |
| 75 |
}; |
| 76 |
} )(); |
| 77 |
|
| 78 |
( function ( $ ) { |
| 79 |
const CartFlowsOnboarding = { |
| 80 |
remaining_install_plugins: 0, |
| 81 |
remaining_active_plugins: 0, |
| 82 |
|
| 83 |
init() { |
| 84 |
this._bind(); |
| 85 |
}, |
| 86 |
|
| 87 |
/** |
| 88 |
* Bind |
| 89 |
*/ |
| 90 |
_bind() { |
| 91 |
//Page builder installation & save option |
| 92 |
$( document ) |
| 93 |
.on( |
| 94 |
'click', |
| 95 |
'.wcf-start-setup', |
| 96 |
CartFlowsOnboarding._redirect_next_step |
| 97 |
) |
| 98 |
.on( |
| 99 |
'click', |
| 100 |
'.install-page-builder-plugins', |
| 101 |
CartFlowsOnboarding._install_page_builder_plugin |
| 102 |
) |
| 103 |
.on( |
| 104 |
'click', |
| 105 |
'.wcf-import-global-flow', |
| 106 |
CartFlowsOnboarding._import_store_checkout_template |
| 107 |
) |
| 108 |
.on( |
| 109 |
'click', |
| 110 |
'.install-required-plugins', |
| 111 |
CartFlowsOnboarding._install_required_plugins |
| 112 |
) |
| 113 |
.on( |
| 114 |
'wp-plugin-installing', |
| 115 |
CartFlowsOnboarding._pluginInstalling |
| 116 |
) |
| 117 |
.on( |
| 118 |
'wp-plugin-install-error', |
| 119 |
CartFlowsOnboarding._installError |
| 120 |
) |
| 121 |
.on( |
| 122 |
'wp-plugin-install-success', |
| 123 |
CartFlowsOnboarding._installSuccess |
| 124 |
); |
| 125 |
}, |
| 126 |
|
| 127 |
/** |
| 128 |
* Dispatch the event to trigger the step redirect. |
| 129 |
*/ |
| 130 |
_redirect_next_step() { |
| 131 |
// Redirect to Page builder step from home page. |
| 132 |
const redirect_page_builder_step_event = new Event( |
| 133 |
'wcf-redirect-page-builder-step' |
| 134 |
); |
| 135 |
document.dispatchEvent( redirect_page_builder_step_event ); |
| 136 |
}, |
| 137 |
|
| 138 |
/** |
| 139 |
* Install Now |
| 140 |
* |
| 141 |
* @param {Object} event event data. |
| 142 |
*/ |
| 143 |
_install_page_builder_plugin( event ) { |
| 144 |
event.preventDefault(); |
| 145 |
|
| 146 |
let plugin_slug = ''; |
| 147 |
|
| 148 |
document.dispatchEvent( |
| 149 |
new Event( 'wcf-page-builder-plugins-install-processing' ) |
| 150 |
); |
| 151 |
|
| 152 |
// Selected page builder |
| 153 |
const plugin_key = |
| 154 |
$( '#wcf-selected-page-builder' ).attr( |
| 155 |
'data-selected-pb' |
| 156 |
) || '', |
| 157 |
plugin_data = cartflows_onboarding.page_builders[ plugin_key ], |
| 158 |
is_installed = plugin_data.install, |
| 159 |
is_activeted = plugin_data.active, |
| 160 |
plugin_init = plugin_data.init; |
| 161 |
plugin_slug = plugin_data.slug; |
| 162 |
|
| 163 |
// Check plugin status first before saving the page builder option. |
| 164 |
if ( 'yes' === is_installed && 'no' === is_activeted ) { |
| 165 |
CartFlowsOnboarding._activatePlugin( |
| 166 |
plugin_init, |
| 167 |
plugin_slug, |
| 168 |
true |
| 169 |
); |
| 170 |
} else if ( 'no' === is_installed ) { |
| 171 |
CartFlowsOnboarding._installPlugin( plugin_slug ); |
| 172 |
} else { |
| 173 |
// Save page builder option and continue. |
| 174 |
save_page_builder_option( plugin_slug ); |
| 175 |
} |
| 176 |
}, |
| 177 |
|
| 178 |
_install_required_plugins( event ) { |
| 179 |
event.preventDefault(); |
| 180 |
|
| 181 |
// Fire an event to change the button state to processing. |
| 182 |
document.dispatchEvent( |
| 183 |
new Event( 'wcf-install-require-plugins-processing' ) |
| 184 |
); |
| 185 |
|
| 186 |
const required_plugins = $( 'form.wcf-install-plugin-form' ).find( |
| 187 |
'input[type=checkbox]' |
| 188 |
); |
| 189 |
|
| 190 |
$.each( required_plugins, function ( index, plugin ) { |
| 191 |
const is_checked = $( plugin ).is( ':checked' ), |
| 192 |
status = plugin.getAttribute( 'data-status' ); |
| 193 |
|
| 194 |
if ( is_checked ) { |
| 195 |
if ( 'not-installed' === status ) { |
| 196 |
CartFlowsOnboarding.remaining_install_plugins++; |
| 197 |
} |
| 198 |
if ( 'inactive' === status ) { |
| 199 |
CartFlowsOnboarding.remaining_active_plugins++; |
| 200 |
} |
| 201 |
} |
| 202 |
} ); |
| 203 |
|
| 204 |
// Have any plugin for install? |
| 205 |
if ( CartFlowsOnboarding.remaining_install_plugins ) { |
| 206 |
CartFlowsOnboarding._install_all_plugins(); |
| 207 |
} else if ( CartFlowsOnboarding.remaining_active_plugins ) { |
| 208 |
CartFlowsOnboarding._activate_all_plugins(); |
| 209 |
} else if ( |
| 210 |
! CartFlowsOnboarding.remaining_active_plugins && |
| 211 |
! CartFlowsOnboarding.remaining_install_plugins |
| 212 |
) { |
| 213 |
trigger_event(); |
| 214 |
} |
| 215 |
}, |
| 216 |
|
| 217 |
_installPlugin( plugin_slug ) { |
| 218 |
if ( |
| 219 |
wp.updates.shouldRequestFilesystemCredentials && |
| 220 |
! wp.updates.ajaxLocked |
| 221 |
) { |
| 222 |
wp.updates.requestFilesystemCredentials( event ); |
| 223 |
|
| 224 |
$document.on( 'credential-modal-cancel', function () { |
| 225 |
const $message = $( '.install-now.updating-message' ); |
| 226 |
|
| 227 |
$message |
| 228 |
.removeClass( 'updating-message' ) |
| 229 |
.text( wp.updates.l10n.installNow ); |
| 230 |
|
| 231 |
wp.a11y.speak( wp.updates.l10n.updateCancel, 'polite' ); |
| 232 |
} ); |
| 233 |
} |
| 234 |
|
| 235 |
wp.updates.queue.push( { |
| 236 |
action: 'install-plugin', // Required action. |
| 237 |
data: { |
| 238 |
slug: plugin_slug, |
| 239 |
}, |
| 240 |
} ); |
| 241 |
|
| 242 |
// Required to set queue. |
| 243 |
wp.updates.queueChecker(); |
| 244 |
}, |
| 245 |
|
| 246 |
_activatePlugin( plugin_init, plugin_slug ) { |
| 247 |
const page_builder_slugs = [ |
| 248 |
'elementor', |
| 249 |
'beaver-builder-lite-version', |
| 250 |
'divi', |
| 251 |
'ultimate-addons-for-gutenberg', |
| 252 |
]; |
| 253 |
|
| 254 |
$.ajax( { |
| 255 |
url: ajaxurl, |
| 256 |
method: 'POST', |
| 257 |
data: { |
| 258 |
action: 'cartflows_wizard_activate_plugin', |
| 259 |
plugin_slug, |
| 260 |
plugin_init, |
| 261 |
security: cartflows_onboarding.wizard_activate_plugin_nonce, |
| 262 |
}, |
| 263 |
} ) |
| 264 |
.done( function ( response ) { |
| 265 |
if ( response.success ) { |
| 266 |
if ( |
| 267 |
jQuery.inArray( plugin_slug, page_builder_slugs ) > |
| 268 |
-1 |
| 269 |
) { |
| 270 |
save_page_builder_option( plugin_slug ); |
| 271 |
} |
| 272 |
} |
| 273 |
} ) |
| 274 |
.fail( function () {} ); |
| 275 |
}, |
| 276 |
|
| 277 |
/** |
| 278 |
* Import the store checkout template. |
| 279 |
* |
| 280 |
* @param {Object} event |
| 281 |
*/ |
| 282 |
_import_store_checkout_template( event ) { |
| 283 |
event.preventDefault(); |
| 284 |
// Selected Template's ID. |
| 285 |
const store_template_flow = |
| 286 |
$( '#wcf-selected-store-checkout-template' ).attr( |
| 287 |
'data-selected-flow-info' |
| 288 |
) || '', |
| 289 |
primary_color = $( 'input[name=primary_color]' ).val(), |
| 290 |
selected_site_logo = $( '.wcf-selected-image' ).data( |
| 291 |
'logo-data' |
| 292 |
); |
| 293 |
|
| 294 |
// Send the event to react for processing when clicked on the footer button. |
| 295 |
document.dispatchEvent( |
| 296 |
new Event( 'wcf-store-checkout-import-text-processing' ) |
| 297 |
); |
| 298 |
|
| 299 |
const has_error = new CustomEvent( |
| 300 |
'wcf-store-checkout-import-error', |
| 301 |
{ |
| 302 |
detail: { |
| 303 |
is_error: false, |
| 304 |
errorMsg: '', |
| 305 |
callToAction: '', |
| 306 |
}, |
| 307 |
} |
| 308 |
); |
| 309 |
|
| 310 |
//Import the requested template via ajax call. |
| 311 |
$.ajax( { |
| 312 |
url: ajaxurl, |
| 313 |
method: 'POST', |
| 314 |
data: { |
| 315 |
action: 'cartflows_import_store_checkout', |
| 316 |
security: cartflows_onboarding.import_store_checkout_nonce, |
| 317 |
flow: store_template_flow, |
| 318 |
primary_color, |
| 319 |
site_logo: selected_site_logo, |
| 320 |
}, |
| 321 |
} ) |
| 322 |
.success( function ( res ) { |
| 323 |
if ( res.success && true === res.data.success ) { |
| 324 |
document.dispatchEvent( |
| 325 |
new Event( 'wcf-store-checkout-import-success' ) |
| 326 |
); |
| 327 |
} else { |
| 328 |
has_error.detail.is_error = true; |
| 329 |
has_error.detail.errorMsg = res.data.message; |
| 330 |
has_error.detail.callToAction = res.data.call_to_action; |
| 331 |
|
| 332 |
document.dispatchEvent( has_error ); |
| 333 |
} |
| 334 |
} ) |
| 335 |
.fail( function () { |
| 336 |
has_error.detail.is_error = true; |
| 337 |
has_error.detail.errorMsg = |
| 338 |
cartflows_onboarding.template_import_errors.api.title; |
| 339 |
has_error.detail.callToAction = |
| 340 |
cartflows_onboarding.template_import_errors.api.msg; |
| 341 |
document.dispatchEvent( has_error ); |
| 342 |
} ); |
| 343 |
}, |
| 344 |
|
| 345 |
/** |
| 346 |
* Installing Plugin |
| 347 |
* |
| 348 |
* @param {Object} event event data. |
| 349 |
*/ |
| 350 |
_pluginInstalling( event ) { |
| 351 |
event.preventDefault(); |
| 352 |
}, |
| 353 |
|
| 354 |
/** |
| 355 |
* Install Error |
| 356 |
* |
| 357 |
* @param {Object} event event data. |
| 358 |
*/ |
| 359 |
_installError( event ) { |
| 360 |
event.preventDefault(); |
| 361 |
|
| 362 |
const redirect_link = $( '.wcf-redirect-link' ).attr( 'value' ); |
| 363 |
if ( '' !== redirect_link ) { |
| 364 |
trigger_event(); |
| 365 |
} |
| 366 |
}, |
| 367 |
|
| 368 |
/** |
| 369 |
* Install Success |
| 370 |
* |
| 371 |
* @param {Object} event event data. |
| 372 |
* @param {Array} args args data. |
| 373 |
*/ |
| 374 |
_installSuccess( event, args ) { |
| 375 |
event.preventDefault(); |
| 376 |
const plugin_init = args.slug + '/' + args.slug + '.php'; |
| 377 |
const plugin_slug = args.slug; |
| 378 |
|
| 379 |
// WordPress adds "Activate" button after waiting for 1000ms. So we will run our activation after that. |
| 380 |
setTimeout( function () { |
| 381 |
CartFlowsOnboarding._activatePlugin( plugin_init, plugin_slug ); |
| 382 |
|
| 383 |
if ( CartFlowsOnboarding.remaining_install_plugins > 0 ) { |
| 384 |
CartFlowsOnboarding.remaining_install_plugins--; |
| 385 |
} |
| 386 |
|
| 387 |
if ( ! CartFlowsOnboarding.remaining_install_plugins ) { |
| 388 |
CartFlowsOnboarding._activate_all_plugins(); |
| 389 |
} |
| 390 |
}, 1500 ); |
| 391 |
}, |
| 392 |
|
| 393 |
_install_all_plugins() { |
| 394 |
const required_plugins = $( 'form.wcf-install-plugin-form' ).find( |
| 395 |
'input[type=checkbox]' |
| 396 |
); |
| 397 |
|
| 398 |
$.each( required_plugins, function ( index, plugin ) { |
| 399 |
const pluginSlug = plugin.getAttribute( 'data-slug' ), |
| 400 |
status = plugin.getAttribute( 'data-status' ), |
| 401 |
is_checked = $( plugin ).is( ':checked' ); |
| 402 |
|
| 403 |
if ( is_checked && 'not-installed' === status ) { |
| 404 |
CartFlowsOnboarding._installPlugin( pluginSlug ); |
| 405 |
} |
| 406 |
} ); |
| 407 |
}, |
| 408 |
|
| 409 |
_activate_all_plugins() { |
| 410 |
if ( |
| 411 |
! CartFlowsOnboarding.remaining_active_plugins && |
| 412 |
! CartFlowsOnboarding.remaining_install_plugins |
| 413 |
) { |
| 414 |
trigger_event(); |
| 415 |
} |
| 416 |
|
| 417 |
const required_plugins = $( 'form.wcf-install-plugin-form' ).find( |
| 418 |
'input[type=checkbox]' |
| 419 |
); |
| 420 |
|
| 421 |
// Activate All Plugins. |
| 422 |
CartFlowsAjaxQueue.stop(); |
| 423 |
CartFlowsAjaxQueue.run(); |
| 424 |
|
| 425 |
$.each( required_plugins, function ( index, plugin ) { |
| 426 |
const pluginSlug = plugin.getAttribute( 'data-slug' ), |
| 427 |
status = plugin.getAttribute( 'data-status' ), |
| 428 |
is_checked = $( plugin ).is( ':checked' ), |
| 429 |
plugin_init = pluginSlug + '/' + pluginSlug + '.php'; |
| 430 |
|
| 431 |
if ( is_checked && 'inactive' === status ) { |
| 432 |
CartFlowsAjaxQueue.add( { |
| 433 |
url: ajaxurl, |
| 434 |
type: 'POST', |
| 435 |
data: { |
| 436 |
action: 'cartflows_wizard_activate_plugin', |
| 437 |
plugin_slug: pluginSlug, |
| 438 |
plugin_init, |
| 439 |
security: |
| 440 |
cartflows_onboarding.wizard_activate_plugin_nonce, |
| 441 |
}, |
| 442 |
success() { |
| 443 |
CartFlowsOnboarding.remaining_active_plugins--; |
| 444 |
|
| 445 |
if ( |
| 446 |
! CartFlowsOnboarding.remaining_active_plugins && |
| 447 |
! CartFlowsOnboarding.remaining_install_plugins |
| 448 |
) { |
| 449 |
trigger_event(); |
| 450 |
} |
| 451 |
}, |
| 452 |
} ); |
| 453 |
} |
| 454 |
} ); |
| 455 |
}, |
| 456 |
}; |
| 457 |
|
| 458 |
function trigger_event() { |
| 459 |
const custom_event = new Event( 'wcf-plugins-install-success' ); |
| 460 |
document.dispatchEvent( custom_event ); |
| 461 |
} |
| 462 |
|
| 463 |
function save_page_builder_option( plugin_slug ) { |
| 464 |
//Save page builder option in global settings. |
| 465 |
$.ajax( { |
| 466 |
url: ajaxurl, |
| 467 |
method: 'POST', |
| 468 |
data: { |
| 469 |
action: 'cartflows_page_builder_save_option', |
| 470 |
security: cartflows_onboarding.page_builder_save_option_nonce, |
| 471 |
page_builder: plugin_slug, |
| 472 |
}, |
| 473 |
} ) |
| 474 |
.success( function () { |
| 475 |
document.dispatchEvent( |
| 476 |
new Event( 'wcf-page-builder-plugins-install-success' ) |
| 477 |
); |
| 478 |
} ) |
| 479 |
.fail( function () {} ); |
| 480 |
} |
| 481 |
|
| 482 |
$( function () { |
| 483 |
CartFlowsOnboarding.init(); |
| 484 |
} ); |
| 485 |
} )( jQuery ); |
| 486 |
|