PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.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 All 502 releases
← All changes | modules/stats.php +202 -459 12.1.316.2 View file →
@@ -1,8 +1,8 @@
1 1 <?php
2 2 /**
3 3 * Module Name: Jetpack Stats
4 - * Module Description: Collect valuable traffic stats and insights.
4 + * Module Description: Clear, concise traffic insights right in your WordPress dashboard.
5 5 * Sort Order: 1
6 6 * Recommendation Order: 2
7 7 * First Introduced: 1.1
8 8 * Requires Connection: Yes
@@ -13,22 +13,26 @@
13 13 *
14 14 * @package automattic/jetpack
15 15 */
16 16
17 +use Automattic\Jetpack\Admin_UI\Admin_Menu;
17 18 use Automattic\Jetpack\Connection\Client;
18 -use Automattic\Jetpack\Connection\Manager as Connection_Manager;
19 -use Automattic\Jetpack\Connection\XMLRPC_Async_Call;
20 19 use Automattic\Jetpack\Redirect;
21 20 use Automattic\Jetpack\Stats\Main as Stats;
22 21 use Automattic\Jetpack\Stats\Options as Stats_Options;
23 22 use Automattic\Jetpack\Stats\Tracking_Pixel as Stats_Tracking_Pixel;
23 +use Automattic\Jetpack\Stats\WPCOM_Stats;
24 24 use Automattic\Jetpack\Stats\XMLRPC_Provider as Stats_XMLRPC;
25 +use Automattic\Jetpack\Stats_Admin\Admin_Post_List_Column;
25 26 use Automattic\Jetpack\Stats_Admin\Dashboard as Stats_Dashboard;
26 27 use Automattic\Jetpack\Stats_Admin\Main as Stats_Main;
27 -use Automattic\Jetpack\Stats_Admin\Notices as Stats_Notices;
28 28 use Automattic\Jetpack\Status\Host;
29 29 use Automattic\Jetpack\Tracking;
30 30
31 +if ( ! defined( 'ABSPATH' ) ) {
32 + exit( 0 );
33 +}
34 +
31 35 if ( defined( 'STATS_DASHBOARD_SERVER' ) ) {
32 36 return;
33 37 }
34 38
@@ -51,20 +55,21 @@
51 55 */
52 56 function stats_load() {
53 57 Jetpack::enable_module_configurable( __FILE__ );
54 58
55 - add_action( 'wp_head', 'stats_admin_bar_head', 100 );
59 + // Only run the callback for those who can see the stats.
60 + if ( is_user_logged_in() && current_user_can( 'view_stats' ) ) {
61 + add_action( 'admin_head', 'stats_admin_bar_head', 100 );
62 + add_action( 'wp_head', 'stats_admin_bar_head', 100 );
63 + }
56 64
65 + Admin_Post_List_Column::register();
66 +
57 67 add_action( 'jetpack_admin_menu', 'stats_admin_menu' );
68 + add_action( 'wp_before_admin_bar_render', 'stats_add_link_to_admin_bar_site_menu' );
58 69
59 70 add_filter( 'pre_option_db_version', 'stats_ignore_db_version' );
60 71
61 - // Add an icon to see stats in WordPress.com for a particular post.
62 - add_action( 'admin_print_styles-edit.php', 'jetpack_stats_load_admin_css' );
63 - add_filter( 'manage_posts_columns', 'jetpack_stats_post_table' );
64 - add_filter( 'manage_pages_columns', 'jetpack_stats_post_table' );
65 - add_action( 'manage_posts_custom_column', 'jetpack_stats_post_table_cell', 10, 2 );
66 - add_action( 'manage_pages_custom_column', 'jetpack_stats_post_table_cell', 10, 2 );
67 72 // Filter for adding the Jetpack plugin version to tracking stats.
68 73 add_filter( 'stats_array', 'filter_stats_array_add_jp_version' );
69 74
70 75 require_once __DIR__ . '/stats/class-jetpack-stats-upgrade-nudges.php';
@@ -134,9 +139,9 @@
134 139 * Stats Build View Data.
135 140 *
136 141 * @deprecated 11.5
137 142 * @access public
138 - * @return array.
143 + * @return array
139 144 */
140 145 function stats_build_view_data() {
141 146 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Tracking_Pixel::build_view_data' );
142 147 return Stats_Tracking_Pixel::build_view_data();
@@ -147,9 +152,9 @@
147 152 *
148 153 * @deprecated 11.5
149 154 *
150 155 * @access public
151 - * @return array.
156 + * @return array
152 157 */
153 158 function stats_get_options() {
154 159 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::get_options' );
155 160 return Stats_Options::get_options();
@@ -161,9 +166,9 @@
161 166 * @deprecated 11.5
162 167 *
163 168 * @access public
164 169 * @param mixed $option Option.
165 - * @return mixed|null.
170 + * @return mixed|null
166 171 */
167 172 function stats_get_option( $option ) {
168 173 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::get_option' );
169 174 return Stats_Options::get_option( $option );
@@ -176,9 +181,9 @@
176 181 *
177 182 * @access public
178 183 * @param mixed $option Option.
179 184 * @param mixed $value Value.
180 - * @return bool.
185 + * @return bool
181 186 */
182 187 function stats_set_option( $option, $value ) {
183 188 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::set_option' );
184 189 return Stats_Options::set_option( $option, $value );
@@ -227,9 +232,9 @@
227 232 $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 );
228 233 $relative_pos = strpos( $redirect_url, '/wp-admin/' );
229 234 if ( false !== $relative_pos ) {
230 235 wp_safe_redirect( admin_url( substr( $redirect_url, $relative_pos + 10 ) ) );
231 - exit;
236 + exit( 0 );
232 237 }
233 238 }
234 239
235 240 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -239,20 +244,17 @@
239 244 Stats_Main::update_new_stats_status( true );
240 245 }
241 246
242 247 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
243 - if ( ( new Host() )->is_woa_site() || ! Stats_Options::get_option( 'enable_odyssey_stats' ) || isset( $_GET['noheader'] ) ) {
248 + if ( ! Stats_Options::get_option( 'enable_odyssey_stats' ) || isset( $_GET['noheader'] ) ) {
244 249 // Show old Jetpack Stats interface for:
245 - // - Atomic sites.
246 250 // - When the "enable_odyssey_stats" option is disabled.
247 251 // - When being shown in the adminbar outside of wp-admin.
248 - $hook = add_submenu_page( 'jetpack', __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'stats', 'jetpack_admin_ui_stats_report_page_wrapper' );
252 + $hook = Admin_Menu::add_menu( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'stats', 'jetpack_admin_ui_stats_report_page_wrapper' );
249 253 add_action( "load-$hook", 'stats_reports_load' );
250 254 } else {
251 255 // Enable the new Odyssey Stats experience.
252 - $stats_dashboard = new Stats_Dashboard();
253 - $hook = add_submenu_page( 'jetpack', __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'stats', array( $stats_dashboard, 'render' ) );
254 - add_action( "load-$hook", array( $stats_dashboard, 'admin_init' ) );
256 + Stats_Dashboard::init();
255 257 }
256 258 }
257 259
258 260 /**
@@ -292,9 +294,8 @@
292 294 // Detect if JS is on. If so, remove cookie so next page load is via JS.
293 295 add_action( 'admin_print_footer_scripts', 'stats_js_remove_stnojs_cookie' );
294 296 } elseif ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
295 297 // Normal page load. Load page content via JS.
296 - add_action( 'admin_print_footer_scripts', 'stats_js_load_page_via_ajax' );
297 298 add_action( 'admin_print_footer_scripts', 'stats_script_dismiss_nudge_handler' );
298 299 }
299 300 }
300 301
@@ -313,10 +314,10 @@
313 314 element.classList.toggle( "is-hidden" );
314 315 // Send an AJAX request.
315 316 // Note we can provide a 'postponed_for' parameter to set the delay.
316 317 // Without a parameter it defaults to 30 days which is what we want here.
317 - let nonce = <?php echo wp_json_encode( wp_create_nonce( 'wp_rest' ) ); ?>;
318 - let url = <?php echo wp_json_encode( rest_url( '/jetpack/v4/stats-app/stats/notices' ) ); ?>;
318 + let nonce = <?php echo wp_json_encode( wp_create_nonce( 'wp_rest' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?>;
319 + let url = <?php echo wp_json_encode( rest_url( '/jetpack/v4/stats-app/stats/notices' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?>;
319 320 let data = {
320 321 id: 'opt_in_new_stats',
321 322 status: 'postponed',
322 323 };
@@ -344,16 +345,8 @@
344 345 max-width: 1040px;
345 346 margin: 0 auto;
346 347 overflow: hidden;
347 348 }
348 -
349 -#stats-loading-wrap p {
350 - text-align: center;
351 - font-size: 2em;
352 - margin-bottom: 3em;
353 - height: 64px;
354 - line-height: 64px;
355 -}
356 349 </style>
357 350 <?php
358 351 }
359 352
@@ -367,9 +360,9 @@
367 360 $parsed = wp_parse_url( admin_url() );
368 361 ?>
369 362 <script type="text/javascript">
370 363 /* <![CDATA[ */
371 -document.cookie = 'stnojs=0; expires=Wed, 9 Mar 2011 16:55:50 UTC; path=<?php echo esc_js( $parsed['path'] ); ?>';
364 +document.cookie = <?php echo wp_json_encode( 'stnojs=0; expires=Wed, 9 Mar 2011 16:55:50 UTC; path=' . $parsed['path'], JSON_UNESCAPED_SLASHES | JSON_HEX_TAG ); ?>;
372 365 /* ]]> */
373 366 </script>
374 367 <?php
375 368 }
@@ -374,33 +367,8 @@
374 367 <?php
375 368 }
376 369
377 370 /**
378 - * Normal page load. Load page content via JS.
379 - *
380 - * @access public
381 - * @return void
382 - */
383 -function stats_js_load_page_via_ajax() {
384 - ?>
385 -<script type="text/javascript">
386 -/* <![CDATA[ */
387 -if ( -1 == document.location.href.indexOf( 'noheader' ) ) {
388 - jQuery( function( $ ) {
389 - const loadStatsUrl = new URL( document.location.href );
390 - loadStatsUrl.searchParams.append( 'noheader', 1 );
391 - $.get( loadStatsUrl.toString(), function( responseText ) {
392 - $( '#stats-loading-wrap' ).replaceWith( responseText );
393 - $( '#jp-stats-wrap' )[0].dispatchEvent( new Event( 'stats-loaded' ) );
394 - } );
395 - } );
396 -}
397 -/* ]]> */
398 -</script>
399 - <?php
400 -}
401 -
402 -/**
403 371 * Jetpack Admin Page Wrapper.
404 372 */
405 373 function jetpack_admin_ui_stats_report_page_wrapper() {
406 374 if ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -417,19 +385,110 @@
417 385 * @param bool $main_chart_only (default: false) Main Chart Only.
418 386 */
419 387 function stats_reports_page( $main_chart_only = false ) {
420 388 if ( isset( $_GET['dashboard'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
421 - return stats_dashboard_widget_content();
389 + stats_dashboard_widget_content();
390 + exit( 0 ); // @phan-suppress-current-line PhanPluginUnreachableCode -- Safer to include it even though stats_dashboard_widget_content() never returns.
422 391 }
423 392
424 - $blog_id = Stats_Options::get_option( 'blog_id' );
393 + $blog_id = Stats_Options::get_option( 'blog_id' );
394 + $learn_url = Redirect::get_url( 'jetpack-stats-learn-more' );
395 + $redirect_url = admin_url( 'admin.php?page=stats&enable_new_stats=1' );
396 + $stats_bg_url = plugins_url( 'images/odyssey-upgrade/background.png', JETPACK__PLUGIN_FILE );
397 + $stats_bg_gradient_url = plugins_url( 'images/odyssey-upgrade/gradient.png', JETPACK__PLUGIN_FILE );
425 398
426 - if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
427 - $nojs_url = add_query_arg( 'nojs', '1' );
428 - $http = is_ssl() ? 'https' : 'http';
429 - // Loading message. No JS fallback message.
399 + if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
430 400 ?>
431 401
402 + <style>
403 + .stats-odyssey-notice {
404 + display: flex;
405 + font-size: var( --font-body );
406 +
407 + border: 1px solid var( --jp-gray-5 );
408 + border-left-color: var( --jp-black );
409 + border-left-width: 6px;
410 + border-radius: 4px;
411 +
412 + margin-top: 24px;
413 + background: white;
414 + position: relative;
415 + }
416 + .stats-odyssey-notice--content__highlighted {
417 + border-left-color: var( --jp-red );
418 + }
419 + .stats-odyssey-notice--content {
420 + padding: 24px 0 24px 30px;
421 + font-size: 2em;
422 + width: 100%;
423 + }
424 + .stats-odyssey-notice--content-header {
425 + font-size: 24px;
426 + line-height: 32px;
427 + margin: 0;
428 + margin-bottom: 8px;
429 + }
430 + .stats-odyssey-notice--content-text {
431 + font-size: 16px;
432 + margin: 0;
433 + }
434 + .stats-odyssey-notice--image-container {
435 + background-image: url("<?php echo esc_url( $stats_bg_url ); ?>"), url("<?php echo esc_url( $stats_bg_gradient_url ); ?>");
436 + background-size: cover;
437 + padding-right: 28px;
438 + width: 100%;
439 + }
440 + .stats-odyssey-notice--close-button {
441 + position: absolute;
442 + top: 1rem;
443 + right: 1rem;
444 + background-color: transparent;
445 + border: none;
446 + cursor: pointer;
447 + }
448 + .stats-odyssey-notice--action-bar {
449 + display: flex;
450 + align-items: center;
451 + margin-top: 24px;
452 + }
453 + .stats-odyssey-notice--primary-button {
454 + margin-right: 18px;
455 + padding-left: 20px;
456 + padding-right: 20px;
457 + font-size: 16px;
458 + border-color: black;
459 + background-color: black;
460 + }
461 + .stats-odyssey-notice--primary-button:hover {
462 + border-color: #3c434a;
463 + background-color: #3c434a;
464 + }
465 + .is-primary-link {
466 + color: white;
467 + text-decoration: none;
468 + }
469 + .is-primary-link:active {
470 + color: white;
471 + }
472 + .is-primary-link:focus {
473 + color: white;
474 + box-shadow: none;
475 + outline: none;
476 + }
477 + .is-primary-link:hover {
478 + color: white;
479 + }
480 + .is-secondary-link {
481 + color: black;
482 + font-size: var( --font-body );
483 + }
484 + .is-secondary-link:hover {
485 + color: black;
486 + }
487 + .is-hidden {
488 + display: none;
489 + }
490 + </style>
432 491 <div id="jp-stats-wrap">
433 492 <div class="wrap">
434 493 <h1><?php esc_html_e( 'Jetpack Stats', 'jetpack' ); ?>
435 494 <?php
@@ -437,9 +496,9 @@
437 496 $i18n_headers = jetpack_get_module_i18n( 'stats' );
438 497 ?>
439 498 <a
440 499 style="font-size:13px;"
441 - href="<?php echo esc_url( admin_url( 'admin.php?page=jetpack#/settings?term=' . rawurlencode( $i18n_headers['name'] ) ) ); ?>"
500 + href="<?php echo esc_url( admin_url( 'admin.php?page=jetpack#/settings?term=' . rawurlencode( $i18n_headers['name'] ?? '' ) ) ); ?>"
442 501 >
443 502 <?php esc_html_e( 'Configure', 'jetpack' ); ?>
444 503 </a>
445 504 <?php
@@ -444,27 +503,27 @@
444 503 </a>
445 504 <?php
446 505 endif;
447 506
448 - /**
449 - * Sets external resource URL.
450 - *
451 - * @module stats
452 - *
453 - * @since 1.4.0
454 - * @todo Clean up various uses of this filter. It's seemingly filtering different types of images in different places.
455 - *
456 - * @param string $args URL of external resource.
457 - */
458 - $static_url = apply_filters( 'jetpack_static_url', "{$http}://en.wordpress.com/i/loading/loading-64.gif" );
459 507 ?>
460 508 </h2>
461 509 </div>
462 - <div id="stats-loading-wrap" class="wrap">
463 - <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>
464 - <p class="hide-if-js"><?php esc_html_e( 'Jetpack Stats work better with JavaScript enabled.', 'jetpack' ); ?><br />
465 - <a href="<?php echo esc_url( $nojs_url ); ?>"><?php esc_html_e( 'View Jetpack Stats without JavaScript', 'jetpack' ); ?></a>.</p>
510 + <div class="wrap">
511 + <div class="stats-odyssey-notice stats-odyssey-notice--content__highlighted">
512 + <div class="stats-odyssey-notice--content">
513 + <h2 class="stats-odyssey-notice--content-header"><?php esc_html_e( 'Deprecated Jetpack Stats Experience', 'jetpack' ); ?></h2>
514 + <p class="stats-odyssey-notice--content-text"><?php esc_html_e( 'The old Jetpack Stats has been deprecated. Please click the button to enable the new experience.', 'jetpack' ); ?></p>
515 + <div class="stats-odyssey-notice--action-bar">
516 + <button class="dops-button stats-odyssey-notice--primary-button">
517 + <a class="is-primary-link" href="<?php echo esc_url( $redirect_url ); ?>"><?php esc_html_e( 'Switch to new Stats', 'jetpack' ); ?></a>
518 + </button>
519 + <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>
520 + </div>
521 + </div>
522 + <div class="stats-odyssey-notice--image-container"></div>
523 + </div>
466 524 </div>
525 + <p></p>
467 526 </div>
468 527 <?php
469 528 return;
470 529 }
@@ -521,21 +580,20 @@
521 580 }
522 581 } elseif ( null === $vals ) {
523 582 $q[ $var ] = '';
524 583 } elseif ( 'data' === $vals ) {
525 - if ( 'index.php' === substr( $val, 0, 9 ) ) {
584 + if ( str_starts_with( $val, 'index.php' ) ) {
526 585 $q[ $var ] = $val;
527 586 }
528 587 }
529 588 }
530 589
590 + $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
531 591 if ( isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
532 592 if ( preg_match( '/^[a-z0-9-]+$/', $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
533 593 $chart = sanitize_title( $_GET['chart'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
534 594 $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-includes/charts/{$chart}.php";
535 595 }
536 - } else {
537 - $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
538 596 }
539 597
540 598 $url = add_query_arg( $q, $url );
541 599 $method = 'GET';
@@ -543,34 +601,19 @@
543 601 $user_id = 0; // Means use the blog token.
544 602
545 603 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
546 604 $get_code = wp_remote_retrieve_response_code( $get );
547 - if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
605 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
548 606 stats_print_wp_remote_error( $get, $url );
549 - } else {
550 - if ( ! empty( $get['headers']['content-type'] ) ) {
551 - $type = $get['headers']['content-type'];
552 - if ( substr( $type, 0, 5 ) === 'image' ) {
553 - $img = $get['body'];
554 - header( 'Content-Type: ' . $type );
555 - header( 'Content-Length: ' . strlen( $img ) );
556 - echo $img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
557 - die();
558 - }
607 + } elseif ( ! empty( $get['headers']['content-type'] ) ) {
608 + $type = $get['headers']['content-type'];
609 + if ( str_starts_with( $type, 'image' ) ) {
610 + $img = $get['body'];
611 + header( 'Content-Type: ' . $type );
612 + header( 'Content-Length: ' . strlen( $img ) );
613 + echo $img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
614 + die( 0 );
559 615 }
560 - $body = stats_convert_post_titles( $get['body'] );
561 - $body = stats_convert_chart_urls( $body );
562 - $body = stats_convert_image_urls( $body );
563 - $body = stats_convert_admin_urls( $body );
564 -
565 - // The response can contain either the content to display OR
566 - // the scripts for the chart UI. The following calls inspect the
567 - // response, insert the Odyssey nudge as needed, and make sure
568 - // everything is output correctly.
569 - stats_print_header_section( $body );
570 - stats_print_odyssey_nudge( $body );
571 - stats_print_content_section( $body );
572 - stats_print_chart_scripts( $body );
573 616 }
574 617
575 618 if ( isset( $_GET['page'] ) && 'stats' === $_GET['page'] && ! isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
576 619 $tracking = new Tracking();
@@ -577,246 +620,13 @@
577 620 $tracking->record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) );
578 621 }
579 622
580 623 if ( isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
581 - die;
624 + die( 0 );
582 625 }
583 626 }
584 627
585 628 /**
586 - * Legacy Stats: Print Header Section
587 - *
588 - * @access public
589 - * @param mixed $html HTML.
590 - * @return void
591 - */
592 -function stats_print_header_section( $html ) {
593 - $header = stats_parse_header_section( $html );
594 - if ( $header !== '' ) {
595 - echo $header; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
596 - }
597 -}
598 -
599 -/**
600 - * Legacy Stats: Print Content Section
601 - *
602 - * @access public
603 - * @param mixed $html HTML.
604 - * @return void
605 - */
606 -function stats_print_content_section( $html ) {
607 - $content = stats_parse_content_section( $html );
608 - if ( $content !== '' ) {
609 - echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
610 - }
611 -}
612 -
613 -/**
614 - * Legacy Stats: Print Chart Scripts
615 - *
616 - * @access public
617 - * @param mixed $html HTML.
618 - * @return void
619 - */
620 -function stats_print_chart_scripts( $html ) {
621 - if ( is_chart_scripts( $html ) ) {
622 - echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
623 - }
624 -}
625 -
626 -/**
627 - * Legacy Stats: Test for presence of chart scripts
628 - *
629 - * @access public
630 - * @param mixed $html Input HTML that may or may not include the chart scripts.
631 - * @return bool
632 - */
633 -function is_chart_scripts( $html ) {
634 - $pos = strpos( $html, STATS_CONTENT_MARKER );
635 - return $pos === false;
636 -}
637 -
638 -/**
639 - * Legacy Stats: Parse Header Section
640 - *
641 - * Returns the section of the content up to and including the date header.
642 - *
643 - * @access public
644 - * @param mixed $html HTML.
645 - * @return string
646 - */
647 -function stats_parse_header_section( $html ) {
648 - $head = strstr( $html, STATS_CONTENT_MARKER, true );
649 - // Enforce a string result instead of string|false.
650 - if ( $head === false ) {
651 - return '';
652 - }
653 - return $head;
654 -}
655 -
656 -/**
657 - * Legacy Stats: Print Content Section
658 - *
659 - * Returns the section of the content excluding the date header.
660 - *
661 - * @access public
662 - * @param mixed $html HTML.
663 - * @return string
664 - */
665 -function stats_parse_content_section( $html ) {
666 - $body = strstr( $html, STATS_BODY_MARKER );
667 - // Enforce a string result instead of string|false.
668 - if ( $body === false ) {
669 - return '';
670 - }
671 - return $body;
672 -}
673 -
674 -/**
675 - * Legacy Stats: Determine if we need to show the Odyssey upgrade nudge.
676 - *
677 - * @access public
678 - * @return boolean
679 - */
680 -function stats_should_show_odyssey_nudge() {
681 - $stats_notices = ( new Stats_Notices() )->get_notices_to_show();
682 - return isset( $stats_notices[ Stats_Notices::OPT_IN_NEW_STATS_NOTICE_ID ] )
683 - && $stats_notices[ Stats_Notices::OPT_IN_NEW_STATS_NOTICE_ID ];
684 -}
685 -
686 -/**
687 - * Legacy Stats: Print the Odyssey upgrade nudge.
688 - *
689 - * @access public
690 - * @param mixed $html HTML.
691 - * @return void
692 - */
693 -function stats_print_odyssey_nudge( $html ) {
694 - if ( ! stats_should_show_odyssey_nudge() ) {
695 - return;
696 - }
697 - $pos = strpos( $html, STATS_CONTENT_MARKER );
698 - if ( $pos === false ) {
699 - return;
700 - }
701 - $learn_url = Redirect::get_url( 'jetpack-stats-learn-more' );
702 - $redirect_url = admin_url( 'admin.php?page=stats&enable_new_stats=1' );
703 - ?>
704 - <style>
705 - .stats-odyssey-notice {
706 - display: flex;
707 - font-size: var( --font-body );
708 -
709 - border: 1px solid var( --jp-gray-5 );
710 - border-left-color: var( --jp-black );
711 - border-left-width: 6px;
712 - border-radius: 4px;
713 -
714 - margin-top: 24px;
715 - background: white;
716 - position: relative;
717 - }
718 - .stats-odyssey-notice--content {
719 - padding: 24px 0 24px 30px;
720 - font-size: 2em;
721 - width: 100%;
722 - }
723 - .stats-odyssey-notice--content-header {
724 - font-size: 24px;
725 - line-height: 32px;
726 - margin: 0;
727 - margin-bottom: 8px;
728 - }
729 - .stats-odyssey-notice--content-text {
730 - font-size: 16px;
731 - margin: 0;
732 - }
733 - .stats-odyssey-notice--image-container {
734 - background-image: url("/wp-content/plugins/jetpack/images/odyssey-upgrade/background.png"), url("/wp-content/plugins/jetpack/images/odyssey-upgrade/gradient.png");
735 - background-size: cover;
736 - padding-right: 28px;
737 - width: 100%;
738 - }
739 - .stats-odyssey-notice--close-button {
740 - position: absolute;
741 - top: 1rem;
742 - right: 1rem;
743 - background-color: transparent;
744 - border: none;
745 - cursor: pointer;
746 - }
747 - .stats-odyssey-notice--action-bar {
748 - display: flex;
749 - align-items: center;
750 - margin-top: 24px;
751 - }
752 - .stats-odyssey-notice--primary-button {
753 - margin-right: 18px;
754 - padding-left: 20px;
755 - padding-right: 20px;
756 - font-size: 16px;
757 - border-color: black;
758 - background-color: black;
759 - }
760 - .stats-odyssey-notice--primary-button:hover {
761 - border-color: #3c434a;
762 - background-color: #3c434a;
763 - }
764 - .is-primary-link {
765 - color: white;
766 - text-decoration: none;
767 - }
768 - .is-primary-link:active {
769 - color: white;
770 - }
771 - .is-primary-link:focus {
772 - color: white;
773 - box-shadow: none;
774 - outline: none;
775 - }
776 - .is-primary-link:hover {
777 - color: white;
778 - }
779 - .is-secondary-link {
780 - color: black;
781 - font-size: var( --font-body );
782 - }
783 - .is-secondary-link:hover {
784 - color: black;
785 - }
786 - .is-hidden {
787 - display: none;
788 - }
789 - </style>
790 - <div id="stats-odyssey-nudge-main" class="stats-odyssey-notice">
791 - <div class="stats-odyssey-notice--content">
792 - <h2 class="stats-odyssey-notice--content-header"><?php esc_html_e( 'Explore the new Jetpack Stats', 'jetpack' ); ?></h2>
793 - <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>
794 - <div class="stats-odyssey-notice--action-bar">
795 - <button class="dops-button stats-odyssey-notice--primary-button">
796 - <a class="is-primary-link" href="<?php echo esc_url( $redirect_url ); ?>"><?php esc_html_e( 'Switch to new Stats', 'jetpack' ); ?></a>
797 - </button>
798 - <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>
799 - </div>
800 - </div>
801 - <div class="stats-odyssey-notice--image-container"></div>
802 - <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>
803 - </div>
804 - <?php
805 -}
806 -
807 -/**
808 - * Stats Convert Admin Urls.
809 - *
810 - * @access public
811 - * @param mixed $html HTML.
812 - * @return string
813 - */
814 -function stats_convert_admin_urls( $html ) {
815 - return str_replace( 'index.php?page=stats', 'admin.php?page=stats', $html );
816 -}
817 -
818 -/**
819 629 * Stats Convert Image URLs.
820 630 *
821 631 * @access public
822 632 * @param mixed $html HTML.
@@ -921,8 +731,13 @@
921 731 * @access public
922 732 * @return void
923 733 */
924 734 function stats_admin_bar_head() {
735 + // Let's not show the stats admin bar to users who are not logged in.
736 + if ( ! is_user_logged_in() ) {
737 + return;
738 + }
739 +
925 740 if ( ! Stats_Options::get_option( 'admin_bar' ) ) {
926 741 return;
927 742 }
928 743
@@ -1003,9 +818,9 @@
1003 818
1004 819 $menu = array(
1005 820 'id' => 'stats',
1006 821 'href' => add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) ), // no menu_page_url() blog-side.
1007 - 'title' => "<div><img src='$img_src' srcset='$img_src 1x, $img_src_2x 2x' width='112' height='24' alt='$alt' title='$title'></div>",
822 + 'title' => "<div><img fetchpriority='low' loading='lazy' decoding='async' src='$img_src' srcset='$img_src 1x, $img_src_2x 2x' width='112' height='24' alt='$alt' title='$title'></div>",
1008 823 );
1009 824
1010 825 $wp_admin_bar->add_menu( $menu );
1011 826 }
@@ -1010,21 +825,33 @@
1010 825 $wp_admin_bar->add_menu( $menu );
1011 826 }
1012 827
1013 828 /**
829 + * Adds a Stats link to the site-name admin bar submenu, alongside Dashboard.
1014 830 *
1015 - * Deprecated. The stats module should not update blog details. This is handled by Sync.
1016 - *
1017 - * Stats Update Blog.
1018 - *
1019 831 * @access public
1020 832 * @return void
1021 - *
1022 - * @deprecated since 10.3.
1023 833 */
1024 -function stats_update_blog() {
1025 - _deprecated_function( __METHOD__, 'jetpack-10.3' );
1026 - XMLRPC_Async_Call::add_call( 'jetpack.updateBlog', 0, stats_get_blog() );
834 +function stats_add_link_to_admin_bar_site_menu() {
835 + global $wp_admin_bar;
836 +
837 + if (
838 + ! is_object( $wp_admin_bar ) ||
839 + ! $wp_admin_bar->get_node( 'dashboard' ) ||
840 + ! current_user_can( 'view_stats' ) ||
841 + ( new Host() )->is_wpcom_platform()
842 + ) {
843 + return;
844 + }
845 +
846 + $wp_admin_bar->add_node(
847 + array(
848 + 'parent' => 'site-name',
849 + 'id' => 'jetpack-stats',
850 + 'title' => __( 'Stats', 'jetpack' ),
851 + 'href' => admin_url( 'admin.php?page=stats' ),
852 + )
853 + );
1027 854 }
1028 855
1029 856 /**
1030 857 * Stats Get Blog.
@@ -1214,9 +1041,9 @@
1214 1041 *
1215 1042 * TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
1216 1043 *
1217 1044 * @access public
1218 - * @return void
1045 + * @return never
1219 1046 */
1220 1047 function stats_dashboard_widget_content() {
1221 1048 $width = isset( $_GET['width'] ) ? intval( $_GET['width'] ) / 2 : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1222 1049 $height = isset( $_GET['height'] ) ? intval( $_GET['height'] ) - 36 : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -1255,9 +1082,9 @@
1255 1082 $user_id = 0; // Means use the blog token.
1256 1083
1257 1084 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1258 1085 $get_code = wp_remote_retrieve_response_code( $get );
1259 - if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1086 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1260 1087 stats_print_wp_remote_error( $get, $url );
1261 1088 } else {
1262 1089 $body = stats_convert_post_titles( $get['body'] );
1263 1090 $body = stats_convert_chart_urls( $body );
@@ -1282,9 +1109,9 @@
1282 1109 $post_ids[] = $post['post_id'];
1283 1110 }
1284 1111
1285 1112 // Cache.
1286 - get_posts( array( 'include' => join( ',', array_unique( $post_ids ) ) ) );
1113 + get_posts( array( 'include' => implode( ',', array_unique( $post_ids ) ) ) );
1287 1114
1288 1115 $searches = array();
1289 1116 $search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" );
1290 1117 foreach ( $search_terms as $search_term ) {
@@ -1360,9 +1187,9 @@
1360 1187 </div>
1361 1188 </div>
1362 1189 </div>
1363 1190 <?php
1364 - exit;
1191 + exit( 0 );
1365 1192 }
1366 1193
1367 1194 /**
1368 1195 * Stats Print WP Remote Error.
@@ -1374,9 +1201,9 @@
1374 1201 */
1375 1202 function stats_print_wp_remote_error( $get, $url ) {
1376 1203 $state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 );
1377 1204 $previous_error = Jetpack::state( $state_name );
1378 - $error = md5( wp_json_encode( compact( 'get', 'url' ) ) );
1205 + $error = md5( wp_json_encode( compact( 'get', 'url' ), JSON_UNESCAPED_SLASHES ) );
1379 1206 Jetpack::state( $state_name, $error );
1380 1207 if ( $error !== $previous_error ) {
1381 1208 ?>
1382 1209 <div class="wrap">
@@ -1436,8 +1263,9 @@
1436 1263 * @type string $days The length of the desired time frame. Default is 30. Maximum 90 days.
1437 1264 * @type int $limit The maximum number of records to return. Default is 10. Maximum 100.
1438 1265 * @type int $post_id The ID of the post to retrieve stats data for
1439 1266 * @type string $summarize If present, summarizes all matching records. Default Null.
1267 + * @type int $blog_id The WordPress.com blog ID to retrieve stats data for. Default is the current blog.
1440 1268 *
1441 1269 * }
1442 1270 *
1443 1271 * @return array {
@@ -1459,13 +1287,13 @@
1459 1287 'days' => false,
1460 1288 'limit' => 3,
1461 1289 'post_id' => false,
1462 1290 'summarize' => '',
1291 + 'blog_id' => Jetpack_Options::get_option( 'id' ),
1463 1292 );
1464 1293
1465 - $args = wp_parse_args( $args, $defaults );
1466 - $args['table'] = $table;
1467 - $args['blog_id'] = Jetpack_Options::get_option( 'id' );
1294 + $args = wp_parse_args( $args, $defaults );
1295 + $args['table'] = $table;
1468 1296
1469 1297 $stats_csv_url = add_query_arg( $args, 'https://stats.wordpress.com/csv.php' );
1470 1298
1471 1299 $key = md5( $stats_csv_url );
@@ -1535,9 +1363,9 @@
1535 1363 $user_id = 0; // Blog token.
1536 1364
1537 1365 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1538 1366 $get_code = wp_remote_retrieve_response_code( $get );
1539 - if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1367 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1540 1368 return array(); // @todo: return an error?
1541 1369 } else {
1542 1370 return stats_str_getcsv( $get['body'] );
1543 1371 }
@@ -1548,13 +1376,19 @@
1548 1376 *
1549 1377 * @since 9.7.0 Remove custom handling since str_getcsv is available on all servers running this now.
1550 1378 *
1551 1379 * @param mixed $csv CSV.
1552 - * @return array.
1380 + * @return array
1553 1381 */
1554 1382 function stats_str_getcsv( $csv ) {
1555 - $lines = str_getcsv( $csv, "\n" );
1556 - return array_map( 'str_getcsv', $lines );
1383 + // @todo Correctly handle embedded newlines. Note, despite claims online, `str_getcsv( $csv, "\n" )` does not actually work.
1384 + $lines = explode( "\n", rtrim( $csv, "\n" ) );
1385 + return array_map(
1386 + function ( $line ) {
1387 + return str_getcsv( $line, ',', '"', '' );
1388 + },
1389 + $lines
1390 + );
1557 1391 }
1558 1392
1559 1393 /**
1560 1394 * Abstract out building the rest api stats path.
@@ -1574,9 +1408,9 @@
1574 1408 * @access public
1575 1409 * @deprecated 11.5 Use WPCOM_Stats available methodsinstead.
1576 1410 * @param array $args (default: array()) The args that are passed to the endpoint.
1577 1411 * @param string $resource (default: '') Optional sub-endpoint following /stats/.
1578 - * @return array|WP_Error.
1412 + * @return array|WP_Error
1579 1413 */
1580 1414 function stats_get_from_restapi( $args = array(), $resource = '' ) {
1581 1415 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
1582 1416 $endpoint = jetpack_stats_api_path( $resource );
@@ -1581,9 +1415,9 @@
1581 1415 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
1582 1416 $endpoint = jetpack_stats_api_path( $resource );
1583 1417 $api_version = '1.1';
1584 1418 $args = wp_parse_args( $args, array() );
1585 - $cache_key = md5( implode( '|', array( $endpoint, $api_version, wp_json_encode( $args ) ) ) );
1419 + $cache_key = md5( implode( '|', array( $endpoint, $api_version, wp_json_encode( $args, JSON_UNESCAPED_SLASHES ) ) ) );
1586 1420
1587 1421 $transient_name = "jetpack_restapi_stats_cache_{$cache_key}";
1588 1422
1589 1423 $stats_cache = get_transient( $transient_name );
@@ -1620,93 +1454,8 @@
1620 1454 return $return;
1621 1455 }
1622 1456
1623 1457 /**
1624 - * Load CSS needed for Stats column width in WP-Admin area.
1625 - *
1626 - * @since 4.7.0
1627 - */
1628 -function jetpack_stats_load_admin_css() {
1629 - ?>
1630 - <style type="text/css">
1631 - .fixed .column-stats {
1632 - width: 5em;
1633 - }
1634 - </style>
1635 - <?php
1636 -}
1637 -
1638 -/**
1639 - * Set header for column that allows to go to WordPress.com to see an entry's stats.
1640 - *
1641 - * @param array $columns An array of column names.
1642 - *
1643 - * @since 4.7.0
1644 - *
1645 - * @return mixed
1646 - */
1647 -function jetpack_stats_post_table( $columns ) {
1648 - // Adds a stats link on the edit posts page.
1649 - if ( ! current_user_can( 'view_stats' ) || ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ) {
1650 - return $columns;
1651 - }
1652 -
1653 - // Array-Fu to add before comments.
1654 - $pos = array_search( 'comments', array_keys( $columns ), true );
1655 -
1656 - // Fallback to the last position if the post type does not support comments.
1657 - if ( ! is_int( $pos ) ) {
1658 - $pos = count( $columns );
1659 - }
1660 -
1661 - // Final fallback, if the array was malformed by another plugin for example.
1662 - if ( ! is_int( $pos ) ) {
1663 - return $columns;
1664 - }
1665 -
1666 - $chunks = array_chunk( $columns, $pos, true );
1667 - $chunks[0]['stats'] = esc_html__( 'Stats', 'jetpack' );
1668 -
1669 - return call_user_func_array( 'array_merge', $chunks );
1670 -}
1671 -
1672 -/**
1673 - * Set content for cell with link to an entry's stats in Odyssey Stats.
1674 - *
1675 - * @param string $column The name of the column to display.
1676 - * @param int $post_id The current post ID.
1677 - *
1678 - * @since 4.7.0
1679 - *
1680 - * @return mixed
1681 - */
1682 -function jetpack_stats_post_table_cell( $column, $post_id ) {
1683 - if ( 'stats' === $column ) {
1684 - if ( 'publish' !== get_post_status( $post_id ) ) {
1685 - printf(
1686 - '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
1687 - esc_html__( 'No stats', 'jetpack' )
1688 - );
1689 - } else {
1690 - $stats_post_url = ! ( new Host() )->is_woa_site() && Stats_Options::get_option( 'enable_odyssey_stats' )
1691 - ? admin_url( sprintf( 'admin.php?page=stats#!/stats/post/%d/%d', $post_id, Jetpack_Options::get_option( 'id', 0 ) ) )
1692 - : Redirect::get_url(
1693 - 'calypso-stats-post',
1694 - array(
1695 - 'path' => $post_id,
1696 - )
1697 - );
1698 -
1699 - printf(
1700 - '<a href="%s" title="%s" class="dashicons dashicons-chart-bar" target="_blank"></a>',
1701 - esc_url( $stats_post_url ),
1702 - esc_html__( 'View stats for this post', 'jetpack' )
1703 - );
1704 - }
1705 - }
1706 -}
1707 -
1708 -/**
1709 1458 * Add the Jetpack plugin version to the stats tracking data.
1710 1459 *
1711 1460 * @param array $kvs The stats array in key values.
1712 1461 * @return array
@@ -1723,14 +1472,8 @@
1723 1472 * @param array $stats_array The stats array.
1724 1473 * @return WP_Error|Object|null
1725 1474 */
1726 1475 function convert_stats_array_to_object( $stats_array ) {
1476 + _deprecated_function( __FUNCTION__, 'jetpack-13.2', 'Automattic\Jetpack\Stats\WPCOM_Stats->convert_stats_array_to_object' );
1727 1477
1728 - if ( is_wp_error( $stats_array ) ) {
1729 - return $stats_array;
1730 - }
1731 - $encoded_array = wp_json_encode( $stats_array );
1732 - if ( ! $encoded_array ) {
1733 - return new WP_Error( 'stats_encoding_error', 'Failed to encode stats array' );
1734 - }
1735 - return json_decode( $encoded_array );
1478 + return ( new WPCOM_Stats() )->convert_stats_array_to_object( $stats_array );
1736 1479 }