easyiit_stats.php 21.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
<?php
/**
 * Plugin Name: Easy stats
 * Plugin URI: http://www.goodao.cn/
 * Description: Easy stats
 * Version: 1.1.00
 * Author: GD-Shop
 * Author URI: http://www.goodao.cn/
 */
error_reporting(0);

ignore_user_abort(true);
date_default_timezone_set('PRC');
define ( 'Easyiit_Stats_FILE', __FILE__);
define ( 'Easyiit_Stats_DIR', rtrim ( plugin_dir_path ( Easyiit_Stats_FILE ), '/' ) );
define ( 'Easyiit_Stats_URL', rtrim ( plugin_dir_url ( Easyiit_Stats_FILE ), '/' ) );
define ( 'Easyiit_Stats_VERSION', '1.1' );
define ( 'Easyiit_Stats_DB_VERSION', '1.1' );

register_activation_hook(__FILE__, 'easyiit_stats_install');

function easyiit_stats_install() {
    global $wpdb;
    $charset_collate = '';
    if (!empty($wpdb->charset)) {
        $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
    }

    if (!empty( $wpdb->collate)) {
        $charset_collate .= " COLLATE {$wpdb->collate}";
    }
  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  
  $table_name = $wpdb->prefix . "stats_day_ip";
  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
      $sql = "CREATE TABLE `".$table_name."` (
				  `id` bigint(20) NOT NULL AUTO_INCREMENT,
				  `day` date DEFAULT NULL,
				  `ip` varchar(50) DEFAULT NULL,
				  `ip_area` varchar(30) DEFAULT NULL,
				  `pv` int(10) DEFAULT NULL,
				  `uv` int(10) DEFAULT NULL,
				  `update` int(10) DEFAULT NULL,
				  `tableid` int(5) DEFAULT NULL,
				  `request` text,
				  `referrer` text,
				  `is_cf` tinyint(1) DEFAULT '0',
				  `is_spider` tinyint(1) DEFAULT '0',
				  `is_moblie` tinyint(1) DEFAULT '0',
				  `is_old_user` tinyint(1) DEFAULT '0',
				  PRIMARY KEY (`id`)
				) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;";	 
     dbDelta($sql);
  }

  $table_name = $wpdb->prefix . "stats_day_data";
  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
      $sql = "CREATE TABLE `".$table_name."` (
				  `id` bigint(20) NOT NULL AUTO_INCREMENT,
				  `siteid` int(5) DEFAULT NULL,
				  `day_at` date DEFAULT NULL,
				  `year` mediumint(9) DEFAULT NULL,
				  `moth` tinyint(4) DEFAULT NULL,
				  `day` tinyint(4) DEFAULT NULL,
				  `ip` varchar(50) DEFAULT NULL,
				  `ip_area` varchar(50) DEFAULT NULL,
				  `time` int(10) DEFAULT NULL,
				  `time_str` datetime DEFAULT NULL,
				  `remote_host` text,
				  `request` text,
				  `referrer` text,
				  `user_agent` text,
				  PRIMARY KEY (`id`)
				) ENGINE=MyISAM AUTO_INCREMENT=31 DEFAULT CHARSET=utf8";	 
     dbDelta($sql);
  }

  $table_name = $wpdb->prefix . "stats_day_count";
  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
      $sql = "CREATE TABLE `".$table_name."` (
				  `day` date DEFAULT NULL,
				  `ip` int(10) DEFAULT NULL,
				  `pv` int(10) DEFAULT NULL,
				  `uv` int(10) DEFAULT NULL,
				  `updatetime` int(10) DEFAULT NULL
				) ENGINE=MyISAM DEFAULT CHARSET=utf8";	 
     dbDelta($sql);
  }

  $table_name = $wpdb->prefix . "stats_moth_count";
  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
      $sql = "CREATE TABLE `".$table_name."` (
				  `year` int(3) DEFAULT NULL,
				  `moth` tinyint(3) DEFAULT NULL,
				  `ip` int(10) DEFAULT NULL,
				  `pv` int(10) DEFAULT NULL,
				  `uv` int(10) DEFAULT NULL,
				  `updatetime` int(10) DEFAULT NULL
				) ENGINE=MyISAM DEFAULT CHARSET=utf8";	 
     dbDelta($sql);
  }
  $table_name = $wpdb->prefix . "stats_year_count";
  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
      $sql = "CREATE TABLE `".$table_name."` (
				  `year` mediumint(5) DEFAULT NULL,
				  `ip` int(10) DEFAULT NULL,
				  `pv` int(10) DEFAULT NULL,
				  `uv` int(10) DEFAULT NULL,
				  `updatetime` int(10) DEFAULT NULL
				) ENGINE=MyISAM DEFAULT CHARSET=utf8";	 
     dbDelta($sql);
  }
  update_option( "stats_db_version", $stats_db_version );
}

