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 +192 -459 13.3.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
@@ -15,10 +15,8 @@
15 15 */
16 16
17 17 use Automattic\Jetpack\Admin_UI\Admin_Menu;
18 18 use Automattic\Jetpack\Connection\Client;
19 -use Automattic\Jetpack\Connection\Manager as Connection_Manager;
20 -use Automattic\Jetpack\Connection\XMLRPC_Async_Call;
21 19 use Automattic\Jetpack\Redirect;
22 20 use Automattic\Jetpack\Stats\Main as Stats;
23 21 use Automattic\Jetpack\Stats\Options as Stats_Options;
24 22 use Automattic\Jetpack\Stats\Tracking_Pixel as Stats_Tracking_Pixel;
@@ -23,14 +21,18 @@
23 21 use Automattic\Jetpack\Stats\Options as Stats_Options;
24 22 use Automattic\Jetpack\Stats\Tracking_Pixel as Stats_Tracking_Pixel;
25 23 use Automattic\Jetpack\Stats\WPCOM_Stats;
26 24 use Automattic\Jetpack\Stats\XMLRPC_Provider as Stats_XMLRPC;
25 +use Automattic\Jetpack\Stats_Admin\Admin_Post_List_Column;
27 26 use Automattic\Jetpack\Stats_Admin\Dashboard as Stats_Dashboard;
28 27 use Automattic\Jetpack\Stats_Admin\Main as Stats_Main;
29 -use Automattic\Jetpack\Stats_Admin\Notices as Stats_Notices;
30 28 use Automattic\Jetpack\Status\Host;
31 29 use Automattic\Jetpack\Tracking;
32 30
31 +if ( ! defined( 'ABSPATH' ) ) {
32 + exit( 0 );
33 +}
34 +
33 35 if ( defined( 'STATS_DASHBOARD_SERVER' ) ) {
34 36 return;
35 37 }
36 38
@@ -53,20 +55,21 @@
53 55 */
54 56 function stats_load() {
55 57 Jetpack::enable_module_configurable( __FILE__ );
56 58
57 - 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 + }
58 64
65 + Admin_Post_List_Column::register();
66 +
59 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' );
60 69
61 70 add_filter( 'pre_option_db_version', 'stats_ignore_db_version' );
62 71
63 - // Add an icon to see stats in WordPress.com for a particular post.
64 - add_action( 'admin_print_styles-edit.php', 'jetpack_stats_load_admin_css' );
65 - add_filter( 'manage_posts_columns', 'jetpack_stats_post_table' );
66 - add_filter( 'manage_pages_columns', 'jetpack_stats_post_table' );
67 - add_action( 'manage_posts_custom_column', 'jetpack_stats_post_table_cell', 10, 2 );
68 - add_action( 'manage_pages_custom_column', 'jetpack_stats_post_table_cell', 10, 2 );
69 72 // Filter for adding the Jetpack plugin version to tracking stats.
70 73 add_filter( 'stats_array', 'filter_stats_array_add_jp_version' );
71 74
72 75 require_once __DIR__ . '/stats/class-jetpack-stats-upgrade-nudges.php';
@@ -136,9 +139,9 @@
136 139 * Stats Build View Data.
137 140 *
138 141 * @deprecated 11.5
139 142 * @access public
140 - * @return array.
143 + * @return array
141 144 */
142 145 function stats_build_view_data() {
143 146 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Tracking_Pixel::build_view_data' );
144 147 return Stats_Tracking_Pixel::build_view_data();
@@ -149,9 +152,9 @@
149 152 *
150 153 * @deprecated 11.5
151 154 *
152 155 * @access public
153 - * @return array.
156 + * @return array
154 157 */
155 158 function stats_get_options() {
156 159 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::get_options' );
157 160 return Stats_Options::get_options();
@@ -163,9 +166,9 @@
163 166 * @deprecated 11.5
164 167 *
165 168 * @access public
166 169 * @param mixed $option Option.
167 - * @return mixed|null.
170 + * @return mixed|null
168 171 */
169 172 function stats_get_option( $option ) {
170 173 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::get_option' );
171 174 return Stats_Options::get_option( $option );
@@ -178,9 +181,9 @@
178 181 *
179 182 * @access public
180 183 * @param mixed $option Option.
181 184 * @param mixed $value Value.
182 - * @return bool.
185 + * @return bool
183 186 */
184 187 function stats_set_option( $option, $value ) {
185 188 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::set_option' );
186 189 return Stats_Options::set_option( $option, $value );
@@ -229,9 +232,9 @@
229 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 );
230 233 $relative_pos = strpos( $redirect_url, '/wp-admin/' );
231 234 if ( false !== $relative_pos ) {
232 235 wp_safe_redirect( admin_url( substr( $redirect_url, $relative_pos + 10 ) ) );
233 - exit;
236 + exit( 0 );
234 237 }
235 238 }
236 239
237 240 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -249,11 +252,9 @@
249 252 $hook = Admin_Menu::add_menu( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'stats', 'jetpack_admin_ui_stats_report_page_wrapper' );
250 253 add_action( "load-$hook", 'stats_reports_load' );
251 254 } else {
252 255 // 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 + Stats_Dashboard::init();
256 257 }
257 258 }
258 259
259 260 /**
@@ -293,9 +294,8 @@
293 294 // Detect if JS is on. If so, remove cookie so next page load is via JS.
294 295 add_action( 'admin_print_footer_scripts', 'stats_js_remove_stnojs_cookie' );
295 296 } elseif ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
296 297 // Normal page load. Load page content via JS.
297 - add_action( 'admin_print_footer_scripts', 'stats_js_load_page_via_ajax' );
298 298 add_action( 'admin_print_footer_scripts', 'stats_script_dismiss_nudge_handler' );
299 299 }
300 300 }
301 301
@@ -314,10 +314,10 @@
314 314 element.classList.toggle( "is-hidden" );
315 315 // Send an AJAX request.
316 316 // Note we can provide a 'postponed_for' parameter to set the delay.
317 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' ) ); ?>;
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 ); ?>;
320 320 let data = {
321 321 id: 'opt_in_new_stats',
322 322 status: 'postponed',
323 323 };
@@ -345,16 +345,8 @@
345 345 max-width: 1040px;
346 346 margin: 0 auto;
347 347 overflow: hidden;
348 348 }
349 -
350 -#stats-loading-wrap p {
351 - text-align: center;
352 - font-size: 2em;
353 - margin-bottom: 3em;
354 - height: 64px;
355 - line-height: 64px;
356 -}
357 349 </style>
358 350 <?php
359 351 }
360 352
@@ -368,9 +360,9 @@
368 360 $parsed = wp_parse_url( admin_url() );
369 361 ?>
370 362 <script type="text/javascript">
371 363 /* <![CDATA[ */
372 -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 ); ?>;
373 365 /* ]]> */
374 366 </script>
375 367 <?php
376 368 }
@@ -375,33 +367,8 @@
375 367 <?php
376 368 }
377 369
378 370 /**
379 - * Normal page load. Load page content via JS.
380 - *
381 - * @access public
382 - * @return void
383 - */
384 -function stats_js_load_page_via_ajax() {
385 - ?>
386 -<script type="text/javascript">
387 -/* <![CDATA[ */
388 -if ( -1 == document.location.href.indexOf( 'noheader' ) ) {
389 - jQuery( function( $ ) {
390 - const loadStatsUrl = new URL( document.location.href );
391 - loadStatsUrl.searchParams.append( 'noheader', 1 );
392 - $.get( loadStatsUrl.toString(), function( responseText ) {
393 - $( '#stats-loading-wrap' ).replaceWith( responseText );
394 - $( '#jp-stats-wrap' )[0].dispatchEvent( new Event( 'stats-loaded' ) );
395 - } );
396 - } );
397 -}
398 -/* ]]> */
399 -</script>
400 - <?php
401 -}
402 -
403 -/**
404 371 * Jetpack Admin Page Wrapper.
405 372 */
406 373 function jetpack_admin_ui_stats_report_page_wrapper() {
407 374 if ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -419,19 +386,109 @@
419 386 */
420 387 function stats_reports_page( $main_chart_only = false ) {
421 388 if ( isset( $_GET['dashboard'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
422 389 stats_dashboard_widget_content();
423 - exit; // @phan-suppress-current-line PhanPluginUnreachableCode -- Safer to include it even though stats_dashboard_widget_content() never returns.
390 + exit( 0 ); // @phan-suppress-current-line PhanPluginUnreachableCode -- Safer to include it even though stats_dashboard_widget_content() never returns.
424 391 }
425 392
426 - $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 );
427 398
428 - if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
429 - $nojs_url = add_query_arg( 'nojs', '1' );
430 - $http = is_ssl() ? 'https' : 'http';
431 - // Loading message. No JS fallback message.
399 + if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
432 400 ?>
433 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>
434 491 <div id="jp-stats-wrap">
435 492 <div class="wrap">
436 493 <h1><?php esc_html_e( 'Jetpack Stats', 'jetpack' ); ?>
437 494 <?php
@@ -439,9 +496,9 @@
439 496 $i18n_headers = jetpack_get_module_i18n( 'stats' );
440 497 ?>
441 498 <a
442 499 style="font-size:13px;"
443 - 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'] ?? '' ) ) ); ?>"
444 501 >
445 502 <?php esc_html_e( 'Configure', 'jetpack' ); ?>
446 503 </a>
447 504 <?php
@@ -446,27 +503,27 @@
446 503 </a>
447 504 <?php
448 505 endif;
449 506
450 - /**
451 - * Sets external resource URL.
452 - *
453 - * @module stats
454 - *
455 - * @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 - *
458 - * @param string $args URL of external resource.
459 - */
460 - $static_url = apply_filters( 'jetpack_static_url', "{$http}://en.wordpress.com/i/loading/loading-64.gif" );
461 507 ?>
462 508 </h2>
463 509 </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>
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>
468 524 </div>
525 + <p></p>
469 526 </div>
470 527 <?php
471 528 return;
472 529 }
@@ -529,15 +586,14 @@
529 586 }
530 587 }
531 588 }
532 589
590 + $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
533 591 if ( isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
534 592 if ( preg_match( '/^[a-z0-9-]+$/', $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
535 593 $chart = sanitize_title( $_GET['chart'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
536 594 $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-includes/charts/{$chart}.php";
537 595 }
538 - } else {
539 - $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
540 596 }
541 597
542 598 $url = add_query_arg( $q, $url );
543 599 $method = 'GET';
@@ -545,34 +601,19 @@
545 601 $user_id = 0; // Means use the blog token.
546 602
547 603 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
548 604 $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'] ) ) {
605 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
550 606 stats_print_wp_remote_error( $get, $url );
551 - } else {
552 - if ( ! empty( $get['headers']['content-type'] ) ) {
553 - $type = $get['headers']['content-type'];
554 - if ( str_starts_with( $type, 'image' ) ) {
555 - $img = $get['body'];
556 - header( 'Content-Type: ' . $type );
557 - header( 'Content-Length: ' . strlen( $img ) );
558 - echo $img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
559 - die();
560 - }
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 );
561 615 }
562 - $body = stats_convert_post_titles( $get['body'] );
563 - $body = stats_convert_chart_urls( $body );
564 - $body = stats_convert_image_urls( $body );
565 - $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 );
575 616 }
576 617
577 618 if ( isset( $_GET['page'] ) && 'stats' === $_GET['page'] && ! isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
578 619 $tracking = new Tracking();
@@ -579,246 +620,13 @@
579 620 $tracking->record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) );
580 621 }
581 622
582 623 if ( isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
583 - die;
624 + die( 0 );
584 625 }
585 626 }
586 627
587 628 /**
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 - * Stats Convert Admin Urls.
811 - *
812 - * @access public
813 - * @param mixed $html HTML.
814 - * @return string
815 - */
816 -function stats_convert_admin_urls( $html ) {
817 - return str_replace( 'index.php?page=stats', 'admin.php?page=stats', $html );
818 -}
819 -
820 -/**
821 629 * Stats Convert Image URLs.
822 630 *
823 631 * @access public
824 632 * @param mixed $html HTML.
@@ -923,8 +731,13 @@
923 731 * @access public
924 732 * @return void
925 733 */
926 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 +
927 740 if ( ! Stats_Options::get_option( 'admin_bar' ) ) {
928 741 return;
929 742 }
930 743
@@ -1005,9 +818,9 @@
1005 818
1006 819 $menu = array(
1007 820 'id' => 'stats',
1008 821 '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>",
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>",
1010 823 );
1011 824
1012 825 $wp_admin_bar->add_menu( $menu );
1013 826 }
@@ -1012,21 +825,33 @@
1012 825 $wp_admin_bar->add_menu( $menu );
1013 826 }
1014 827
1015 828 /**
829 + * Adds a Stats link to the site-name admin bar submenu, alongside Dashboard.
1016 830 *
1017 - * Deprecated. The stats module should not update blog details. This is handled by Sync.
1018 - *
1019 - * Stats Update Blog.
1020 - *
1021 831 * @access public
1022 832 * @return void
1023 - *
1024 - * @deprecated since 10.3.
1025 833 */
1026 -function stats_update_blog() {
1027 - _deprecated_function( __METHOD__, 'jetpack-10.3' );
1028 - 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 + );
1029 854 }
1030 855
1031 856 /**
1032 857 * Stats Get Blog.
@@ -1257,9 +1082,9 @@
1257 1082 $user_id = 0; // Means use the blog token.
1258 1083
1259 1084 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1260 1085 $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'] ) ) {
1086 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1262 1087 stats_print_wp_remote_error( $get, $url );
1263 1088 } else {
1264 1089 $body = stats_convert_post_titles( $get['body'] );
1265 1090 $body = stats_convert_chart_urls( $body );
@@ -1362,9 +1187,9 @@
1362 1187 </div>
1363 1188 </div>
1364 1189 </div>
1365 1190 <?php
1366 - exit;
1191 + exit( 0 );
1367 1192 }
1368 1193
1369 1194 /**
1370 1195 * Stats Print WP Remote Error.
@@ -1376,9 +1201,9 @@
1376 1201 */
1377 1202 function stats_print_wp_remote_error( $get, $url ) {
1378 1203 $state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 );
1379 1204 $previous_error = Jetpack::state( $state_name );
1380 - $error = md5( wp_json_encode( compact( 'get', 'url' ) ) );
1205 + $error = md5( wp_json_encode( compact( 'get', 'url' ), JSON_UNESCAPED_SLASHES ) );
1381 1206 Jetpack::state( $state_name, $error );
1382 1207 if ( $error !== $previous_error ) {
1383 1208 ?>
1384 1209 <div class="wrap">
@@ -1438,8 +1263,9 @@
1438 1263 * @type string $days The length of the desired time frame. Default is 30. Maximum 90 days.
1439 1264 * @type int $limit The maximum number of records to return. Default is 10. Maximum 100.
1440 1265 * @type int $post_id The ID of the post to retrieve stats data for
1441 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.
1442 1268 *
1443 1269 * }
1444 1270 *
1445 1271 * @return array {
@@ -1461,13 +1287,13 @@
1461 1287 'days' => false,
1462 1288 'limit' => 3,
1463 1289 'post_id' => false,
1464 1290 'summarize' => '',
1291 + 'blog_id' => Jetpack_Options::get_option( 'id' ),
1465 1292 );
1466 1293
1467 - $args = wp_parse_args( $args, $defaults );
1468 - $args['table'] = $table;
1469 - $args['blog_id'] = Jetpack_Options::get_option( 'id' );
1294 + $args = wp_parse_args( $args, $defaults );
1295 + $args['table'] = $table;
1470 1296
1471 1297 $stats_csv_url = add_query_arg( $args, 'https://stats.wordpress.com/csv.php' );
1472 1298
1473 1299 $key = md5( $stats_csv_url );
@@ -1537,9 +1363,9 @@
1537 1363 $user_id = 0; // Blog token.
1538 1364
1539 1365 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1540 1366 $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'] ) ) {
1367 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1542 1368 return array(); // @todo: return an error?
1543 1369 } else {
1544 1370 return stats_str_getcsv( $get['body'] );
1545 1371 }
@@ -1550,13 +1376,19 @@
1550 1376 *
1551 1377 * @since 9.7.0 Remove custom handling since str_getcsv is available on all servers running this now.
1552 1378 *
1553 1379 * @param mixed $csv CSV.
1554 - * @return array.
1380 + * @return array
1555 1381 */
1556 1382 function stats_str_getcsv( $csv ) {
1557 - $lines = str_getcsv( $csv, "\n" );
1558 - 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 + );
1559 1391 }
1560 1392
1561 1393 /**
1562 1394 * Abstract out building the rest api stats path.
@@ -1576,9 +1408,9 @@
1576 1408 * @access public
1577 1409 * @deprecated 11.5 Use WPCOM_Stats available methodsinstead.
1578 1410 * @param array $args (default: array()) The args that are passed to the endpoint.
1579 1411 * @param string $resource (default: '') Optional sub-endpoint following /stats/.
1580 - * @return array|WP_Error.
1412 + * @return array|WP_Error
1581 1413 */
1582 1414 function stats_get_from_restapi( $args = array(), $resource = '' ) {
1583 1415 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
1584 1416 $endpoint = jetpack_stats_api_path( $resource );
@@ -1583,9 +1415,9 @@
1583 1415 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
1584 1416 $endpoint = jetpack_stats_api_path( $resource );
1585 1417 $api_version = '1.1';
1586 1418 $args = wp_parse_args( $args, array() );
1587 - $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 ) ) ) );
1588 1420
1589 1421 $transient_name = "jetpack_restapi_stats_cache_{$cache_key}";
1590 1422
1591 1423 $stats_cache = get_transient( $transient_name );
@@ -1619,107 +1451,8 @@
1619 1451 // To reduce size in storage: store with time as key, store JSON encoded data (unless error).
1620 1452 set_transient( $transient_name, array( time() => $data ), 5 * MINUTE_IN_SECONDS );
1621 1453
1622 1454 return $return;
1623 -}
1624 -
1625 -/**
1626 - * Load CSS needed for Stats column width in WP-Admin area.
1627 - *
1628 - * @since 4.7.0
1629 - */
1630 -function jetpack_stats_load_admin_css() {
1631 - ?>
1632 - <style type="text/css">
1633 - .fixed .column-stats {
1634 - width: 5em;
1635 - }
1636 - </style>
1637 - <?php
1638 -}
1639 -
1640 -/**
1641 - * Set header for column that allows to view an entry's stats.
1642 - *
1643 - * @param array $columns An array of column names.
1644 - *
1645 - * @since 4.7.0
1646 - *
1647 - * @return mixed
1648 - */
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 - ) {
1666 - return $columns;
1667 - }
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.
1673 - 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 - return $columns;
1680 - }
1681 -
1682 - $chunks = array_chunk( $columns, $pos, true );
1683 - $chunks[0]['stats'] = esc_html__( 'Stats', 'jetpack' );
1684 -
1685 - return call_user_func_array( 'array_merge', $chunks );
1686 -}
1687 -
1688 -/**
1689 - * Set content for cell with link to an entry's stats in Odyssey Stats.
1690 - *
1691 - * @param string $column The name of the column to display.
1692 - * @param int $post_id The current post ID.
1693 - *
1694 - * @since 4.7.0
1695 - *
1696 - * @return mixed
1697 - */
1698 -function jetpack_stats_post_table_cell( $column, $post_id ) {
1699 - if ( 'stats' === $column ) {
1700 - if ( 'publish' !== get_post_status( $post_id ) ) {
1701 - printf(
1702 - '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
1703 - esc_html__( 'No stats', 'jetpack' )
1704 - );
1705 - } 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 - printf(
1716 - '<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' )
1719 - );
1720 - }
1721 - }
1722 1455 }
1723 1456
1724 1457 /**
1725 1458 * Add the Jetpack plugin version to the stats tracking data.