如何根据电子邮件地址过滤用户?

时间:2013-06-02 作者:Monty Swanson

是否有一种方法/插件可以通过电子邮件地址过滤用户?我最近有很多来自特定电子邮件域用户的垃圾邮件帖子。

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

我不确定你指的是用户还是评论员,所以我都选了。

SQL电子邮件保存在$wpdb->comments 在列中comment_author_email 征求意见
保存在$wpdb->users 在列中user_email 对于用户。

要从评论中的电子邮件中获取域部分,我们可以使用:

SELECT (
    SUBSTR(
        `comment_author_email`,
        INSTR( `comment_author_email`, \'@\' ) + 1,
        LENGTH( `comment_author_email` ) - INSTR( `comment_author_email`, \'@\' )
    )
) AS domain
从WordPress的注释表中获取最常用域的完整SQL查询如下所示:

SELECT (
    SUBSTR(
        `comment_author_email`,
        INSTR( `comment_author_email`, \'@\' ) + 1,
        LENGTH( `comment_author_email` ) - INSTR( `comment_author_email`, \'@\' )
    )
) AS domain,
COUNT(*) AS amount
FROM        `$wpdb->comments`
WHERE       `comment_approved` = \'spam\'
GROUP BY    domain
HAVING      amount > 1
ORDER BY    amount DESC
LIMIT 0, 150
我使用该查询创建了一个简单的仪表板小部件。

Download on GitHub.

screenshot

<?php # -*- coding: utf-8 -*-
namespace WPSE;
/**
 * Plugin Name: Top Mail Domains
 * Description: List the most used domain names from spammers and users in two dashboard widgets.
 * Version:     2013.06.02
 * Author:      Thomas Scholz
 * Author URI:  http://wpkrauts.com
 * License:     MIT
 */

\\is_admin() && \\add_action(
    \'wp_dashboard_setup\',
    array (
        Top_Spam_Mail_Domains::get_instance(),
        \'setup\'
    )
);

class Top_Spam_Mail_Domains
{
    protected static $instance;

    public static function get_instance()
    {
        if ( NULL === self::$instance )
            self::$instance = new self;

        return self::$instance;
    }

    public function setup()
    {
        $comment_model  = new Comment_Data;
        $comment_widget = new Widget( $comment_model );

        \\wp_add_dashboard_widget(
            \'top_comment_spam_domains\',
            \'Top Comment Mail Domains\',
            array ( $comment_widget, \'display\' )
        );

        $user_model  = new User_Data;
        $user_widget = new Widget( $user_model );

        \\wp_add_dashboard_widget(
            \'top_user_spam_domains\',
            \'Top User Mail Domains\',
            array ( $user_widget, \'display\' )
        );
    }
}

class Widget
{
    protected $source;

    public function __construct( Data $source )
    {
        $this->source = $source;
    }

    public function display()
    {
        $data = $this->source->get_domains(1);

        if ( empty ( $data ) )
            return print \'No domains found.\';

        print $this->data_to_table( $data );
    }

    protected function data_to_table( Array $data )
    {
        $table  = \'<table class="widefat">\';
        $header = \'<tr><th>Domain</th><th>Amount</th></tr>\';
        $table .= "<thead>$header</thead><tfoot>$header</tfoot>";

        foreach ( $data as $result )
            $table .= sprintf(
                \'<tr class="%1$s"><td>%2$s</td><td>%3$s</td></tr>\',
                $this->row_class(),
                \\esc_html( $result->domain ),
                \\esc_html( $result->amount )
            );

        return "$table</table>";
    }

    protected function row_class()
    {
        static $count = 1;

        return ( $count++ % 2 ) ? \'\' : \'alt\';
    }
}

abstract class Data
{
    public function get_domains( $min = 5 )
    {
        global $wpdb;

        $sql = $this->get_sql( absint( $min ) );
        return $wpdb->get_results( $sql );
    }

    abstract protected function get_sql( $min );
}

class Comment_Data extends Data
{
    protected function get_sql( $min )
    {
        global $wpdb;

        return "
SELECT (
    SUBSTR(
        `comment_author_email`,
        INSTR( `comment_author_email`, \'@\' ) + 1,
        LENGTH( `comment_author_email` ) - INSTR( `comment_author_email`, \'@\' )
    )
) AS domain,
COUNT(*) AS amount
FROM        `$wpdb->comments`
WHERE       `comment_approved` = \'spam\'
GROUP BY    domain
HAVING      amount > $min
ORDER BY    amount DESC
LIMIT 0, 150
        ";
    }
}


class User_Data extends Data
{
    protected function get_sql( $min )
    {
        global $wpdb;

        return "
        SELECT (
        SUBSTR(
        `user_email`,
        INSTR( `user_email`, \'@\' ) + 1,
        LENGTH( `user_email` ) - INSTR( `user_email`, \'@\' )
        )
        ) AS domain,
        COUNT(*) AS amount
        FROM        `$wpdb->users`
        GROUP BY    domain
        HAVING      amount > $min
        ORDER BY    amount DESC
        LIMIT 0, 150
        ";
    }
}

结束

相关推荐

如果USER_META下的字段使用GET_USERS()为空,则回显文本

使用get\\u users()我列出了用户及其信息。我在数据库中有一个自定义字段,people\\u lists\\u class。<?php $blogusers = get_users(\'role=contributor&orderby=display_name\'); foreach ($blogusers as $user) { echo \'<li class=\"person member