<?php
class keyspider_post{
    public function init(){
        global $keyspider_key;
        add_action('wp_ajax_keyspider_zhizhu', [$this,'keyspider_zhizhu']);
        add_action('wp_ajax_keyspider_shouquan', [$this,'keyspider_shouquan']);
        add_action('wp_ajax_keyspider_zhizhu_clear', [$this,'keyspider_zhizhu_clear']);
        add_action('wp_ajax_keyspider_zhizhu_linkdelete', [$this,'keyspider_zhizhu_linkdelete']);
        add_action('wp_ajax_keyspider_liuliang', [$this,'keyspider_liuliang']);
        add_action('wp_ajax_keyspider_liuliang_delete', [$this,'keyspider_liuliang_delete']);
        $keyspider_key = apply_filters('keyspider_dhdfkdksj',4);
    }
    public function keyspider_liuliang_delete(){
        if(isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'keyspider')){
            global $wpdb;
            $res = $wpdb->query("DELETE FROM " . $wpdb->prefix . "keyspider_liuliang");
            echo wp_json_encode(['msg'=>'操作成功','code'=>1]);exit;
        }
        echo wp_json_encode(['msg'=>'操作失败','code'=>0]);exit;
    }
    public function keyspider_liuliang(){
        if(isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'keyspider')){
            global $keyspider_key;
            if(!$keyspider_key){
                $ss  = keyspider_seo::pay_money();
                if(!$ss){
                     echo wp_json_encode(['msg'=>'请先授权','code'=>0]);exit;
                }
            }
            $open = isset($_POST['open'])?(int)$_POST['open']:0;
            $log = isset($_POST['log'])?(int)$_POST['log']:0;
            $pinci = isset($_POST['pinci'])?(int)$_POST['pinci']:0;
            $diyu = isset($_POST['diyu'])?(int)$_POST['diyu']:0;
            
            $keyspider_wyc = get_option('keyspider_liuliang');
            if($keyspider_wyc!==false){
                $res = update_option('keyspider_liuliang',['open'=>$open,'log'=>$log,'pinci'=>$pinci,'diyu'=>$diyu]);
            }else{
                $res = add_option('keyspider_liuliang',['open'=>$open,'log'=>$log,'pinci'=>$pinci,'diyu'=>$diyu]);
            }
            
            echo wp_json_encode(['msg'=>'保存成功','code'=>1]);exit;
            
        }
        echo wp_json_encode(['msg'=>'保存失败','code'=>0]);exit;
    }
    public function keyspider_zhizhu(){
        if(isset($_POST['nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'keyspider')){
           global $keyspider_key;
            if(!$keyspider_key){
                $ss  = keyspider_seo::pay_money();
                if(!$ss){
                     echo wp_json_encode(['msg'=>'请先授权','code'=>0]);exit;
                }
            }
            $list = [
                'open'=>isset($_POST['open'])?(int)$_POST['open']:0,
                'log'=>isset($_POST['log'])?(int)$_POST['log']:0,
                'type'=>isset($_POST['type'])?sanitize_text_field(wp_unslash($_POST['type'])):"",
                'silian_open'=>isset($_POST['silian_open'])?(int)$_POST['silian_open']:0
            ];
            
            $keyspider_zhizhu = get_option('keyspider_zhizhu');
            if($keyspider_zhizhu!==false){
                update_option('keyspider_zhizhu',$list);
                if(isset($_POST['silian_open']) && (int)$_POST['silian_open']==1){
                    keyspider_seo::silian(1);
                }
            }else{
                add_option('keyspider_zhizhu',$list);
            }
            echo wp_json_encode(['msg'=>'保存成功','code'=>1]);exit;
        }
        echo wp_json_encode(['msg'=>'保存失败','code'=>0]);exit;
    }
    public function keyspider_shouquan(){
        if(isset($_POST['nonce']) && isset($_POST['action']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'keyspider')){
            $key = isset($_POST['key'])?sanitize_text_field(wp_unslash($_POST['key'])):'';
            
            $data =  keyspider_common::keyspider_url(0);
            $url1 = isset($_SERVER['SERVER_NAME'])?sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])):"";
            $url = 'https://api.seoceo.cn/api/money/log2?url='.$data.'&url1='.$url1.'&key='.$key.'&type=4';
            $defaults = array(
                'timeout' => 4000,
                'connecttimeout'=>4000,
                'redirection' => 3,
                'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
                'sslverify' => FALSE,
            );
            $result = wp_remote_get($url,$defaults);
            if(!is_wp_error($result)){
                $content = wp_remote_retrieve_body($result);
                if($content){
                     global $keyspider_wzt_log;
       
                    if($keyspider_wzt_log!==false){
                        update_option('keyspider_wzt_log',$key);
                    }else{
                        add_option('keyspider_wzt_log',$key);
                    }
                    echo wp_json_encode(['code'=>1]);exit;
                }
            }
        }
        echo wp_json_encode(['code'=>0]);exit;
    }
    public function keyspider_zhizhu_clear(){
        if(isset($_POST['nonce']) && isset($_POST['action']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'keyspider')){
            global $keyspider_wzt_log;
            if(!$keyspider_wzt_log){
                  echo wp_json_encode(['msg'=>'请先授权','code'=>0]);exit;
            }
            global $keyspider_key;
            if(!$keyspider_key){
                $ss  = keyspider_seo::pay_money();
                if(!$ss){
                     echo wp_json_encode(['msg'=>'请先授权','code'=>0]);exit;
                }
            }
            global $wpdb;
            $res = $wpdb->query( "DELETE FROM " . $wpdb->prefix . "keyspider_zhizhu  " );
            if($res){
            echo wp_json_encode(['code'=>1]);exit; 
            }
        }
        echo wp_json_encode(['msg'=>'删除失败','code'=>0]);exit;
    }
    public function keyspider_zhizhu_linkdelete(){
        if(isset($_POST['nonce']) && isset($_POST['action']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])),'keyspider')){
            global $wpdb;
            $id = isset($_POST['id'])?(int)$_POST['id']:0;
            $res = $wpdb->query($wpdb->prepare("DELETE FROM " . $wpdb->prefix . "keyspider_zhizhu where id=%d",$id));
            echo wp_json_encode(['code'=>1]);exit; 
        }
        echo wp_json_encode(['code'=>0]);exit; 
    }
}