<?php
if(!defined('ABSPATH'))exit;
class baiduseo_kp{
    public function init(){
        if(is_admin()){
            global $wpdb;
            $charset_collate = '';
            if (!empty($wpdb->charset)) {
              $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
            }
            if (!empty( $wpdb->collate)) {
              $charset_collate .= " COLLATE {$wpdb->collate}";
            }
            if ( ! function_exists( 'dbDelta' ) ) {
                require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            }
            
            if($wpdb->get_var("show tables like '{$wpdb->prefix}baiduseo_kp_log'") !=  $wpdb->prefix."baiduseo_kp_log"){
                 $sql16 = "CREATE TABLE " . $wpdb->prefix . "baiduseo_kp_log(
                    id bigint NOT NULL AUTO_INCREMENT,
                    orderno varchar(64) NOT NULL ,
                    num varchar(255) NOT NULL DEFAULT 0,
                    time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
                    remark varchar(64) NOT NULL,
                    type tinyint default 0,
                    UNIQUE KEY id (id)
                ) $charset_collate;";
                dbDelta($sql16);
            }
            $res = $wpdb->query('Describe '.$wpdb->prefix.'baiduseo_kp_log type');
            
            if($res){
                 
            }else{
               $wpdb->query(' ALTER TABLE '.$wpdb->prefix.'baiduseo_kp_log ADD COLUMN `type` tinyint DEFAULT 0');
            }
        }
        add_filter('baiduseo_check1', [$this, 'baiduseo_yz_key6'], 10, 3);
        add_filter('baiduseo_check2', [$this, 'baiduseo_yz_key7'], 10, 3);
        add_filter('baiduseo_check3', [$this, 'baiduseo_yz_key8'], 10, 3);
        add_filter('baiduseo_check4', [$this, 'baiduseo_yz_key9'], 10, 3);
        add_filter('baiduseo_check5', [$this, 'baiduseo_yz_key10'], 10, 3);
        add_filter('baiduseo_check6', [$this, 'baiduseo_yz_key11'], 10, 3);
        add_filter('baiduseo_check7', [$this, 'baiduseo_yz_key12'], 10, 6);
        
    }
    public static function  get_jifen(){
        $url = 'https://api.seoceo.cn/api/kp/jifen?url='.baiduseo_common::baiduseo_url(0);
        $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)){
            $jifen = wp_remote_retrieve_body($result);
            
            $jifen =$jifen?$jifen:0;
        }else{
            $jifen = 0;
        }
        return $jifen;
    }
    public static function baiduseo_wzt_log(){
        $baiduseo_wzt_log = get_option('baiduseo_wzt_log');
        return $baiduseo_wzt_log;
    }
    public static function baiduseo_get_key(){
        $baiduseo_get_token = baiduseo_common::baiduseo_get_token();
        return $baiduseo_get_token;
    }
    public static function  baiduseo_yz_key($value){
        $key = baiduseo_seo::baiduseo_sanitizing_date($value[0]);
        return $key;
    }
    public static function  baiduseo_yz_key2(){
        return baiduseo_common::baiduseo_dssdd(1);
    }
    public static function  baiduseo_yz_key3($value){
        $key = baiduseo_tag::baiduseo_sanitizing_date($value[1]);
        return $key;
    }
    public static function  baiduseo_yz_key4(){
        $key = baiduseo_zz::baiduseo_sanitizing_date($value[2]);
        return $key;
    }
    public static function  baiduseo_yz_key5($value){
        $arr = self::baiduseo_yz_key2();
        return baiduseo_seo::baiduseo_sanitizing_key($value, $arr);
    }
    public static function  baiduseo_yz_key6($txt,$value){
        if($value){
            $log = self::baiduseo_wzt_log();
            if($log){
                return;
            }else{
                echo json_encode(['msg'=>'请先授权','code'=>0]);exit;
            }
        }else{
            return;
        }
    }
    public static function  baiduseo_yz_key7($txt,$value){
        if($value){
            $log = self::baiduseo_get_key();
            return ['token'=>$log,'status'=>1];
        }else{
            $log = self::baiduseo_wzt_log();
            return ['token'=>$log,'status'=>0];
        }
    }
     public static function  baiduseo_yz_key8($txt,$value){
        if($value){
            $log = self::baiduseo_yz_key2();
            return ['token'=>$log,'status'=>0];
        }else{
            $log = self::baiduseo_wzt_log();
            return ['token'=>$log,'status'=>0];
        }
    }
    public static function  baiduseo_yz_key9($txt,$value){
        $log = self::baiduseo_yz_key($value);
        return json_encode(['code'=>(bool)$log]);
    }
    public static function  baiduseo_yz_key10($txt,$value){
        $log = self::baiduseo_yz_key2($value);
        return json_encode(['code'=>(bool)$log]);
    }
    public static function  baiduseo_yz_key11($txt,$value){
        $log = self::baiduseo_yz_key3($value);
        return json_encode(['code'=>(bool)$log]);
    }
    public static function  baiduseo_yz_key12($txt,$value,$value1,$code1,$code2,$code3){
        $txt = false;
        if($code1){
            foreach($value1 as $k=>$v){
                if(strpos((string)$v,$code1)!==false){
                    $txt = true;
                }
            }
        }elseif($code2){
            foreach($value1 as $k=>$v){
                if(strpos((string)$v,$code2)!==false){
                    $txt = true;
                }
            }
        }elseif($code3){
            foreach($value1 as $k=>$v){
                if(strpos((string)$v,$code3)!==false){
                    $txt = true;
                }
            }
        }
        return $txt;
        
    }
    public static function baiduseo_generate_short_code($str) {
        // 先将中文转为UTF-8编码的哈希
        $hash = md5(trim(urlencode($str)));
        $num = wp_rand(4,8);
        $short_code = substr($hash, 0, $num);
        $short_code = strtr($short_code, ['0'=>'a','1'=>'b','2'=>'c','3'=>'d','4'=>'e','5'=>'f','6'=>'g','7'=>'h','8'=>'i','9'=>'j']);
        
        return $short_code;
    }
}
