Enhancement #1 Switched from in_array() to faster isset() for referer checking

This commit is contained in:
Stefan Kalscheuer 2016-08-21 19:21:22 +02:00
parent 1e0659e649
commit 19644dd62b
6 changed files with 62 additions and 23 deletions

@ -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

@ -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 ] );
}
}

@ -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 )
)
)
);
}
}
}

@ -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' );
}
}
/**
* 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 );
}
}
}
}

@ -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' );

15
views/settings_page.php Normal file → Executable file

@ -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'] ) ) {
<?php esc_html_e( 'Referer blacklist:', 'statify-blacklist' ); ?><br/>
<textarea cols="40" rows="5" name="statifyblacklist[referer]" id="statify-blacklist_referer"><?php
if ( isset( $statifyBlacklistUpdateResult ) && $statifyBlacklistUpdateResult !== false ) {
print esc_html( implode( "\r\n", $statifyBlacklistUpdateResult ) );
print esc_html( implode( "\r\n", array_keys( $statifyBlacklistUpdateResult ) ) );
} else {
print esc_html( implode( "\r\n", StatifyBlacklist::$_options['referer'] ) );
print esc_html( implode( "\r\n", array_keys( StatifyBlacklist::$_options['referer'] ) ) );
}
?></textarea>
<br />
?></textarea><br/>
<small>
(<?php esc_html_e( 'Add one domain (without subdomains) each line, e.g. example.com', 'statify-blacklist' ); ?>
)
@ -92,12 +91,12 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
<?php wp_nonce_field( 'statify-blacklist-settings' ); ?>
<p class="submit">
<input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes' ) ?>" />
<hr />
<input class="button-primary" type="submit" name="submit" value="<?php _e( 'Save Changes' ) ?>">
<hr>
<input class="button-secondary" type="submit" name="cleanUp"
value="<?php esc_html_e( 'CleanUp Database', 'statify-blacklist' ) ?>"
onclick="return confirm('Do you really want to apply filters to database? This cannot be undone.');">
<br />
<br>
<small><?php esc_html_e( 'Applies filter (even if disabled) to data stored in database. This cannot be undone!', 'statify-blacklist' ); ?></small>
</p>
</form>