Ei kuvausta

smtp-about.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* eslint-disable no-prototype-builtins */
  2. /* global wp_mail_smtp_about */
  3. 'use strict';
  4. var WPMailSMTP = window.WPMailSMTP || {};
  5. WPMailSMTP.Admin = WPMailSMTP.Admin || {};
  6. /**
  7. * WP Mail SMTP Admin area About module.
  8. *
  9. * @since 1.5.0
  10. */
  11. WPMailSMTP.Admin.About = WPMailSMTP.Admin.About || ( function( document, window, $ ) {
  12. /**
  13. * Public functions and properties.
  14. *
  15. * @since 1.5.0
  16. *
  17. * @type {object}
  18. */
  19. var app = {
  20. /**
  21. * Start the engine. DOM is not ready yet, use only to init something.
  22. *
  23. * @since 1.5.0
  24. */
  25. init: function() {
  26. // Do that when DOM is ready.
  27. $( app.ready );
  28. },
  29. /**
  30. * DOM is fully loaded.
  31. *
  32. * @since 1.5.0
  33. */
  34. ready: function() {
  35. app.pageHolder = $( '.wp-mail-smtp-page-about' );
  36. app.bindActions();
  37. $( '.wp-mail-smtp-page' ).trigger( 'WPMailSMTP.Admin.About.ready' );
  38. },
  39. /**
  40. * Process all generic actions/events, mostly custom that were fired by our API.
  41. *
  42. * @since 1.5.0
  43. */
  44. bindActions: function() {
  45. /*
  46. * Make plugins description the same height.
  47. */
  48. jQuery( '.wp-mail-smtp-admin-about-plugins .plugin-item .details' ).matchHeight();
  49. /*
  50. * Install/Active the plugins.
  51. */
  52. $( document ).on( 'click', '.wp-mail-smtp-admin-about-plugins .plugin-item .action-button .button', function( e ) {
  53. e.preventDefault();
  54. var $btn = $( this );
  55. if ( $btn.hasClass( 'disabled' ) || $btn.hasClass( 'loading' ) ) {
  56. return false;
  57. }
  58. var $plugin = $btn.closest( '.plugin-item' ),
  59. plugin = $btn.attr( 'data-plugin' ),
  60. task,
  61. cssClass,
  62. statusText,
  63. buttonText,
  64. successText;
  65. $btn.addClass( 'loading disabled' );
  66. $btn.text( wp_mail_smtp_about.plugin_processing );
  67. if ( $btn.hasClass( 'status-inactive' ) ) {
  68. // Activate.
  69. task = 'about_plugin_activate';
  70. cssClass = 'status-active button button-secondary disabled';
  71. statusText = wp_mail_smtp_about.plugin_active;
  72. buttonText = wp_mail_smtp_about.plugin_activated;
  73. } else if ( $btn.hasClass( 'status-download' ) ) {
  74. // Install & Activate.
  75. task = 'about_plugin_install';
  76. cssClass = 'status-active button disabled';
  77. statusText = wp_mail_smtp_about.plugin_active;
  78. buttonText = wp_mail_smtp_about.plugin_activated;
  79. } else {
  80. return;
  81. }
  82. // Setup ajax POST data.
  83. var data = {
  84. action: 'wp_mail_smtp_ajax',
  85. task: task,
  86. nonce : wp_mail_smtp_about.nonce,
  87. plugin: plugin
  88. };
  89. $.post( wp_mail_smtp_about.ajax_url, data, function( res ) {
  90. var isInstallSuccessful;
  91. if ( res.success ) {
  92. isInstallSuccessful = true;
  93. if ( 'about_plugin_install' === task ) {
  94. $btn.attr( 'data-plugin', res.data.basename );
  95. successText = res.data.msg;
  96. if ( ! res.data.is_activated ) {
  97. cssClass = 'button';
  98. statusText = wp_mail_smtp_about.plugin_inactive;
  99. buttonText = wp_mail_smtp_about.plugin_activate;
  100. }
  101. } else {
  102. successText = res.data;
  103. }
  104. $plugin.find( '.actions' ).append( '<div class="msg success">' + successText + '</div>' );
  105. $plugin.find( 'span.status-label' )
  106. .removeClass( 'status-active status-inactive status-download' )
  107. .addClass( cssClass )
  108. .removeClass( 'button button-primary button-secondary disabled' )
  109. .text( statusText );
  110. $btn
  111. .removeClass( 'status-active status-inactive status-download' )
  112. .removeClass( 'button button-primary button-secondary disabled' )
  113. .addClass( cssClass ).html( buttonText );
  114. } else {
  115. isInstallSuccessful = false;
  116. if (
  117. res.hasOwnProperty( 'data' ) &&
  118. res.data.hasOwnProperty( 0 ) &&
  119. res.data[ 0 ].hasOwnProperty( 'code' ) &&
  120. res.data[ 0 ].code === 'download_failed'
  121. ) {
  122. // Specific server-returned error.
  123. $plugin.find( '.actions' ).append( '<div class="msg error">' + wp_mail_smtp_about.plugin_install_error + '</div>' );
  124. } else {
  125. // Generic error.
  126. $plugin.find( '.actions' ).append( '<div class="msg error">' + res.data + '</div>' );
  127. }
  128. $btn.html( wp_mail_smtp_about.plugin_download_btn );
  129. }
  130. if ( ! isInstallSuccessful ) {
  131. $btn.removeClass( 'disabled' );
  132. }
  133. $btn.removeClass( 'loading' );
  134. // Automatically clear plugin messages after 3 seconds.
  135. setTimeout( function() {
  136. $( '.plugin-item .msg' ).remove();
  137. }, 3000 );
  138. } ).fail( function( xhr ) {
  139. console.log( xhr.responseText );
  140. } );
  141. } );
  142. }
  143. };
  144. // Provide access to public functions/properties.
  145. return app;
  146. }( document, window, jQuery ) );
  147. // Initialize.
  148. WPMailSMTP.Admin.About.init();