forked from blogfor/search-keyword-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathss-keyword-trace.php
120 lines (98 loc) · 4.03 KB
/
ss-keyword-trace.php
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
<?php
/**
* @author Ramen Dey & Bivash Kanti Pal <[email protected]>
* @package Search Statistics
* @version 1.0.0
* @copyright 20013 - blogfordeveloper.com
* @license GNU GPL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
$referer = '';
$search_engine = '';
$repeat_coount='' ;
/**
* Saves keyword and referer info to db
*
*
*/
function save_keyword( $keyword, $referer ) {
global $wpdb;
if( !$keyword ) return false;
$date = date( 'YmdHi' );
$referer_info = parse_url( $referer );
$mySearch =& new WP_Query("s=$keyword & showposts=-1");
$NumResults = $mySearch->post_count;
if(is_user_logged_in())
{
global $current_user;
get_currentuserinfo();
$user=$current_user->ID;
}
else
{
$user='Non-Registered';
}
$search_count=search_count($keyword,$user);
$repeat_coount=repeat_count($keyword,$user);
if($repeat_coount!=null)
{
if(is_numeric($user))
$row=$wpdb->get_var( "SELECT id FROM " . SS_TABLE . " WHERE keywords = '" . mysql_escape_string($keyword)."' and user='".mysql_escape_string($user)."'");
else
$row=$wpdb->get_var( "SELECT id FROM " . SS_TABLE . " WHERE keywords = '" . mysql_escape_string($keyword)."'");
$wpdb->update( SS_TABLE, array(
'query_date' => $date,
'repeat_count' => ++$repeat_coount,
'search_count' => $NumResults),
array('id' => $row),
array(
'%s', '%s','%s','%s', '%s','%d','%d' ) );
}
else
{
$wpdb->insert( SS_TABLE, array(
'keywords' => $keyword,
'query_date' => $date,
'source' => $referer_info['host'],
'user' => $user,
'agent' => $_SERVER['HTTP_USER_AGENT'],
'repeat_count' => 0,
'search_count' => $NumResults), array(
'%s', '%s','%s','%s', '%s','%d','%d' ) );
}
}
/**
* Count repeat if user and keyword matched
*/
function repeat_count($keyword,$user)
{
global $wpdb;
if(is_numeric($user))
$repeat_count = $wpdb->get_var( "SELECT repeat_count FROM " . SS_TABLE . " WHERE keywords ='" . mysql_escape_string($keyword)."' and user='".mysql_escape_string($user)."'");
else
{
//echo "SELECT repeat_count FROM " . SS_TABLE . " WHERE keywords ='" . mysql_escape_string($keyword)."'";
$repeat_count = $wpdb->get_var( "SELECT repeat_count FROM " . SS_TABLE . " WHERE keywords ='" . mysql_escape_string($keyword)."'");
}
return $repeat_count;
}
/**
* Count repeat if user and keyword matched
*/
function search_count($keyword,$user)
{
global $wpdb;
if(is_numeric($user))
$search_count = $wpdb->get_var( "SELECT search_count FROM " . SS_TABLE . " WHERE keywords LIKE '" . $keyword."' and user='".$user."'");
return $search_count;
}
?>