// 插件停用时,运行回调方法删除数据表,删除options表中的插件版本号
register_deactivation_hook(__FILE__, 'easyiit_stats_uninstall');
function easyiit_stats_uninstall() {

}

include_once(Easyiit_Stats_DIR."/function/function.php");
add_action('admin_menu', 'stats_settings');
function stats_settings() {
    add_menu_page('统计系统', '统计系统', 7, 'stats_settings');
	add_submenu_page('stats_settings', '设置', '设置', 7, 'stats_settings', 'settings');
}

function settings() {
	global $wpdb;
	include_once(Easyiit_Stats_DIR."/class/easyiit_stats.class.php");	
	$easy_stats = new easyiit_stats;		
	if(isset($_POST['reset_stats_data'])&&$_POST['reset_stats_data']){
        $easy_stats->reset_stats();
		$info = "清空数据成功。";
		echo stats_show_message($info);
	}
    $stats_setting = get_option('stats_settings');
    $month_start = explode("-",$stats_setting['month_start']); 
    
	if(isset($_POST['save_settings'])&&$_POST['save_settings']){        
        $data['start_day_num']=$_POST['start_day_num'];
	    $data['month_start'] = $_POST['m_start_year'].'-'.$_POST['m_start_month'];
	    update_option('stats_settings',$data);
	    $info = "保存成功。";
	    echo stats_show_message($info);
	    $stats_setting = get_option('stats_settings');
        $month_start = explode("-",$stats_setting['month_start']); 
	}
	
?>
<h3>操作</h3>
<form method="post" action="" onsubmit='if(!confirm("确认清空所有统计数据吗")){return false;}'>
	<input type="submit" name="reset_stats_data" value="清空所有统计数据">
	<p style="color:red">此功能将重置数据库,清空所有统计数据!</p>
</form>
<h3>设置</h3>
<p>
<form method="post" action="">
<p><label>保留数据天数:</label><input type="text" name="start_day_num" value='<?php echo $stats_setting['start_day_num'];?>'></p>
<p><label>月度统计起始月份:</label>
<select name="m_start_year">
    <?php for($y=2016;$y<2030;$y++){?>
        <option value="<?php echo $y?>" <?php if ($y==$month_start[0]) echo 'selected';?>><?php echo $y?></option>
	<?php }?>
</select>
<select name="m_start_month">
    <option value="01" <?php if ('01'==$month_start[1]) echo 'selected';?>>01</option>
	<option value="02" <?php if ('02'==$month_start[1]) echo 'selected';?>>02</option>
	<option value="03" <?php if ('03'==$month_start[1]) echo 'selected';?>>03</option>
	<option value="04" <?php if ('04'==$month_start[1]) echo 'selected';?>>04</option>
	<option value="05" <?php if ('05'==$month_start[1]) echo 'selected';?>>05</option>
	<option value="06" <?php if ('06'==$month_start[1]) echo 'selected';?>>06</option>
	<option value="07" <?php if ('07'==$month_start[1]) echo 'selected';?>>07</option>
	<option value="08" <?php if ('08'==$month_start[1]) echo 'selected';?>>08</option>
	<option value="09" <?php if ('09'==$month_start[1]) echo 'selected';?>>09</option>
	<option value="10" <?php if ('10'==$month_start[1]) echo 'selected';?>>10</option>
	<option value="11" <?php if ('11'==$month_start[1]) echo 'selected';?>>11</option>
	<option value="12" <?php if ('12'==$month_start[1]) echo 'selected';?>>12</option>
</select>
</p>
<input type="submit" name="save_settings" value="保存">
</form>
</p>
<?php
}

