query("OPTIMIZE TABLE `" .$wpdb->options. "`");
}
############################
######### INTERN #########
############################
/**
* Initialisierung der internen Variablen
*
* @since 2.4
* @change 2.4
*/
private static function _init_internal_vars()
{
self::$base = plugin_basename(__FILE__);
self::$short = 'antispam_bee';
self::$secret = substr(md5(get_bloginfo('url')), 0, 5). '-comment';
self::$default = array(
'options' => array(
/* Allgemein */
'advanced_check' => 1,
'spam_ip' => 1,
'already_commented' => 1,
'ignore_pings' => 0,
'always_allowed' => 0,
'dashboard_chart' => 1,
'dashboard_count' => 0,
/* Filter */
'country_code' => 0,
'country_black' => '',
'country_white' => '',
'translate_api' => 0,
'translate_lang' => '',
'honey_pot' => 0,
'honey_key' => '',
/* Erweitert */
'flag_spam' => 1,
'email_notify' => 1,
'no_notice' => 1,
'cronjob_enable' => 0,
'cronjob_interval' => 0,
'ignore_filter' => 0,
'ignore_type' => 0,
'ignore_reasons' => array(),
/* Tab */
'tab_index' => 0
),
'reasons' => array(
'css' => 'CSS Hack',
'empty' => 'Empty Data',
'server' => 'Server IP',
'spamip' => 'Spam IP',
'country' => 'Country Check',
'honey' => 'Honey Pot',
'lang' => 'Comment Language'
)
);
}
/**
* Prüfung und Rückgabe eines Array-Keys
*
* @since 2.4.2
* @change 2.4.2
*
* @param array $array Array mit Werten
* @param string $key Name des Keys
* @return mixed Wert des angeforderten Keys
*/
public static function get_key($array, $key)
{
if ( empty($array) or empty($key) or empty($array[$key]) ) {
return null;
}
return $array[$key];
}
/**
* Lokalisierung der Admin-Seiten
*
* @since 0.1
* @change 2.4
*
* @param string $page Kennzeichnung der Seite
* @return boolean TRUE Bei Erfolg
*/
private static function _current_page($page)
{
switch ($page) {
case 'dashboard':
return ( empty($GLOBALS['pagenow']) or ( !empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] == 'index.php' ) );
case 'options':
return ( !empty($_REQUEST['page']) && $_REQUEST['page'] == self::$short );
case 'plugins':
return ( !empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] == 'plugins.php' );
case 'admin-post':
return ( !empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] == 'admin-post.php' );
default:
return false;
}
}
/**
* Einbindung der Sprachdatei
*
* @since 0.1
* @change 2.4
*/
public static function load_plugin_lang()
{
load_plugin_textdomain(
self::$short,
false,
'antispam-bee/lang'
);
}
/**
* Hinzufügen des Links zu den Einstellungen
*
* @since 1.1
* @change 1.1
*/
public static function init_action_links($data)
{
/* Rechte? */
if ( !current_user_can('manage_options') ) {
return $data;
}
return array_merge(
$data,
array(
sprintf(
'%s',
add_query_arg(
array(
'page' => self::$short
),
admin_url('options-general.php')
),
__('Settings')
)
)
);
}
/**
* Meta-Links des Plugins
*
* @since 0.1
* @change 2.4.3
*
* @param array $data Bereits vorhandene Links
* @param string $page Aktuelle Seite
* @return array $data Modifizierte Links
*/
public static function init_row_meta($data, $page)
{
/* Rechte */
if ( $page != self::$base ) {
return $data;
}
return array_merge(
$data,
array(
'Flattr',
'Google+'
)
);
}
/**
* Anzeige der Admin-Notiz
*
* @since 2.4.3
* @change 2.4.3
*/
public static function init_admin_notice() {
/* Alles klar? */
if ( self::_is_version($GLOBALS['wp_version'], '3.3') && self::_is_version(phpversion(), '5.1.2') ) {
return;
}
/* Warnung */
echo sprintf(
'
',
esc_html__('Antispam Bee requires WordPress 3.3 and PHP 5.1.2', self::$short)
);
}
/**
* Vergleich der Versionen
*
* @since 2.4.3
* @change 2.4.3
*
* @param integer $current Aktuelle Version
* @param integer $required Mindestversion
* @return boolean TRUE, wenn Voraussetzungen erfüllt
*/
private static function _is_version($current, $required) {
return version_compare(
$current,
$required. 'alpha',
'>='
);
}
############################
####### RESSOURCEN #######
############################
/**
* Registrierung von Ressourcen (CSS & JS)
*
* @since 1.6
* @change 2.4
*/
public static function init_plugin_sources()
{
/* Infos auslesen */
$plugin = get_plugin_data(__FILE__);
/* JS einbinden */
wp_register_script(
'ab_script',
plugins_url('js/script.js', __FILE__),
array('jquery', 'jquery-ui-core', 'jquery-ui-tabs'),
$plugin['Version']
);
/* CSS einbinden */
wp_register_style(
'ab_style',
plugins_url('css/style.css', __FILE__),
array(),
$plugin['Version']
);
}
/**
* Initialisierung der Optionsseite
*
* @since 0.1
* @change 2.4.3
*/
public static function add_sidebar_menu()
{
/* Menü anlegen */
$page = add_options_page(
'Antispam Bee',
'Antispam Bee',
'manage_options',
self::$short,
array(
'Antispam_Bee_GUI',
'options_page'
)
);
/* JS einbinden */
add_action(
'admin_print_scripts-' . $page,
array(
__CLASS__,
'add_options_script'
)
);
/* CSS einbinden */
add_action(
'admin_print_styles-' . $page,
array(
__CLASS__,
'add_options_style'
)
);
/* PHP laden */
add_action(
'load-' .$page,
array(
__CLASS__,
'init_options_page'
)
);
}
/**
* Initialisierung von JavaScript
*
* @since 1.6
* @change 2.4
*/
public static function add_options_script()
{
wp_enqueue_script('ab_script');
}
/**
* Initialisierung von Stylesheets
*
* @since 1.6
* @change 2.4
*/
public static function add_options_style()
{
wp_enqueue_style('ab_style');
}
/**
* Einbindung der GUI
*
* @since 2.4
* @change 2.4
*/
public static function init_options_page()
{
require_once( dirname(__FILE__). '/inc/gui.class.php' );
}
############################
####### DASHBOARD ########
############################
/**
* Anzeige des Spam-Counters auf dem Dashboard
*
* @since 0.1
* @change 2.4
*/
public static function add_dashboard_count()
{
/* Aktiv? */
if ( !self::get_option('dashboard_count') ) {
return;
}
/* Ausgabe */
echo sprintf(
'
| %s |
%s |
',
esc_html( self::_get_spam_count() ),
esc_html__('Blocked', self::$short)
);
}
/**
* Initialisierung des Dashboard-Chart
*
* @since 1.9
* @change 2.4
*/
public static function add_dashboard_chart()
{
/* Filter */
if ( !current_user_can('level_2') or !self::get_option('dashboard_chart') ) {
return;
}
/* Widget hinzufügen */
wp_add_dashboard_widget(
'ab_widget',
'Antispam Bee',
array(
__CLASS__,
'show_spam_chart'
)
);
/* JS laden */
add_action(
'wp_print_scripts',
array(
__CLASS__,
'add_dashboard_script'
)
);
/* CSS laden */
add_action(
'admin_head',
array(
__CLASS__,
'add_dashboard_style'
)
);
}
/**
* Ausgabe der Dashboard-CSS
*
* @since 1.9
* @change 2.4
*/
public static function add_dashboard_style()
{
/* Plugin-Info */
$plugin = get_plugin_data(__FILE__);
/* CSS registrieren */
wp_register_style(
'ab_chart',
plugins_url('css/dashboard.css', __FILE__),
array(),
$plugin['Version']
);
/* CSS ausgeben */
wp_print_styles('ab_chart');
}
/**
* Ausgabe der Dashboard-JS
*
* @since 1.9
* @change 2.4
*/
public static function add_dashboard_script()
{
/* Init */
$items = (array)self::get_option('daily_stats');
/* Leer? */
if ( empty($items) ) {
return;
}
/* Sortieren */
krsort($items, SORT_NUMERIC);
/* Init */
$output = array(
'created' => array(),
'count' => array()
);
/* Init */
$i = 0;
/* Zeilen loopen */
foreach($items as $timestamp => $count) {
array_push(
$output['created'],
( $timestamp == strtotime('today', current_time('timestamp')) ? __('Today', self::$short) : date('d.m', $timestamp) )
);
array_push(
$output['count'],
(int)$count
);
}
/* Zusammenfassen */
$stats = array(
'created' => implode(',', $output['created']),
'count' => implode(',', $output['count'])
);
/* Plugin-Info */
$plugin = get_plugin_data(__FILE__);
/* JS einbinden */
wp_register_script(
'ab_chart',
plugins_url('js/dashboard.js', __FILE__),
array('jquery'),
$plugin['Version']
);
wp_register_script(
'google_jsapi',
'https://www.google.com/jsapi',
false
);
/* Einbinden */
wp_enqueue_script('google_jsapi');
wp_enqueue_script('ab_chart');
/* Übergeben */
wp_localize_script(
'ab_chart',
'antispambee',
$stats
);
}
/**
* Ausgabe des Dashboard-Chart
*
* @since 1.9
* @change 2.4
*/
public static function show_spam_chart()
{
/* Init */
$items = (array)self::get_option('daily_stats');
/* Ausgabe */
echo sprintf(
'%s
',
( empty($items) ? esc_html__('No data available.', self::$short) : '' )
);
}
############################
######## OPTIONS #########
############################
/**
* Rückgabe der Optionen
*
* @since 2.4
* @change 2.4
*
* @return array $options Array mit Optionen
*/
public static function get_options()
{
if ( !$options = wp_cache_get(self::$short) ) {
$options = wp_parse_args(
get_option(self::$short),
self::$default['options']
);
wp_cache_set(
self::$short,
$options
);
}
return $options;
}
/**
* Rückgabe eines Optionsfeldes
*
* @since 0.1
* @change 2.4.2
*
* @param string $field Name des Feldes
* @return mixed Wert des Feldes
*/
public static function get_option($field)
{
$options = self::get_options();
return self::get_key($options, $field);
}
/**
* Aktualisiert ein Optionsfeld
*
* @since 0.1
* @change 2.4
*
* @param string $field Name des Feldes
* @param mixed Wert des Feldes
*/
private static function _update_option($field, $value)
{
self::update_options(
array(
$field => $value
)
);
}
/**
* Aktualisiert mehrere Optionsfelder
*
* @since 0.1
* @change 2.4
*
* @param array $data Array mit Feldern
*/
public static function update_options($data)
{
/* Option zuweisen */
$options = array_merge(
(array)get_option(self::$short),
$data
);
/* DB updaten */
update_option(
self::$short,
$options
);
/* Cache updaten */
wp_cache_set(
self::$short,
$options
);
}
############################
######## CRONJOBS ########
############################
/**
* Ausführung des täglichen Cronjobs
*
* @since 0.1
* @change 2.4
*/
public static function start_daily_cronjob()
{
/* Kein Cronjob? */
if ( !self::get_option('cronjob_enable') ) {
return;
}
/* Timestamp updaten */
self::_update_option(
'cronjob_timestamp',
time()
);
/* Spam löschen */
self::_delete_old_spam();
}
/**
* Löschung alter Spamkommentare
*
* @since 0.1
* @change 2.4
*/
private static function _delete_old_spam()
{
/* Anzahl der Tage */
$days = (int)self::get_option('cronjob_interval');
/* Kein Wert? */
if ( empty($days) ) {
return false;
}
/* Global */
global $wpdb;
/* Kommentare löschen */
$wpdb->query(
$wpdb->prepare(
"DELETE FROM `$wpdb->comments` WHERE `comment_approved` = 'spam' AND SUBDATE(NOW(), %d) > comment_date_gmt",
$days
)
);
/* DB optimieren */
$wpdb->query("OPTIMIZE TABLE `$wpdb->comments`");
}
/**
* Initialisierung des Cronjobs
*
* @since 0.1
* @change 2.4
*/
public static function init_scheduled_hook()
{
if ( !wp_next_scheduled('antispam_bee_daily_cronjob') ) {
wp_schedule_event(
time(),
'daily',
'antispam_bee_daily_cronjob'
);
}
}
/**
* Löschung des Cronjobs
*
* @since 0.1
* @change 2.4
*/
public static function clear_scheduled_hook()
{
if ( wp_next_scheduled('antispam_bee_daily_cronjob') ) {
wp_clear_scheduled_hook('antispam_bee_daily_cronjob');
}
}
############################
###### SPAMPRÜFUNG #######
############################
/**
* Überprüfung der POST-Werte
*
* @since 0.1
* @change 2.4.2
*/
public static function precheck_incoming_request()
{
/* Nur Frontend */
if ( is_feed() or is_trackback() or self::_is_mobile() ) {
return;
}
/* Allgemeine Werte */
$request_url = self::get_key($_SERVER, 'REQUEST_URI');
$hidden_field = self::get_key($_POST, 'comment');
$plugin_field = self::get_key($_POST, self::$secret);
/* Falsch verbunden */
if ( empty($_POST) or empty($request_url) or strpos($request_url, 'wp-comments-post.php') === false ) {
return;
}
/* Felder prüfen */
if ( empty($hidden_field) && !empty($plugin_field) ) {
$_POST['comment'] = $plugin_field;
unset($_POST[self::$secret]);
} else {
$_POST['bee_spam'] = 1;
}
}
/**
* Prüfung der eingehenden Anfragen auf Spam
*
* @since 0.1
* @change 2.4.2
*
* @param array $comment Unbehandelter Kommentar
* @return array $comment Behandelter Kommentar
*/
public static function handle_incoming_request($comment)
{
/* Server-Werte */
$url = self::get_key($_SERVER, 'REQUEST_URI');
/* Leere Werte? */
if ( empty($url) ) {
return self::_handle_spam_request(
$comment,
'empty'
);
}
/* Ping-Optionen */
$ping = array(
'types' => array('pingback', 'trackback', 'pings'),
'allowed' => !self::get_option('ignore_pings')
);
/* Kommentar */
if ( strpos($url, 'wp-comments-post.php') !== false && !empty($_POST) ) {
/* Filter ausführen */
$status = self::_verify_comment_request($comment);
/* Spam lokalisiert */
if ( !empty($status['reason']) ) {
return self::_handle_spam_request(
$comment,
$status['reason']
);
}
/* Trackback */
} else if ( in_array(self::get_key($comment, 'comment_type'), $ping['types']) && $ping['allowed'] ) {
/* Filter ausführen */
$status = self::_verify_trackback_request($comment);
/* Spam lokalisiert */
if ( !empty($status['reason']) ) {
return self::_handle_spam_request(
$comment,
$status['reason'],
true
);
}
}
return $comment;
}
/**
* Bereitet die Ersetzung des KOmmentarfeldes vor
*
* @since 0.1
* @change 2.4
*/
public static function prepare_comment_field()
{
/* Nur Frontend */
if ( is_feed() or is_trackback() or is_robots() or self::_is_mobile() ) {
return;
}
/* Nur Beiträge */
if ( !is_singular() && !self::get_option('always_allowed') ) {
return;
}
/* Fire! */
ob_start(
array(
'Antispam_Bee',
'replace_comment_field'
)
);
}
/**
* ersetzt das Kommentarfeld
*
* @since 2.4
* @change 2.4.1
*
* @param string $data HTML-Code der Webseite
* @return string Behandelter HTML-Code
*/
public static function replace_comment_field($data)
{
/* Leer? */
if ( empty($data) ) {
return;
}
/* Convert */
return preg_replace(
'#