remove underscore prefix from options field

This commit is contained in:
Stefan Kalscheuer 2019-10-06 17:52:08 +02:00
parent a88a89c442
commit 84cf79fd04
4 changed files with 55 additions and 55 deletions

View File

@ -229,20 +229,20 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
} }
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
$clean_ref = ( 1 === self::$_options['referer']['cron'] ); $clean_ref = ( 1 === self::$options['referer']['cron'] );
$clean_trg = ( 1 === self::$_options['target']['cron'] ); $clean_trg = ( 1 === self::$options['target']['cron'] );
} else { } else {
$clean_ref = true; $clean_ref = true;
$clean_trg = true; $clean_trg = true;
} }
if ( $clean_ref ) { if ( $clean_ref ) {
if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) { if ( isset( self::$options['referer']['regexp'] ) && self::$options['referer']['regexp'] > 0 ) {
// Merge given regular expressions into one. // Merge given regular expressions into one.
$referer_regexp = implode( '|', array_keys( self::$_options['referer']['blacklist'] ) ); $referer_regexp = implode( '|', array_keys( self::$options['referer']['blacklist'] ) );
} else { } else {
// Sanitize URLs. // Sanitize URLs.
$referer = self::sanitize_urls( self::$_options['referer']['blacklist'] ); $referer = self::sanitize_urls( self::$options['referer']['blacklist'] );
// Build filter regexp. // Build filter regexp.
$referer_regexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) ); $referer_regexp = str_replace( '.', '\.', implode( '|', array_flip( $referer ) ) );
@ -250,12 +250,12 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
} }
if ( $clean_trg ) { if ( $clean_trg ) {
if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) { if ( isset( self::$options['target']['regexp'] ) && self::$options['target']['regexp'] > 0 ) {
// Merge given regular expressions into one. // Merge given regular expressions into one.
$target_regexp = implode( '|', array_keys( self::$_options['target']['blacklist'] ) ); $target_regexp = implode( '|', array_keys( self::$options['target']['blacklist'] ) );
} else { } else {
// Build filter regexp. // Build filter regexp.
$target_regexp = str_replace( '.', '\.', implode( '|', array_flip( self::$_options['target']['blacklist'] ) ) ); $target_regexp = str_replace( '.', '\.', implode( '|', array_flip( self::$options['target']['blacklist'] ) ) );
} }
} }
@ -268,7 +268,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
$wpdb->query( $wpdb->query(
$wpdb->prepare( $wpdb->prepare(
"DELETE FROM `$wpdb->statify` WHERE " "DELETE FROM `$wpdb->statify` WHERE "
. ( ( 1 === self::$_options['referer']['regexp'] ) ? ' BINARY ' : '' ) . ( ( 1 === self::$options['referer']['regexp'] ) ? ' BINARY ' : '' )
. 'referrer REGEXP %s', $referer_regexp . 'referrer REGEXP %s', $referer_regexp
) )
); );
@ -277,7 +277,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
$wpdb->query( $wpdb->query(
$wpdb->prepare( $wpdb->prepare(
"DELETE FROM `$wpdb->statify` WHERE " "DELETE FROM `$wpdb->statify` WHERE "
. ( ( 1 === self::$_options['target']['regexp'] ) ? ' BINARY ' : '' ) . ( ( 1 === self::$options['target']['regexp'] ) ? ' BINARY ' : '' )
. 'target REGEXP %s', $target_regexp . 'target REGEXP %s', $target_regexp
) )
); );

View File

