| 1 |
<?php |
| 2 |
/** |
| 3 |
* Author: Alin Marcu |
| 4 |
* Author URI: http://deconf.com |
| 5 |
* License: GPLv2 or later |
| 6 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 7 |
*/ |
| 8 |
function file_get_contents_clicky($url) { |
| 9 |
|
| 10 |
$args = array( |
| 11 |
'timeout' => 15, |
| 12 |
'redirection' => 1, |
| 13 |
'httpversion' => '1.0', |
| 14 |
'user-agent' => 'Clicky Analytics (+http://deconf.com/clicky-analytics-dashboard-wordpress/)' |
| 15 |
); |
| 16 |
$result = wp_remote_get( $url ); |
| 17 |
if ( is_array($result) AND 200 == $result['response']['code'] ) { |
| 18 |
$data = $result['body']; |
| 19 |
} else{ |
| 20 |
$data = ''; |
| 21 |
} |
| 22 |
|
| 23 |
return $data; |
| 24 |
} |
| 25 |
|
| 26 |
function ca_tracking_code(){ |
| 27 |
global $current_user; |
| 28 |
|
| 29 |
$custom_tracking=""; |
| 30 |
|
| 31 |
get_currentuserinfo(); |
| 32 |
|
| 33 |
if ($current_user->user_login){ |
| 34 |
|
| 35 |
if ((get_option('ca_track_username')) AND ((get_option('ca_track_email')))){ |
| 36 |
|
| 37 |
$custom_tracking="<script type=\"text/javascript\"> |
| 38 |
var clicky_custom = clicky_custom || {}; |
| 39 |
clicky_custom.visitor = clicky_custom.visitor || {}; |
| 40 |
clicky_custom.visitor [\"username\"] = '".$current_user->user_login."'; |
| 41 |
clicky_custom.visitor [\"email\"] = '".$current_user->user_email."'; |
| 42 |
</script>"; |
| 43 |
|
| 44 |
} |
| 45 |
else if (get_option('ca_track_username')){ |
| 46 |
|
| 47 |
$custom_tracking="<script type=\"text/javascript\"> |
| 48 |
var clicky_custom = clicky_custom || {}; |
| 49 |
clicky_custom.visitor = clicky_custom.visitor || {}; |
| 50 |
clicky_custom.visitor [\"username\"] = '".$current_user->user_login."'; |
| 51 |
</script>"; |
| 52 |
} |
| 53 |
else if (get_option('ca_track_email')){ |
| 54 |
$custom_tracking="<script type=\"text/javascript\"> |
| 55 |
var clicky_custom = clicky_custom || {}; |
| 56 |
clicky_custom.visitor = clicky_custom.visitor || {}; |
| 57 |
clicky_custom.visitor [\"email\"] = '".$current_user->user_email."'; |
| 58 |
</script>"; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$main_tracking="<script type=\"text/javascript\"> |
| 63 |
var clicky_site_ids = clicky_site_ids || []; |
| 64 |
clicky_site_ids.push(".get_option('ca_siteid')."); |
| 65 |
(function() { |
| 66 |
var s = document.createElement('script'); |
| 67 |
s.type = 'text/javascript'; |
| 68 |
s.async = true; |
| 69 |
s.src = '//static.getclicky.com/js'; |
| 70 |
( document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0] ).appendChild( s ); |
| 71 |
})(); |
| 72 |
</script>"; |
| 73 |
$video_tracking=""; |
| 74 |
if (get_option('ca_track_youtube')){ |
| 75 |
$video_tracking.="<script src='//static.getclicky.com/inc/javascript/video/youtube.js'></script>"; |
| 76 |
} |
| 77 |
if (get_option('ca_track_html5')){ |
| 78 |
$video_tracking.='<script src="//static.getclicky.com/inc/javascript/video/html.js"></script>'; |
| 79 |
} |
| 80 |
|
| 81 |
$tracking = "\n<!-- BEGIN Clicky Analytics v".CADASH_CURRENT_VERSION." Tracking - http://deconf.com/clicky-analytics-dashboard-wordpress/ -->\n"; |
| 82 |
|
| 83 |
$tracking .= $custom_tracking.$main_tracking.$video_tracking; |
| 84 |
|
| 85 |
$tracking .= "\n<!-- END Clicky Analytics v".CADASH_CURRENT_VERSION." Tracking - http://deconf.com/clicky-analytics-dashboard-wordpress/ -->\n"; |
| 86 |
|
| 87 |
return $tracking; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
function ca_validation($item){ |
| 92 |
|
| 93 |
return addslashes ($item); |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
function ca_clear_cache(){ |
| 98 |
global $wpdb; |
| 99 |
$sqlquery=$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_clicky%%'"); |
| 100 |
$sqlquery=$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_clicky%%'"); |
| 101 |
} |
| 102 |
|
| 103 |
function ca_safe_get($key) { |
| 104 |
if (array_key_exists($key, $_POST)) { |
| 105 |
return $_POST[$key]; |
| 106 |
} |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
// Get Top Pages |
| 111 |
function ca_top_pages($api_url, $siteid, $sitekey, $from){ |
| 112 |
|
| 113 |
$metric = 'type=pages'; |
| 114 |
try{ |
| 115 |
$serial='clicky_qr3'.$from; |
| 116 |
$transient = get_transient($serial); |
| 117 |
if ( empty( $transient ) ){ |
| 118 |
$url = $api_url."site_id=".$siteid."&sitekey=".$sitekey."&".$from."&".$metric."&limit=30&output=php"; |
| 119 |
//echo $url; |
| 120 |
$result = unserialize(file_get_contents_clicky($url)); |
| 121 |
set_transient( $serial, $result, get_option('ca_cachetime') ); |
| 122 |
//echo "QR3-Refresh"; |
| 123 |
}else{ |
| 124 |
$result = $transient; |
| 125 |
//echo "QR3-Cache"; |
| 126 |
} |
| 127 |
} |
| 128 |
catch(exception $e) { |
| 129 |
return "<p>" . __ ( "ERROR LOG:", 'clicky-analytics' ) . "</p><p>" . $e . "</p>"; |
| 130 |
} |
| 131 |
$i=0; |
| 132 |
foreach( $result as $item ) { |
| 133 |
if ($item!="Invalid sitekey."){ |
| 134 |
foreach( $item as $date => $item1 ) { |
| 135 |
if (!$item1) { |
| 136 |
return; |
| 137 |
} |
| 138 |
foreach( $item1 as $item2) { |
| 139 |
$goores[$i][0]=ca_validation($item2['title']); |
| 140 |
$goores[$i][1]=ca_validation($item2['value']); |
| 141 |
$i++; |
| 142 |
|
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
$j=0; |
| 148 |
$ca_statsdata=""; |
| 149 |
for ($j=0;$j<=$i-1;$j++){ |
| 150 |
|
| 151 |
$ca_statsdata.="['".$goores[$j][0]."',".$goores[$j][1]."],"; |
| 152 |
|
| 153 |
} |
| 154 |
return wp_kses(rtrim($ca_statsdata,','), GADASH_ALLOW); |
| 155 |
} |
| 156 |
|
| 157 |
// Get Top referrers |
| 158 |
function ca_top_referrers($api_url, $siteid, $sitekey, $from){ |
| 159 |
|
| 160 |
$metric = 'type=links-domains'; |
| 161 |
try{ |
| 162 |
$serial='clicky_qr4'.$from; |
| 163 |
$transient = get_transient($serial); |
| 164 |
if ( empty( $transient ) ){ |
| 165 |
$url = $api_url."site_id=".$siteid."&sitekey=".$sitekey."&".$from."&".$metric."&limit=30&output=php"; |
| 166 |
//echo $url; |
| 167 |
$result = unserialize(file_get_contents_clicky($url)); |
| 168 |
set_transient( $serial, $result, get_option('ca_cachetime') ); |
| 169 |
//echo "QR4-Refresh"; |
| 170 |
}else{ |
| 171 |
$result = $transient; |
| 172 |
//echo "QR4-Cache"; |
| 173 |
} |
| 174 |
} |
| 175 |
catch(exception $e) { |
| 176 |
return "<p>" . __ ( "ERROR LOG:", 'clicky-analytics' ) . "</p><p>" . $e . "</p>"; |
| 177 |
} |
| 178 |
$i=0; |
| 179 |
foreach( $result as $item ) { |
| 180 |
if ($item!="Invalid sitekey."){ |
| 181 |
foreach( $item as $date => $item1 ) { |
| 182 |
if (!$item1) { |
| 183 |
return; |
| 184 |
} |
| 185 |
foreach( $item1 as $item2) { |
| 186 |
$goores[$i][0]=ca_validation($item2['title']); |
| 187 |
$goores[$i][1]=ca_validation($item2['value']); |
| 188 |
$i++; |
| 189 |
|
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
$j=0; |
| 195 |
$ca_statsdata=""; |
| 196 |
for ($j=0;$j<=$i-1;$j++){ |
| 197 |
|
| 198 |
$ca_statsdata.="['".$goores[$j][0]."',".$goores[$j][1]."],"; |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
return wp_kses(rtrim($ca_statsdata,','), GADASH_ALLOW); |
| 203 |
} |
| 204 |
// Get Top searches |
| 205 |
function ca_top_searches($api_url, $siteid, $sitekey, $from){ |
| 206 |
|
| 207 |
$metric = 'type=searches'; |
| 208 |
try{ |
| 209 |
$serial='clicky_qr5'.$from; |
| 210 |
$transient = get_transient($serial); |
| 211 |
if ( empty( $transient ) ){ |
| 212 |
$url = $api_url."site_id=".$siteid."&sitekey=".$sitekey."&".$from."&".$metric."&limit=30&output=php"; |
| 213 |
//echo $url; |
| 214 |
$result = unserialize(file_get_contents_clicky($url)); |
| 215 |
set_transient( $serial, $result, get_option('ca_cachetime') ); |
| 216 |
//echo "QR4-Refresh"; |
| 217 |
}else{ |
| 218 |
$result = $transient; |
| 219 |
//echo "QR4-Cache"; |
| 220 |
} |
| 221 |
} |
| 222 |
catch(exception $e) { |
| 223 |
return "<p>" . __ ( "ERROR LOG:", 'clicky-analytics' ) . "</p><p>" . $e . "</p>"; |
| 224 |
} |
| 225 |
$i=0; |
| 226 |
foreach( $result as $item ) { |
| 227 |
if ($item!="Invalid sitekey."){ |
| 228 |
foreach( $item as $date => $item1 ) { |
| 229 |
if (!$item1) { |
| 230 |
return; |
| 231 |
} |
| 232 |
foreach( $item1 as $item2) { |
| 233 |
$goores[$i][0]=ca_validation($item2['title']); |
| 234 |
$goores[$i][1]=ca_validation($item2['value']); |
| 235 |
$i++; |
| 236 |
|
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
$j=0; |
| 242 |
$ca_statsdata=""; |
| 243 |
for ($j=0;$j<=$i-1;$j++){ |
| 244 |
if (isset($goores[$j][1])){ |
| 245 |
$ca_statsdata.="['".$goores[$j][0]."',".$goores[$j][1]."],"; |
| 246 |
} |
| 247 |
} |
| 248 |
return wp_kses(rtrim($ca_statsdata,','), GADASH_ALLOW); |
| 249 |
} |
| 250 |
?> |