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

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

View File

@ -7,6 +7,7 @@ defined( 'ABSPATH' ) OR exit;
* Statify Blacklist admin configuration * Statify Blacklist admin configuration
* *
* @since 1.0.0 * @since 1.0.0
* @version 1.4.0~dev
*/ */
class StatifyBlacklist_Admin extends StatifyBlacklist { class StatifyBlacklist_Admin extends StatifyBlacklist {
/** /**
@ -114,12 +115,12 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* Filter database for cleanup. * Filter database for cleanup.
* *
* @since 1.1.0 * @since 1.1.0
* @changed 1.3.0 * @changed 1.4.0
*/ */
public static function cleanup_database() { public static function cleanup_database() {
/* Check user permissions */ /* Check user permissions */
if ( ! current_user_can( 'manage_options' ) && ! ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { 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; global $wpdb;

View File

@ -7,6 +7,7 @@ defined( 'ABSPATH' ) OR exit;
* Statify Blacklist system configuration * Statify Blacklist system configuration
* *
* @since 1.0.0 * @since 1.0.0
* @version 1.4.0~dev
*/ */
class StatifyBlacklist_System extends StatifyBlacklist { class StatifyBlacklist_System extends StatifyBlacklist {
@ -16,25 +17,26 @@ class StatifyBlacklist_System extends StatifyBlacklist {
* Plugin install handler. * 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. * @param bool $network_wide Whether the plugin was activated network-wide or not.
*/ */
public static function install( $network_wide = false ) { public static function install( $network_wide = false ) {
global $wpdb;
// Create tables for each site in a network. // Create tables for each site in a network.
if ( is_multisite() && $network_wide ) { if ( is_multisite() && $network_wide ) {
// Todo: Use get_sites() in WordPress 4.6+ if ( function_exists( 'get_sites' ) ) {
$ids = $wpdb->get_col( "SELECT blog_id FROM `$wpdb->blogs`" ); $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 ) { foreach ( $sites as $site ) {
switch_to_blog( $site_id ); switch_to_blog( $site['blog_id'] );
add_option( add_option(
'statify-blacklist', 'statify-blacklist',
array( self::defaultOptions()
'activate-referer' => 0,
'referer' => array()
)
); );
} }
@ -42,14 +44,28 @@ class StatifyBlacklist_System extends StatifyBlacklist {
} else { } else {
add_option( add_option(
'statify-blacklist', 'statify-blacklist',
array( self::defaultOptions()
'activate-referer' => 0,
'referer' => array()
)
); );
} }
} }
/**
* 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. * Plugin uninstall handler.
@ -57,16 +73,19 @@ class StatifyBlacklist_System extends StatifyBlacklist {
* @since 1.0.0 * @since 1.0.0
*/ */
public static function uninstall() { public static function uninstall() {
global $wpdb;
if ( is_multisite() ) { if ( is_multisite() ) {
$old = get_current_blog_id(); $old = get_current_blog_id();
// Todo: Use get_sites() in WordPress 4.6+ if ( function_exists( 'get_sites' ) ) {
$ids = $wpdb->get_col( "SELECT blog_id FROM `$wpdb->blogs`" ); $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 ) { foreach ( $sites as $site ) {
switch_to_blog( $id ); switch_to_blog( $site['blog_id'] );
delete_option( 'statify-blacklist' ); 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 Text Domain: statify-blacklist
Domain Path: /lang Domain Path: /lang
Author: Stefan Kalscheuer Author: Stefan Kalscheuer
Author URI: https://stklcode.de Author URI: https://www.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.3.1 Version: 1.4.0~dev
*/ */
/* Quit */ /* Quit */

View File

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

View File

@ -10,7 +10,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
/* Check user capabilities */ /* Check user capabilities */
if ( ! current_user_can( 'manage_options' ) ) { 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'] ) ) { if ( ! empty( $_POST['cleanUp'] ) ) {