Corrections for WP Coding Standard

All PHP classes have been reworked to match the WP coding standard. This includes mostly code styling, but also the use of wp_ functions instead of PHP builtins.
This commit is contained in:
Stefan Kalscheuer 2017-07-15 15:48:55 +02:00
parent d769c6789c
commit 4890dbaeeb
6 changed files with 229 additions and 212 deletions

View File

@ -10,7 +10,7 @@
*/
// Quit.
defined( 'ABSPATH' ) OR exit;
defined( 'ABSPATH' ) or exit;
/**
* Statify Blacklist admin configuration.
@ -72,14 +72,14 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
add_submenu_page(
'settings.php', $title, $title, 'manage_network_plugins', 'statify-blacklist-settings', array(
'StatifyBlacklist_Admin',
'settings_page'
'settings_page',
)
);
} else {
add_submenu_page(
'options-general.php', $title, $title, 'manage_options', 'statify-blacklist', array(
'StatifyBlacklist_Admin',
'settings_page'
'settings_page',
)
);
}
@ -92,7 +92,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* @since 1.0.0
*/
public static function settings_page() {
include STATIFYBLACKLIST_DIR . '/views/settings_page.php';
include STATIFYBLACKLIST_DIR . '/views/settings-page.php';
}
/**
@ -104,10 +104,9 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* @param string $file The filename.
*
* @return array Merged links.
*
*/
public static function plugin_meta_link( $links, $file ) {
if ( $file === STATIFYBLACKLIST_BASE ) {
if ( STATIFYBLACKLIST_BASE === $file ) {
$links[] = '<a href="https://github.com/stklcode/statify-blacklist">GitHub</a>';
}
@ -119,16 +118,15 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
*
* @since 1.0.0
*
* @param array $links Registered links
* @param string $file The filename
* @param array $links Registered links.
* @param string $file The filename.
*
* @return array Merged links.
*
*/
public static function plugin_actions_links( $links, $file ) {
$base = self::$multisite ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
if ( $file === STATIFYBLACKLIST_BASE && current_user_can( 'manage_options' ) ) {
if ( STATIFYBLACKLIST_BASE === $file && current_user_can( 'manage_options' ) ) {
array_unshift(
$links,
sprintf( '<a href="%s">%s</a>', esc_attr( add_query_arg( 'page', 'statify-blacklist', $base ) ), __( 'Settings' ) )
@ -148,7 +146,7 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
public static function cleanup_database() {
// Check user permissions.
if ( ! current_user_can( 'manage_options' ) && ! ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
die( __( 'Are you sure you want to do this?' ) );
die( esc_html__( 'Are you sure you want to do this?' ) );
}
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
@ -159,11 +157,10 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
$cleanTrg = true;
}
if ( $cleanRef ) {
if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) {
// Merge given regular expressions into one.
$refererRegexp = implode( "|", array_keys( self::$_options['referer']['blacklist'] ) );
$refererRegexp = implode( '|', array_keys( self::$_options['referer']['blacklist'] ) );
} else {
// Sanitize URLs.
$referer = self::sanitizeURLs( self::$_options['referer']['blacklist'] );
@ -176,14 +173,13 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
if ( $cleanTrg ) {
if ( isset( self::$_options['target']['regexp'] ) && self::$_options['target']['regexp'] > 0 ) {
// Merge given regular expressions into one.
$targetRegexp = implode( "|", array_keys( self::$_options['target']['blacklist'] ) );
$targetRegexp = implode( '|', array_keys( self::$_options['target']['blacklist'] ) );
} else {
// Build filter regexp.
$targetRegexp = str_replace( '.', '\.', implode( '|', array_flip( self::$_options['target']['blacklist'] ) ) );
}
}
if ( ! empty( $refererRegexp ) || ! empty( $targetRegexp ) ) {
global $wpdb;
@ -192,8 +188,8 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM `$wpdb->statify` WHERE "
. ( ( 1 === self::$_options['referer']['regexp'] ) ? " BINARY " : "" )
. "referrer REGEXP %s", $refererRegexp
. ( ( 1 === self::$_options['referer']['regexp'] ) ? ' BINARY ' : '' )
. 'referrer REGEXP %s', $refererRegexp
)
);
}
@ -201,8 +197,8 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM `$wpdb->statify` WHERE "
. ( ( 1 === self::$_options['target']['regexp'] ) ? " BINARY " : "" )
. "target REGEXP %s", $targetRegexp
. ( ( 1 === self::$_options['target']['regexp'] ) ? ' BINARY ' : '' )
. 'target REGEXP %s', $targetRegexp
)
);
}
@ -224,7 +220,6 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* @param array $urls given array of URLs.
*
* @return array sanitized array.
*
*/
private static function sanitizeURLs( $urls ) {
return array_flip(
@ -247,18 +242,15 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
* @param array $ips given array of URLs.
*
* @return array sanitized array.
*
*/
private static function sanitizeIPs( $ips ) {
return array_filter(
$ips, function ( $ip ) {
return preg_match(
'/^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])' .
'(\/([0-9]|[1-2][0-9]|3[0-2]))?$/', $ip
'/^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\/([0-9]|[1-2][0-9]|3[0-2]))?$/', $ip
) ||
preg_match(
'/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' .
'(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/', $ip
'/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$/', $ip
);
}
);

View File

@ -10,7 +10,7 @@
*/
// Quit.
defined( 'ABSPATH' ) OR exit;
defined( 'ABSPATH' ) or exit;
/**
* Statify Blacklist system configuration.
@ -32,7 +32,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
if ( function_exists( 'get_sites' ) ) {
$sites = get_sites();
} elseif ( function_exists( 'wp_get_sites' ) ) {
$sites = wp_get_sites(); // legacy support for WP < 4.6.
$sites = wp_get_sites(); // Legacy support for WP < 4.6.
} else {
return;
}
@ -41,7 +41,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
switch_to_blog( $site['blog_id'] );
add_option(
'statify-blacklist',
self::defaultOptions()
self::default_options()
);
}
@ -49,7 +49,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
} else {
add_option(
'statify-blacklist',
self::defaultOptions()
self::default_options()
);
}
}
@ -67,7 +67,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
if ( function_exists( 'get_sites' ) ) {
$sites = get_sites();
} elseif ( function_exists( 'wp_get_sites' ) ) {
$sites = wp_get_sites(); // legacy support for WP < 4.6.
$sites = wp_get_sites(); // Legacy support for WP < 4.6.
} else {
return;
}
@ -111,19 +111,19 @@ class StatifyBlacklist_System extends StatifyBlacklist {
'active' => self::$_options['active_referer'],
'cron' => self::$_options['cron_referer'],
'regexp' => self::$_options['referer_regexp'],
'blacklist' => self::$_options['referer']
'blacklist' => self::$_options['referer'],
),
'target' => array(
'active' => 0,
'cron' => 0,
'regexp' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'ip' => array(
'active' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'version' => 1.4
'version' => 1.4,
);
if ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) {
update_site_option( 'statify-blacklist', $options );
@ -136,7 +136,7 @@ class StatifyBlacklist_System extends StatifyBlacklist {
// Version older than current major release.
if ( self::VERSION_MAIN > self::$_options['version'] ) {
// Merge default options with current config, assuming only additive changes.
$options = array_merge_recursive( self::defaultOptions(), self::$_options );
$options = array_merge_recursive( self::default_options(), self::$_options );
$options['version'] = self::VERSION_MAIN;
if ( ( is_multisite() && array_key_exists( STATIFYBLACKLIST_BASE, (array) get_site_option( 'active_sitewide_plugins' ) ) ) ) {
update_site_option( 'statify-blacklist', $options );

View File

@ -9,7 +9,7 @@
*/
// Quit.
defined( 'ABSPATH' ) OR exit;
defined( 'ABSPATH' ) or exit;
/**
* Statify Blacklist.
@ -58,7 +58,7 @@ class StatifyBlacklist {
*/
public function __construct() {
// Skip on autosave or AJAX.
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) OR ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) or ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return;
}
@ -88,8 +88,10 @@ class StatifyBlacklist {
add_filter(
'network_admin_plugin_action_links', array(
'StatifyBlacklist_Admin',
'plugin_actions_links'
), 10, 2
'plugin_actions_links',
),
10,
2
);
} else {
add_action( 'admin_menu', array( 'StatifyBlacklist_Admin', '_add_menu_page' ) );
@ -112,12 +114,11 @@ class StatifyBlacklist {
* @since 1.2.1 update_options($options = null) Parameter with default value introduced.
*
* @param array $options Optional. New options to save.
*
*/
public static function update_options( $options = null ) {
self::$_options = wp_parse_args(
get_option( 'statify-blacklist' ),
self::defaultOptions()
self::default_options()
);
}
@ -128,25 +129,25 @@ class StatifyBlacklist {
*
* @return array The options array.
*/
protected static function defaultOptions() {
protected static function default_options() {
return array(
'referer' => array(
'active' => 0,
'cron' => 0,
'regexp' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'target' => array(
'active' => 0,
'cron' => 0,
'regexp' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'ip' => array(
'active' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'version' => self::VERSION_MAIN
'version' => self::VERSION_MAIN,
);
}
@ -163,62 +164,68 @@ class StatifyBlacklist {
// Regular Expression filtering since 1.3.0.
if ( isset( self::$_options['referer']['regexp'] ) && self::$_options['referer']['regexp'] > 0 ) {
// Get full referer string.
$referer = ( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' );
$referer = wp_get_raw_referer();
if ( ! $referer ) {
$referer = '';
}
// Merge given regular expressions into one.
$regexp = '/' . implode( "|", array_keys( self::$_options['referer']['blacklist'] ) ) . '/';
$regexp = '/' . implode( '|', array_keys( self::$_options['referer']['blacklist'] ) ) . '/';
if ( 2 === self::$_options['referer']['regexp'] ) {
$regexp .= 'i';
}
// Check blacklist (return NULL to continue filtering).
return ( 1 === preg_match( $regexp, $referer ) ) ? true : null;
} else {
// Extract relevant domain parts.
$referer = strtolower( ( isset( $_SERVER['HTTP_REFERER'] ) ? parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_HOST ) : '' ) );
$referer = wp_parse_url( wp_get_raw_referer() );
$referer = strtolower( ( isset( $referer['host'] ) ? $referer['host'] : '' ) );
// Get blacklist.
$blacklist = self::$_options['referer']['blacklist'];
// Check blacklist.
if ( isset( $blacklist[$referer] ) ) {
if ( isset( $blacklist[ $referer ] ) ) {
return true;
}
}
}
// Target blacklist (since 1.4.0)
// Target blacklist (since 1.4.0).
if ( isset( self::$_options['target']['active'] ) && 0 !== self::$_options['target']['active'] ) {
// Regular Expression filtering since 1.3.0.
if ( isset( self::$_options['target']['regexp'] ) && 0 < self::$_options['target']['regexp'] ) {
// Get full referer string.
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/' );
// Merge given regular expressions into one
$regexp = '/' . implode( "|", array_keys( self::$_options['target']['blacklist'] ) ) . '/';
// @codingStandardsIgnoreStart The globals are checked.
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' );
// @codingStandardsIgnoreEnd
// Merge given regular expressions into one.
$regexp = '/' . implode( '|', array_keys( self::$_options['target']['blacklist'] ) ) . '/';
if ( 2 === self::$_options['target']['regexp'] ) {
$regexp .= 'i';
}
// Check blacklist (return NULL to continue filtering).
return ( 1 === preg_match( $regexp, $target ) ) ? true : null;
} else {
// Extract target page.
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/' );
// @codingStandardsIgnoreStart The globals are checked.
$target = ( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/' );
// @codingStandardsIgnoreEnd
// Get blacklist.
$blacklist = self::$_options['target']['blacklist'];
// Check blacklist.
if ( isset( $blacklist[$target] ) ) {
if ( isset( $blacklist[ $target ] ) ) {
return true;
}
}
}
// IP blacklist (since 1.4.0).
if ( isset ( self::$_options['ip']['active'] ) && 0 !== self::$_options['ip']['active'] ) {
if ( false !== ( $ip = self::getIP() ) ) {
if ( isset( self::$_options['ip']['active'] ) && 0 !== self::$_options['ip']['active'] ) {
if ( false !== ( $ip = self::get_ip() ) ) {
foreach ( self::$_options['ip']['blacklist'] as $net ) {
if ( self::cidrMatch( $ip, $net ) ) {
if ( self::cidr_match( $ip, $net ) ) {
return true;
}
}
@ -226,7 +233,6 @@ class StatifyBlacklist {
}
// Skip and continue (return NULL), if all blacklists are inactive.
return null;
}
@ -239,7 +245,7 @@ class StatifyBlacklist {
*
* @return string|bool the client's IP address or FALSE, if none could be determined.
*/
private static function getIP() {
private static function get_ip() {
foreach (
array(
// 'HTTP_CLIENT_IP',
@ -249,16 +255,18 @@ class StatifyBlacklist {
// 'HTTP_X_CLUSTER_CLIENT_IP',
// 'HTTP_FORWARDED_FOR',
// 'HTTP_FORWARDED',
'REMOTE_ADDR'
'REMOTE_ADDR',
) as $k
) {
if ( isset( $_SERVER[$k] ) ) {
foreach ( explode( ',', $_SERVER[$k] ) as $ip ) {
// @codingStandardsIgnoreStart The globals are checked.
if ( isset( $_SERVER[ $k ] ) ) {
foreach ( explode( ',', $_SERVER[ $k ] ) as $ip ) {
if ( false !== filter_var( $ip, FILTER_VALIDATE_IP ) ) {
return $ip;
}
}
}
// @codingStandardsIgnoreEnd
}
return false;
@ -272,9 +280,9 @@ class StatifyBlacklist {
*
* @return bool TRUE, if the given IP addresses matches the given subnet.
*/
private static function cidrMatch( $ip, $net ) {
private static function cidr_match( $ip, $net ) {
if ( substr_count( $net, ':' ) > 1 ) { // Check for IPv6.
if ( ! ( ( extension_loaded( 'sockets' ) && defined( 'AF_INET6' ) ) || @inet_pton( '::1' ) ) ) {
if ( ! ( ( extension_loaded( 'sockets' ) && defined( 'AF_INET6' ) ) || inet_pton( '::1' ) ) ) {
return false;
}
@ -289,8 +297,8 @@ class StatifyBlacklist {
$mask = 128;
}
$bytesAddr = unpack( 'n*', @inet_pton( $base ) );
$bytesTest = unpack( 'n*', @inet_pton( $ip ) );
$bytesAddr = unpack( 'n*', inet_pton( $base ) );
$bytesTest = unpack( 'n*', inet_pton( $ip ) );
if ( ! $bytesAddr || ! $bytesTest ) {
return false;
@ -300,7 +308,7 @@ class StatifyBlacklist {
$left = $mask - 16 * ( $i - 1 );
$left = ( $left <= 16 ) ? $left : 16;
$maskB = ~( 0xffff >> $left ) & 0xffff;
if ( ( $bytesAddr[$i] & $maskB ) !== ( $bytesTest[$i] & $maskB ) ) {
if ( ( $bytesAddr[ $i ] & $maskB ) !== ( $bytesTest[ $i ] & $maskB ) ) {
return false;
}
}
@ -314,7 +322,7 @@ class StatifyBlacklist {
if ( false !== strpos( $net, '/' ) ) { // Parse CIDR subnet.
list( $base, $mask ) = explode( '/', $net, 2 );
if ( $mask === '0' ) {
if ( '0' === $mask ) {
return filter_var( $base, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
}

View File

@ -1,34 +1,42 @@
<?php
/*
Plugin Name: Statify Blacklist
Plugin URI: https://de.wordpress.org/plugins/statify-blacklist/
Description: Extension for the Statify plugin to add a customizable blacklists.
Version: 1.4.0
Author: Stefan Kalscheuer (@stklcode)
Author URI: https://www.stklcode.de
Plugin URI: https://wordpress.org/plugins/statify-blacklist
Text Domain: statify-blacklist
Domain Path: /lang
License: GPLv2 or later
/**
* Statify Blacklist
*
* @package PluginPackage
* @author Stefan Kalscheuer <stefan@stklcode.de>
* @license GPL-2.0+
*
* @wordpress-plugin
*
* Plugin Name: Statify Blacklist
* Plugin URI: https://de.wordpress.org/plugins/statify-blacklist/
* Description: Extension for the Statify plugin to add a customizable blacklists.
* Version: 1.4.0
* Author: Stefan Kalscheuer (@stklcode)
* Author URI: https://www.stklcode.de
* Plugin URI: https://wordpress.org/plugins/statify-blacklist
* Text Domain: statify-blacklist
* Domain Path: /lang
* License: GPLv2 or later
*
* Statify Blacklist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* Statify Blacklist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Statify Blacklist. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
*/
Statify Blacklist is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
// Quit.
defined( 'ABSPATH' ) or exit;
Statify Blacklist is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Statify Blacklist. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
*/
// Quit
defined( 'ABSPATH' ) OR exit;
// Constants
// Constants.
define( 'STATIFYBLACKLIST_FILE', __FILE__ );
define( 'STATIFYBLACKLIST_DIR', dirname( __FILE__ ) );
define( 'STATIFYBLACKLIST_BASE', plugin_basename( __FILE__ ) );
@ -40,11 +48,11 @@ register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System
register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) );
// Upgrade hook
// Upgrade hook.
register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'upgrade' ) );
// Autoload
spl_autoload_register( 'statifyBlacklist_autoload' );
// Autoload.
spl_autoload_register( 'statify_blacklist_autoload' );
/**
* Autoloader for StatifyBlacklist classes.
@ -53,14 +61,20 @@ spl_autoload_register( 'statifyBlacklist_autoload' );
*
* @since 1.0.0
*/
function statifyBlacklist_autoload( $class ) {
function statify_blacklist_autoload( $class ) {
$plugin_classes = array(
'StatifyBlacklist',
'StatifyBlacklist_Admin',
'StatifyBlacklist_System'
'StatifyBlacklist_System',
);
if ( in_array( $class, $plugin_classes ) ) {
require_once( sprintf( '%s/inc/%s.class.php', STATIFYBLACKLIST_DIR, strtolower( $class ) ) );
require_once(
sprintf(
'%s/inc/%s.class.php',
STATIFYBLACKLIST_DIR,
strtolower( str_replace( '_', '-', $class ) )
)
);
}
}

View File

@ -20,17 +20,17 @@ const ABSPATH = false;
/**
* The StatifyBlacklist base class.
*/
require_once( '../inc/statifyblacklist.class.php' );
require_once( 'inc/statifyblacklist.class.php' );
/**
* The StatifyBlacklist system class.
*/
require_once( '../inc/statifyblacklist_system.class.php' );
require_once( 'inc/statifyblacklist-system.class.php' );
/**
* The StatifyBlacklist admin class.
*/
require_once( '../inc/statifyblacklist_admin.class.php' );
require_once( 'inc/statifyblacklist-admin.class.php' );
/**
* Class StatifyBlacklistTest.
@ -39,13 +39,13 @@ require_once( '../inc/statifyblacklist_admin.class.php' );
*
* @since 1.3.0
*/
class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
class StatifyBlacklistTest extends PHPUnit\Framework\TestCase {
/**
* Test simple referer filter.
*/
public function testRefererFilter() {
// Prepare Options: 2 blacklisted domains, disabled
// Prepare Options: 2 blacklisted domains, disabled.
StatifyBlacklist::$_options = array(
'referer' => array(
'active' => 0,
@ -53,20 +53,20 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'regexp' => 0,
'blacklist' => array(
'example.com' => 0,
'example.net' => 1
)
'example.net' => 1,
),
),
'target' => array(
'active' => 0,
'cron' => 0,
'regexp' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'ip' => array(
'active' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'version' => StatifyBlacklist::VERSION_MAIN
'version' => StatifyBlacklist::VERSION_MAIN,
);
// No multisite.
@ -113,20 +113,20 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'regexp' => 1,
'blacklist' => array(
'example.[a-z]+' => 0,
'test' => 1
)
'test' => 1,
),
),
'target' => array(
'active' => 0,
'cron' => 0,
'regexp' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'ip' => array(
'active' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'version' => StatifyBlacklist::VERSION_MAIN
'version' => StatifyBlacklist::VERSION_MAIN,
);
// No multisite.
@ -166,10 +166,10 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'cron_referer' => 0,
'referer' => array(
'example.net' => 0,
'example.com' => 1
'example.com' => 1,
),
'referer_regexp' => 0,
'version' => 1.3
'version' => 1.3,
);
// Set options in mock.
@ -182,10 +182,10 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
$optionsUpdated = get_option( 'statify-blacklist' );
// Verify size against default options (no junk left).
$this->assertEquals( 4, sizeof( $optionsUpdated ) );
$this->assertEquals( 4, sizeof( $optionsUpdated['referer'] ) );
$this->assertEquals( 4, sizeof( $optionsUpdated['target'] ) );
$this->assertEquals( 2, sizeof( $optionsUpdated['ip'] ) );
$this->assertEquals( 4, count( $optionsUpdated ) );
$this->assertEquals( 4, count( $optionsUpdated['referer'] ) );
$this->assertEquals( 4, count( $optionsUpdated['target'] ) );
$this->assertEquals( 2, count( $optionsUpdated['ip'] ) );
// Verify that original attributes are unchanged.
$this->assertEquals( $options13['active_referer'], $optionsUpdated['referer']['active'] );
@ -210,92 +210,92 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
*/
public function testCidrMatch() {
// IPv4 tests.
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '127.0.0.1', '127.0.0.1' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '127.0.0.1', '127.0.0.1/32' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '127.0.0.1', '127.0.0.1/32' ) ) );
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'127.0.0.1',
'127.0.0.1/33'
'127.0.0.1/33',
)
)
);
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'127.0.0.1',
'127.0.0.1/-1'
'127.0.0.1/-1',
)
)
);
$this->assertTrue(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'192.0.2.123',
'192.0.2.0/24'
'192.0.2.0/24',
)
)
);
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'192.0.3.123',
'192.0.2.0/24'
'192.0.2.0/24',
)
)
);
$this->assertTrue(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'192.0.2.123',
'192.0.2.120/29'
'192.0.2.120/29',
)
)
);
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'192.0.2.128',
'192.0.2.120/29'
'192.0.2.120/29',
)
)
);
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '10.11.12.13', '10.0.0.0/8' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '10.11.12.13', '10.0.0.0/8' ) ) );
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'10.11.12.345',
'10.0.0.0/8'
'10.0.0.0/8',
)
)
);
// IPv6 tests.
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/128' ) ) );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/129' ) ) );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidrMatch', array( '::1', '::1/-1' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1' ) ) );
$this->assertTrue( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/128' ) ) );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/129' ) ) );
$this->assertFalse( invokeStatic( StatifyBlacklist::class, 'cidr_match', array( '::1', '::1/-1' ) ) );
$this->assertTrue(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'2001:db8:a0b:12f0:1:2:3:4',
'2001:db8:a0b:12f0::1/64 '
'2001:db8:a0b:12f0::1/64 ',
)
)
);
$this->assertTrue(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'2001:db8:a0b:12f0::123:456',
'2001:db8:a0b:12f0::1/96 '
'2001:db8:a0b:12f0::1/96 ',
)
)
);
$this->assertFalse(
invokeStatic(
StatifyBlacklist::class, 'cidrMatch', array(
StatifyBlacklist::class, 'cidr_match', array(
'2001:db8:a0b:12f0::1:132:465',
'2001:db8:a0b:12f0::1/96 '
'2001:db8:a0b:12f0::1/96 ',
)
)
);
@ -318,14 +318,14 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'2001:db8:a0b:12f0::',
'2001:db8:a0b:12f0::1',
'2001:db8:a0b:12f0::1/128',
'2001:db8:a0b:12f0::/64'
'2001:db8:a0b:12f0::/64',
);
$invalid = array(
'2001:db8:a0b:12f0::x',
'2001:db8:a0b:12f0:::',
'2001:fffff:a0b:12f0::1',
'2001:db8:a0b:12f0::/129',
'1:2:3:4:5:6:7:8:9'
'1:2:3:4:5:6:7:8:9',
);
$result = invokeStatic( StatifyBlacklist_Admin::class, 'sanitizeIPs', array( array_merge( $valid, $invalid ) ) );
$this->assertNotFalse( $result );
@ -343,22 +343,22 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'active' => 0,
'cron' => 0,
'regexp' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'target' => array(
'active' => 0,
'cron' => 0,
'regexp' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'ip' => array(
'active' => 0,
'blacklist' => array(
'192.0.2.123',
'2001:db8:a0b:12f0::1'
)
'2001:db8:a0b:12f0::1',
),
'version' => StatifyBlacklist::VERSION_MAIN
),
'version' => StatifyBlacklist::VERSION_MAIN,
);
// No multisite.
@ -381,7 +381,7 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
// Subnet matching.
StatifyBlacklist::$_options['ip']['blacklist'] = array(
'192.0.2.0/25',
'2001:db8:a0b:12f0::/96'
'2001:db8:a0b:12f0::/96',
);
$_SERVER['REMOTE_ADDR'] = '192.0.2.123';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
@ -413,7 +413,7 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'active' => 0,
'cron' => 0,
'regexp' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'target' => array(
'active' => 0,
@ -421,14 +421,14 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
'regexp' => 0,
'blacklist' => array(
'/excluded/page/' => 0,
'/?page_id=3' => 1
)
'/?page_id=3' => 1,
),
),
'ip' => array(
'active' => 0,
'blacklist' => array()
'blacklist' => array(),
),
'version' => StatifyBlacklist::VERSION_MAIN
'version' => StatifyBlacklist::VERSION_MAIN,
);
// No multisite.
@ -471,12 +471,7 @@ class StatifyBlacklistTest extends PHPUnit_Framework_TestCase {
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
}
/**
* Test target filter using regular expressions.
*/
public function testTargetRegexFilter() {
// TODO
}
// TODO: Test target regex filter.
}
@ -491,7 +486,6 @@ function invokeStatic( $class, $methodName, $parameters = array() ) {
// Some mocked WP functions.
$mock_options = array();
$mock_multisite = false;
@ -523,13 +517,22 @@ function wp_parse_args( $args, $defaults = '' ) {
function get_option( $option, $default = false ) {
global $mock_options;
return isset( $mock_options[$option] ) ? $mock_options[$option] : $default;
return isset( $mock_options[ $option ] ) ? $mock_options[ $option ] : $default;
}
/** @ignore */
function update_option( $option, $value, $autoload = null ) {
global $mock_options;
$mock_options[$option] = $value;
$mock_options[ $option ] = $value;
}
/** @ignore */
function wp_get_raw_referer() {
return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
}
function wp_parse_url( $value ) {
return parse_url( $value );
}
/** @ignore */

View File

@ -10,7 +10,7 @@
*/
// Quit.
defined( 'ABSPATH' ) OR exit;
defined( 'ABSPATH' ) or exit;
// Update plugin options.
if ( ! empty( $_POST['statifyblacklist'] ) ) {
@ -51,27 +51,27 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
$statifyBlacklistUpdateResult = StatifyBlacklist_Admin::update_options(
array(
'referer' => array(
'active' => (int) @$_POST['statifyblacklist']['referer']['active'],
'cron' => (int) @$_POST['statifyblacklist']['referer']['cron'],
'regexp' => (int) @$_POST['statifyblacklist']['referer']['regexp'],
'blacklist' => array_flip( $referer )
'active' => (int) $_POST['statifyblacklist']['referer']['active'],
'cron' => (int) $_POST['statifyblacklist']['referer']['cron'],
'regexp' => (int) $_POST['statifyblacklist']['referer']['regexp'],
'blacklist' => array_flip( $referer ),
),
'target' => array(
'active' => (int) @$_POST['statifyblacklist']['target']['active'],
'cron' => (int) @$_POST['statifyblacklist']['target']['cron'],
'regexp' => (int) @$_POST['statifyblacklist']['target']['regexp'],
'blacklist' => array_flip( $target )
'active' => (int) $_POST['statifyblacklist']['target']['active'],
'cron' => (int) $_POST['statifyblacklist']['target']['cron'],
'regexp' => (int) $_POST['statifyblacklist']['target']['regexp'],
'blacklist' => array_flip( $target ),
),
'ip' => array(
'active' => (int) @$_POST['statifyblacklist']['ip']['active'],
'blacklist' => $ip
'active' => (int) $_POST['statifyblacklist']['ip']['active'],
'blacklist' => $ip,
),
'version' => StatifyBlacklist::VERSION_MAIN
'version' => StatifyBlacklist::VERSION_MAIN,
)
);
// Generate messages.
if ( $statifyBlacklistUpdateResult !== false ) {
if ( false !== $statifyBlacklistUpdateResult ) {
if ( array_key_exists( 'referer', $statifyBlacklistUpdateResult ) ) {
$statifyBlacklistPostWarning = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' );
} elseif ( array_key_exists( 'ip', $statifyBlacklistUpdateResult ) ) {
@ -85,7 +85,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
?>
<div class="wrap">
<h1><?php _e( 'Statify Blacklist', 'statify-blacklist' ) ?></h1>
<h1><?php esc_html_e( 'Statify Blacklist', 'statify-blacklist' ) ?></h1>
<?php
if ( is_plugin_inactive( 'statify/statify.php' ) ) {
print '<div class="notice notice-warning"><p>';
@ -271,7 +271,7 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
<?php wp_nonce_field( 'statify-blacklist-settings' ); ?>
<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 esc_html_e( 'Save Changes' ) ?>">
<hr />
<input class="button-secondary" type="submit" name="cleanUp"
value="<?php esc_html_e( 'CleanUp Database', 'statify-blacklist' ) ?>"