add_action( 'wp_ajax_nopriv_stats_init', 'stats_init' );
add_action( 'wp_ajax_stats_init', 'stats_init' );
function stats_init($name) {
	include_once(Easyiit_Stats_DIR."/class/easyiit_stats.class.php");	
	$easy_stats = new easyiit_stats;		
	$easy_stats->init();
	echo 200;
	exit();
}

/*function add_stats_scripts() {
    wp_enqueue_script( 'stats-script', '/style/public/stats/stats_init.js', array( 'jquery' ), '20170720', true );
}
add_action( 'wp_enqueue_scripts', 'add_stats_scripts' );*/
 

function stats_data() {
	global $wpdb;
	//统计数据
   $day = date('Y-m-d');
   $res = $wpdb->get_row("select pv,uv,ip from wp_stats_day_count where day ='$day'",ARRAY_A);
   $data['today_pv'] = (int)$res['pv'];
   $data['today_ip'] = (int)$res['ip'];
   $data['today_uv'] = (int)$res['uv'];
   //全部访问统计
   $res =array();
   $res = $wpdb->get_row("select ip, pv, uv from wp_stats_year_count",ARRAY_A);
   $totol_ip = $wpdb->get_row("select count(id) t_ip, sum(pv) t_pv from wp_stats_day_ip where is_spider=1",ARRAY_A);
   $data['total_ip'] = (int)$totol_ip['t_ip'];
   $data['total_pv'] = (int)$totol_ip['t_pv'];
   $data['total_uv'] = (int)$res['uv'];
   //全部询盘数
   $res = $wpdb->get_row("select count(*) as total from wp_cf7_data",ARRAY_A);
   $data['cf_count']= (int)$res['total'];
   //询盘未读
   $res =array();
   $res = $wpdb->get_row("select count(*) as total from wp_cf7_data_entry where name='readed' and value='0'",ARRAY_A);
   $data['total_cf_noread']= $res['total'];

   //上月询盘转化率
   $data['total_cf_f']= number_format(($data['cf_count']/$data['total_ip']), 4)*100;

   //周访问统计
   $wpdb->query("select day, ip, pv, uv from wp_stats_day_count where  day >= date_format(date_sub(date_sub(now(), INTERVAL WEEKDAY(NOW()) DAY), INTERVAL 1 WEEK), '%Y-%m-%d')",ARRAY_A);
   $week_data = $wpdb->last_result;
   for($k=-6;$k<=0;$k++){
	   $d = date('Y-m-d',strtotime ( "$k day"));
	   $res =array();
	   $res = $wpdb->get_row("select day, ip, pv, uv from wp_stats_day_count where  day='$d'",ARRAY_A);
	   $res['ip']=$res['ip']?$res['ip']:0;
	   $res['pv']=$res['pv']?$res['pv']:0;
	   $res['uv']=$res['uv']?$res['uv']:0;
	   $res['day']=$res['day']?$res['day']:$d;
	   $data['last7days_ip'][]=(int)$res['ip'];
	   $data['last7days_pv'][]=(int)$res['pv'];
	   $data['last7days_uv'][]=(int)$res['uv'];
	   $data['last7days'][$d]=$res;
   }

   $data['last7days_start_y']=date('Y',strtotime ( "-6 day"));
   $data['last7days_start_y']=date('Y',strtotime ( "-6 day"));
   $data['last7days_start_m']=intval(date('m',strtotime ( "-6 day")));
   $data['last7days_start_d']=intval(date('d',strtotime ( "-6 day")));
   $data['last7days_end_y']=date('Y');
   $data['last7days_end_m']=intval(date('m'));
   $data['last7days_end_d']=intval(date('d'));
   
   //访问国家前10
   
   $res =array();
   $sql = 'select * from ( 
		select count(1) ip_area,ip_area show_area, SUM(pv) show_pv, SUM(uv) show_uv, count(ip) show_ip 
		from wp_stats_day_ip t 
		group by t.ip_area 
		order by count(1) desc 
		) t_1 limit 0, 15';
   $wpdb->query($sql);
   $country_top10 = $wpdb->last_result;
   $tmp_i = 0;
   foreach($country_top10 as $key=>$v){
	   if($v->show_area!='Unknown' && $v->show_area!='香港' && $v->show_area!='中国' && $v->show_area!='台湾' && $v->show_area!=''){
			if($tmp_i < 10){
				$data['country_top10_country'][]=$v->show_area;
				$data['country_top10_pv'][]=(int)$v->show_pv;
				$data['country_top10_ip'][]=(int)$v->show_ip;
			}
			$tmp_i ++;
	   }
   }
   //最近访问前30 ip
   $res =array();
   $wpdb->query('select * from wp_stats_day_ip where is_spider=1 order by id desc limit 0,30');
   $ip_view_list = $wpdb->last_result;
   $tmp_data  = array();
   foreach($ip_view_list as $v){
       $v->update=date("Y-m-d H:i:s",$v->update);
	   if($v->ip_area == 'Unknown'){
		   $v->ip_area = '美国';
	   }
	   $tmp_data[]=$v;
   }
   $data['ip_view_list'] = $tmp_data;
   echo json_encode($data);exit();
   
}
add_action( 'wp_ajax_nopriv_stats_data', 'stats_data' );
add_action( 'wp_ajax_stats_data', 'stats_data' );

