Nav apraksta

smtp-admin.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* globals wp_mail_smtp, jconfirm, ajaxurl */
  2. 'use strict';
  3. var WPMailSMTP = window.WPMailSMTP || {};
  4. WPMailSMTP.Admin = WPMailSMTP.Admin || {};
  5. /**
  6. * WP Mail SMTP Admin area module.
  7. *
  8. * @since 1.6.0
  9. */
  10. WPMailSMTP.Admin.Settings = WPMailSMTP.Admin.Settings || ( function( document, window, $ ) {
  11. /**
  12. * Public functions and properties.
  13. *
  14. * @since 1.6.0
  15. *
  16. * @type {object}
  17. */
  18. var app = {
  19. /**
  20. * State attribute showing if one of the plugin settings
  21. * changed and was not yet saved.
  22. *
  23. * @since 1.9.0
  24. *
  25. * @type {boolean}
  26. */
  27. pluginSettingsChanged: false,
  28. /**
  29. * Start the engine. DOM is not ready yet, use only to init something.
  30. *
  31. * @since 1.6.0
  32. */
  33. init: function() {
  34. // Do that when DOM is ready.
  35. $( app.ready );
  36. },
  37. /**
  38. * DOM is fully loaded.
  39. *
  40. * @since 1.6.0
  41. */
  42. ready: function() {
  43. app.pageHolder = $( '.wp-mail-smtp-tab-settings' );
  44. // If there are screen options we have to move them.
  45. $( '#screen-meta-links, #screen-meta' ).prependTo( '#wp-mail-smtp-header-temp' ).show();
  46. app.bindActions();
  47. app.setJQueryConfirmDefaults();
  48. // Flyout Menu.
  49. app.initFlyoutMenu();
  50. },
  51. /**
  52. * Process all generic actions/events, mostly custom that were fired by our API.
  53. *
  54. * @since 1.6.0
  55. */
  56. bindActions: function() {
  57. // Mailer selection.
  58. $( '.wp-mail-smtp-mailer-image', app.pageHolder ).on( 'click', function() {
  59. $( this ).parents( '.wp-mail-smtp-mailer' ).find( 'input' ).trigger( 'click' );
  60. } );
  61. $( '.wp-mail-smtp-mailer input', app.pageHolder ).on( 'click', function() {
  62. var $input = $( this );
  63. if ( $input.prop( 'disabled' ) ) {
  64. // Educational Popup.
  65. if ( $input.hasClass( 'educate' ) ) {
  66. app.education.upgradeMailer( $input );
  67. }
  68. return false;
  69. }
  70. // Deselect the current mailer.
  71. $( '.wp-mail-smtp-mailer', app.pageHolder ).removeClass( 'active' );
  72. // Select the correct one.
  73. $( this ).parents( '.wp-mail-smtp-mailer' ).addClass( 'active' );
  74. // Hide all mailers options and display for a currently clicked one.
  75. $( '.wp-mail-smtp-mailer-option', app.pageHolder ).addClass( 'hidden' ).removeClass( 'active' );
  76. $( '.wp-mail-smtp-mailer-option-' + $( this ).val(), app.pageHolder ).addClass( 'active' ).removeClass( 'hidden' );
  77. } );
  78. app.mailers.smtp.bindActions();
  79. // Dismiss Pro banner at the bottom of the page.
  80. $( '#wp-mail-smtp-pro-banner-dismiss', app.pageHolder ).on( 'click', function() {
  81. $.ajax( {
  82. url: ajaxurl,
  83. dataType: 'json',
  84. type: 'POST',
  85. data: {
  86. action: 'wp_mail_smtp_ajax',
  87. task: 'pro_banner_dismiss'
  88. }
  89. } )
  90. .always( function() {
  91. $( '#wp-mail-smtp-pro-banner', app.pageHolder ).fadeOut( 'fast' );
  92. } );
  93. } );
  94. // Dissmis educational notices for certain mailers.
  95. $( '.js-wp-mail-smtp-mailer-notice-dismiss', app.pageHolder ).on( 'click', function( e ) {
  96. e.preventDefault();
  97. var $btn = $( this ),
  98. $notice = $btn.parents( '.inline-notice' );
  99. if ( $btn.hasClass( 'disabled' ) ) {
  100. return false;
  101. }
  102. $.ajax( {
  103. url: ajaxurl,
  104. dataType: 'json',
  105. type: 'POST',
  106. data: {
  107. action: 'wp_mail_smtp_ajax',
  108. task: 'notice_dismiss',
  109. notice: $notice.data( 'notice' ),
  110. mailer: $notice.data( 'mailer' )
  111. },
  112. beforeSend: function() {
  113. $btn.addClass( 'disabled' );
  114. }
  115. } )
  116. .always( function() {
  117. $notice.fadeOut( 'fast', function() {
  118. $btn.removeClass( 'disabled' );
  119. } );
  120. } );
  121. } );
  122. // Show/hide debug output.
  123. $( '#wp-mail-smtp-debug .error-log-toggle' ).on( 'click', function( e ) {
  124. e.preventDefault();
  125. $( '#wp-mail-smtp-debug .error-log' ).slideToggle();
  126. } );
  127. // Copy debug output to clipboard.
  128. $( '#wp-mail-smtp-debug .error-log-copy' ).on( 'click', function( e ) {
  129. e.preventDefault();
  130. var $self = $( this );
  131. // Get error log.
  132. var $content = $( '#wp-mail-smtp-debug .error-log' );
  133. // Copy to clipboard.
  134. if ( ! $content.is( ':visible' ) ) {
  135. $content.addClass( 'error-log-selection' );
  136. }
  137. var range = document.createRange();
  138. range.selectNode( $content[0] );
  139. window.getSelection().removeAllRanges();
  140. window.getSelection().addRange( range );
  141. document.execCommand( 'Copy' );
  142. window.getSelection().removeAllRanges();
  143. $content.removeClass( 'error-log-selection' );
  144. $self.addClass( 'error-log-copy-copied' );
  145. setTimeout(
  146. function() {
  147. $self.removeClass( 'error-log-copy-copied' );
  148. },
  149. 1500
  150. );
  151. } );
  152. // Remove mailer connection.
  153. $( '.js-wp-mail-smtp-provider-remove', app.pageHolder ).on( 'click', function() {
  154. return confirm( wp_mail_smtp.text_provider_remove );
  155. } );
  156. // Copy input text to clipboard.
  157. $( '.wp-mail-smtp-setting-copy', app.pageHolder ).on( 'click', function( e ) {
  158. e.preventDefault();
  159. var target = $( '#' + $( this ).data( 'source_id' ) ).get( 0 );
  160. target.select();
  161. document.execCommand( 'Copy' );
  162. var $buttonIcon = $( this ).find( '.dashicons' );
  163. $buttonIcon
  164. .removeClass( 'dashicons-admin-page' )
  165. .addClass( 'wp-mail-smtp-dashicons-yes-alt-green wp-mail-smtp-success wp-mail-smtp-animate' );
  166. setTimeout(
  167. function() {
  168. $buttonIcon
  169. .removeClass( 'wp-mail-smtp-dashicons-yes-alt-green wp-mail-smtp-success wp-mail-smtp-animate' )
  170. .addClass( 'dashicons-admin-page' );
  171. },
  172. 1000
  173. );
  174. } );
  175. // Notice bar: click on the dissmiss button.
  176. $( '#wp-mail-smtp-notice-bar' ).on( 'click', '.dismiss', function() {
  177. var $notice = $( this ).closest( '#wp-mail-smtp-notice-bar' );
  178. $notice.addClass( 'out' );
  179. setTimeout(
  180. function() {
  181. $notice.remove();
  182. },
  183. 300
  184. );
  185. $.post(
  186. ajaxurl,
  187. {
  188. action: 'wp_mail_smtp_notice_bar_dismiss',
  189. nonce: wp_mail_smtp.nonce,
  190. }
  191. );
  192. } );
  193. app.triggerExitNotice();
  194. app.beforeSaveChecks();
  195. // Register change event to show/hide plugin supported settings for currently selected mailer.
  196. $( '.js-wp-mail-smtp-setting-mailer-radio-input', app.pageHolder ).on( 'change', this.processMailerSettingsOnChange );
  197. // Disable multiple click on the Email Test tab submit button and display a loader icon.
  198. $( '.wp-mail-smtp-tab-tools-test #email-test-form' ).on( 'submit', function() {
  199. var $button = $( this ).find( '.wp-mail-smtp-btn' );
  200. $button.attr( 'disabled', true );
  201. $button.find( 'span' ).hide();
  202. $button.find( '.wp-mail-smtp-loading' ).show();
  203. } );
  204. },
  205. education: {
  206. upgradeMailer: function( $input ) {
  207. $.alert( {
  208. backgroundDismiss: true,
  209. escapeKey: true,
  210. animationBounce: 1,
  211. type: 'blue',
  212. closeIcon: true,
  213. title: wp_mail_smtp.education.upgrade_title.replace( /%name%/g, $input.siblings( 'label' ).text().trim() ),
  214. icon: '"></i>' + wp_mail_smtp.education.upgrade_icon_lock + '<i class="',
  215. content: $( '.wp-mail-smtp-mailer-options .wp-mail-smtp-mailer-option-' + $input.val() + ' .wp-mail-smtp-setting-field' ).html(),
  216. boxWidth: '550px',
  217. onOpenBefore: function() {
  218. this.$btnc.after( '<div class="discount-note">' + wp_mail_smtp.education.upgrade_bonus + wp_mail_smtp.education.upgrade_doc + '</div>' );
  219. this.$body.addClass( 'wp-mail-smtp-upgrade-mailer-education-modal' );
  220. },
  221. buttons: {
  222. confirm: {
  223. text: wp_mail_smtp.education.upgrade_button,
  224. btnClass: 'btn-confirm',
  225. keys: [ 'enter' ],
  226. action: function() {
  227. var appendChar = /(\?)/.test( wp_mail_smtp.education.upgrade_url ) ? '&' : '?',
  228. upgradeURL = wp_mail_smtp.education.upgrade_url + appendChar + 'utm_content=' + encodeURIComponent( $input.val() );
  229. window.open( upgradeURL, '_blank' );
  230. }
  231. }
  232. }
  233. } );
  234. }
  235. },
  236. /**
  237. * Individual mailers specific js code.
  238. *
  239. * @since 1.6.0
  240. */
  241. mailers: {
  242. smtp: {
  243. bindActions: function() {
  244. // Hide SMTP-specific user/pass when Auth disabled.
  245. $( '#wp-mail-smtp-setting-smtp-auth' ).on( 'change', function() {
  246. $( '#wp-mail-smtp-setting-row-smtp-user, #wp-mail-smtp-setting-row-smtp-pass' ).toggleClass( 'inactive' );
  247. } );
  248. // Port default values based on encryption type.
  249. $( '#wp-mail-smtp-setting-row-smtp-encryption input' ).on( 'change', function() {
  250. var $input = $( this ),
  251. $smtpPort = $( '#wp-mail-smtp-setting-smtp-port', app.pageHolder );
  252. if ( 'tls' === $input.val() ) {
  253. $smtpPort.val( '587' );
  254. $( '#wp-mail-smtp-setting-row-smtp-autotls' ).addClass( 'inactive' );
  255. } else if ( 'ssl' === $input.val() ) {
  256. $smtpPort.val( '465' );
  257. $( '#wp-mail-smtp-setting-row-smtp-autotls' ).removeClass( 'inactive' );
  258. } else {
  259. $smtpPort.val( '25' );
  260. $( '#wp-mail-smtp-setting-row-smtp-autotls' ).removeClass( 'inactive' );
  261. }
  262. } );
  263. }
  264. }
  265. },
  266. /**
  267. * Exit notice JS code when plugin settings are not saved.
  268. *
  269. * @since 1.9.0
  270. */
  271. triggerExitNotice: function() {
  272. var $settingPages = $( '.wp-mail-smtp-page-general' );
  273. // Display an exit notice, if settings are not saved.
  274. $( window ).on( 'beforeunload', function() {
  275. if ( app.pluginSettingsChanged ) {
  276. return wp_mail_smtp.text_settings_not_saved;
  277. }
  278. } );
  279. // Set settings changed attribute, if any input was changed.
  280. $( ':input:not( #wp-mail-smtp-setting-license-key, .wp-mail-smtp-not-form-input )', $settingPages ).on( 'change', function() {
  281. app.pluginSettingsChanged = true;
  282. } );
  283. // Clear the settings changed attribute, if the settings are about to be saved.
  284. $( 'form', $settingPages ).on( 'submit', function() {
  285. app.pluginSettingsChanged = false;
  286. } );
  287. },
  288. /**
  289. * Perform any checks before the settings are saved.
  290. *
  291. * Checks:
  292. * - warn users if they try to save the settings with the default (PHP) mailer selected.
  293. *
  294. * @since 2.1.0
  295. */
  296. beforeSaveChecks: function() {
  297. $( 'form', app.pageHolder ).on( 'submit', function() {
  298. if ( $( '.wp-mail-smtp-mailer input:checked', app.pageHolder ).val() === 'mail' ) {
  299. var $thisForm = $( this );
  300. $.alert( {
  301. backgroundDismiss: false,
  302. escapeKey: false,
  303. animationBounce: 1,
  304. type: 'orange',
  305. icon: '"></i><img src="' + wp_mail_smtp.plugin_url + '/assets/images/font-awesome/exclamation-circle-solid-orange.svg" style="width: 40px; height: 40px;" alt="' + wp_mail_smtp.default_mailer_notice.icon_alt + '"><i class="',
  306. title: wp_mail_smtp.default_mailer_notice.title,
  307. content: wp_mail_smtp.default_mailer_notice.content,
  308. boxWidth: '550px',
  309. buttons: {
  310. confirm: {
  311. text: wp_mail_smtp.default_mailer_notice.save_button,
  312. btnClass: 'btn-confirm',
  313. keys: [ 'enter' ],
  314. action: function() {
  315. $thisForm.off( 'submit' ).trigger( 'submit' );
  316. }
  317. },
  318. cancel: {
  319. text: wp_mail_smtp.default_mailer_notice.cancel_button,
  320. btnClass: 'btn-cancel',
  321. },
  322. }
  323. } );
  324. return false;
  325. }
  326. } );
  327. },
  328. /**
  329. * On change callback for showing/hiding plugin supported settings for currently selected mailer.
  330. *
  331. * @since 2.3.0
  332. */
  333. processMailerSettingsOnChange: function() {
  334. var mailerSupportedSettings = wp_mail_smtp.all_mailers_supports[ $( this ).val() ];
  335. for ( var setting in mailerSupportedSettings ) {
  336. // eslint-disable-next-line no-prototype-builtins
  337. if ( mailerSupportedSettings.hasOwnProperty( setting ) ) {
  338. $( '.js-wp-mail-smtp-setting-' + setting, app.pageHolder ).toggle( mailerSupportedSettings[ setting ] );
  339. }
  340. }
  341. // Special case: "from email" (group settings).
  342. var $mainSettingInGroup = $( '.js-wp-mail-smtp-setting-from_email' );
  343. $mainSettingInGroup.closest( '.wp-mail-smtp-setting-row' ).toggle(
  344. mailerSupportedSettings['from_email'] || mailerSupportedSettings['from_email_force']
  345. );
  346. $mainSettingInGroup.siblings( '.wp-mail-smtp-setting-mid-row-sep' ).toggle(
  347. mailerSupportedSettings['from_email'] && mailerSupportedSettings['from_email_force']
  348. );
  349. // Special case: "from name" (group settings).
  350. $mainSettingInGroup = $( '.js-wp-mail-smtp-setting-from_name' );
  351. $mainSettingInGroup.closest( '.wp-mail-smtp-setting-row' ).toggle(
  352. mailerSupportedSettings['from_name'] || mailerSupportedSettings['from_name_force']
  353. );
  354. $mainSettingInGroup.siblings( '.wp-mail-smtp-setting-mid-row-sep' ).toggle(
  355. mailerSupportedSettings['from_name'] && mailerSupportedSettings['from_name_force']
  356. );
  357. },
  358. /**
  359. * Set jQuery-Confirm default options.
  360. *
  361. * @since 2.9.0
  362. */
  363. setJQueryConfirmDefaults: function() {
  364. jconfirm.defaults = {
  365. typeAnimated: false,
  366. draggable: false,
  367. animateFromElement: false,
  368. theme: 'modern',
  369. boxWidth: '400px',
  370. useBootstrap: false
  371. };
  372. },
  373. /**
  374. * Flyout Menu (quick links).
  375. *
  376. * @since 3.0.0
  377. */
  378. initFlyoutMenu: function() {
  379. // Flyout Menu Elements.
  380. var $flyoutMenu = $( '#wp-mail-smtp-flyout' );
  381. if ( $flyoutMenu.length === 0 ) {
  382. return;
  383. }
  384. var $head = $flyoutMenu.find( '.wp-mail-smtp-flyout-head' );
  385. // Click on the menu head icon.
  386. $head.on( 'click', function( e ) {
  387. e.preventDefault();
  388. $flyoutMenu.toggleClass( 'opened' );
  389. } );
  390. // Page elements and other values.
  391. var $wpfooter = $( '#wpfooter' );
  392. if ( $wpfooter.length === 0 ) {
  393. return;
  394. }
  395. var $overlap = $( '.wp-mail-smtp-page-logs-archive, .wp-mail-smtp-tab-tools-action-scheduler, .wp-mail-smtp-page-reports, .wp-mail-smtp-tab-tools-debug-events' ),
  396. wpfooterTop = $wpfooter.offset().top,
  397. wpfooterBottom = wpfooterTop + $wpfooter.height(),
  398. overlapBottom = $overlap.length > 0 ? $overlap.offset().top + $overlap.height() + 85 : 0;
  399. // Hide menu if scrolled down to the bottom of the page or overlap some critical controls.
  400. $( window ).on( 'resize scroll', _.debounce( function() {
  401. var viewTop = $( window ).scrollTop(),
  402. viewBottom = viewTop + $( window ).height();
  403. if ( wpfooterBottom <= viewBottom && wpfooterTop >= viewTop && overlapBottom > viewBottom ) {
  404. $flyoutMenu.addClass( 'out' );
  405. } else {
  406. $flyoutMenu.removeClass( 'out' );
  407. }
  408. }, 50 ) );
  409. $( window ).trigger( 'scroll' );
  410. }
  411. };
  412. // Provide access to public functions/properties.
  413. return app;
  414. }( document, window, jQuery ) );
  415. // Initialize.
  416. WPMailSMTP.Admin.Settings.init();