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:
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,20 +242,17 @@ 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
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
) ||
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
);
}
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
);
}
);
}
}

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;
}
@ -87,9 +87,11 @@ class StatifyBlacklist {
add_action( 'network_admin_menu', array( 'StatifyBlacklist_Admin', '_add_menu_page' ) );
add_filter(
'network_admin_plugin_action_links', array(
'StatifyBlacklist_Admin',
'plugin_actions_links'
), 10, 2
'StatifyBlacklist_Admin',
'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 );
}