fix storage of user agent filter list (#28)

The user agent filer list is not flipped with the actual values as keys
like the lists for referrer and target. Hence the numeric keys are
compared against the actual user agent. We now flip the values in the
upgrade hook.
This commit is contained in:
BananaSquishee 2021-05-28 11:11:30 +02:00 committed by GitHub
parent 6fdaa8bd5a
commit 6ffa650254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 4 deletions

View File

@ -103,6 +103,9 @@ This version should be compatible with latest WordPress 5.6.
## Changelog ## ## Changelog ##
### 1.6.1 / unreleased ###
* Fix storage of user agent filter list (#28, props @BananaSquishee)
### 1.6.0 / 09.12.2020 ### ### 1.6.0 / 09.12.2020 ###
Plugin renamed to _Statify Filter_. Plugin renamed to _Statify Filter_.

View File

@ -178,6 +178,30 @@ class StatifyBlacklist_System extends StatifyBlacklist {
self::update_options(); self::update_options();
} }
// Version older than 1.6.
if ( self::$options['version'] < 1.6 ) {
$options = self::$options;
if ( ! isset( $options['ua'] ) ) {
$options['ua'] = array(
'active' => 0,
'regexp' => 0,
'blacklist' => array(),
);
} elseif ( ! isset( $options['ua']['blacklist'] ) ) {
$options['ua']['blacklist'] = array();
} elseif ( isset( $options['ua'] ) ) {
// User agent strings got stored incorrectly in 1.6.0 - luckily the version was not updated, either.
$options['ua']['blacklist'] = array_flip( $options['ua']['blacklist'] );
}
$options['version'] = 1.6;
if ( self::$multisite ) {
update_site_option( 'statify-blacklist', $options );
} else {
update_option( 'statify-blacklist', $options );
}
self::update_options();
}
// Version older than current major release. // Version older than current major release.
if ( self::VERSION_MAIN > self::$options['version'] ) { if ( self::VERSION_MAIN > self::$options['version'] ) {
// Merge default options with current config, assuming only additive changes. // Merge default options with current config, assuming only additive changes.

View File

@ -24,7 +24,7 @@ class StatifyBlacklist {
* @since 1.4.0 * @since 1.4.0
* @var int VERSION_MAIN * @var int VERSION_MAIN
*/ */
const VERSION_MAIN = 1.4; const VERSION_MAIN = 1.6;
/** /**
* Operation mode "normal". * Operation mode "normal".

View File

@ -42,10 +42,12 @@ class StatifyBlacklist_System_Test extends PHPUnit\Framework\TestCase {
$options_updated = get_option( 'statify-blacklist' ); $options_updated = get_option( 'statify-blacklist' );
// Verify size against default options (no junk left). // Verify size against default options (no junk left).
$this->assertEquals( 4, count( $options_updated ) ); $this->assertEquals( 5, count( $options_updated ) );
$this->assertEquals( 4, count( $options_updated['referer'] ) ); $this->assertEquals( 4, count( $options_updated['referer'] ) );
$this->assertEquals( 4, count( $options_updated['target'] ) ); $this->assertEquals( 4, count( $options_updated['target'] ) );
$this->assertEquals( 2, count( $options_updated['ip'] ) ); $this->assertEquals( 2, count( $options_updated['ip'] ) );
$this->assertEquals( 3, count( $options_updated['ua'] ) );
$this->assertEquals( 1.6, $options_updated['version'] );
// Verify that original attributes are unchanged. // Verify that original attributes are unchanged.
$this->assertEquals( $options13['active_referer'], $options_updated['referer']['active'] ); $this->assertEquals( $options13['active_referer'], $options_updated['referer']['active'] );
@ -60,8 +62,31 @@ class StatifyBlacklist_System_Test extends PHPUnit\Framework\TestCase {
$this->assertEquals( array(), $options_updated['target']['blacklist'] ); $this->assertEquals( array(), $options_updated['target']['blacklist'] );
$this->assertEquals( 0, $options_updated['ip']['active'] ); $this->assertEquals( 0, $options_updated['ip']['active'] );
$this->assertEquals( array(), $options_updated['ip']['blacklist'] ); $this->assertEquals( array(), $options_updated['ip']['blacklist'] );
$this->assertEquals( 0, $options_updated['ua']['active'] );
$this->assertEquals( 0, $options_updated['ua']['regexp'] );
$this->assertEquals( array(), $options_updated['ua']['blacklist'] );
// Verify that version number has changed to current release. // Verify that version number has changed to current release.
$this->assertEquals( StatifyBlacklist::VERSION_MAIN, $options_updated['version'] ); $this->assertEquals( StatifyBlacklist::VERSION_MAIN, $options_updated['version'] );
// Test upgrade of incorrectly stored user agent list in 1.6.
$options_updated['version'] = 1.4;
$options_updated['ua']['blacklist'] = array( 'user agent 1', 'user agent 2' );
update_option( 'statify-blacklist', $options_updated );
// Execute upgrade.
StatifyBlacklist_System::upgrade();
// Retrieve updated options.
$options_updated = get_option( 'statify-blacklist' );
$this->assertEquals(
array(
'user agent 1' => 0,
'user agent 2' => 1,
),
$options_updated['ua']['blacklist']
);
$this->assertEquals( 1.6, $options_updated['version'] );
$this->assertEquals( StatifyBlacklist::VERSION_MAIN, $options_updated['version'] );
} }
} }

View File

@ -88,7 +88,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
); );
} }
// TODO: Extract user agent array. // Extract user agent array.
if ( isset( $_POST['statifyblacklist']['ua']['blacklist'] ) ) { if ( isset( $_POST['statifyblacklist']['ua']['blacklist'] ) ) {
$ua_string = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['ua']['blacklist'] ) ); $ua_string = sanitize_textarea_field( wp_unslash( $_POST['statifyblacklist']['ua']['blacklist'] ) );
} }
@ -139,7 +139,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
? (int) $_POST['statifyblacklist']['ua']['active'] : 0, ? (int) $_POST['statifyblacklist']['ua']['active'] : 0,
'regexp' => isset( $_POST['statifyblacklist']['ua']['regexp'] ) 'regexp' => isset( $_POST['statifyblacklist']['ua']['regexp'] )
? (int) $_POST['statifyblacklist']['ua']['regexp'] : 0, ? (int) $_POST['statifyblacklist']['ua']['regexp'] : 0,
'blacklist' => $ua, 'blacklist' => array_flip( $ua ),
), ),
'version' => StatifyBlacklist::VERSION_MAIN, 'version' => StatifyBlacklist::VERSION_MAIN,
) )