Edit File by line
/home/barbar84/www/wp-conte.../plugins/akismet
File: class.akismet.php
<?php
[0] Fix | Delete
[1] Fix | Delete
class Akismet {
[2] Fix | Delete
const API_HOST = 'rest.akismet.com';
[3] Fix | Delete
const API_PORT = 80;
[4] Fix | Delete
const MAX_DELAY_BEFORE_MODERATION_EMAIL = 86400; // One day in seconds
[5] Fix | Delete
[6] Fix | Delete
private static $last_comment = '';
[7] Fix | Delete
private static $initiated = false;
[8] Fix | Delete
private static $prevent_moderation_email_for_these_comments = array();
[9] Fix | Delete
private static $last_comment_result = null;
[10] Fix | Delete
private static $comment_as_submitted_allowed_keys = array( 'blog' => '', 'blog_charset' => '', 'blog_lang' => '', 'blog_ua' => '', 'comment_agent' => '', 'comment_author' => '', 'comment_author_IP' => '', 'comment_author_email' => '', 'comment_author_url' => '', 'comment_content' => '', 'comment_date_gmt' => '', 'comment_tags' => '', 'comment_type' => '', 'guid' => '', 'is_test' => '', 'permalink' => '', 'reporter' => '', 'site_domain' => '', 'submit_referer' => '', 'submit_uri' => '', 'user_ID' => '', 'user_agent' => '', 'user_id' => '', 'user_ip' => '' );
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Is the comment check happening in the context of an API call? Of if false, then it's during the POST that happens after filling out a comment form.
[14] Fix | Delete
*
[15] Fix | Delete
* @var type bool
[16] Fix | Delete
*/
[17] Fix | Delete
private static $is_api_call = false;
[18] Fix | Delete
[19] Fix | Delete
public static function init() {
[20] Fix | Delete
if ( ! self::$initiated ) {
[21] Fix | Delete
self::init_hooks();
[22] Fix | Delete
}
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Initializes WordPress hooks
[27] Fix | Delete
*/
[28] Fix | Delete
private static function init_hooks() {
[29] Fix | Delete
self::$initiated = true;
[30] Fix | Delete
[31] Fix | Delete
add_action( 'wp_insert_comment', array( 'Akismet', 'auto_check_update_meta' ), 10, 2 );
[32] Fix | Delete
add_filter( 'preprocess_comment', array( 'Akismet', 'auto_check_comment' ), 1 );
[33] Fix | Delete
add_filter( 'rest_pre_insert_comment', array( 'Akismet', 'rest_auto_check_comment' ), 1 );
[34] Fix | Delete
[35] Fix | Delete
add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments' ) );
[36] Fix | Delete
add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments_meta' ) );
[37] Fix | Delete
add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_orphaned_commentmeta' ) );
[38] Fix | Delete
add_action( 'akismet_schedule_cron_recheck', array( 'Akismet', 'cron_recheck' ) );
[39] Fix | Delete
[40] Fix | Delete
add_action( 'comment_form', array( 'Akismet', 'add_comment_nonce' ), 1 );
[41] Fix | Delete
[42] Fix | Delete
add_action( 'admin_head-edit-comments.php', array( 'Akismet', 'load_form_js' ) );
[43] Fix | Delete
add_action( 'comment_form', array( 'Akismet', 'load_form_js' ) );
[44] Fix | Delete
add_action( 'comment_form', array( 'Akismet', 'inject_ak_js' ) );
[45] Fix | Delete
add_filter( 'script_loader_tag', array( 'Akismet', 'set_form_js_async' ), 10, 3 );
[46] Fix | Delete
[47] Fix | Delete
add_filter( 'comment_moderation_recipients', array( 'Akismet', 'disable_moderation_emails_if_unreachable' ), 1000, 2 );
[48] Fix | Delete
add_filter( 'pre_comment_approved', array( 'Akismet', 'last_comment_status' ), 10, 2 );
[49] Fix | Delete
[50] Fix | Delete
add_action( 'transition_comment_status', array( 'Akismet', 'transition_comment_status' ), 10, 3 );
[51] Fix | Delete
[52] Fix | Delete
// Run this early in the pingback call, before doing a remote fetch of the source uri
[53] Fix | Delete
add_action( 'xmlrpc_call', array( 'Akismet', 'pre_check_pingback' ) );
[54] Fix | Delete
[55] Fix | Delete
// Jetpack compatibility
[56] Fix | Delete
add_filter( 'jetpack_options_whitelist', array( 'Akismet', 'add_to_jetpack_options_whitelist' ) );
[57] Fix | Delete
add_action( 'update_option_wordpress_api_key', array( 'Akismet', 'updated_option' ), 10, 2 );
[58] Fix | Delete
add_action( 'add_option_wordpress_api_key', array( 'Akismet', 'added_option' ), 10, 2 );
[59] Fix | Delete
[60] Fix | Delete
add_action( 'comment_form_after', array( 'Akismet', 'display_comment_form_privacy_notice' ) );
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
public static function get_api_key() {
[64] Fix | Delete
return apply_filters( 'akismet_get_api_key', defined('WPCOM_API_KEY') ? constant('WPCOM_API_KEY') : get_option('wordpress_api_key') );
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
public static function check_key_status( $key, $ip = null ) {
[68] Fix | Delete
return self::http_post( Akismet::build_query( array( 'key' => $key, 'blog' => get_option( 'home' ) ) ), 'verify-key', $ip );
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
public static function verify_key( $key, $ip = null ) {
[72] Fix | Delete
// Shortcut for obviously invalid keys.
[73] Fix | Delete
if ( strlen( $key ) != 12 ) {
[74] Fix | Delete
return 'invalid';
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
$response = self::check_key_status( $key, $ip );
[78] Fix | Delete
[79] Fix | Delete
if ( $response[1] != 'valid' && $response[1] != 'invalid' )
[80] Fix | Delete
return 'failed';
[81] Fix | Delete
[82] Fix | Delete
return $response[1];
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
public static function deactivate_key( $key ) {
[86] Fix | Delete
$response = self::http_post( Akismet::build_query( array( 'key' => $key, 'blog' => get_option( 'home' ) ) ), 'deactivate' );
[87] Fix | Delete
[88] Fix | Delete
if ( $response[1] != 'deactivated' )
[89] Fix | Delete
return 'failed';
[90] Fix | Delete
[91] Fix | Delete
return $response[1];
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
/**
[95] Fix | Delete
* Add the akismet option to the Jetpack options management whitelist.
[96] Fix | Delete
*
[97] Fix | Delete
* @param array $options The list of whitelisted option names.
[98] Fix | Delete
* @return array The updated whitelist
[99] Fix | Delete
*/
[100] Fix | Delete
public static function add_to_jetpack_options_whitelist( $options ) {
[101] Fix | Delete
$options[] = 'wordpress_api_key';
[102] Fix | Delete
return $options;
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* When the akismet option is updated, run the registration call.
[107] Fix | Delete
*
[108] Fix | Delete
* This should only be run when the option is updated from the Jetpack/WP.com
[109] Fix | Delete
* API call, and only if the new key is different than the old key.
[110] Fix | Delete
*
[111] Fix | Delete
* @param mixed $old_value The old option value.
[112] Fix | Delete
* @param mixed $value The new option value.
[113] Fix | Delete
*/
[114] Fix | Delete
public static function updated_option( $old_value, $value ) {
[115] Fix | Delete
// Not an API call
[116] Fix | Delete
if ( ! class_exists( 'WPCOM_JSON_API_Update_Option_Endpoint' ) ) {
[117] Fix | Delete
return;
[118] Fix | Delete
}
[119] Fix | Delete
// Only run the registration if the old key is different.
[120] Fix | Delete
if ( $old_value !== $value ) {
[121] Fix | Delete
self::verify_key( $value );
[122] Fix | Delete
}
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* Treat the creation of an API key the same as updating the API key to a new value.
[127] Fix | Delete
*
[128] Fix | Delete
* @param mixed $option_name Will always be "wordpress_api_key", until something else hooks in here.
[129] Fix | Delete
* @param mixed $value The option value.
[130] Fix | Delete
*/
[131] Fix | Delete
public static function added_option( $option_name, $value ) {
[132] Fix | Delete
if ( 'wordpress_api_key' === $option_name ) {
[133] Fix | Delete
return self::updated_option( '', $value );
[134] Fix | Delete
}
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
public static function rest_auto_check_comment( $commentdata ) {
[138] Fix | Delete
self::$is_api_call = true;
[139] Fix | Delete
[140] Fix | Delete
return self::auto_check_comment( $commentdata );
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
public static function auto_check_comment( $commentdata ) {
[144] Fix | Delete
// If no key is configured, then there's no point in doing any of this.
[145] Fix | Delete
if ( ! self::get_api_key() ) {
[146] Fix | Delete
return $commentdata;
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
self::$last_comment_result = null;
[150] Fix | Delete
[151] Fix | Delete
$comment = $commentdata;
[152] Fix | Delete
[153] Fix | Delete
$comment['user_ip'] = self::get_ip_address();
[154] Fix | Delete
$comment['user_agent'] = self::get_user_agent();
[155] Fix | Delete
$comment['referrer'] = self::get_referer();
[156] Fix | Delete
$comment['blog'] = get_option( 'home' );
[157] Fix | Delete
$comment['blog_lang'] = get_locale();
[158] Fix | Delete
$comment['blog_charset'] = get_option('blog_charset');
[159] Fix | Delete
$comment['permalink'] = get_permalink( $comment['comment_post_ID'] );
[160] Fix | Delete
[161] Fix | Delete
if ( ! empty( $comment['user_ID'] ) ) {
[162] Fix | Delete
$comment['user_role'] = Akismet::get_user_roles( $comment['user_ID'] );
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
/** See filter documentation in init_hooks(). */
[166] Fix | Delete
$akismet_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
[167] Fix | Delete
$comment['akismet_comment_nonce'] = 'inactive';
[168] Fix | Delete
if ( $akismet_nonce_option == 'true' || $akismet_nonce_option == '' ) {
[169] Fix | Delete
$comment['akismet_comment_nonce'] = 'failed';
[170] Fix | Delete
if ( isset( $_POST['akismet_comment_nonce'] ) && wp_verify_nonce( $_POST['akismet_comment_nonce'], 'akismet_comment_nonce_' . $comment['comment_post_ID'] ) )
[171] Fix | Delete
$comment['akismet_comment_nonce'] = 'passed';
[172] Fix | Delete
[173] Fix | Delete
// comment reply in wp-admin
[174] Fix | Delete
if ( isset( $_POST['_ajax_nonce-replyto-comment'] ) && check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ) )
[175] Fix | Delete
$comment['akismet_comment_nonce'] = 'passed';
[176] Fix | Delete
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
if ( self::is_test_mode() )
[180] Fix | Delete
$comment['is_test'] = 'true';
[181] Fix | Delete
[182] Fix | Delete
foreach( $_POST as $key => $value ) {
[183] Fix | Delete
if ( is_string( $value ) )
[184] Fix | Delete
$comment["POST_{$key}"] = $value;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
foreach ( $_SERVER as $key => $value ) {
[188] Fix | Delete
if ( ! is_string( $value ) ) {
[189] Fix | Delete
continue;
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
if ( preg_match( "/^HTTP_COOKIE/", $key ) ) {
[193] Fix | Delete
continue;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
// Send any potentially useful $_SERVER vars, but avoid sending junk we don't need.
[197] Fix | Delete
if ( preg_match( "/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/", $key ) ) {
[198] Fix | Delete
$comment[ "$key" ] = $value;
[199] Fix | Delete
}
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
$post = get_post( $comment['comment_post_ID'] );
[203] Fix | Delete
[204] Fix | Delete
if ( ! is_null( $post ) ) {
[205] Fix | Delete
// $post can technically be null, although in the past, it's always been an indicator of another plugin interfering.
[206] Fix | Delete
$comment[ 'comment_post_modified_gmt' ] = $post->post_modified_gmt;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
$response = self::http_post( Akismet::build_query( $comment ), 'comment-check' );
[210] Fix | Delete
[211] Fix | Delete
do_action( 'akismet_comment_check_response', $response );
[212] Fix | Delete
[213] Fix | Delete
$commentdata['comment_as_submitted'] = array_intersect_key( $comment, self::$comment_as_submitted_allowed_keys );
[214] Fix | Delete
[215] Fix | Delete
// Also include any form fields we inject into the comment form, like ak_js
[216] Fix | Delete
foreach ( $_POST as $key => $value ) {
[217] Fix | Delete
if ( is_string( $value ) && strpos( $key, 'ak_' ) === 0 ) {
[218] Fix | Delete
$commentdata['comment_as_submitted'][ 'POST_' . $key ] = $value;
[219] Fix | Delete
}
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
$commentdata['akismet_result'] = $response[1];
[223] Fix | Delete
[224] Fix | Delete
if ( isset( $response[0]['x-akismet-pro-tip'] ) )
[225] Fix | Delete
$commentdata['akismet_pro_tip'] = $response[0]['x-akismet-pro-tip'];
[226] Fix | Delete
[227] Fix | Delete
if ( isset( $response[0]['x-akismet-error'] ) ) {
[228] Fix | Delete
// An error occurred that we anticipated (like a suspended key) and want the user to act on.
[229] Fix | Delete
// Send to moderation.
[230] Fix | Delete
self::$last_comment_result = '0';
[231] Fix | Delete
}
[232] Fix | Delete
else if ( 'true' == $response[1] ) {
[233] Fix | Delete
// akismet_spam_count will be incremented later by comment_is_spam()
[234] Fix | Delete
self::$last_comment_result = 'spam';
[235] Fix | Delete
[236] Fix | Delete
$discard = ( isset( $commentdata['akismet_pro_tip'] ) && $commentdata['akismet_pro_tip'] === 'discard' && self::allow_discard() );
[237] Fix | Delete
[238] Fix | Delete
do_action( 'akismet_spam_caught', $discard );
[239] Fix | Delete
[240] Fix | Delete
if ( $discard ) {
[241] Fix | Delete
// The spam is obvious, so we're bailing out early.
[242] Fix | Delete
// akismet_result_spam() won't be called so bump the counter here
[243] Fix | Delete
if ( $incr = apply_filters( 'akismet_spam_count_incr', 1 ) ) {
[244] Fix | Delete
update_option( 'akismet_spam_count', get_option( 'akismet_spam_count' ) + $incr );
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
if ( self::$is_api_call ) {
[248] Fix | Delete
return new WP_Error( 'akismet_rest_comment_discarded', __( 'Comment discarded.', 'akismet' ) );
[249] Fix | Delete
}
[250] Fix | Delete
else {
[251] Fix | Delete
// Redirect back to the previous page, or failing that, the post permalink, or failing that, the homepage of the blog.
[252] Fix | Delete
$redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ( $post ? get_permalink( $post ) : home_url() );
[253] Fix | Delete
wp_safe_redirect( esc_url_raw( $redirect_to ) );
[254] Fix | Delete
die();
[255] Fix | Delete
}
[256] Fix | Delete
}
[257] Fix | Delete
else if ( self::$is_api_call ) {
[258] Fix | Delete
// The way the REST API structures its calls, we can set the comment_approved value right away.
[259] Fix | Delete
$commentdata['comment_approved'] = 'spam';
[260] Fix | Delete
}
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
// if the response is neither true nor false, hold the comment for moderation and schedule a recheck
[264] Fix | Delete
if ( 'true' != $response[1] && 'false' != $response[1] ) {
[265] Fix | Delete
if ( !current_user_can('moderate_comments') ) {
[266] Fix | Delete
// Comment status should be moderated
[267] Fix | Delete
self::$last_comment_result = '0';
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
if ( ! wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
[271] Fix | Delete
wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
[272] Fix | Delete
do_action( 'akismet_scheduled_recheck', 'invalid-response-' . $response[1] );
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
self::$prevent_moderation_email_for_these_comments[] = $commentdata;
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
// Delete old comments daily
[279] Fix | Delete
if ( ! wp_next_scheduled( 'akismet_scheduled_delete' ) ) {
[280] Fix | Delete
wp_schedule_event( time(), 'daily', 'akismet_scheduled_delete' );
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
self::set_last_comment( $commentdata );
[284] Fix | Delete
self::fix_scheduled_recheck();
[285] Fix | Delete
[286] Fix | Delete
return $commentdata;
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
public static function get_last_comment() {
[290] Fix | Delete
return self::$last_comment;
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
public static function set_last_comment( $comment ) {
[294] Fix | Delete
if ( is_null( $comment ) ) {
[295] Fix | Delete
self::$last_comment = null;
[296] Fix | Delete
}
[297] Fix | Delete
else {
[298] Fix | Delete
// We filter it here so that it matches the filtered comment data that we'll have to compare against later.
[299] Fix | Delete
// wp_filter_comment expects comment_author_IP
[300] Fix | Delete
self::$last_comment = wp_filter_comment(
[301] Fix | Delete
array_merge(
[302] Fix | Delete
array( 'comment_author_IP' => self::get_ip_address() ),
[303] Fix | Delete
$comment
[304] Fix | Delete
)
[305] Fix | Delete
);
[306] Fix | Delete
}
[307] Fix | Delete
}
[308] Fix | Delete
[309] Fix | Delete
// this fires on wp_insert_comment. we can't update comment_meta when auto_check_comment() runs
[310] Fix | Delete
// because we don't know the comment ID at that point.
[311] Fix | Delete
public static function auto_check_update_meta( $id, $comment ) {
[312] Fix | Delete
// wp_insert_comment() might be called in other contexts, so make sure this is the same comment
[313] Fix | Delete
// as was checked by auto_check_comment
[314] Fix | Delete
if ( is_object( $comment ) && !empty( self::$last_comment ) && is_array( self::$last_comment ) ) {
[315] Fix | Delete
if ( self::matches_last_comment( $comment ) ) {
[316] Fix | Delete
load_plugin_textdomain( 'akismet' );
[317] Fix | Delete
[318] Fix | Delete
// normal result: true or false
[319] Fix | Delete
if ( self::$last_comment['akismet_result'] == 'true' ) {
[320] Fix | Delete
update_comment_meta( $comment->comment_ID, 'akismet_result', 'true' );
[321] Fix | Delete
self::update_comment_history( $comment->comment_ID, '', 'check-spam' );
[322] Fix | Delete
if ( $comment->comment_approved != 'spam' ) {
[323] Fix | Delete
self::update_comment_history(
[324] Fix | Delete
$comment->comment_ID,
[325] Fix | Delete
'',
[326] Fix | Delete
'status-changed-' . $comment->comment_approved
[327] Fix | Delete
);
[328] Fix | Delete
}
[329] Fix | Delete
} elseif ( self::$last_comment['akismet_result'] == 'false' ) {
[330] Fix | Delete
update_comment_meta( $comment->comment_ID, 'akismet_result', 'false' );
[331] Fix | Delete
self::update_comment_history( $comment->comment_ID, '', 'check-ham' );
[332] Fix | Delete
// Status could be spam or trash, depending on the WP version and whether this change applies:
[333] Fix | Delete
// https://core.trac.wordpress.org/changeset/34726
[334] Fix | Delete
if ( $comment->comment_approved == 'spam' || $comment->comment_approved == 'trash' ) {
[335] Fix | Delete
if ( function_exists( 'wp_check_comment_disallowed_list' ) ) {
[336] Fix | Delete
if ( wp_check_comment_disallowed_list( $comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent ) ) {
[337] Fix | Delete
self::update_comment_history( $comment->comment_ID, '', 'wp-disallowed' );
[338] Fix | Delete
} else {
[339] Fix | Delete
self::update_comment_history( $comment->comment_ID, '', 'status-changed-' . $comment->comment_approved );
[340] Fix | Delete
}
[341] Fix | Delete
} else if ( function_exists( 'wp_blacklist_check' ) && wp_blacklist_check( $comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent ) ) {
[342] Fix | Delete
self::update_comment_history( $comment->comment_ID, '', 'wp-blacklisted' );
[343] Fix | Delete
} else {
[344] Fix | Delete
self::update_comment_history( $comment->comment_ID, '', 'status-changed-' . $comment->comment_approved );
[345] Fix | Delete
}
[346] Fix | Delete
}
[347] Fix | Delete
} else {
[348] Fix | Delete
// abnormal result: error
[349] Fix | Delete
update_comment_meta( $comment->comment_ID, 'akismet_error', time() );
[350] Fix | Delete
self::update_comment_history(
[351] Fix | Delete
$comment->comment_ID,
[352] Fix | Delete
'',
[353] Fix | Delete
'check-error',
[354] Fix | Delete
array( 'response' => substr( self::$last_comment['akismet_result'], 0, 50 ) )
[355] Fix | Delete
);
[356] Fix | Delete
}
[357] Fix | Delete
[358] Fix | Delete
// record the complete original data as submitted for checking
[359] Fix | Delete
if ( isset( self::$last_comment['comment_as_submitted'] ) ) {
[360] Fix | Delete
update_comment_meta( $comment->comment_ID, 'akismet_as_submitted', self::$last_comment['comment_as_submitted'] );
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
if ( isset( self::$last_comment['akismet_pro_tip'] ) ) {
[364] Fix | Delete
update_comment_meta( $comment->comment_ID, 'akismet_pro_tip', self::$last_comment['akismet_pro_tip'] );
[365] Fix | Delete
}
[366] Fix | Delete
}
[367] Fix | Delete
}
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
public static function delete_old_comments() {
[371] Fix | Delete
global $wpdb;
[372] Fix | Delete
[373] Fix | Delete
/**
[374] Fix | Delete
* Determines how many comments will be deleted in each batch.
[375] Fix | Delete
*
[376] Fix | Delete
* @param int The default, as defined by AKISMET_DELETE_LIMIT.
[377] Fix | Delete
*/
[378] Fix | Delete
$delete_limit = apply_filters( 'akismet_delete_comment_limit', defined( 'AKISMET_DELETE_LIMIT' ) ? AKISMET_DELETE_LIMIT : 10000 );
[379] Fix | Delete
$delete_limit = max( 1, intval( $delete_limit ) );
[380] Fix | Delete
[381] Fix | Delete
/**
[382] Fix | Delete
* Determines how many days a comment will be left in the Spam queue before being deleted.
[383] Fix | Delete
*
[384] Fix | Delete
* @param int The default number of days.
[385] Fix | Delete
*/
[386] Fix | Delete
$delete_interval = apply_filters( 'akismet_delete_comment_interval', 15 );
[387] Fix | Delete
$delete_interval = max( 1, intval( $delete_interval ) );
[388] Fix | Delete
[389] Fix | Delete
while ( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB(NOW(), INTERVAL %d DAY) > comment_date_gmt AND comment_approved = 'spam' LIMIT %d", $delete_interval, $delete_limit ) ) ) {
[390] Fix | Delete
if ( empty( $comment_ids ) )
[391] Fix | Delete
return;
[392] Fix | Delete
[393] Fix | Delete
$wpdb->queries = array();
[394] Fix | Delete
[395] Fix | Delete
foreach ( $comment_ids as $comment_id ) {
[396] Fix | Delete
do_action( 'delete_comment', $comment_id );
[397] Fix | Delete
do_action( 'akismet_batch_delete_count', __FUNCTION__ );
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
// Prepared as strings since comment_id is an unsigned BIGINT, and using %d will constrain the value to the maximum signed BIGINT.
[401] Fix | Delete
$format_string = implode( ", ", array_fill( 0, count( $comment_ids ), '%s' ) );
[402] Fix | Delete
[403] Fix | Delete
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->comments} WHERE comment_id IN ( " . $format_string . " )", $comment_ids ) );
[404] Fix | Delete
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( " . $format_string . " )", $comment_ids ) );
[405] Fix | Delete
[406] Fix | Delete
clean_comment_cache( $comment_ids );
[407] Fix | Delete
do_action( 'akismet_delete_comment_batch', count( $comment_ids ) );
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->comments ) ) // lucky number
[411] Fix | Delete
$wpdb->query("OPTIMIZE TABLE {$wpdb->comments}");
[412] Fix | Delete
}
[413] Fix | Delete
[414] Fix | Delete
public static function delete_old_comments_meta() {
[415] Fix | Delete
global $wpdb;
[416] Fix | Delete
[417] Fix | Delete
$interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 );
[418] Fix | Delete
[419] Fix | Delete
# enforce a minimum of 1 day
[420] Fix | Delete
$interval = absint( $interval );
[421] Fix | Delete
if ( $interval < 1 )
[422] Fix | Delete
$interval = 1;
[423] Fix | Delete
[424] Fix | Delete
// akismet_as_submitted meta values are large, so expire them
[425] Fix | Delete
// after $interval days regardless of the comment status
[426] Fix | Delete
while ( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT m.comment_id FROM {$wpdb->commentmeta} as m INNER JOIN {$wpdb->comments} as c USING(comment_id) WHERE m.meta_key = 'akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > c.comment_date_gmt LIMIT 10000", $interval ) ) ) {
[427] Fix | Delete
if ( empty( $comment_ids ) )
[428] Fix | Delete
return;
[429] Fix | Delete
[430] Fix | Delete
$wpdb->queries = array();
[431] Fix | Delete
[432] Fix | Delete
foreach ( $comment_ids as $comment_id ) {
[433] Fix | Delete
delete_comment_meta( $comment_id, 'akismet_as_submitted' );
[434] Fix | Delete
do_action( 'akismet_batch_delete_count', __FUNCTION__ );
[435] Fix | Delete
}
[436] Fix | Delete
[437] Fix | Delete
do_action( 'akismet_delete_commentmeta_batch', count( $comment_ids ) );
[438] Fix | Delete
}
[439] Fix | Delete
[440] Fix | Delete
if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->commentmeta ) ) // lucky number
[441] Fix | Delete
$wpdb->query("OPTIMIZE TABLE {$wpdb->commentmeta}");
[442] Fix | Delete
}
[443] Fix | Delete
[444] Fix | Delete
// Clear out comments meta that no longer have corresponding comments in the database
[445] Fix | Delete
public static function delete_orphaned_commentmeta() {
[446] Fix | Delete
global $wpdb;
[447] Fix | Delete
[448] Fix | Delete
$last_meta_id = 0;
[449] Fix | Delete
$start_time = isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true );
[450] Fix | Delete
$max_exec_time = max( ini_get('max_execution_time') - 5, 3 );
[451] Fix | Delete
[452] Fix | Delete
while ( $commentmeta_results = $wpdb->get_results( $wpdb->prepare( "SELECT m.meta_id, m.comment_id, m.meta_key FROM {$wpdb->commentmeta} as m LEFT JOIN {$wpdb->comments} as c USING(comment_id) WHERE c.comment_id IS NULL AND m.meta_id > %d ORDER BY m.meta_id LIMIT 1000", $last_meta_id ) ) ) {
[453] Fix | Delete
if ( empty( $commentmeta_results ) )
[454] Fix | Delete
return;
[455] Fix | Delete
[456] Fix | Delete
$wpdb->queries = array();
[457] Fix | Delete
[458] Fix | Delete
$commentmeta_deleted = 0;
[459] Fix | Delete
[460] Fix | Delete
foreach ( $commentmeta_results as $commentmeta ) {
[461] Fix | Delete
if ( 'akismet_' == substr( $commentmeta->meta_key, 0, 8 ) ) {
[462] Fix | Delete
delete_comment_meta( $commentmeta->comment_id, $commentmeta->meta_key );
[463] Fix | Delete
do_action( 'akismet_batch_delete_count', __FUNCTION__ );
[464] Fix | Delete
$commentmeta_deleted++;
[465] Fix | Delete
}
[466] Fix | Delete
[467] Fix | Delete
$last_meta_id = $commentmeta->meta_id;
[468] Fix | Delete
}
[469] Fix | Delete
[470] Fix | Delete
do_action( 'akismet_delete_commentmeta_batch', $commentmeta_deleted );
[471] Fix | Delete
[472] Fix | Delete
// If we're getting close to max_execution_time, quit for this round.
[473] Fix | Delete
if ( microtime(true) - $start_time > $max_exec_time )
[474] Fix | Delete
return;
[475] Fix | Delete
}
[476] Fix | Delete
[477] Fix | Delete
if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->commentmeta ) ) // lucky number
[478] Fix | Delete
$wpdb->query("OPTIMIZE TABLE {$wpdb->commentmeta}");
[479] Fix | Delete
}
[480] Fix | Delete
[481] Fix | Delete
// how many approved comments does this author have?
[482] Fix | Delete
public static function get_user_comments_approved( $user_id, $comment_author_email, $comment_author, $comment_author_url ) {
[483] Fix | Delete
global $wpdb;
[484] Fix | Delete
[485] Fix | Delete
if ( !empty( $user_id ) )
[486] Fix | Delete
return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE user_id = %d AND comment_approved = 1", $user_id ) );
[487] Fix | Delete
[488] Fix | Delete
if ( !empty( $comment_author_email ) )
[489] Fix | Delete
return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_author_email = %s AND comment_author = %s AND comment_author_url = %s AND comment_approved = 1", $comment_author_email, $comment_author, $comment_author_url ) );
[490] Fix | Delete
[491] Fix | Delete
return 0;
[492] Fix | Delete
}
[493] Fix | Delete
[494] Fix | Delete
// get the full comment history for a given comment, as an array in reverse chronological order
[495] Fix | Delete
public static function get_comment_history( $comment_id ) {
[496] Fix | Delete
$history = get_comment_meta( $comment_id, 'akismet_history', false );
[497] Fix | Delete
if ( empty( $history ) || empty( $history[ 0 ] ) ) {
[498] Fix | Delete
return false;
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function