/**

    *WordPress 自动定时清除垃圾评论

    *https://www.weixiaoduo.com/cron-delete-all-spam/

*/

classBing_cron_delete_spam{

    //每隔多少秒清理垃圾评论

    public$time=604800;//自己修改时间,604800 为一周

    //Hook 名

    public$hook='delete_all_spam';

    //初始化

    function__construct(){

        add_action('init',array($this,'cron'));

        add_filter('cron_schedules',array($this,'schedules'));

        add_action($this->hook,array($this,'delete'));

    }

    //设置定时任务

    functioncron(){

        if(!wp_next_scheduled($this->hook))wp_schedule_event(current_time('timestamp'),$this->hook,$this->hook);

    }

    //扩展定时器

    functionschedules($schedules){

        $schedules[$this->hook]=array(

            'interval'=>$this->time,

            'display'=>__('清除垃圾评论')

        );

        return$schedules;

    }

    //删除垃圾评论

    functiondelete(){

        global$wpdb;

        $wpdb->delete($wpdb->comments,array('comment_approved'=>'spam'));

    }

}

$cron_delete_spam=newBing_cron_delete_spam;