admin-settings.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. jQuery(document).ready(function($) {
  2. var working = false;
  3. var message, container, messageBox, deleteInput, deleteMsgs, buttons, confirm, cancel, lineBreak;
  4. container = document.createElement( 'div' );
  5. messageBox = document.createElement( 'p' );
  6. deleteInput = document.createElement( 'input' );
  7. deleteInput.type = 'text';
  8. deleteInput.id = 'confirmDeleteInput';
  9. buttons = document.createElement( 'div' );
  10. buttons.style.marginTop = '10px';
  11. buttons.style.backgroundColor = '#f4f5f6';
  12. confirm = document.createElement( 'div' );
  13. confirm.style.padding = '8px';
  14. confirm.style.cursor = 'default';
  15. confirm.style.backgroundColor = '#d9534f';
  16. confirm.style.borderColor = '#d9534f';
  17. confirm.style.fontSize = '14pt';
  18. confirm.style.fontWeight = 'bold';
  19. confirm.style.color = '#ffffff';
  20. confirm.style.borderRadius = '4px';
  21. cancel = document.createElement( 'div' );
  22. cancel.style.padding = '8px';
  23. cancel.style.cursor = 'default';
  24. cancel.style.backgroundColor = '#5bc0de';
  25. cancel.style.borderColor = '#5bc0de';
  26. cancel.style.fontSize = '14pt';
  27. cancel.style.fontWeight = 'bold';
  28. cancel.style.color = '#ffffff';
  29. cancel.style.borderRadius = '4px';
  30. lineBreak = document.createElement( 'br' );
  31. container.classList.add( 'message' );
  32. messageBox.innerHTML += 'This will DELETE all forms, form submissions,' +
  33. ' and deactivate Ninja Forms';
  34. messageBox.appendChild( lineBreak );
  35. messageBox.innerHTML += '<br>Type <span style="color:red;">DELETE</span>' +
  36. ' to' +
  37. ' confirm';
  38. container.appendChild( messageBox );
  39. container.appendChild( deleteInput );
  40. container.appendChild( lineBreak );
  41. deleteMsgs = document.createElement( 'div' );
  42. deleteMsgs.id = 'progressMsg';
  43. deleteMsgs.style.display = 'none';
  44. deleteMsgs.style.color = 'red';
  45. container.appendChild( deleteMsgs );
  46. confirm.innerHTML = 'Delete';
  47. confirm.classList.add( 'confirm', 'nf-button', 'primary' );
  48. confirm.style.float = 'left';
  49. cancel.innerHTML = 'Cancel';
  50. cancel.classList.add( 'cancel', 'nf-button', 'secondary', 'cancel-delete-all' );
  51. cancel.style.float = 'right';
  52. buttons.appendChild( confirm );
  53. buttons.appendChild( cancel );
  54. buttons.classList.add( 'buttons' );
  55. container.appendChild( buttons );
  56. message = document.createElement( 'div' );
  57. message.appendChild( container );
  58. var downgradeContainer, downgradeTitle, downgradeWarning, downgradeCTA, downgradeInput;
  59. downgradeInput = document.createElement( 'input' );
  60. downgradeInput.setAttribute( 'type', 'text' );
  61. downgradeInput.id = 'downgradeConfirmInput';
  62. downgradeInput.style.marginBottom = '15px';
  63. downgradeInput.style.padding = '7px';
  64. downgradeInput.style.border = '1px solid #D3D3D3';
  65. downgradeInput.style.width = '100%';
  66. downgradeInput.style.height = '3em';
  67. downgradeCTA = document.createElement( 'p' );
  68. downgradeCTA.innerHTML = nf_settings.i18n.downgradeConfirmMessage;
  69. downgradeWarning = document.createElement( 'p' );
  70. downgradeWarning.innerHTML = nf_settings.i18n.downgradeWarningMessage;
  71. downgradeWarning.style.color = 'red';
  72. downgradeTitle = document.createElement( 'h3' );
  73. downgradeTitle.innerHTML = nf_settings.i18n.downgradeMessage;
  74. downgradeContainer = document.createElement( 'div' );
  75. downgradeContainer.appendChild( downgradeTitle );
  76. downgradeContainer.appendChild( downgradeWarning );
  77. downgradeContainer.appendChild( downgradeCTA );
  78. downgradeContainer.appendChild( downgradeInput );
  79. // set up delete model with all the elements created above
  80. deleteAllDataModal = new jBox( 'Modal', {
  81. width: 450,
  82. addClass: 'dashboard-modal',
  83. overlay: true,
  84. closeOnClick: 'body'
  85. } );
  86. deleteAllDataModal.setContent( message.innerHTML );
  87. deleteAllDataModal.setTitle( 'Delete All Ninja Forms Data?' );
  88. // add event listener for cancel button
  89. var btnCancel = deleteAllDataModal.container[0].getElementsByClassName('cancel')[0];
  90. btnCancel.addEventListener('click', function() {
  91. if( ! working ) {
  92. deleteAllDataModal.close();
  93. }
  94. } );
  95. var doAllDataDeletions = function( formIndex ) {
  96. var last_form = 0;
  97. // Gives the user confidence things are happening
  98. $( '#progressMsg' ).html( 'Deleting submissions for '
  99. + nf_settings.forms[ formIndex ].title + "" + ' ( ID: '
  100. + nf_settings.forms[ formIndex ].id + ' )' );
  101. $( '#progressMsg').show();
  102. // notify php this is the last one so it delete data and deactivate NF
  103. if( formIndex === nf_settings.forms.length - 1 ) {
  104. last_form = 1;
  105. }
  106. // do this deletion thang
  107. $.post(
  108. nf_settings.ajax_url,
  109. {
  110. 'action': 'nf_delete_all_data',
  111. 'form': nf_settings.forms[ formIndex ].id,
  112. 'security': nf_settings.nonce,
  113. 'last_form': last_form
  114. }
  115. ).then (function( response ) {
  116. formIndex = formIndex + 1;
  117. response = JSON.parse( response );
  118. // we expect success and then move to the next form
  119. if( response.data.success ) {
  120. if( formIndex < nf_settings.forms.length ) {
  121. doAllDataDeletions( formIndex )
  122. } else {
  123. // if we're finished deleting data then redirect to plugins
  124. if( response.data.plugin_url ) {
  125. window.location = response.data.plugin_url;
  126. }
  127. }
  128. }
  129. } ).fail( function( xhr, status, error ) {
  130. // writes error messages to console to help us debug
  131. console.log( xhr.status + ' ' + error + '\r\n' +
  132. 'There was an error deleting submissions for '
  133. + nf_settings.forms[ formIndex ].title );
  134. });
  135. };
  136. // Add event listener for delete button
  137. var btnDelete = deleteAllDataModal.container[0].getElementsByClassName('confirm')[0];
  138. btnDelete.addEventListener('click', function() {
  139. var confirmVal = $('#confirmDeleteInput').val();
  140. if (! working) {
  141. working = true;
  142. // Gotta type DELETE to play
  143. if ('DELETE' === confirmVal) {
  144. this.style.backgroundColor = '#9f9f9f';
  145. this.style.borderColor = '#3f3f3f';
  146. var cancelBtn = $( '.cancel-delete-all' );
  147. cancelBtn.css( 'backgroundColor', '#9f9f9f' );
  148. cancelBtn.css( 'borderColor', '#3f3f3f');
  149. // this is the first one, so we'll start with index 0
  150. doAllDataDeletions(0);
  151. } else {
  152. deleteAllDataModal.close();
  153. working = false;
  154. }
  155. }
  156. } );
  157. $( '.js-delete-saved-field' ).click( function(){
  158. var that = this;
  159. var data = {
  160. 'action': 'nf_delete_saved_field',
  161. 'field': {
  162. id: $( that ).data( 'id' )
  163. },
  164. 'security': nf_settings.nonce
  165. };
  166. $.post( nf_settings.ajax_url, data )
  167. .done( function( response ) {
  168. $( that ).closest( 'tr').fadeOut().remove();
  169. });
  170. });
  171. // Start building our downgrade model.
  172. jQuery( '#nfDowngrade' ).click( function( e ) {
  173. var data = {
  174. class: 'nfDowngradeModal',
  175. closeOnClick: 'body',
  176. closeOnEsc: true,
  177. // TODO: Maybe this should be build using DOM node construction?
  178. content: downgradeContainer.innerHTML,
  179. btnPrimary: {
  180. text: nf_settings.i18n.downgradeButtonPrimary,
  181. class: 'nfDowngradeButtonPrimary',
  182. callback: function( e ) {
  183. // If our "Downgrade" button does not have have an attribute of disabled...
  184. if( 'disabled' !== $('.nfDowngradeButtonPrimary').attr( 'disabled' ) ){
  185. // ...get the url...
  186. var url = window.location.href;
  187. // ...split the url based on the question mark from the query string...
  188. url = url.split( '?' );
  189. // build the downgrade url and redirect the user.
  190. url[0] = url[0] + '?page=ninja-forms&nf-switcher=rollback';
  191. window.location.replace( url[0] );
  192. }
  193. }
  194. },
  195. btnSecondary: {
  196. text: nf_settings.i18n.downgradeButtonSecondary,
  197. class: 'nfDowngradeButtonSecondary',
  198. callback: function( e ) {
  199. // Close the modal if this button is clicked.
  200. downgradeModel.toggleModal( false );
  201. }
  202. },
  203. useProgressBar: false,
  204. };
  205. var downgradeModel = new NinjaModal( data );
  206. // Style and add the disabled tag by default.
  207. $('.nfDowngradeButtonPrimary').css( 'background', '#ccc' );
  208. $('.nfDowngradeButtonPrimary').css( 'border', '#ccc 1px solid' );
  209. $('.nfDowngradeButtonPrimary').attr( 'disabled', true );
  210. // Listen to our input and...
  211. $('#downgradeConfirmInput').on( 'keyup', function(){
  212. // ...if DOWNGRADE is typed then...
  213. if( 'DOWNGRADE' == $('#downgradeConfirmInput').val() ) {
  214. // ...apply our blue styling to button and remove disabled attribute.
  215. $('.nfDowngradeButtonPrimary').css( 'background', '#1EA9EA' );
  216. $('.nfDowngradeButtonPrimary').css( 'border', '#1EA9EA 1px solid' );
  217. $('.nfDowngradeButtonPrimary').removeAttr( 'disabled' );
  218. }
  219. // ...if DOWNGRADE is not typed then...
  220. if( 'DOWNGRADE' !== $('#downgradeConfirmInput').val() ) {
  221. // ...set styling back to default and reapply the disabled prop.
  222. $('.nfDowngradeButtonPrimary').css( 'background', '#ccc' );
  223. $('.nfDowngradeButtonPrimary').css( 'border', '#ccc 1px solid' );
  224. $('.nfDowngradeButtonPrimary').prop( 'disabled', true );
  225. }
  226. })
  227. });
  228. $( document ).on( 'click', '#delete_on_uninstall', function( e ) {
  229. deleteAllDataModal.open();
  230. } );
  231. $( document ).on( 'click', '.nf-delete-on-uninstall-yes', function( e ) {
  232. e.preventDefault();
  233. $( "#delete_on_uninstall" ).attr( 'checked', true );
  234. } );
  235. // If we're allowed to track site data...
  236. if ( '1' == nf_settings.allow_telemetry ) {
  237. // Show the optout button.
  238. $( '#nfTelOptin' ).addClass( 'hidden' );
  239. $( '#nfTelOptout' ).removeClass( 'hidden' );
  240. } // Otherwise...
  241. else {
  242. // Show the optin button.
  243. $( '#nfTelOptout' ).addClass( 'hidden' );
  244. $( '#nfTelOptin' ).removeClass( 'hidden' );
  245. }
  246. // If optin is clicked...
  247. $( '#nfTelOptin' ).click( function( e ) {
  248. // Hide the button.
  249. $( '#nfTelOptin' ).addClass( 'hidden' );
  250. $( '#nfTelSpinner' ).css( 'display', 'inline-block' );
  251. // Hit AJAX endpoint and opt-in.
  252. $.post( ajaxurl, { action: 'nf_optin', ninja_forms_opt_in: 1 },
  253. function( response ) {
  254. $( '#nfTelOptout' ).removeClass( 'hidden' );
  255. $( '#nfTelSpinner' ).css( 'display', 'none' );
  256. } );
  257. } );
  258. // If optout is clicked...
  259. $( '#nfTelOptout' ).click( function( e ) {
  260. // Hide the button.
  261. $( '#nfTelOptout' ).addClass( 'hidden' );
  262. $( '#nfTelSpinner' ).css( 'display', 'inline-block' );
  263. // Hit AJAX endpoint and opt-out.
  264. $.post( ajaxurl, { action: 'nf_optin', ninja_forms_opt_in: 0 },
  265. function( response ) {
  266. $( '#nfTelOptin' ).removeClass( 'hidden' );
  267. $( '#nfTelSpinner' ).css( 'display', 'none' );
  268. } );
  269. } );
  270. jQuery( '#nfTrashExpiredSubmissions' ).click( function( e ) {
  271. var that = this;
  272. var data = {
  273. closeOnClick: false,
  274. closeOnEsc: true,
  275. content: '<p>' + nf_settings.i18n.trashExpiredSubsMessage + '<p>',
  276. btnPrimary: {
  277. text: nf_settings.i18n.trashExpiredSubsButtonPrimary,
  278. callback: function( e ) {
  279. // Hide the buttons.
  280. deleteModal.maybeShowActions( false );
  281. // Show the progress bar.
  282. deleteModal.maybeShowProgress( true );
  283. // Begin our cleanup process.
  284. that.submissionExpirationProcess( that, -1, deleteModal );
  285. }
  286. },
  287. btnSecondary: {
  288. text: nf_settings.i18n.trashExpiredSubsButtonSecondary,
  289. callback: function( e ) {
  290. deleteModal.toggleModal( false );
  291. }
  292. },
  293. useProgressBar: true,
  294. };
  295. this.submissionExpirationProcess = function( context, steps, modal ) {
  296. var data = {
  297. action: 'nf_batch_process',
  298. batch_type: 'expired_submission_cleanup',
  299. security: nf_settings.batch_nonce
  300. };
  301. jQuery.post( nf_settings.ajax_url, data, function( response ) {
  302. response = JSON.parse( response );
  303. // If we're done...
  304. if ( response.batch_complete ) {
  305. // Push our progress bar to 100%.
  306. modal.setProgress( 100 );
  307. modal.toggleModal( false );
  308. // Exit.
  309. return false;
  310. }
  311. // If we do not yet have a determined number of steps...
  312. if ( -1 == steps ) {
  313. // If step_toal is defined...
  314. if ( 'undefined' != typeof response.step_total ) {
  315. // Use the step_total.
  316. steps = response.step_total;
  317. } // Otherwise... (step_total is not defined)
  318. else {
  319. // Use step_remaining.
  320. steps = response.step_remaining;
  321. }
  322. }
  323. // Calculate our current step.
  324. var step = steps - response.step_remaining;
  325. // Calculate our maximum progress for this step.
  326. var maxProgress = Math.round( step / steps * 100 );
  327. // Increment the progress.
  328. modal.incrementProgress ( maxProgress );
  329. // Recall our function...
  330. context.submissionExpirationProcess( context, steps, modal );
  331. } );
  332. }
  333. var deleteModal = new NinjaModal( data );
  334. });
  335. });