From 6ffa6502545a669c84e1e7eab57553d6f7283c6e Mon Sep 17 00:00:00 2001 From: BananaSquishee <80574305+BananaSquishee@users.noreply.github.com> Date: Fri, 28 May 2021 11:11:30 +0200 Subject: [PATCH] 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. --- README.md | 3 +++ inc/class-statifyblacklist-system.php | 24 ++++++++++++++++++++++++ inc/class-statifyblacklist.php | 2 +- test/StatifyBlacklist_System_Test.php | 27 ++++++++++++++++++++++++++- views/settings-page.php | 4 ++-- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 720b372..7923fae 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,9 @@ This version should be compatible with latest WordPress 5.6. ## Changelog ## +### 1.6.1 / unreleased ### +* Fix storage of user agent filter list (#28, props @BananaSquishee) + ### 1.6.0 / 09.12.2020 ### Plugin renamed to _Statify Filter_. diff --git a/inc/class-statifyblacklist-system.php b/inc/class-statifyblacklist-system.php index 815de57..64a6fab 100644 --- a/inc/class-statifyblacklist-system.php +++ b/inc/class-statifyblacklist-system.php @@ -178,6 +178,30 @@ class StatifyBlacklist_System extends StatifyBlacklist { 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. if ( self::VERSION_MAIN > self::$options['version'] ) { // Merge default options with current config, assuming only additive changes. diff --git a/inc/class-statifyblacklist.php b/inc/class-statifyblacklist.php index 5c1d2f7..d059035 100644 --- a/inc/class-statifyblacklist.php +++ b/inc/class-statifyblacklist.php @@ -24,7 +24,7 @@ class StatifyBlacklist { * @since 1.4.0 * @var int VERSION_MAIN */ - const VERSION_MAIN = 1.4; + const VERSION_MAIN = 1.6; /** * Operation mode "normal". diff --git a/test/StatifyBlacklist_System_Test.php b/test/StatifyBlacklist_System_Test.php index 9a13a5b..f94fe89 100644 --- a/test/StatifyBlacklist_System_Test.php +++ b/test/StatifyBlacklist_System_Test.php @@ -42,10 +42,12 @@ class StatifyBlacklist_System_Test extends PHPUnit\Framework\TestCase { $options_updated = get_option( 'statify-blacklist' ); // 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['target'] ) ); $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. $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( 0, $options_updated['ip']['active'] ); $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. $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'] ); } } diff --git a/views/settings-page.php b/views/settings-page.php index b7debe2..26407fb 100755 --- a/views/settings-page.php +++ b/views/settings-page.php @@ -88,7 +88,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) { ); } - // TODO: Extract user agent array. + // Extract user agent array. if ( isset( $_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, 'regexp' => isset( $_POST['statifyblacklist']['ua']['regexp'] ) ? (int) $_POST['statifyblacklist']['ua']['regexp'] : 0, - 'blacklist' => $ua, + 'blacklist' => array_flip( $ua ), ), 'version' => StatifyBlacklist::VERSION_MAIN, )