Skip to content

Commit

Permalink
Rename settings option to faustwp_settings (#607)
Browse files Browse the repository at this point in the history
* refactor: rename settings option to faustwp_settings

* feat: add database upgrade functions
  • Loading branch information
apmatthews authored Oct 22, 2021
1 parent 4d7f981 commit a12f341
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 34 deletions.
1 change: 1 addition & 0 deletions plugins/faustwp/faustwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
require FAUSTWP_DIR . '/includes/rest/callbacks.php';
require FAUSTWP_DIR . '/includes/settings/callbacks.php';
require FAUSTWP_DIR . '/includes/updates/callbacks.php';
require FAUSTWP_DIR . '/includes/updates/upgrade-database.php';
require FAUSTWP_DIR . '/includes/utilities/callbacks.php';

if ( is_events_enabled() ) {
Expand Down
16 changes: 8 additions & 8 deletions plugins/faustwp/includes/settings/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function register_settings_menu() {
* @return void
*/
function register_settings() {
register_setting( 'wpe_headless', 'wpe_headless' );
register_setting( 'faustwp_settings', 'faustwp_settings' );
}

add_action( 'admin_init', __NAMESPACE__ . '\\register_settings_section' );
Expand Down Expand Up @@ -182,7 +182,7 @@ function display_menu_locations_field() {
$menu_locations = faustwp_get_setting( 'menu_locations', 'Primary, Footer' );

?>
<input type="text" id="menu_locations" name="wpe_headless[menu_locations]" value="<?php echo esc_attr( $menu_locations ); ?>" class="regular-text" />
<input type="text" id="menu_locations" name="faustwp_settings[menu_locations]" value="<?php echo esc_attr( $menu_locations ); ?>" class="regular-text" />

<p class="description">
<?php esc_html_e( 'A comma-separated list of menu locations. Assign menus to locations at Appearance → Menus.', 'faustwp' ); ?>
Expand All @@ -209,7 +209,7 @@ function display_secret_key_field() {

?>
<input type="text" id="secret_key" value="<?php echo esc_attr( $secret_key ); ?>" class="regular-text code" readonly />
<input type="hidden" name="wpe_headless[secret_key]" value="<?php echo esc_attr( $secret_key ); ?>" />
<input type="hidden" name="faustwp_settings[secret_key]" value="<?php echo esc_attr( $secret_key ); ?>" />

<a
href="<?php echo esc_url( $regenerate_url ); ?>"
Expand Down Expand Up @@ -251,7 +251,7 @@ function display_frontend_uri_field() {
$frontend_uri = faustwp_get_setting( 'frontend_uri', '' );

?>
<input type="text" id="frontend_uri" name="wpe_headless[frontend_uri]" value="<?php echo esc_attr( $frontend_uri ); ?>" class="regular-text" />
<input type="text" id="frontend_uri" name="faustwp_settings[frontend_uri]" value="<?php echo esc_attr( $frontend_uri ); ?>" class="regular-text" />
<p class="description">
<?php esc_html_e( 'The URL to your headless front-end. This is used for authenticated post previews and for rewriting links to point to your front-end site.', 'faustwp' ); ?>
</p>
Expand All @@ -274,25 +274,25 @@ function display_enable_disable_fields() {
?>
<fieldset>
<label for="disable_theme">
<input type="checkbox" id="disable_theme" name="wpe_headless[disable_theme]" value="1" <?php checked( $disable_theme ); ?> />
<input type="checkbox" id="disable_theme" name="faustwp_settings[disable_theme]" value="1" <?php checked( $disable_theme ); ?> />
<?php esc_html_e( 'Disable WordPress theme admin pages', 'faustwp' ); ?>
</label>
<br />

<label for="enable_rewrites">
<input type="checkbox" id="enable_rewrites" name="wpe_headless[enable_rewrites]" value="1" <?php checked( $enable_rewrites ); ?> />
<input type="checkbox" id="enable_rewrites" name="faustwp_settings[enable_rewrites]" value="1" <?php checked( $enable_rewrites ); ?> />
<?php esc_html_e( 'Enable Post and Category URL rewrites', 'faustwp' ); ?>
</label>
<br />

<label for="enable_redirects">
<input type="checkbox" id="enable_redirects" name="wpe_headless[enable_redirects]" value="1" <?php checked( $enable_redirects ); ?> />
<input type="checkbox" id="enable_redirects" name="faustwp_settings[enable_redirects]" value="1" <?php checked( $enable_redirects ); ?> />
<?php esc_html_e( 'Enable public route redirects', 'faustwp' ); ?>
</label>
<br />

<label for="enable_image_source">
<input type="checkbox" id="enable_image_source" name="wpe_headless[enable_image_source]" value="1" <?php checked( $enable_image_source ); ?> />
<input type="checkbox" id="enable_image_source" name="faustwp_settings[enable_image_source]" value="1" <?php checked( $enable_image_source ); ?> />
<?php esc_html_e( 'Use the WordPress domain for media URLs in post content', 'faustwp' ); ?>
</label>
</fieldset>
Expand Down
4 changes: 2 additions & 2 deletions plugins/faustwp/includes/settings/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function faustwp_update_setting( $name, $value ) {
$settings = faustwp_get_settings();
$settings[ $name ] = $value;

update_option( 'wpe_headless', $settings );
update_option( 'faustwp_settings', $settings );
}

/**
Expand All @@ -115,7 +115,7 @@ function faustwp_update_setting( $name, $value ) {
* @return array An array of settings.
*/
function faustwp_get_settings() {
$settings = get_option( 'wpe_headless', array() );
$settings = get_option( 'faustwp_settings', array() );

/**
* Filter 'faustwp_get_settings'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<div>
<div class="content">
<form action="options.php" method="POST">
<?php settings_fields( 'wpe_headless' ); ?>
<?php settings_fields( 'faustwp_settings' ); ?>

<?php do_settings_sections( 'faustwp-settings' ); ?>

Expand Down
58 changes: 58 additions & 0 deletions plugins/faustwp/includes/updates/upgrade-database.php
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 );
}
10 changes: 5 additions & 5 deletions plugins/faustwp/tests/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ public function amOnFaustWPSettingsPage()
}

/**
* Set a wpe_headless setting value.
* Set a faustwp_settings setting value.
*
* @param string $name The wpe_headless setting name.
* @param string $value The wpe_headless setting value.
* @param string $name The faustwp_settings setting name.
* @param string $value The faustwp_settings setting value.
*/
public function haveFaustWPSetting($name, $value = '')
{
$options = $this->grabOptionFromDatabase('wpe_headless');
$options = $this->grabOptionFromDatabase('faustwp_settings') ?: [];
$options[ $name ] = $value;

$this->haveOptionInDatabase('wpe_headless', $options);
$this->haveOptionInDatabase('faustwp_settings', $options);
}
}
20 changes: 10 additions & 10 deletions plugins/faustwp/tests/acceptance/SettingsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public function i_can_see_the_default_settings(AcceptanceTester $I)

// Deactivate plugin and remove settings.
$I->deactivatePlugin('faustwp');
$I->dontHaveOptionInDatabase('wpe_headless');
$I->dontHaveOptionInDatabase('faustwp_settings');

// Reactivate plugin triggering default settings.
$I->activatePlugin('faustwp');

$settings = $I->grabOptionFromDatabase('wpe_headless');
$settings = $I->grabOptionFromDatabase('faustwp_settings');

$I->amOnFaustWPSettingsPage();
$I->seeInField('wpe_headless[frontend_uri]', '');
$I->seeInField('wpe_headless[secret_key]', $settings['secret_key']);
$I->seeInField('wpe_headless[menu_locations]', 'Primary, Footer');
$I->seeInField('faustwp_settings[frontend_uri]', '');
$I->seeInField('faustwp_settings[secret_key]', $settings['secret_key']);
$I->seeInField('faustwp_settings[menu_locations]', 'Primary, Footer');
$I->seeCheckboxIsChecked('#disable_theme');
$I->seeCheckboxIsChecked('#enable_rewrites');
$I->seeCheckboxIsChecked('#enable_redirects');
Expand All @@ -48,12 +48,12 @@ public function i_can_regenerate_my_secret_key(AcceptanceTester $I)
$I->loginAsAdmin();
$I->amOnFaustWPSettingsPage();

$old_secret_key = $I->grabValueFrom('wpe_headless[secret_key]');
$old_secret_key = $I->grabValueFrom('faustwp_settings[secret_key]');

$I->click('.content form a.field-action');
$I->acceptPopup();
$I->dontSeeInField('wpe_headless[secret_key]', '');
$I->dontSeeInField('wpe_headless[secret_key]', $old_secret_key);
$I->dontSeeInField('faustwp_settings[secret_key]', '');
$I->dontSeeInField('faustwp_settings[secret_key]', $old_secret_key);
}

/**
Expand All @@ -64,10 +64,10 @@ public function i_can_cancel_my_secret_key_from_being_regenerated(AcceptanceTest
$I->loginAsAdmin();
$I->amOnFaustWPSettingsPage();

$secret_key = $I->grabValueFrom('wpe_headless[secret_key]');
$secret_key = $I->grabValueFrom('faustwp_settings[secret_key]');

$I->click('.content form a.field-action');
$I->cancelPopup();
$I->seeInField('wpe_headless[secret_key]', $secret_key);
$I->seeInField('faustwp_settings[secret_key]', $secret_key);
}
}
16 changes: 8 additions & 8 deletions plugins/faustwp/tests/integration/settings/test-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,44 @@
class FunctionsTest extends \WP_UnitTestCase {
/** @test */
public function is_redirects_enabled_will_return_true_if_enabled() {
delete_option( 'wpe_headless' );
delete_option( 'faustwp_settings' );

$this->assertFalse( is_redirects_enabled() );

update_option( 'wpe_headless', array( 'enable_redirects' => '1' ) );
update_option( 'faustwp_settings', array( 'enable_redirects' => '1' ) );

$this->assertTrue( is_redirects_enabled() );
}

/** @test */
public function is_rewrites_enabled_will_return_true_if_enabled() {
delete_option( 'wpe_headless' );
delete_option( 'faustwp_settings' );

$this->assertFalse( is_rewrites_enabled() );

update_option( 'wpe_headless', array( 'enable_rewrites' => '1' ) );
update_option( 'faustwp_settings', array( 'enable_rewrites' => '1' ) );

$this->assertTrue( is_rewrites_enabled() );
}

/** @test */
public function is_themes_disabled_will_return_true_if_disabled() {
delete_option( 'wpe_headless' );
delete_option( 'faustwp_settings' );

$this->assertFalse( is_themes_disabled() );

update_option( 'wpe_headless', array( 'disable_theme' => '1' ) );
update_option( 'faustwp_settings', array( 'disable_theme' => '1' ) );

$this->assertTrue( is_themes_disabled() );
}

/** @test */
public function is_image_source_replacement_enabled_will_return_true_if_disabled() {
delete_option( 'wpe_headless' );
delete_option( 'faustwp_settings' );

$this->assertFalse( is_image_source_replacement_enabled() );

update_option( 'wpe_headless', array( 'enable_image_source' => '1' ) );
update_option( 'faustwp_settings', array( 'enable_image_source' => '1' ) );

$this->assertTrue( is_image_source_replacement_enabled() );
}
Expand Down
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 );
}
}

0 comments on commit a12f341

Please sign in to comment.