Ticket #11869: ajax_upgrade_works.diff

File ajax_upgrade_works.diff, 8.6 KB (added by ocean90, 3 years ago)
Line 
1Index: wp-admin/admin-ajax.php
2===================================================================
3--- wp-admin/admin-ajax.php     (revision 14061)
4+++ wp-admin/admin-ajax.php     (working copy)
5@@ -1422,6 +1422,26 @@
6        update_post_meta( $link_id, '_menu_item_url', $link_url );
7 
8        die( json_encode($link_id) );
9+case 'wpms-upgrade-site':
10+       if ( is_multisite() && ! current_user_can( 'manage_network' ) )
11+       die( '-1' );
12+
13+       $site_id = intval( $_POST['site_id'] );
14+       $site_url = esc_url_raw( $_POST['site_url'] );
15+
16+       // Perform a request to update the site
17+       $response = wp_remote_get( trailingslashit( $site_url ) . "wp-admin/upgrade.php?step=1", array( 'timeout' => 120, 'httpversion' => '1.1' ) );
18+
19+       if ( !is_wp_error( $response ) )
20+               $result = '1';
21+       else
22+               $result = '0';
23+
24+       do_action( 'after_mu_upgrade', $response );
25+       do_action( 'wpmu_upgrade_site', $site_id );
26+
27+       die( $result );
28+       break;
29 default :
30        do_action( 'wp_ajax_' . $_POST['action'] );
31        die('0');
32Index: wp-admin/ms-upgrade-network.php
33===================================================================
34--- wp-admin/ms-upgrade-network.php     (revision 14061)
35+++ wp-admin/ms-upgrade-network.php     (working copy)
36@@ -10,17 +10,15 @@
37 require_once('admin.php');
38 
39 if ( !is_multisite() )
40-       wp_die( __('Multisite support is not enabled.') );
41+       wp_die( __( 'Multisite support is not enabled.' ) );
42 
43-require_once( ABSPATH . WPINC . '/http.php' );
44+if ( ! current_user_can( 'manage_network' ) )
45+       wp_die( __( 'You do not have permission to access this page.' ) );
46 
47-$title = __('Update Network');
48+$title = __( 'Update Network' );
49 $parent_file = 'ms-admin.php';
50 require_once('admin-header.php');
51 
52-if ( ! current_user_can( 'manage_network' ) )
53-       wp_die( __('You do not have permission to access this page.') );
54-
55 echo '<div class="wrap">';
56 screen_icon();
57 echo '<h2>'.__('Update Network').'</h2>';
58@@ -28,50 +26,145 @@
59 $action = isset($_GET['action']) ? $_GET['action'] : 'show';
60 
61 switch ( $action ) {
62-       case "upgrade":
63-               $n = ( isset($_GET['n']) ) ? intval($_GET['n']) : 0;
64+       case 'upgrade':         
65+               // Get all the blogs of the site.
66+               $blogs = $wpdb->get_results( "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' ORDER BY registered DESC", ARRAY_A );
67+               $total = count( $blogs );
68 
69-               if ( $n < 5 ) {
70-                       global $wp_db_version;
71-                       update_site_option( 'wpmu_upgrade_site', $wp_db_version );
72-               }
73-
74-               $blogs = $wpdb->get_results( "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' ORDER BY registered DESC LIMIT {$n}, 5", ARRAY_A );
75-               if ( empty( $blogs ) ) {
76-                       echo '<p>' . __( 'All done!' ) . '</p>';
77-                       break;
78-               }
79-               echo "<ul>";
80+               // Feedback
81+               echo '<div id="message" class="updated">';
82+               echo '<p><strong>Updating sites <span id="update_site_count">0</span> of '. $total .'</strong></p>';
83+               echo '</div>';
84+               
85+               echo '<ul id="updating">';
86                foreach ( (array) $blogs as $details ) {
87-                       if ( $details['spam'] == 0 && $details['deleted'] == 0 && $details['archived'] == 0 ) {
88                                $siteurl = get_blog_option( $details['blog_id'], 'siteurl' );
89-                               echo "<li>$siteurl</li>";
90-                               $response = wp_remote_get( trailingslashit( $siteurl ) . "wp-admin/upgrade.php?step=1", array( 'timeout' => 120, 'httpversion' => '1.1' ) );
91-                               if ( is_wp_error( $response ) )
92-                                       wp_die( "<strong>Warning!</strong> Problem updating {$siteurl}. Your server may not be able to connect to sites running on it.<br /> Error message: <em>" . $response->get_error_message() ."</em>" );
93-                               do_action( 'after_mu_upgrade', $response );
94-                               do_action( 'wpmu_upgrade_site', $details[ 'blog_id' ] );
95-                       }
96+                               echo '<li class="hidden" id="site-id-'. $details['blog_id'] .'" value="'. $details['blog_id'] .'"><span class="site_url">'. $siteurl .'</span> <strong class="status"></strong> <a href="#retry" class="button hidden">retry</a></li>' . "\n";
97                }
98-               echo "</ul>";
99-               ?><p><?php _e("If your browser doesn't start loading the next page automatically click this link:"); ?> <a class="button" href="ms-upgrade-network.php?action=upgrade&amp;n=<?php echo ($n + 5) ?>"><?php _e("Next Sites"); ?></a></p>
100+               echo '</ul>';
101+               echo '<p id="alldone"></p>';
102+               ?>
103                <script type='text/javascript'>
104-               <!--
105-               function nextpage() {
106-                       location.href = "ms-upgrade-network.php?action=upgrade&n=<?php echo ($n + 5) ?>";
107-               }
108-               setTimeout( "nextpage()", 250 );
109-               //-->
110-               </script><?php
111-       break;
112+                       jQuery(document).ready(function(){                     
113+                               // Get all mu sites
114+                               var sites = jQuery('#updating li');
115+
116+                               // Number of concurrent sites to perform the upgrade
117+                               var num = 5;
118+                               var offset = 0;
119+
120+                               // number for successful, error and total sites
121+                               var e = 0;
122+                               var s = 0;
123+                               var t = <?php echo $total; ?>;
124+
125+                               // array for queue
126+                               var sites_queue =[];
127+                               
128+                               // Run the upgrade script
129+                               wpms_get_sites_to_update( offset, num );
130+                               
131+                               function wpms_get_sites_to_update( offset, num ) {                                     
132+                                       for ( var i = offset; i < num; i++ ) {
133+                                               if ( undefined == sites[i] ) // sites[i] doesn't exists
134+                                                       break;
135+                                               
136+                                               if ( '-1' != jQuery.inArray(sites[i], sites_queue) ) // duplicate
137+                                                       break;
138+                                               
139+                                               // set sites[i] it into an array to avoid duplicates
140+                                               sites_queue.push( sites[i] );
141+                                               
142+                                               var site_id  = sites[i].value;
143+                                               var site_url = jQuery( '#site-id-' + site_id + ' .site_url' ).text();
144+                                               
145+                                               // Update the site status.
146+                                               jQuery( '#site-id-' + site_id + ' .status' ).text('<?php echo esc_js( __( 'updating...' ) );?>');
147+                                               
148+                                               // update counting
149+                                               var c = jQuery('span#update_site_count');
150+                                               c.text( parseInt( c.text() ) + 1 );
151+
152+                                               // Show the site.
153+                                               jQuery( '#site-id-' + site_id ).removeClass('hidden');
154+                       
155+                                               // update the offset
156+                                               offset = num;
157+                                       
158+                                               // Perform the upgrade
159+                                               wpms_upgrade_site( site_id, site_url, offset );
160+                                       };
161+                               }
162+                               
163+                               jQuery('#updating li a.button').click(function() {
164+                                       // get site_id for retry
165+                                   var site_id = jQuery(this).parent().get(0).value;
166+                                   var site_url = jQuery( '#site-id-' + site_id + ' .site_url' ).text();
167+                                   
168+                                       // update the site status
169+                                       jQuery( '#site-id-' + site_id + ' .status' ).text('<?php echo esc_js( __( 'updating...' ) );?>');
170+                                       
171+                                       // remove retry button
172+                                       jQuery('#site-id-' + site_id).find('.button').addClass('hidden');
173+                                       
174+                                       // update error count
175+                                       e = e - 1;
176+                                       
177+                                       // next try
178+                                   wpms_upgrade_site( site_id, site_url, null );
179+                               })
180+                               
181+                               function wpms_upgrade_site( site_id, site_url, offset ) {
182+                                       params = {
183+                                               action: 'wpms-upgrade-site',
184+                                               site_id: site_id,
185+                                               site_url: site_url,
186+                                       };
187+
188+                                       jQuery.post( ajaxurl, params, function(response) {
189+                                               
190+                                               if ( '1' == response ) {
191+                                                       // Update the site status
192+                                                       jQuery('#site-id-' + site_id).find('.status').html( '<?php echo esc_js( __( 'Successfully updated!' ) );?>' );
193+                                                       
194+                                                       // update successful count
195+                                                       s = s + 1;
196+                                               } else {
197+                                                       // Update the site status
198+                                                       jQuery('#site-id-' + site_id).find('.status').html( '<?php echo esc_js( __( 'Update failed.' ) );?>' );
199+                                                       
200+                                                       // show retry button
201+                                                       jQuery('#site-id-' + site_id).find('.button').removeClass('hidden');
202+
203+                                                       // update error count
204+                                                       e = e + 1;
205+                                               };
206+                                               // Update the status
207+                                               if ( s == t )
208+                                                       jQuery('div#message p strong').html( '<?php echo esc_js( __( 'All sites successfully updated!' ) );?>');
209+                                               else if ( (s + e) == t)
210+                                                       jQuery('div#message p strong').html( s + ' sites successfully updated and ' + e + ' updates failed!'); // translation ?
211+
212+                                               // next sites
213+                                               if ( null != offset ) {
214+                                                       num = num + 5;
215+                                                       wpms_get_sites_to_update( offset, num );
216+                                               }
217+
218+                                       }, 'json');
219+                               }
220+                       });
221+               </script>
222+               <?php   
223+               break;
224+               
225        case 'show':
226        default:
227-               ?><p><?php _e("You can update all the sites on your network through this page. It works by calling the update script of each site automatically. Hit the link below to update."); ?></p>
228-               <p><a class="button" href="ms-upgrade-network.php?action=upgrade"><?php _e("Update Network"); ?></a></p><?php
229+               ?><p><?php _e( 'You can update all the sites on your network through this page. It works by calling the update script of each site automatically. Hit the link below to update.' ); ?></p>
230+               <p><a class="button" href="ms-upgrade-network.php?action=upgrade"><?php _e( 'Update Network' ); ?></a></p><?php
231                do_action( 'wpmu_upgrade_page' );
232        break;
233 }
234-?>
235-</div>
236+echo '</div>';
237 
238-<?php include('admin-footer.php'); ?>
239+include('admin-footer.php'); ?>