Minor corrections

This commit is contained in:
Stefan Kalscheuer 2017-06-02 18:14:06 +02:00
parent e66e3745a5
commit 9f9c7af298
6 changed files with 66 additions and 41 deletions

View File

@ -6,7 +6,8 @@ defined( 'ABSPATH' ) OR exit;
/**
* Statify Blacklist
*
* @since 1.0.0
* @since 1.0.0
* @version 1.4.0~dev
*/
class StatifyBlacklist {
/**

View File

@ -6,7 +6,8 @@ defined( 'ABSPATH' ) OR exit;
/**
* Statify Blacklist admin configuration
*
* @since 1.0.0
* @since 1.0.0
* @version 1.4.0~dev
*/
class StatifyBlacklist_Admin extends StatifyBlacklist {
/**
@ -114,12 +115,12 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* Filter database for cleanup.
*
* @since 1.1.0
* @changed 1.3.0
* @changed 1.4.0
*/
public static function cleanup_database() {
/* Check user permissions */
if ( ! current_user_can( 'manage_options' ) && ! ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
die( _e( 'Are you sure you want to do this?' ) );
die( __( 'Are you sure you want to do this?' ) );
}
global $wpdb;

View File

@ -6,7 +6,8 @@ defined( 'ABSPATH' ) OR exit;
/**
* Statify Blacklist system configuration
*
* @since 1.0.0
* @since 1.0.0
* @version 1.4.0~dev
*/
class StatifyBlacklist_System extends StatifyBlacklist {
@ -15,26 +16,27 @@ class StatifyBlacklist_System extends StatifyBlacklist {
/**
* Plugin install handler.
*
* @since 1.0.0
* @since 1.0.0
* @changed 1.4.0
*
* @param bool $network_wide Whether the plugin was activated network-wide or not.
*/
public static function install( $network_wide = false ) {
global $wpdb;
// Create tables for each site in a network.
if ( is_multisite() && $network_wide ) {
// Todo: Use get_sites() in WordPress 4.6+
$ids = $wpdb->get_col( "SELECT blog_id FROM `$wpdb->blogs`" );
if ( function_exists( 'get_sites' ) ) {
$sites = get_sites();
} elseif ( function_exists( 'wp_get_sites' ) ) {
$sites = wp_get_sites(); /* legacy support for WP < 4.6 */
} else {
return;
}
foreach ( $ids as $site_id ) {
switch_to_blog( $site_id );
foreach ( $sites as $site ) {
switch_to_blog( $site['blog_id'] );
add_option(
'statify-blacklist',
array(
'activate-referer' => 0,
'referer' => array()
)
self::defaultOptions()
);
}
@ -42,14 +44,28 @@ class StatifyBlacklist_System extends StatifyBlacklist {
} else {
add_option(
'statify-blacklist',
array(
'activate-referer' => 0,
'referer' => array()
)
self::defaultOptions()
);
}
}
/**
* Create default plugin configuration.
*
* @since 1.4.0
*
* @return array the options array
*/
private static function defaultOptions() {
return array(
'activate-referer' => 0,
'cron_referer' => 0,
'referer' => array(),
'referer_regexp' => 0,
'version' => self::VERSION_MAIN
);
}
/**
* Plugin uninstall handler.
@ -57,16 +73,19 @@ class StatifyBlacklist_System extends StatifyBlacklist {
* @since 1.0.0
*/
public static function uninstall() {
global $wpdb;
if ( is_multisite() ) {
$old = get_current_blog_id();
// Todo: Use get_sites() in WordPress 4.6+
$ids = $wpdb->get_col( "SELECT blog_id FROM `$wpdb->blogs`" );
if ( function_exists( 'get_sites' ) ) {
$sites = get_sites();
} elseif ( function_exists( 'wp_get_sites' ) ) {
$sites = wp_get_sites(); /* legacy support for WP < 4.6 */
} else {
return;
}
foreach ( $ids as $id ) {
switch_to_blog( $id );
foreach ( $sites as $site ) {
switch_to_blog( $site['blog_id'] );
delete_option( 'statify-blacklist' );
}

View File

@ -5,10 +5,10 @@ Description: Extension for the statify plugin to add a customizable blacklists.
Text Domain: statify-blacklist
Domain Path: /lang
Author: Stefan Kalscheuer
Author URI: https://stklcode.de
Author URI: https://www.stklcode.de
Plugin URI: https://wordpress.org/plugins/statify-blacklist
License: GPLv3 or later
Version: 1.3.1
Version: 1.4.0~dev
*/
/* Quit */

View File

@ -7,6 +7,8 @@ require_once( '../inc/statifyblacklist.class.php' );
* Class StatifyBlacklistTest
*
* PHPUnit test class for StatifyBlacklist
*
* @version 1.4.0~dev
*/
class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
@ -18,7 +20,8 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'referer' => array(
'example.com' => 0,
'example.net' => 1
)
),
'version' => 1.3
);
/* No multisite */
@ -26,25 +29,25 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
/* No referer */
unset( $_SERVER['HTTP_REFERER'] );
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Non-blacklisted referer */
$_SERVER['HTTP_REFERER'] = 'http://example.org';
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Blacklisted referer */
$_SERVER['HTTP_REFERER'] = 'http://example.com';
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Blacklisted referer with path */
$_SERVER['HTTP_REFERER'] = 'http://example.net/foo/bar.html';
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Activate filter and run tests again */
StatifyBlacklist::$_options['active_referer'] = 1;
unset( $_SERVER['HTTP_REFERER'] );
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['HTTP_REFERER'] = 'http://example.org';
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['HTTP_REFERER'] = 'http://example.com';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
@ -60,9 +63,10 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'cron_referer' => 0,
'referer' => array(
'example.[a-z]+' => 0,
'test' => 1
'test' => 1
),
'referer_regexp' => 1
'referer_regexp' => 1,
'version' => 1.3
);
/* No multisite */
@ -70,10 +74,10 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
/* No referer */
unset( $_SERVER['HTTP_REFERER'] );
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Non-blacklisted referer */
$_SERVER['HTTP_REFERER'] = 'http://not.evil';
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Blacklisted referer */
$_SERVER['HTTP_REFERER'] = 'http://example.com';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
@ -85,7 +89,7 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
/* Mathinc with wrong case */
$_SERVER['HTTP_REFERER'] = 'http://eXaMpLe.NeT/tEsT/mE';
$this->assertFalse( StatifyBlacklist::apply_blacklist_filter() );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
/* Set RegExp filter to case insensitive */
StatifyBlacklist::$_options['referer_regexp'] = 2;

View File

@ -10,7 +10,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
/* Check user capabilities */
if ( ! current_user_can( 'manage_options' ) ) {
die( _e( 'Are you sure you want to do this?' ) );
die( __( 'Are you sure you want to do this?' ) );
}
if ( ! empty( $_POST['cleanUp'] ) ) {