From 19644dd62bba3cfd8e3f84bde313aa05d901b413 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 21 Aug 2016 19:21:22 +0200 Subject: [PATCH] Enhancement #1 Switched from in_array() to faster isset() for referer checking --- README.md | 3 +++ inc/statifyblacklist.class.php | 3 ++- inc/statifyblacklist_admin.class.php | 23 +++++++++-------- inc/statifyblacklist_system.class.php | 36 ++++++++++++++++++++++++--- statify-blacklist.php | 5 +++- views/settings_page.php | 15 ++++++----- 6 files changed, 62 insertions(+), 23 deletions(-) mode change 100644 => 100755 views/settings_page.php diff --git a/README.md b/README.md index bb05979..9499280 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,9 @@ If you like to have this feature, please leave a feature request in GitHub or th 1. Statify Blacklist settings page ## Changelog ## +### 1.2.0 / (work in progress) ### +* Switched from `in_array()` to faster `isset()` for referer checking + ### 1.1.2 / 17.08.2016 ### * Prepared for localization diff --git a/inc/statifyblacklist.class.php b/inc/statifyblacklist.class.php index a590846..ad58dcb 100644 --- a/inc/statifyblacklist.class.php +++ b/inc/statifyblacklist.class.php @@ -100,6 +100,7 @@ class StatifyBlacklist { * @return TRUE if referer matches blacklist. * * @since 1.0.0 + * @changed 1.2.0 */ public static function apply_blacklist_filter() { /* Skip if blacklist is inactive */ @@ -121,6 +122,6 @@ class StatifyBlacklist { /* Check blacklist */ - return in_array( $referer, $blacklist ); + return isset( $blacklist[ $referer ] ); } } diff --git a/inc/statifyblacklist_admin.class.php b/inc/statifyblacklist_admin.class.php index f2ad789..375551b 100644 --- a/inc/statifyblacklist_admin.class.php +++ b/inc/statifyblacklist_admin.class.php @@ -18,7 +18,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist { public static function update_options( $options ) { if ( isset( $options ) && current_user_can( 'manage_options' ) ) { /* Sanitize URLs and remove empty inputs */ - $givenReferer = $options['referer']; + $givenReferer = $options['referer']; $sanitizedReferer = self::sanitizeURLs( $givenReferer ); /* Abort on errors */ @@ -109,7 +109,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist { * Filter database for cleanup. * * @since 1.1.0 - * @changed 1.1.1 + * @changed 1.2.1 */ public static function cleanup_database() { /* Check user permissions */ @@ -123,7 +123,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist { $referer = self::sanitizeURLs( self::$_options['referer'] ); /* Build filter regexp */ - $refererRegexp = str_replace( '.', '\.', implode( '|', $referer ) ); + $refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) ); if ( ! empty( $refererRegexp ) ) { /* Execute filter on database */ $wpdb->query( @@ -144,15 +144,18 @@ class StatifyBlacklist_Admin extends StatifyBlacklist { * @return array sanitized array * * @since 1.1.1 + * @changed 1.2.0 */ private static function sanitizeURLs( $urls ) { - return array_filter( - array_map( - function ( $r ) { - return preg_replace( '/[^\da-z\.-]/i', '', filter_var( $r, FILTER_SANITIZE_URL ) ); - }, - $urls + return array_flip( + array_filter( + array_map( + function ( $r ) { + return preg_replace( '/[^\da-z\.-]/i', '', filter_var( $r, FILTER_SANITIZE_URL ) ); + }, + array_flip( $urls ) + ) ) ); } -} \ No newline at end of file +} diff --git a/inc/statifyblacklist_system.class.php b/inc/statifyblacklist_system.class.php index a8f6b43..2319aa9 100644 --- a/inc/statifyblacklist_system.class.php +++ b/inc/statifyblacklist_system.class.php @@ -28,7 +28,10 @@ class StatifyBlacklist_System extends StatifyBlacklist { switch_to_blog( $site_id ); add_option( 'statify-blacklist', - array() + array( + 'activate-referer' => 0, + 'referer' => array() + ) ); } @@ -36,7 +39,10 @@ class StatifyBlacklist_System extends StatifyBlacklist { } else { add_option( 'statify-blacklist', - array() + array( + 'activate-referer' => 0, + 'referer' => array() + ) ); } } @@ -66,4 +72,28 @@ class StatifyBlacklist_System extends StatifyBlacklist { delete_option( 'statify-blacklist' ); } -} \ No newline at end of file + + + /** + * Upgrade plugin options. + * + * @param object $upgrader Upgrader object (unused) + * @param array $options Options array + * + * @since 1.2.0 + */ + public static function upgrade() { + self::update_options(); + /* Check if config array is not associative (pre 1.2.0) */ + if ( array_keys( self::$_options['referer'] ) === range( 0, count( self::$_options['referer'] ) - 1 ) ) { + /* Flip referer array to make domains keys */ + $options = self::$_options; + $options['referer'] = array_flip( self::$_options['referer'] ); + if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) { + update_site_option( 'statify-blacklist', $options ); + } else { + update_option( 'statify-blacklist', $options ); + } + } + } +} diff --git a/statify-blacklist.php b/statify-blacklist.php index 8f7dd54..99bfd50 100644 --- a/statify-blacklist.php +++ b/statify-blacklist.php @@ -8,7 +8,7 @@ Author: Stefan Kalscheuer Author URI: https://stklcode.de Plugin URI: https://wordpress.org/plugins/statify-blacklist License: GPLv3 or later -Version: 1.1.2 +Version: 1.2.0 */ /* Quit */ @@ -26,6 +26,9 @@ register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) ); +/* Upgrade hook to v1.2.0 */ +register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'upgrade' ) ); + /* Autoload */ spl_autoload_register( 'statifyBlacklist_autoload' ); diff --git a/views/settings_page.php b/views/settings_page.php old mode 100644 new mode 100755 index 7e0f5aa..d5ba191 --- a/views/settings_page.php +++ b/views/settings_page.php @@ -28,7 +28,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) { $statifyBlacklistUpdateResult = StatifyBlacklist_Admin::update_options( array( 'active_referer' => (int) @$_POST['statifyblacklist']['active_referer'], - 'referer' => $referer + 'referer' => array_flip( $referer ) ) ); @@ -76,12 +76,11 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
-
+ ?>
( ) @@ -92,12 +91,12 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {

- -


+ +
-
+