Skip to content

Instantly share code, notes, and snippets.

@middlesister
Last active January 4, 2016 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save middlesister/8652490 to your computer and use it in GitHub Desktop.
Save middlesister/8652490 to your computer and use it in GitHub Desktop.
set a theme option when upgrading a theme from a specific version
<?php
/**
* @group default
*/
class TestUpgrade extends Thematic_UnitTestCase {
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include '../extensions/upgrade.php';
$thematic_pre_20_options = array(
'index_insert' => 2,
'author_info' => 0, // 0 = not checked 1 = checked
'footer_txt' => 'Powered by [wp-link]. Built on the [theme-link].',
'del_legacy_opt'=> 0, // 0 = not checked 1 = check
);
update_option( 'thematic_theme_opt', $thematic_pre_20_options );
}
function setUp() {
delete_transient( 'thematic_upgrade' );
parent::setUp();
}
function tearDown() {
delete_transient( 'thematic_upgrade' );
parent::tearDown();
}
function test_attached_pre_filter() {
global $wp_filter;
$hookname = $wp_filter['upgrader_pre_install'];
$this->assertArrayHasKey( 'thematic_pre_upgrade', $hookname[10] );
}
function test_attached_post_filter() {
global $wp_filter;
$hookname = $wp_filter['upgrader_post_install'];
$this->assertArrayHasKey( 'thematic_post_upgrade', $hookname[10] );
}
function test_attached_upgrade_complete_action() {
global $wp_filter;
$hookname = $wp_filter['upgrader_process_complete'];
$this->assertArrayHasKey( 'thematic_upgrade_complete', $hookname[10] );
}
// Setup data that is shared between the following test functions
function test_upgrader_data() {
$data = array(
'skin' => array(
'title' => 'update theme',
'theme' => 'thematic'
),
'result' => array(
'destination_name' => 'thematic',
),
'hook_extra' => array(
'theme' => 'thematic',
'type' => 'theme',
'action' => 'update'
)
);
$this->assertNotEmpty( $data );
return $data;
}
/**
* @depends test_upgrader_data
*/
function test_set_pre_upgrade_transient( $data ) {
// run the filter that the function is attached to
$res = apply_filters('upgrader_pre_install', true, $data['hook_extra'] );
// get the transient that the function is supposed to create
$transient = get_transient( 'thematic_upgrade');
// check we have transient
$this->assertNotEmpty( $transient );
$this->assertArrayHasKey( 'thematic_previous_version', $transient, 'Transient \'thematic_upgrade\' do not exist' );
}
/**
* @depends test_upgrader_data
*/
function test_post_upgrade_if_pre_transient_not_exists( $data ) {
$theme_options = $this->get_test_options( 'thematic_theme_opt' );
$this->assertEmpty( $theme_options['legacy_xhtml'], 'legacy_mode should be off before upgrade' );
$this->assertFalse( get_transient( 'thematic_upgrade' ) );
$res_post = apply_filters( 'upgrader_post_install', true, $data['hook_extra'], $data['result'] );
$new_transient = get_transient( 'thematic_upgrade' );
$this->assertNotEmpty( $new_transient['activate_xhtml'], 'activate_xhtml transient should be set if no previous transient exists' );
}
/**
* @depends test_upgrader_data
*/
function test_post_upgrade_if_pre_transient_low_version( $data ) {
$theme_options = $this->get_test_options( 'thematic_theme_opt' );
$this->assertEmpty( $theme_options['legacy_xhtml'] );
add_filter( 'pre_transient_thematic_upgrade', array( $this, 'set_test_transient_thematic_legacy_upgrade' ) );
$transient = get_transient( 'thematic_upgrade' );
$this->assertArrayHasKey( 'thematic_previous_version', $transient );
$this->assertTrue( version_compare( $transient['thematic_previous_version'], '2.0', '<' ) );
$res_post = apply_filters( 'upgrader_post_install', true, $data['hook_extra'], $data['result'] );
remove_filter( 'pre_transient_thematic_upgrade', array( $this, 'set_test_transient_thematic_legacy_upgrade' ) );
$new_transient = get_transient( 'thematic_upgrade' );
$this->assertNotEmpty( $new_transient['activate_xhtml'], 'activate_xhtml transient should be set if previous version is lower than 2.0' );
}
/**
* @depends test_upgrader_data
*/
function test_post_upgrade_if_pre_transient_current_version( $data ) {
$theme_options = $this->get_test_options( 'thematic_theme_opt' );
$this->assertEmpty( $theme_options['legacy_xhtml'] );
add_filter( 'pre_transient_thematic_upgrade', array( $this, 'set_test_transient_thematic_recent_upgrade' ) );
$transient = get_transient( 'thematic_upgrade' );
$this->assertArrayHasKey( 'thematic_previous_version', $transient );
$this->assertTrue( version_compare( $transient['thematic_previous_version'], '2.0-alpha', '>=' ) );
$res_post = apply_filters( 'upgrader_post_install', true, $data['hook_extra'], $data['result'] );
remove_filter( 'pre_transient_thematic_upgrade', array( $this, 'set_test_transient_thematic_recent_upgrade' ) );
$new_transient = get_transient( 'thematic_upgrade' );
$this->assertEmpty( $new_transient['activate_xhtml'], 'activate_xhtml transient should not be set if previous version is lower than 2.0' );
}
/**
* @depends test_upgrader_data
*/
function test_upgrade_complete_set_xhtml_mode( $data ) {
$theme_options_pre = $this->get_test_options( 'thematic_theme_opt' );
$this->assertEmpty( $theme_options_pre['legacy_xhtml'], 'legacy_mode is off before upgrade' );
$upgrader = new Theme_Upgrader( new Theme_Upgrader_Skin( $data['skin'] ) );
$transient['activate_xhtml'] = true;
set_transient( 'thematic_upgrade', $transient, 60 * 60 * 12 );
thematic_upgrade_complete( $upgrader, $data['hook_extra'] );
$theme_options_post = $this->get_test_options( 'thematic_theme_opt' );
$this->assertNotEmpty( $theme_options_post['legacy_xhtml'], 'legacy mode should be set after upgrade' );
}
function set_test_transient_thematic_legacy_upgrade( $return ) {
$return['thematic_previous_version'] = '1.0.4';
return $return;
}
function set_test_transient_thematic_recent_upgrade( $return ) {
$return['thematic_previous_version'] = '2.0-alpha';
return $return;
}
function unset_test_transient_thematic_upgrade( $return ) {
return false;
}
}
<?php
/**
* Get the previous theme version before upgrading
*
* @since 2.0
*
* @param bool $return Default is true
* @param array $hook_extra Information about the action
*/
function thematic_pre_upgrade( $return, $hook_extra ) {
// check that we are upgrading Thematic
if( 'thematic' == $hook_extra['theme'] && 'update' == $hook_extra['action'] ) {
// fetch the theme before upgrade
$previous_theme = wp_get_theme( 'thematic' );
// get the theme version
$transient_data = array(
'thematic_previous_version' => $previous_theme->Version
);
// store the previous theme version in a transient
set_transient( 'thematic_upgrade', $transient_data, HOUR_IN_SECONDS );
}
return $return;
}
add_filter( 'upgrader_pre_install' , 'thematic_pre_upgrade', 10 , 2);
/**
* Check theme version after upgrade
*
* Activate xhtml mode if applicable. Sets a transient that tells
* Thematic to activate the xhtml mode. The transient is active for an
* hour. During that hour it will not be possible do remove the xhtml
* mode.
*
* @since 2.0
*
* @param bool $return Default is true
* @param array $hook_extra Information about the action
* @param array $result The result after install/upgrade
*/
function thematic_post_upgrade( $return, $hook_extra, $result ) {
// fetch the current theme options
$thematic_options = thematic_get_wp_opt( 'thematic_theme_opt' );
// check that we are upgrading Thematic
if( 'thematic' == $hook_extra['theme'] && 'update' == $hook_extra['action'] ) {
// get the upgrade transient set before upgrading
$transient = get_transient( 'thematic_upgrade' );
// set activation key only if no transient is present
// or if the previous version is earlier than 2.0-alpha
if( !$transient ||
isset( $transient ) && version_compare( $transient['thematic_previous_version'], '2.0-alpha', '<' ) ) {
// set activation key
$transient['activate_xhtml'] = true;
// update the upgrade transient with the activation key
set_transient( 'thematic_upgrade', $transient, HOUR_IN_SECONDS );
}
}
return $return;
}
add_filter( 'upgrader_post_install' , 'thematic_post_upgrade', 10 , 3);
/**
* Activate xhtml mode directly after upgrade from pre-2.0
*
* @since 2.0
*
* @param Theme_Upgrader $upgrader Current upgrader object
* @param array $hook_extra Information about the action
*/
function thematic_upgrade_complete( $upgrader, $hook_extra ) {
// check that we are upgrading Thematic
if( 'thematic' == $hook_extra['theme'] && 'update' == $hook_extra['action'] ) {
thematic_automatic_activation_legacy_xhtml();
}
}
add_action( 'upgrader_process_complete', 'thematic_upgrade_complete', 10, 2);
/**
* Activate the legacy mode if the transient is present
*
* Make sure the legacy mode gets set on page refresh since it for some reason
* seems to fail on the `upgrader_process_complete` hook.
*
* @since 2.0
*/
function thematic_automatic_activation_legacy_xhtml() {
// fetch the upgrade transient
$transient = get_transient( 'thematic_upgrade' );
// if activation key is set, activate the xhtml mode in theme options
if( $transient && $transient['activate_xhtml'] ) {
$thematic_options = thematic_get_wp_opt( 'thematic_theme_opt' );
$thematic_options['legacy_xhtml'] = '1';
$legacy_is_set = update_option( 'thematic_theme_opt', $thematic_options );
// delete the transient after succesful xhtml mode activation
if( $legacy_is_set )
delete_transient( 'thematic_upgrade' );
}
}
add_action( 'init', 'thematic_automatic_activation_legacy_xhtml');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment