Current File : /home/kelaby89/muzza.fit/wp-content/themes/deadlift/assets/js/admin/import.js |
jQuery( document ).ready( function( $ ) {
$( '.deadlift-notice .btn-get-ocdi' ).on( 'click', function( e ) {
e.preventDefault();
var $btn = $( this );
$btn.addClass( 'updating-message' ).text( wolfthemeAdmin.installingPluginsMessage );
console.log( "Installing..." );
//return;
installPlugins(Object.entries(wolfthemeAdmin.requiredPlugins));
} );
function installPlugins(plugins) {
//var plugins = Object.entries(wolfthemeAdmin.requiredPlugins) || plugins;
if (plugins.length > 0) {
// pop function remove the last entry from the arraay
let current = plugins.pop(); // return the removed entry
// pop returns rentry like ['slug', 'url']
//console.log( current[0] );
$.ajax({
type : 'POST',
url : ajaxurl,
data : {
action : 'deadlift_import_external_plugin',
slug : current[0],
security : wolfthemeAdmin.installNonce,
},
success : function( response ) {
console.log( response );
if ( 0 === plugins.length ) {
$( '.deadlift-install-import-plugin-btn' ).text( wolfthemeAdmin.activatingPluginsMessage );
console.log( "Activating..." );
activatePlugins(Object.entries(wolfthemeAdmin.requiredPlugins));
}
},
error : function( xhr, ajaxOptions, thrownError ){
console.log(thrownError);
}
})
.done(function (result) {
installPlugins(plugins);
});
}
}
// Recursive function so the plugins are activated one by one
function activatePlugins(plugins) {
//var plugins = Object.entries(wolfthemeAdmin.requiredPlugins) || plugins;
if (plugins.length > 0) {
// pop functino remove the last entry from the arraay
let current = plugins.pop(); // return the removed entry
// pop returns rentry like ['slug', 'path']
//return;
$.ajax({
type : "POST",
url : ajaxurl,
data : {
action : 'deadlift_activate_all_plugins',
slug : current[0],
security : wolfthemeAdmin.activateNonce,
},
success : function( response ) {
console.log( response );
if ( 0 === plugins.length ) {
$( '.deadlift-install-import-plugin-btn' ).text( wolfthemeAdmin.redirectingMessage );
dismiss_and_redirect();
}
},
error : function( xhr, ajaxOptions, thrownError ){
console.log(thrownError);
activatePlugins(Object.entries(wolfthemeAdmin.requiredPlugins)); // retry in case of error
}
})
.done(function (result) {
activatePlugins(plugins);
});
}
}
function dismiss_and_redirect() {
//console.log('redirecting');
//return;
$.ajax( {
type:'POST',
url: ajaxurl,
data: {
action: 'deadlift_plugin_install_dismiss_notice',
security: wolfthemeAdmin.dismissNonce
},
success : function( response ) {
console.log('Redirecting...');
setTimeout( function() {
window.location.href = wolfthemeAdmin.redirectUrl;
}, 1000 );
},
error: function( xhr, ajaxOptions, thrownError ){
console.log(thrownError);
}
} )
}
$( '.btn-get-ocdi-discard' ).on( 'click', function ( e ) {
e.preventDefault();
$.ajax( {
type:'POST',
url: ajaxurl,
data: {
action: 'deadlift_plugin_install_dismiss_notice',
security: wolfthemeAdmin.dismissNonce
},
success : function( response ) {
//console.log( response );
window.location.href = wolfthemeAdmin.adminUrl;
},
error: function( xhr, ajaxOptions, thrownError ){
console.log(thrownError);
}
} )
} );
} );