Server IP : 172.67.157.199 / Your IP : 3.148.109.216 [ Web Server : Apache System : Linux b70eb322-3aee-0c53-7c82-0db91281f2c6.secureserver.net 6.1.90-1.el9.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 2 12:09:22 EDT 2024 x86_64 User : root ( 0) PHP Version : 8.0.30.2 Disable Function : NONE Domains : 0 Domains MySQL : ON | cURL : ON | WGET : ON | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/chroot/var/www/wp-content/plugins/defender-security/src/model/ |
Upload File : |
<?php /** * The spam comment model class. * * @package WP_Defender\Model */ namespace WP_Defender\Model; /** * Class Spam_Comment * * Provides methods to retrieve spam comment IPs from WordPress database. */ class Spam_Comment { /** * Retrieve aggregated spam comment IPs across all sites in a multisite setup or from a single site. * * @return array Associative array of IP addresses as keys and their spam comment count as values. */ public static function get_spam_comments_ip(): array { global $wpdb; $spam_ips = array(); if ( is_multisite() ) { $offset = 0; $limit = 100; $mu_spam_ips = array(); while ( $blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs} LIMIT {$offset}, {$limit}", ARRAY_A ) ) { // phpcs:ignore if ( ! empty( $blogs ) && is_array( $blogs ) ) { foreach ( $blogs as $blog ) { switch_to_blog( $blog['blog_id'] ); $mu_spam_ips = self::fetch_manual_spam_comments_ip(); if ( ! empty( $mu_spam_ips ) && is_array( $mu_spam_ips ) ) { foreach ( $mu_spam_ips as $ip => $count ) { $spam_ips[ $ip ] = isset( $spam_ips[ $ip ] ) ? $spam_ips[ $ip ] + $count : $count; } } restore_current_blog(); } } $offset += $limit; } } else { $spam_ips = self::fetch_manual_spam_comments_ip(); } return is_array( $spam_ips ) ? $spam_ips : array(); } /** * Fetches spam comment IPs and their counts from the current WordPress site which are flagged by moderator. * * @return array Associative array of IP addresses as keys and their spam comment count as values. */ private static function fetch_manual_spam_comments_ip(): array { global $wpdb; $sql = "SELECT c.comment_author_IP as ip, COUNT(*) as count FROM {$wpdb->comments} c JOIN {$wpdb->commentmeta} cm ON c.comment_ID = cm.comment_id WHERE c.comment_approved = 'spam' AND c.comment_author_IP IS NOT NULL AND c.comment_author_IP != '' AND cm.meta_key = '_wp_trash_meta_time' AND cm.meta_value >= UNIX_TIMESTAMP(NOW() - INTERVAL 12 HOUR) GROUP BY c.comment_author_IP ORDER BY count DESC"; $results = $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore $spam_ips = array(); if ( is_array( $results ) && ! empty( $results ) ) { foreach ( $results as $row ) { $spam_ips[ $row['ip'] ] = (int) $row['count']; } } return $spam_ips; } }