Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
/* global wpforms_challenge_admin, ajaxurl, WPFormsBuilder */
|
||||
/**
|
||||
* WPForms Challenge Admin function.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @since 1.6.2 Challenge v2
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var WPFormsChallenge = window.WPFormsChallenge || {};
|
||||
|
||||
WPFormsChallenge.admin = window.WPFormsChallenge.admin || ( function( document, window, $ ) {
|
||||
|
||||
/**
|
||||
* Public functions and properties.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @type {object}
|
||||
*/
|
||||
var app = {
|
||||
|
||||
l10n: wpforms_challenge_admin,
|
||||
|
||||
/**
|
||||
* Start the engine.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
init: function() {
|
||||
|
||||
$( app.ready );
|
||||
},
|
||||
|
||||
/**
|
||||
* Document ready.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
ready: function() {
|
||||
|
||||
app.events();
|
||||
},
|
||||
|
||||
/**
|
||||
* Register JS events.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
events: function() {
|
||||
|
||||
$( '.wpforms-challenge-list-block' )
|
||||
.on( 'click', '.challenge-skip', app.skipChallenge )
|
||||
.on( 'click', '.challenge-cancel', app.cancelChallenge )
|
||||
.on( 'click', '.toggle-list', app.toggleList );
|
||||
},
|
||||
|
||||
/**
|
||||
* Toggle list icon click.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {object} e Event object.
|
||||
*/
|
||||
toggleList: function( e ) {
|
||||
|
||||
var $icon = $( e.target ),
|
||||
$listBlock = $( '.wpforms-challenge-list-block' );
|
||||
|
||||
if ( ! $listBlock.length || ! $icon.length ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $listBlock.hasClass( 'closed' ) ) {
|
||||
wpforms_challenge_admin.option.window_closed = '0';
|
||||
$listBlock.removeClass( 'closed' );
|
||||
|
||||
setTimeout( function() {
|
||||
$listBlock.removeClass( 'transition-back' );
|
||||
}, 600 );
|
||||
} else {
|
||||
wpforms_challenge_admin.option.window_closed = '1';
|
||||
$listBlock.addClass( 'closed' );
|
||||
|
||||
// Add `transition-back` class when the forward transition is completed.
|
||||
// It is needed to properly implement transitions order for some elements.
|
||||
setTimeout( function() {
|
||||
$listBlock.addClass( 'transition-back' );
|
||||
}, 600 );
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Skip the Challenge without starting it.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
skipChallenge: function() {
|
||||
|
||||
var optionData = {
|
||||
status : 'skipped',
|
||||
seconds_spent: 0,
|
||||
seconds_left : app.l10n.minutes_left * 60,
|
||||
};
|
||||
|
||||
$( '.wpforms-challenge' ).remove();
|
||||
|
||||
// In the Form Builder, we must also make the Embed button clickable.
|
||||
$( '#wpforms-embed' ).removeClass( 'wpforms-disabled' );
|
||||
|
||||
app.saveChallengeOption( optionData );
|
||||
},
|
||||
|
||||
/**
|
||||
* Cancel Challenge after starting it.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*/
|
||||
cancelChallenge: function() {
|
||||
|
||||
var core = WPFormsChallenge.core;
|
||||
|
||||
core.timer.pause();
|
||||
|
||||
/* eslint-disable camelcase */
|
||||
var optionData = {
|
||||
status : 'canceled',
|
||||
seconds_spent: core.timer.getSecondsSpent(),
|
||||
seconds_left : core.timer.getSecondsLeft(),
|
||||
feedback_sent: false,
|
||||
};
|
||||
/* eslint-enable */
|
||||
|
||||
core.removeChallengeUI();
|
||||
core.clearLocalStorage();
|
||||
|
||||
if ( typeof WPFormsBuilder !== 'undefined' ) {
|
||||
WPFormsChallenge.admin.saveChallengeOption( optionData )
|
||||
.done( function() { // Save the form before removing scripts if we're in a WPForms Builder.
|
||||
if ( localStorage.getItem( 'wpformsChallengeStep' ) !== null ) {
|
||||
WPFormsBuilder.formSave( false );
|
||||
}
|
||||
} ).done( // Remove scripts related to challenge.
|
||||
$( '#wpforms-challenge-admin-js, #wpforms-challenge-core-js, #wpforms-challenge-admin-js-extra, #wpforms-challenge-builder-js' )
|
||||
.remove()
|
||||
);
|
||||
} else {
|
||||
WPFormsChallenge.admin.saveChallengeOption( optionData )
|
||||
.done( app.triggerPageSave ); // Assume we're on form embed page.
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Set Challenge parameter(s) to Challenge option.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {object} optionData Query using option schema keys.
|
||||
*
|
||||
* @returns {promise} jQuery.post() promise interface.
|
||||
*/
|
||||
saveChallengeOption: function( optionData ) {
|
||||
|
||||
var data = {
|
||||
action : 'wpforms_challenge_save_option',
|
||||
option_data: optionData,
|
||||
_wpnonce : app.l10n.nonce,
|
||||
};
|
||||
|
||||
// Save window closed (collapsed) state as well.
|
||||
data.option_data.window_closed = wpforms_challenge_admin.option.window_closed;
|
||||
|
||||
$.extend( wpforms_challenge_admin.option, optionData );
|
||||
|
||||
return $.post( ajaxurl, data, function( response ) {
|
||||
if ( ! response.success ) {
|
||||
console.error( 'Error saving WPForms Challenge option.' );
|
||||
}
|
||||
} );
|
||||
},
|
||||
};
|
||||
|
||||
// Provide access to public functions/properties.
|
||||
return app;
|
||||
|
||||
}( document, window, jQuery ) );
|
||||
|
||||
WPFormsChallenge.admin.init();
|
1
wp-content/plugins/wpforms-lite/assets/js/components/admin/challenge/challenge-admin.min.js
vendored
Normal file
1
wp-content/plugins/wpforms-lite/assets/js/components/admin/challenge/challenge-admin.min.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
"use strict";var WPFormsChallenge=window.WPFormsChallenge||{};WPFormsChallenge.admin=window.WPFormsChallenge.admin||function(o){var l={l10n:wpforms_challenge_admin,init:function(){o(l.ready)},ready:function(){l.events()},events:function(){o(".wpforms-challenge-list-block").on("click",".challenge-skip",l.skipChallenge).on("click",".challenge-cancel",l.cancelChallenge).on("click",".toggle-list",l.toggleList)},toggleList:function(e){var e=o(e.target),n=o(".wpforms-challenge-list-block");n.length&&e.length&&(n.hasClass("closed")?(wpforms_challenge_admin.option.window_closed="0",n.removeClass("closed"),setTimeout(function(){n.removeClass("transition-back")},600)):(wpforms_challenge_admin.option.window_closed="1",n.addClass("closed"),setTimeout(function(){n.addClass("transition-back")},600)))},skipChallenge:function(){var e={status:"skipped",seconds_spent:0,seconds_left:60*l.l10n.minutes_left};o(".wpforms-challenge").remove(),o("#wpforms-embed").removeClass("wpforms-disabled"),l.saveChallengeOption(e)},cancelChallenge:function(){var e=WPFormsChallenge.core,n=(e.timer.pause(),{status:"canceled",seconds_spent:e.timer.getSecondsSpent(),seconds_left:e.timer.getSecondsLeft(),feedback_sent:!1});e.removeChallengeUI(),e.clearLocalStorage(),"undefined"!=typeof WPFormsBuilder?WPFormsChallenge.admin.saveChallengeOption(n).done(function(){null!==localStorage.getItem("wpformsChallengeStep")&&WPFormsBuilder.formSave(!1)}).done(o("#wpforms-challenge-admin-js, #wpforms-challenge-core-js, #wpforms-challenge-admin-js-extra, #wpforms-challenge-builder-js").remove()):WPFormsChallenge.admin.saveChallengeOption(n).done(l.triggerPageSave)},saveChallengeOption:function(e){var n={action:"wpforms_challenge_save_option",option_data:e,_wpnonce:l.l10n.nonce};return n.option_data.window_closed=wpforms_challenge_admin.option.window_closed,o.extend(wpforms_challenge_admin.option,e),o.post(ajaxurl,n,function(e){e.success||console.error("Error saving WPForms Challenge option.")})}};return l}((document,window,jQuery)),WPFormsChallenge.admin.init();
|
@@ -0,0 +1,280 @@
|
||||
/* global WPForms, WPFormsBuilder, wpforms_challenge_admin, WPFormsFormEmbedWizard */
|
||||
/**
|
||||
* WPForms Challenge function.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @since 1.6.2 Challenge v2
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var WPFormsChallenge = window.WPFormsChallenge || {};
|
||||
|
||||
WPFormsChallenge.builder = window.WPFormsChallenge.builder || ( function( document, window, $ ) {
|
||||
|
||||
/**
|
||||
* Public functions and properties.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @type {object}
|
||||
*/
|
||||
var app = {
|
||||
|
||||
/**
|
||||
* Start the engine.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
init: function() {
|
||||
|
||||
$( app.ready );
|
||||
$( window ).on( 'load', function() {
|
||||
|
||||
// in case of jQuery 3.+ we need to wait for an `ready` event first.
|
||||
if ( typeof $.ready.then === 'function' ) {
|
||||
$.ready.then( app.load );
|
||||
} else {
|
||||
app.load();
|
||||
}
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Document ready.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
ready: function() {
|
||||
|
||||
app.setup();
|
||||
app.events();
|
||||
},
|
||||
|
||||
/**
|
||||
* Window load.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
load: function() {
|
||||
|
||||
if ( [ 'started', 'paused' ].indexOf( wpforms_challenge_admin.option.status ) > -1 ) {
|
||||
WPFormsChallenge.core.updateTooltipUI();
|
||||
}
|
||||
|
||||
$( '.wpforms-challenge' ).show();
|
||||
},
|
||||
|
||||
/**
|
||||
* Initial setup.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
setup: function() {
|
||||
|
||||
if ( wpforms_challenge_admin.option.status === 'inited' ) {
|
||||
WPFormsChallenge.core.clearLocalStorage();
|
||||
app.showWelcomePopup();
|
||||
}
|
||||
|
||||
$( '#wpforms-embed' ).addClass( 'wpforms-disabled' );
|
||||
|
||||
var tooltipAnchors = [
|
||||
'#wpforms-setup-name',
|
||||
'.wpforms-setup-title .wpforms-setup-title-after',
|
||||
'#add-fields a i',
|
||||
'#wpforms-builder-settings-notifications-title',
|
||||
];
|
||||
|
||||
$.each( tooltipAnchors, function( i, anchor ) {
|
||||
|
||||
WPFormsChallenge.core.initTooltips( i + 1, anchor, null );
|
||||
} );
|
||||
|
||||
$( document ).on( 'wpformsWizardPopupClose', app.enableEmbed );
|
||||
},
|
||||
|
||||
/**
|
||||
* Register JS events.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
events: function() {
|
||||
|
||||
// Start the Challenge.
|
||||
$( '#wpforms-challenge-welcome-builder-popup' ).on( 'click', 'button', app.startChallenge );
|
||||
|
||||
// Step 1.
|
||||
$( '.wpforms-challenge-step1-done' ).on( 'click', function() {
|
||||
WPFormsChallenge.core.stepCompleted( 1 );
|
||||
} );
|
||||
|
||||
$( '#wpforms-builder' )
|
||||
|
||||
// Register select template event when the setup panel is ready.
|
||||
.on( 'wpformsBuilderSetupReady', function() {
|
||||
app.eventSelectTemplate();
|
||||
} )
|
||||
|
||||
// Restore tooltips when switching builder panels/sections.
|
||||
.on( 'wpformsPanelSwitch wpformsPanelSectionSwitch', function() {
|
||||
WPFormsChallenge.core.updateTooltipUI();
|
||||
} );
|
||||
|
||||
// Step 3 - Add fields.
|
||||
$( '.wpforms-challenge-step3-done' ).on( 'click', app.gotoNotificationStep );
|
||||
|
||||
// Step 4 - Notifications.
|
||||
$( document ).on( 'click', '.wpforms-challenge-step4-done', app.showEmbedPopup );
|
||||
|
||||
// Tooltipster ready.
|
||||
$.tooltipster.on( 'ready', app.tooltipsterReady );
|
||||
|
||||
// Move to step 3 if challenge is forced and exisiting form is opened.
|
||||
$( document ).on( 'wpformsBuilderReady', function() {
|
||||
if ( $( '.wpforms-panel-fields-button' ).hasClass( 'active' ) && WPFormsChallenge.core.loadStep() <= 2 ) {
|
||||
WPFormsChallenge.core.stepCompleted( 1 );
|
||||
WPFormsChallenge.core.stepCompleted( 2 );
|
||||
}
|
||||
} );
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Register select template event.
|
||||
*
|
||||
* @since 1.6.8
|
||||
*/
|
||||
eventSelectTemplate: function() {
|
||||
|
||||
$( '#wpforms-panel-setup' )
|
||||
|
||||
// Step 2 - Select the Form template.
|
||||
.off( 'click', '.wpforms-template-select' ) // Intercept Form Builder's form template selection and apply own logic.
|
||||
.on( 'click', '.wpforms-template-select', function( e ) {
|
||||
app.builderTemplateSelect( this, e );
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Start the Challenge.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*/
|
||||
startChallenge: function() {
|
||||
|
||||
WPFormsChallenge.admin.saveChallengeOption( { status: 'started' } );
|
||||
WPFormsChallenge.core.initListUI( 'started' );
|
||||
$( '.wpforms-challenge-popup-container' ).fadeOut( function() {
|
||||
$( '#wpforms-challenge-welcome-builder-popup' ).hide();
|
||||
} );
|
||||
WPFormsChallenge.core.timer.run( WPFormsChallenge.core.timer.initialSecondsLeft );
|
||||
WPFormsChallenge.core.updateTooltipUI();
|
||||
},
|
||||
|
||||
/**
|
||||
* Go to Step.
|
||||
*
|
||||
* @since 1.6.2
|
||||
* @since 1.7.5 Deprecated.
|
||||
*
|
||||
* @param {number|string} step Last saved step.
|
||||
*/
|
||||
gotoStep: function( step ) {
|
||||
console.warn( 'WARNING! Function "WPFormsChallenge.builder.gotoStep()" has been deprecated.' );
|
||||
},
|
||||
|
||||
/**
|
||||
* Save the second step before a template is selected.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {string} el Element selector.
|
||||
* @param {object} e Event.
|
||||
*/
|
||||
builderTemplateSelect: function( el, e ) {
|
||||
|
||||
WPFormsChallenge.core.resumeChallengeAndExec( e, function() {
|
||||
|
||||
WPFormsChallenge.core.stepCompleted( 2 )
|
||||
.done( WPForms.Admin.Builder.Setup.selectTemplate.bind( el, e ) );
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Tooltipster ready event callback.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*
|
||||
* @param {object} e Event object.
|
||||
*/
|
||||
tooltipsterReady: function( e ) {
|
||||
|
||||
var step = $( e.origin ).data( 'wpforms-challenge-step' );
|
||||
var formId = $( '#wpforms-builder-form' ).data( 'id' );
|
||||
|
||||
step = parseInt( step, 10 ) || 0;
|
||||
formId = parseInt( formId, 10 ) || 0;
|
||||
|
||||
// Save challenge form ID right after it's created.
|
||||
if ( 3 === step && formId > 0 ) {
|
||||
WPFormsChallenge.admin.saveChallengeOption( { form_id: formId } ); // eslint-disable-line camelcase
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Display 'Welcome to the Form Builder' popup.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*/
|
||||
showWelcomePopup: function() {
|
||||
|
||||
$( '#wpforms-challenge-welcome-builder-popup' ).show();
|
||||
$( '.wpforms-challenge-popup-container' ).fadeIn();
|
||||
},
|
||||
|
||||
/**
|
||||
* Go to Notification step.
|
||||
*
|
||||
* @since 1.7.5
|
||||
*
|
||||
* @param {object} e Event object.
|
||||
*/
|
||||
gotoNotificationStep: function( e ) {
|
||||
|
||||
WPFormsChallenge.core.stepCompleted( 3 ).done( function() {
|
||||
|
||||
WPFormsBuilder.panelSwitch( 'settings' );
|
||||
WPFormsBuilder.panelSectionSwitch( $( '.wpforms-panel .wpforms-panel-sidebar-section-notifications' ) );
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Display 'Embed in a Page' popup.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*/
|
||||
showEmbedPopup: function() {
|
||||
|
||||
WPFormsChallenge.core.stepCompleted( 4 ).done(
|
||||
WPFormsFormEmbedWizard.openPopup
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Enable Embed button when Embed popup is closed.
|
||||
*
|
||||
* @since 1.7.4
|
||||
*/
|
||||
enableEmbed: function() {
|
||||
|
||||
$( '#wpforms-embed' ).removeClass( 'wpforms-disabled' );
|
||||
},
|
||||
};
|
||||
|
||||
// Provide access to public functions/properties.
|
||||
return app;
|
||||
|
||||
}( document, window, jQuery ) );
|
||||
|
||||
// Initialize.
|
||||
WPFormsChallenge.builder.init();
|
@@ -0,0 +1 @@
|
||||
"use strict";var WPFormsChallenge=window.WPFormsChallenge||{};WPFormsChallenge.builder=window.WPFormsChallenge.builder||function(e,o,n){var t={init:function(){n(t.ready),n(o).on("load",function(){"function"==typeof n.ready.then?n.ready.then(t.load):t.load()})},ready:function(){t.setup(),t.events()},load:function(){-1<["started","paused"].indexOf(wpforms_challenge_admin.option.status)&&WPFormsChallenge.core.updateTooltipUI(),n(".wpforms-challenge").show()},setup:function(){"inited"===wpforms_challenge_admin.option.status&&(WPFormsChallenge.core.clearLocalStorage(),t.showWelcomePopup()),n("#wpforms-embed").addClass("wpforms-disabled");n.each(["#wpforms-setup-name",".wpforms-setup-title .wpforms-setup-title-after","#add-fields a i","#wpforms-builder-settings-notifications-title"],function(e,o){WPFormsChallenge.core.initTooltips(e+1,o,null)}),n(e).on("wpformsWizardPopupClose",t.enableEmbed)},events:function(){n("#wpforms-challenge-welcome-builder-popup").on("click","button",t.startChallenge),n(".wpforms-challenge-step1-done").on("click",function(){WPFormsChallenge.core.stepCompleted(1)}),n("#wpforms-builder").on("wpformsBuilderSetupReady",function(){t.eventSelectTemplate()}).on("wpformsPanelSwitch wpformsPanelSectionSwitch",function(){WPFormsChallenge.core.updateTooltipUI()}),n(".wpforms-challenge-step3-done").on("click",t.gotoNotificationStep),n(e).on("click",".wpforms-challenge-step4-done",t.showEmbedPopup),n.tooltipster.on("ready",t.tooltipsterReady),n(e).on("wpformsBuilderReady",function(){n(".wpforms-panel-fields-button").hasClass("active")&&WPFormsChallenge.core.loadStep()<=2&&(WPFormsChallenge.core.stepCompleted(1),WPFormsChallenge.core.stepCompleted(2))})},eventSelectTemplate:function(){n("#wpforms-panel-setup").off("click",".wpforms-template-select").on("click",".wpforms-template-select",function(e){t.builderTemplateSelect(this,e)})},startChallenge:function(){WPFormsChallenge.admin.saveChallengeOption({status:"started"}),WPFormsChallenge.core.initListUI("started"),n(".wpforms-challenge-popup-container").fadeOut(function(){n("#wpforms-challenge-welcome-builder-popup").hide()}),WPFormsChallenge.core.timer.run(WPFormsChallenge.core.timer.initialSecondsLeft),WPFormsChallenge.core.updateTooltipUI()},gotoStep:function(e){console.warn('WARNING! Function "WPFormsChallenge.builder.gotoStep()" has been deprecated.')},builderTemplateSelect:function(e,o){WPFormsChallenge.core.resumeChallengeAndExec(o,function(){WPFormsChallenge.core.stepCompleted(2).done(WPForms.Admin.Builder.Setup.selectTemplate.bind(e,o))})},tooltipsterReady:function(e){var e=n(e.origin).data("wpforms-challenge-step"),o=n("#wpforms-builder-form").data("id"),e=parseInt(e,10)||0,o=parseInt(o,10)||0;3===e&&0<o&&WPFormsChallenge.admin.saveChallengeOption({form_id:o})},showWelcomePopup:function(){n("#wpforms-challenge-welcome-builder-popup").show(),n(".wpforms-challenge-popup-container").fadeIn()},gotoNotificationStep:function(e){WPFormsChallenge.core.stepCompleted(3).done(function(){WPFormsBuilder.panelSwitch("settings"),WPFormsBuilder.panelSectionSwitch(n(".wpforms-panel .wpforms-panel-sidebar-section-notifications"))})},showEmbedPopup:function(){WPFormsChallenge.core.stepCompleted(4).done(WPFormsFormEmbedWizard.openPopup)},enableEmbed:function(){n("#wpforms-embed").removeClass("wpforms-disabled")}};return t}(document,window,jQuery),WPFormsChallenge.builder.init();
|
@@ -0,0 +1,848 @@
|
||||
/* global wpforms_challenge_admin */
|
||||
/**
|
||||
* WPForms Challenge function.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @since 1.6.2 Challenge v2
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var WPFormsChallenge = window.WPFormsChallenge || {};
|
||||
|
||||
WPFormsChallenge.core = window.WPFormsChallenge.core || ( function( document, window, $ ) {
|
||||
|
||||
/**
|
||||
* Public functions and properties.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @type {object}
|
||||
*/
|
||||
var app = {};
|
||||
|
||||
/**
|
||||
* Runtime variables.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*
|
||||
* @type {object}
|
||||
*/
|
||||
var vars = {};
|
||||
|
||||
/**
|
||||
* DOM elements.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*
|
||||
* @type {object}
|
||||
*/
|
||||
var el = {};
|
||||
|
||||
/**
|
||||
* Timer functions and properties.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @type {object}
|
||||
*/
|
||||
var timer = {
|
||||
|
||||
/**
|
||||
* Number of minutes to complete the challenge.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @type {number}
|
||||
*/
|
||||
initialSecondsLeft: WPFormsChallenge.admin.l10n.minutes_left * 60,
|
||||
|
||||
/**
|
||||
* Load timer ID.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @returns {string} ID from setInterval().
|
||||
*/
|
||||
loadId: function() {
|
||||
|
||||
return localStorage.getItem( 'wpformsChallengeTimerId' );
|
||||
},
|
||||
|
||||
/**
|
||||
* Save timer ID.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number|string} id setInterval() ID to save.
|
||||
*/
|
||||
saveId: function( id ) {
|
||||
|
||||
localStorage.setItem( 'wpformsChallengeTimerId', id );
|
||||
},
|
||||
|
||||
/**
|
||||
* Run the timer.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number} secondsLeft Number of seconds left to complete the Challenge.
|
||||
*
|
||||
* @returns {string|void} ID from setInterval().
|
||||
*/
|
||||
run: function( secondsLeft ) {
|
||||
|
||||
if ( 5 === app.loadStep() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
var timerId = setInterval( function() {
|
||||
|
||||
app.updateTimerUI( secondsLeft );
|
||||
secondsLeft--;
|
||||
if ( 0 > secondsLeft ) {
|
||||
timer.saveSecondsLeft( 0 );
|
||||
clearInterval( timerId );
|
||||
}
|
||||
}, 1000 );
|
||||
|
||||
timer.saveId( timerId );
|
||||
|
||||
return timerId;
|
||||
},
|
||||
|
||||
/**
|
||||
* Pause the timer.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
pause: function() {
|
||||
|
||||
var timerId;
|
||||
var elSeconds;
|
||||
var secondsLeft = timer.getSecondsLeft();
|
||||
|
||||
if ( 0 === secondsLeft || 5 === app.loadStep() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
timerId = timer.loadId();
|
||||
clearInterval( timerId );
|
||||
|
||||
elSeconds = $( '#wpforms-challenge-timer' ).data( 'seconds-left' );
|
||||
|
||||
if ( elSeconds ) {
|
||||
timer.saveSecondsLeft( elSeconds );
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Resume the timer.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
resume: function() {
|
||||
|
||||
var timerId;
|
||||
var secondsLeft = timer.getSecondsLeft();
|
||||
|
||||
if ( 0 === secondsLeft || 5 === app.loadStep() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
timerId = timer.loadId();
|
||||
|
||||
if ( timerId ) {
|
||||
clearInterval( timerId );
|
||||
}
|
||||
|
||||
timer.run( secondsLeft );
|
||||
},
|
||||
|
||||
/**
|
||||
* Clear all frontend saved timer data.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
clear: function() {
|
||||
|
||||
localStorage.removeItem( 'wpformsChallengeSecondsLeft' );
|
||||
localStorage.removeItem( 'wpformsChallengeTimerId' );
|
||||
localStorage.removeItem( 'wpformsChallengeTimerStatus' );
|
||||
$( '#wpforms-challenge-timer' ).removeData( 'seconds-left' );
|
||||
},
|
||||
|
||||
/**
|
||||
* Get number of seconds left to complete the Challenge.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @returns {number} Number of seconds left to complete the Challenge.
|
||||
*/
|
||||
getSecondsLeft: function() {
|
||||
|
||||
var secondsLeft = localStorage.getItem( 'wpformsChallengeSecondsLeft' );
|
||||
secondsLeft = parseInt( secondsLeft, 10 ) || 0;
|
||||
|
||||
return secondsLeft;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get number of seconds spent completing the Challenge.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number} secondsLeft Number of seconds left to complete the Challenge.
|
||||
*
|
||||
* @returns {number} Number of seconds spent completing the Challenge.
|
||||
*/
|
||||
getSecondsSpent: function( secondsLeft ) {
|
||||
|
||||
secondsLeft = secondsLeft || timer.getSecondsLeft();
|
||||
|
||||
return timer.initialSecondsLeft - secondsLeft;
|
||||
},
|
||||
|
||||
/**
|
||||
* Save number of seconds left to complete the Challenge.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number|string} secondsLeft Number of seconds left to complete the Challenge.
|
||||
*/
|
||||
saveSecondsLeft: function( secondsLeft ) {
|
||||
|
||||
localStorage.setItem( 'wpformsChallengeSecondsLeft', secondsLeft );
|
||||
},
|
||||
|
||||
/**
|
||||
* Get 'minutes' part of timer display.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number} secondsLeft Number of seconds left to complete the Challenge.
|
||||
*
|
||||
* @returns {number} 'Minutes' part of timer display.
|
||||
*/
|
||||
getMinutesFormatted: function( secondsLeft ) {
|
||||
|
||||
secondsLeft = secondsLeft || timer.getSecondsLeft();
|
||||
|
||||
return Math.floor( secondsLeft / 60 );
|
||||
},
|
||||
|
||||
/**
|
||||
* Get 'seconds' part of timer display.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number} secondsLeft Number of seconds left to complete the Challenge.
|
||||
*
|
||||
* @returns {number} 'Seconds' part of timer display.
|
||||
*/
|
||||
getSecondsFormatted: function( secondsLeft ) {
|
||||
|
||||
secondsLeft = secondsLeft || timer.getSecondsLeft();
|
||||
|
||||
return secondsLeft % 60;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get formatted timer for display.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number} secondsLeft Number of seconds left to complete the Challenge.
|
||||
*
|
||||
* @returns {string} Formatted timer for display.
|
||||
*/
|
||||
getFormatted: function( secondsLeft ) {
|
||||
|
||||
secondsLeft = secondsLeft || timer.getSecondsLeft();
|
||||
|
||||
var timerMinutes = timer.getMinutesFormatted( secondsLeft );
|
||||
var timerSeconds = timer.getSecondsFormatted( secondsLeft );
|
||||
|
||||
return timerMinutes + ( 9 < timerSeconds ? ':' : ':0' ) + timerSeconds;
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Public functions and properties.
|
||||
*/
|
||||
app = {
|
||||
|
||||
/**
|
||||
* Public timer functions and properties.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
timer: timer,
|
||||
|
||||
/**
|
||||
* Start the engine.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
init: function() {
|
||||
|
||||
$( app.ready );
|
||||
$( window ).on( 'load', function() {
|
||||
|
||||
// in case of jQuery 3.+ we need to wait for an `ready` event first.
|
||||
if ( typeof $.ready.then === 'function' ) {
|
||||
$.ready.then( app.load );
|
||||
} else {
|
||||
app.load();
|
||||
}
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Document ready.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
ready: function() {
|
||||
|
||||
app.setup();
|
||||
app.events();
|
||||
},
|
||||
|
||||
/**
|
||||
* Window load.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
load: function() {
|
||||
|
||||
if ( wpforms_challenge_admin.option.status === 'started' ) {
|
||||
app.timer.run( app.timer.getSecondsLeft() );
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Initial setup.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
setup: function() {
|
||||
|
||||
var secondsLeft;
|
||||
var timerId = app.timer.loadId();
|
||||
|
||||
if ( timerId ) {
|
||||
clearInterval( timerId );
|
||||
secondsLeft = app.timer.getSecondsLeft();
|
||||
}
|
||||
|
||||
if ( ! timerId || 0 === app.loadStep() || wpforms_challenge_admin.option.status === 'inited' ) {
|
||||
secondsLeft = app.timer.initialSecondsLeft;
|
||||
}
|
||||
|
||||
app.initElements();
|
||||
app.refreshStep();
|
||||
app.initListUI( null, true );
|
||||
app.updateListUI();
|
||||
app.updateTimerUI( secondsLeft );
|
||||
},
|
||||
|
||||
/**
|
||||
* Register JS events.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
events: function() {
|
||||
|
||||
$( [ window, document ] )
|
||||
.on( 'blur', app.pauseChallenge )
|
||||
.on( 'focus', app.resumeChallenge )
|
||||
.on( 'click', '.wpforms-challenge-done-btn', app.resumeChallenge );
|
||||
|
||||
el.$btnPause.on( 'click', app.pauseChallenge );
|
||||
el.$btnResume.on( 'click', app.resumeChallenge );
|
||||
|
||||
el.$listSteps.on( 'click', '.wpforms-challenge-item-current', app.refreshPage );
|
||||
},
|
||||
|
||||
/**
|
||||
* DOM elements.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*/
|
||||
initElements: function() {
|
||||
|
||||
el = {
|
||||
$challenge: $( '.wpforms-challenge' ),
|
||||
$btnPause: $( '.wpforms-challenge-pause' ),
|
||||
$btnResume: $( '.wpforms-challenge-resume' ),
|
||||
$listSteps: $( '.wpforms-challenge-list' ),
|
||||
$listBlock: $( '.wpforms-challenge-list-block' ),
|
||||
$listBtnToggle: $( '.wpforms-challenge-list-block .toggle-list' ),
|
||||
$progressBar: $( '.wpforms-challenge-bar' ),
|
||||
$tooltipBtnDone: function() {
|
||||
return $( '.wpforms-challenge-tooltip .wpforms-challenge-done-btn' );
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Get last saved step.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @returns {number} Last saved step.
|
||||
*/
|
||||
loadStep: function() {
|
||||
|
||||
var step = localStorage.getItem( 'wpformsChallengeStep' );
|
||||
step = parseInt( step, 10 ) || 0;
|
||||
|
||||
return step;
|
||||
},
|
||||
|
||||
/**
|
||||
* Save Challenge step.
|
||||
*
|
||||
* @param {number|string} step Step to save.
|
||||
*
|
||||
* @returns {object} jqXHR object from saveChallengeOption().
|
||||
*/
|
||||
saveStep: function( step ) {
|
||||
|
||||
localStorage.setItem( 'wpformsChallengeStep', step );
|
||||
|
||||
return WPFormsChallenge.admin.saveChallengeOption( { step: step } );
|
||||
},
|
||||
|
||||
/**
|
||||
* Update a step with backend data.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
refreshStep: function() {
|
||||
|
||||
var savedStep = el.$challenge.data( 'wpforms-challenge-saved-step' );
|
||||
savedStep = parseInt( savedStep, 10 ) || 0;
|
||||
|
||||
// Step saved on a backend has a priority.
|
||||
if ( app.loadStep() !== savedStep ) {
|
||||
app.saveStep( savedStep );
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Complete Challenge step.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number|string} step Step to complete.
|
||||
*
|
||||
* @returns {object} jqXHR object from saveStep().
|
||||
*/
|
||||
stepCompleted: function( step ) {
|
||||
|
||||
app.updateListUI( step );
|
||||
app.updateTooltipUI( step );
|
||||
|
||||
return app.saveStep( step );
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialize Challenge tooltips.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number|string} step Last saved step.
|
||||
* @param {string} anchor Element selector to bind tooltip to.
|
||||
* @param {object} args Tooltipster arguments.
|
||||
*/
|
||||
initTooltips: function( step, anchor, args ) {
|
||||
|
||||
if ( typeof $.fn.tooltipster === 'undefined' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
var $dot = $( '<span class="wpforms-challenge-dot wpforms-challenge-dot-step' + step + '" data-wpforms-challenge-step="' + step + '"> </span>' );
|
||||
var tooltipsterArgs = {
|
||||
content : $( '#tooltip-content' + step ),
|
||||
trigger : null,
|
||||
interactive : true,
|
||||
animationDuration: 0,
|
||||
delay : 0,
|
||||
theme : [ 'tooltipster-default', 'wpforms-challenge-tooltip' ],
|
||||
side : [ 'top' ],
|
||||
distance : 3,
|
||||
functionReady : function( instance, helper ) {
|
||||
|
||||
$( helper.tooltip ).addClass( 'wpforms-challenge-tooltip-step' + step );
|
||||
|
||||
// Custom positioning.
|
||||
if ( step === 4 || step === 3 ) {
|
||||
instance.option( 'side', 'right' );
|
||||
} else if ( step === 1 ) {
|
||||
instance.option( 'side', 'left' );
|
||||
}
|
||||
|
||||
// Reposition is needed to render max-width CSS correctly.
|
||||
instance.reposition();
|
||||
},
|
||||
};
|
||||
|
||||
if ( typeof args === 'object' && args !== null ) {
|
||||
$.extend( tooltipsterArgs, args );
|
||||
}
|
||||
|
||||
$dot.insertAfter( anchor ).tooltipster( tooltipsterArgs );
|
||||
},
|
||||
|
||||
/**
|
||||
* Update tooltips appearance.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number|string} step Last saved step.
|
||||
*/
|
||||
updateTooltipUI: function( step ) {
|
||||
|
||||
var nextStep;
|
||||
|
||||
step = step || app.loadStep();
|
||||
nextStep = step + 1;
|
||||
|
||||
$( '.wpforms-challenge-dot' ).each( function( i, el ) {
|
||||
|
||||
var $dot = $( el ),
|
||||
elStep = $dot.data( 'wpforms-challenge-step' );
|
||||
|
||||
if ( elStep < nextStep ) {
|
||||
$dot.addClass( 'wpforms-challenge-dot-completed' );
|
||||
}
|
||||
|
||||
if ( elStep > nextStep ) {
|
||||
$dot.addClass( 'wpforms-challenge-dot-next' );
|
||||
}
|
||||
|
||||
if ( elStep === nextStep ) {
|
||||
$dot.removeClass( 'wpforms-challenge-dot-completed wpforms-challenge-dot-next' );
|
||||
}
|
||||
|
||||
// Zero timeout is needed to properly detect $el visibility.
|
||||
setTimeout( function() {
|
||||
if ( $dot.is( ':visible' ) && elStep === nextStep ) {
|
||||
$dot.tooltipster( 'open' );
|
||||
} else {
|
||||
$dot.tooltipster( 'close' );
|
||||
}
|
||||
}, 0 );
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Init ListUI.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*
|
||||
* @param {number|string} status Challenge status.
|
||||
* @param {boolean} initial Initial run, false by default.
|
||||
*/
|
||||
initListUI: function( status, initial ) {
|
||||
|
||||
status = status || wpforms_challenge_admin.option.status;
|
||||
|
||||
if ( [ 'started', 'paused' ].indexOf( status ) > -1 ) {
|
||||
el.$listBlock.find( 'p' ).hide();
|
||||
el.$listBtnToggle.show();
|
||||
el.$progressBar.show();
|
||||
|
||||
// Transform skip button to cancel button.
|
||||
var $skipBtn = el.$listBlock.find( '.list-block-button.challenge-skip' );
|
||||
|
||||
$skipBtn
|
||||
.attr( 'title', $skipBtn.data( 'cancel-title' ) )
|
||||
.removeClass( 'challenge-skip' )
|
||||
.addClass( 'challenge-cancel' );
|
||||
}
|
||||
|
||||
// Set initial window closed (collapsed) state if window is short or if it is closed manually.
|
||||
if (
|
||||
initial &&
|
||||
(
|
||||
( $( window ).height() < 900 && wpforms_challenge_admin.option.window_closed === '' ) ||
|
||||
wpforms_challenge_admin.option.window_closed === '1'
|
||||
)
|
||||
) {
|
||||
el.$listBlock.find( 'p' ).hide();
|
||||
el.$listBtnToggle.trigger( 'click' );
|
||||
}
|
||||
|
||||
if ( status === 'paused' ) {
|
||||
|
||||
el.$challenge.addClass( 'paused' );
|
||||
el.$btnPause.hide();
|
||||
el.$btnResume.show();
|
||||
|
||||
} else {
|
||||
|
||||
// Zero timeout is needed to avoid firing 'focus' and 'click' events in the same loop.
|
||||
setTimeout( function() {
|
||||
el.$btnPause.show();
|
||||
}, 0 );
|
||||
|
||||
el.$challenge.removeClass( 'paused' );
|
||||
el.$btnResume.hide();
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Update Challenge task list appearance.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number|string} step Last saved step.
|
||||
*/
|
||||
updateListUI: function( step ) {
|
||||
|
||||
step = step || app.loadStep();
|
||||
|
||||
el.$listSteps.find( 'li' ).slice( 0, step ).addClass( 'wpforms-challenge-item-completed' ).removeClass( 'wpforms-challenge-item-current' );
|
||||
el.$listSteps.find( 'li' ).eq( step ).addClass( 'wpforms-challenge-item-current' );
|
||||
el.$progressBar.find( 'div' ).css( 'width', ( step * 20 ) + '%' );
|
||||
},
|
||||
|
||||
/**
|
||||
* Update Challenge timer appearance.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {number} secondsLeft Number of seconds left to complete the Challenge.
|
||||
*/
|
||||
updateTimerUI: function( secondsLeft ) {
|
||||
|
||||
if ( ! secondsLeft || isNaN( secondsLeft ) || '0' === secondsLeft ) {
|
||||
secondsLeft = 0;
|
||||
}
|
||||
|
||||
app.timer.saveSecondsLeft( secondsLeft );
|
||||
$( '#wpforms-challenge-timer' ).text( app.timer.getFormatted( secondsLeft ) ).data( 'seconds-left', secondsLeft );
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove Challenge interface.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
removeChallengeUI: function() {
|
||||
|
||||
$( '.wpforms-challenge-dot' ).remove();
|
||||
el.$challenge.remove();
|
||||
},
|
||||
|
||||
/**
|
||||
* Clear all Challenge frontend saved data.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
clearLocalStorage: function() {
|
||||
|
||||
localStorage.removeItem( 'wpformsChallengeStep' );
|
||||
app.timer.clear();
|
||||
},
|
||||
|
||||
/**
|
||||
* Pause Challenge.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*
|
||||
* @param {object} e Event object.
|
||||
*/
|
||||
pauseChallenge: function( e ) {
|
||||
|
||||
// Skip if out to the iframe.
|
||||
if ( document.activeElement.tagName === 'IFRAME' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip if is not started.
|
||||
if ( wpforms_challenge_admin.option.status !== 'started' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
vars.pauseEvent = e.type;
|
||||
|
||||
app.pauseResumeChallenge( 'pause' );
|
||||
},
|
||||
|
||||
/**
|
||||
* Resume Challenge.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*
|
||||
* @param {object} e Event object.
|
||||
*
|
||||
* @returns {Function|void} Return pause challenge function or void.
|
||||
*/
|
||||
resumeChallenge: function( e ) {
|
||||
|
||||
// Skip if is not paused.
|
||||
if ( wpforms_challenge_admin.option.status !== 'paused' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Resume on 'focus' only if it has been paused on 'blur'.
|
||||
if ( e.type === 'focus' && vars.pauseEvent !== 'blur' ) {
|
||||
delete vars.pauseEvent;
|
||||
return;
|
||||
}
|
||||
|
||||
vars.resumeEvent = e.type;
|
||||
|
||||
return app.pauseResumeChallenge( 'resume' );
|
||||
},
|
||||
|
||||
/**
|
||||
* Pause/Resume Challenge.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*
|
||||
* @param {string} action Action to perform. `pause` or `resume`.
|
||||
*
|
||||
* @returns {Function} Save challenge option.
|
||||
*/
|
||||
pauseResumeChallenge: function( action ) {
|
||||
|
||||
action = action === 'pause' ? action : 'resume';
|
||||
|
||||
app.timer[ action ]();
|
||||
|
||||
var optionData = {
|
||||
status : action === 'pause' ? 'paused' : 'started',
|
||||
seconds_spent: app.timer.getSecondsSpent(),
|
||||
seconds_left : app.timer.getSecondsLeft(),
|
||||
};
|
||||
|
||||
app.initListUI( optionData.status );
|
||||
|
||||
return WPFormsChallenge.admin.saveChallengeOption( optionData );
|
||||
},
|
||||
|
||||
/**
|
||||
* Resume Challenge and execute the callback.
|
||||
*
|
||||
* @since 1.7.5
|
||||
*
|
||||
* @param {object} e Event object.
|
||||
* @param {Function} callback Callback function.
|
||||
*/
|
||||
resumeChallengeAndExec: function( e, callback ) {
|
||||
|
||||
if ( typeof callback !== 'function' ) {
|
||||
callback = function() {};
|
||||
}
|
||||
|
||||
if ( wpforms_challenge_admin.option.status !== 'paused' ) {
|
||||
callback();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var resumeResult = app.resumeChallenge( e );
|
||||
|
||||
if ( typeof resumeResult === 'object' && typeof resumeResult.done === 'function' ) {
|
||||
resumeResult.done( callback );
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Refresh Page in order to re-init current step.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*
|
||||
* @param {object} e Event object.
|
||||
*/
|
||||
refreshPage: function( e ) {
|
||||
|
||||
window.location.reload( true );
|
||||
},
|
||||
|
||||
/**
|
||||
* Check if we're in Gutenberg editor.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @returns {boolean} Is Gutenberg or not.
|
||||
*/
|
||||
isGutenberg: function() {
|
||||
|
||||
return typeof wp !== 'undefined' && Object.prototype.hasOwnProperty.call( wp, 'blocks' );
|
||||
},
|
||||
|
||||
/**
|
||||
* Trigger form embed page save potentially reloading it.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
triggerPageSave: function() {
|
||||
|
||||
if ( app.isGutenberg() ) {
|
||||
app.gutenbergPageSave();
|
||||
|
||||
} else {
|
||||
$( '#post #publish' ).trigger( 'click' );
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Save page for Gutenberg.
|
||||
*
|
||||
* @since 1.5.2
|
||||
*/
|
||||
gutenbergPageSave: function() {
|
||||
|
||||
var $gb = $( '.block-editor' ),
|
||||
$updateBtn = $gb.find( '.editor-post-publish-button.editor-post-publish-button__button' );
|
||||
|
||||
// Trigger click on the Update button.
|
||||
if ( $updateBtn.length > 0 ) {
|
||||
$updateBtn.trigger( 'click' );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Use MutationObserver to wait while Gutenberg create/display panel with Publish button.
|
||||
var obs = {
|
||||
targetNode : $gb.find( '.edit-post-layout, .block-editor-editor-skeleton__publish > div' )[0],
|
||||
config : {
|
||||
childList: true,
|
||||
attributes: true,
|
||||
subtree: true,
|
||||
},
|
||||
};
|
||||
|
||||
obs.callback = function( mutationsList, observer ) {
|
||||
|
||||
var $btn = $gb.find( '.editor-post-publish-button, .editor-post-publish-panel__header-publish-button .editor-post-publish-button__button' );
|
||||
|
||||
if ( $btn.length > 0 ) {
|
||||
$btn.trigger( 'click' );
|
||||
observer.disconnect();
|
||||
}
|
||||
};
|
||||
|
||||
obs.observer = new MutationObserver( obs.callback );
|
||||
obs.observer.observe( obs.targetNode, obs.config );
|
||||
|
||||
// Trigger click on the Publish button that opens the additional publishing panel.
|
||||
$gb.find( '.edit-post-toggle-publish-panel__button, .editor-post-publish-panel__toggle.editor-post-publish-button__button' )
|
||||
.trigger( 'click' );
|
||||
},
|
||||
};
|
||||
|
||||
// Provide access to public functions/properties.
|
||||
return app;
|
||||
|
||||
}( document, window, jQuery ) );
|
||||
|
||||
WPFormsChallenge.core.init();
|
1
wp-content/plugins/wpforms-lite/assets/js/components/admin/challenge/challenge-core.min.js
vendored
Normal file
1
wp-content/plugins/wpforms-lite/assets/js/components/admin/challenge/challenge-core.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,323 @@
|
||||
/* global ajaxurl */
|
||||
/**
|
||||
* WPForms Challenge function.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @since 1.6.2 Challenge v2.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var WPFormsChallenge = window.WPFormsChallenge || {};
|
||||
|
||||
WPFormsChallenge.embed = window.WPFormsChallenge.embed || ( function( document, window, $ ) {
|
||||
|
||||
/**
|
||||
* Public functions and properties.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @type {object}
|
||||
*/
|
||||
var app = {
|
||||
|
||||
/**
|
||||
* Start the engine.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
init: function() {
|
||||
|
||||
$( app.ready );
|
||||
$( window ).on( 'load', function() {
|
||||
|
||||
// in case of jQuery 3.+ we need to wait for an `ready` event first.
|
||||
if ( typeof $.ready.then === 'function' ) {
|
||||
$.ready.then( app.load );
|
||||
} else {
|
||||
app.load();
|
||||
}
|
||||
} );
|
||||
},
|
||||
|
||||
/**
|
||||
* Document ready.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
ready: function() {
|
||||
|
||||
app.setup();
|
||||
app.events();
|
||||
app.observeFullscreenMode();
|
||||
},
|
||||
|
||||
/**
|
||||
* Window load.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
load: function() {
|
||||
|
||||
// If the page is Add new page.
|
||||
if ( window.location.href.indexOf( 'post-new.php' ) > -1 ) {
|
||||
app.lastStep();
|
||||
$( '.wpforms-challenge-dot-completed' ).hide();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ( WPFormsChallenge.core.isGutenberg() ) {
|
||||
WPFormsChallenge.core.initTooltips( 5, '.block-editor .edit-post-header', { side: 'bottom' } );
|
||||
app.updateTooltipVisibility();
|
||||
} else {
|
||||
WPFormsChallenge.core.initTooltips( 5, '.wpforms-insert-form-button', { side: 'right' } );
|
||||
}
|
||||
|
||||
WPFormsChallenge.core.updateTooltipUI();
|
||||
},
|
||||
|
||||
/**
|
||||
* Initial setup.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
setup: function() {
|
||||
|
||||
if ( 5 === WPFormsChallenge.core.loadStep() ) {
|
||||
$( '.wpforms-challenge' ).addClass( 'wpforms-challenge-completed' );
|
||||
app.showPopup();
|
||||
}
|
||||
|
||||
$( '.wpforms-challenge' ).show();
|
||||
},
|
||||
|
||||
/**
|
||||
* Register JS events.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
events: function() {
|
||||
|
||||
$( '.wpforms-challenge-step5-done' )
|
||||
.on( 'click', app.lastStep );
|
||||
|
||||
$( '.wpforms-challenge-popup-close, .wpforms-challenge-end' )
|
||||
.on( 'click', app.completeChallenge );
|
||||
|
||||
$( '#wpforms-challenge-contact-form .wpforms-challenge-popup-contact-btn' )
|
||||
.on( 'click', app.submitContactForm );
|
||||
},
|
||||
|
||||
/**
|
||||
* Last step done routine.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*/
|
||||
lastStep: function() {
|
||||
|
||||
WPFormsChallenge.core.timer.pause();
|
||||
WPFormsChallenge.core.stepCompleted( 5 );
|
||||
$( '.wpforms-challenge' ).addClass( 'wpforms-challenge-completed' );
|
||||
app.showPopup();
|
||||
},
|
||||
|
||||
/**
|
||||
* Show either 'Congratulations' or 'Contact Us' popup.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
showPopup: function() {
|
||||
|
||||
var secondsLeft = WPFormsChallenge.core.timer.getSecondsLeft();
|
||||
|
||||
$( '.wpforms-challenge-popup-container' ).show();
|
||||
|
||||
if ( 0 < secondsLeft ) {
|
||||
var secondsSpent = WPFormsChallenge.core.timer.getSecondsSpent( secondsLeft );
|
||||
|
||||
$( '#wpforms-challenge-congrats-minutes' )
|
||||
.text( WPFormsChallenge.core.timer.getMinutesFormatted( secondsSpent ) );
|
||||
$( '#wpforms-challenge-congrats-seconds' )
|
||||
.text( WPFormsChallenge.core.timer.getSecondsFormatted( secondsSpent ) );
|
||||
$( '#wpforms-challenge-congrats-popup' ).show();
|
||||
} else {
|
||||
$( '#wpforms-challenge-contact-popup' ).show();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Hide the popup.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
hidePopup: function() {
|
||||
|
||||
$( '.wpforms-challenge-popup-container' ).hide();
|
||||
$( '.wpforms-challenge-popup' ).hide();
|
||||
},
|
||||
|
||||
/**
|
||||
* Complete Challenge.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*/
|
||||
completeChallenge: function() {
|
||||
|
||||
var optionData = {
|
||||
status : 'completed',
|
||||
seconds_spent: WPFormsChallenge.core.timer.getSecondsSpent(),
|
||||
seconds_left : WPFormsChallenge.core.timer.getSecondsLeft(),
|
||||
};
|
||||
|
||||
app.hidePopup();
|
||||
|
||||
WPFormsChallenge.core.removeChallengeUI();
|
||||
WPFormsChallenge.core.clearLocalStorage();
|
||||
|
||||
WPFormsChallenge.admin.saveChallengeOption( optionData )
|
||||
.done( WPFormsChallenge.core.triggerPageSave ); // Save and reload the page to remove WPForms Challenge JS.
|
||||
},
|
||||
|
||||
/**
|
||||
* Submit contact form button click event handler.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param {object} e Event object.
|
||||
*/
|
||||
submitContactForm: function( e ) {
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
var $btn = $( this ),
|
||||
$form = $btn.closest( '#wpforms-challenge-contact-form' );
|
||||
|
||||
/* eslint-disable camelcase */
|
||||
var data = {
|
||||
action : 'wpforms_challenge_send_contact_form',
|
||||
_wpnonce : WPFormsChallenge.admin.l10n.nonce,
|
||||
contact_data: {
|
||||
message : $form.find( '.wpforms-challenge-contact-message' ).val(),
|
||||
contact_me: $form.find( '.wpforms-challenge-contact-permission' ).prop( 'checked' ),
|
||||
},
|
||||
};
|
||||
/* eslint-enable */
|
||||
|
||||
$btn.prop( 'disabled', true );
|
||||
|
||||
$.post( ajaxurl, data, function( response ) {
|
||||
|
||||
if ( ! response.success ) {
|
||||
console.error( 'Error sending WPForms Challenge Contact Form.' );
|
||||
}
|
||||
} ).done( app.completeChallenge );
|
||||
},
|
||||
|
||||
/**
|
||||
* Observe Gutenberg's Fullscreen Mode state to adjust tooltip positioning.
|
||||
*
|
||||
* @since 1.6.2
|
||||
*/
|
||||
observeFullscreenMode: function() {
|
||||
|
||||
var $body = $( 'body' ),
|
||||
isFullScreenPrev = $body.hasClass( 'is-fullscreen-mode' );
|
||||
|
||||
// MutationObserver configuration and callback.
|
||||
var obs = {
|
||||
targetNode : $body[0],
|
||||
config : {
|
||||
attributes: true,
|
||||
},
|
||||
};
|
||||
|
||||
obs.callback = function( mutationsList, observer ) {
|
||||
|
||||
var mutation,
|
||||
isFullScreen,
|
||||
$step5 = $( '.wpforms-challenge-tooltip-step5' ),
|
||||
$step5Arrow = $step5.find( '.tooltipster-arrow' );
|
||||
|
||||
for ( var i in mutationsList ) {
|
||||
mutation = mutationsList[ i ];
|
||||
if ( mutation.type !== 'attributes' || mutation.attributeName !== 'class' ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
isFullScreen = $body.hasClass( 'is-fullscreen-mode' );
|
||||
if ( isFullScreen === isFullScreenPrev ) {
|
||||
continue;
|
||||
}
|
||||
isFullScreenPrev = isFullScreen;
|
||||
|
||||
if ( isFullScreen ) {
|
||||
$step5.css( {
|
||||
'top': '93px',
|
||||
'left': '0',
|
||||
} );
|
||||
$step5Arrow.css( 'left', '91px' );
|
||||
} else {
|
||||
$step5.css( {
|
||||
'top': '125px',
|
||||
'left': '66px',
|
||||
} );
|
||||
$step5Arrow.css( 'left', '130px' );
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
obs.observer = new MutationObserver( obs.callback );
|
||||
obs.observer.observe( obs.targetNode, obs.config );
|
||||
},
|
||||
|
||||
/**
|
||||
* Update tooltip z-index when Gutenberg sidebar is open.
|
||||
*
|
||||
* @since 1.7.4
|
||||
*
|
||||
* @returns {Function} Default function.
|
||||
*/
|
||||
updateTooltipVisibility: function() {
|
||||
|
||||
var targetNode = document.querySelector( '.interface-interface-skeleton__body' );
|
||||
|
||||
if ( targetNode === null ) {
|
||||
return app.updateTooltipVisibilityDefault();
|
||||
}
|
||||
|
||||
var observer = new MutationObserver( function( mutationsList ) {
|
||||
|
||||
var $step5 = $( '.wpforms-challenge-tooltip-step5' );
|
||||
|
||||
for ( var mutation of mutationsList ) {
|
||||
|
||||
if ( mutation.type === 'childList' ) {
|
||||
$step5.toggleClass( 'wpforms-challenge-tooltip-step5-hide' );
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
observer.observe( targetNode, { attributes: true, childList: true } );
|
||||
},
|
||||
|
||||
/**
|
||||
* Update tooltip visibility for WP 5.6 version.
|
||||
*
|
||||
* @since 1.7.4
|
||||
*/
|
||||
updateTooltipVisibilityDefault: function() {
|
||||
|
||||
$( '.editor-inserter__toggle' ).on( 'click', function() {
|
||||
|
||||
$( '.wpforms-challenge-tooltip-step5' ).toggleClass( 'wpforms-challenge-tooltip-step5-hide' );
|
||||
} );
|
||||
},
|
||||
};
|
||||
|
||||
// Provide access to public functions/properties.
|
||||
return app;
|
||||
|
||||
}( document, window, jQuery ) );
|
||||
|
||||
// Initialize.
|
||||
WPFormsChallenge.embed.init();
|
1
wp-content/plugins/wpforms-lite/assets/js/components/admin/challenge/challenge-embed.min.js
vendored
Normal file
1
wp-content/plugins/wpforms-lite/assets/js/components/admin/challenge/challenge-embed.min.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
"use strict";var WPFormsChallenge=window.WPFormsChallenge||{};WPFormsChallenge.embed=window.WPFormsChallenge.embed||function(o,e,c){var t={init:function(){c(t.ready),c(e).on("load",function(){"function"==typeof c.ready.then?c.ready.then(t.load):t.load()})},ready:function(){t.setup(),t.events(),t.observeFullscreenMode()},load:function(){-1<e.location.href.indexOf("post-new.php")?(t.lastStep(),c(".wpforms-challenge-dot-completed").hide()):(WPFormsChallenge.core.isGutenberg()?(WPFormsChallenge.core.initTooltips(5,".block-editor .edit-post-header",{side:"bottom"}),t.updateTooltipVisibility()):WPFormsChallenge.core.initTooltips(5,".wpforms-insert-form-button",{side:"right"}),WPFormsChallenge.core.updateTooltipUI())},setup:function(){5===WPFormsChallenge.core.loadStep()&&(c(".wpforms-challenge").addClass("wpforms-challenge-completed"),t.showPopup()),c(".wpforms-challenge").show()},events:function(){c(".wpforms-challenge-step5-done").on("click",t.lastStep),c(".wpforms-challenge-popup-close, .wpforms-challenge-end").on("click",t.completeChallenge),c("#wpforms-challenge-contact-form .wpforms-challenge-popup-contact-btn").on("click",t.submitContactForm)},lastStep:function(){WPFormsChallenge.core.timer.pause(),WPFormsChallenge.core.stepCompleted(5),c(".wpforms-challenge").addClass("wpforms-challenge-completed"),t.showPopup()},showPopup:function(){var e=WPFormsChallenge.core.timer.getSecondsLeft();c(".wpforms-challenge-popup-container").show(),(0<e?(e=WPFormsChallenge.core.timer.getSecondsSpent(e),c("#wpforms-challenge-congrats-minutes").text(WPFormsChallenge.core.timer.getMinutesFormatted(e)),c("#wpforms-challenge-congrats-seconds").text(WPFormsChallenge.core.timer.getSecondsFormatted(e)),c("#wpforms-challenge-congrats-popup")):c("#wpforms-challenge-contact-popup")).show()},hidePopup:function(){c(".wpforms-challenge-popup-container").hide(),c(".wpforms-challenge-popup").hide()},completeChallenge:function(){var e={status:"completed",seconds_spent:WPFormsChallenge.core.timer.getSecondsSpent(),seconds_left:WPFormsChallenge.core.timer.getSecondsLeft()};t.hidePopup(),WPFormsChallenge.core.removeChallengeUI(),WPFormsChallenge.core.clearLocalStorage(),WPFormsChallenge.admin.saveChallengeOption(e).done(WPFormsChallenge.core.triggerPageSave)},submitContactForm:function(e){e.preventDefault();var e=c(this),o=e.closest("#wpforms-challenge-contact-form"),o={action:"wpforms_challenge_send_contact_form",_wpnonce:WPFormsChallenge.admin.l10n.nonce,contact_data:{message:o.find(".wpforms-challenge-contact-message").val(),contact_me:o.find(".wpforms-challenge-contact-permission").prop("checked")}};e.prop("disabled",!0),c.post(ajaxurl,o,function(e){e.success||console.error("Error sending WPForms Challenge Contact Form.")}).done(t.completeChallenge)},observeFullscreenMode:function(){var r=c("body"),a=r.hasClass("is-fullscreen-mode"),e={targetNode:r[0],config:{attributes:!0},callback:function(e,o){var t,l,n=c(".wpforms-challenge-tooltip-step5"),s=n.find(".tooltipster-arrow");for(l in e)"attributes"===(t=e[l]).type&&"class"===t.attributeName&&(t=r.hasClass("is-fullscreen-mode"))!==a&&((a=t)?(n.css({top:"93px",left:"0"}),s.css("left","91px")):(n.css({top:"125px",left:"66px"}),s.css("left","130px")))}};e.observer=new MutationObserver(e.callback),e.observer.observe(e.targetNode,e.config)},updateTooltipVisibility:function(){var e=o.querySelector(".interface-interface-skeleton__body");if(null===e)return t.updateTooltipVisibilityDefault();new MutationObserver(function(e){var o,t=c(".wpforms-challenge-tooltip-step5");for(o of e)"childList"===o.type&&t.toggleClass("wpforms-challenge-tooltip-step5-hide")}).observe(e,{attributes:!0,childList:!0})},updateTooltipVisibilityDefault:function(){c(".editor-inserter__toggle").on("click",function(){c(".wpforms-challenge-tooltip-step5").toggleClass("wpforms-challenge-tooltip-step5-hide")})}};return t}(document,window,jQuery),WPFormsChallenge.embed.init();
|
Reference in New Issue
Block a user