upgrade.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. jQuery(document).ready(function($) {
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Ninja Forms THREE Upgrade App
  5. |--------------------------------------------------------------------------
  6. */
  7. var nfUpgradeApp = {
  8. forms: [],
  9. step: 'checking',
  10. container: '#nfUpgradeApp',
  11. tmpl: {
  12. test: wp.template( 'test' ),
  13. table: wp.template( 'table' ),
  14. legend: wp.template( 'legend' ),
  15. },
  16. formCheckPointer: 0,
  17. formConvertPointer: 0,
  18. updateTable: function(){
  19. var data = {
  20. title: '',
  21. headers: [ 'Title', 'Status' ],
  22. rows: this.forms,
  23. step: this.step,
  24. showSupportLink: 0,
  25. };
  26. if( 'checking' == this.step ) {
  27. data.title = 'Form Upgrade List';
  28. data.legend = this.tmpl.legend( {
  29. no_issues_detected: 'No Issues Detected',
  30. will_need_attention: 'Will Need Attention After Upgrade',
  31. }),
  32. data.next = 'Start Upgrade';
  33. data.readyToConvert = 1;
  34. _.each(this.forms, function (form) {
  35. if ( ! form.checked ) data.readyToConvert = 0;
  36. }, this);
  37. }
  38. if( 'converting' == this.step ) {
  39. data.title = 'Converting Forms';
  40. var redirectToThree = 1;
  41. _.each(this.forms, function (form) {
  42. if ( ! form.converted ) redirectToThree = 0;
  43. if ( form.failed ) data.showSupportLink = 1;
  44. }, this);
  45. if( redirectToThree ) {
  46. jQuery( window ).unbind( 'beforeunload' );
  47. window.location.href = nfThreeUpgrade.redirectURL;
  48. }
  49. }
  50. jQuery( this.container ).html( this.tmpl.table( data ) );
  51. },
  52. checkForms: function() {
  53. var form = this.forms[ this.formCheckPointer ] || null;
  54. if( form ) this.checkForm( form );
  55. this.formCheckPointer++;
  56. },
  57. checkForm: function( form ) {
  58. var that = this;
  59. $.post( ajaxurl, { action: 'ninja_forms_upgrade_check', formID: form.id }, function( response ) {
  60. var icon = ( response.canUpgrade ) ? '' : 'flag';
  61. var flagged = ( response.canUpgrade ) ? 0 : 1;
  62. that.updateForm( form.id, 'title', response.title );
  63. that.updateForm( form.id, 'icon', icon );
  64. that.updateForm( form.id, 'checked', true );
  65. that.updateForm( form.id, 'flagged', flagged );
  66. that.updateTable();
  67. that.checkForms();
  68. }, 'json' );
  69. },
  70. updateForm: function( formID, property, value ) {
  71. _.each( this.forms, function( form ) {
  72. if( formID != form.id ) return;
  73. form[ property ] = value;
  74. });
  75. },
  76. start: function () {
  77. _.each( nfThreeUpgrade.forms, function( formID ) {
  78. this.forms.push({
  79. id: formID,
  80. title: '',
  81. icon: 'update',
  82. checked: false,
  83. converted: false,
  84. failed: false,
  85. });
  86. }, this );
  87. this.checkForms();
  88. this.updateTable();
  89. var that = this;
  90. jQuery( '#nfUpgradeApp' ).on( 'click','.js-nfUpgrade-startConversion', function() {
  91. that.startConversion( that );
  92. } );
  93. },
  94. startConversion: function( app ) {
  95. console.log( 'HERE' );
  96. console.log( app );
  97. app.step = 'converting';
  98. // Add a notice if the user tries to navigate away during conversion.
  99. jQuery( window ).bind( 'beforeunload', function(){
  100. return 'You have unsaved changes.';
  101. } );
  102. $.post( ajaxurl, { nf2to3: 1, action: 'ninja_forms_ajax_migrate_database', security: nfThreeUpgrade.nonce }, function( response ) {
  103. $.post( ajaxurl, { action: 'nfThreeUpgrade_GetSerializedFields' }, function( fieldsExport ) {
  104. $.post(ajaxurl, { nf2to3: 1, fields: fieldsExport.serialized, action: 'ninja_forms_ajax_import_fields' }, function ( fieldsImport ) {
  105. app.convertForms();
  106. }, 'json' );
  107. }, 'json' );
  108. });
  109. },
  110. convertForms: function() {
  111. var form = this.forms[ this.formConvertPointer ] || null;
  112. if( form ) this.convertForm( form );
  113. this.formConvertPointer++;
  114. },
  115. convertForm: function( form ) {
  116. var app = this;
  117. console.log( 'Converting...' );
  118. console.log( form );
  119. form.icon = 'update'
  120. app.updateTable();
  121. $.post(ajaxurl, {action: 'nfThreeUpgrade_GetSerializedForm', formID: form.id}, function ( formExport ) {
  122. $.post(ajaxurl, { nf2to3: 1, action: 'ninja_forms_ajax_import_form', formID: form.id, import: formExport.serialized, flagged: form.flagged }, function ( formImport ) {
  123. form.converted = true;
  124. form.icon = 'yes';
  125. app.updateTable();
  126. }, 'json').fail( function() {
  127. form.converted = false;
  128. form.failed = true;
  129. form.icon = 'no';
  130. app.updateTable();
  131. }).always( function() {
  132. app.convertForms();
  133. });
  134. }, 'json' );
  135. }
  136. };
  137. nfUpgradeApp.start();
  138. });