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