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

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

View File

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

View File

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

View File

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