From 1e0659e64940f43f6b3ebff1eb632d62ae5a377c Mon Sep 17 00:00:00 2001
From: Stefan Kalscheuer
Date: Sat, 20 Aug 2016 18:50:38 +0200
Subject: [PATCH] WP coding style
---
inc/statifyblacklist.class.php | 202 ++++++++++-----------
inc/statifyblacklist_admin.class.php | 248 ++++++++++++++------------
inc/statifyblacklist_system.class.php | 99 +++++-----
statify-blacklist.php | 35 ++--
views/settings_page.php | 166 +++++++++--------
5 files changed, 388 insertions(+), 362 deletions(-)
diff --git a/inc/statifyblacklist.class.php b/inc/statifyblacklist.class.php
index 4d55a5b..a590846 100644
--- a/inc/statifyblacklist.class.php
+++ b/inc/statifyblacklist.class.php
@@ -1,124 +1,126 @@
0,
- 'referer' => array()
- )
- );
- }
+ /**
+ * Update options
+ *
+ * @since 1.0.0
+ * @changed 1.1.1
+ */
+ public static function update_options() {
+ self::$_options = wp_parse_args(
+ get_option( 'statify-blacklist' ),
+ array(
+ 'active_referer' => 0,
+ 'referer' => array()
+ )
+ );
+ }
- /**
- * Apply the blacklist filter if active
- *
- * @return TRUE if referer matches blacklist.
- *
- * @since 1.0.0
- */
- public static function apply_blacklist_filter() {
- /* Skip if blacklist is inactive */
- if (self::$_options['active_referer'] != 1) {
- return false;
- }
+ /**
+ * Apply the blacklist filter if active
+ *
+ * @return TRUE if referer matches blacklist.
+ *
+ * @since 1.0.0
+ */
+ public static function apply_blacklist_filter() {
+ /* Skip if blacklist is inactive */
+ if ( self::$_options['active_referer'] != 1 ) {
+ return false;
+ }
- /* Extract relevant domain parts */
- $referer = strtolower( ( isset($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) : '' ) );
- $referer = explode('.', $referer);
- if( count($referer) >1 )
- $referer = implode('.', array_slice($referer, -2));
- else
- $referer = implode('.', $referer);
+ /* Extract relevant domain parts */
+ $referer = strtolower( ( isset( $_SERVER['HTTP_REFERER'] ) ? parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_HOST ) : '' ) );
+ $referer = explode( '.', $referer );
+ if ( count( $referer ) > 1 ) {
+ $referer = implode( '.', array_slice( $referer, - 2 ) );
+ } else {
+ $referer = implode( '.', $referer );
+ }
- /* Get blacklist */
- $blacklist = self::$_options['referer'];
+ /* Get blacklist */
+ $blacklist = self::$_options['referer'];
- /* Check blacklist */
- return in_array($referer, $blacklist);
- }
+ /* Check blacklist */
+
+ return in_array( $referer, $blacklist );
+ }
}
diff --git a/inc/statifyblacklist_admin.class.php b/inc/statifyblacklist_admin.class.php
index aea2e09..f2ad789 100644
--- a/inc/statifyblacklist_admin.class.php
+++ b/inc/statifyblacklist_admin.class.php
@@ -1,144 +1,158 @@
GitHub';
- }
- return $links;
- }
+ /**
+ * Add plugin meta links
+ *
+ * @param $links
+ * @param $file
+ *
+ * @return array
+ *
+ * @since 1.0.0
+ */
+ public static function plugin_meta_link( $links, $file ) {
+ if ( $file == STATIFYBLACKLIST_BASE ) {
+ $links[] = 'GitHub';
+ }
- /**
- * Add plugin action links
- *
- * @param array $input Registered links
- * @return array Merged links
- *
- * @since 1.0.0
- */
- public static function plugin_actions_links($links, $file) {
- $base = self::$multisite ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
+ return $links;
+ }
- if( $file == STATIFYBLACKLIST_BASE && current_user_can('manage_options') ) {
- array_unshift(
- $links,
- sprintf( '%s', esc_attr(add_query_arg( 'page', 'statify-blacklist', $base )), __('Settings'))
- );
- }
- return $links;
- }
+ /**
+ * Add plugin action links
+ *
+ * @param array $input Registered links
+ *
+ * @return array Merged links
+ *
+ * @since 1.0.0
+ */
+ public static function plugin_actions_links( $links, $file ) {
+ $base = self::$multisite ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
- /**
- * Filter database for cleanup.
- *
- * @since 1.1.0
- * @changed 1.1.1
- */
- public static function cleanup_database() {
- /* Check user permissions */
- if (!current_user_can('manage_options'))
- die(_e('Are you sure you want to do this?'));
+ if ( $file == STATIFYBLACKLIST_BASE && current_user_can( 'manage_options' ) ) {
+ array_unshift(
+ $links,
+ sprintf( '%s', esc_attr( add_query_arg( 'page', 'statify-blacklist', $base ) ), __( 'Settings' ) )
+ );
+ }
- global $wpdb;
+ return $links;
+ }
- /* Sanitize URLs */
- $referer = self::sanitizeURLs(self::$_options['referer']);
+ /**
+ * Filter database for cleanup.
+ *
+ * @since 1.1.0
+ * @changed 1.1.1
+ */
+ public static function cleanup_database() {
+ /* Check user permissions */
+ if ( ! current_user_can( 'manage_options' ) ) {
+ die( _e( 'Are you sure you want to do this?' ) );
+ }
- /* Build filter regexp */
- $refererRegexp = str_replace('.', '\.', implode('|', $referer));
- if (!empty($refererRegexp)) {
- /* Execute filter on database */
- $wpdb->query(
- $wpdb->prepare("DELETE FROM `$wpdb->statify` WHERE referrer REGEXP %s", $refererRegexp)
- );
+ global $wpdb;
- /* Optimize DB */
- $wpdb->query("OPTIMIZE TABLE `$wpdb->statify`");
- }
- }
+ /* Sanitize URLs */
+ $referer = self::sanitizeURLs( self::$_options['referer'] );
+
+ /* Build filter regexp */
+ $refererRegexp = str_replace( '.', '\.', implode( '|', $referer ) );
+ if ( ! empty( $refererRegexp ) ) {
+ /* Execute filter on database */
+ $wpdb->query(
+ $wpdb->prepare( "DELETE FROM `$wpdb->statify` WHERE referrer REGEXP %s", $refererRegexp )
+ );
+
+ /* Optimize DB */
+ $wpdb->query( "OPTIMIZE TABLE `$wpdb->statify`" );
+ }
+ }
- /**
- * Sanitize URLs and remove empty results
- * @param $urls array given array of URLs
- * @return array sanitized array
- *
- * @since 1.1.1
- */
- private static function sanitizeURLs($urls) {
- return array_filter(
- array_map(
- function($r) {
- return preg_replace('/[^\da-z\.-]/i', '', filter_var($r, FILTER_SANITIZE_URL));
- },
- $urls
- )
- );
- }
+ /**
+ * Sanitize URLs and remove empty results
+ *
+ * @param $urls array given array of URLs
+ *
+ * @return array sanitized array
+ *
+ * @since 1.1.1
+ */
+ private static function sanitizeURLs( $urls ) {
+ return array_filter(
+ array_map(
+ function ( $r ) {
+ return preg_replace( '/[^\da-z\.-]/i', '', filter_var( $r, FILTER_SANITIZE_URL ) );
+ },
+ $urls
+ )
+ );
+ }
}
\ No newline at end of file
diff --git a/inc/statifyblacklist_system.class.php b/inc/statifyblacklist_system.class.php
index 67f2ebb..a8f6b43 100644
--- a/inc/statifyblacklist_system.class.php
+++ b/inc/statifyblacklist_system.class.php
@@ -1,70 +1,69 @@
get_col( "SELECT blog_id FROM `$wpdb->blogs`" );
+ // 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`" );
- foreach ( $ids as $site_id ) {
- switch_to_blog( $site_id );
- add_option(
- 'statify-blacklist',
- array()
- );
- }
+ foreach ( $ids as $site_id ) {
+ switch_to_blog( $site_id );
+ add_option(
+ 'statify-blacklist',
+ array()
+ );
+ }
- restore_current_blog();
- } else {
- add_option(
- 'statify-blacklist',
- array()
- );
- }
- }
+ restore_current_blog();
+ } else {
+ add_option(
+ 'statify-blacklist',
+ array()
+ );
+ }
+ }
- /**
- * Plugin uninstall handler.
- *
- * @since 1.0.0
- */
- public static function uninstall() {
- global $wpdb;
+ /**
+ * Plugin uninstall handler.
+ *
+ * @since 1.0.0
+ */
+ public static function uninstall() {
+ global $wpdb;
- if ( is_multisite() ) {
- $old = get_current_blog_id();
+ 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`" );
+ // Todo: Use get_sites() in WordPress 4.6+
+ $ids = $wpdb->get_col( "SELECT blog_id FROM `$wpdb->blogs`" );
- foreach ( $ids as $id ) {
- switch_to_blog( $id );
- delete_option('statify-blacklist');
- }
+ foreach ( $ids as $id ) {
+ switch_to_blog( $id );
+ delete_option( 'statify-blacklist' );
+ }
- switch_to_blog( $old );
- }
+ switch_to_blog( $old );
+ }
- delete_option('statify-blacklist');
- }
+ delete_option( 'statify-blacklist' );
+ }
}
\ No newline at end of file
diff --git a/statify-blacklist.php b/statify-blacklist.php
index 35e3bb3..8f7dd54 100644
--- a/statify-blacklist.php
+++ b/statify-blacklist.php
@@ -12,37 +12,38 @@ Version: 1.1.2
*/
/* Quit */
-defined('ABSPATH') OR exit;
+defined( 'ABSPATH' ) OR exit;
/* Constants */
-define('STATIFYBLACKLIST_FILE', __FILE__);
-define('STATIFYBLACKLIST_DIR', dirname(__FILE__));
-define('STATIFYBLACKLIST_BASE', plugin_basename(__FILE__));
+define( 'STATIFYBLACKLIST_FILE', __FILE__ );
+define( 'STATIFYBLACKLIST_DIR', dirname( __FILE__ ) );
+define( 'STATIFYBLACKLIST_BASE', plugin_basename( __FILE__ ) );
/* System Hooks */
-add_action('plugins_loaded', array('StatifyBlacklist', 'instance'));
+add_action( 'plugins_loaded', array( 'StatifyBlacklist', 'instance' ) );
-register_activation_hook(STATIFYBLACKLIST_FILE, array('StatifyBlacklist_System', 'install'));
+register_activation_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'install' ) );
-register_uninstall_hook(STATIFYBLACKLIST_FILE, array('StatifyBlacklist_System', 'uninstall'));
+register_uninstall_hook( STATIFYBLACKLIST_FILE, array( 'StatifyBlacklist_System', 'uninstall' ) );
/* Autoload */
-spl_autoload_register('statifyBlacklist_autoload');
+spl_autoload_register( 'statifyBlacklist_autoload' );
/**
* Autoloader for StatifyBlacklist classes.
*
* @param $class
+ *
* @since 1.0.0
*/
-function statifyBlacklist_autoload($class) {
- $plugin_classes = array(
- 'StatifyBlacklist',
- 'StatifyBlacklist_Admin',
- 'StatifyBlacklist_System'
- );
+function statifyBlacklist_autoload( $class ) {
+ $plugin_classes = array(
+ 'StatifyBlacklist',
+ 'StatifyBlacklist_Admin',
+ 'StatifyBlacklist_System'
+ );
- if (in_array($class, $plugin_classes)) {
- require_once(sprintf('%s/inc/%s.class.php', STATIFYBLACKLIST_DIR, strtolower($class)));
- }
+ if ( in_array( $class, $plugin_classes ) ) {
+ require_once( sprintf( '%s/inc/%s.class.php', STATIFYBLACKLIST_DIR, strtolower( $class ) ) );
+ }
}
diff --git a/views/settings_page.php b/views/settings_page.php
index 6a19a16..7e0f5aa 100644
--- a/views/settings_page.php
+++ b/views/settings_page.php
@@ -1,94 +1,104 @@
(int)@$_POST['statifyblacklist']['active_referer'],
- 'referer' => $referer
- )
- );
+ /* Update options (data will be sanitized) */
+ $statifyBlacklistUpdateResult = StatifyBlacklist_Admin::update_options(
+ array(
+ 'active_referer' => (int) @$_POST['statifyblacklist']['active_referer'],
+ 'referer' => $referer
+ )
+ );
- /* Generate messages */
- if ($statifyBlacklistUpdateResult !== false) {
- $statifyBlacklistPostWarning = 'Some URLs are invalid and have been sanitized. Settings have not been saved yet.';
- } else {
- $statifyBlacklistPostSuccess = 'Settings updated successfully.';
- }
- }
+ /* Generate messages */
+ if ( $statifyBlacklistUpdateResult !== false ) {
+ $statifyBlacklistPostWarning = 'Some URLs are invalid and have been sanitized. Settings have not been saved yet.';
+ } else {
+ $statifyBlacklistPostSuccess = 'Settings updated successfully.';
+ }
+ }
}
?>
-
-
';
- esc_html_e('Statify plugin is not active.', 'statify-blacklist');
- print '
';
- }
- if (isset($statifyBlacklistPostWarning)) {
- print '';
- esc_html_e($statifyBlacklistPostWarning);
- print '
';
- }
- if (isset($statifyBlacklistPostSuccess)) {
- print '';
- esc_html_e($statifyBlacklistPostSuccess);
- print '
';
- }
- ?>
-
-
+
+
+
+
+
+
+
+