Initial commit. Extracted from former pull request on Statify project
This commit is contained in:
commit
4005f936cd
120
inc/statifyblacklist.class.php
Normal file
120
inc/statifyblacklist.class.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/* Quit */
|
||||
defined('ABSPATH') OR exit;
|
||||
|
||||
/**
|
||||
* Statify Blacklist
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class StatifyBlacklist
|
||||
{
|
||||
/**
|
||||
* Plugin options
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static $_options;
|
||||
|
||||
public static $multisite;
|
||||
|
||||
/**
|
||||
* Class self initialize
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function instance()
|
||||
{
|
||||
new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
/* Skip on autosave or AJAX */
|
||||
if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) OR (defined('DOING_AJAX') && DOING_AJAX) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Plugin options */
|
||||
self::update_options();
|
||||
|
||||
self::$multisite = (is_multisite() && array_key_exists(STATIFYBLACKLIST_BASE, (array)get_site_option('active_sitewide_plugins')));
|
||||
|
||||
/* Add Filter to statify hook */
|
||||
add_filter('statify_skip_tracking', array('StatifyBlacklist', 'apply_blacklist_filter'));
|
||||
|
||||
/* Admin only filters */
|
||||
if ( is_admin() ) {
|
||||
add_action('wpmu_new_blog', array('StatifyBlacklist_Install', 'init_site'));
|
||||
|
||||
add_action('delete_blog', array('StatifyBlacklist_System', 'init_site'));
|
||||
|
||||
add_filter('plugin_row_meta', array('StatifyBlacklist_Admin', 'plugin_meta_link'), 10, 2);
|
||||
|
||||
if (is_multisite()) {
|
||||
register_deactivation_hook(__FILE__, array('StatifyBlacklist_Admin', 'single_site_deactivate'));
|
||||
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);
|
||||
} else {
|
||||
add_action('admin_menu', array('StatifyBlacklist_Admin', '_add_menu_page'));
|
||||
add_filter('plugin_action_links', array('StatifyBlacklist_Admin', 'plugin_actions_links'), 10, 2 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update options
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function update_options($options = null) {
|
||||
if (isset($options)) {
|
||||
if ((is_multisite() && array_key_exists(STATIFYBLACKLIST_BASE, (array)get_site_option('active_sitewide_plugins'))))
|
||||
update_site_option('statify-blacklist', $options);
|
||||
else
|
||||
update_option('statify-blacklist', $options);
|
||||
}
|
||||
|
||||
self::$_options = wp_parse_args(
|
||||
get_option('statify-blacklist'),
|
||||
array(
|
||||
'active_referer' => 0,
|
||||
'referer' => ''
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/* Get blacklist */
|
||||
$blacklist = explode("\r\n", self::$_options['referer']);
|
||||
|
||||
/* Check blacklist */
|
||||
return in_array($referer, $blacklist);
|
||||
}
|
||||
}
|
54
inc/statifyblacklist_admin.class.php
Normal file
54
inc/statifyblacklist_admin.class.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
class StatifyBlacklist_Admin extends StatifyBlacklist
|
||||
{
|
||||
public function _add_menu_page() {
|
||||
$title = __( 'Statify Blacklist', 'statify-blacklist' );
|
||||
if (self::$multisite)
|
||||
add_submenu_page( 'settings.php', $title, $title, 'manage_network_plugins', 'statify-blacklist-settings', array('StatifyBlacklist_Admin', 'settings_page') );
|
||||
else
|
||||
add_submenu_page( 'options-general.php', $title, $title, 'manage_options', 'statify-blacklist', array('StatifyBlacklist_Admin', 'settings_page') );
|
||||
|
||||
}
|
||||
|
||||
public static function settings_page() {
|
||||
include STATIFYBLACKLIST_DIR . '/views/settings_page.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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[] = '<a href="https://github.com/stklcode/statify-blacklist">GitHub</a>';
|
||||
}
|
||||
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' );
|
||||
|
||||
if( $file == STATIFYBLACKLIST_BASE && current_user_can('manage_options') ) {
|
||||
array_unshift(
|
||||
$links,
|
||||
sprintf( '<a href="%s">%s</a>', esc_attr(add_query_arg( 'page', 'statify-blacklist', $base )), __('Settings'))
|
||||
);
|
||||
}
|
||||
return $links;
|
||||
}
|
||||
}
|
80
inc/statifyblacklist_system.class.php
Normal file
80
inc/statifyblacklist_system.class.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
/* Quit */
|
||||
defined('ABSPATH') OR exit;
|
||||
|
||||
/**
|
||||
* Statify system configuration
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
class StatifyBlacklist_System extends StatifyBlacklist
|
||||
{
|
||||
/**
|
||||
* Plugin install handler.
|
||||
*
|
||||
* @since 1.0.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`" );
|
||||
|
||||
foreach ( $ids as $site_id ) {
|
||||
switch_to_blog( $site_id );
|
||||
add_option(
|
||||
'statify-blacklist',
|
||||
array()
|
||||
);
|
||||
}
|
||||
|
||||
restore_current_blog();
|
||||
} else {
|
||||
add_option(
|
||||
'statify-blacklist',
|
||||
array()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin deactivation actions
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
public static function deactivate()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin uninstall handler.
|
||||
*
|
||||
* @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`" );
|
||||
|
||||
foreach ( $ids as $id ) {
|
||||
switch_to_blog( $id );
|
||||
delete_option('statify-blacklist');
|
||||
}
|
||||
|
||||
switch_to_blog( $old );
|
||||
}
|
||||
|
||||
delete_option('statify-blacklist');
|
||||
}
|
||||
}
|
74
statifyblacklist.php
Normal file
74
statifyblacklist.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: Statify Blacklist
|
||||
Description: Extension for the statify plugin to add a customizable blacklists.
|
||||
Text Domain: statify-blacklist
|
||||
Domain Path: /lang
|
||||
Author: stklcode
|
||||
Author URI: https://stklcode.de
|
||||
Plugin URI: https://wordpress.org/plugins/statifyRefererBlacklist
|
||||
License: GPLv3 or later
|
||||
Version: 1.0.0
|
||||
*/
|
||||
|
||||
/* Quit */
|
||||
defined('ABSPATH') OR exit;
|
||||
|
||||
/* Constants */
|
||||
define('STATIFYBLACKLIST_FILE', __FILE__);
|
||||
define('STATIFYBLACKLIST_DIR', dirname(__FILE__));
|
||||
define('STATIFYBLACKLIST_BASE', plugin_basename(__FILE__));
|
||||
|
||||
/* System Hooks */
|
||||
add_action(
|
||||
'plugins_loaded',
|
||||
array(
|
||||
'StatifyBlacklist',
|
||||
'instance'
|
||||
)
|
||||
);
|
||||
|
||||
register_activation_hook(
|
||||
STATIFYBLACKLIST_FILE,
|
||||
array(
|
||||
'StatifyBlacklist_System',
|
||||
'install'
|
||||
)
|
||||
);
|
||||
|
||||
register_deactivation_hook(
|
||||
STATIFYBLACKLIST_FILE,
|
||||
array(
|
||||
'StatifyBlacklist_System',
|
||||
'deactivate'
|
||||
)
|
||||
);
|
||||
|
||||
register_uninstall_hook(
|
||||
STATIFYBLACKLIST_FILE,
|
||||
array(
|
||||
'StatifyBlacklist_System',
|
||||
'uninstall'
|
||||
)
|
||||
);
|
||||
|
||||
/* Autoload */
|
||||
spl_autoload_register('statifyBlacklist_autoload');
|
||||
|
||||
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)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
37
views/settings_page.php
Normal file
37
views/settings_page.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/* Update plugin options */
|
||||
if ( ! empty($_POST['statifyblacklist']) ) {
|
||||
StatifyBlacklist::update_options(
|
||||
array(
|
||||
'active_referer' => (int)@$_POST['statifyblacklist']['active_referer'],
|
||||
'referer' => (string)@$_POST['statifyblacklist']['referer']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<div class="wrap">
|
||||
<h1><?php _e( 'Statify Blacklist', 'statify-blacklist') ?></h1>
|
||||
<form action="" method="post" id="statify-blacklist-settings">
|
||||
<ul style="list-style: none;">
|
||||
<li>
|
||||
<label for="statify-blacklist_active_referer">
|
||||
<input type="checkbox" name="statifyblacklist[active_referer]" id="statifyblacklist_active_referer" value="1" <?php checked(StatifyBlacklist::$_options['active_referer'], 1); ?> />
|
||||
<?php esc_html_e('Activate referer blacklist', 'statify-blacklist'); ?>
|
||||
</label>
|
||||
</li>
|
||||
<li>
|
||||
<label for="statify-blacklist_referer">
|
||||
<?php esc_html_e('Referer blacklist:', 'statify-blacklist'); ?><br />
|
||||
<textarea cols="40" rows="5" name="statifyblacklist[referer]" id="statify-blacklist_referer"><?php print StatifyBlacklist::$_options['referer']; ?></textarea><br />
|
||||
<small>(<?php esc_html_e('Add one domain (without subdomains) each line, e.g. example.com', 'statify-blacklist'); ?>)</small>
|
||||
</label>
|
||||
</li>
|
||||
</ul>
|
||||
<?php wp_nonce_field('statify-blacklist-settings'); ?>
|
||||
|
||||
<p class="submit"><input class="button-primary" type="submit" name="submit" value="<?php _e('Save Changes') ?>"></p>
|
||||
</form>
|
||||
</div>
|
Loading…
x
Reference in New Issue
Block a user