function stats_get_ip_detail() {
	global $wpdb;
   $id = intval($_GET['id']);
   $res = $wpdb->get_row("select day,ip from wp_stats_day_ip where id ='$id'",ARRAY_A);
   $ip = $res['ip'];
   $day = $res['day'];
   $wpdb->query("select time_str,request from wp_stats_day_data where day_at = '$day' AND ip = '$ip' order by id desc limit 0,30");
   $ip_detail = $wpdb->last_result;
   echo json_encode($ip_detail);exit();
}

add_action( 'wp_ajax_nopriv_stats_get_ip_detail', 'stats_get_ip_detail' );
add_action( 'wp_ajax_stats_get_ip_detail', 'stats_get_ip_detail' );

function stats_data_uvhz() {
	global $wpdb;
   
   //本月
   $res =array();
   $y=date('Y',strtotime($_GET['month']));
   $m=intval(date('m',strtotime($_GET['month'])));

   $res = $wpdb->get_row("select ip, pv, uv from wp_stats_moth_count where year='$y' and moth='$m'",ARRAY_A);
   $data['moth_moth'] =date('Y年m月', strtotime($_GET['month']));   
   $data['moth_begin'] = $moth_begin = $BeginDate = date('Y-m-01', strtotime($_GET['month']));
   $data['moth_end']= $moth_end = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));

   $data['moth_ip'] = intval($res['ip']);
   $data['moth_pv'] = intval($res['pv']);
   $data['moth_uv'] = intval($res['uv']);
   
   for($k=$data['moth_begin'];$k<=$data['moth_end'];$k++){
	   $res =array();
	   $day=$k;
	   $res = $wpdb->get_row("select ip, pv, uv from wp_stats_day_count where day='$day'",ARRAY_A);
	    $v['ip'] = intval($res['ip']);
		$v['pv'] = intval($res['pv']);
		$v['uv'] = intval($res['uv']);
	    $data['moth']['data'][$day] = $v;
   }
   //国家前10
   
	$res_cf7_ip_area = $wpdb->get_results("select b.`value` as ip_area from wp_cf7_data as d left join wp_cf7_data_entry as b on d.id=b.data_id where b.`name`='ip_area' AND (unix_timestamp(d.`created`) >= unix_timestamp('".$moth_begin."') AND unix_timestamp(d.`created`) <= unix_timestamp('".$moth_end."'))",ARRAY_A);
	$ip_areas = $country_top10 =[];
	if($res_cf7_ip_area){
		foreach ($res_cf7_ip_area as $v){
			$ip_areas[] = $v['ip_area'];
		}
	}
	$i = 0;
	if($ip_areas){
		$sort_ip_area = array_count_values($ip_areas);
		arsort($sort_ip_area);
		foreach ($sort_ip_area as $k=>$v){
			if($i < 10 && $k != '香港' && $k != '台湾' && $k != '未知' && $k != ''){
				$tmp_a['ip_area'] = $v;
				$tmp_a['show_area'] = $k;
				$country_top10[] = $tmp_a;
				$i++;
			}
		}
	}

   $data['country_top10'] = $country_top10;
   //询盘量
   $res =array();
   $s_time_s = $data['moth_begin'].' 00:00:00';
   $e_time_s = $data['moth_end'].' 23:59:59';
   $res = $wpdb->get_row("select count(*) as total from wp_cf7_data where created between '".$s_time_s."' and '".$e_time_s."'",ARRAY_A);

   $data['cf_count']= $res['total'];
   //询盘转化率
   $data['cf_f_count']= number_format($data['cf_count']/$data['moth_ip'], 4)*100;
 
   echo json_encode($data);exit();
   
}
add_action( 'wp_ajax_nopriv_stats_data_uvhz', 'stats_data_uvhz' );
add_action( 'wp_ajax_stats_data_uvhz', 'stats_data_uvhz' );