@ -134,10 +134,10 @@ class StatifyBlacklist_System extends StatifyBlacklist {
public static function upgrade() { public static function upgrade() {
self::update_options(); self::update_options();
// Check if config array is not associative (pre 1.2.0). // Check if config array is not associative (pre 1.2.0).
if ( array_keys( self::$_options['referer'] ) === range( 0, count( self::$_options['referer'] ) - 1 ) ) { if ( array_keys( self::$options['referer'] ) === range( 0, count( self::$options['referer'] ) - 1 ) ) {
// Flip referer array to make domains keys. // Flip referer array to make domains keys.
$options = self::$_options; $options = self::$options;
$options['referer'] = array_flip( self::$_options['referer'] ); $options['referer'] = array_flip( self::$options['referer'] );
if ( self::$multisite ) { if ( self::$multisite ) {
update_site_option( 'statify-blacklist', $options ); update_site_option( 'statify-blacklist', $options );
} else { } else {
@ -146,14 +146,14 @@ class StatifyBlacklist_System extends StatifyBlacklist {
} }
// Version not set (pre 1.3.0) or older than 1.4. // Version not set (pre 1.3.0) or older than 1.4.
if ( ! isset( self::$_options['version'] ) || self::$_options['version'] < 1.4 ) { if ( ! isset( self::$options['version'] ) || self::$options['version'] < 1.4 ) {
// Upgrade options to new schema. // Upgrade options to new schema.
$options = array( $options = array(
'referer' => array( 'referer' => array(
'active' => self::$_options['active_referer'], 'active' => self::$options['active_referer'],
'cron' => self::$_options['cron_referer'], 'cron' => self::$options['cron_referer'],
'regexp' => self::$_options['referer_regexp'], 'regexp' => self::$options['referer_regexp'],
'blacklist' => self::$_options['referer'], 'blacklist' => self::$options['referer'],
), ),
'target' => array( 'target' => array(
'active' => 0, 'active' => 0,
@ -176,9 +176,9 @@ class StatifyBlacklist_System extends StatifyBlacklist {
} }
// Version older than current major release. // Version older than current major release.
if ( self::VERSION_MAIN > self::$_options['version'] ) { if ( self::VERSION_MAIN > self::$options['version'] ) {
// Merge default options with current config, assuming only additive changes. // Merge default options with current config, assuming only additive changes.
$options = array_merge_recursive( self::default_options(), self::$_options ); $options = array_merge_recursive( self::default_options(), self::$options );
$options['version'] = self::VERSION_MAIN; $options['version'] = self::VERSION_MAIN;
if ( self::$multisite ) { if ( self::$multisite ) {
update_site_option( 'statify-blacklist', $options ); update_site_option( 'statify-blacklist', $options );

View File

@ -61,9 +61,9 @@ class StatifyBlacklist {
* Plugin options. * Plugin options.
* *
* @since 1.0.0 * @since 1.0.0
* @var array $_options * @var array $options
*/ */
public static $_options; public static $options;
/** /**
* Multisite Status. * Multisite Status.
@ -93,7 +93,7 @@ class StatifyBlacklist {
self::update_options(); self::update_options();
// Add Filter to statify hook if enabled. // Add Filter to statify hook if enabled.
if ( 0 !== self::$_options['referer']['active'] || 0 !== self::$_options['target']['active'] || 0 !== self::$_options['ip']['active'] ) { if ( 0 !== self::$options['referer']['active'] || 0 !== self::$options['target']['active'] || 0 !== self::$options['ip']['active'] ) {
add_filter( 'statify__skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) ); add_filter( 'statify__skip_tracking', array( 'StatifyBlacklist', 'apply_blacklist_filter' ) );
} }
@ -104,7 +104,7 @@ class StatifyBlacklist {
// CronJob to clean up database. // CronJob to clean up database.
if ( defined( 'DOING_CRON' ) && DOING_CRON && if ( defined( 'DOING_CRON' ) && DOING_CRON &&
( 1 === self::$_options['referer']['cron'] || 1 === self::$_options['target']['cron'] ) ) { ( 1 === self::$options['referer']['cron'] || 1 === self::$options['target']['cron'] ) ) {
add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) ); add_action( 'statify_cleanup', array( 'StatifyBlacklist_Admin', 'cleanup_database' ) );
} }
} }
@ -125,7 +125,7 @@ class StatifyBlacklist {
} else { } else {
$o = get_option( 'statify-blacklist' ); $o = get_option( 'statify-blacklist' );
} }
self::$_options = wp_parse_args( $o, self::default_options() ); self::$options = wp_parse_args( $o, self::default_options() );
} }
/** /**
@ -166,9 +166,9 @@ class StatifyBlacklist {
*/ */
public static function apply_blacklist_filter() { public static function apply_blacklist_filter() {
// Referer blacklist. // Referer blacklist.
if ( isset( self::$_options['referer']['active'] ) && 0 !== self::$_options['referer']['active'] ) { if ( isset( self::$options['referer']['active'] ) && 0 !== self::$options['referer']['active'] ) {
// Determine filter mode. // Determine filter mode.
$mode = isset( self::$_options['referer']['regexp'] ) ? intval( self::$_options['referer']['regexp'] ) : 0; $mode = isset( self::$options['referer']['regexp'] ) ? intval( self::$options['referer']['regexp'] ) : 0;
// Get full referer string. // Get full referer string.
$referer = wp_get_raw_referer(); $referer = wp_get_raw_referer();
@ -183,8 +183,8 @@ class StatifyBlacklist {
case self::MODE_REGEX_CI: case self::MODE_REGEX_CI:
// Merge given regular expressions into one. // Merge given regular expressions into one.
$regexp = self::regex( $regexp = self::regex(
array_keys( self::$_options['referer']['blacklist'] ), array_keys( self::$options['referer']['blacklist'] ),
self::MODE_REGEX_CI === self::$_options['referer']['regexp'] self::MODE_REGEX_CI === self::$options['referer']['regexp']
); );
// Check blacklist (no return to continue filtering #12). // Check blacklist (no return to continue filtering #12).
@ -196,7 +196,7 @@ class StatifyBlacklist {
// Keyword filter since 1.5.0 (#15). // Keyword filter since 1.5.0 (#15).
case self::MODE_KEYWORD: case self::MODE_KEYWORD:
// Get blacklist. // Get blacklist.
$blacklist = self::$_options['referer']['blacklist']; $blacklist = self::$options['referer']['blacklist'];
foreach ( array_keys( $blacklist ) as $keyword ) { foreach ( array_keys( $blacklist ) as $keyword ) {
if ( false !== strpos( strtolower( $referer ), strtolower( $keyword ) ) ) { if ( false !== strpos( strtolower( $referer ), strtolower( $keyword ) ) ) {
@ -212,7 +212,7 @@ class StatifyBlacklist {
$referer = strtolower( ( isset( $referer['host'] ) ? $referer['host'] : '' ) ); $referer = strtolower( ( isset( $referer['host'] ) ? $referer['host'] : '' ) );
// Get blacklist. // Get blacklist.
$blacklist = self::$_options['referer']['blacklist']; $blacklist = self::$options['referer']['blacklist'];
// Check blacklist. // Check blacklist.
if ( isset( $blacklist[ $referer ] ) ) { if ( isset( $blacklist[ $referer ] ) ) {
@ -222,17 +222,17 @@ class StatifyBlacklist {
} }
// Target blacklist (since 1.4.0). // Target blacklist (since 1.4.0).
if ( isset( self::$_options['target']['active'] ) && 0 !== self::$_options['target']['active'] ) { if ( isset( self::$options['target']['active'] ) && 0 !== self::$options['target']['active'] ) {
// Regular Expression filtering since 1.3.0. // Regular Expression filtering since 1.3.0.
if ( isset( self::$_options['target']['regexp'] ) && 0 < self::$_options['target']['regexp'] ) { if ( isset( self::$options['target']['regexp'] ) && 0 < self::$options['target']['regexp'] ) {
// Get full referer string. // Get full referer string.
// @codingStandardsIgnoreStart The globals are checked. // @codingStandardsIgnoreStart The globals are checked.
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' ); $target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' );
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
// Merge given regular expressions into one. // Merge given regular expressions into one.
$regexp = self::regex( $regexp = self::regex(
array_keys( self::$_options['target']['blacklist'] ), array_keys( self::$options['target']['blacklist'] ),
self::MODE_REGEX_CI === self::$_options['target']['regexp'] self::MODE_REGEX_CI === self::$options['target']['regexp']
); );
// Check blacklist (no return to continue filtering #12). // Check blacklist (no return to continue filtering #12).
@ -245,7 +245,7 @@ class StatifyBlacklist {
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' ); $target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' );
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
// Get blacklist. // Get blacklist.
$blacklist = self::$_options['target']['blacklist']; $blacklist = self::$options['target']['blacklist'];
// Check blacklist. // Check blacklist.
if ( isset( $blacklist[ $target ] ) ) { if ( isset( $blacklist[ $target ] ) ) {
return true; return true;
@ -254,10 +254,10 @@ class StatifyBlacklist {
} }
// IP blacklist (since 1.4.0). // IP blacklist (since 1.4.0).
if ( isset( self::$_options['ip']['active'] ) && 0 !== self::$_options['ip']['active'] ) { if ( isset( self::$options['ip']['active'] ) && 0 !== self::$options['ip']['active'] ) {
$ip = self::get_ip(); $ip = self::get_ip();
if ( false !== ( $ip ) ) { if ( false !== ( $ip ) ) {
foreach ( self::$_options['ip']['blacklist'] as $net ) { foreach ( self::$options['ip']['blacklist'] as $net ) {
if ( self::cidr_match( $ip, $net ) ) { if ( self::cidr_match( $ip, $net ) ) {
return true; return true;
} }

View File

@ -48,7 +48,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
*/ */
public function test_referer_filter() { public function test_referer_filter() {
// Prepare Options: 2 blacklisted domains, disabled. // Prepare Options: 2 blacklisted domains, disabled.
StatifyBlacklist::$_options = array( StatifyBlacklist::$options = array(
'referer' => array( 'referer' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
@ -88,7 +88,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
// Activate filter and run tests again. // Activate filter and run tests again.
StatifyBlacklist::$_options['referer']['active'] = 1; StatifyBlacklist::$options['referer']['active'] = 1;
unset( $_SERVER['HTTP_REFERER'] ); unset( $_SERVER['HTTP_REFERER'] );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
@ -110,7 +110,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
*/ */
public function test_referer_regex_filter() { public function test_referer_regex_filter() {
// Prepare Options: 2 regular expressions. // Prepare Options: 2 regular expressions.
StatifyBlacklist::$_options = array( StatifyBlacklist::$options = array(
'referer' => array( 'referer' => array(
'active' => 1, 'active' => 1,
'cron' => 0, 'cron' => 0,
@ -156,7 +156,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
// Set RegExp filter to case insensitive. // Set RegExp filter to case insensitive.
StatifyBlacklist::$_options['referer']['regexp'] = 2; StatifyBlacklist::$options['referer']['regexp'] = 2;
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
} }
@ -167,7 +167,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
*/ */
public function test_referer_keyword_filter() { public function test_referer_keyword_filter() {
// Prepare Options: 2 regular expressions. // Prepare Options: 2 regular expressions.
StatifyBlacklist::$_options = array( StatifyBlacklist::$options = array(
'referer' => array( 'referer' => array(
'active' => 1, 'active' => 1,
'cron' => 0, 'cron' => 0,
@ -415,7 +415,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
*/ */
public function test_ip_filter() { public function test_ip_filter() {
// Prepare Options: 2 blacklisted IPs, disabled. // Prepare Options: 2 blacklisted IPs, disabled.
StatifyBlacklist::$_options = array( StatifyBlacklist::$options = array(
'referer' => array( 'referer' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
@ -445,7 +445,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
$_SERVER['REMOTE_ADDR'] = '192.0.2.123'; $_SERVER['REMOTE_ADDR'] = '192.0.2.123';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
// Activate filter. // Activate filter.
StatifyBlacklist::$_options['ip']['active'] = 1; StatifyBlacklist::$options['ip']['active'] = 1;
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
// Try matching v6 address. // Try matching v6 address.
$_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::1'; $_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::1';
@ -456,11 +456,11 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
$_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::2'; $_SERVER['REMOTE_ADDR'] = '2001:db8:a0b:12f0::2';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
// Subnet matching. // Subnet matching.
StatifyBlacklist::$_options['ip']['blacklist'] = array( StatifyBlacklist::$options['ip']['blacklist'] = array(
'192.0.2.0/25', '192.0.2.0/25',
'2001:db8:a0b:12f0::/96', '2001:db8:a0b:12f0::/96',
); );
$_SERVER['REMOTE_ADDR'] = '192.0.2.123'; $_SERVER['REMOTE_ADDR'] = '192.0.2.123';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REMOTE_ADDR'] = '192.0.2.234'; $_SERVER['REMOTE_ADDR'] = '192.0.2.234';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
@ -487,7 +487,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
*/ */
public function test_target_filter() { public function test_target_filter() {
// Prepare Options: 2 blacklisted domains, disabled. // Prepare Options: 2 blacklisted domains, disabled.
StatifyBlacklist::$_options = array( StatifyBlacklist::$options = array(
'referer' => array( 'referer' => array(
'active' => 0, 'active' => 0,
'cron' => 0, 'cron' => 0,
@ -530,7 +530,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
// Activate filter and run tests again. // Activate filter and run tests again.
StatifyBlacklist::$_options['target']['active'] = 1; StatifyBlacklist::$options['target']['active'] = 1;
unset( $_SERVER['REQUEST_URI'] ); unset( $_SERVER['REQUEST_URI'] );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
@ -562,7 +562,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
*/ */
public function test_combined_filters() { public function test_combined_filters() {
// Prepare Options: simple referer + simple target + ip. // Prepare Options: simple referer + simple target + ip.
StatifyBlacklist::$_options = array( StatifyBlacklist::$options = array(
'referer' => array( 'referer' => array(
'active' => 1, 'active' => 1,
'cron' => 0, 'cron' => 0,
@ -614,10 +614,10 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
$_SERVER['REMOTE_ADDR'] = '192.0.2.234'; $_SERVER['REMOTE_ADDR'] = '192.0.2.234';
// Same for RegExp filters. // Same for RegExp filters.
StatifyBlacklist::$_options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX; StatifyBlacklist::$options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX;
StatifyBlacklist::$_options['referer']['blacklist'] = array( 'example\.com' => 0 ); StatifyBlacklist::$options['referer']['blacklist'] = array( 'example\.com' => 0 );
StatifyBlacklist::$_options['target']['regexp'] = StatifyBlacklist::MODE_REGEX; StatifyBlacklist::$options['target']['regexp'] = StatifyBlacklist::MODE_REGEX;
StatifyBlacklist::$_options['target']['blacklist'] = array( '/excluded/.*' => 0 ); StatifyBlacklist::$options['target']['blacklist'] = array( '/excluded/.*' => 0 );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['HTTP_REFERER'] = 'https://example.com'; $_SERVER['HTTP_REFERER'] = 'https://example.com';
@ -625,7 +625,7 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
// Check case-insensitive match. // Check case-insensitive match.
$_SERVER['HTTP_REFERER'] = 'https://eXaMpLe.com'; $_SERVER['HTTP_REFERER'] = 'https://eXaMpLe.com';
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() ); $this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
StatifyBlacklist::$_options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX_CI; StatifyBlacklist::$options['referer']['regexp'] = StatifyBlacklist::MODE_REGEX_CI;
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() ); $this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['HTTP_REFERER'] = 'https://example.net'; $_SERVER['HTTP_REFERER'] = 'https://example.net';
$_SERVER['REQUEST_URI'] = '/excluded/page/'; $_SERVER['REQUEST_URI'] = '/excluded/page/';