-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename settings option to faustwp_settings (#607)
* refactor: rename settings option to faustwp_settings * feat: add database upgrade functions
- Loading branch information
1 parent
4d7f981
commit a12f341
Showing
9 changed files
with
186 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Database upgrade functions. | ||
* | ||
* @package FaustWP | ||
*/ | ||
|
||
namespace WPE\FaustWP\Updates; | ||
|
||
add_action( 'plugins_loaded', __NAMESPACE__ . '\\upgrade_database' ); | ||
/** | ||
* Migrates the plugin's stored data to the latest format. | ||
* | ||
* @return bool True if updates were carried out or false. | ||
*/ | ||
function upgrade_database(): bool { | ||
$current_version = get_option( 'faustwp_current_version', '0.0.0' ); | ||
$file_data = get_file_data( FAUSTWP_FILE, array( 'Version' => 'Version' ) ); | ||
$plugin_version = $file_data['Version']; | ||
|
||
if ( 1 === version_compare( $plugin_version, $current_version ) ) { | ||
|
||
// Array of versions requiring update and their callbacks. | ||
// Note these do not have to exactly match plugin version. | ||
$update_versions = array( | ||
'0.6.1' => 'upgrade_0_6_1', | ||
); | ||
|
||
foreach ( $update_versions as $version => $callback ) { | ||
if ( 1 === version_compare( $version, $current_version ) ) { | ||
call_user_func( __NAMESPACE__ . '\\' . $callback ); | ||
} | ||
} | ||
|
||
// Save the last updated version. | ||
update_option( 'faustwp_current_version', $plugin_version ); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Update settings option name for versions after to 0.6.1. | ||
* | ||
* @return bool True if the database was updated or false. | ||
*/ | ||
function upgrade_0_6_1(): bool { | ||
$settings = get_option( 'wpe_headless', array() ); | ||
|
||
if ( empty( $settings ) ) { | ||
return false; | ||
} | ||
|
||
delete_option( 'wpe_headless' ); | ||
|
||
return update_option( 'faustwp_settings', $settings ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
plugins/faustwp/tests/integration/updates/test-upgrade-database.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* Tests database upgrades. | ||
*/ | ||
|
||
namespace WPE\FaustWP\Tests\Updates; | ||
|
||
use \WP_UnitTestCase; | ||
use function WPE\FaustWP\Updates\upgrade_database; | ||
|
||
/** | ||
* Class UpdatesTestCases | ||
* | ||
* @package FaustWP | ||
*/ | ||
class UpdatesTestCases extends WP_UnitTestCase { | ||
|
||
protected $versions; | ||
|
||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$file_data = get_file_data( FAUSTWP_FILE, array( 'Version' => 'Version' ) ); | ||
$plugin_version = $file_data['Version']; | ||
|
||
$this->versions = array( | ||
'old' => '0.5.0', | ||
'current' => $plugin_version, | ||
'new' => $this->new_version( $plugin_version ), | ||
); | ||
} | ||
|
||
public function tearDown(): void { | ||
parent::tearDown(); | ||
|
||
delete_option( 'faustwp_current_version' ); | ||
} | ||
|
||
/** | ||
* @covers ::\WPE\FaustWP\Updates\upgrade_database() | ||
*/ | ||
public function test_upgrade_database_installed_new_version(): void { | ||
update_option( 'faustwp_current_version', $this->versions['old'] ); | ||
|
||
self::assertTrue( upgrade_database() ); | ||
self::assertEquals( get_option( 'faustwp_current_version' ), $this->versions['current'] ); | ||
} | ||
|
||
/** | ||
* @covers ::\WPE\FaustWP\Updates\upgrade_database() | ||
*/ | ||
public function test_upgrade_database_no_saved_version(): void { | ||
self::assertTrue( upgrade_database() ); | ||
self::assertEquals( get_option( 'faustwp_current_version' ), $this->versions['current'] ); | ||
} | ||
|
||
/** | ||
* @covers ::\WPE\FaustWP\Updates\upgrade_database() | ||
*/ | ||
public function test_upgrade_database_current_version(): void { | ||
update_option( 'faustwp_current_version', $this->versions['current'] ); | ||
|
||
self::assertFalse( upgrade_database() ); | ||
self::assertEquals( get_option( 'faustwp_current_version' ), $this->versions['current'] ); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @covers ::\WPE\FaustWP\Updates\upgrade_database() | ||
*/ | ||
public function test_upgrade_database_installed_old_version(): void { | ||
update_option( 'faustwp_current_version', $this->versions['new'] ); | ||
|
||
self::assertFalse( upgrade_database() ); | ||
self::assertEquals( get_option( 'faustwp_current_version' ), $this->versions['new'] ); | ||
} | ||
|
||
/** | ||
* Increments the patch version supplied for easier testing | ||
* | ||
* @param string $old_version The version to increment. | ||
* | ||
* @return string | ||
*/ | ||
protected function new_version( $old_version ) { | ||
$version_parts = explode( '.', $old_version ); | ||
|
||
$version_parts[2] = strval( intval( $version_parts[2] ) + 1 ); | ||
|
||
return implode( '.', $version_parts ); | ||
} | ||
} |