Skip to content

Commit

Permalink
Redo the way default options are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
ingeniumed committed Oct 9, 2024
1 parent c2da69a commit 5478576
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 55 deletions.
14 changes: 13 additions & 1 deletion modules/settings/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
class Settings {
const SETTINGS_SLUG = 'vw-settings';

// Its key to centralize these, so we can easily update them in the future
const DEFAULT_SETTINGS_OPTIONS = [
'post_types' => [
'post' => 'on',
'page' => 'on',
],
'publish_guard' => 'on',
'email_address' => '',
'webhook_url' => '',
];

/**
* Initialize the rest of the stuff in the class if the module is active
*/
Expand Down Expand Up @@ -223,7 +234,8 @@ public static function helper_settings_validate_and_save(): bool {

$new_options = self::settings_validate( $new_options );

OptionsUtilities::update_module_options( self::SETTINGS_SLUG, $new_options );
// Blend the new options with the old options, including any new options that may have been added
OptionsUtilities::update_module_options( $new_options );

// Redirect back to the settings page that was submitted without any previous messages
$goback = add_query_arg( 'message', 'settings-updated', remove_query_arg( [ 'message' ], wp_get_referer() ) );
Expand Down
42 changes: 18 additions & 24 deletions modules/shared/php/options-utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,23 @@ class OptionsUtilities {
const OPTIONS_GROUP = 'vip_workflow_';
const OPTIONS_GROUP_NAME = 'vip_workflow_options';

// Its key to centralize these here, so we can easily update them in the future
private const DEFAULT_OPTIONS = [
'post_types' => [
'post' => 'on',
'page' => 'on',
],
'publish_guard' => 'on',
'email_address' => '',
'webhook_url' => '',
];

/**
* Given a module name, return a set of saved module options
*
* @param string $module_slug The slug used for this module
* @param array $default_options The default options for the module
* @return object The set of saved module options for this module, or an empty stdClass if none are found
*/
public static function get_module_options( string $module_slug ): object|null {
public static function get_module_options( string $module_slug, array $default_options = [] ): object|null {
$module_options_key = self::get_module_options_key( $module_slug );
$module_options = get_option( $module_options_key, new stdClass() );

// Ensure all default options are set
foreach ( self::DEFAULT_OPTIONS as $key => $value ) {
if ( ! isset( $module_options->$key ) ) {
$module_options->$key = $value;
if ( [] !== $module_options ) {
// Ensure all default options are set
foreach ( $default_options as $key => $value ) {
if ( ! isset( $module_options->$key ) ) {
$module_options->$key = $value;
}
}
}

Expand All @@ -54,20 +46,23 @@ public static function get_module_options( string $module_slug ): object|null {
* @return string|array|boolean|null The value of the key, or null if it doesn't exist
*/
public static function get_options_by_key( string $key ): string|array|bool|null {
$module_options = self::get_module_options( Settings::SETTINGS_SLUG );
$module_options = self::get_module_options( Settings::SETTINGS_SLUG, Settings::DEFAULT_SETTINGS_OPTIONS );
return $module_options->$key;
}

/**
* Update a module option, using the module's name and the key
* Update a module option, using the module's name and the key.
*
* Note: This method is used to update a single key in the module options, so it will override the entire options object.
*
* @param string $module_slug The slug used for this module
* @param string $key The option key
* @param string $value The option value
* @param array $default_options The default options for the module
* @return bool True if the option was updated, false otherwise.
*/
public static function update_module_option_key( string $module_slug, string $key, string $value ): bool {
$module_options = self::get_module_options( $module_slug );
public static function update_module_option_key( string $module_slug, string $key, string $value, array $default_options = [] ): bool {
$module_options = self::get_module_options( $module_slug, $default_options );
$module_options->$key = $value;

$module_options_key = self::get_module_options_key( $module_slug );
Expand All @@ -77,13 +72,12 @@ public static function update_module_option_key( string $module_slug, string $ke
/**
* Update a module options, using the module's name
*
* @param string $module_slug The slug used for this module
* @param object $new_options The new options to save
* @return bool True if the options were updated, false otherwise.
*/
public static function update_module_options( string $module_slug, array $new_options ): bool {
$module_options_key = self::get_module_options_key( $module_slug );
$old_options = self::get_module_options( $module_slug );
public static function update_module_options( array $new_options ): bool {
$module_options_key = self::get_module_options_key( Settings::SETTINGS_SLUG );
$old_options = self::get_module_options( Settings::SETTINGS_SLUG, Settings::DEFAULT_SETTINGS_OPTIONS );
$new_options = (object) array_merge( (array) $old_options, $new_options );

return update_option( $module_options_key, $new_options );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@
class RequiredMetadataIdHandlerTest extends WP_UnitTestCase {

/**
* Before each test, ensure default custom statuses are available.
* Before each test, ensure default custom statuses are available and reset all module options.
*/
protected function setUp(): void {
parent::setUp();

// Reset all module options
OptionsUtilities::reset_all_module_options();

// Normally custom statuses are installed on 'admin_init', which is only run when a page is accessed
// in the admin web interface. Manually install them here. This avoid issues when a test creates or deletes
// a status and it's the only status existing, which can cause errors due to status restrictions.
CustomStatus::setup_install();
}

protected function tearDown(): void {
parent::tearDown();

// Reset all module options
OptionsUtilities::reset_all_module_options();
}

public function test_remove_deleted_metadata_from_required_metadata() {
$meta_id = 1;
$custom_status_term = CustomStatus::add_custom_status( [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@
class RequiredUserIdHandlerTest extends WP_UnitTestCase {

/**
* Before each test, ensure default custom statuses are available.
* Before each test, ensure default custom statuses are available and reset all module options.
*/
protected function setUp(): void {
parent::setUp();

// Reset all module options
OptionsUtilities::reset_all_module_options();

// Normally custom statuses are installed on 'admin_init', which is only run when a page is accessed
// in the admin web interface. Manually install them here. This avoid issues when a test creates or deletes
// a status and it's the only status existing, which can cause errors due to status restrictions.
CustomStatus::setup_install();
}

protected function tearDown(): void {
parent::tearDown();

// Reset all module options
OptionsUtilities::reset_all_module_options();
}

public function test_remove_deleted_user_from_required_users_no_reassigned_user() {
$deleted_user_id = 1;
$custom_status_term = CustomStatus::add_custom_status( [
Expand Down
12 changes: 4 additions & 8 deletions tests/modules/custom-status/rest/test-custom-status-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@
class CustomStatusRestApiTest extends RestTestCase {

/**
* Before each test, ensure default custom statuses are available.
* Before each test, ensure default custom statuses are available and reset all module options.
*/
protected function setUp(): void {
parent::setUp();

// Reset all module options
OptionsUtilities::reset_all_module_options();

// Normally custom statuses are installed on 'admin_init', which is only run when a page is accessed
// in the admin web interface. Manually install them here. This avoid issues when a test creates or deletes
// a status and it's the only status existing, which can cause errors due to status restrictions.
CustomStatus::setup_install();
}

protected function tearDown(): void {
parent::tearDown();

// Reset all module options
OptionsUtilities::reset_all_module_options();
}

public function test_create_custom_status_with_optional_fields() {
$editorial_metadata_term = EditorialMetadata::insert_editorial_metadata_term( [
'name' => 'Test Metadata 1',
Expand Down
8 changes: 2 additions & 6 deletions tests/modules/notifications/test-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ public function test_send_to_webhook_happy_path() {
];
}, 10, 3 );

OptionsUtilities::update_module_option_key( Settings::SETTINGS_SLUG, 'webhook_url', 'https://webhook.site/this-url-doesnt-exist' );
OptionsUtilities::update_module_option_key( Settings::SETTINGS_SLUG, 'webhook_url', 'https://webhook.site/this-url-doesnt-exist', Settings::DEFAULT_SETTINGS_OPTIONS );

$response = Notifications::send_to_webhook( 'Test Message', 'status-change', '2024-09-19 00:26:50' );

$this->assertTrue( $response );

OptionsUtilities::update_module_option_key( Settings::SETTINGS_SLUG, 'webhook_url', '' );
}

public function test_send_to_webhook_error_path() {
Expand All @@ -77,12 +75,10 @@ public function test_send_to_webhook_error_path() {
return new WP_Error( 'http_request_failed', 'Error Message' );
}, 10, 3 );

OptionsUtilities::update_module_option_key( Settings::SETTINGS_SLUG, 'webhook_url', 'https://webhook.site/this-url-doesnt-exist' );
OptionsUtilities::update_module_option_key( Settings::SETTINGS_SLUG, 'webhook_url', 'https://webhook.site/this-url-doesnt-exist', Settings::DEFAULT_SETTINGS_OPTIONS );

$response = Notifications::send_to_webhook( 'Test Message', 'status-change', '2024-09-19 00:26:50' );

$this->assertFalse( $response );

OptionsUtilities::update_module_option_key( Settings::SETTINGS_SLUG, 'webhook_url', '' );
}
}

0 comments on commit 5478576

Please sign in to comment.