4

Is there a way to test run a theme through the automatic updater?

We have a theme where a version will introduce new functionality which might break existing themes. I would like to check previous theme version upon upgrade and include some upgrade-specific code if the upgrade is between two certain versions.

I first thought I could use after_theme_switch hook, but the theme is mainly used as parent theme and will probably be upgraded while not being the active theme. I have looked into using upgrader_pre_install and upgrader_post_install hooks, but how to I test these?

The ultimate would be to have it as a unit test, but any other way to mock-upgrade would be great. Uploading a zip file is treated as an install and not upgrade.

Is this even possible from within a theme, our should I look into writing a mock-upgrade plugin?

2

You are asking two questions. The one being how can you test an update and the other being how can you run code after an update.

You can test a theme update by pushing the updates out your self and testing it if it works. You can do that by using GitHub Updater.

If you want to run code only after a upgrade there is no core solution. There are two ways it could work.

  1. You save the theme version to the database as with this you can calculate what version or update it is and then run the update code.
  2. If you are including new options in the update you could check if the new options exist yet and if not then run the update code.

I think the after_setup_theme should be fine.

  • Thank you for your comment. I think I found a solution myself that I will post as an answer. Only after_setup_theme will not work since I need to get the previous theme version somehow. upgrader_pre_install worked well for this. – middlesister Jan 27 '14 at 17:30
  • Hm I think your second case would be the best option for me. Luckily we have some new options in the updated version. I obviously made everything a bit too complicated. Thanks – middlesister Jan 27 '14 at 17:49
0

This is the solution I found myself so far. My code can be found in this gist: https://gist.github.com/middlesister/8652490

Fetch previous theme version and store it in a transient on upgrader_pre_install. Compare the previous theme version on upgrader_post_install and see if the upgrade is from a specific version. Set a flag in the transient if it is.

Check the transient and if the flag is set, do what I need and delete the transient afterwards. I tried the upgrader_process_completehook but it didn't seem to work so I added that on the init hook. This means the code will be executed on next page refresh, but since the transient is set to live for an hour I can live with that.

The unit tests are passing, and I tried putting the code in a plugin while using the automatic updater to upgrade from an old version to the current version and it seems to do what I want.

The problem is I need to get this code executed somehow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.