Skip to content

Commit

Permalink
Fix custom settings fields removed from saved settings
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Mar 31, 2020
1 parent 681cecf commit 215e79e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Text Domain: wp-auth0
*/

define( 'WPA0_VERSION', '4.0.0-beta.2' );
define( 'WPA0_VERSION', '4.0.0-beta.3' );
define( 'AUTH0_DB_VERSION', 23 );

define( 'WPA0_PLUGIN_FILE', __FILE__ );
Expand Down
18 changes: 17 additions & 1 deletion lib/admin/WP_Auth0_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class WP_Auth0_Admin {

const OPT_SECTIONS = [ 'basic', 'features', 'appearance', 'advanced' ];

protected $a0_options;

protected $router;
Expand Down Expand Up @@ -102,8 +104,22 @@ public function input_validator( array $input ) {
$input[ $key ] = $this->a0_options->get_constant_val( $key );
}

// Remove unknown keys.
$option_keys = $this->a0_options->get_defaults( true );

// Look for custom settings fields.
$custom_opts = [];
foreach ( self::OPT_SECTIONS as $section ) {
$custom_opts = array_merge( $custom_opts, apply_filters( 'auth0_settings_fields', [], $section ) );
}

// Merge in any custom setting option keys.
foreach ( $custom_opts as $custom_opt ) {
if ( $custom_opt && $custom_opt['opt'] ) {
$option_keys[] = $custom_opt['opt'];
}
}

// Remove unknown keys.
foreach ( $input as $key => $val ) {
if ( ! in_array( $key, $option_keys ) ) {
unset( $input[ $key ] );
Expand Down
2 changes: 1 addition & 1 deletion templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<?php settings_fields( WP_Auth0_Options::Instance()->get_options_name() . '_basic' ); ?>

<div class="tab-content">
<?php foreach ( [ 'basic', 'features', 'appearance', 'advanced' ] as $tab ) : ?>
<?php foreach ( WP_Auth0_Admin::OPT_SECTIONS as $tab ) : ?>
<div class="tab-pane" id="panel-<?php echo $tab; ?>">
<?php do_settings_sections( WP_Auth0_Options::Instance()->get_options_name() . '_' . $tab ); ?>
</div>
Expand Down
28 changes: 28 additions & 0 deletions tests/testAdminSettingsValidationPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,32 @@ public function testThatAuth0SettingIsRegistered() {
$wp_registered_settings['wp_auth0_settings']['sanitize_callback'][1]
);
}

public function testThatCustomSettingsFieldsAreKeptDuringValidation() {
$admin = new WP_Auth0_Admin(self::$opts, new WP_Auth0_Routes(self::$opts));

add_filter( 'auth0_settings_fields', [$this, 'addCustomSettings'], 10, 2 );
$result = $admin->input_validator( ['test_opt_name' => '__test_value__'] );
remove_filter( 'auth0_settings_fields', [$this, 'addCustomSettings'], 10 );

$this->assertArrayHasKey('test_opt_name', $result);
$this->assertEquals( '__test_value__', $result['test_opt_name'] );
}

public function testThatUnknownSettingsFieldsAreRemovedDuringValidation() {
$admin = new WP_Auth0_Admin(self::$opts, new WP_Auth0_Routes(self::$opts));

$result = $admin->input_validator( ['unknown_opt_name' => '__test_value__'] );

$this->assertArrayNotHasKey('unknown_opt_name', $result);
}

public function addCustomSettings( $options, $id ) {
// From https://auth0.com/docs/cms/wordpress/extending#auth0_settings_fields
if ( 'basic' === $id ) {
$options[] = ['opt' => 'test_opt_name'];
}

return $options;
}
}

0 comments on commit 215e79e

Please sign in to comment.