<?php
if(!defined('ABSPATH'))exit;
class baiduseo_cron_tongbu{
    public function init(){
        add_action( 'baiduseo_cronhook1', [$this,'baiduseo_tongbu'] );
  
        if(!wp_next_scheduled( 'baiduseo_cronhook1' )){
            wp_schedule_event( strtotime(current_time('Y-m-d H:i:00',1)), 'daily', 'baiduseo_cronhook1' );
        }
    }
    public function baiduseo_tongbu(){
        global $wpdb;
       
         
        $currnetTime= current_time( 'Y/m/d H:i:s');
        $this->baiduseo_tongbu2();
        $this->baiduseo_tongbu3();
        
    }

     public function baiduseo_tongbu2(){
        global $wpdb;
        $data =  baiduseo_common::baiduseo_url(0);
        $url = "https://api.seoceo.cn/api/tongbu/kp_log?url={$data}";
        $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);
            $content = json_decode($content,true);
            if(isset($content['data'])){
                $content = $content['data'];
                
                foreach($content as $key=>$val){
                    $res = $wpdb->get_results($wpdb->prepare(' select * from  '.$wpdb->prefix.'baiduseo_kp_log where orderno=%s ',sanitize_text_field($val['orderno'])),ARRAY_A);
                    if(!empty($res)){
                         $wpdb->update($wpdb->prefix . 'baiduseo_kp_log',['num'=>sanitize_text_field($val['num']),'time'=>sanitize_text_field($val['time']),'remark'=>sanitize_text_field($val['remark']),'type'=>(int)$val['type']],['id'=>$val['id']]);
                    }else{
                         $wpdb->insert($wpdb->prefix."baiduseo_kp_log",['num'=>sanitize_text_field($val['num']),'time'=>sanitize_text_field($val['time']),'remark'=>sanitize_text_field($val['remark']),'orderno'=>sanitize_text_field($val['orderno']),'type'=>(int)$val['type']]);
                    }
                }
            }
            
        }
    }
    public function baiduseo_tongbu3(){ 
         global $wpdb;
        $data =  baiduseo_common::baiduseo_url(0);
        $url = "https://api.seoceo.cn/api/tongbu/long?url={$data}";
         $current_timestamp = current_time('timestamp');
        $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);
            $content = json_decode($content,true);
            if(isset($content['data'])){
                $content = $content['data'];
                 $seven_days_later_timestamp = $current_timestamp + 7 * 24 * 60 * 60;
                        
                        
                $seven_days_later = gmdate('Y-m-d H:i:s', $seven_days_later_timestamp);
                foreach($content as $key=>$val){
                    $res = $wpdb->get_results($wpdb->prepare(' select * from  '.$wpdb->prefix.'baiduseo_long where keywords=%s ',sanitize_text_field($val['keywords'])),ARRAY_A);
                    if(!empty($res)){
                         $wpdb->update($wpdb->prefix . 'baiduseo_long',['link'=>sanitize_text_field($val['link']),'guo'=>$seven_days_later,'size'=>(int)$val['size']],['id'=>$res[0]['id']]);
                    }
                }
            }
            
        }
        $baiduseo_tongchang = get_option('baiduseo_tongchang');
        if(is_array($baiduseo_tongchang)){
            $time = current_time( 'timestamp' );
            foreach($baiduseo_tongchang as $k=>$v){
                if($v<$time-24*3600){
                    unset($baiduseo_tongchang[$k]);
                }
            }
            $count = count($baiduseo_tongchang);
            if($count>5){
                delete_option('baiduseo_wzt_log');
            }
        }
        
    }

}