diff options
Diffstat (limited to 'plugins/jetpack/modules/stats.php')
-rw-r--r-- | plugins/jetpack/modules/stats.php | 172 |
1 files changed, 94 insertions, 78 deletions
diff --git a/plugins/jetpack/modules/stats.php b/plugins/jetpack/modules/stats.php index 32c6cf2c..260b3f58 100644 --- a/plugins/jetpack/modules/stats.php +++ b/plugins/jetpack/modules/stats.php @@ -10,11 +10,26 @@ if ( defined( 'STATS_VERSION' ) ) { return; } -define( 'STATS_VERSION', '7' ); +define( 'STATS_VERSION', '8' ); defined( 'STATS_DASHBOARD_SERVER' ) or define( 'STATS_DASHBOARD_SERVER', 'dashboard.wordpress.com' ); add_action( 'jetpack_modules_loaded', 'stats_load' ); +// Tell HQ about changed settings +Jetpack_Sync::sync_options( __FILE__, + 'stats_options', + 'home', + 'siteurl', + 'blogname', + 'blogdescription', + 'gmt_offset', + 'timezone_string', + 'page_on_front', + 'permalink_structure', + 'category_base', + 'tag_base' +); + function stats_load() { global $wp_roles; @@ -23,6 +38,14 @@ function stats_load() { Jetpack::module_configuration_head( __FILE__, 'stats_configuration_head' ); Jetpack::module_configuration_screen( __FILE__, 'stats_configuration_screen' ); + // Tell HQ about changed posts + $post_stati = get_post_stati( array( 'public' => true ) ); // All public post stati + $post_stati[] = 'private'; // Content from private stati will be redacted + Jetpack_Sync::sync_posts( __FILE__, array( + 'post_types' => get_post_types( array( 'public' => true ) ), // All public post types + 'post_stati' => $post_stati, + ) ); + // Generate the tracking code after wp() has queried for posts. add_action( 'template_redirect', 'stats_template_redirect', 1 ); @@ -32,22 +55,6 @@ function stats_load() { add_action( 'wp_dashboard_setup', 'stats_register_dashboard_widget' ); - // Tell HQ about changed settings - add_action( 'update_option_home', 'stats_update_blog' ); - add_action( 'update_option_siteurl', 'stats_update_blog' ); - add_action( 'update_option_blogname', 'stats_update_blog' ); - add_action( 'update_option_blogdescription', 'stats_update_blog' ); - add_action( 'update_option_timezone_string', 'stats_update_blog' ); - add_action( 'add_option_timezone_string', 'stats_update_blog' ); - add_action( 'update_option_gmt_offset', 'stats_update_blog' ); - add_action( 'update_option_page_on_front', 'stats_update_blog' ); - add_action( 'update_option_permalink_structure', 'stats_update_blog' ); - add_action( 'update_option_category_base', 'stats_update_blog' ); - add_action( 'update_option_tag_base', 'stats_update_blog' ); - - // Tell HQ about changed posts - add_action( 'save_post', 'stats_update_post', 10, 1 ); - add_filter( 'jetpack_xmlrpc_methods', 'stats_xmlrpc_methods' ); // Map stats caps @@ -111,6 +118,7 @@ function stats_template_redirect() { add_action( 'wp_head', 'stats_add_shutdown_action' ); $blog = Jetpack::get_option( 'id' ); + $tz = get_option( 'gmt_offset' ); $v = 'ext'; $j = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ); if ( $wp_the_query->is_single || $wp_the_query->is_page || $wp_the_query->is_posts_page ) { @@ -134,7 +142,7 @@ function stats_template_redirect() { $http = is_ssl() ? 'https' : 'http'; $week = gmdate( 'YW' ); - $data = stats_array( compact( 'v', 'j', 'blog', 'post' ) ); + $data = stats_array( compact( 'v', 'j', 'blog', 'post', 'tz' ) ); $stats_footer = <<<END @@ -369,7 +377,7 @@ function stats_reports_page() { 'data' => 'data', 'blog_subscribers' => 'int', 'comment_subscribers' => null, - 'type' => array( 'email', 'pending' ), + 'type' => array( 'wpcom', 'email', 'pending' ), 'pagenum' => 'int', ); foreach ( $args as $var => $vals ) { @@ -401,24 +409,21 @@ function stats_reports_page() { $url = add_query_arg( $q, $url ); $method = 'GET'; $timeout = 90; - $user_id = 1; // means send the wp.com user_id, not 1 + $user_id = JETPACK_MASTER_USER; // means send the wp.com user_id $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); $get_code = wp_remote_retrieve_response_code( $get ); - $get_code_type = intval( $get_code / 100 ); - if ( is_wp_error( $get ) || ( 2 != $get_code_type && 304 != $get_code ) ) { - // @todo nicer looking error - if ( 3 == $get_code_type ) { - echo '<p>' . __( 'We were unable to get your stats just now (too many redirects). Please try again.', 'jetpack' ) . '</p>'; - } else { - echo '<p>' . __( 'We were unable to get your stats just now. Please try again.', 'jetpack' ) . '</p>'; - } + if ( is_wp_error( $get ) || ( 2 != intval( $get_code / 100 ) && 304 != $get_code ) || empty( $get['body'] ) ) { + stats_print_wp_remote_error( $get, $url ); } else { if ( !empty( $get['headers']['content-type'] ) ) { $type = $get['headers']['content-type']; if ( substr( $type, 0, 5 ) == 'image' ) { + $img = $get['body']; header( 'Content-Type: ' . $type ); - die( $get['body'] ); + header( 'Content-Length: ' . strlen( $img ) ); + echo $img; + die(); } } $body = stats_convert_post_titles( $get['body'] ); @@ -442,7 +447,13 @@ function stats_convert_image_urls( $html ) { } function stats_convert_chart_urls( $html ) { - $html = preg_replace( '|https?://[-.a-z0-9]+/wp-includes/charts/([-.a-z0-9]+).php|', 'admin.php?page=stats&noheader&chart=$1', $html ); + $html = preg_replace_callback( '|https?://[-.a-z0-9]+/wp-includes/charts/([-.a-z0-9]+).php(\??)|', + create_function( + '$matches', + // If there is a query string, change the beginning '?' to a '&' so it fits into the middle of this query string + 'return "admin.php?page=stats&noheader&chart=" . $matches[1] . str_replace( "?", "&", $matches[2] );' + ), + $html ); return $html; } @@ -591,7 +602,9 @@ function stats_admin_bar_menu( &$wp_admin_bar ) { $img_src = esc_attr( add_query_arg( array( 'noheader'=>'', 'proxy'=>'', 'chart'=>'admin-bar-hours-scale' ), $url ) ); $img_src_2x = esc_attr( add_query_arg( array( 'noheader'=>'', 'proxy'=>'', 'chart'=>'admin-bar-hours-scale-2x' ), $url ) ); - $title = __( 'Views over 48 hours. Click for more Site Stats.', 'jetpack' ); + $alt = esc_attr( __( 'Stats', 'jetpack' ) ); + + $title = esc_attr( __( 'Views over 48 hours. Click for more Site Stats.', 'jetpack' ) ); $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 ); @@ -602,14 +615,6 @@ function stats_update_blog() { Jetpack::xmlrpc_async_call( 'jetpack.updateBlog', stats_get_blog() ); } -function stats_update_post( $post ) { - if ( !$stats_post = stats_get_post( $post ) ) - return; - - $jetpack = Jetpack::init(); - $jetpack->sync->post( $stats_post->ID, array_keys( get_object_vars( $stats_post ) ) ); -} - function stats_get_blog() { $home = parse_url( trailingslashit( get_option( 'home' ) ) ); $blog = array( @@ -632,36 +637,9 @@ function stats_get_blog() { return array_map( 'esc_html', $blog ); } -function stats_get_posts( $args ) { - list( $post_ids ) = $args; - $post_ids = array_map( 'intval', (array) $post_ids ); - $r = array( - 'include' => $post_ids, - 'post_type' => array_values( get_post_types( array( 'public' => true ) ) ), - 'post_status' => array_values( get_post_stati( array( 'public' => true ) ) ), - ); - $posts = get_posts( $r ); - foreach ( $posts as $i => $post ) - $posts[$i] = stats_get_post( $post ); - return $posts; -} - -function stats_get_post( $post ) { - if ( !$post = get_post( $post ) ) { - return null; - } - - $stats_post = wp_clone( $post ); - $stats_post->permalink = get_permalink( $post ); - foreach ( array( 'post_content', 'post_excerpt', 'post_content_filtered', 'post_password' ) as $do_not_want ) - unset( $stats_post->$do_not_want ); - return $stats_post; -} - function stats_xmlrpc_methods( $methods ) { $my_methods = array( 'jetpack.getBlog' => 'stats_get_blog', - 'jetpack.getPosts' => 'stats_get_posts', ); return array_merge( $methods, $my_methods ); @@ -737,7 +715,7 @@ function stats_dashboard_widget_control() { </p> <p> - <label for="top"><?php _e( 'Show top posts over', 'jetpack'); ?></label> + <label for="top"><?php _e( 'Show top posts over', 'jetpack' ); ?></label> <select id="top" name="top"> <?php foreach ( $intervals as $val => $label ) { @@ -750,7 +728,7 @@ function stats_dashboard_widget_control() { </p> <p> - <label for="search"><?php _e( 'Show top search terms over', 'jetpack'); ?></label> + <label for="search"><?php _e( 'Show top search terms over', 'jetpack' ); ?></label> <select id="search" name="search"> <?php foreach ( $intervals as $val => $label ) { @@ -892,18 +870,12 @@ function stats_dashboard_widget_content() { $url = add_query_arg( $q, $url ); $method = 'GET'; $timeout = 90; - $user_id = 1; // means send the wp.com user_id, not 1 + $user_id = JETPACK_MASTER_USER; $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); $get_code = wp_remote_retrieve_response_code( $get ); - $get_code_type = intval( $get_code / 100 ); - if ( is_wp_error( $get ) || ( 2 != $get_code_type && 304 != $get_code ) || empty( $get['body'] ) ) { - // @todo - if ( 3 == $get_code_type ) { - echo '<p>' . __( 'We were unable to get your stats just now (too many redirects). Please try again.', 'jetpack' ) . '</p>'; - } else { - echo '<p>' . __( 'We were unable to get your stats just now. Please try again.', 'jetpack' ) . '</p>'; - } + if ( is_wp_error( $get ) || ( 2 != intval( $get_code / 100 ) && 304 != $get_code ) || empty( $get['body'] ) ) { + stats_print_wp_remote_error( $get, $url ); } else { $body = stats_convert_post_titles($get['body']); $body = stats_convert_chart_urls($body); @@ -976,6 +948,50 @@ function stats_dashboard_widget_content() { exit; } +function stats_print_wp_remote_error( $get, $url ) { + $state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 ); + $previous_error = Jetpack::state( $state_name ); + $error = md5( serialize( compact( 'get', 'url' ) ) ); + Jetpack::state( $state_name, $error ); + if ( $error !== $previous_error ) { +?> + <div class="wrap"> + <p><?php _e( 'We were unable to get your stats just now. Please reload this page to try again.', 'jetpack' ); ?></p> + </div> +<?php + return; + } +?> + <div class="wrap"> + <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> + <pre> + User Agent: "<?php print htmlspecialchars( $_SERVER['HTTP_USER_AGENT'] ); ?>" + Page URL: "http<?php print (is_ssl()?'s':'') . '://' . htmlspecialchars( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); ?>" + API URL: "<?php print clean_url( $url ); ?>" +<?php + if ( is_wp_error( $get ) ) { + foreach ( $get->get_error_codes() as $code ) { + foreach ( $get->get_error_messages($code) as $message ) { + ?> + <?php print $code . ': "' . $message . '"' ?> + +<?php + } + } + } else { + $get_code = wp_remote_retrieve_response_code( $get ); + $content_length = strlen( wp_remote_retrieve_body( $get ) ); + ?> + Response code: "<?php print $get_code ?>" + Content length: "<?php print $content_length ?>" + +<?php + } + ?></pre> + </div> + <?php +} + function stats_get_csv( $table, $args = null ) { $defaults = array( 'end' => false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '' ); @@ -1034,7 +1050,7 @@ function stats_get_csv( $table, $args = null ) { function stats_get_remote_csv( $url ) { $method = 'GET'; $timeout = 90; - $user_id = 1; // means send the wp.com user_id, not 1 + $user_id = JETPACK_MASTER_USER; $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); $get_code = wp_remote_retrieve_response_code( $get ); |