add_action( 'wp_ajax_nopriv_stats_data_uvzd', 'stats_data_uvzd' );
add_action( 'wp_ajax_stats_data_uvzd', 'stats_data_uvzd' );
function stats_data_uvzd(){
	global $wpdb;
	$res =array();
	$y=date('Y',strtotime($_GET['month']));
	$m=intval(date('m',strtotime($_GET['month'])));
	$res = $wpdb->get_row("select ip, pv, uv from wp_stats_moth_count where year='$y' and moth='$m'",ARRAY_A);
	$data['moth_moth'] =date('Y年m月', strtotime($_GET['month']));   
	$data['moth_begin'] = $moth_begin = $BeginDate = date('Y-m-01', strtotime($_GET['month']));
	$data['moth_end']= $moth_end = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));

	$data['moth_ip'] = intval($res['ip']);
	$data['moth_pv'] = intval($res['pv']);
	$data['moth_uv'] = intval($res['uv']);

	//国家前10
	$res =array();

	$sql = "select * from ( 
		select count(1) ip_area,ip_area show_area, SUM(pv) show_pv, SUM(uv) show_uv, count(ip) show_ip 
		from wp_stats_day_ip t 
		where t.day>='$moth_begin' and t.is_spider=1 and t.day <='$moth_end' 
		group by t.ip_area 
		order by count(1) desc 
		) t_1 limit 0, 10";
	$wpdb->query($sql);
	$country_top10_area = array();
	if($wpdb->last_result){
		foreach($wpdb->last_result as $val){
			if($val->show_area != '台湾' && $val->show_area != '香港' && $val->show_area != 'Unknown'){
				$country_top10_area[] = $val;
			}
		}
	}
	$country_top10 = $country_top10_area;
	$data['country_top10'] = $country_top10;
	$res_zd = $wpdb->get_row("select count(id) as pc from wp_stats_day_ip where is_moblie=0 and date(`day`) between '".$data['moth_begin']."' and '".$data['moth_end']."'",ARRAY_A);
	$data['count_pc'] = $res_zd['pc'];
	$res_zd = $wpdb->get_row("select count(id) as mo from wp_stats_day_ip where is_moblie=1 and date(`day`) between '".$data['moth_begin']."' and '".$data['moth_end']."'",ARRAY_A);
	$data['count_mo'] = $res_zd['mo'];
	echo json_encode($data);
	exit();
}


