summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/stats.php')
-rw-r--r--plugins/jetpack/modules/stats.php108
1 files changed, 42 insertions, 66 deletions
diff --git a/plugins/jetpack/modules/stats.php b/plugins/jetpack/modules/stats.php
index 56ff817d..362793b8 100644
--- a/plugins/jetpack/modules/stats.php
+++ b/plugins/jetpack/modules/stats.php
@@ -14,6 +14,9 @@
* @package Jetpack
*/
+use Automattic\Jetpack\Tracking;
+use Automattic\Jetpack\Connection\Client;
+
if ( defined( 'STATS_VERSION' ) ) {
return;
}
@@ -166,7 +169,7 @@ function stats_map_meta_caps( $caps, $cap, $user_id ) {
* @return void
*/
function stats_template_redirect() {
- global $current_user, $rendered_stats_footer;
+ global $current_user;
if ( is_feed() || is_robots() || is_trackback() || is_preview() || jetpack_is_dnt_enabled() ) {
return;
@@ -181,9 +184,7 @@ function stats_template_redirect() {
}
add_action( 'wp_footer', 'stats_footer', 101 );
- add_action( 'wp_head', 'stats_add_shutdown_action' );
- $rendered_stats_footer = false;
}
@@ -222,16 +223,6 @@ function stats_build_view_data() {
return compact( 'v', 'j', 'blog', 'post', 'tz', 'srv' );
}
-/**
- * Stats Add Shutdown Action.
- *
- * @access public
- * @return void
- */
-function stats_add_shutdown_action() {
- // Just in case wp_footer isn't in your theme.
- add_action( 'shutdown', 'stats_footer', 101 );
-}
/**
* Stats Footer.
@@ -240,17 +231,13 @@ function stats_add_shutdown_action() {
* @return void
*/
function stats_footer() {
- global $rendered_stats_footer;
-
- if ( ! $rendered_stats_footer ) {
- $data = stats_build_view_data();
- if ( Jetpack_AMP_Support::is_amp_request() ) {
- stats_render_amp_footer( $data );
- } else {
- stats_render_footer( $data );
- }
- $rendered_stats_footer = true;
+ $data = stats_build_view_data();
+ if ( Jetpack_AMP_Support::is_amp_request() ) {
+ stats_render_amp_footer( $data );
+ } else {
+ stats_render_footer( $data );
}
+
}
function stats_render_footer( $data ) {
@@ -682,7 +669,7 @@ function stats_reports_page( $main_chart_only = false ) {
$timeout = 90;
$user_id = JETPACK_MASTER_USER; // means send the wp.com user_id
- $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
+ $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
$get_code = wp_remote_retrieve_response_code( $get );
if ( is_wp_error( $get ) || ( 2 !== intval( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
stats_print_wp_remote_error( $get, $url );
@@ -705,7 +692,8 @@ function stats_reports_page( $main_chart_only = false ) {
}
if ( isset( $_GET['page'] ) && 'stats' === $_GET['page'] && ! isset( $_GET['chart'] ) ) {
- JetpackTracking::record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) );
+ $tracking = new Tracking();
+ $tracking->record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) );
}
if ( isset( $_GET['noheader'] ) ) {
@@ -844,7 +832,7 @@ function stats_admin_bar_head() {
add_action( 'admin_bar_menu', 'stats_admin_bar_menu', 100 );
?>
-<style type='text/css'>
+<style data-ampdevmode type='text/css'>
#wpadminbar .quicklinks li#wp-admin-bar-stats {
height: 32px;
}
@@ -890,14 +878,10 @@ function stats_admin_bar_menu( &$wp_admin_bar ) {
$title = esc_attr( __( 'Views over 48 hours. Click for more Site Stats.', 'jetpack' ) );
$menu = array(
- 'id' => 'stats',
- 'href' => $url,
+ 'id' => 'stats',
+ 'href' => $url,
+ 'title' => "<div><img src='$img_src' srcset='$img_src 1x, $img_src_2x 2x' width='112' height='24' alt='$alt' title='$title'></div>",
);
- if ( Jetpack_AMP_Support::is_amp_request() ) {
- $menu['title'] = "<amp-img src='$img_src_2x' width=112 height=24 layout=fixed alt='$alt' title='$title'></amp-img>";
- } else {
- $menu['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>";
- }
$wp_admin_bar->add_menu( $menu );
}
@@ -919,7 +903,7 @@ function stats_update_blog() {
* @return string
*/
function stats_get_blog() {
- $home = parse_url( trailingslashit( get_option( 'home' ) ) );
+ $home = wp_parse_url( trailingslashit( get_option( 'home' ) ) );
$blog = array(
'host' => $home['host'],
'path' => $home['path'],
@@ -1323,7 +1307,7 @@ function stats_dashboard_widget_content() {
$timeout = 90;
$user_id = JETPACK_MASTER_USER;
- $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
+ $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
$get_code = wp_remote_retrieve_response_code( $get );
if ( is_wp_error( $get ) || ( 2 !== intval( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
stats_print_wp_remote_error( $get, $url );
@@ -1577,7 +1561,7 @@ function stats_get_remote_csv( $url ) {
$timeout = 90;
$user_id = JETPACK_MASTER_USER;
- $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
+ $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
$get_code = wp_remote_retrieve_response_code( $get );
if ( is_wp_error( $get ) || ( 2 !== intval( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
return array(); // @todo: return an error?
@@ -1640,48 +1624,40 @@ function stats_get_from_restapi( $args = array(), $resource = '' ) {
$args = wp_parse_args( $args, array() );
$cache_key = md5( implode( '|', array( $endpoint, $api_version, serialize( $args ) ) ) );
- // Get cache.
- $stats_cache = Jetpack_Options::get_option( 'restapi_stats_cache', array() );
- if ( ! is_array( $stats_cache ) ) {
- $stats_cache = array();
- }
+ $transient_name = "jetpack_restapi_stats_cache_{$cache_key}";
+
+ $stats_cache = get_transient( $transient_name );
// Return or expire this key.
- if ( isset( $stats_cache[ $cache_key ] ) ) {
- $time = key( $stats_cache[ $cache_key ] );
- if ( time() - $time < ( 5 * MINUTE_IN_SECONDS ) ) {
- $cached_stats = $stats_cache[ $cache_key ][ $time ];
- if ( is_wp_error( $cached_stats ) ) {
- return $cached_stats;
- }
- $cached_stats = (object) array_merge( array( 'cached_at' => $time ), (array) $cached_stats );
- return $cached_stats;
+ if ( $stats_cache ) {
+ $time = key( $stats_cache );
+ $data = $stats_cache[ $time ]; // WP_Error or string (JSON encoded object)
+
+ if ( is_wp_error( $data ) ) {
+ return $data;
}
- unset( $stats_cache[ $cache_key ] );
+
+ return (object) array_merge( array( 'cached_at' => $time ), (array) json_decode( $data ) );
}
// Do the dirty work.
- $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, $api_version, $args );
+ $response = Client::wpcom_json_api_request_as_blog( $endpoint, $api_version, $args );
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ // WP_Error
$data = is_wp_error( $response ) ? $response : new WP_Error( 'stats_error' );
+ // WP_Error
+ $return = $data;
} else {
- $data = json_decode( wp_remote_retrieve_body( $response ) );
+ // string (JSON encoded object)
+ $data = wp_remote_retrieve_body( $response );
+ // object (rare: null on JSON failure)
+ $return = json_decode( $data );
}
- // Expire old keys.
- foreach ( $stats_cache as $k => $cache ) {
- if ( ! is_array( $cache ) || ( 5 * MINUTE_IN_SECONDS ) < time() - key( $cache ) ) {
- unset( $stats_cache[ $k ] );
- }
- }
+ // To reduce size in storage: store with time as key, store JSON encoded data (unless error).
+ set_transient( $transient_name, array( time() => $data ), 5 * MINUTE_IN_SECONDS );
- // Set cache.
- $stats_cache[ $cache_key ] = array(
- time() => $data,
- );
- Jetpack_Options::update_option( 'restapi_stats_cache', $stats_cache, false );
-
- return $data;
+ return $return;
}
/**