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

View File

@ -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 1. Statify Blacklist settings page
## Changelog ## ## Changelog ##
### 1.2.0 / (work in progress) ###
* Switched from `in_array()` to faster `isset()` for referer checking
### 1.1.2 / 17.08.2016 ### ### 1.1.2 / 17.08.2016 ###
* Prepared for localization * Prepared for localization

View File

@ -100,6 +100,7 @@ class StatifyBlacklist {
* @return TRUE if referer matches blacklist. * @return TRUE if referer matches blacklist.
* *
* @since 1.0.0 * @since 1.0.0
* @changed 1.2.0
*/ */
public static function apply_blacklist_filter() { public static function apply_blacklist_filter() {
/* Skip if blacklist is inactive */ /* Skip if blacklist is inactive */
@ -121,6 +122,6 @@ class StatifyBlacklist {
/* Check blacklist */ /* 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 ) { public static function update_options( $options ) {
if ( isset( $options ) && current_user_can( 'manage_options' ) ) { if ( isset( $options ) && current_user_can( 'manage_options' ) ) {
/* Sanitize URLs and remove empty inputs */ /* Sanitize URLs and remove empty inputs */
$givenReferer = $options['referer']; $givenReferer = $options['referer'];
$sanitizedReferer = self::sanitizeURLs( $givenReferer ); $sanitizedReferer = self::sanitizeURLs( $givenReferer );
/* Abort on errors */ /* Abort on errors */
@ -109,7 +109,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* Filter database for cleanup. * Filter database for cleanup.
* *
* @since 1.1.0 * @since 1.1.0
* @changed 1.1.1 * @changed 1.2.1
*/ */
public static function cleanup_database() { public static function cleanup_database() {
/* Check user permissions */ /* Check user permissions */
@ -123,7 +123,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
$referer = self::sanitizeURLs( self::$_options['referer'] ); $referer = self::sanitizeURLs( self::$_options['referer'] );
/* Build filter regexp */ /* Build filter regexp */
$refererRegexp = str_replace( '.', '\.', implode( '|', $referer ) ); $refererRegexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) );
if ( ! empty( $refererRegexp ) ) { if ( ! empty( $refererRegexp ) ) {
/* Execute filter on database */ /* Execute filter on database */
$wpdb->query( $wpdb->query(
@ -144,15 +144,18 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* @return array sanitized array * @return array sanitized array
* *
* @since 1.1.1 * @since 1.1.1
* @changed 1.2.0
*/ */
private static function sanitizeURLs( $urls ) { private static function sanitizeURLs( $urls ) {
return array_filter( return array_flip(
array_map( array_filter(
function ( $r ) { array_map(
return preg_replace( '/[^\da-z\.-]/i', '', filter_var( $r, FILTER_SANITIZE_URL ) ); function ( $r ) {
}, return preg_replace( '/[^\da-z\.-]/i', '', filter_var( $r, FILTER_SANITIZE_URL ) );
$urls },
array_flip( $urls )
)
) )
); );
} }
} }

View File

@ -28,7 +28,10 @@ class StatifyBlacklist_System extends StatifyBlacklist {
switch_to_blog( $site_id ); switch_to_blog( $site_id );
add_option( add_option(
'statify-blacklist', 'statify-blacklist',
array() array(
'activate-referer' => 0,
'referer' => array()
)
); );
} }
@ -36,7 +39,10 @@ class StatifyBlacklist_System extends StatifyBlacklist {
} else { } else {
add_option( add_option(
'statify-blacklist', 'statify-blacklist',
array() array(
'activate-referer' => 0,
'referer' => array()
)
); );
} }
} }
@ -66,4 +72,28 @@ class StatifyBlacklist_System extends StatifyBlacklist {
delete_option( 'statify-blacklist' ); 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 );
}
}
}
}

View File

@ -8,7 +8,7 @@ Author: Stefan Kalscheuer
Author URI: https://stklcode.de Author URI: https://stklcode.de
Plugin URI: https://wordpress.org/plugins/statify-blacklist Plugin URI: https://wordpress.org/plugins/statify-blacklist
License: GPLv3 or later License: GPLv3 or later
Version: 1.1.2 Version: 1.2.0
*/ */
/* Quit */ /* Quit */
@ -26,6 +26,9 @@ register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System
register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) ); 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 */ /* Autoload */
spl_autoload_register( 'statifyBlacklist_autoload' ); spl_autoload_register( 'statifyBlacklist_autoload' );

15
views/settings_page.php Normal file → Executable file
View File

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