query("OPTIMIZE TABLE `" .$wpdb->options. "`");
}
############################
######### INTERN #########
############################
/**
* Initialisierung der internen Variablen
*
* @since 2.4
* @change 2.5.2
*/
private static function _init_internal_vars()
{
self::$_base = plugin_basename(__FILE__);
self::$_secret = substr(md5(get_bloginfo('url')), 0, 5). '-comment';
self::$defaults = array(
'options' => array(
/* Allgemein */
'advanced_check' => 1,
'regexp_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' => '',
'dnsbl_check' => 0,
'bbcode_check' => 1,
/* Erweitert */
'flag_spam' => 1,
'email_notify' => 1,
'no_notice' => 1,
'cronjob_enable' => 0,
'cronjob_interval' => 0,
'ignore_filter' => 0,
'ignore_type' => 0,
'reasons_enable' => 0,
'ignore_reasons' => array()
),
'reasons' => array(
'css' => 'CSS Hack',
'empty' => 'Empty Data',
'server' => 'Server IP',
'localdb' => 'Local DB Spam',
'country' => 'Country Check',
'dnsbl' => 'DNSBL Spam',
'bbcode' => 'BBCode',
'lang' => 'Comment Language',
'regexp' => 'RegExp'
)
);
}
/**
* 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'] == 'antispam_bee' );
case 'plugins':
return ( !empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] == 'plugins.php' );
case 'admin-post':
return ( !empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] == 'admin-post.php' );
case 'edit-comments':
return ( !empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] == 'edit-comments.php' );
default:
return false;
}
}
/**
* Einbindung der Sprachdatei
*
* @since 0.1
* @change 2.4
*/
public static function load_plugin_lang()
{
load_plugin_textdomain(
'antispam_bee',
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' => 'antispam_bee'
),
admin_url('options-general.php')
),
__('Settings')
)
)
);
}
/**
* Meta-Links des Plugins
*
* @since 0.1
* @change 2.4.4
*
* @param array $input Bereits vorhandene Links
* @param string $file Aktuelle Seite
* @return array $data Modifizierte Links
*/
public static function init_row_meta($input, $file)
{
/* Rechte */
if ( $file != self::$_base ) {
return $input;
}
return array_merge(
$input,
array(
'Flattr',
'PayPal'
)
);
}
############################
####### RESSOURCEN #######
############################
/**
* Registrierung von Ressourcen (CSS & JS)
*
* @since 1.6
* @change 2.4.5
*/
public static function init_plugin_sources()
{
/* Infos auslesen */
$plugin = get_plugin_data(__FILE__);
/* JS einbinden */
wp_register_script(
'ab_script',
plugins_url('js/scripts.min.js', __FILE__),
array('jquery'),
$plugin['Version']
);
/* CSS einbinden */
wp_register_style(
'ab_style',
plugins_url('css/styles.min.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',
'antispam_bee',
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.6.0
*/
public static function add_dashboard_count()
{
/* Aktiv? */
if ( ! self::get_option('dashboard_count') ) {
return;
}
/* Add list item icon */
echo '';
/* Ausgabe */
echo sprintf(
'
%s %s
',
add_query_arg(
array(
'page' => 'antispam_bee'
),
admin_url('options-general.php')
),
esc_html( self::_get_spam_count() ),
esc_html__('Blocked', 'antispam_bee')
);
}
/**
* Initialisierung des Dashboard-Chart
*
* @since 1.9
* @change 2.5.6
*/
public static function add_dashboard_chart()
{
/* Filter */
if ( ! current_user_can('publish_posts') 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'
)
);
}
/**
* Print dashboard styles
*
* @since 1.9.0
* @change 2.5.8
*/
public static function add_dashboard_style()
{
/* Get plugin data */
$plugin = get_plugin_data(__FILE__);
/* Register styles */
wp_register_style(
'ab_chart',
plugins_url('css/dashboard.min.css', __FILE__),
array(),
$plugin['Version']
);
/* Embed styles */
wp_print_styles('ab_chart');
}
/**
* Print dashboard scripts
*
* @since 1.9.0
* @change 2.5.8
*/
public static function add_dashboard_script()
{
/* Get stats */
if ( ! self::get_option('daily_stats') ) {
return;
}
/* Get plugin data */
$plugin = get_plugin_data(__FILE__);
/* Register scripts */
wp_register_script(
'sm_raphael_js',
plugins_url('js/raphael.min.js', __FILE__),
array(),
$plugin['Version'],
true
);
wp_register_script(
'sm_raphael_helper',
plugins_url('js/raphael.helper.min.js', __FILE__),
array(),
$plugin['Version'],
true
);
wp_register_script(
'ab_chart_js',
plugins_url('js/dashboard.min.js', __FILE__),
array('jquery'),
$plugin['Version'],
true
);
/* Embed scripts */
wp_enqueue_script('sm_raphael_js');
wp_enqueue_script('sm_raphael_helper');
wp_enqueue_script('ab_chart_js');
}
/**
* Print dashboard html
*
* @since 1.9.0
* @change 2.5.8
*/
public static function show_spam_chart()
{
/* Get stats */
$items = (array)self::get_option('daily_stats');
/* Emty array? */
if ( empty($items) ) {
echo sprintf(
'',
esc_html__('No data available.', 'antispam_bee')
);
return;
}
/* Sort stats */
ksort($items, SORT_NUMERIC);
/* HTML start */
$html = "\n";
/* Timestamp table */
$html .= "\n";
foreach($items as $date => $count) {
$html .= "| " .$date. " | \n";
}
$html .= "
\n";
/* Counter table */
$html .= "\n";
foreach($items as $date => $count) {
$html .= "| " .(int) $count. " | \n";
}
$html .= "
\n";
/* HTML end */
$html .= "
\n";
/* Print html */
echo '' .$html. '
';
}
############################
######## 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('antispam_bee') ) {
$options = wp_parse_args(
get_option('antispam_bee'),
self::$defaults['options']
);
wp_cache_set(
'antispam_bee',
$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('antispam_bee'),
$data
);
/* DB updaten */
update_option(
'antispam_bee',
$options
);
/* Cache updaten */
wp_cache_set(
'antispam_bee',
$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;
}
/* Find the comment textarea */
if ( ! preg_match('##s',
'