html-admin-page-upgrade-network.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Network Admin Database Upgrade
  4. *
  5. * Shows the databse upgrade process.
  6. *
  7. * @date 24/8/18
  8. * @since 5.7.4
  9. * @param void
  10. */
  11. ?>
  12. <style type="text/css">
  13. /* hide steps */
  14. .show-on-complete {
  15. display: none;
  16. }
  17. </style>
  18. <div id="acf-upgrade-wrap" class="wrap">
  19. <h1><?php _e( 'Upgrade Database', 'acf' ); ?></h1>
  20. <p><?php echo sprintf( __( 'The following sites require a DB upgrade. Check the ones you want to update and then click %s.', 'acf' ), '"' . __( 'Upgrade Sites', 'acf' ) . '"' ); ?></p>
  21. <p><input type="submit" name="upgrade" value="<?php _e( 'Upgrade Sites', 'acf' ); ?>" class="button" id="upgrade-sites"></p>
  22. <table class="wp-list-table widefat">
  23. <thead>
  24. <tr>
  25. <td class="manage-column check-column" scope="col">
  26. <input type="checkbox" id="sites-select-all">
  27. </td>
  28. <th class="manage-column" scope="col" style="width:33%;">
  29. <label for="sites-select-all"><?php _e( 'Site', 'acf' ); ?></label>
  30. </th>
  31. <th><?php _e( 'Description', 'acf' ); ?></th>
  32. </tr>
  33. </thead>
  34. <tfoot>
  35. <tr>
  36. <td class="manage-column check-column" scope="col">
  37. <input type="checkbox" id="sites-select-all-2">
  38. </td>
  39. <th class="manage-column" scope="col">
  40. <label for="sites-select-all-2"><?php _e( 'Site', 'acf' ); ?></label>
  41. </th>
  42. <th><?php _e( 'Description', 'acf' ); ?></th>
  43. </tr>
  44. </tfoot>
  45. <tbody id="the-list">
  46. <?php
  47. $sites = acf_get_sites();
  48. if ( $sites ) :
  49. foreach ( $sites as $i => $site ) :
  50. // switch blog
  51. switch_to_blog( $site['blog_id'] );
  52. ?>
  53. <tr
  54. <?php
  55. if ( $i % 2 == 0 ) :
  56. ?>
  57. class="alternate"<?php endif; ?>>
  58. <th class="check-column" scope="row">
  59. <?php if ( acf_has_upgrade() ) : ?>
  60. <input type="checkbox" value="<?php echo $site['blog_id']; ?>" name="checked[]">
  61. <?php endif; ?>
  62. </th>
  63. <td>
  64. <strong><?php echo get_bloginfo( 'name' ); ?></strong><br /><?php echo home_url(); ?>
  65. </td>
  66. <td>
  67. <?php if ( acf_has_upgrade() ) : ?>
  68. <span class="response"><?php printf( __( 'Site requires database upgrade from %1$s to %2$s', 'acf' ), acf_get_db_version(), ACF_VERSION ); ?></span>
  69. <?php else : ?>
  70. <?php _e( 'Site is up to date', 'acf' ); ?>
  71. <?php endif; ?>
  72. </td>
  73. </tr>
  74. <?php
  75. // restore
  76. restore_current_blog();
  77. endforeach;
  78. endif;
  79. ?>
  80. </tbody>
  81. </table>
  82. <p><input type="submit" name="upgrade" value="<?php _e( 'Upgrade Sites', 'acf' ); ?>" class="button" id="upgrade-sites-2"></p>
  83. <p class="show-on-complete"><?php echo sprintf( __( 'Database Upgrade complete. <a href="%s">Return to network dashboard</a>', 'acf' ), network_admin_url() ); ?></p>
  84. <script type="text/javascript">
  85. (function($) {
  86. var upgrader = new acf.Model({
  87. events: {
  88. 'click #upgrade-sites': 'onClick',
  89. 'click #upgrade-sites-2': 'onClick'
  90. },
  91. $inputs: function(){
  92. return $('#the-list input:checked');
  93. },
  94. onClick: function( e, $el ){
  95. // prevent default
  96. e.preventDefault();
  97. // bail early if no selection
  98. if( !this.$inputs().length ) {
  99. return alert('<?php _e( 'Please select at least one site to upgrade.', 'acf' ); ?>');
  100. }
  101. // confirm action
  102. if( !confirm("<?php _e( 'It is strongly recommended that you backup your database before proceeding. Are you sure you wish to run the updater now?', 'acf' ); ?>") ) {
  103. return;
  104. }
  105. // upgrade
  106. this.upgrade();
  107. },
  108. upgrade: function(){
  109. // vars
  110. var $inputs = this.$inputs();
  111. // bail early if no sites selected
  112. if( !$inputs.length ) {
  113. return this.complete();
  114. }
  115. // disable buttons
  116. $('.button').prop('disabled', true);
  117. // vars
  118. var $input = $inputs.first();
  119. var $row = $input.closest('tr');
  120. var text = '';
  121. var success = false;
  122. // show loading
  123. $row.find('.response').html('<i class="acf-loading"></i></span> <?php printf( __( 'Upgrading data to version %s', 'acf' ), ACF_VERSION ); ?>');
  124. // send ajax request to upgrade DB
  125. $.ajax({
  126. url: acf.get('ajaxurl'),
  127. dataType: 'json',
  128. type: 'post',
  129. data: acf.prepareForAjax({
  130. action: 'acf/ajax/upgrade',
  131. blog_id: $input.val()
  132. }),
  133. success: function( json ){
  134. success = true;
  135. $input.remove();
  136. text = '<?php _e( 'Upgrade complete.', 'acf' ); ?>';
  137. },
  138. error: function( jqXHR, textStatus, errorThrown ){
  139. text = '<?php _e( 'Upgrade failed.', 'acf' ); ?>';
  140. if( error = acf.getXhrError(jqXHR) ) {
  141. text += ' <code>' + error + '</code>';
  142. }
  143. },
  144. complete: this.proxy(function(){
  145. // display text
  146. $row.find('.response').html( text );
  147. // if successful upgrade, proceed to next site. Otherwise, skip to complete.
  148. if( success ) {
  149. this.upgrade();
  150. } else {
  151. this.complete();
  152. }
  153. })
  154. });
  155. },
  156. complete: function(){
  157. // enable buttons
  158. $('.button').prop('disabled', false);
  159. // show message
  160. $('.show-on-complete').show();
  161. }
  162. });
  163. })(jQuery);
  164. </script>
  165. </div>