/* globals wp_mail_smtp, jconfirm, ajaxurl */ 'use strict'; var WPMailSMTP = window.WPMailSMTP || {}; WPMailSMTP.Admin = WPMailSMTP.Admin || {}; /** * WP Mail SMTP Admin area module. * * @since 1.6.0 */ WPMailSMTP.Admin.Settings = WPMailSMTP.Admin.Settings || ( function( document, window, $ ) { /** * Public functions and properties. * * @since 1.6.0 * * @type {object} */ var app = { /** * State attribute showing if one of the plugin settings * changed and was not yet saved. * * @since 1.9.0 * * @type {boolean} */ pluginSettingsChanged: false, /** * Start the engine. DOM is not ready yet, use only to init something. * * @since 1.6.0 */ init: function() { // Do that when DOM is ready. $( app.ready ); }, /** * DOM is fully loaded. * * @since 1.6.0 */ ready: function() { app.pageHolder = $( '.wp-mail-smtp-tab-settings' ); app.settingsForm = $( '.wp-mail-smtp-connection-settings-form' ); // If there are screen options we have to move them. $( '#screen-meta-links, #screen-meta' ).prependTo( '#wp-mail-smtp-header-temp' ).show(); app.bindActions(); var removableQueryParams = [ 'sendlayer_quick_connect_result', 'sendlayer_quick_connect_disconnect_result' ]; if ( ! $( '.wp-mail-smtp-tab-tools-debug-events' ).length ) { removableQueryParams.push( 'debug_event_id' ); } app.cleanQueryParams( removableQueryParams ); app.setJQueryConfirmDefaults(); // Flyout Menu. app.initFlyoutMenu(); }, /** * Process all generic actions/events, mostly custom that were fired by our API. * * @since 1.6.0 */ bindActions: function() { // Mailer selection. $( '.wp-mail-smtp-mailer-image', app.settingsForm ).on( 'click', function() { $( this ).parents( '.wp-mail-smtp-mailer' ).find( 'input' ).trigger( 'click' ); } ); $( '.wp-mail-smtp-mailer input', app.settingsForm ).on( 'click', function() { var $input = $( this ); if ( $input.prop( 'disabled' ) ) { // Educational Popup. if ( $input.hasClass( 'educate' ) ) { app.education.upgradeMailer( $input ); } return false; } // Deselect the current mailer. $( '.wp-mail-smtp-mailer', app.settingsForm ).removeClass( 'active' ); // Select the correct one. $( this ).parents( '.wp-mail-smtp-mailer' ).addClass( 'active' ); // Hide all mailers options and display for a currently clicked one. $( '.wp-mail-smtp-mailer-option', app.settingsForm ).addClass( 'hidden' ).removeClass( 'active' ); $( '.wp-mail-smtp-mailer-option-' + $( this ).val(), app.settingsForm ).addClass( 'active' ).removeClass( 'hidden' ); } ); app.mailers.sendlayer.bindActions(); app.mailers.smtp.bindActions(); // Dismiss Pro banner at the bottom of the page. $( '#wp-mail-smtp-pro-banner-dismiss', app.pageHolder ).on( 'click', function() { $.ajax( { url: ajaxurl, dataType: 'json', type: 'POST', data: { action: 'wp_mail_smtp_ajax', task: 'pro_banner_dismiss', nonce: wp_mail_smtp.nonce } } ) .always( function() { $( '#wp-mail-smtp-pro-banner', app.pageHolder ).fadeOut( 'fast' ); } ); } ); // Dissmis educational notices for certain mailers. $( '.js-wp-mail-smtp-mailer-notice-dismiss', app.settingsForm ).on( 'click', function( e ) { e.preventDefault(); var $btn = $( this ), $notice = $btn.parents( '.inline-notice' ); if ( $btn.hasClass( 'disabled' ) ) { return false; } $.ajax( { url: ajaxurl, dataType: 'json', type: 'POST', data: { action: 'wp_mail_smtp_ajax', nonce: wp_mail_smtp.nonce, task: 'notice_dismiss', notice: $notice.data( 'notice' ), mailer: $notice.data( 'mailer' ) }, beforeSend: function() { $btn.addClass( 'disabled' ); } } ) .always( function() { $notice.fadeOut( 'fast', function() { $btn.removeClass( 'disabled' ); } ); } ); } ); // Show/hide debug output. $( '#wp-mail-smtp-debug .error-log-toggle' ).on( 'click', function( e ) { e.preventDefault(); $( '#wp-mail-smtp-debug .error-log' ).slideToggle(); } ); // Copy debug output to clipboard. $( '#wp-mail-smtp-debug .error-log-copy' ).on( 'click', function( e ) { e.preventDefault(); var $self = $( this ); // Get error log. var $content = $( '#wp-mail-smtp-debug .error-log' ); // Copy to clipboard. if ( ! $content.is( ':visible' ) ) { $content.addClass( 'error-log-selection' ); } var range = document.createRange(); range.selectNode( $content[0] ); window.getSelection().removeAllRanges(); window.getSelection().addRange( range ); document.execCommand( 'Copy' ); window.getSelection().removeAllRanges(); $content.removeClass( 'error-log-selection' ); $self.addClass( 'error-log-copy-copied' ); setTimeout( function() { $self.removeClass( 'error-log-copy-copied' ); }, 1500 ); } ); // Remove mailer connection. $( '.js-wp-mail-smtp-provider-remove', app.settingsForm ).on( 'click', function() { return confirm( wp_mail_smtp.text_provider_remove ); } ); // Copy input text to clipboard. $( '.wp-mail-smtp-setting-copy', app.settingsForm ).on( 'click', function( e ) { e.preventDefault(); var target = $( '#' + $( this ).data( 'source_id' ) ).get( 0 ); target.select(); document.execCommand( 'Copy' ); var $buttonIcon = $( this ).find( '.dashicons' ); $buttonIcon .removeClass( 'dashicons-admin-page' ) .addClass( 'wp-mail-smtp-dashicons-yes-alt-green wp-mail-smtp-success wp-mail-smtp-animate' ); setTimeout( function() { $buttonIcon .removeClass( 'wp-mail-smtp-dashicons-yes-alt-green wp-mail-smtp-success wp-mail-smtp-animate' ) .addClass( 'dashicons-admin-page' ); }, 1000 ); } ); // Notice bar: click on the dissmiss button. $( '#wp-mail-smtp-notice-bar' ).on( 'click', '.dismiss', function() { var $notice = $( this ).closest( '#wp-mail-smtp-notice-bar' ); $notice.addClass( 'out' ); setTimeout( function() { $notice.remove(); }, 300 ); $.post( ajaxurl, { action: 'wp_mail_smtp_notice_bar_dismiss', nonce: wp_mail_smtp.nonce, } ); } ); app.triggerExitNotice(); app.beforeSaveChecks(); // Register change event to show/hide plugin supported settings for currently selected mailer. $( '.js-wp-mail-smtp-setting-mailer-radio-input', app.settingsForm ).on( 'change', this.processMailerSettingsOnChange ); // Disable multiple click on the Email Test tab submit button and display a loader icon. $( '.wp-mail-smtp-tab-tools-test #email-test-form' ).on( 'submit', function() { var $button = $( this ).find( '.wp-mail-smtp-btn' ); $button.attr( 'disabled', true ); $button.find( 'span' ).hide(); $button.find( '.wp-mail-smtp-loading' ).show(); } ); $( '#wp-mail-smtp-setting-gmail-one_click_setup_enabled-lite' ).on( 'click', function( e ) { e.preventDefault(); app.education.gmailOneClickSetupUpgrade(); } ); $( '#wp-mail-smtp-setting-misc-rate_limit-lite' ).on( 'click', function( e ) { e.preventDefault(); app.education.rateLimitUpgrade(); } ); // Confirm before enabling the "Hide Email Delivery Errors" option. $( '#wp-mail-smtp-setting-email_delivery_errors_hidden' ).on( 'change', app.confirmHideDeliveryErrors ); // Obfuscated fields $( '.wp-mail-smtp-btn[data-clear-field]' ).on( 'click', function( e ) { var $button = $( this ); var fieldId = $button.attr( 'data-clear-field' ); var $field = $( `#${fieldId}` ); $field.prop( 'disabled', false ); $field.attr( 'name', $field.attr( 'data-name' ) ); $field.removeAttr( 'value' ); $field.focus(); $button.remove(); } ); $( '.email_test_tab_removal_notice' ).on( 'click', '.notice-dismiss', function() { var $button = $( this ); $.ajax( { url: ajaxurl, dataType: 'json', type: 'POST', data: { action: 'wp_mail_smtp_ajax', nonce: wp_mail_smtp.nonce, task: 'email_test_tab_removal_notice_dismiss', }, beforeSend: function() { $button.prop( 'disabled', true ); }, } ); } ); // Banner: toggle the inline error-log panel. $( document ).on( 'click', '.wpms-email-sending-errors-banner__error-log-toggle', function( e ) { e.preventDefault(); var $btn = $( this ); var $panel = $btn.closest( '.wpms-email-sending-errors-banner' ) .find( '.wpms-email-sending-errors-banner__error-log' ); if ( ! $panel.length ) { return; } var showLabel = $btn.data( 'show-label' ); var hideLabel = $btn.data( 'hide-label' ); var isHidden = $panel.prop( 'hidden' ) || ! $panel.is( ':visible' ); if ( isHidden ) { $panel.prop( 'hidden', false ).hide().slideDown( 200 ); $btn.text( hideLabel ); } else { $panel.slideUp( 200, function() { $panel.prop( 'hidden', true ); } ); $btn.text( showLabel ); } } ); // Banner: copy the inline error-log panel contents to clipboard. $( document ).on( 'click', '.wpms-email-sending-errors-error-log__copy-icon', function( e ) { e.preventDefault(); var $btn = $( this ); var $panel = $btn.closest( '.wpms-email-sending-errors-banner__error-log' ); var $default = $btn.find( '.wpms-email-sending-errors-error-log__copy-icon-default' ); var $done = $btn.find( '.wpms-email-sending-errors-error-log__copy-icon-done' ); var $tooltip = $panel.find( '.wpms-email-sending-errors-error-log__copy-tooltip' ); if ( ! $panel.length ) { return; } var $content = $panel.clone(); $content.find( '.wpms-email-sending-errors-error-log__copy-icon, .wpms-email-sending-errors-error-log__copy-tooltip' ).remove(); $content.find( 'br' ).replaceWith( '\n' ); var text = $content.text().trim(); var afterCopy = function() { $default.prop( 'hidden', true ); $done.prop( 'hidden', false ); $tooltip.prop( 'hidden', false ); setTimeout( function() { $default.prop( 'hidden', false ); $done.prop( 'hidden', true ); $tooltip.prop( 'hidden', true ); }, 2000 ); }; var copyViaExecCommand = function() { var $tmp = $( '