| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module Name: WordPress.com Stats |
| 4 |
* Module Description: Simple, concise site stats with no additional load on your server. |
| 5 |
* Sort Order: 1 |
| 6 |
* First Introduced: 1.1 |
| 7 |
* Requires Connection: Yes |
| 8 |
* Auto Activate: Yes |
| 9 |
* Module Tags: WordPress.com Stats |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( defined( 'STATS_VERSION' ) ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
define( 'STATS_VERSION', '9' ); |
| 17 |
defined( 'STATS_DASHBOARD_SERVER' ) or define( 'STATS_DASHBOARD_SERVER', 'dashboard.wordpress.com' ); |
| 18 |
|
| 19 |
add_action( 'jetpack_modules_loaded', 'stats_load' ); |
| 20 |
|
| 21 |
// Tell HQ about changed settings |
| 22 |
Jetpack_Sync::sync_options( __FILE__, |
| 23 |
'stats_options', |
| 24 |
'home', |
| 25 |
'siteurl', |
| 26 |
'blogname', |
| 27 |
'blogdescription', |
| 28 |
'gmt_offset', |
| 29 |
'timezone_string', |
| 30 |
'page_on_front', |
| 31 |
'permalink_structure', |
| 32 |
'category_base', |
| 33 |
'tag_base' |
| 34 |
); |
| 35 |
|
| 36 |
function stats_load() { |
| 37 |
global $wp_roles; |
| 38 |
|
| 39 |
Jetpack::enable_module_configurable( __FILE__ ); |
| 40 |
Jetpack::module_configuration_load( __FILE__, 'stats_configuration_load' ); |
| 41 |
Jetpack::module_configuration_head( __FILE__, 'stats_configuration_head' ); |
| 42 |
Jetpack::module_configuration_screen( __FILE__, 'stats_configuration_screen' ); |
| 43 |
|
| 44 |
// Tell HQ about changed posts |
| 45 |
$post_stati = get_post_stati( array( 'public' => true ) ); // All public post stati |
| 46 |
$post_stati[] = 'private'; // Content from private stati will be redacted |
| 47 |
Jetpack_Sync::sync_posts( __FILE__, array( |
| 48 |
'post_types' => get_post_types( array( 'public' => true ) ), // All public post types |
| 49 |
'post_stati' => $post_stati, |
| 50 |
) ); |
| 51 |
|
| 52 |
// Generate the tracking code after wp() has queried for posts. |
| 53 |
add_action( 'template_redirect', 'stats_template_redirect', 1 ); |
| 54 |
|
| 55 |
add_action( 'wp_head', 'stats_admin_bar_head', 100 ); |
| 56 |
|
| 57 |
add_action( 'wp_head', 'stats_hide_smile_css' ); |
| 58 |
|
| 59 |
add_action( 'jetpack_admin_menu', 'stats_admin_menu' ); |
| 60 |
|
| 61 |
add_action( 'wp_dashboard_setup', 'stats_register_dashboard_widget' ); |
| 62 |
|
| 63 |
add_filter( 'jetpack_xmlrpc_methods', 'stats_xmlrpc_methods' ); |
| 64 |
|
| 65 |
// Map stats caps |
| 66 |
add_filter( 'map_meta_cap', 'stats_map_meta_caps', 10, 4 ); |
| 67 |
|
| 68 |
add_filter( 'pre_option_db_version', 'stats_ignore_db_version' ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Prevent sparkline img requests being redirected to upgrade.php. |
| 73 |
* See wp-admin/admin.php where it checks $wp_db_version. |
| 74 |
*/ |
| 75 |
function stats_ignore_db_version( $version ) { |
| 76 |
if ( |
| 77 |
is_admin() && |
| 78 |
isset( $_GET['page'] ) && $_GET['page'] == 'stats' && |
| 79 |
isset( $_GET['chart'] ) && strpos($_GET['chart'], 'admin-bar-hours') === 0 |
| 80 |
) { |
| 81 |
global $wp_db_version; |
| 82 |
return $wp_db_version; |
| 83 |
} |
| 84 |
return $version; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Maps view_stats cap to read cap as needed |
| 89 |
* |
| 90 |
* @return array Possibly mapped capabilities for meta capability |
| 91 |
*/ |
| 92 |
function stats_map_meta_caps( $caps, $cap, $user_id, $args ) { |
| 93 |
// Map view_stats to exists |
| 94 |
if ( 'view_stats' == $cap ) { |
| 95 |
$user = new WP_User( $user_id ); |
| 96 |
$user_role = array_shift( $user->roles ); |
| 97 |
$stats_roles = stats_get_option( 'roles' ); |
| 98 |
|
| 99 |
// Is the users role in the available stats roles? |
| 100 |
if ( in_array( $user_role, $stats_roles ) ) { |
| 101 |
$caps = array( 'read' ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $caps; |
| 106 |
} |
| 107 |
|
| 108 |
function stats_template_redirect() { |
| 109 |
global $wp_the_query, $current_user, $stats_footer; |
| 110 |
|
| 111 |
if ( is_feed() || is_robots() || is_trackback() ) |
| 112 |
return; |
| 113 |
|
| 114 |
$options = stats_get_options(); |
| 115 |
|
| 116 |
// Should we be counting this user's views? |
| 117 |
if ( !empty( $current_user->ID ) ) { |
| 118 |
$count_roles = stats_get_option( 'count_roles' ); |
| 119 |
if ( ! array_intersect( $current_user->roles, $count_roles ) ) |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
add_action( 'wp_footer', 'stats_footer', 101 ); |
| 124 |
add_action( 'wp_head', 'stats_add_shutdown_action' ); |
| 125 |
|
| 126 |
$blog = Jetpack_Options::get_option( 'id' ); |
| 127 |
$tz = get_option( 'gmt_offset' ); |
| 128 |
$v = 'ext'; |
| 129 |
$j = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ); |
| 130 |
if ( $wp_the_query->is_single || $wp_the_query->is_page || $wp_the_query->is_posts_page ) { |
| 131 |
// Store and reset the queried_object and queried_object_id |
| 132 |
// Otherwise, redirect_canonical() will redirect to home_url( '/' ) for show_on_front = page sites where home_url() is not all lowercase. |
| 133 |
// Repro: |
| 134 |
// 1. Set home_url = http://ExamPle.com/ |
| 135 |
// 2. Set show_on_front = page |
| 136 |
// 3. Set page_on_front = something |
| 137 |
// 4. Visit http://example.com/ |
| 138 |
|
| 139 |
$queried_object = ( isset( $wp_the_query->queried_object ) ) ? $wp_the_query->queried_object : null; |
| 140 |
$queried_object_id = ( isset( $wp_the_query->queried_object_id ) ) ? $wp_the_query->queried_object_id : null; |
| 141 |
$post = $wp_the_query->get_queried_object_id(); |
| 142 |
$wp_the_query->queried_object = $queried_object; |
| 143 |
$wp_the_query->queried_object_id = $queried_object_id; |
| 144 |
} else { |
| 145 |
$post = '0'; |
| 146 |
} |
| 147 |
|
| 148 |
$http = is_ssl() ? 'https' : 'http'; |
| 149 |
$week = gmdate( 'YW' ); |
| 150 |
|
| 151 |
$data = stats_array( compact( 'v', 'j', 'blog', 'post', 'tz' ) ); |
| 152 |
|
| 153 |
$stats_footer = <<<END |
| 154 |
|
| 155 |
<script src="$http://stats.wordpress.com/e-$week.js" type="text/javascript"></script> |
| 156 |
<script type="text/javascript"> |
| 157 |
st_go({{$data}}); |
| 158 |
var load_cmc = function(){linktracker_init($blog,$post,2);}; |
| 159 |
if ( typeof addLoadEvent != 'undefined' ) addLoadEvent(load_cmc); |
| 160 |
else load_cmc(); |
| 161 |
</script> |
| 162 |
END; |
| 163 |
} |
| 164 |
|
| 165 |
function stats_add_shutdown_action() { |
| 166 |
// just in case wp_footer isn't in your theme |
| 167 |
add_action( 'shutdown', 'stats_footer', 101 ); |
| 168 |
} |
| 169 |
|
| 170 |
function stats_footer() { |
| 171 |
global $stats_footer; |
| 172 |
print $stats_footer; |
| 173 |
$stats_footer = ''; |
| 174 |
} |
| 175 |
|
| 176 |
function stats_get_options() { |
| 177 |
$options = get_option( 'stats_options' ); |
| 178 |
|
| 179 |
if ( !isset( $options['version'] ) || $options['version'] < STATS_VERSION ) |
| 180 |
$options = stats_upgrade_options( $options ); |
| 181 |
|
| 182 |
return $options; |
| 183 |
} |
| 184 |
|
| 185 |
function stats_get_option( $option ) { |
| 186 |
$options = stats_get_options(); |
| 187 |
|
| 188 |
if ( $option == 'blog_id' ) |
| 189 |
return Jetpack_Options::get_option( 'id' ); |
| 190 |
|
| 191 |
if ( isset( $options[$option] ) ) |
| 192 |
return $options[$option]; |
| 193 |
|
| 194 |
return null; |
| 195 |
} |
| 196 |
|
| 197 |
function stats_set_option( $option, $value ) { |
| 198 |
$options = stats_get_options(); |
| 199 |
|
| 200 |
$options[$option] = $value; |
| 201 |
|
| 202 |
stats_set_options($options); |
| 203 |
} |
| 204 |
|
| 205 |
function stats_set_options($options) { |
| 206 |
update_option( 'stats_options', $options ); |
| 207 |
} |
| 208 |
|
| 209 |
function stats_upgrade_options( $options ) { |
| 210 |
$defaults = array( |
| 211 |
'admin_bar' => true, |
| 212 |
'roles' => array( 'administrator' ), |
| 213 |
'count_roles' => array(), |
| 214 |
'blog_id' => Jetpack_Options::get_option( 'id' ), |
| 215 |
'do_not_track' => true, // @todo |
| 216 |
'hide_smile' => false, |
| 217 |
); |
| 218 |
|
| 219 |
if ( isset( $options['reg_users'] ) ) { |
| 220 |
if ( ! function_exists( 'get_editable_roles' ) ) |
| 221 |
require_once( ABSPATH . 'wp-admin/includes/user.php' ); |
| 222 |
if ( $options['reg_users'] ) |
| 223 |
$options['count_roles'] = array_keys( get_editable_roles() ); |
| 224 |
unset( $options['reg_users'] ); |
| 225 |
} |
| 226 |
|
| 227 |
if ( is_array( $options ) && !empty( $options ) ) |
| 228 |
$new_options = array_merge( $defaults, $options ); |
| 229 |
else |
| 230 |
$new_options = $defaults; |
| 231 |
|
| 232 |
$new_options['version'] = STATS_VERSION; |
| 233 |
|
| 234 |
stats_set_options( $new_options ); |
| 235 |
|
| 236 |
stats_update_blog(); |
| 237 |
|
| 238 |
return $new_options; |
| 239 |
} |
| 240 |
|
| 241 |
function stats_array( $kvs ) { |
| 242 |
$kvs = apply_filters( 'stats_array', $kvs ); |
| 243 |
$kvs = array_map( 'addslashes', $kvs ); |
| 244 |
foreach ( $kvs as $k => $v ) |
| 245 |
$jskvs[] = "$k:'$v'"; |
| 246 |
return join( ',', $jskvs ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Admin Pages |
| 251 |
*/ |
| 252 |
function stats_admin_menu() { |
| 253 |
global $pagenow; |
| 254 |
|
| 255 |
// If we're at an old Stats URL, redirect to the new one. |
| 256 |
// Don't even bother with caps, menu_page_url(), etc. Just do it. |
| 257 |
if ( 'index.php' == $pagenow && isset( $_GET['page'] ) && 'stats' == $_GET['page'] ) { |
| 258 |
$redirect_url = str_replace( array( '/wp-admin/index.php?', '/wp-admin/?' ), '/wp-admin/admin.php?', $_SERVER['REQUEST_URI'] ); |
| 259 |
$relative_pos = strpos( $redirect_url, '/wp-admin/' ); |
| 260 |
if ( false !== $relative_pos ) { |
| 261 |
wp_safe_redirect( admin_url( substr( $redirect_url, $relative_pos + 10 ) ) ); |
| 262 |
exit; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
$hook = add_submenu_page( 'jetpack', __( 'Site Stats', 'jetpack' ), __( 'Site Stats', 'jetpack' ), 'view_stats', 'stats', 'stats_reports_page' ); |
| 267 |
add_action( "load-$hook", 'stats_reports_load' ); |
| 268 |
} |
| 269 |
|
| 270 |
function stats_admin_path() { |
| 271 |
return Jetpack::module_configuration_url( __FILE__ ); |
| 272 |
} |
| 273 |
|
| 274 |
function stats_reports_load() { |
| 275 |
wp_enqueue_script( 'jquery' ); |
| 276 |
wp_enqueue_script( 'postbox' ); |
| 277 |
|
| 278 |
add_action( 'admin_print_styles', 'stats_reports_css' ); |
| 279 |
|
| 280 |
if ( isset( $_GET['nojs'] ) && $_GET['nojs'] ) { |
| 281 |
$parsed = parse_url( admin_url() ); |
| 282 |
// Remember user doesn't want JS |
| 283 |
setcookie( 'stnojs', '1', time() + 172800, $parsed['path'] ); // 2 days |
| 284 |
} |
| 285 |
|
| 286 |
if ( isset( $_COOKIE['stnojs'] ) && $_COOKIE['stnojs'] ) { |
| 287 |
// Detect if JS is on. If so, remove cookie so next page load is via JS |
| 288 |
add_action( 'admin_print_footer_scripts', 'stats_js_remove_stnojs_cookie' ); |
| 289 |
} else if ( !isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) ) { |
| 290 |
// Normal page load. Load page content via JS. |
| 291 |
add_action( 'admin_print_footer_scripts', 'stats_js_load_page_via_ajax' ); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
function stats_reports_css() { |
| 296 |
?> |
| 297 |
<style type="text/css"> |
| 298 |
#stats-loading-wrap p { |
| 299 |
text-align: center; |
| 300 |
font-size: 2em; |
| 301 |
margin: 7.5em 15px 0 0; |
| 302 |
height: 64px; |
| 303 |
line-height: 64px; |
| 304 |
} |
| 305 |
</style> |
| 306 |
<?php |
| 307 |
} |
| 308 |
|
| 309 |
// Detect if JS is on. If so, remove cookie so next page load is via JS. |
| 310 |
function stats_js_remove_stnojs_cookie() { |
| 311 |
$parsed = parse_url( admin_url() ); |
| 312 |
?> |
| 313 |
<script type="text/javascript"> |
| 314 |
/* <![CDATA[ */ |
| 315 |
document.cookie = 'stnojs=0; expires=Wed, 9 Mar 2011 16:55:50 UTC; path=<?php echo esc_js( $parsed['path'] ); ?>'; |
| 316 |
/* ]]> */ |
| 317 |
</script> |
| 318 |
<?php |
| 319 |
} |
| 320 |
|
| 321 |
// Normal page load. Load page content via JS. |
| 322 |
function stats_js_load_page_via_ajax() { |
| 323 |
?> |
| 324 |
<script type="text/javascript"> |
| 325 |
/* <![CDATA[ */ |
| 326 |
if ( -1 == document.location.href.indexOf( 'noheader' ) ) { |
| 327 |
jQuery( function( $ ) { |
| 328 |
$.get( document.location.href + '&noheader', function( responseText ) { |
| 329 |
$( '#stats-loading-wrap' ).replaceWith( responseText ); |
| 330 |
} ); |
| 331 |
} ); |
| 332 |
} |
| 333 |
/* ]]> */ |
| 334 |
</script> |
| 335 |
<?php |
| 336 |
} |
| 337 |
|
| 338 |
function stats_reports_page() { |
| 339 |
if ( isset( $_GET['dashboard'] ) ) |
| 340 |
return stats_dashboard_widget_content(); |
| 341 |
|
| 342 |
if ( !isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { |
| 343 |
$nojs_url = add_query_arg( 'nojs', '1' ); |
| 344 |
$http = is_ssl() ? 'https' : 'http'; |
| 345 |
// Loading message |
| 346 |
// No JS fallback message |
| 347 |
?> |
| 348 |
<style type="text/css"> |
| 349 |
@media only screen and (-moz-min-device-pixel-ratio: 1.5), only screen and (-o-min-device-pixel-ratio: 3/2), only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio: 1.5) { |
| 350 |
img.wpcom-loading-64 { width: 32px; height: 32px; } |
| 351 |
} |
| 352 |
</style> |
| 353 |
<div id="stats-loading-wrap" class="wrap"> |
| 354 |
<p class="hide-if-no-js"><img class="wpcom-loading-64" alt="<?php esc_attr_e( 'Loading…', 'jetpack' ); ?>" src="<?php echo esc_url( apply_filters( 'jetpack_static_url', "{$http}://en.wordpress.com/i/loading/loading-64.gif" ) ); ?>" /></p> |
| 355 |
<p class="hide-if-js"><?php esc_html_e( 'Your Site Stats work better with Javascript enabled.', 'jetpack' ); ?><br /> |
| 356 |
<a href="<?php echo esc_url( $nojs_url ); ?>"><?php esc_html_e( 'View Site Stats without Javascript', 'jetpack' ); ?></a>.</p> |
| 357 |
</div> |
| 358 |
<?php |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
$blog_id = stats_get_option( 'blog_id' ); |
| 363 |
$day = isset( $_GET['day'] ) && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $_GET['day'] ) ? $_GET['day'] : false; |
| 364 |
$q = array( |
| 365 |
'noheader' => 'true', |
| 366 |
'proxy' => '', |
| 367 |
'page' => 'stats', |
| 368 |
'day' => $day, |
| 369 |
'blog' => $blog_id, |
| 370 |
'charset' => get_option( 'blog_charset' ), |
| 371 |
'color' => get_user_option( 'admin_color' ), |
| 372 |
'ssl' => is_ssl(), |
| 373 |
'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ), |
| 374 |
); |
| 375 |
$args = array( |
| 376 |
'view' => array( 'referrers', 'postviews', 'searchterms', 'clicks', 'post', 'table' ), |
| 377 |
'numdays' => 'int', |
| 378 |
'day' => 'date', |
| 379 |
'unit' => array( 1, 7, 31, 'human' ), |
| 380 |
'humanize' => array( 'true' ), |
| 381 |
'num' => 'int', |
| 382 |
'summarize' => null, |
| 383 |
'post' => 'int', |
| 384 |
'width' => 'int', |
| 385 |
'height' => 'int', |
| 386 |
'data' => 'data', |
| 387 |
'blog_subscribers' => 'int', |
| 388 |
'comment_subscribers' => null, |
| 389 |
'type' => array( 'wpcom', 'email', 'pending' ), |
| 390 |
'pagenum' => 'int', |
| 391 |
); |
| 392 |
foreach ( $args as $var => $vals ) { |
| 393 |
if ( !isset( $_REQUEST[$var] ) ) |
| 394 |
continue; |
| 395 |
if ( is_array( $vals ) ) { |
| 396 |
if ( in_array( $_REQUEST[$var], $vals ) ) |
| 397 |
$q[$var] = $_REQUEST[$var]; |
| 398 |
} elseif ( $vals == 'int' ) { |
| 399 |
$q[$var] = intval( $_REQUEST[$var] ); |
| 400 |
} elseif ( $vals == 'date' ) { |
| 401 |
if ( preg_match( '/^\d{4}-\d{2}-\d{2}$/', $_REQUEST[$var] ) ) |
| 402 |
$q[$var] = $_REQUEST[$var]; |
| 403 |
} elseif ( $vals == null ) { |
| 404 |
$q[$var] = ''; |
| 405 |
} elseif ( $vals == 'data' ) { |
| 406 |
if ( substr( $_REQUEST[$var], 0, 9 ) == 'index.php' ) |
| 407 |
$q[$var] = $_REQUEST[$var]; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
if ( isset( $_GET['chart'] ) ) { |
| 412 |
if ( preg_match( '/^[a-z0-9-]+$/', $_GET['chart'] ) ) { |
| 413 |
$chart = sanitize_title( $_GET['chart'] ); |
| 414 |
$url = 'http://' . STATS_DASHBOARD_SERVER . "/wp-includes/charts/{$chart}.php"; |
| 415 |
} |
| 416 |
} else { |
| 417 |
$url = 'http://' . STATS_DASHBOARD_SERVER . "/wp-admin/index.php"; |
| 418 |
} |
| 419 |
|
| 420 |
$url = add_query_arg( $q, $url ); |
| 421 |
$method = 'GET'; |
| 422 |
$timeout = 90; |
| 423 |
$user_id = JETPACK_MASTER_USER; // means send the wp.com user_id |
| 424 |
|
| 425 |
$get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); |
| 426 |
$get_code = wp_remote_retrieve_response_code( $get ); |
| 427 |
if ( is_wp_error( $get ) || ( 2 != intval( $get_code / 100 ) && 304 != $get_code ) || empty( $get['body'] ) ) { |
| 428 |
stats_print_wp_remote_error( $get, $url ); |
| 429 |
} else { |
| 430 |
if ( !empty( $get['headers']['content-type'] ) ) { |
| 431 |
$type = $get['headers']['content-type']; |
| 432 |
if ( substr( $type, 0, 5 ) == 'image' ) { |
| 433 |
$img = $get['body']; |
| 434 |
header( 'Content-Type: ' . $type ); |
| 435 |
header( 'Content-Length: ' . strlen( $img ) ); |
| 436 |
echo $img; |
| 437 |
die(); |
| 438 |
} |
| 439 |
} |
| 440 |
$body = stats_convert_post_titles( $get['body'] ); |
| 441 |
$body = stats_convert_chart_urls( $body ); |
| 442 |
$body = stats_convert_image_urls( $body ); |
| 443 |
$body = stats_convert_admin_urls( $body ); |
| 444 |
echo $body; |
| 445 |
} |
| 446 |
if ( isset( $_GET['noheader'] ) ) |
| 447 |
die; |
| 448 |
} |
| 449 |
|
| 450 |
function stats_convert_admin_urls( $html ) { |
| 451 |
return str_replace( 'index.php?page=stats', 'admin.php?page=stats', $html ); |
| 452 |
} |
| 453 |
|
| 454 |
function stats_convert_image_urls( $html ) { |
| 455 |
$url = ( is_ssl() ? 'https' : 'http' ) . '://' . STATS_DASHBOARD_SERVER; |
| 456 |
$html = preg_replace( '|(["\'])(/i/stats.+)\\1|', '$1' . $url . '$2$1', $html ); |
| 457 |
return $html; |
| 458 |
} |
| 459 |
|
| 460 |
function stats_convert_chart_urls( $html ) { |
| 461 |
$html = preg_replace_callback( '|https?://[-.a-z0-9]+/wp-includes/charts/([-.a-z0-9]+).php(\??)|', |
| 462 |
create_function( |
| 463 |
'$matches', |
| 464 |
// If there is a query string, change the beginning '?' to a '&' so it fits into the middle of this query string |
| 465 |
'return "admin.php?page=stats&noheader&chart=" . $matches[1] . str_replace( "?", "&", $matches[2] );' |
| 466 |
), |
| 467 |
$html ); |
| 468 |
return $html; |
| 469 |
} |
| 470 |
|
| 471 |
function stats_convert_post_titles( $html ) { |
| 472 |
global $wpdb, $stats_posts; |
| 473 |
$pattern = "<span class='post-(\d+)-link'>.*?</span>"; |
| 474 |
if ( !preg_match_all( "!$pattern!", $html, $matches ) ) |
| 475 |
return $html; |
| 476 |
$posts = get_posts( array( |
| 477 |
'include' => implode( ',', $matches[1] ), |
| 478 |
'post_type' => 'any', |
| 479 |
'post_status' => 'any', |
| 480 |
'numberposts' => -1, |
| 481 |
)); |
| 482 |
foreach ( $posts as $post ) |
| 483 |
$stats_posts[$post->ID] = $post; |
| 484 |
$html = preg_replace_callback( "!$pattern!", 'stats_convert_post_title', $html ); |
| 485 |
return $html; |
| 486 |
} |
| 487 |
|
| 488 |
function stats_convert_post_title( $matches ) { |
| 489 |
global $stats_posts; |
| 490 |
$post_id = $matches[1]; |
| 491 |
if ( isset( $stats_posts[$post_id] ) ) |
| 492 |
return '<a href="' . get_permalink( $post_id ) . '" target="_blank">' . get_the_title( $post_id ) . '</a>'; |
| 493 |
return $matches[0]; |
| 494 |
} |
| 495 |
|
| 496 |
function stats_configuration_load() { |
| 497 |
if ( isset( $_POST['action'] ) && $_POST['action'] == 'save_options' && $_POST['_wpnonce'] == wp_create_nonce( 'stats' ) ) { |
| 498 |
$options = stats_get_options(); |
| 499 |
$options['admin_bar'] = isset( $_POST['admin_bar'] ) && $_POST['admin_bar']; |
| 500 |
$options['hide_smile'] = isset( $_POST['hide_smile'] ) && $_POST['hide_smile']; |
| 501 |
|
| 502 |
$options['roles'] = array( 'administrator' ); |
| 503 |
foreach ( get_editable_roles() as $role => $details ) |
| 504 |
if ( isset( $_POST["role_$role"] ) && $_POST["role_$role"] ) |
| 505 |
$options['roles'][] = $role; |
| 506 |
|
| 507 |
$options['count_roles'] = array(); |
| 508 |
foreach ( get_editable_roles() as $role => $details ) |
| 509 |
if ( isset( $_POST["count_role_$role"] ) && $_POST["count_role_$role"] ) |
| 510 |
$options['count_roles'][] = $role; |
| 511 |
|
| 512 |
stats_set_options( $options ); |
| 513 |
stats_update_blog(); |
| 514 |
Jetpack::state( 'message', 'module_configured' ); |
| 515 |
wp_safe_redirect( Jetpack::module_configuration_url( 'stats' ) ); |
| 516 |
exit; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
function stats_configuration_head() { |
| 521 |
?> |
| 522 |
<style type="text/css"> |
| 523 |
#statserror { |
| 524 |
border: 1px solid #766; |
| 525 |
background-color: #d22; |
| 526 |
padding: 1em 3em; |
| 527 |
} |
| 528 |
.stats-smiley { |
| 529 |
vertical-align: 1px; |
| 530 |
} |
| 531 |
</style> |
| 532 |
<?php |
| 533 |
} |
| 534 |
|
| 535 |
function stats_configuration_screen() { |
| 536 |
$options = stats_get_options(); |
| 537 |
?> |
| 538 |
<div class="narrow"> |
| 539 |
<p><?php printf( __( 'Visit <a href="%s">Site Stats</a> to see your stats.', 'jetpack' ), esc_url( menu_page_url( 'stats', false ) ) ); ?></p> |
| 540 |
<form method="post"> |
| 541 |
<input type='hidden' name='action' value='save_options' /> |
| 542 |
<?php wp_nonce_field( 'stats' ); ?> |
| 543 |
<table id="menu" class="form-table"> |
| 544 |
<tr valign="top"><th scope="row"><label for="admin_bar"><?php _e( 'Admin bar' , 'jetpack' ); ?></label></th> |
| 545 |
<td><label><input type='checkbox'<?php checked( $options['admin_bar'] ); ?> name='admin_bar' id='admin_bar' /> <?php _e( "Put a chart showing 48 hours of views in the admin bar.", 'jetpack' ); ?></label></td></tr> |
| 546 |
<tr valign="top"><th scope="row"><?php _e( 'Registered users', 'jetpack' ); ?></th> |
| 547 |
<td> |
| 548 |
<?php _e( "Count the page views of registered users who are logged in.", 'jetpack' ); ?><br/> |
| 549 |
<?php |
| 550 |
$count_roles = stats_get_option( 'count_roles' ); |
| 551 |
foreach ( get_editable_roles() as $role => $details ) { |
| 552 |
?> |
| 553 |
<label><input type='checkbox' name='count_role_<?php echo $role; ?>'<?php checked( in_array( $role, $count_roles ) ); ?> /> <?php echo translate_user_role( $details['name'] ); ?></label><br/> |
| 554 |
<?php |
| 555 |
} |
| 556 |
?> |
| 557 |
</td></tr> |
| 558 |
<tr valign="top"><th scope="row"><?php _e( 'Smiley' , 'jetpack' ); ?></th> |
| 559 |
<td><label><input type='checkbox'<?php checked( isset( $options['hide_smile'] ) && $options['hide_smile'] ); ?> name='hide_smile' id='hide_smile' /> <?php _e( 'Hide the stats smiley face image.', 'jetpack' ); ?></label><br /> <span class="description"><?php _e( 'The image helps collect stats and <strong>makes the world a better place</strong> but should still work when hidden', 'jetpack' ); ?> <img class="stats-smiley" alt="<?php esc_attr_e( 'Smiley face', 'jetpack' ); ?>" src="<?php echo esc_url( plugins_url( '_inc/images/stats-smiley.gif', dirname( __FILE__ ) ) ); ?>" width="6" height="5" /></span></td></tr> |
| 560 |
<tr valign="top"><th scope="row"><?php _e( 'Report visibility' , 'jetpack' ); ?></th> |
| 561 |
<td> |
| 562 |
<?php _e( 'Select the roles that will be able to view stats reports.', 'jetpack' ); ?><br/> |
| 563 |
<?php |
| 564 |
$stats_roles = stats_get_option( 'roles' ); |
| 565 |
foreach ( get_editable_roles() as $role => $details ) { |
| 566 |
?> |
| 567 |
<label><input type='checkbox' <?php if ( $role == 'administrator' ) echo "disabled='disabled' "; ?>name='role_<?php echo $role; ?>'<?php checked( $role == 'administrator' || in_array( $role, $stats_roles ) ); ?> /> <?php echo translate_user_role( $details['name'] ); ?></label><br/> |
| 568 |
<?php |
| 569 |
} |
| 570 |
?> |
| 571 |
</td></tr> |
| 572 |
</table> |
| 573 |
<p class="submit"><input type='submit' class='button-primary' value='<?php echo esc_attr( __( 'Save configuration', 'jetpack' ) ); ?>' /></p> |
| 574 |
</form> |
| 575 |
</div> |
| 576 |
<?php |
| 577 |
} |
| 578 |
|
| 579 |
function stats_hide_smile_css() { |
| 580 |
$options = stats_get_options(); |
| 581 |
if ( isset( $options['hide_smile'] ) && $options['hide_smile'] ) { |
| 582 |
?> |
| 583 |
<style type='text/css'>img#wpstats{display:none}</style><?php |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
function stats_admin_bar_head() { |
| 588 |
if ( !stats_get_option( 'admin_bar' ) ) |
| 589 |
return; |
| 590 |
|
| 591 |
if ( !current_user_can( 'view_stats' ) ) |
| 592 |
return; |
| 593 |
|
| 594 |
if ( function_exists( 'is_admin_bar_showing' ) && !is_admin_bar_showing() ) { |
| 595 |
return; |
| 596 |
} |
| 597 |
|
| 598 |
add_action( 'admin_bar_menu', 'stats_admin_bar_menu', 100 ); |
| 599 |
?> |
| 600 |
|
| 601 |
<style type='text/css'> |
| 602 |
#wpadminbar .quicklinks li#wp-admin-bar-stats { |
| 603 |
height: 28px; |
| 604 |
} |
| 605 |
#wpadminbar .quicklinks li#wp-admin-bar-stats a { |
| 606 |
height: 28px; |
| 607 |
padding: 0; |
| 608 |
} |
| 609 |
#wpadminbar .quicklinks li#wp-admin-bar-stats a div { |
| 610 |
height: 28px; |
| 611 |
width: 95px; |
| 612 |
overflow: hidden; |
| 613 |
margin: 0 10px; |
| 614 |
} |
| 615 |
#wpadminbar .quicklinks li#wp-admin-bar-stats a:hover div { |
| 616 |
width: auto; |
| 617 |
margin: 0 8px 0 10px; |
| 618 |
} |
| 619 |
#wpadminbar .quicklinks li#wp-admin-bar-stats a img { |
| 620 |
height: 24px; |
| 621 |
padding: 2px 0; |
| 622 |
max-width: none; |
| 623 |
border: none; |
| 624 |
} |
| 625 |
</style> |
| 626 |
<?php |
| 627 |
} |
| 628 |
|
| 629 |
function stats_admin_bar_menu( &$wp_admin_bar ) { |
| 630 |
$blog_id = stats_get_option( 'blog_id' ); |
| 631 |
|
| 632 |
$url = add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) ); // no menu_page_url() blog-side. |
| 633 |
|
| 634 |
$img_src = esc_attr( add_query_arg( array( 'noheader'=>'', 'proxy'=>'', 'chart'=>'admin-bar-hours-scale' ), $url ) ); |
| 635 |
$img_src_2x = esc_attr( add_query_arg( array( 'noheader'=>'', 'proxy'=>'', 'chart'=>'admin-bar-hours-scale-2x' ), $url ) ); |
| 636 |
|
| 637 |
$alt = esc_attr( __( 'Stats', 'jetpack' ) ); |
| 638 |
|
| 639 |
$title = esc_attr( __( 'Views over 48 hours. Click for more Site Stats.', 'jetpack' ) ); |
| 640 |
|
| 641 |
$menu = array( 'id' => 'stats', 'title' => "<div><script type='text/javascript'>var src;if(typeof(window.devicePixelRatio)=='undefined'||window.devicePixelRatio<2){src='$img_src';}else{src='$img_src_2x';}document.write('<img src=\''+src+'\' alt=\'$alt\' title=\'$title\' />');</script></div>", 'href' => $url ); |
| 642 |
|
| 643 |
$wp_admin_bar->add_menu( $menu ); |
| 644 |
} |
| 645 |
|
| 646 |
function stats_update_blog() { |
| 647 |
Jetpack::xmlrpc_async_call( 'jetpack.updateBlog', stats_get_blog() ); |
| 648 |
} |
| 649 |
|
| 650 |
function stats_get_blog() { |
| 651 |
$home = parse_url( trailingslashit( get_option( 'home' ) ) ); |
| 652 |
$blog = array( |
| 653 |
'host' => $home['host'], |
| 654 |
'path' => $home['path'], |
| 655 |
'blogname' => get_option( 'blogname' ), |
| 656 |
'blogdescription' => get_option( 'blogdescription' ), |
| 657 |
'siteurl' => get_option( 'siteurl' ), |
| 658 |
'gmt_offset' => get_option( 'gmt_offset' ), |
| 659 |
'timezone_string' => get_option( 'timezone_string' ), |
| 660 |
'stats_version' => STATS_VERSION, |
| 661 |
'stats_api' => 'jetpack', |
| 662 |
'page_on_front' => get_option( 'page_on_front' ), |
| 663 |
'permalink_structure' => get_option( 'permalink_structure' ), |
| 664 |
'category_base' => get_option( 'category_base' ), |
| 665 |
'tag_base' => get_option( 'tag_base' ), |
| 666 |
); |
| 667 |
$blog = array_merge( stats_get_options(), $blog ); |
| 668 |
unset( $blog['roles'], $blog['blog_id'] ); |
| 669 |
return stats_esc_html_deep( $blog ); |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
* Modified from stripslashes_deep() |
| 674 |
*/ |
| 675 |
function stats_esc_html_deep( $value ) { |
| 676 |
if ( is_array( $value ) ) { |
| 677 |
$value = array_map( 'stats_esc_html_deep', $value ); |
| 678 |
} elseif ( is_object( $value ) ) { |
| 679 |
$vars = get_object_vars( $value ); |
| 680 |
foreach ( $vars as $key => $data ) { |
| 681 |
$value->{$key} = stats_esc_html_deep( $data ); |
| 682 |
} |
| 683 |
} elseif ( is_string( $value ) ) { |
| 684 |
$value = esc_html( $value ); |
| 685 |
} |
| 686 |
|
| 687 |
return $value; |
| 688 |
} |
| 689 |
|
| 690 |
function stats_xmlrpc_methods( $methods ) { |
| 691 |
$my_methods = array( |
| 692 |
'jetpack.getBlog' => 'stats_get_blog', |
| 693 |
); |
| 694 |
|
| 695 |
return array_merge( $methods, $my_methods ); |
| 696 |
} |
| 697 |
|
| 698 |
function stats_register_dashboard_widget() { |
| 699 |
if ( ! current_user_can( 'view_stats' ) ) |
| 700 |
return; |
| 701 |
|
| 702 |
// wp_dashboard_empty: we load in the content after the page load via JS |
| 703 |
wp_add_dashboard_widget( 'dashboard_stats', __( 'Site Stats', 'jetpack' ), 'wp_dashboard_empty', 'stats_dashboard_widget_control' ); |
| 704 |
|
| 705 |
add_action( 'admin_head', 'stats_dashboard_head' ); |
| 706 |
} |
| 707 |
|
| 708 |
function stats_dashboard_widget_options() { |
| 709 |
$defaults = array( 'chart' => 1, 'top' => 1, 'search' => 7 ); |
| 710 |
if ( ( !$options = get_option( 'stats_dashboard_widget' ) ) || !is_array( $options ) ) |
| 711 |
$options = array(); |
| 712 |
|
| 713 |
// Ignore obsolete option values |
| 714 |
$intervals = array( 1, 7, 31, 90, 365 ); |
| 715 |
foreach ( array( 'top', 'search' ) as $key ) |
| 716 |
if ( isset( $options[$key] ) && !in_array( $options[$key], $intervals ) ) |
| 717 |
unset( $options[$key] ); |
| 718 |
|
| 719 |
return array_merge( $defaults, $options ); |
| 720 |
} |
| 721 |
|
| 722 |
function stats_dashboard_widget_control() { |
| 723 |
$periods = array( |
| 724 |
'1' => __( 'day', 'jetpack' ), |
| 725 |
'7' => __( 'week', 'jetpack' ), |
| 726 |
'31' => __( 'month', 'jetpack' ), |
| 727 |
); |
| 728 |
$intervals = array( |
| 729 |
'1' => __( 'the past day', 'jetpack' ), |
| 730 |
'7' => __( 'the past week', 'jetpack' ), |
| 731 |
'31' => __( 'the past month', 'jetpack' ), |
| 732 |
'90' => __( 'the past quarter', 'jetpack' ), |
| 733 |
'365' => __( 'the past year', 'jetpack' ), |
| 734 |
); |
| 735 |
$defaults = array( |
| 736 |
'top' => 1, |
| 737 |
'search' => 7, |
| 738 |
); |
| 739 |
|
| 740 |
$options = stats_dashboard_widget_options(); |
| 741 |
|
| 742 |
if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['widget_id'] ) && 'dashboard_stats' == $_POST['widget_id'] ) { |
| 743 |
if ( isset( $periods[ $_POST['chart'] ] ) ) |
| 744 |
$options['chart'] = $_POST['chart']; |
| 745 |
foreach ( array( 'top', 'search' ) as $key ) { |
| 746 |
if ( isset( $intervals[ $_POST[$key] ] ) ) |
| 747 |
$options[$key] = $_POST[$key]; |
| 748 |
else |
| 749 |
$options[$key] = $defaults[$key]; |
| 750 |
} |
| 751 |
update_option( 'stats_dashboard_widget', $options ); |
| 752 |
} |
| 753 |
?> |
| 754 |
<p> |
| 755 |
<label for="chart"><?php _e( 'Chart stats by' , 'jetpack' ); ?></label> |
| 756 |
<select id="chart" name="chart"> |
| 757 |
<?php |
| 758 |
foreach ( $periods as $val => $label ) { |
| 759 |
?> |
| 760 |
<option value="<?php echo $val; ?>"<?php selected( $val, $options['chart'] ); ?>><?php echo esc_html( $label ); ?></option> |
| 761 |
<?php |
| 762 |
} |
| 763 |
?> |
| 764 |
</select>. |
| 765 |
</p> |
| 766 |
|
| 767 |
<p> |
| 768 |
<label for="top"><?php _e( 'Show top posts over', 'jetpack' ); ?></label> |
| 769 |
<select id="top" name="top"> |
| 770 |
<?php |
| 771 |
foreach ( $intervals as $val => $label ) { |
| 772 |
?> |
| 773 |
<option value="<?php echo $val; ?>"<?php selected( $val, $options['top'] ); ?>><?php echo esc_html( $label ); ?></option> |
| 774 |
<?php |
| 775 |
} |
| 776 |
?> |
| 777 |
</select>. |
| 778 |
</p> |
| 779 |
|
| 780 |
<p> |
| 781 |
<label for="search"><?php _e( 'Show top search terms over', 'jetpack' ); ?></label> |
| 782 |
<select id="search" name="search"> |
| 783 |
<?php |
| 784 |
foreach ( $intervals as $val => $label ) { |
| 785 |
?> |
| 786 |
<option value="<?php echo $val; ?>"<?php selected( $val, $options['search'] ); ?>><?php echo esc_html( $label ); ?></option> |
| 787 |
<?php |
| 788 |
} |
| 789 |
?> |
| 790 |
</select>. |
| 791 |
</p> |
| 792 |
<?php |
| 793 |
} |
| 794 |
|
| 795 |
// Javascript and CSS for dashboard widget |
| 796 |
function stats_dashboard_head() { ?> |
| 797 |
<script type="text/javascript"> |
| 798 |
/* <![CDATA[ */ |
| 799 |
jQuery(window).load( function() { |
| 800 |
jQuery( function($) { |
| 801 |
resizeChart(); |
| 802 |
jQuery(window).resize( _.debounce( function(){ |
| 803 |
resizeChart(); |
| 804 |
}, 100) ); |
| 805 |
} ); |
| 806 |
|
| 807 |
function resizeChart() { |
| 808 |
var dashStats = jQuery( '#dashboard_stats.postbox div.inside' ); |
| 809 |
|
| 810 |
if ( dashStats.find( '.dashboard-widget-control-form' ).length ) { |
| 811 |
return; |
| 812 |
} |
| 813 |
|
| 814 |
if ( ! dashStats.length ) { |
| 815 |
dashStats = jQuery( '#dashboard_stats div.dashboard-widget-content' ); |
| 816 |
var h = parseInt( dashStats.parent().height() ) - parseInt( dashStats.prev().height() ); |
| 817 |
var args = 'width=' + dashStats.width() + '&height=' + h.toString(); |
| 818 |
} else { |
| 819 |
var args = 'width=' + ( dashStats.prev().width() * 2 ).toString(); |
| 820 |
} |
| 821 |
|
| 822 |
dashStats.not( '.dashboard-widget-control' ).load( 'admin.php?page=stats&noheader&dashboard&' + args ); |
| 823 |
} |
| 824 |
} ); |
| 825 |
/* ]]> */ |
| 826 |
</script> |
| 827 |
<style type="text/css"> |
| 828 |
/* <![CDATA[ */ |
| 829 |
#stat-chart { |
| 830 |
background: none !important; |
| 831 |
} |
| 832 |
#dashboard_stats .inside { |
| 833 |
margin: 10px 0 0 0 !important; |
| 834 |
} |
| 835 |
#dashboard_stats #stats-graph { |
| 836 |
margin: 0; |
| 837 |
} |
| 838 |
#stats-info { |
| 839 |
border-top: 1px solid #dfdfdf; |
| 840 |
margin: 7px -10px 0 -10px; |
| 841 |
padding: 10px; |
| 842 |
background: #fcfcfc; |
| 843 |
-moz-box-shadow:inset 0 1px 0 #fff; |
| 844 |
-webkit-box-shadow:inset 0 1px 0 #fff; |
| 845 |
box-shadow:inset 0 1px 0 #fff; |
| 846 |
overflow: hidden; |
| 847 |
border-radius: 0 0 2px 2px; |
| 848 |
-webkit-border-radius: 0 0 2px 2px; |
| 849 |
-moz-border-radius: 0 0 2px 2px; |
| 850 |
-khtml-border-radius: 0 0 2px 2px; |
| 851 |
} |
| 852 |
#stats-info #top-posts, #stats-info #top-search { |
| 853 |
float: left; |
| 854 |
width: 50%; |
| 855 |
} |
| 856 |
#top-posts .stats-section-inner p { |
| 857 |
white-space: nowrap; |
| 858 |
overflow: hidden; |
| 859 |
} |
| 860 |
#top-posts .stats-section-inner p a { |
| 861 |
overflow: hidden; |
| 862 |
text-overflow: ellipsis; |
| 863 |
} |
| 864 |
#stats-info div#active { |
| 865 |
border-top: 1px solid #dfdfdf; |
| 866 |
margin: 0 -10px; |
| 867 |
padding: 10px 10px 0 10px; |
| 868 |
-moz-box-shadow:inset 0 1px 0 #fff; |
| 869 |
-webkit-box-shadow:inset 0 1px 0 #fff; |
| 870 |
box-shadow:inset 0 1px 0 #fff; |
| 871 |
overflow: hidden; |
| 872 |
} |
| 873 |
#top-search p { |
| 874 |
color: #999; |
| 875 |
} |
| 876 |
#stats-info h4 { |
| 877 |
font-size: 1em; |
| 878 |
margin: 0 0 .5em 0 !important; |
| 879 |
} |
| 880 |
#stats-info p { |
| 881 |
margin: 0 0 .25em; |
| 882 |
color: #999; |
| 883 |
} |
| 884 |
#stats-info p.widget-loading { |
| 885 |
margin: 1em 0 0; |
| 886 |
color: #333; |
| 887 |
} |
| 888 |
#stats-info p a { |
| 889 |
display: block; |
| 890 |
} |
| 891 |
#stats-info p a.button { |
| 892 |
display: inline; |
| 893 |
} |
| 894 |
/* ]]> */ |
| 895 |
</style> |
| 896 |
<?php |
| 897 |
} |
| 898 |
|
| 899 |
function stats_dashboard_widget_content() { |
| 900 |
if ( !isset( $_GET['width'] ) || ( !$width = (int) ( $_GET['width'] / 2 ) ) || $width < 250 ) |
| 901 |
$width = 370; |
| 902 |
if ( !isset( $_GET['height'] ) || ( !$height = (int) $_GET['height'] - 36 ) || $height < 230 ) |
| 903 |
$height = 180; |
| 904 |
|
| 905 |
$_width = $width - 5; |
| 906 |
$_height = $height - ( $GLOBALS['is_winIE'] ? 16 : 5 ); // hack! |
| 907 |
|
| 908 |
$options = stats_dashboard_widget_options(); |
| 909 |
$blog_id = Jetpack_Options::get_option( 'id' ); |
| 910 |
|
| 911 |
$q = array( |
| 912 |
'noheader' => 'true', |
| 913 |
'proxy' => '', |
| 914 |
'blog' => $blog_id, |
| 915 |
'page' => 'stats', |
| 916 |
'chart' => '', |
| 917 |
'unit' => $options['chart'], |
| 918 |
'color' => get_user_option( 'admin_color' ), |
| 919 |
'width' => $_width, |
| 920 |
'height' => $_height, |
| 921 |
'ssl' => is_ssl(), |
| 922 |
'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ), |
| 923 |
); |
| 924 |
|
| 925 |
$url = 'http://' . STATS_DASHBOARD_SERVER . "/wp-admin/index.php"; |
| 926 |
|
| 927 |
$url = add_query_arg( $q, $url ); |
| 928 |
$method = 'GET'; |
| 929 |
$timeout = 90; |
| 930 |
$user_id = JETPACK_MASTER_USER; |
| 931 |
|
| 932 |
$get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); |
| 933 |
$get_code = wp_remote_retrieve_response_code( $get ); |
| 934 |
if ( is_wp_error( $get ) || ( 2 != intval( $get_code / 100 ) && 304 != $get_code ) || empty( $get['body'] ) ) { |
| 935 |
stats_print_wp_remote_error( $get, $url ); |
| 936 |
} else { |
| 937 |
$body = stats_convert_post_titles($get['body']); |
| 938 |
$body = stats_convert_chart_urls($body); |
| 939 |
$body = stats_convert_image_urls($body); |
| 940 |
echo $body; |
| 941 |
} |
| 942 |
|
| 943 |
$post_ids = array(); |
| 944 |
|
| 945 |
$csv_args = array( 'top' => '&limit=8', 'search' => '&limit=5' ); |
| 946 |
/* translators: Stats dashboard widget postviews list: "$post_title $views Views" */ |
| 947 |
$printf = __( '%1$s %2$s Views' , 'jetpack' ); |
| 948 |
|
| 949 |
foreach ( $top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" ) as $i => $post ) { |
| 950 |
if ( $post['post_id'] == 0 ) { |
| 951 |
unset( $top_posts[$i] ); |
| 952 |
continue; |
| 953 |
} |
| 954 |
$post_ids[] = $post['post_id']; |
| 955 |
} |
| 956 |
|
| 957 |
// cache |
| 958 |
get_posts( array( 'include' => join( ',', array_unique( $post_ids ) ) ) ); |
| 959 |
|
| 960 |
$searches = array(); |
| 961 |
foreach ( $search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" ) as $search_term ) { |
| 962 |
if ( $search_term['searchterm'] == 'encrypted_search_terms' ) |
| 963 |
continue; |
| 964 |
$searches[] = esc_html( $search_term['searchterm'] ); |
| 965 |
} |
| 966 |
|
| 967 |
?> |
| 968 |
<a class="button" href="admin.php?page=stats"><?php _e( 'View All', 'jetpack' ); ?></a> |
| 969 |
<div id="stats-info"> |
| 970 |
<div id="top-posts" class='stats-section'> |
| 971 |
<div class="stats-section-inner"> |
| 972 |
<h4 class="heading"><?php _e( 'Top Posts' , 'jetpack' ); ?></h4> |
| 973 |
<?php |
| 974 |
if ( empty( $top_posts ) ) { |
| 975 |
?> |
| 976 |
<p class="nothing"><?php _e( 'Sorry, nothing to report.', 'jetpack' ); ?></p> |
| 977 |
<?php |
| 978 |
} else { |
| 979 |
foreach ( $top_posts as $post ) { |
| 980 |
if ( !get_post( $post['post_id'] ) ) |
| 981 |
continue; |
| 982 |
?> |
| 983 |
<p><?php printf( |
| 984 |
$printf, |
| 985 |
'<a href="' . get_permalink( $post['post_id'] ) . '">' . get_the_title( $post['post_id'] ) . '</a>', |
| 986 |
number_format_i18n( $post['views'] ) |
| 987 |
); ?></p> |
| 988 |
<?php |
| 989 |
} |
| 990 |
} |
| 991 |
?> |
| 992 |
</div> |
| 993 |
</div> |
| 994 |
<div id="top-search" class='stats-section'> |
| 995 |
<div class="stats-section-inner"> |
| 996 |
<h4 class="heading"><?php _e( 'Top Searches' , 'jetpack' ); ?></h4> |
| 997 |
<?php |
| 998 |
if ( empty( $searches ) ) { |
| 999 |
?> |
| 1000 |
<p class="nothing"><?php _e( 'Sorry, nothing to report.', 'jetpack' ); ?></p> |
| 1001 |
<?php |
| 1002 |
} else { |
| 1003 |
?> |
| 1004 |
<p><?php echo join( ', ', $searches );?></p> |
| 1005 |
<?php |
| 1006 |
} |
| 1007 |
?> |
| 1008 |
</div> |
| 1009 |
</div> |
| 1010 |
</div> |
| 1011 |
<div class="clear"></div> |
| 1012 |
<?php |
| 1013 |
exit; |
| 1014 |
} |
| 1015 |
|
| 1016 |
function stats_print_wp_remote_error( $get, $url ) { |
| 1017 |
$state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 ); |
| 1018 |
$previous_error = Jetpack::state( $state_name ); |
| 1019 |
$error = md5( serialize( compact( 'get', 'url' ) ) ); |
| 1020 |
Jetpack::state( $state_name, $error ); |
| 1021 |
if ( $error !== $previous_error ) { |
| 1022 |
?> |
| 1023 |
<div class="wrap"> |
| 1024 |
<p><?php _e( 'We were unable to get your stats just now. Please reload this page to try again.', 'jetpack' ); ?></p> |
| 1025 |
</div> |
| 1026 |
<?php |
| 1027 |
return; |
| 1028 |
} |
| 1029 |
?> |
| 1030 |
<div class="wrap"> |
| 1031 |
<p><?php printf( __( 'We were unable to get your stats just now. Please reload this page to try again. If this error persists, please <a href="%1$s">contact support</a>. In your report please include the information below.', 'jetpack' ), 'http://support.wordpress.com/contact/?jetpack=needs-service' ); ?></p> |
| 1032 |
<pre> |
| 1033 |
User Agent: "<?php echo esc_html( $_SERVER['HTTP_USER_AGENT'] ); ?>" |
| 1034 |
Page URL: "http<?php echo (is_ssl()?'s':'') . '://' . esc_html( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); ?>" |
| 1035 |
API URL: "<?php echo clean_url( $url ); ?>" |
| 1036 |
<?php |
| 1037 |
if ( is_wp_error( $get ) ) { |
| 1038 |
foreach ( $get->get_error_codes() as $code ) { |
| 1039 |
foreach ( $get->get_error_messages($code) as $message ) { |
| 1040 |
?> |
| 1041 |
<?php print $code . ': "' . $message . '"' ?> |
| 1042 |
|
| 1043 |
<?php |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} else { |
| 1047 |
$get_code = wp_remote_retrieve_response_code( $get ); |
| 1048 |
$content_length = strlen( wp_remote_retrieve_body( $get ) ); |
| 1049 |
?> |
| 1050 |
Response code: "<?php print $get_code ?>" |
| 1051 |
Content length: "<?php print $content_length ?>" |
| 1052 |
|
| 1053 |
<?php |
| 1054 |
} |
| 1055 |
?></pre> |
| 1056 |
</div> |
| 1057 |
<?php |
| 1058 |
} |
| 1059 |
|
| 1060 |
function stats_get_csv( $table, $args = null ) { |
| 1061 |
$defaults = array( 'end' => false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '' ); |
| 1062 |
|
| 1063 |
$args = wp_parse_args( $args, $defaults ); |
| 1064 |
$args['table'] = $table; |
| 1065 |
$args['blog_id'] = Jetpack_Options::get_option( 'id' ); |
| 1066 |
|
| 1067 |
$stats_csv_url = add_query_arg( $args, 'http://stats.wordpress.com/csv.php' ); |
| 1068 |
|
| 1069 |
$key = md5( $stats_csv_url ); |
| 1070 |
|
| 1071 |
// Get cache |
| 1072 |
$stats_cache = get_option( 'stats_cache' ); |
| 1073 |
if ( !$stats_cache || !is_array( $stats_cache ) ) |
| 1074 |
$stats_cache = array(); |
| 1075 |
|
| 1076 |
// Return or expire this key |
| 1077 |
if ( isset( $stats_cache[$key] ) ) { |
| 1078 |
$time = key( $stats_cache[$key] ); |
| 1079 |
if ( time() - $time < 300 ) |
| 1080 |
return $stats_cache[$key][$time]; |
| 1081 |
unset( $stats_cache[$key] ); |
| 1082 |
} |
| 1083 |
|
| 1084 |
$stats_rows = array(); |
| 1085 |
do { |
| 1086 |
if ( !$stats = stats_get_remote_csv( $stats_csv_url ) ) |
| 1087 |
break; |
| 1088 |
|
| 1089 |
$labels = array_shift( $stats ); |
| 1090 |
|
| 1091 |
if ( 0 === stripos( $labels[0], 'error' ) ) |
| 1092 |
break; |
| 1093 |
|
| 1094 |
$stats_rows = array(); |
| 1095 |
for ( $s = 0; isset( $stats[$s] ); $s++ ) { |
| 1096 |
$row = array(); |
| 1097 |
foreach ( $labels as $col => $label ) |
| 1098 |
$row[$label] = $stats[$s][$col]; |
| 1099 |
$stats_rows[] = $row; |
| 1100 |
} |
| 1101 |
} while( 0 ); |
| 1102 |
|
| 1103 |
// Expire old keys |
| 1104 |
foreach ( $stats_cache as $k => $cache ) |
| 1105 |
if ( !is_array( $cache ) || 300 < time() - key($cache) ) |
| 1106 |
unset( $stats_cache[$k] ); |
| 1107 |
|
| 1108 |
// Set cache |
| 1109 |
$stats_cache[$key] = array( time() => $stats_rows ); |
| 1110 |
update_option( 'stats_cache', $stats_cache ); |
| 1111 |
|
| 1112 |
return $stats_rows; |
| 1113 |
} |
| 1114 |
|
| 1115 |
function stats_get_remote_csv( $url ) { |
| 1116 |
$method = 'GET'; |
| 1117 |
$timeout = 90; |
| 1118 |
$user_id = JETPACK_MASTER_USER; |
| 1119 |
|
| 1120 |
$get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); |
| 1121 |
$get_code = wp_remote_retrieve_response_code( $get ); |
| 1122 |
if ( is_wp_error( $get ) || ( 2 != intval( $get_code / 100 ) && 304 != $get_code ) || empty( $get['body'] ) ) { |
| 1123 |
return array(); // @todo: return an error? |
| 1124 |
} else { |
| 1125 |
return stats_str_getcsv( $get['body'] ); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
// rather than parsing the csv and its special cases, we create a new file and do fgetcsv on it. |
| 1130 |
function stats_str_getcsv( $csv ) { |
| 1131 |
if ( !$temp = tmpfile() ) // tmpfile() automatically unlinks |
| 1132 |
return false; |
| 1133 |
|
| 1134 |
$data = array(); |
| 1135 |
|
| 1136 |
fwrite( $temp, $csv, strlen( $csv ) ); |
| 1137 |
fseek( $temp, 0 ); |
| 1138 |
while ( false !== $row = fgetcsv( $temp, 2000 ) ) |
| 1139 |
$data[] = $row; |
| 1140 |
fclose( $temp ); |
| 1141 |
|
| 1142 |
return $data; |
| 1143 |
} |
| 1144 |
|
| 1145 |
|