暂无描述

connect.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* globals wp_mail_smtp_connect */
  2. /**
  3. * Connect functionality - Upgrade plugin from Lite to Pro version.
  4. *
  5. * @since 2.6.0
  6. */
  7. 'use strict';
  8. var WPMailSMTPConnect = window.WPMailSMTPConnect || ( function( document, window, $ ) {
  9. /**
  10. * Elements reference.
  11. *
  12. * @since 2.6.0
  13. *
  14. * @type {object}
  15. */
  16. var el = {
  17. $connectBtn: $( '#wp-mail-smtp-setting-upgrade-license-button' ),
  18. $connectKey: $( '#wp-mail-smtp-setting-upgrade-license-key' )
  19. };
  20. /**
  21. * Public functions and properties.
  22. *
  23. * @since 2.6.0
  24. *
  25. * @type {object}
  26. */
  27. var app = {
  28. /**
  29. * Start the engine.
  30. *
  31. * @since 2.6.0
  32. */
  33. init: function() {
  34. $( app.ready );
  35. },
  36. /**
  37. * Document ready.
  38. *
  39. * @since 2.6.0
  40. */
  41. ready: function() {
  42. app.events();
  43. },
  44. /**
  45. * Register JS events.
  46. *
  47. * @since 2.6.0
  48. */
  49. events: function() {
  50. app.connectBtnClick();
  51. },
  52. /**
  53. * Register connect button event.
  54. *
  55. * @since 2.6.0
  56. */
  57. connectBtnClick: function() {
  58. el.$connectBtn.on( 'click', function() {
  59. app.gotoUpgradeUrl();
  60. } );
  61. },
  62. /**
  63. * Get the alert arguments in case of Pro already installed.
  64. *
  65. * @since 2.6.0
  66. *
  67. * @param {object} res Ajax query result object.
  68. *
  69. * @returns {object} Alert arguments.
  70. */
  71. proAlreadyInstalled: function( res ) {
  72. return {
  73. title: wp_mail_smtp_connect.text.almost_done,
  74. content: res.data.message,
  75. icon: '"></i><img src="' + wp_mail_smtp_connect.plugin_url + '/assets/images/font-awesome/check-circle-solid-green.svg" style="width: 40px; height: 40px;"><i class="',
  76. type: 'green',
  77. buttons: {
  78. confirm: {
  79. text: wp_mail_smtp_connect.text.plugin_activate_btn,
  80. btnClass: 'btn-confirm',
  81. keys: [ 'enter' ],
  82. action: function() {
  83. window.location.reload();
  84. },
  85. },
  86. },
  87. };
  88. },
  89. /**
  90. * Go to upgrade url.
  91. *
  92. * @since 2.6.0
  93. */
  94. gotoUpgradeUrl: function() {
  95. var data = {
  96. action: 'wp_mail_smtp_connect_url',
  97. key: el.$connectKey.val(),
  98. nonce: wp_mail_smtp_connect.nonce,
  99. };
  100. $.post( wp_mail_smtp_connect.ajax_url, data )
  101. .done( function( res ) {
  102. if ( res.success ) {
  103. if ( res.data.reload ) {
  104. $.alert( app.proAlreadyInstalled( res ) );
  105. return;
  106. }
  107. window.location.href = res.data.url;
  108. return;
  109. }
  110. $.alert( {
  111. title: wp_mail_smtp_connect.text.oops,
  112. content: res.data.message,
  113. icon: '"></i><img src="' + wp_mail_smtp_connect.plugin_url + '/assets/images/font-awesome/exclamation-circle-solid-orange.svg" style="width: 40px; height: 40px;"><i class="',
  114. type: 'orange',
  115. buttons: {
  116. confirm: {
  117. text: wp_mail_smtp_connect.text.ok,
  118. btnClass: 'btn-confirm',
  119. keys: [ 'enter' ],
  120. },
  121. },
  122. } );
  123. } )
  124. .fail( function( xhr ) {
  125. app.failAlert( xhr );
  126. } );
  127. },
  128. /**
  129. * Alert in case of server error.
  130. *
  131. * @since 2.6.0
  132. *
  133. * @param {object} xhr XHR object.
  134. */
  135. failAlert: function( xhr ) {
  136. $.alert( {
  137. title: wp_mail_smtp_connect.text.oops,
  138. content: wp_mail_smtp_connect.text.server_error + '<br>' + xhr.status + ' ' + xhr.statusText + ' ' + xhr.responseText,
  139. icon: '"></i><img src="' + wp_mail_smtp_connect.plugin_url + '/assets/images/font-awesome/exclamation-circle-regular-red.svg" style="width: 40px; height: 40px;"><i class="',
  140. type: 'red',
  141. buttons: {
  142. confirm: {
  143. text: wp_mail_smtp_connect.text.ok,
  144. btnClass: 'btn-confirm',
  145. keys: [ 'enter' ],
  146. },
  147. },
  148. } );
  149. },
  150. };
  151. // Provide access to public functions/properties.
  152. return app;
  153. }( document, window, jQuery ) );
  154. // Initialize.
  155. WPMailSMTPConnect.init();