Using Grunt to test WordPress Upgrade changes

During the final moments of a WordPress release development cycle I was asked a very simple question:

How do I test the changes to src/wp-admin/includes/update-core.php before they’re committed to trunk?

One method that others have resorted to is to build a custom WordPress release zip and hack WordPress to always install from that custom zip file – It’s not hard to do that, but it’s a real pain when your development workflow includes testing things time and time again.  [note: I’ve used this method in the past, before Grunt was “a thing”]

So, here’s my current solution: Use grunt build and hit Update, something we all use already, coupled with WordPress’s own update routines!

The way that I do it, is that I apply the patch to /src/, and then alter class-wp-upgrader.php to use the local copy of update-core.php instead of the one from the zip file it’ll download, and then just hit Update on the dashboard.

This takes very little effort to pull off – below is an example of the code changes needed to cause it to abort a update early on with a custom message, all that’s left to do is to run grunt build and hit the update button! – Just make sure you’re in /build/ rather than /src/, as it could quickly lead to custom code going missing.. I know this from experience!

Index: src/wp-admin/includes/class-wp-upgrader.php
==================================================================
@@ class Core_Upgrader extends WP_Upgrader
 $working_dir = $this->unpack_package( $download );
 if ( is_wp_error($working_dir) )
 	return $working_dir;

 // Copy update-core.php from the new version into place.
-if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
+if ( false && !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
 	$wp_filesystem->delete($working_dir, true);
 	return new WP_Error( 'copy_failed_for_update_core_file', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), 'wp-admin/includes/update-core.php' );
 }
 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);

 require_once( ABSPATH . 'wp-admin/includes/update-core.php' );

Index: src/wp-admin/includes/update-core.php
==================================================================
  * @param string $from New release unzipped path.
  * @param string $to   Path to old WordPress installation.
  * @return WP_Error|null WP_Error on failure, null on success.
  */
 function update_core($from, $to) {
 	global $wp_filesystem, $_old_files, $_new_bundled_files, $wpdb;

+	// Demo code to show that this process works
+	echo "Hi! This is text to confirm that I'm using a custom update-core.php file during the update!";
+	die();
+
 	@set_time_limit( 300 );

A few frequently asked questions that have come up in the past with this method:

  1. Question: It’s still downloading the zip file from the internet though!
    Answer: Yes, it does. However my internet connections are usually fast enough that this isn’t a problem.
  2. Question: What if there’s other alterations needed in other update related files?
    Answer: It doesn’t actually matter, update-core.php is the only file from the zip that’s used during update, all the other files (WP_Upgrader / WP_Filesystem / etc) use what’s installed, they’re only overwritten once the update commences using the old code.
  3. Question: How do I stop it from redirecting after the update?
    Answer: When you find out, let me know!  I’ve resorted to simply adding a die(); after the debug code I’m using.
  4. Question: Why isn’t there a grunt task to build the WordPress zips?
    Answer: Haven’t needed one yet I guess? Why don’t you submit a patch on Trac for it? :)

WordCamp Brisbane 2015 – WordPress Conference

WordCamp Brisbane 2015 has been announced! May 30/31st at QUT in Gardens point. It seems like just yesterday that I wrote a post, but the reality is that the last few posts on this site have been WordCamp announcements.. Although I missed announcing WordCamp Sydney 2014, I’m happy to say, I’ve not missed my chance to post about WordCamp Brisbane 2015 :) What is a WordCamp?

WordCamps are casual, locally-organized conferences covering everything related to WordPress, the free and open source personal publishing software that powers over 75 million sites on the web. – http://central.wordcamp.org/about/

WordCamps are attended by people ranging from blogging newbies to professional WordPress developers and consultants, and usually combine scheduled programming with unconference sessions and other activities. – http://central.wordcamp.org/about/

What should I expect from a WordCamp? For more information about what to expect from a WordCamp, check out WordCamp Centra’s What to Expect page.

WordCamp Sydney 2012 – July 21-22

Please note: I’m not a WordCamp Sydney organiser, Any questions related to WordCamp Sydney should be directed towards their contact form. If you’re interested in WordPress in Sydney, also check out the WordPress Sydney meetup group.

Howdy folks,
So I don’t blog often, it’s a bad habit of mine, but it doesn’t mean I don’t love WordPress any less!

The number of WordPress professionals in Australia is forever increasing, how many are there? We don’t know – but what we do know, is that every time a WordCamp is held in Australia that up to 200 people jump at the opportunity to come out and meet other WordPress users, Designers, Developers, and even some of the crew who make using WordPress possible.

So I’m glad to be able to say, 2012 has brought with it yet another WordCamp, WordCamp Sydney, which is being held at the University of Sydney in July. I’m glad to be able to say I’ll be there, well really, I have to say, I’m rather looking forward to it!
(Did you know that the 2nd WordCamp to ever be held in Australia was WordCamp Australia held in Sydney in 2008?).

There’s not too much longer until the WordCamp is on top of us, so head over and check out their new site, talk to your friends about it, mention it to colleagues, friends and family, anyone who you think would be interested in a weekend of WordPress goodness – and don’t forget, WordCamp’s are not just for developers, they’re for users, designers and even those who don’t use WordPress, but are interested in the digital web we all work in.

WordCamp Sydney July 21-22, 2012