add_action( 'wp_ajax_nopriv_stats_data_uvym', 'stats_data_uvym' );
add_action( 'wp_ajax_stats_data_uvym', 'stats_data_uvym' );
function stats_data_uvym(){
	global $wpdb;
	$res =array();
	$y=date('Y',strtotime($_GET['month']));
	$m=intval(date('m',strtotime($_GET['month'])));
	$res = $wpdb->get_row("select ip, pv, uv from wp_stats_moth_count where year='$y' and moth='$m'",ARRAY_A);
	$data['moth_moth'] =date('Y年m月', strtotime($_GET['month']));   
	$data['moth_begin'] = $moth_begin = $BeginDate = date('Y-m-01', strtotime($_GET['month']));
	$data['moth_end']= $moth_end = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));

	$data['moth_ip'] = intval($res['ip']);
	$data['moth_pv'] = intval($res['pv']);
	$data['moth_uv'] = intval($res['uv']);

	//受访页面前15
	$res =array();

	$sql = "select * from ( 
			select count(1) num_request,request
			from wp_stats_day_ip t 
			where t.day>='$moth_begin' and t.is_spider=1 and t.day <='$moth_end' 
			group by t.request 
			order by count(1) desc 
			) t_1 limit 0, 15";
	$wpdb->query($sql);
	$referrer_top15_area = array();
	if($wpdb->last_result){
		foreach($wpdb->last_result as $val){
			if($val->request != ''){
				$referrer_top15_area[] = $val;
			}
		}
	}
	$data['referrer_top15_area'] = $referrer_top15_area;
	
	echo json_encode($data);
	exit();
}

add_action( 'wp_ajax_nopriv_stats_data_uvly', 'stats_data_uvly' );
add_action( 'wp_ajax_stats_data_uvly', 'stats_data_uvly' );
function stats_data_uvly(){
	global $wpdb;
	$res =array();
	$y=date('Y',strtotime($_GET['month']));
	$m=intval(date('m',strtotime($_GET['month'])));
	$res = $wpdb->get_row("select ip, pv, uv from wp_stats_moth_count where year='$y' and moth='$m'",ARRAY_A);
	$data['moth_moth'] =date('Y年m月', strtotime($_GET['month']));   
	$data['moth_begin'] = $moth_begin = $BeginDate = date('Y-m-01', strtotime($_GET['month']));
	$data['moth_end']= $moth_end = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));

	$data['moth_ip'] = intval($res['ip']);
	$data['moth_pv'] = intval($res['pv']);
	$data['moth_uv'] = intval($res['uv']);

	//访问来源页面前15
	$res =array();

	$sql = "select * from ( 
			select count(1) num_referrer,referrer
			from wp_stats_day_ip t 
			where t.day>='$moth_begin' and t.is_spider=1 and t.day <='$moth_end' 
			group by t.referrer 
			order by count(1) desc 
			) t_1 limit 0, 15";
	$wpdb->query($sql);
	$referrer_top15_area = array();
	if($wpdb->last_result){
		foreach($wpdb->last_result as $val){
			if(($val->referrer != '')){
				if(strpos($val->referrer,home_url()) !== 0){
					if(strlen($val->referrer) > 50){
                    	$val->referrer = substr($val->referrer,0,50);
                    }
					$referrer_top15_area[] = $val;
				}
			}
		}
	}
	$data['referrer_top15_area'] = $referrer_top15_area;
	
	echo json_encode($data);
	exit();
}

