PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.2
Jetpack – WP Security, Backup, Speed, & Growth v6.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
← All changes | modules/stats.php +987 -881 13.3.36.2 View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2 /**
3 - * Module Name: Jetpack Stats
3 + * Module Name: Site Stats
4 4 * Module Description: Collect valuable traffic stats and insights.
5 5 * Sort Order: 1
6 6 * Recommendation Order: 2
7 7 * First Introduced: 1.1
@@ -6,44 +6,22 @@
6 6 * Recommendation Order: 2
7 7 * First Introduced: 1.1
8 8 * Requires Connection: Yes
9 9 * Auto Activate: Yes
10 - * Module Tags: Jetpack Stats, Site Stats, Recommended
10 + * Module Tags: Site Stats, Recommended
11 11 * Feature: Engagement
12 12 * Additional Search Queries: statistics, tracking, analytics, views, traffic, stats
13 13 *
14 - * @package automattic/jetpack
14 + * @package Jetpack
15 15 */
16 16
17 -use Automattic\Jetpack\Admin_UI\Admin_Menu;
18 -use Automattic\Jetpack\Connection\Client;
19 -use Automattic\Jetpack\Connection\Manager as Connection_Manager;
20 -use Automattic\Jetpack\Connection\XMLRPC_Async_Call;
21 -use Automattic\Jetpack\Redirect;
22 -use Automattic\Jetpack\Stats\Main as Stats;
23 -use Automattic\Jetpack\Stats\Options as Stats_Options;
24 -use Automattic\Jetpack\Stats\Tracking_Pixel as Stats_Tracking_Pixel;
25 -use Automattic\Jetpack\Stats\WPCOM_Stats;
26 -use Automattic\Jetpack\Stats\XMLRPC_Provider as Stats_XMLRPC;
27 -use Automattic\Jetpack\Stats_Admin\Dashboard as Stats_Dashboard;
28 -use Automattic\Jetpack\Stats_Admin\Main as Stats_Main;
29 -use Automattic\Jetpack\Stats_Admin\Notices as Stats_Notices;
30 -use Automattic\Jetpack\Status\Host;
31 -use Automattic\Jetpack\Tracking;
32 -
33 -if ( defined( 'STATS_DASHBOARD_SERVER' ) ) {
17 +if ( defined( 'STATS_VERSION' ) ) {
34 18 return;
35 19 }
36 20
37 -define( 'STATS_DASHBOARD_SERVER', 'dashboard.wordpress.com' );
21 +define( 'STATS_VERSION', '9' );
22 +defined( 'STATS_DASHBOARD_SERVER' ) or define( 'STATS_DASHBOARD_SERVER', 'dashboard.wordpress.com' );
38 23
39 -/**
40 - * Stats content markers.
41 - * Used to test for content vs script when parsing server-generated HTML.
42 - */
43 -const STATS_BODY_MARKER = '<div id="statchart"';
44 -const STATS_CONTENT_MARKER = '<div class="gotonewdash">';
45 -
46 24 add_action( 'jetpack_modules_loaded', 'stats_load' );
47 25
48 26 /**
49 27 * Load Stats.
@@ -52,37 +30,92 @@
52 30 * @return void
53 31 */
54 32 function stats_load() {
55 33 Jetpack::enable_module_configurable( __FILE__ );
34 + Jetpack::module_configuration_load( __FILE__, 'stats_configuration_load' );
35 + Jetpack::module_configuration_head( __FILE__, 'stats_configuration_head' );
36 + Jetpack::module_configuration_screen( __FILE__, 'stats_configuration_screen' );
56 37
38 + // Generate the tracking code after wp() has queried for posts.
39 + add_action( 'template_redirect', 'stats_template_redirect', 1 );
40 +
57 41 add_action( 'wp_head', 'stats_admin_bar_head', 100 );
58 42
43 + add_action( 'wp_head', 'stats_hide_smile_css' );
44 +
59 45 add_action( 'jetpack_admin_menu', 'stats_admin_menu' );
60 46
47 + // Map stats caps.
48 + add_filter( 'map_meta_cap', 'stats_map_meta_caps', 10, 3 );
49 +
50 + if ( isset( $_GET['oldwidget'] ) ) {
51 + // Old one.
52 + add_action( 'wp_dashboard_setup', 'stats_register_dashboard_widget' );
53 + } else {
54 + add_action( 'admin_init', 'stats_merged_widget_admin_init' );
55 + }
56 +
57 + add_filter( 'jetpack_xmlrpc_methods', 'stats_xmlrpc_methods' );
58 +
61 59 add_filter( 'pre_option_db_version', 'stats_ignore_db_version' );
62 60
63 - // Add an icon to see stats in WordPress.com for a particular post.
61 + // Add an icon to see stats in WordPress.com for a particular post
64 62 add_action( 'admin_print_styles-edit.php', 'jetpack_stats_load_admin_css' );
65 63 add_filter( 'manage_posts_columns', 'jetpack_stats_post_table' );
66 64 add_filter( 'manage_pages_columns', 'jetpack_stats_post_table' );
67 65 add_action( 'manage_posts_custom_column', 'jetpack_stats_post_table_cell', 10, 2 );
68 66 add_action( 'manage_pages_custom_column', 'jetpack_stats_post_table_cell', 10, 2 );
69 - // Filter for adding the Jetpack plugin version to tracking stats.
70 - add_filter( 'stats_array', 'filter_stats_array_add_jp_version' );
67 +}
71 68
72 - require_once __DIR__ . '/stats/class-jetpack-stats-upgrade-nudges.php';
73 - add_action( 'updating_jetpack_version', array( 'Jetpack_Stats_Upgrade_Nudges', 'unset_nudges_setting' ) );
69 +/**
70 + * Delay conditional for current_user_can to after init.
71 + *
72 + * @access public
73 + * @return void
74 + */
75 +function stats_merged_widget_admin_init() {
76 + if ( current_user_can( 'view_stats' ) ) {
77 + add_action( 'load-index.php', 'stats_enqueue_dashboard_head' );
78 + add_action( 'wp_dashboard_setup', 'stats_register_widget_control_callback' ); // Hacky but works.
79 + add_action( 'jetpack_dashboard_widget', 'stats_jetpack_dashboard_widget' );
80 + }
74 81 }
75 82
76 83 /**
84 + * Enqueue Stats Dashboard
85 + *
86 + * @access public
87 + * @return void
88 + */
89 +function stats_enqueue_dashboard_head() {
90 + add_action( 'admin_head', 'stats_dashboard_head' );
91 +}
92 +
93 +/**
77 94 * Checks if filter is set and dnt is enabled.
78 95 *
79 - * @deprecated 11.5
80 96 * @return bool
81 97 */
82 98 function jetpack_is_dnt_enabled() {
83 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Main::jetpack_is_dnt_enabled' );
84 - return Stats::jetpack_is_dnt_enabled();
99 + /**
100 + * Filter the option which decides honor DNT or not.
101 + *
102 + * @module stats
103 + * @since 6.1.0
104 + *
105 + * @param bool false Honors DNT for clients who don't want to be tracked. Defaults to false. Set to true to enable.
106 + */
107 + if ( false === apply_filters( 'jetpack_honor_dnt_header_for_stats', false ) ) {
108 + return false;
109 + }
110 +
111 + foreach ( $_SERVER as $name => $value ) {
112 + if ( 'http_dnt' == strtolower( $name ) && 1 == $value ) {
113 + return true;
114 + }
115 + }
116 +
117 + return false;
85 118 }
86 119
87 120 /**
88 121 * Prevent sparkline img requests being redirected to upgrade.php.
@@ -94,10 +127,10 @@
94 127 */
95 128 function stats_ignore_db_version( $version ) {
96 129 if (
97 130 is_admin() &&
98 - isset( $_GET['page'] ) && 'stats' === $_GET['page'] && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
99 - isset( $_GET['chart'] ) && strpos( $_GET['chart'], 'admin-bar-hours' ) === 0 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
131 + isset( $_GET['page'] ) && 'stats' === $_GET['page'] &&
132 + isset( $_GET['chart'] ) && strpos($_GET['chart'], 'admin-bar-hours') === 0
100 133 ) {
101 134 global $wp_db_version;
102 135 return $wp_db_version;
103 136 }
@@ -106,10 +139,8 @@
106 139
107 140 /**
108 141 * Maps view_stats cap to read cap as needed.
109 142 *
110 - * @deprecated 11.5
111 - *
112 143 * @access public
113 144 * @param mixed $caps Caps.
114 145 * @param mixed $cap Cap.
115 146 * @param mixed $user_id User ID.
@@ -115,68 +146,185 @@
115 146 * @param mixed $user_id User ID.
116 147 * @return array Possibly mapped capabilities for meta capability.
117 148 */
118 149 function stats_map_meta_caps( $caps, $cap, $user_id ) {
119 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Main::map_meta_caps' );
120 - return Stats::map_meta_caps( $caps, $cap, $user_id );
150 + // Map view_stats to exists.
151 + if ( 'view_stats' === $cap ) {
152 + $user = new WP_User( $user_id );
153 + $user_role = array_shift( $user->roles );
154 + $stats_roles = stats_get_option( 'roles' );
155 +
156 + // Is the users role in the available stats roles?
157 + if ( is_array( $stats_roles ) && in_array( $user_role, $stats_roles ) ) {
158 + $caps = array( 'read' );
159 + }
160 + }
161 +
162 + return $caps;
121 163 }
122 164
123 165 /**
124 166 * Stats Template Redirect.
125 167 *
126 - * @deprecated 11.5
127 168 * @access public
128 169 * @return void
129 170 */
130 171 function stats_template_redirect() {
131 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Main::template_redirect' );
132 - Stats::template_redirect();
172 + global $current_user, $rendered_stats_footer;
173 +
174 + if ( is_feed() || is_robots() || is_trackback() || is_preview() || jetpack_is_dnt_enabled() ) {
175 + return;
176 + }
177 +
178 + // Should we be counting this user's views?
179 + if ( ! empty( $current_user->ID ) ) {
180 + $count_roles = stats_get_option( 'count_roles' );
181 + if ( ! is_array( $count_roles ) || ! array_intersect( $current_user->roles, $count_roles ) ) {
182 + return;
183 + }
184 + }
185 +
186 + add_action( 'wp_footer', 'stats_footer', 101 );
187 + add_action( 'wp_head', 'stats_add_shutdown_action' );
188 +
189 + $rendered_stats_footer = false;
133 190 }
134 191
192 +
135 193 /**
136 194 * Stats Build View Data.
137 195 *
138 - * @deprecated 11.5
139 196 * @access public
140 197 * @return array.
141 198 */
142 199 function stats_build_view_data() {
143 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Tracking_Pixel::build_view_data' );
144 - return Stats_Tracking_Pixel::build_view_data();
200 + global $wp_the_query;
201 +
202 + $blog = Jetpack_Options::get_option( 'id' );
203 + $tz = get_option( 'gmt_offset' );
204 + $v = 'ext';
205 + $blog_url = parse_url( site_url() );
206 + $srv = $blog_url['host'];
207 + $j = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
208 + if ( $wp_the_query->is_single || $wp_the_query->is_page || $wp_the_query->is_posts_page ) {
209 + // Store and reset the queried_object and queried_object_id
210 + // Otherwise, redirect_canonical() will redirect to home_url( '/' ) for show_on_front = page sites where home_url() is not all lowercase.
211 + // Repro:
212 + // 1. Set home_url = https://ExamPle.com/
213 + // 2. Set show_on_front = page
214 + // 3. Set page_on_front = something
215 + // 4. Visit https://example.com/ !
216 + $queried_object = ( isset( $wp_the_query->queried_object ) ) ? $wp_the_query->queried_object : null;
217 + $queried_object_id = ( isset( $wp_the_query->queried_object_id ) ) ? $wp_the_query->queried_object_id : null;
218 + $post = $wp_the_query->get_queried_object_id();
219 + $wp_the_query->queried_object = $queried_object;
220 + $wp_the_query->queried_object_id = $queried_object_id;
221 + } else {
222 + $post = '0';
223 + }
224 +
225 + return compact( 'v', 'j', 'blog', 'post', 'tz', 'srv' );
145 226 }
146 227
147 228 /**
229 + * Stats Add Shutdown Action.
230 + *
231 + * @access public
232 + * @return void
233 + */
234 +function stats_add_shutdown_action() {
235 + // Just in case wp_footer isn't in your theme.
236 + add_action( 'shutdown', 'stats_footer', 101 );
237 +}
238 +
239 +/**
240 + * Stats Footer.
241 + *
242 + * @access public
243 + * @return void
244 + */
245 +function stats_footer() {
246 + global $rendered_stats_footer;
247 +
248 + if ( ! $rendered_stats_footer ) {
249 + $data = stats_build_view_data();
250 + if ( Jetpack_AMP_Support::is_amp_request() ) {
251 + stats_render_amp_footer( $data );
252 + } else {
253 + stats_render_footer( $data );
254 + }
255 + $rendered_stats_footer = true;
256 + }
257 +}
258 +
259 +function stats_render_footer( $data ) {
260 + $script = 'https://stats.wp.com/e-' . gmdate( 'YW' ) . '.js';
261 + $data_stats_array = stats_array( $data );
262 +
263 + $stats_footer = <<<END
264 +<script type='text/javascript' src='{$script}' async='async' defer='defer'></script>
265 +<script type='text/javascript'>
266 + _stq = window._stq || [];
267 + _stq.push([ 'view', {{$data_stats_array}} ]);
268 + _stq.push([ 'clickTrackerInit', '{$data['blog']}', '{$data['post']}' ]);
269 +</script>
270 +
271 +END;
272 + print $stats_footer;
273 +}
274 +
275 +function stats_render_amp_footer( $data ) {
276 + $data['host'] = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; // input var ok.
277 + $data['rand'] = 'RANDOM'; // AMP placeholder.
278 + $data['ref'] = 'DOCUMENT_REFERRER'; // AMP placeholder.
279 + $data = array_map( 'rawurlencode', $data );
280 + $pixel_url = add_query_arg( $data, 'https://pixel.wp.com/g.gif' );
281 +
282 + ?>
283 + <amp-pixel src="<?php echo esc_url( $pixel_url ); ?>"></amp-pixel>
284 + <?php
285 +}
286 +
287 +/**
148 288 * Stats Get Options.
149 289 *
150 - * @deprecated 11.5
151 - *
152 290 * @access public
153 291 * @return array.
154 292 */
155 293 function stats_get_options() {
156 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::get_options' );
157 - return Stats_Options::get_options();
294 + $options = get_option( 'stats_options' );
295 +
296 + if ( ! isset( $options['version'] ) || $options['version'] < STATS_VERSION ) {
297 + $options = stats_upgrade_options( $options );
298 + }
299 +
300 + return $options;
158 301 }
159 302
160 303 /**
161 304 * Get Stats Options.
162 305 *
163 - * @deprecated 11.5
164 - *
165 306 * @access public
166 307 * @param mixed $option Option.
167 308 * @return mixed|null.
168 309 */
169 310 function stats_get_option( $option ) {
170 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::get_option' );
171 - return Stats_Options::get_option( $option );
311 + $options = stats_get_options();
312 +
313 + if ( 'blog_id' === $option ) {
314 + return Jetpack_Options::get_option( 'id' );
315 + }
316 +
317 + if ( isset( $options[ $option ] ) ) {
318 + return $options[ $option ];
319 + }
320 +
321 + return null;
172 322 }
173 323
174 324 /**
175 325 * Stats Set Options.
176 326 *
177 - * @deprecated 11.5
178 - *
179 327 * @access public
180 328 * @param mixed $option Option.
181 329 * @param mixed $value Value.
182 330 * @return bool.
@@ -181,41 +329,95 @@
181 329 * @param mixed $value Value.
182 330 * @return bool.
183 331 */
184 332 function stats_set_option( $option, $value ) {
185 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::set_option' );
186 - return Stats_Options::set_option( $option, $value );
333 + $options = stats_get_options();
334 +
335 + $options[ $option ] = $value;
336 +
337 + return stats_set_options( $options );
187 338 }
188 339
189 340 /**
190 341 * Stats Set Options.
191 342 *
192 - * @deprecated 11.5
193 - *
194 343 * @access public
195 344 * @param mixed $options Options.
196 345 * @return bool
197 346 */
198 347 function stats_set_options( $options ) {
199 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::set_options' );
200 - return Stats_Options::set_options( $options );
348 + return update_option( 'stats_options', $options );
201 349 }
202 350
203 351 /**
204 352 * Stats Upgrade Options.
205 353 *
206 - * @deprecated 11.5
207 - *
208 354 * @access public
209 355 * @param mixed $options Options.
210 356 * @return array|bool
211 357 */
212 358 function stats_upgrade_options( $options ) {
213 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::upgrade_options' );
214 - return Stats_Options::upgrade_options( $options );
359 + $defaults = array(
360 + 'admin_bar' => true,
361 + 'roles' => array( 'administrator' ),
362 + 'count_roles' => array(),
363 + 'blog_id' => Jetpack_Options::get_option( 'id' ),
364 + 'do_not_track' => true, // @todo
365 + 'hide_smile' => true,
366 + );
367 +
368 + if ( isset( $options['reg_users'] ) ) {
369 + if ( ! function_exists( 'get_editable_roles' ) ) {
370 + require_once ABSPATH . 'wp-admin/includes/user.php';
371 + }
372 + if ( $options['reg_users'] ) {
373 + $options['count_roles'] = array_keys( get_editable_roles() );
374 + }
375 + unset( $options['reg_users'] );
376 + }
377 +
378 + if ( is_array( $options ) && ! empty( $options ) ) {
379 + $new_options = array_merge( $defaults, $options );
380 + } else { $new_options = $defaults;
381 + }
382 +
383 + $new_options['version'] = STATS_VERSION;
384 +
385 + if ( ! stats_set_options( $new_options ) ) {
386 + return false;
387 + }
388 +
389 + stats_update_blog();
390 +
391 + return $new_options;
215 392 }
216 393
217 394 /**
395 + * Stats Array.
396 + *
397 + * @access public
398 + * @param mixed $kvs KVS.
399 + * @return array
400 + */
401 +function stats_array( $kvs ) {
402 + /**
403 + * Filter the options added to the JavaScript Stats tracking code.
404 + *
405 + * @module stats
406 + *
407 + * @since 1.1.0
408 + *
409 + * @param array $kvs Array of options about the site and page you're on.
410 + */
411 + $kvs = apply_filters( 'stats_array', $kvs );
412 + $kvs = array_map( 'addslashes', $kvs );
413 + foreach ( $kvs as $k => $v ) {
414 + $jskvs[] = "$k:'$v'";
415 + }
416 + return join( ',', $jskvs );
417 +}
418 +
419 +/**
218 420 * Admin Pages.
219 421 *
220 422 * @access public
221 423 * @return void
@@ -224,10 +426,10 @@
224 426 global $pagenow;
225 427
226 428 // If we're at an old Stats URL, redirect to the new one.
227 429 // Don't even bother with caps, menu_page_url(), etc. Just do it.
228 - if ( 'index.php' === $pagenow && isset( $_GET['page'] ) && 'stats' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
229 - $redirect_url = str_replace( array( '/wp-admin/index.php?', '/wp-admin/?' ), '/wp-admin/admin.php?', isset( $_SERVER['REQUEST_URI'] ) ? filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : null );
430 + if ( 'index.php' === $pagenow && isset( $_GET['page'] ) && 'stats' === $_GET['page'] ) {
431 + $redirect_url = str_replace( array( '/wp-admin/index.php?', '/wp-admin/?' ), '/wp-admin/admin.php?', $_SERVER['REQUEST_URI'] );
230 432 $relative_pos = strpos( $redirect_url, '/wp-admin/' );
231 433 if ( false !== $relative_pos ) {
232 434 wp_safe_redirect( admin_url( substr( $redirect_url, $relative_pos + 10 ) ) );
233 435 exit;
@@ -233,28 +435,10 @@
233 435 exit;
234 436 }
235 437 }
236 438
237 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
238 - if ( ! ( new Host() )->is_woa_site() && isset( $_GET['enable_new_stats'] ) && '1' === $_GET['enable_new_stats'] ) {
239 - // Passing true enables Odyssey Stats.
240 - // We're ignorning the return value for now.
241 - Stats_Main::update_new_stats_status( true );
242 - }
243 -
244 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
245 - if ( ! Stats_Options::get_option( 'enable_odyssey_stats' ) || isset( $_GET['noheader'] ) ) {
246 - // Show old Jetpack Stats interface for:
247 - // - When the "enable_odyssey_stats" option is disabled.
248 - // - When being shown in the adminbar outside of wp-admin.
249 - $hook = Admin_Menu::add_menu( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'stats', 'jetpack_admin_ui_stats_report_page_wrapper' );
250 - add_action( "load-$hook", 'stats_reports_load' );
251 - } else {
252 - // Enable the new Odyssey Stats experience.
253 - $stats_dashboard = new Stats_Dashboard();
254 - $hook = Admin_Menu::add_menu( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'stats', array( $stats_dashboard, 'render' ) );
255 - add_action( "load-$hook", array( $stats_dashboard, 'admin_init' ) );
256 - }
439 + $hook = add_submenu_page( 'jetpack', __( 'Site Stats', 'jetpack' ), __( 'Site Stats', 'jetpack' ), 'view_stats', 'stats', 'stats_reports_page' );
440 + add_action( "load-$hook", 'stats_reports_load' );
257 441 }
258 442
259 443 /**
260 444 * Stats Admin Path.
@@ -272,68 +456,34 @@
272 456 * @access public
273 457 * @return void
274 458 */
275 459 function stats_reports_load() {
276 - require_once __DIR__ . '/stats/class-jetpack-stats-upgrade-nudges.php';
277 - Jetpack_Stats_Upgrade_Nudges::init();
278 -
279 460 wp_enqueue_script( 'jquery' );
280 461 wp_enqueue_script( 'postbox' );
281 462 wp_enqueue_script( 'underscore' );
282 463
283 - Jetpack_Admin_Page::load_wrapper_styles();
464 + $rtl = is_rtl() ? '.rtl' : '';
465 + wp_enqueue_style( 'dops-css', plugins_url( "_inc/build/admin.dops-style$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
466 + wp_enqueue_style( 'components-css', plugins_url( "_inc/build/style.min$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
467 +
284 468 add_action( 'admin_print_styles', 'stats_reports_css' );
285 469
286 - if ( ! empty( $_GET['nojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
287 - $parsed = wp_parse_url( admin_url() );
470 + if ( isset( $_GET['nojs'] ) && $_GET['nojs'] ) {
471 + $parsed = parse_url( admin_url() );
288 472 // Remember user doesn't want JS.
289 - setcookie( 'stnojs', '1', time() + 172800, $parsed['path'], COOKIE_DOMAIN, is_ssl(), true ); // 2 days.
473 + setcookie( 'stnojs', '1', time() + 172800, $parsed['path'] ); // 2 days.
290 474 }
291 475
292 - if ( ! empty( $_COOKIE['stnojs'] ) ) {
476 + if ( isset( $_COOKIE['stnojs'] ) && $_COOKIE['stnojs'] ) {
293 477 // Detect if JS is on. If so, remove cookie so next page load is via JS.
294 478 add_action( 'admin_print_footer_scripts', 'stats_js_remove_stnojs_cookie' );
295 - } elseif ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
479 + } else if ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) ) {
296 480 // Normal page load. Load page content via JS.
297 481 add_action( 'admin_print_footer_scripts', 'stats_js_load_page_via_ajax' );
298 - add_action( 'admin_print_footer_scripts', 'stats_script_dismiss_nudge_handler' );
299 482 }
300 483 }
301 484
302 485 /**
303 - * JavaScript to dismiss the Odyssey nudge.
304 - *
305 - * @access public
306 - * @return void
307 - */
308 -function stats_script_dismiss_nudge_handler() {
309 - ?>
310 - <script type="text/javascript">
311 - function stats_odyssey_dismiss_nudge() {
312 - // Hide the nudge UI, effectively dismissing it.
313 - var element = document.getElementById( "stats-odyssey-nudge-main" );
314 - element.classList.toggle( "is-hidden" );
315 - // Send an AJAX request.
316 - // Note we can provide a 'postponed_for' parameter to set the delay.
317 - // Without a parameter it defaults to 30 days which is what we want here.
318 - let nonce = <?php echo wp_json_encode( wp_create_nonce( 'wp_rest' ) ); ?>;
319 - let url = <?php echo wp_json_encode( rest_url( '/jetpack/v4/stats-app/stats/notices' ) ); ?>;
320 - let data = {
321 - id: 'opt_in_new_stats',
322 - status: 'postponed',
323 - };
324 - jQuery.ajax({
325 - type: "POST",
326 - url: url,
327 - data: data,
328 - headers: { "x-wp-nonce": nonce },
329 - });
330 - }
331 - </script>
332 - <?php
333 -}
334 -
335 -/**
336 486 * Stats Reports CSS.
337 487 *
338 488 * @access public
339 489 * @return void
@@ -338,12 +488,20 @@
338 488 * @access public
339 489 * @return void
340 490 */
341 491 function stats_reports_css() {
342 - ?>
492 +?>
343 493 <style type="text/css">
344 -#jp-stats-wrap, #jp-stats-report-bottom {
345 - max-width: 1040px;
494 +#wpcontent {
495 + padding-left: 0 !important;
496 +}
497 +
498 +.jetpack_page_stats {
499 + background-color: #f3f6f8;
500 +}
501 +
502 +#jp-stats-wrap {
503 + max-width: 720px;
346 504 margin: 0 auto;
347 505 overflow: hidden;
348 506 }
349 507
@@ -349,16 +507,17 @@
349 507
350 508 #stats-loading-wrap p {
351 509 text-align: center;
352 510 font-size: 2em;
353 - margin-bottom: 3em;
511 + margin: 7.5em 15px 0 0;
354 512 height: 64px;
355 513 line-height: 64px;
356 514 }
357 515 </style>
358 - <?php
516 +<?php
359 517 }
360 518
519 +
361 520 /**
362 521 * Detect if JS is on. If so, remove cookie so next page load is via JS.
363 522 *
364 523 * @access public
@@ -364,16 +523,16 @@
364 523 * @access public
365 524 * @return void
366 525 */
367 526 function stats_js_remove_stnojs_cookie() {
368 - $parsed = wp_parse_url( admin_url() );
369 - ?>
527 + $parsed = parse_url( admin_url() );
528 +?>
370 529 <script type="text/javascript">
371 530 /* <![CDATA[ */
372 531 document.cookie = 'stnojs=0; expires=Wed, 9 Mar 2011 16:55:50 UTC; path=<?php echo esc_js( $parsed['path'] ); ?>';
373 532 /* ]]> */
374 533 </script>
375 - <?php
534 +<?php
376 535 }
377 536
378 537 /**
379 538 * Normal page load. Load page content via JS.
@@ -381,38 +540,24 @@
381 540 * @access public
382 541 * @return void
383 542 */
384 543 function stats_js_load_page_via_ajax() {
385 - ?>
544 +?>
386 545 <script type="text/javascript">
387 546 /* <![CDATA[ */
388 547 if ( -1 == document.location.href.indexOf( 'noheader' ) ) {
389 548 jQuery( function( $ ) {
390 - const loadStatsUrl = new URL( document.location.href );
391 - loadStatsUrl.searchParams.append( 'noheader', 1 );
392 - $.get( loadStatsUrl.toString(), function( responseText ) {
549 + $.get( document.location.href + '&noheader', function( responseText ) {
393 550 $( '#stats-loading-wrap' ).replaceWith( responseText );
394 - $( '#jp-stats-wrap' )[0].dispatchEvent( new Event( 'stats-loaded' ) );
395 551 } );
396 552 } );
397 553 }
398 554 /* ]]> */
399 555 </script>
400 - <?php
556 +<?php
401 557 }
402 558
403 559 /**
404 - * Jetpack Admin Page Wrapper.
405 - */
406 -function jetpack_admin_ui_stats_report_page_wrapper() {
407 - if ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
408 - Jetpack_Admin_Page::wrap_ui( 'stats_reports_page', array( 'is-wide' => true ) );
409 - } else {
410 - stats_reports_page();
411 - }
412 -}
413 -
414 -/**
415 560 * Stats Report Page.
416 561 *
417 562 * @access public
418 563 * @param bool $main_chart_only (default: false) Main Chart Only.
@@ -417,37 +562,41 @@
417 562 * @access public
418 563 * @param bool $main_chart_only (default: false) Main Chart Only.
419 564 */
420 565 function stats_reports_page( $main_chart_only = false ) {
421 - if ( isset( $_GET['dashboard'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
422 - stats_dashboard_widget_content();
423 - exit; // @phan-suppress-current-line PhanPluginUnreachableCode -- Safer to include it even though stats_dashboard_widget_content() never returns.
566 +
567 + if ( isset( $_GET['dashboard'] ) ) {
568 + return stats_dashboard_widget_content();
424 569 }
425 570
426 - $blog_id = Stats_Options::get_option( 'blog_id' );
571 + $blog_id = stats_get_option( 'blog_id' );
572 + $domain = Jetpack::build_raw_urls( get_home_url() );
427 573
428 - if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
574 + $jetpack_admin_url = admin_url() . 'admin.php?page=jetpack';
575 +
576 + if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) {
429 577 $nojs_url = add_query_arg( 'nojs', '1' );
430 - $http = is_ssl() ? 'https' : 'http';
578 + $http = is_ssl() ? 'https' : 'http';
431 579 // Loading message. No JS fallback message.
432 - ?>
433 -
580 +?>
581 +<div id="jp-plugin-container">
582 + <div class="jp-masthead">
583 + <div class="jp-masthead__inside-container">
584 + <div class="jp-masthead__logo-container">
585 + <a class="jp-masthead__logo-link" href="<?php echo esc_url( $jetpack_admin_url ); ?>">
586 + <svg class="jetpack-logo__masthead" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" height="32" viewBox="0 0 118 32"><path fill="#00BE28" d="M16,0C7.2,0,0,7.2,0,16s7.2,16,16,16s16-7.2,16-16S24.8,0,16,0z M15,19H7l8-16V19z M17,29V13h8L17,29z"></path><path d="M41.3,26.6c-0.5-0.7-0.9-1.4-1.3-2.1c2.3-1.4,3-2.5,3-4.6V8h-3V6h6v13.4C46,22.8,45,24.8,41.3,26.6z"></path><path d="M65,18.4c0,1.1,0.8,1.3,1.4,1.3c0.5,0,2-0.2,2.6-0.4v2.1c-0.9,0.3-2.5,0.5-3.7,0.5c-1.5,0-3.2-0.5-3.2-3.1V12H60v-2h2.1V7.1 H65V10h4v2h-4V18.4z"></path><path d="M71,10h3v1.3c1.1-0.8,1.9-1.3,3.3-1.3c2.5,0,4.5,1.8,4.5,5.6s-2.2,6.3-5.8,6.3c-0.9,0-1.3-0.1-2-0.3V28h-3V10z M76.5,12.3 c-0.8,0-1.6,0.4-2.5,1.2v5.9c0.6,0.1,0.9,0.2,1.8,0.2c2,0,3.2-1.3,3.2-3.9C79,13.4,78.1,12.3,76.5,12.3z"></path><path d="M93,22h-3v-1.5c-0.9,0.7-1.9,1.5-3.5,1.5c-1.5,0-3.1-1.1-3.1-3.2c0-2.9,2.5-3.4,4.2-3.7l2.4-0.3v-0.3c0-1.5-0.5-2.3-2-2.3 c-0.7,0-2.3,0.5-3.7,1.1L84,11c1.2-0.4,3-1,4.4-1c2.7,0,4.6,1.4,4.6,4.7L93,22z M90,16.4l-2.2,0.4c-0.7,0.1-1.4,0.5-1.4,1.6 c0,0.9,0.5,1.4,1.3,1.4s1.5-0.5,2.3-1V16.4z"></path><path d="M104.5,21.3c-1.1,0.4-2.2,0.6-3.5,0.6c-4.2,0-5.9-2.4-5.9-5.9c0-3.7,2.3-6,6.1-6c1.4,0,2.3,0.2,3.2,0.5V13 c-0.8-0.3-2-0.6-3.2-0.6c-1.7,0-3.2,0.9-3.2,3.6c0,2.9,1.5,3.8,3.3,3.8c0.9,0,1.9-0.2,3.2-0.7V21.3z"></path><path d="M110,15.2c0.2-0.3,0.2-0.8,3.8-5.2h3.7l-4.6,5.7l5,6.3h-3.7l-4.2-5.8V22h-3V6h3V15.2z"></path><path d="M58.5,21.3c-1.5,0.5-2.7,0.6-4.2,0.6c-3.6,0-5.8-1.8-5.8-6c0-3.1,1.9-5.9,5.5-5.9s4.9,2.5,4.9,4.9c0,0.8,0,1.5-0.1,2h-7.3 c0.1,2.5,1.5,2.8,3.6,2.8c1.1,0,2.2-0.3,3.4-0.7C58.5,19,58.5,21.3,58.5,21.3z M56,15c0-1.4-0.5-2.9-2-2.9c-1.4,0-2.3,1.3-2.4,2.9 C51.6,15,56,15,56,15z"></path></svg>
587 + </a>
588 + </div>
589 + <div class="jp-masthead__nav"><span class="dops-button-group"><a href="<?php echo esc_url( $jetpack_admin_url ); ?>" type="button" class="dops-button is-compact"><?php esc_html_e( 'Dashboard', 'jetpack' ); ?></a><a href="<?php echo esc_url( $jetpack_admin_url . '#/settings' ); ?>" type="button" class="dops-button is-compact"><?php esc_html_e( 'Settings', 'jetpack' ); ?></a></span></div>
590 + </div>
591 + </div>
434 592 <div id="jp-stats-wrap">
435 - <div class="wrap">
436 - <h1><?php esc_html_e( 'Jetpack Stats', 'jetpack' ); ?>
437 - <?php
438 - if ( current_user_can( 'jetpack_manage_modules' ) ) :
439 - $i18n_headers = jetpack_get_module_i18n( 'stats' );
440 - ?>
441 - <a
442 - style="font-size:13px;"
443 - href="<?php echo esc_url( admin_url( 'admin.php?page=jetpack#/settings?term=' . rawurlencode( $i18n_headers['name'] ) ) ); ?>"
444 - >
445 - <?php esc_html_e( 'Configure', 'jetpack' ); ?>
446 - </a>
447 - <?php
448 - endif;
449 -
593 +<div class="wrap">
594 + <h2><?php esc_html_e( 'Site Stats', 'jetpack' ); ?> <?php if ( current_user_can( 'jetpack_manage_modules' ) ) : ?><a style="font-size:13px;" href="<?php echo esc_url( admin_url( 'admin.php?page=jetpack&configure=stats' ) ); ?>"><?php esc_html_e( 'Configure', 'jetpack' ); ?></a><?php endif; ?></h2>
595 +</div>
596 +<div id="stats-loading-wrap" class="wrap">
597 +<p class="hide-if-no-js"><img width="32" height="32" alt="<?php esc_attr_e( 'Loading&hellip;', 'jetpack' ); ?>" src="<?php
598 + echo esc_url(
450 599 /**
451 600 * Sets external resource URL.
452 601 *
453 602 * @module stats
@@ -452,37 +601,50 @@
452 601 *
453 602 * @module stats
454 603 *
455 604 * @since 1.4.0
456 - * @todo Clean up various uses of this filter. It's seemingly filtering different types of images in different places.
457 605 *
458 606 * @param string $args URL of external resource.
459 607 */
460 - $static_url = apply_filters( 'jetpack_static_url', "{$http}://en.wordpress.com/i/loading/loading-64.gif" );
461 - ?>
462 - </h2>
463 - </div>
464 - <div id="stats-loading-wrap" class="wrap">
465 - <p class="hide-if-no-js"><img width="32" height="32" alt="<?php esc_attr_e( 'Loading&hellip;', 'jetpack' ); ?>" src="<?php echo esc_url( $static_url ); ?>" /></p>
466 - <p class="hide-if-js"><?php esc_html_e( 'Jetpack Stats work better with JavaScript enabled.', 'jetpack' ); ?><br />
467 - <a href="<?php echo esc_url( $nojs_url ); ?>"><?php esc_html_e( 'View Jetpack Stats without JavaScript', 'jetpack' ); ?></a>.</p>
468 - </div>
469 - </div>
470 - <?php
608 + apply_filters( 'jetpack_static_url', "{$http}://en.wordpress.com/i/loading/loading-64.gif" )
609 + ); ?>" /></p>
610 +<p style="font-size: 11pt; margin: 0;"><a href="https://wordpress.com/stats/<?php echo esc_attr( $domain ); ?>" target="_blank"><?php esc_html_e( 'View stats on WordPress.com right now', 'jetpack' ); ?></a></p>
611 +<p class="hide-if-js"><?php esc_html_e( 'Your Site Stats work better with JavaScript enabled.', 'jetpack' ); ?><br />
612 +<a href="<?php echo esc_url( $nojs_url ); ?>"><?php esc_html_e( 'View Site Stats without JavaScript', 'jetpack' ); ?></a>.</p>
613 +</div>
614 +</div>
615 +<div class="jp-footer">
616 + <ul class="jp-footer__links">
617 + <li class="jp-footer__link-item">
618 + <a href="https://jetpack.com" target="_blank" rel="noopener noreferrer" class="jp-footer__link" title="<?php esc_html_e( 'Jetpack version', 'jetpack' ); ?>">Jetpack <?php echo JETPACK__VERSION; ?></a>
619 + </li>
620 + <li class="jp-footer__link-item">
621 + <a href="https://wordpress.com/tos/" target="_blank" rel="noopener noreferrer" title="<?php esc_html__( 'WordPress.com Terms of Service', 'jetpack' ); ?>" class="jp-footer__link"><?php echo esc_html_x( 'Terms', 'Navigation item', 'jetpack' ); ?></a>
622 + </li>
623 + <li class="jp-footer__link-item">
624 + <a href="<?php echo esc_url( $jetpack_admin_url . '#/privacy' ); ?>" rel="noopener noreferrer" title="<?php esc_html_e( 'Automattic\'s Privacy Policy', 'jetpack' ); ?>" class="jp-footer__link"><?php echo esc_html_x( 'Privacy', 'Navigation item', 'jetpack' ); ?></a>
625 + </li>
626 + <li class="jp-footer__link-item">
627 + <a href="<?php echo esc_url( admin_url() . 'admin.php?page=jetpack-debugger' ); ?>" title="<?php esc_html_e( 'Test your site\'s compatibility with Jetpack.', 'jetpack' ); ?>" class="jp-footer__link"><?php echo esc_html_x( 'Debug', 'Navigation item', 'jetpack' ); ?></a>
628 + </li>
629 + </ul>
630 +</div>
631 +</div>
632 +<?php
471 633 return;
472 634 }
473 635
474 - $day = isset( $_GET['day'] ) && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $_GET['day'] ) ? $_GET['day'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
475 - $q = array(
636 + $day = isset( $_GET['day'] ) && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $_GET['day'] ) ? $_GET['day'] : false;
637 + $q = array(
476 638 'noheader' => 'true',
477 - 'proxy' => '',
478 - 'page' => 'stats',
479 - 'day' => $day,
480 - 'blog' => $blog_id,
481 - 'charset' => get_option( 'blog_charset' ),
482 - 'color' => get_user_option( 'admin_color' ),
483 - 'ssl' => is_ssl(),
484 - 'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ),
639 + 'proxy' => '',
640 + 'page' => 'stats',
641 + 'day' => $day,
642 + 'blog' => $blog_id,
643 + 'charset' => get_option( 'blog_charset' ),
644 + 'color' => get_user_option( 'admin_color' ),
645 + 'ssl' => is_ssl(),
646 + 'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ),
485 647 );
486 648 if ( get_locale() !== 'en_US' ) {
487 649 $q['jp_lang'] = get_locale();
488 650 }
@@ -487,76 +649,70 @@
487 649 $q['jp_lang'] = get_locale();
488 650 }
489 651 // Only show the main chart, without extra header data, or metaboxes.
490 652 $q['main_chart_only'] = $main_chart_only;
491 - $args = array(
492 - 'view' => array( 'referrers', 'postviews', 'searchterms', 'clicks', 'post', 'table' ),
493 - 'numdays' => 'int',
494 - 'day' => 'date',
495 - 'unit' => array( '1', '7', '31', 'human' ),
496 - 'humanize' => array( 'true' ),
497 - 'num' => 'int',
498 - 'summarize' => null,
499 - 'post' => 'int',
500 - 'width' => 'int',
501 - 'height' => 'int',
502 - 'data' => 'data',
503 - 'blog_subscribers' => 'int',
653 + $args = array(
654 + 'view' => array( 'referrers', 'postviews', 'searchterms', 'clicks', 'post', 'table' ),
655 + 'numdays' => 'int',
656 + 'day' => 'date',
657 + 'unit' => array( 1, 7, 31, 'human' ),
658 + 'humanize' => array( 'true' ),
659 + 'num' => 'int',
660 + 'summarize' => null,
661 + 'post' => 'int',
662 + 'width' => 'int',
663 + 'height' => 'int',
664 + 'data' => 'data',
665 + 'blog_subscribers' => 'int',
504 666 'comment_subscribers' => null,
505 - 'type' => array( 'wpcom', 'email', 'pending' ),
506 - 'pagenum' => 'int',
507 - 'masterbar' => null,
667 + 'type' => array( 'wpcom', 'email', 'pending' ),
668 + 'pagenum' => 'int',
508 669 );
509 670 foreach ( $args as $var => $vals ) {
510 - if ( ! isset( $_REQUEST[ $var ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
671 + if ( ! isset( $_REQUEST[$var] ) )
511 672 continue;
512 - }
513 - $val = wp_unslash( $_REQUEST[ $var ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
514 673 if ( is_array( $vals ) ) {
515 - if ( in_array( $val, $vals, true ) ) {
516 - $q[ $var ] = $val;
517 - }
674 + if ( in_array( $_REQUEST[$var], $vals ) )
675 + $q[$var] = $_REQUEST[$var];
518 676 } elseif ( 'int' === $vals ) {
519 - $q[ $var ] = (int) $val;
677 + $q[$var] = intval( $_REQUEST[$var] );
520 678 } elseif ( 'date' === $vals ) {
521 - if ( preg_match( '/^\d{4}-\d{2}-\d{2}$/', $val ) ) {
522 - $q[ $var ] = $val;
523 - }
679 + if ( preg_match( '/^\d{4}-\d{2}-\d{2}$/', $_REQUEST[$var] ) )
680 + $q[$var] = $_REQUEST[$var];
524 681 } elseif ( null === $vals ) {
525 - $q[ $var ] = '';
682 + $q[$var] = '';
526 683 } elseif ( 'data' === $vals ) {
527 - if ( str_starts_with( $val, 'index.php' ) ) {
528 - $q[ $var ] = $val;
529 - }
684 + if ( 'index.php' === substr( $_REQUEST[$var], 0, 9 ) )
685 + $q[$var] = $_REQUEST[$var];
530 686 }
531 687 }
532 688
533 - if ( isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
534 - if ( preg_match( '/^[a-z0-9-]+$/', $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
535 - $chart = sanitize_title( $_GET['chart'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
536 - $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-includes/charts/{$chart}.php";
689 + if ( isset( $_GET['chart'] ) ) {
690 + if ( preg_match( '/^[a-z0-9-]+$/', $_GET['chart'] ) ) {
691 + $chart = sanitize_title( $_GET['chart'] );
692 + $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-includes/charts/{$chart}.php";
537 693 }
538 694 } else {
539 - $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
695 + $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-admin/index.php";
540 696 }
541 697
542 - $url = add_query_arg( $q, $url );
543 - $method = 'GET';
698 + $url = add_query_arg( $q, $url );
699 + $method = 'GET';
544 700 $timeout = 90;
545 - $user_id = 0; // Means use the blog token.
701 + $user_id = JETPACK_MASTER_USER; // means send the wp.com user_id
546 702
547 - $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
703 + $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
548 704 $get_code = wp_remote_retrieve_response_code( $get );
549 - if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
705 + if ( is_wp_error( $get ) || ( 2 !== intval( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
550 706 stats_print_wp_remote_error( $get, $url );
551 707 } else {
552 708 if ( ! empty( $get['headers']['content-type'] ) ) {
553 709 $type = $get['headers']['content-type'];
554 - if ( str_starts_with( $type, 'image' ) ) {
710 + if ( substr( $type, 0, 5 ) === 'image' ) {
555 711 $img = $get['body'];
556 712 header( 'Content-Type: ' . $type );
557 713 header( 'Content-Length: ' . strlen( $img ) );
558 - echo $img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
714 + echo $img;
559 715 die();
560 716 }
561 717 }
562 718 $body = stats_convert_post_titles( $get['body'] );
@@ -562,252 +718,21 @@
562 718 $body = stats_convert_post_titles( $get['body'] );
563 719 $body = stats_convert_chart_urls( $body );
564 720 $body = stats_convert_image_urls( $body );
565 721 $body = stats_convert_admin_urls( $body );
566 -
567 - // The response can contain either the content to display OR
568 - // the scripts for the chart UI. The following calls inspect the
569 - // response, insert the Odyssey nudge as needed, and make sure
570 - // everything is output correctly.
571 - stats_print_header_section( $body );
572 - stats_print_odyssey_nudge( $body );
573 - stats_print_content_section( $body );
574 - stats_print_chart_scripts( $body );
722 + echo $body;
575 723 }
576 724
577 - if ( isset( $_GET['page'] ) && 'stats' === $_GET['page'] && ! isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
578 - $tracking = new Tracking();
579 - $tracking->record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) );
725 + if ( isset( $_GET['page'] ) && 'stats' === $_GET['page'] && ! isset( $_GET['chart'] ) ) {
726 + JetpackTracking::record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) );
580 727 }
581 728
582 - if ( isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
729 + if ( isset( $_GET['noheader'] ) ) {
583 730 die;
584 731 }
585 732 }
586 733
587 734 /**
588 - * Legacy Stats: Print Header Section
589 - *
590 - * @access public
591 - * @param mixed $html HTML.
592 - * @return void
593 - */
594 -function stats_print_header_section( $html ) {
595 - $header = stats_parse_header_section( $html );
596 - if ( $header !== '' ) {
597 - echo $header; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
598 - }
599 -}
600 -
601 -/**
602 - * Legacy Stats: Print Content Section
603 - *
604 - * @access public
605 - * @param mixed $html HTML.
606 - * @return void
607 - */
608 -function stats_print_content_section( $html ) {
609 - $content = stats_parse_content_section( $html );
610 - if ( $content !== '' ) {
611 - echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
612 - }
613 -}
614 -
615 -/**
616 - * Legacy Stats: Print Chart Scripts
617 - *
618 - * @access public
619 - * @param mixed $html HTML.
620 - * @return void
621 - */
622 -function stats_print_chart_scripts( $html ) {
623 - if ( is_chart_scripts( $html ) ) {
624 - echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
625 - }
626 -}
627 -
628 -/**
629 - * Legacy Stats: Test for presence of chart scripts
630 - *
631 - * @access public
632 - * @param mixed $html Input HTML that may or may not include the chart scripts.
633 - * @return bool
634 - */
635 -function is_chart_scripts( $html ) {
636 - $pos = strpos( $html, STATS_CONTENT_MARKER );
637 - return $pos === false;
638 -}
639 -
640 -/**
641 - * Legacy Stats: Parse Header Section
642 - *
643 - * Returns the section of the content up to and including the date header.
644 - *
645 - * @access public
646 - * @param mixed $html HTML.
647 - * @return string
648 - */
649 -function stats_parse_header_section( $html ) {
650 - $head = strstr( $html, STATS_CONTENT_MARKER, true );
651 - // Enforce a string result instead of string|false.
652 - if ( $head === false ) {
653 - return '';
654 - }
655 - return $head;
656 -}
657 -
658 -/**
659 - * Legacy Stats: Print Content Section
660 - *
661 - * Returns the section of the content excluding the date header.
662 - *
663 - * @access public
664 - * @param mixed $html HTML.
665 - * @return string
666 - */
667 -function stats_parse_content_section( $html ) {
668 - $body = strstr( $html, STATS_BODY_MARKER );
669 - // Enforce a string result instead of string|false.
670 - if ( $body === false ) {
671 - return '';
672 - }
673 - return $body;
674 -}
675 -
676 -/**
677 - * Legacy Stats: Determine if we need to show the Odyssey upgrade nudge.
678 - *
679 - * @access public
680 - * @return boolean
681 - */
682 -function stats_should_show_odyssey_nudge() {
683 - $stats_notices = ( new Stats_Notices() )->get_notices_to_show();
684 - return isset( $stats_notices[ Stats_Notices::OPT_IN_NEW_STATS_NOTICE_ID ] )
685 - && $stats_notices[ Stats_Notices::OPT_IN_NEW_STATS_NOTICE_ID ];
686 -}
687 -
688 -/**
689 - * Legacy Stats: Print the Odyssey upgrade nudge.
690 - *
691 - * @access public
692 - * @param mixed $html HTML.
693 - * @return void
694 - */
695 -function stats_print_odyssey_nudge( $html ) {
696 - if ( ! stats_should_show_odyssey_nudge() ) {
697 - return;
698 - }
699 - $pos = strpos( $html, STATS_CONTENT_MARKER );
700 - if ( $pos === false ) {
701 - return;
702 - }
703 - $learn_url = Redirect::get_url( 'jetpack-stats-learn-more' );
704 - $redirect_url = admin_url( 'admin.php?page=stats&enable_new_stats=1' );
705 - ?>
706 - <style>
707 - .stats-odyssey-notice {
708 - display: flex;
709 - font-size: var( --font-body );
710 -
711 - border: 1px solid var( --jp-gray-5 );
712 - border-left-color: var( --jp-black );
713 - border-left-width: 6px;
714 - border-radius: 4px;
715 -
716 - margin-top: 24px;
717 - background: white;
718 - position: relative;
719 - }
720 - .stats-odyssey-notice--content {
721 - padding: 24px 0 24px 30px;
722 - font-size: 2em;
723 - width: 100%;
724 - }
725 - .stats-odyssey-notice--content-header {
726 - font-size: 24px;
727 - line-height: 32px;
728 - margin: 0;
729 - margin-bottom: 8px;
730 - }
731 - .stats-odyssey-notice--content-text {
732 - font-size: 16px;
733 - margin: 0;
734 - }
735 - .stats-odyssey-notice--image-container {
736 - background-image: url("/wp-content/plugins/jetpack/images/odyssey-upgrade/background.png"), url("/wp-content/plugins/jetpack/images/odyssey-upgrade/gradient.png");
737 - background-size: cover;
738 - padding-right: 28px;
739 - width: 100%;
740 - }
741 - .stats-odyssey-notice--close-button {
742 - position: absolute;
743 - top: 1rem;
744 - right: 1rem;
745 - background-color: transparent;
746 - border: none;
747 - cursor: pointer;
748 - }
749 - .stats-odyssey-notice--action-bar {
750 - display: flex;
751 - align-items: center;
752 - margin-top: 24px;
753 - }
754 - .stats-odyssey-notice--primary-button {
755 - margin-right: 18px;
756 - padding-left: 20px;
757 - padding-right: 20px;
758 - font-size: 16px;
759 - border-color: black;
760 - background-color: black;
761 - }
762 - .stats-odyssey-notice--primary-button:hover {
763 - border-color: #3c434a;
764 - background-color: #3c434a;
765 - }
766 - .is-primary-link {
767 - color: white;
768 - text-decoration: none;
769 - }
770 - .is-primary-link:active {
771 - color: white;
772 - }
773 - .is-primary-link:focus {
774 - color: white;
775 - box-shadow: none;
776 - outline: none;
777 - }
778 - .is-primary-link:hover {
779 - color: white;
780 - }
781 - .is-secondary-link {
782 - color: black;
783 - font-size: var( --font-body );
784 - }
785 - .is-secondary-link:hover {
786 - color: black;
787 - }
788 - .is-hidden {
789 - display: none;
790 - }
791 - </style>
792 - <div id="stats-odyssey-nudge-main" class="stats-odyssey-notice">
793 - <div class="stats-odyssey-notice--content">
794 - <h2 class="stats-odyssey-notice--content-header"><?php esc_html_e( 'Explore the new Jetpack Stats', 'jetpack' ); ?></h2>
795 - <p class="stats-odyssey-notice--content-text"><?php esc_html_e( "We've added new stats and insights in a more modern and mobile friendly experience to help you grow your site.", 'jetpack' ); ?></p>
796 - <div class="stats-odyssey-notice--action-bar">
797 - <button class="dops-button stats-odyssey-notice--primary-button">
798 - <a class="is-primary-link" href="<?php echo esc_url( $redirect_url ); ?>"><?php esc_html_e( 'Switch to new Stats', 'jetpack' ); ?></a>
799 - </button>
800 - <a class="is-secondary-link" href="<?php echo esc_url( $learn_url ); ?>" rel="noopener noreferrer" target="_blank"><?php esc_html_e( 'Learn about Stats', 'jetpack' ); ?> <svg xmlns="http://www.w3.org/2000/svg" style="vertical-align: middle;" viewBox="0 0 24 24" width="16" height="16" aria-hidden="true" focusable="false"><path d="M18.2 17c0 .7-.6 1.2-1.2 1.2H7c-.7 0-1.2-.6-1.2-1.2V7c0-.7.6-1.2 1.2-1.2h3.2V4.2H7C5.5 4.2 4.2 5.5 4.2 7v10c0 1.5 1.2 2.8 2.8 2.8h10c1.5 0 2.8-1.2 2.8-2.8v-3.6h-1.5V17zM14.9 3v1.5h3.7l-6.4 6.4 1.1 1.1 6.4-6.4v3.7h1.5V3h-6.3z"></path></svg></a>
801 - </div>
802 - </div>
803 - <div class="stats-odyssey-notice--image-container"></div>
804 - <button class="stats-odyssey-notice--close-button" onclick="stats_odyssey_dismiss_nudge()"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg></button>
805 - </div>
806 - <?php
807 -}
808 -
809 -/**
810 735 * Stats Convert Admin Urls.
811 736 *
812 737 * @access public
813 738 * @param mixed $html HTML.
@@ -824,9 +749,9 @@
824 749 * @param mixed $html HTML.
825 750 * @return string
826 751 */
827 752 function stats_convert_image_urls( $html ) {
828 - $url = set_url_scheme( 'https://' . STATS_DASHBOARD_SERVER );
753 + $url = set_url_scheme( 'https://' . STATS_DASHBOARD_SERVER );
829 754 $html = preg_replace( '|(["\'])(/i/stats.+)\\1|', '$1' . $url . '$2$1', $html );
830 755 return $html;
831 756 }
832 757
@@ -834,9 +759,9 @@
834 759 * Callback for preg_replace_callback used in stats_convert_chart_urls()
835 760 *
836 761 * @since 5.6.0
837 762 *
838 - * @param array $matches The matches resulting from the preg_replace_callback call.
763 + * @param array $matches The matches resulting from the preg_replace_callback call.
839 764 * @return string The admin url for the chart.
840 765 */
841 766 function jetpack_stats_convert_chart_urls_callback( $matches ) {
842 767 // If there is a query string, change the beginning '?' to a '&' so it fits into the middle of this query string.
@@ -873,12 +798,12 @@
873 798 return $html;
874 799 }
875 800 $posts = get_posts(
876 801 array(
877 - 'include' => implode( ',', $matches[1] ),
878 - 'post_type' => 'any',
879 - 'post_status' => 'any',
880 - 'numberposts' => -1,
802 + 'include' => implode( ',', $matches[1] ),
803 + 'post_type' => 'any',
804 + 'post_status' => 'any',
805 + 'numberposts' => -1,
881 806 'suppress_filters' => false,
882 807 )
883 808 );
884 809 foreach ( $posts as $post ) {
@@ -897,25 +822,130 @@
897 822 */
898 823 function stats_convert_post_title( $matches ) {
899 824 global $stats_posts;
900 825 $post_id = $matches[1];
901 - if ( isset( $stats_posts[ $post_id ] ) ) {
826 + if ( isset( $stats_posts[$post_id] ) )
902 827 return '<a href="' . get_permalink( $post_id ) . '" target="_blank">' . get_the_title( $post_id ) . '</a>';
828 + return $matches[0];
829 +}
830 +
831 +/**
832 + * Stats Configuration Load.
833 + *
834 + * @access public
835 + * @return void
836 + */
837 +function stats_configuration_load() {
838 + if ( isset( $_POST['action'] ) && 'save_options' === $_POST['action'] && $_POST['_wpnonce'] === wp_create_nonce( 'stats' ) ) {
839 + $options = stats_get_options();
840 + $options['admin_bar'] = isset( $_POST['admin_bar'] ) && $_POST['admin_bar'];
841 + $options['hide_smile'] = isset( $_POST['hide_smile'] ) && $_POST['hide_smile'];
842 +
843 + $options['roles'] = array( 'administrator' );
844 + foreach ( get_editable_roles() as $role => $details ) {
845 + if ( isset( $_POST["role_$role"] ) && $_POST["role_$role"] ) {
846 + $options['roles'][] = $role;
847 + }
848 + }
849 +
850 + $options['count_roles'] = array();
851 + foreach ( get_editable_roles() as $role => $details ) {
852 + if ( isset( $_POST["count_role_$role"] ) && $_POST["count_role_$role"] ) {
853 + $options['count_roles'][] = $role;
854 + }
855 + }
856 +
857 + stats_set_options( $options );
858 + stats_update_blog();
859 + Jetpack::state( 'message', 'module_configured' );
860 + wp_safe_redirect( Jetpack::module_configuration_url( 'stats' ) );
861 + exit;
903 862 }
904 - return $matches[0];
905 863 }
906 864
907 865 /**
908 - * CSS to hide the tracking pixel smiley.
909 - * It is now hidden for everyone (used to be visible if you had set the hide_smile option).
866 + * Stats Configuration Head.
910 867 *
911 868 * @access public
912 869 * @return void
913 870 */
871 +function stats_configuration_head() {
872 +?>
873 + <style type="text/css">
874 + #statserror {
875 + border: 1px solid #766;
876 + background-color: #d22;
877 + padding: 1em 3em;
878 + }
879 + .stats-smiley {
880 + vertical-align: 1px;
881 + }
882 + </style>
883 + <?php
884 +}
885 +
886 +/**
887 + * Stats Configuration Screen.
888 + *
889 + * @access public
890 + * @return void
891 + */
892 +function stats_configuration_screen() {
893 + $options = stats_get_options();
894 +?>
895 + <div class="narrow">
896 + <p><?php printf( __( 'Visit <a href="%s">Site Stats</a> to see your stats.', 'jetpack' ), esc_url( menu_page_url( 'stats', false ) ) ); ?></p>
897 + <form method="post">
898 + <input type='hidden' name='action' value='save_options' />
899 + <?php wp_nonce_field( 'stats' ); ?>
900 + <table id="menu" class="form-table">
901 + <tr valign="top"><th scope="row"><label for="admin_bar"><?php esc_html_e( 'Admin bar' , 'jetpack' ); ?></label></th>
902 + <td><label><input type='checkbox'<?php checked( $options['admin_bar'] ); ?> name='admin_bar' id='admin_bar' /> <?php esc_html_e( 'Put a chart showing 48 hours of views in the admin bar.', 'jetpack' ); ?></label></td></tr>
903 + <tr valign="top"><th scope="row"><?php esc_html_e( 'Registered users', 'jetpack' ); ?></th>
904 + <td>
905 + <?php esc_html_e( "Count the page views of registered users who are logged in.", 'jetpack' ); ?><br/>
906 + <?php
907 + $count_roles = stats_get_option( 'count_roles' );
908 + foreach ( get_editable_roles() as $role => $details ) {
909 +?>
910 + <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/>
911 + <?php
912 + }
913 +?>
914 + </td></tr>
915 + <tr valign="top"><th scope="row"><?php esc_html_e( 'Smiley' , 'jetpack' ); ?></th>
916 + <td><label><input type='checkbox'<?php checked( isset( $options['hide_smile'] ) && $options['hide_smile'] ); ?> name='hide_smile' id='hide_smile' /> <?php esc_html_e( 'Hide the stats smiley face image.', 'jetpack' ); ?></label><br /> <span class="description"><?php echo wp_kses( __( 'The image helps collect stats and <strong>makes the world a better place</strong> but should still work when hidden', 'jetpack' ), array( 'strong' => array() ) ); ?> <img class="stats-smiley" alt="<?php esc_attr_e( 'Smiley face', 'jetpack' ); ?>" src="<?php echo esc_url( plugins_url( 'images/stats-smiley.gif', dirname( __FILE__ ) ) ); ?>" width="6" height="5" /></span></td></tr>
917 + <tr valign="top"><th scope="row"><?php esc_html_e( 'Report visibility' , 'jetpack' ); ?></th>
918 + <td>
919 + <?php esc_html_e( 'Select the roles that will be able to view stats reports.', 'jetpack' ); ?><br/>
920 + <?php
921 + $stats_roles = stats_get_option( 'roles' );
922 + foreach ( get_editable_roles() as $role => $details ) {
923 +?>
924 + <label><input type='checkbox' <?php if ( 'administrator' === $role ) echo "disabled='disabled' "; ?>name='role_<?php echo $role; ?>'<?php checked( 'administrator' === $role || in_array( $role, $stats_roles ) ); ?> /> <?php echo translate_user_role( $details['name'] ); ?></label><br/>
925 + <?php
926 + }
927 +?>
928 + </td></tr>
929 + </table>
930 + <p class="submit"><input type='submit' class='button-primary' value='<?php echo esc_attr( __( 'Save configuration', 'jetpack' ) ); ?>' /></p>
931 + </form>
932 + </div>
933 + <?php
934 +}
935 +
936 +/**
937 + * Stats Hide Smile.
938 + *
939 + * @access public
940 + * @return void
941 + */
914 942 function stats_hide_smile_css() {
915 - ?>
916 -<style>img#wpstats{display:none}</style>
917 - <?php
943 + $options = stats_get_options();
944 + if ( isset( $options['hide_smile'] ) && $options['hide_smile'] ) {
945 +?>
946 +<style type='text/css'>img#wpstats{display:none}</style><?php
947 + }
918 948 }
919 949
920 950 /**
921 951 * Stats Admin Bar Head.
@@ -923,24 +953,22 @@
923 953 * @access public
924 954 * @return void
925 955 */
926 956 function stats_admin_bar_head() {
927 - if ( ! Stats_Options::get_option( 'admin_bar' ) ) {
957 + if ( ! stats_get_option( 'admin_bar' ) )
928 958 return;
929 - }
930 959
931 - if ( ! current_user_can( 'view_stats' ) ) {
960 + if ( ! current_user_can( 'view_stats' ) )
932 961 return;
933 - }
934 962
935 - if ( ! is_admin_bar_showing() ) {
963 + if ( function_exists( 'is_admin_bar_showing' ) && ! is_admin_bar_showing() ) {
936 964 return;
937 965 }
938 966
939 967 add_action( 'admin_bar_menu', 'stats_admin_bar_menu', 100 );
940 - ?>
968 +?>
941 969
942 -<style data-ampdevmode type='text/css'>
970 +<style type='text/css'>
943 971 #wpadminbar .quicklinks li#wp-admin-bar-stats {
944 972 height: 32px;
945 973 }
946 974 #wpadminbar .quicklinks li#wp-admin-bar-stats a {
@@ -958,40 +986,17 @@
958 986 margin: 0 8px 0 10px;
959 987 }
960 988 #wpadminbar .quicklinks li#wp-admin-bar-stats a img {
961 989 height: 24px;
962 - margin: 4px 0;
990 + padding: 4px 0;
963 991 max-width: none;
964 992 border: none;
965 993 }
966 994 </style>
967 - <?php
995 +<?php
968 996 }
969 997
970 998 /**
971 - * Gets the image source of the given stats chart.
972 - *
973 - * @param string $chart Name of the chart.
974 - * @param array $args Extra list of argument to use in the image source.
975 - * @return string An image source.
976 - */
977 -function stats_get_image_chart_src( $chart, $args = array() ) {
978 - $url = add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) );
979 -
980 - return add_query_arg(
981 - array_merge(
982 - array(
983 - 'noheader' => '',
984 - 'proxy' => '',
985 - 'chart' => $chart,
986 - ),
987 - $args
988 - ),
989 - $url
990 - );
991 -}
992 -
993 -/**
994 999 * Stats AdminBar.
995 1000 *
996 1001 * @access public
997 1002 * @param mixed $wp_admin_bar WPAdminBar.
@@ -997,67 +1002,122 @@
997 1002 * @param mixed $wp_admin_bar WPAdminBar.
998 1003 * @return void
999 1004 */
1000 1005 function stats_admin_bar_menu( &$wp_admin_bar ) {
1001 - $img_src = esc_attr( stats_get_image_chart_src( 'admin-bar-hours-scale' ) );
1002 - $img_src_2x = esc_attr( stats_get_image_chart_src( 'admin-bar-hours-scale-2x' ) );
1003 - $alt = esc_attr( __( 'Stats', 'jetpack' ) );
1004 - $title = esc_attr( __( 'Views over 48 hours. Click for more Jetpack Stats.', 'jetpack' ) );
1006 + $url = add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) ); // no menu_page_url() blog-side.
1005 1007
1006 - $menu = array(
1007 - 'id' => 'stats',
1008 - 'href' => add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) ), // no menu_page_url() blog-side.
1009 - 'title' => "<div><img src='$img_src' srcset='$img_src 1x, $img_src_2x 2x' width='112' height='24' alt='$alt' title='$title'></div>",
1010 - );
1008 + $img_src = esc_attr( add_query_arg( array( 'noheader' => '', 'proxy' => '', 'chart' => 'admin-bar-hours-scale' ), $url ) );
1009 + $img_src_2x = esc_attr( add_query_arg( array( 'noheader' => '', 'proxy' => '', 'chart' => 'admin-bar-hours-scale-2x' ), $url ) );
1011 1010
1011 + $alt = esc_attr( __( 'Stats', 'jetpack' ) );
1012 +
1013 + $title = esc_attr( __( 'Views over 48 hours. Click for more Site Stats.', 'jetpack' ) );
1014 +
1015 + $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 );
1016 +
1012 1017 $wp_admin_bar->add_menu( $menu );
1013 1018 }
1014 1019
1015 1020 /**
1016 - *
1017 - * Deprecated. The stats module should not update blog details. This is handled by Sync.
1018 - *
1019 1021 * Stats Update Blog.
1020 1022 *
1021 1023 * @access public
1022 1024 * @return void
1023 - *
1024 - * @deprecated since 10.3.
1025 1025 */
1026 1026 function stats_update_blog() {
1027 - _deprecated_function( __METHOD__, 'jetpack-10.3' );
1028 - XMLRPC_Async_Call::add_call( 'jetpack.updateBlog', 0, stats_get_blog() );
1027 + Jetpack::xmlrpc_async_call( 'jetpack.updateBlog', stats_get_blog() );
1029 1028 }
1030 1029
1031 1030 /**
1032 1031 * Stats Get Blog.
1033 1032 *
1034 - * @deprecated 11.5
1033 + * @access public
1034 + * @return string
1035 + */
1036 +function stats_get_blog() {
1037 + $home = parse_url( trailingslashit( get_option( 'home' ) ) );
1038 + $blog = array(
1039 + 'host' => $home['host'],
1040 + 'path' => $home['path'],
1041 + 'blogname' => get_option( 'blogname' ),
1042 + 'blogdescription' => get_option( 'blogdescription' ),
1043 + 'siteurl' => get_option( 'siteurl' ),
1044 + 'gmt_offset' => get_option( 'gmt_offset' ),
1045 + 'timezone_string' => get_option( 'timezone_string' ),
1046 + 'stats_version' => STATS_VERSION,
1047 + 'stats_api' => 'jetpack',
1048 + 'page_on_front' => get_option( 'page_on_front' ),
1049 + 'permalink_structure' => get_option( 'permalink_structure' ),
1050 + 'category_base' => get_option( 'category_base' ),
1051 + 'tag_base' => get_option( 'tag_base' ),
1052 + );
1053 + $blog = array_merge( stats_get_options(), $blog );
1054 + unset( $blog['roles'], $blog['blog_id'] );
1055 + return stats_esc_html_deep( $blog );
1056 +}
1057 +
1058 +/**
1059 + * Modified from stripslashes_deep()
1035 1060 *
1036 1061 * @access public
1062 + * @param mixed $value Value.
1037 1063 * @return string
1038 1064 */
1039 -function stats_get_blog() {
1040 - _deprecated_function( __METHOD__, 'jetpack-11.5' );
1041 - return Stats_XMLRPC::init()->get_blog();
1065 +function stats_esc_html_deep( $value ) {
1066 + if ( is_array( $value ) ) {
1067 + $value = array_map( 'stats_esc_html_deep', $value );
1068 + } elseif ( is_object( $value ) ) {
1069 + $vars = get_object_vars( $value );
1070 + foreach ( $vars as $key => $data ) {
1071 + $value->{$key} = stats_esc_html_deep( $data );
1072 + }
1073 + } elseif ( is_string( $value ) ) {
1074 + $value = esc_html( $value );
1075 + }
1076 +
1077 + return $value;
1042 1078 }
1043 1079
1044 1080 /**
1081 + * Stats xmlrpc_methods function.
1082 + *
1083 + * @access public
1084 + * @param mixed $methods Methods.
1085 + * @return array
1086 + */
1087 +function stats_xmlrpc_methods( $methods ) {
1088 + $my_methods = array(
1089 + 'jetpack.getBlog' => 'stats_get_blog',
1090 + );
1091 +
1092 + return array_merge( $methods, $my_methods );
1093 +}
1094 +
1095 +/**
1096 + * Register Stats Dashboard Widget.
1097 + *
1098 + * @access public
1099 + * @return void
1100 + */
1101 +function stats_register_dashboard_widget() {
1102 + if ( ! current_user_can( 'view_stats' ) )
1103 + return;
1104 +
1105 + // With wp_dashboard_empty: we load in the content after the page load via JS.
1106 + wp_add_dashboard_widget( 'dashboard_stats', __( 'Site Stats', 'jetpack' ), 'wp_dashboard_empty', 'stats_dashboard_widget_control' );
1107 +
1108 + add_action( 'admin_head', 'stats_dashboard_head' );
1109 +}
1110 +
1111 +/**
1045 1112 * Stats Dashboard Widget Options.
1046 1113 *
1047 - * TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
1048 - *
1049 1114 * @access public
1050 1115 * @return array
1051 1116 */
1052 1117 function stats_dashboard_widget_options() {
1053 - $defaults = array(
1054 - 'chart' => 1,
1055 - 'top' => 1,
1056 - 'search' => 7,
1057 - );
1058 - $options = get_option( 'stats_dashboard_widget' );
1059 - if ( ( ! $options ) || ! is_array( $options ) ) {
1118 + $defaults = array( 'chart' => 1, 'top' => 1, 'search' => 7 );
1119 + if ( ( ! $options = get_option( 'stats_dashboard_widget' ) ) || ! is_array( $options ) ) {
1060 1120 $options = array();
1061 1121 }
1062 1122
1063 1123 // Ignore obsolete option values.
@@ -1062,97 +1122,65 @@
1062 1122
1063 1123 // Ignore obsolete option values.
1064 1124 $intervals = array( 1, 7, 31, 90, 365 );
1065 1125 foreach ( array( 'top', 'search' ) as $key ) {
1066 - if ( isset( $options[ $key ] ) && ! in_array( (int) $options[ $key ], $intervals, true ) ) {
1126 + if ( isset( $options[ $key ] ) && ! in_array( $options[ $key ], $intervals ) ) {
1067 1127 unset( $options[ $key ] );
1068 1128 }
1069 1129 }
1070 1130
1071 - return array_merge( $defaults, $options );
1131 + return array_merge( $defaults, $options );
1072 1132 }
1073 1133
1074 1134 /**
1075 1135 * Stats Dashboard Widget Control.
1076 1136 *
1077 - * TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
1078 - *
1079 1137 * @access public
1080 1138 * @return void
1081 1139 */
1082 1140 function stats_dashboard_widget_control() {
1083 - stats_dashboard_widget_controls_handle_submission();
1084 1141 $periods = array(
1085 - '1' => __( 'day', 'jetpack' ),
1086 - '7' => __( 'week', 'jetpack' ),
1142 + '1' => __( 'day', 'jetpack' ),
1143 + '7' => __( 'week', 'jetpack' ),
1087 1144 '31' => __( 'month', 'jetpack' ),
1088 1145 );
1089 1146 $intervals = array(
1090 - '1' => __( 'the past day', 'jetpack' ),
1091 - '7' => __( 'the past week', 'jetpack' ),
1092 - '31' => __( 'the past month', 'jetpack' ),
1093 - '90' => __( 'the past quarter', 'jetpack' ),
1147 + '1' => __( 'the past day', 'jetpack' ),
1148 + '7' => __( 'the past week', 'jetpack' ),
1149 + '31' => __( 'the past month', 'jetpack' ),
1150 + '90' => __( 'the past quarter', 'jetpack' ),
1094 1151 '365' => __( 'the past year', 'jetpack' ),
1095 1152 );
1096 - stats_dashboard_widget_controls_html( $intervals, $periods, stats_dashboard_widget_options() );
1097 -}
1098 -
1099 -/**
1100 - * Handle widget controls form submission.
1101 - *
1102 - * TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
1103 - *
1104 - * @access public
1105 - * @return void
1106 - */
1107 -function stats_dashboard_widget_controls_handle_submission() {
1108 - $options = stats_dashboard_widget_options();
1109 1153 $defaults = array(
1110 - 'top' => 1,
1154 + 'top' => 1,
1111 1155 'search' => 7,
1112 1156 );
1113 1157
1114 - // Check if the correct form was submitted.
1115 - if ( isset( $_POST['stats_id'] ) && 'dashboard_stats' === $_POST['stats_id'] ) {
1116 - // Perform nonce verification.
1117 - if (
1118 - isset( $_POST['dashboard-widget-nonce'] ) &&
1119 - wp_verify_nonce( filter_var( wp_unslash( $_POST['dashboard-widget-nonce'] ) ), 'edit-dashboard-widget_dashboard_stats' )
1120 - ) {
1121 - // Update options.
1122 - $options['chart'] = isset( $_POST['chart'] ) ? (int) $_POST['chart'] : 1;
1123 - foreach ( array( 'top', 'search' ) as $key ) {
1124 - $options[ $key ] = isset( $_POST[ $key ] ) ? (int) $_POST[ $key ] : $defaults[ $key ];
1158 + $options = stats_dashboard_widget_options();
1159 +
1160 + if ( 'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['widget_id'] ) && 'dashboard_stats' === $_POST['widget_id'] ) {
1161 + if ( isset( $periods[ $_POST['chart'] ] ) ) {
1162 + $options['chart'] = $_POST['chart'];
1163 + }
1164 + foreach ( array( 'top', 'search' ) as $key ) {
1165 + if ( isset( $intervals[ $_POST[ $key ] ] ) ) {
1166 + $options[ $key ] = $_POST[ $key ];
1167 + } else { $options[ $key ] = $defaults[ $key ];
1125 1168 }
1126 - update_option( 'stats_dashboard_widget', $options );
1127 1169 }
1170 + update_option( 'stats_dashboard_widget', $options );
1128 1171 }
1129 -}
1130 -
1131 -/**
1132 - * Output HTML for widget controls.
1133 - *
1134 - * @param array $intervals Array of intervals.
1135 - * @param array $periods Array of periods.
1136 - * @param array $options Array of options.
1137 - *
1138 - * TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
1139 - *
1140 - * @access public
1141 - * @return void
1142 - */
1143 -function stats_dashboard_widget_controls_html( $intervals, $periods, $options ) {
1144 - ?>
1172 +?>
1145 1173 <p>
1146 - <label for="chart"><?php esc_html_e( 'Chart stats by', 'jetpack' ); ?></label>
1174 + <label for="chart"><?php esc_html_e( 'Chart stats by' , 'jetpack' ); ?></label>
1147 1175 <select id="chart" name="chart">
1148 1176 <?php
1149 1177 foreach ( $periods as $val => $label ) {
1150 - ?>
1151 - <option value="<?php echo esc_attr( $val ); ?>"<?php selected( $val, $options['chart'] ); ?>><?php echo esc_html( $label ); ?></option>
1178 +?>
1179 + <option value="<?php echo $val; ?>"<?php selected( $val, $options['chart'] ); ?>><?php echo esc_html( $label ); ?></option>
1152 1180 <?php
1153 1181 }
1154 - ?>
1182 +?>
1155 1183 </select>.
1156 1184 </p>
1157 1185
1158 1186 <p>
@@ -1159,13 +1187,13 @@
1159 1187 <label for="top"><?php esc_html_e( 'Show top posts over', 'jetpack' ); ?></label>
1160 1188 <select id="top" name="top">
1161 1189 <?php
1162 1190 foreach ( $intervals as $val => $label ) {
1163 - ?>
1164 - <option value="<?php echo esc_attr( $val ); ?>"<?php selected( $val, $options['top'] ); ?>><?php echo esc_html( $label ); ?></option>
1191 +?>
1192 + <option value="<?php echo $val; ?>"<?php selected( $val, $options['top'] ); ?>><?php echo esc_html( $label ); ?></option>
1165 1193 <?php
1166 1194 }
1167 - ?>
1195 +?>
1168 1196 </select>.
1169 1197 </p>
1170 1198
1171 1199 <p>
@@ -1172,13 +1200,13 @@
1172 1200 <label for="search"><?php esc_html_e( 'Show top search terms over', 'jetpack' ); ?></label>
1173 1201 <select id="search" name="search">
1174 1202 <?php
1175 1203 foreach ( $intervals as $val => $label ) {
1176 - ?>
1177 - <option value="<?php echo esc_attr( $val ); ?>"<?php selected( $val, $options['search'] ); ?>><?php echo esc_html( $label ); ?></option>
1204 +?>
1205 + <option value="<?php echo $val; ?>"<?php selected( $val, $options['search'] ); ?>><?php echo esc_html( $label ); ?></option>
1178 1206 <?php
1179 1207 }
1180 - ?>
1208 +?>
1181 1209 </select>.
1182 1210 </p>
1183 1211 <?php
1184 1212 }
@@ -1185,53 +1213,207 @@
1185 1213
1186 1214 /**
1187 1215 * Jetpack Stats Dashboard Widget.
1188 1216 *
1189 - * TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
1190 - *
1191 1217 * @access public
1192 1218 * @return void
1193 1219 */
1194 1220 function stats_jetpack_dashboard_widget() {
1195 - ?>
1221 +?>
1196 1222 <form id="stats_dashboard_widget_control" action="<?php echo esc_url( admin_url() ); ?>" method="post">
1197 1223 <?php stats_dashboard_widget_control(); ?>
1198 1224 <?php wp_nonce_field( 'edit-dashboard-widget_dashboard_stats', 'dashboard-widget-nonce' ); ?>
1199 - <input type="hidden" name="stats_id" value="dashboard_stats" />
1225 + <input type="hidden" name="widget_id" value="dashboard_stats" />
1200 1226 <?php submit_button( __( 'Submit', 'jetpack' ) ); ?>
1201 1227 </form>
1202 - <button type="button" class="handlediv js-toggle-stats_dashboard_widget_control" aria-expanded="true">
1203 - <span class="screen-reader-text"><?php esc_html_e( 'Configure', 'jetpack' ); ?></span>
1204 - <span class="toggle-indicator" aria-hidden="true"></span>
1205 - </button>
1206 - <div id="dashboard_stats" class="is-loading">
1228 + <span id="js-toggle-stats_dashboard_widget_control">
1229 + <?php esc_html_e( 'Configure', 'jetpack' ); ?>
1230 + </span>
1231 + <div id="dashboard_stats">
1207 1232 <div class="inside">
1208 1233 <div style="height: 250px;"></div>
1209 1234 </div>
1210 1235 </div>
1236 + <script>
1237 + jQuery(document).ready(function($){
1238 + var $toggle = $('#js-toggle-stats_dashboard_widget_control');
1239 +
1240 + $toggle.parent().prev().append( $toggle );
1241 + $toggle.show().click(function(e){
1242 + e.preventDefault();
1243 + e.stopImmediatePropagation();
1244 + $(this).parent().toggleClass('controlVisible');
1245 + $('#stats_dashboard_widget_control').slideToggle();
1246 + });
1247 + });
1248 + </script>
1249 + <style>
1250 + #js-toggle-stats_dashboard_widget_control {
1251 + display: none;
1252 + float: right;
1253 + margin-top: 0.2em;
1254 + font-weight: 400;
1255 + color: #444;
1256 + font-size: .8em;
1257 + text-decoration: underline;
1258 + cursor: pointer;
1259 + }
1260 + #stats_dashboard_widget_control {
1261 + display: none;
1262 + padding: 0 10px;
1263 + overflow: hidden;
1264 + }
1265 + #stats_dashboard_widget_control .button-primary {
1266 + float: right;
1267 + }
1268 + #dashboard_stats {
1269 + box-sizing: border-box;
1270 + width: 100%;
1271 + padding: 0 10px;
1272 + }
1273 + </style>
1211 1274 <?php
1212 1275 }
1213 1276
1214 1277 /**
1278 + * Register Stats Widget Control Callback.
1279 + *
1280 + * @access public
1281 + * @return void
1282 + */
1283 +function stats_register_widget_control_callback() {
1284 + $GLOBALS['wp_dashboard_control_callbacks']['dashboard_stats'] = 'stats_dashboard_widget_control';
1285 +}
1286 +
1287 +/**
1288 + * JavaScript and CSS for dashboard widget.
1289 + *
1290 + * @access public
1291 + * @return void
1292 + */
1293 +function stats_dashboard_head() { ?>
1294 +<script type="text/javascript">
1295 +/* <![CDATA[ */
1296 +jQuery( function($) {
1297 + var dashStats = jQuery( '#dashboard_stats div.inside' );
1298 +
1299 + if ( dashStats.find( '.dashboard-widget-control-form' ).length ) {
1300 + return;
1301 + }
1302 +
1303 + if ( ! dashStats.length ) {
1304 + dashStats = jQuery( '#dashboard_stats div.dashboard-widget-content' );
1305 + var h = parseInt( dashStats.parent().height() ) - parseInt( dashStats.prev().height() );
1306 + var args = 'width=' + dashStats.width() + '&height=' + h.toString();
1307 + } else {
1308 + if ( jQuery('#dashboard_stats' ).hasClass('postbox') ) {
1309 + var args = 'width=' + ( dashStats.prev().width() * 2 ).toString();
1310 + } else {
1311 + var args = 'width=' + ( dashStats.width() * 2 ).toString();
1312 + }
1313 + }
1314 +
1315 + dashStats
1316 + .not( '.dashboard-widget-control' )
1317 + .load( 'admin.php?page=stats&noheader&dashboard&' + args );
1318 +
1319 + jQuery( window ).one( 'resize', function() {
1320 + jQuery( '#stat-chart' ).css( 'width', 'auto' );
1321 + } );
1322 +} );
1323 +/* ]]> */
1324 +</script>
1325 +<style type="text/css">
1326 +/* <![CDATA[ */
1327 +#stat-chart {
1328 + background: none !important;
1329 +}
1330 +#dashboard_stats .inside {
1331 + margin: 10px 0 0 0 !important;
1332 +}
1333 +#dashboard_stats #stats-graph {
1334 + margin: 0;
1335 +}
1336 +#stats-info {
1337 + border-top: 1px solid #dfdfdf;
1338 + margin: 7px -10px 0 -10px;
1339 + padding: 10px;
1340 + background: #fcfcfc;
1341 + -moz-box-shadow:inset 0 1px 0 #fff;
1342 + -webkit-box-shadow:inset 0 1px 0 #fff;
1343 + box-shadow:inset 0 1px 0 #fff;
1344 + overflow: hidden;
1345 + border-radius: 0 0 2px 2px;
1346 + -webkit-border-radius: 0 0 2px 2px;
1347 + -moz-border-radius: 0 0 2px 2px;
1348 + -khtml-border-radius: 0 0 2px 2px;
1349 +}
1350 +#stats-info #top-posts, #stats-info #top-search {
1351 + float: left;
1352 + width: 50%;
1353 +}
1354 +#stats-info #top-posts {
1355 + padding-right: 3%;
1356 +}
1357 +#top-posts .stats-section-inner p {
1358 + white-space: nowrap;
1359 + overflow: hidden;
1360 +}
1361 +#top-posts .stats-section-inner p a {
1362 + overflow: hidden;
1363 + text-overflow: ellipsis;
1364 +}
1365 +#stats-info div#active {
1366 + border-top: 1px solid #dfdfdf;
1367 + margin: 0 -10px;
1368 + padding: 10px 10px 0 10px;
1369 + -moz-box-shadow:inset 0 1px 0 #fff;
1370 + -webkit-box-shadow:inset 0 1px 0 #fff;
1371 + box-shadow:inset 0 1px 0 #fff;
1372 + overflow: hidden;
1373 +}
1374 +#top-search p {
1375 + color: #999;
1376 +}
1377 +#stats-info h3 {
1378 + font-size: 1em;
1379 + margin: 0 0 .5em 0 !important;
1380 +}
1381 +#stats-info p {
1382 + margin: 0 0 .25em;
1383 + color: #999;
1384 +}
1385 +#stats-info p.widget-loading {
1386 + margin: 1em 0 0;
1387 + color: #333;
1388 +}
1389 +#stats-info p a {
1390 + display: block;
1391 +}
1392 +#stats-info p a.button {
1393 + display: inline;
1394 +}
1395 +/* ]]> */
1396 +</style>
1397 +<?php
1398 +}
1399 +
1400 +/**
1215 1401 * Stats Dashboard Widget Content.
1216 1402 *
1217 - * TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
1218 - *
1219 1403 * @access public
1220 - * @return never
1404 + * @return void
1221 1405 */
1222 1406 function stats_dashboard_widget_content() {
1223 - $width = isset( $_GET['width'] ) ? intval( $_GET['width'] ) / 2 : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1224 - $height = isset( $_GET['height'] ) ? intval( $_GET['height'] ) - 36 : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1225 - if ( ! $width || $width < 250 ) {
1407 + if ( ! isset( $_GET['width'] ) || ( ! $width = (int) ( $_GET['width'] / 2 ) ) || $width < 250 ) {
1226 1408 $width = 370;
1227 1409 }
1228 - if ( ! $height || $height < 230 ) {
1410 + if ( ! isset( $_GET['height'] ) || ( ! $height = (int) $_GET['height'] - 36 ) || $height < 230 ) {
1229 1411 $height = 180;
1230 1412 }
1231 1413
1232 - $_width = $width - 5;
1233 - $_height = $height - 5;
1414 + $_width = $width - 5;
1415 + $_height = $height - ( $GLOBALS['is_winIE'] ? 16 : 5 ); // Hack!
1234 1416
1235 1417 $options = stats_dashboard_widget_options();
1236 1418 $blog_id = Jetpack_Options::get_option( 'id' );
1237 1419
@@ -1236,50 +1418,48 @@
1236 1418 $blog_id = Jetpack_Options::get_option( 'id' );
1237 1419
1238 1420 $q = array(
1239 1421 'noheader' => 'true',
1240 - 'proxy' => '',
1241 - 'blog' => $blog_id,
1242 - 'page' => 'stats',
1243 - 'chart' => '',
1244 - 'unit' => $options['chart'],
1245 - 'color' => get_user_option( 'admin_color' ),
1246 - 'width' => $_width,
1247 - 'height' => $_height,
1248 - 'ssl' => is_ssl(),
1249 - 'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ),
1422 + 'proxy' => '',
1423 + 'blog' => $blog_id,
1424 + 'page' => 'stats',
1425 + 'chart' => '',
1426 + 'unit' => $options['chart'],
1427 + 'color' => get_user_option( 'admin_color' ),
1428 + 'width' => $_width,
1429 + 'height' => $_height,
1430 + 'ssl' => is_ssl(),
1431 + 'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ),
1250 1432 );
1251 1433
1252 - $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
1434 + $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-admin/index.php";
1253 1435
1254 - $url = add_query_arg( $q, $url );
1255 - $method = 'GET';
1436 + $url = add_query_arg( $q, $url );
1437 + $method = 'GET';
1256 1438 $timeout = 90;
1257 - $user_id = 0; // Means use the blog token.
1439 + $user_id = JETPACK_MASTER_USER;
1258 1440
1259 - $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1441 + $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1260 1442 $get_code = wp_remote_retrieve_response_code( $get );
1261 - if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1443 + if ( is_wp_error( $get ) || ( 2 !== intval( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1262 1444 stats_print_wp_remote_error( $get, $url );
1263 1445 } else {
1264 1446 $body = stats_convert_post_titles( $get['body'] );
1265 1447 $body = stats_convert_chart_urls( $body );
1266 1448 $body = stats_convert_image_urls( $body );
1267 - echo $body; // phpcs:ignore WordPress.Security.EscapeOutput
1449 + echo $body;
1268 1450 }
1269 1451
1270 1452 $post_ids = array();
1271 1453
1272 - $csv_end_date = current_time( 'Y-m-d' );
1273 - $csv_args = array(
1274 - 'top' => "&limit=6&end=$csv_end_date",
1275 - 'search' => "&limit=5&end=$csv_end_date",
1276 - );
1454 + $csv_end_date = date( 'Y-m-d', current_time( 'timestamp' ) );
1455 + $csv_args = array( 'top' => "&limit=8&end=$csv_end_date", 'search' => "&limit=5&end=$csv_end_date" );
1456 + /* Translators: Stats dashboard widget postviews list: "$post_title $views Views". */
1457 + $printf = __( '%1$s %2$s Views' , 'jetpack' );
1277 1458
1278 - $top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" );
1279 - foreach ( $top_posts as $i => $post ) {
1459 + foreach ( $top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" ) as $i => $post ) {
1280 1460 if ( 0 === $post['post_id'] ) {
1281 - unset( $top_posts[ $i ] );
1461 + unset( $top_posts[$i] );
1282 1462 continue;
1283 1463 }
1284 1464 $post_ids[] = $post['post_id'];
1285 1465 }
@@ -1284,13 +1464,12 @@
1284 1464 $post_ids[] = $post['post_id'];
1285 1465 }
1286 1466
1287 1467 // Cache.
1288 - get_posts( array( 'include' => implode( ',', array_unique( $post_ids ) ) ) );
1468 + get_posts( array( 'include' => join( ',', array_unique( $post_ids ) ) ) );
1289 1469
1290 - $searches = array();
1291 - $search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" );
1292 - foreach ( $search_terms as $search_term ) {
1470 + $searches = array();
1471 + foreach ( $search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" ) as $search_term ) {
1293 1472 if ( 'encrypted_search_terms' === $search_term['searchterm'] ) {
1294 1473 continue;
1295 1474 }
1296 1475 $searches[] = esc_html( $search_term['searchterm'] );
@@ -1295,75 +1474,55 @@
1295 1474 }
1296 1475 $searches[] = esc_html( $search_term['searchterm'] );
1297 1476 }
1298 1477
1299 - ?>
1478 +?>
1479 +<a class="button" href="admin.php?page=stats"><?php esc_html_e( 'View All', 'jetpack' ); ?></a>
1300 1480 <div id="stats-info">
1301 - <div id="stats-info-container">
1302 - <div class="stats-info-header">
1303 - <h2><?php esc_html_e( 'Highlights', 'jetpack' ); ?></h2>
1304 - <div class="stats-info-header-right">
1305 - <a href="<?php echo esc_url( admin_url( 'admin.php?page=stats' ) ); ?>" class="button button-primary">
1306 - <?php esc_html_e( 'View detailed stats', 'jetpack' ); ?>
1307 - </a>
1308 - </div>
1481 + <div id="top-posts" class='stats-section'>
1482 + <div class="stats-section-inner">
1483 + <h3 class="heading"><?php esc_html_e( 'Top Posts' , 'jetpack' ); ?></h3>
1484 + <?php
1485 + if ( empty( $top_posts ) ) {
1486 +?>
1487 + <p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
1488 + <?php
1489 + } else {
1490 + foreach ( $top_posts as $post ) {
1491 + if ( ! get_post( $post['post_id'] ) ) {
1492 + continue;
1493 + }
1494 +?>
1495 + <p><?php printf(
1496 + $printf,
1497 + '<a href="' . get_permalink( $post['post_id'] ) . '">' . get_the_title( $post['post_id'] ) . '</a>',
1498 + number_format_i18n( $post['views'] )
1499 + ); ?></p>
1500 + <?php
1501 + }
1502 + }
1503 +?>
1309 1504 </div>
1310 - <div class="stats-info-content">
1311 - <div id="top-posts" class="stats-section">
1312 - <div class="stats-section-inner">
1313 - <h3 class="heading"><?php esc_html_e( 'Top Posts', 'jetpack' ); ?></h3>
1314 - <?php
1315 - if ( empty( $top_posts ) ) {
1316 - ?>
1317 - <p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
1318 - <?php
1319 - } else {
1320 - foreach ( $top_posts as $post ) {
1321 - if ( ! get_post( $post['post_id'] ) ) {
1322 - continue;
1323 - }
1324 - ?>
1325 - <p>
1326 - <?php
1327 - printf(
1328 - esc_html(
1329 - /* Translators: Stats dashboard widget Post list with view count: "Post Title 1 View (or Views if plural)". */
1330 - _n( '%1$s %2$s View', '%1$s %2$s Views', $post['views'], 'jetpack' )
1331 - ),
1332 - '<a href="' . esc_url( get_permalink( $post['post_id'] ) ) . '">' . esc_html( get_the_title( $post['post_id'] ) ) . '</a>',
1333 - esc_html( number_format_i18n( $post['views'] ) )
1334 - );
1335 - ?>
1336 - </p>
1337 - <?php
1338 - }
1339 - }
1340 - ?>
1341 - </div>
1342 - </div>
1343 - <div id="top-search" class="stats-section">
1344 - <div class="stats-section-inner">
1345 - <h3 class="heading"><?php esc_html_e( 'Top Searches', 'jetpack' ); ?></h3>
1346 - <?php
1347 - if ( empty( $searches ) ) {
1348 - ?>
1349 - <p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
1350 - <?php
1351 - } else {
1352 - foreach ( $searches as $search_term_item ) {
1353 - printf(
1354 - '<p>%s</p>',
1355 - esc_html( $search_term_item )
1356 - );
1357 - }
1358 - }
1359 - ?>
1360 - </div>
1361 - </div>
1505 + </div>
1506 + <div id="top-search" class='stats-section'>
1507 + <div class="stats-section-inner">
1508 + <h3 class="heading"><?php esc_html_e( 'Top Searches' , 'jetpack' ); ?></h3>
1509 + <?php
1510 + if ( empty( $searches ) ) {
1511 +?>
1512 + <p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
1513 + <?php
1514 + } else {
1515 +?>
1516 + <p><?php echo join( ',&nbsp; ', $searches );?></p>
1517 + <?php
1518 + }
1519 +?>
1362 1520 </div>
1363 1521 </div>
1364 1522 </div>
1365 - <?php
1523 +<div class="clear"></div>
1524 +<?php
1366 1525 exit;
1367 1526 }
1368 1527
1369 1528 /**
@@ -1374,55 +1533,47 @@
1374 1533 * @param mixed $url URL.
1375 1534 * @return void
1376 1535 */
1377 1536 function stats_print_wp_remote_error( $get, $url ) {
1378 - $state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 );
1537 + $state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 );
1379 1538 $previous_error = Jetpack::state( $state_name );
1380 - $error = md5( wp_json_encode( compact( 'get', 'url' ) ) );
1539 + $error = md5( serialize( compact( 'get', 'url' ) ) );
1381 1540 Jetpack::state( $state_name, $error );
1382 1541 if ( $error !== $previous_error ) {
1383 - ?>
1384 - <div class="wrap">
1385 - <p><?php esc_html_e( 'We were unable to get your stats just now. Please reload this page to try again.', 'jetpack' ); ?></p>
1386 - </div>
1387 - <?php
1542 +?>
1543 + <div class="wrap">
1544 + <p><?php esc_html_e( 'We were unable to get your stats just now. Please reload this page to try again.', 'jetpack' ); ?></p>
1545 + </div>
1546 +<?php
1388 1547 return;
1389 1548 }
1390 - ?>
1549 +?>
1391 1550 <div class="wrap">
1392 - <p>
1393 - <?php
1394 - printf(
1395 - /* translators: placeholder is an a href for a support site. */
1396 - esc_html__( 'We were unable to get your stats just now. Please reload this page to try again. If this error persists, please contact %1$s. In your report, please include the information below.', 'jetpack' ),
1397 - sprintf(
1398 - '<a href="https://support.wordpress.com/contact/?jetpack=needs-service">%s</a>',
1399 - esc_html__( 'Jetpack Support', 'jetpack' )
1400 - )
1401 - );
1402 - ?>
1403 - </p>
1404 - <pre class="stats-widget-error">
1405 - User Agent: "<?php echo isset( $_SERVER['HTTP_USER_AGENT'] ) ? esc_html( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized ?>"
1406 - Page URL: "http<?php echo ( is_ssl() ? 's' : '' ) . '://' . esc_html( ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized ?>"
1407 - API URL: "<?php echo esc_url( $url ); ?>"
1408 - <?php
1409 - if ( is_wp_error( $get ) ) {
1410 - foreach ( $get->get_error_codes() as $code ) {
1411 - foreach ( $get->get_error_messages( $code ) as $message ) {
1412 - print esc_html( $code ) . ': "' . esc_html( $message ) . '"';
1413 - }
1414 - }
1415 - } else {
1416 - $get_code = wp_remote_retrieve_response_code( $get );
1417 - $content_length = strlen( wp_remote_retrieve_body( $get ) );
1418 - ?>
1419 - Response code: "<?php print esc_html( $get_code ); ?>"
1420 - Content length: "<?php print esc_html( $content_length ); ?>"
1421 - <?php
1551 + <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" target="_blank">contact support</a>. In your report please include the information below.', 'jetpack' ), 'https://support.wordpress.com/contact/?jetpack=needs-service' ); ?></p>
1552 + <pre>
1553 + User Agent: "<?php echo esc_html( $_SERVER['HTTP_USER_AGENT'] ); ?>"
1554 + Page URL: "http<?php echo (is_ssl()?'s':'') . '://' . esc_html( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); ?>"
1555 + API URL: "<?php echo esc_url( $url ); ?>"
1556 +<?php
1557 +if ( is_wp_error( $get ) ) {
1558 + foreach ( $get->get_error_codes() as $code ) {
1559 + foreach ( $get->get_error_messages( $code ) as $message ) {
1560 +?>
1561 +<?php print $code . ': "' . $message . '"' ?>
1562 +
1563 +<?php
1422 1564 }
1423 - ?>
1424 - </pre>
1565 + }
1566 +} else {
1567 + $get_code = wp_remote_retrieve_response_code( $get );
1568 + $content_length = strlen( wp_remote_retrieve_body( $get ) );
1569 +?>
1570 +Response code: "<?php print $get_code ?>"
1571 +Content length: "<?php print $content_length ?>"
1572 +
1573 +<?php
1574 +}
1575 + ?></pre>
1425 1576 </div>
1426 1577 <?php
1427 1578 }
1428 1579
@@ -1455,18 +1606,12 @@
1455 1606 * }
1456 1607 * }
1457 1608 */
1458 1609 function stats_get_csv( $table, $args = null ) {
1459 - $defaults = array(
1460 - 'end' => false,
1461 - 'days' => false,
1462 - 'limit' => 3,
1463 - 'post_id' => false,
1464 - 'summarize' => '',
1465 - );
1610 + $defaults = array( 'end' => false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '' );
1466 1611
1467 - $args = wp_parse_args( $args, $defaults );
1468 - $args['table'] = $table;
1612 + $args = wp_parse_args( $args, $defaults );
1613 + $args['table'] = $table;
1469 1614 $args['blog_id'] = Jetpack_Options::get_option( 'id' );
1470 1615
1471 1616 $stats_csv_url = add_query_arg( $args, 'https://stats.wordpress.com/csv.php' );
1472 1617
@@ -1488,10 +1633,9 @@
1488 1633 }
1489 1634
1490 1635 $stats_rows = array();
1491 1636 do {
1492 - $stats = stats_get_remote_csv( $stats_csv_url );
1493 - if ( ! $stats ) {
1637 + if ( ! $stats = stats_get_remote_csv( $stats_csv_url ) ) {
1494 1638 break;
1495 1639 }
1496 1640
1497 1641 $labels = array_shift( $stats );
@@ -1531,15 +1675,15 @@
1531 1675 * @param mixed $url URL.
1532 1676 * @return array
1533 1677 */
1534 1678 function stats_get_remote_csv( $url ) {
1535 - $method = 'GET';
1679 + $method = 'GET';
1536 1680 $timeout = 90;
1537 - $user_id = 0; // Blog token.
1681 + $user_id = JETPACK_MASTER_USER;
1538 1682
1539 - $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1683 + $get = Jetpack_Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1540 1684 $get_code = wp_remote_retrieve_response_code( $get );
1541 - if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1685 + if ( is_wp_error( $get ) || ( 2 !== intval( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1542 1686 return array(); // @todo: return an error?
1543 1687 } else {
1544 1688 return stats_str_getcsv( $get['body'] );
1545 1689 }
@@ -1545,18 +1689,33 @@
1545 1689 }
1546 1690 }
1547 1691
1548 1692 /**
1549 - * Recursively run str_getcsv on the stats csv.
1693 + * Rather than parsing the csv and its special cases, we create a new file and do fgetcsv on it.
1550 1694 *
1551 - * @since 9.7.0 Remove custom handling since str_getcsv is available on all servers running this now.
1552 - *
1695 + * @access public
1553 1696 * @param mixed $csv CSV.
1554 1697 * @return array.
1555 1698 */
1556 1699 function stats_str_getcsv( $csv ) {
1557 - $lines = str_getcsv( $csv, "\n" );
1558 - return array_map( 'str_getcsv', $lines );
1700 + if ( function_exists( 'str_getcsv' ) ) {
1701 + $lines = str_getcsv( $csv, "\n" );
1702 + return array_map( 'str_getcsv', $lines );
1703 + }
1704 + if ( ! $temp = tmpfile() ) { // The tmpfile() automatically unlinks.
1705 + return false;
1706 + }
1707 +
1708 + $data = array();
1709 +
1710 + fwrite( $temp, $csv, strlen( $csv ) );
1711 + fseek( $temp, 0 );
1712 + while ( false !== $row = fgetcsv( $temp, 2000 ) ) {
1713 + $data[] = $row;
1714 + }
1715 + fclose( $temp );
1716 +
1717 + return $data;
1559 1718 }
1560 1719
1561 1720 /**
1562 1721 * Abstract out building the rest api stats path.
@@ -1565,9 +1724,9 @@
1565 1724 * @return string
1566 1725 */
1567 1726 function jetpack_stats_api_path( $resource = '' ) {
1568 1727 $resource = ltrim( $resource, '/' );
1569 - return sprintf( '/sites/%d/stats/%s', Stats_Options::get_option( 'blog_id' ), $resource );
1728 + return sprintf( '/sites/%d/stats/%s', stats_get_option( 'blog_id' ), $resource );
1570 1729 }
1571 1730
1572 1731 /**
1573 1732 * Fetches stats data from the REST API. Caches locally for 5 minutes.
@@ -1573,54 +1732,58 @@
1573 1732 * Fetches stats data from the REST API. Caches locally for 5 minutes.
1574 1733 *
1575 1734 * @link: https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/
1576 1735 * @access public
1577 - * @deprecated 11.5 Use WPCOM_Stats available methodsinstead.
1578 1736 * @param array $args (default: array()) The args that are passed to the endpoint.
1579 1737 * @param string $resource (default: '') Optional sub-endpoint following /stats/.
1580 1738 * @return array|WP_Error.
1581 1739 */
1582 1740 function stats_get_from_restapi( $args = array(), $resource = '' ) {
1583 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
1584 1741 $endpoint = jetpack_stats_api_path( $resource );
1585 1742 $api_version = '1.1';
1586 1743 $args = wp_parse_args( $args, array() );
1587 - $cache_key = md5( implode( '|', array( $endpoint, $api_version, wp_json_encode( $args ) ) ) );
1744 + $cache_key = md5( implode( '|', array( $endpoint, $api_version, serialize( $args ) ) ) );
1588 1745
1589 - $transient_name = "jetpack_restapi_stats_cache_{$cache_key}";
1746 + // Get cache.
1747 + $stats_cache = Jetpack_Options::get_option( 'restapi_stats_cache', array() );
1748 + if ( ! is_array( $stats_cache ) ) {
1749 + $stats_cache = array();
1750 + }
1590 1751
1591 - $stats_cache = get_transient( $transient_name );
1592 -
1593 1752 // Return or expire this key.
1594 - if ( $stats_cache ) {
1595 - $time = key( $stats_cache );
1596 - $data = $stats_cache[ $time ]; // WP_Error or string (JSON encoded object).
1597 -
1598 - if ( is_wp_error( $data ) ) {
1599 - return $data;
1753 + if ( isset( $stats_cache[ $cache_key ] ) ) {
1754 + $time = key( $stats_cache[ $cache_key ] );
1755 + if ( time() - $time < ( 5 * MINUTE_IN_SECONDS ) ) {
1756 + $cached_stats = $stats_cache[ $cache_key ][ $time ];
1757 + $cached_stats = (object) array_merge( array( 'cached_at' => $time ), (array) $cached_stats );
1758 + return $cached_stats;
1600 1759 }
1601 -
1602 - return (object) array_merge( array( 'cached_at' => $time ), (array) json_decode( $data ) );
1760 + unset( $stats_cache[ $cache_key ] );
1603 1761 }
1604 1762
1605 1763 // Do the dirty work.
1606 - $response = Client::wpcom_json_api_request_as_blog( $endpoint, $api_version, $args );
1764 + $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, $api_version, $args );
1607 1765 if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
1608 - // WP_Error.
1609 - $data = is_wp_error( $response ) ? $response : new WP_Error( 'stats_error' );
1610 - // WP_Error.
1611 - $return = $data;
1612 - } else {
1613 - // string (JSON encoded object).
1614 - $data = wp_remote_retrieve_body( $response );
1615 - // object (rare: null on JSON failure).
1616 - $return = json_decode( $data );
1766 + // If bad, just return it, don't cache.
1767 + return $response;
1617 1768 }
1618 1769
1619 - // To reduce size in storage: store with time as key, store JSON encoded data (unless error).
1620 - set_transient( $transient_name, array( time() => $data ), 5 * MINUTE_IN_SECONDS );
1770 + $data = json_decode( wp_remote_retrieve_body( $response ) );
1621 1771
1622 - return $return;
1772 + // Expire old keys.
1773 + foreach ( $stats_cache as $k => $cache ) {
1774 + if ( ! is_array( $cache ) || ( 5 * MINUTE_IN_SECONDS ) < time() - key( $cache ) ) {
1775 + unset( $stats_cache[ $k ] );
1776 + }
1777 + }
1778 +
1779 + // Set cache.
1780 + $stats_cache[ $cache_key ] = array(
1781 + time() => $data,
1782 + );
1783 + Jetpack_Options::update_option( 'restapi_stats_cache', $stats_cache, false );
1784 +
1785 + return $data;
1623 1786 }
1624 1787
1625 1788 /**
1626 1789 * Load CSS needed for Stats column width in WP-Admin area.
@@ -1637,9 +1800,9 @@
1637 1800 <?php
1638 1801 }
1639 1802
1640 1803 /**
1641 - * Set header for column that allows to view an entry's stats.
1804 + * Set header for column that allows to go to WordPress.com to see an entry's stats.
1642 1805 *
1643 1806 * @param array $columns An array of column names.
1644 1807 *
1645 1808 * @since 4.7.0
@@ -1645,41 +1808,17 @@
1645 1808 * @since 4.7.0
1646 1809 *
1647 1810 * @return mixed
1648 1811 */
1649 -function jetpack_stats_post_table( $columns ) {
1650 - /*
1651 - * Stats can be accessed in wp-admin or in Calypso,
1652 - * depending on what version of the stats screen is enabled on your site.
1653 - *
1654 - * In both cases, the user must be allowed to access stats.
1655 - *
1656 - * If the Odyssey Stats experience isn't enabled, the user will need to go to Calypso,
1657 - * so they need to be connected to WordPress.com to be able to access that page.
1658 - */
1659 - if (
1660 - ! current_user_can( 'view_stats' )
1661 - || (
1662 - ! Stats_Options::get_option( 'enable_odyssey_stats' )
1663 - && ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected()
1664 - )
1665 - ) {
1812 +function jetpack_stats_post_table( $columns ) { // Adds a stats link on the edit posts page
1813 + if ( ! current_user_can( 'view_stats' ) || ! Jetpack::is_user_connected() ) {
1666 1814 return $columns;
1667 1815 }
1668 -
1669 - // Array-Fu to add before comments.
1670 - $pos = array_search( 'comments', array_keys( $columns ), true );
1671 -
1672 - // Fallback to the last position if the post type does not support comments.
1816 + // Array-Fu to add before comments
1817 + $pos = array_search( 'comments', array_keys( $columns ) );
1673 1818 if ( ! is_int( $pos ) ) {
1674 - $pos = count( $columns );
1675 - }
1676 -
1677 - // Final fallback, if the array was malformed by another plugin for example.
1678 - if ( ! is_int( $pos ) ) {
1679 1819 return $columns;
1680 1820 }
1681 -
1682 1821 $chunks = array_chunk( $columns, $pos, true );
1683 1822 $chunks[0]['stats'] = esc_html__( 'Stats', 'jetpack' );
1684 1823
1685 1824 return call_user_func_array( 'array_merge', $chunks );
@@ -1685,9 +1824,9 @@
1685 1824 return call_user_func_array( 'array_merge', $chunks );
1686 1825 }
1687 1826
1688 1827 /**
1689 - * Set content for cell with link to an entry's stats in Odyssey Stats.
1828 + * Set content for cell with link to an entry's stats in WordPress.com.
1690 1829 *
1691 1830 * @param string $column The name of the column to display.
1692 1831 * @param int $post_id The current post ID.
1693 1832 *
@@ -1695,52 +1834,19 @@
1695 1834 *
1696 1835 * @return mixed
1697 1836 */
1698 1837 function jetpack_stats_post_table_cell( $column, $post_id ) {
1699 - if ( 'stats' === $column ) {
1700 - if ( 'publish' !== get_post_status( $post_id ) ) {
1838 + if ( 'stats' == $column ) {
1839 + if ( 'publish' != get_post_status( $post_id ) ) {
1701 1840 printf(
1702 1841 '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
1703 1842 esc_html__( 'No stats', 'jetpack' )
1704 1843 );
1705 1844 } else {
1706 - $stats_post_url = ! ( new Host() )->is_woa_site() && Stats_Options::get_option( 'enable_odyssey_stats' )
1707 - ? admin_url( sprintf( 'admin.php?page=stats#!/stats/post/%d/%d', $post_id, Jetpack_Options::get_option( 'id', 0 ) ) )
1708 - : Redirect::get_url(
1709 - 'calypso-stats-post',
1710 - array(
1711 - 'path' => $post_id,
1712 - )
1713 - );
1714 -
1715 1845 printf(
1716 1846 '<a href="%s" title="%s" class="dashicons dashicons-chart-bar" target="_blank"></a>',
1717 - esc_url( $stats_post_url ),
1718 - esc_html__( 'View stats for this post', 'jetpack' )
1847 + esc_url( "https://wordpress.com/stats/post/$post_id/" . Jetpack::build_raw_urls( get_home_url() ) ),
1848 + esc_html__( 'View stats for this post in WordPress.com', 'jetpack' )
1719 1849 );
1720 1850 }
1721 1851 }
1722 -}
1723 -
1724 -/**
1725 - * Add the Jetpack plugin version to the stats tracking data.
1726 - *
1727 - * @param array $kvs The stats array in key values.
1728 - * @return array
1729 - */
1730 -function filter_stats_array_add_jp_version( $kvs ) {
1731 - $kvs['j'] = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION );
1732 -
1733 - return $kvs;
1734 -}
1735 -
1736 -/**
1737 - * Convert stats array to object after sanity checking the array is valid.
1738 - *
1739 - * @param array $stats_array The stats array.
1740 - * @return WP_Error|Object|null
1741 - */
1742 -function convert_stats_array_to_object( $stats_array ) {
1743 - _deprecated_function( __FUNCTION__, 'jetpack-13.2', 'Automattic\Jetpack\Stats\WPCOM_Stats->convert_stats_array_to_object' );
1744 -
1745 - return ( new WPCOM_Stats() )->convert_stats_array_to_object( $stats_array );
1746 1852 }