laipower/wp-content/plugins/wp-mail-smtp/assets/js/smtp-about.js

182 lines
4.6 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-prototype-builtins */
/* global wp_mail_smtp_about */
'use strict';
2020-04-07 13:03:04 +00:00
var WPMailSMTP = window.WPMailSMTP || {};
WPMailSMTP.Admin = WPMailSMTP.Admin || {};
/**
* WP Mail SMTP Admin area About module.
*
* @since 1.5.0
*/
WPMailSMTP.Admin.About = WPMailSMTP.Admin.About || ( function( document, window, $ ) {
2020-04-07 13:03:04 +00:00
/**
* Public functions and properties.
*
* @since 1.5.0
*
* @type {object}
2020-04-07 13:03:04 +00:00
*/
var app = {
/**
* Start the engine. DOM is not ready yet, use only to init something.
*
* @since 1.5.0
*/
init: function() {
2020-04-07 13:03:04 +00:00
// Do that when DOM is ready.
$( document ).ready( app.ready );
},
/**
* DOM is fully loaded.
*
* @since 1.5.0
*/
ready: function() {
2020-04-07 13:03:04 +00:00
app.pageHolder = $( '.wp-mail-smtp-page-about' );
app.bindActions();
$( '.wp-mail-smtp-page' ).trigger( 'WPMailSMTP.Admin.About.ready' );
},
/**
* Process all generic actions/events, mostly custom that were fired by our API.
*
* @since 1.5.0
*/
bindActions: function() {
2020-04-07 13:03:04 +00:00
/*
* Make plugins description the same height.
*/
jQuery( '.wp-mail-smtp-admin-about-plugins .plugin-item .details' ).matchHeight();
2020-04-07 13:03:04 +00:00
/*
* Install/Active the plugins.
*/
$( document ).on( 'click', '.wp-mail-smtp-admin-about-plugins .plugin-item .action-button .button', function( e ) {
e.preventDefault();
var $btn = $( this );
if ( $btn.hasClass( 'disabled' ) || $btn.hasClass( 'loading' ) ) {
return false;
}
var $plugin = $btn.closest( '.plugin-item' ),
plugin = $btn.attr( 'data-plugin' ),
task,
cssClass,
statusText,
buttonText,
successText;
$btn.addClass( 'loading disabled' );
$btn.text( wp_mail_smtp_about.plugin_processing );
if ( $btn.hasClass( 'status-inactive' ) ) {
2020-04-07 13:03:04 +00:00
// Activate.
task = 'about_plugin_activate';
cssClass = 'status-active button button-secondary disabled';
statusText = wp_mail_smtp_about.plugin_active;
buttonText = wp_mail_smtp_about.plugin_activated;
} else if ( $btn.hasClass( 'status-download' ) ) {
2020-04-07 13:03:04 +00:00
// Install & Activate.
task = 'about_plugin_install';
cssClass = 'status-active button disabled';
statusText = wp_mail_smtp_about.plugin_active;
buttonText = wp_mail_smtp_about.plugin_activated;
} else {
return;
}
// Setup ajax POST data.
var data = {
action: 'wp_mail_smtp_ajax',
task: task,
nonce : wp_mail_smtp_about.nonce,
plugin: plugin
};
$.post( wp_mail_smtp_about.ajax_url, data, function( res ) {
var isInstallSuccessful;
2020-04-07 13:03:04 +00:00
if ( res.success ) {
isInstallSuccessful = true;
2020-04-07 13:03:04 +00:00
if ( 'about_plugin_install' === task ) {
$btn.attr( 'data-plugin', res.data.basename );
successText = res.data.msg;
if ( ! res.data.is_activated ) {
cssClass = 'button';
statusText = wp_mail_smtp_about.plugin_inactive;
buttonText = wp_mail_smtp_about.plugin_activate;
}
} else {
successText = res.data;
}
$plugin.find( '.actions' ).append( '<div class="msg success">' + successText + '</div>' );
2020-04-07 13:03:04 +00:00
$plugin.find( 'span.status-label' )
.removeClass( 'status-active status-inactive status-download' )
.addClass( cssClass )
.removeClass( 'button button-primary button-secondary disabled' )
.text( statusText );
2020-04-07 13:03:04 +00:00
$btn
.removeClass( 'status-active status-inactive status-download' )
.removeClass( 'button button-primary button-secondary disabled' )
.addClass( cssClass ).html( buttonText );
} else {
isInstallSuccessful = false;
2020-04-07 13:03:04 +00:00
if (
res.hasOwnProperty( 'data' ) &&
res.data.hasOwnProperty( 0 ) &&
res.data[ 0 ].hasOwnProperty( 'code' ) &&
res.data[ 0 ].code === 'download_failed'
) {
2020-04-07 13:03:04 +00:00
// Specific server-returned error.
$plugin.find( '.actions' ).append( '<div class="msg error">' + wp_mail_smtp_about.plugin_install_error + '</div>' );
} else {
2020-04-07 13:03:04 +00:00
// Generic error.
$plugin.find( '.actions' ).append( '<div class="msg error">' + res.data + '</div>' );
}
$btn.html( wp_mail_smtp_about.plugin_download_btn );
}
if ( ! isInstallSuccessful ) {
2020-04-07 13:03:04 +00:00
$btn.removeClass( 'disabled' );
}
$btn.removeClass( 'loading' );
// Automatically clear plugin messages after 3 seconds.
setTimeout( function() {
2020-04-07 13:03:04 +00:00
$( '.plugin-item .msg' ).remove();
}, 3000 );
} ).fail( function( xhr ) {
2020-04-07 13:03:04 +00:00
console.log( xhr.responseText );
} );
} );
2020-04-07 13:03:04 +00:00
}
};
// Provide access to public functions/properties.
return app;
}( document, window, jQuery ) );
2020-04-07 13:03:04 +00:00
// Initialize.
WPMailSMTP.Admin.About.init();