add_action( 'wp_ajax_nopriv_stats_data_xptj', 'stats_data_xptj' );
add_action( 'wp_ajax_stats_data_xptj', 'stats_data_xptj' );
function stats_data_xptj(){
	global $wpdb;
	$res =array();
	$y=date('Y',strtotime($_GET['month']));
	$m=intval(date('m',strtotime($_GET['month'])));
	$data['moth_moth'] =date('Y年m月', strtotime($_GET['month']));   
	$data['moth_begin'] = $moth_begin = $BeginDate = date('Y-m-01', strtotime($_GET['month']));
	$data['moth_end']= $moth_end = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
	//全部询盘数
	$res = $wpdb->get_row("select count(*) as total from wp_cf7_data",ARRAY_A);
	$data['cf_count']= (int)$res['total'];

	//上月询盘数量
		
	$data['moth_end_sfm'] = $data['moth_end'].' 23:59:59';
	//访问国家top15
	$s_time_s = $data['moth_begin'].' 00:00:00';
	$e_time_s = $data['moth_end'].' 23:59:59';
	$res_cf7_ip_area = $wpdb->get_results("select b.`value` as ip_area from wp_cf7_data as d left join wp_cf7_data_entry as b on d.id=b.data_id where b.`name`='ip_area' AND (unix_timestamp(d.`created`) >= unix_timestamp('".$s_time_s."') AND unix_timestamp(d.`created`) <= unix_timestamp('".$e_time_s."'))",ARRAY_A);
	$ip_areas = $country_top15_area =[];
	if($res_cf7_ip_area){
		foreach ($res_cf7_ip_area as $v){
			$ip_areas[] = $v['ip_area'];
		}
	}
	$i = 0;
	if($ip_areas){
		$sort_ip_area = array_count_values($ip_areas);
		arsort($sort_ip_area);
		foreach ($sort_ip_area as $k=>$v){
			if($i < 15 && $k != '香港' && $k != '台湾' && $k != '未知' && $k != ''){
				$tmp_a['ip_area'] = $v;
				$tmp_a['show_area'] = $k;
				$country_top15_area[] = $tmp_a;
				$i++;
			}
		}
	}
	$res1 = $wpdb->get_row("select count(*) as total from wp_cf7_data where created between '".$s_time_s."' and '".$e_time_s."'",ARRAY_A);
	$data['cf_count_month']= (int)$res1['total'];
	
	$data['country_top20'] = $country_top15_area;
	echo json_encode($data);exit();
}

add_action( 'wp_ajax_nopriv_stats_data_uvgj', 'stats_data_uvgj' );
add_action( 'wp_ajax_stats_data_uvgj', 'stats_data_uvgj' );
function stats_data_uvgj(){
	global $wpdb;
	$res =array();
	$y=date('Y',strtotime($_GET['month']));
	$m=intval(date('m',strtotime($_GET['month'])));
	$res = $wpdb->get_row("select ip, pv, uv from wp_stats_moth_count where year='$y' and moth='$m'",ARRAY_A);
	$data['moth_moth'] =date('Y年m月', strtotime($_GET['month']));   
	$data['moth_begin'] = $moth_begin = $BeginDate = date('Y-m-01', strtotime($_GET['month']));
	$data['moth_end']= $moth_end = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));

	$data['moth_ip'] = intval($res['ip']);
	$data['moth_pv'] = intval($res['pv']);
	$data['moth_uv'] = intval($res['uv']);

	//访问国家top15
	$country_top15_area = array();
	$sql = "select * from ( 
			select count(1) ip_area,ip_area show_area
			from wp_stats_day_ip t 
			where t.day>='$moth_begin' and t.is_spider=1 and t.day <='$moth_end' 
			group by t.ip_area 
			order by count(1) desc 
			) t_1 limit 0, 19";
	$wpdb->query($sql);
	if($wpdb->last_result){
		foreach($wpdb->last_result as $val){
			if($val->show_area != '中国' &&$val->show_area != '台湾' && $val->show_area != '香港' && $val->show_area != 'Unknown' && $val->show_area != ''){
				$country_top15_area[] = $val;
			}
		}
	}
	$data['country_top15'] = $country_top15_area;
	echo json_encode($data);
	exit();
}

?>