PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 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 All 506 releases
← All changes | modules/stats.php +187 -517 13.4.5 → 16.3-a.5 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,19 @@
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_Bar as Stats_Admin_Bar;
26 +use Automattic\Jetpack\Stats_Admin\Admin_Post_List_Column;
27 27 use Automattic\Jetpack\Stats_Admin\Dashboard as Stats_Dashboard;
28 28 use Automattic\Jetpack\Stats_Admin\Main as Stats_Main;
29 -use Automattic\Jetpack\Stats_Admin\Notices as Stats_Notices;
30 29 use Automattic\Jetpack\Status\Host;
31 30 use Automattic\Jetpack\Tracking;
32 31
32 +if ( ! defined( 'ABSPATH' ) ) {
33 + exit( 0 );
34 +}
35 +
33 36 if ( defined( 'STATS_DASHBOARD_SERVER' ) ) {
34 37 return;
35 38 }
36 39
@@ -53,20 +56,13 @@
53 56 */
54 57 function stats_load() {
55 58 Jetpack::enable_module_configurable( __FILE__ );
56 59
57 - add_action( 'wp_head', 'stats_admin_bar_head', 100 );
60 + Stats_Admin_Bar::init();
61 + Admin_Post_List_Column::register();
58 62
59 63 add_action( 'jetpack_admin_menu', 'stats_admin_menu' );
60 64
61 - add_filter( 'pre_option_db_version', 'stats_ignore_db_version' );
62 -
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 65 // Filter for adding the Jetpack plugin version to tracking stats.
70 66 add_filter( 'stats_array', 'filter_stats_array_add_jp_version' );
71 67
72 68 require_once __DIR__ . '/stats/class-jetpack-stats-upgrade-nudges.php';
@@ -85,24 +81,18 @@
85 81 }
86 82
87 83 /**
88 84 * Prevent sparkline img requests being redirected to upgrade.php.
89 - * See wp-admin/admin.php where it checks $wp_db_version.
90 85 *
86 + * @deprecated 16.3
87 + *
91 88 * @access public
92 89 * @param mixed $version Version.
93 90 * @return string $version.
94 91 */
95 92 function stats_ignore_db_version( $version ) {
96 - if (
97 - 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
100 - ) {
101 - global $wp_db_version;
102 - return $wp_db_version;
103 - }
104 - return $version;
93 + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Stats_Admin\Admin_Bar::ignore_db_version' );
94 + return Stats_Admin_Bar::ignore_db_version( $version );
105 95 }
106 96
107 97 /**
108 98 * Maps view_stats cap to read cap as needed.
@@ -229,9 +219,9 @@
229 219 $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 220 $relative_pos = strpos( $redirect_url, '/wp-admin/' );
231 221 if ( false !== $relative_pos ) {
232 222 wp_safe_redirect( admin_url( substr( $redirect_url, $relative_pos + 10 ) ) );
233 - exit;
223 + exit( 0 );
234 224 }
235 225 }
236 226
237 227 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -245,15 +235,24 @@
245 235 if ( ! Stats_Options::get_option( 'enable_odyssey_stats' ) || isset( $_GET['noheader'] ) ) {
246 236 // Show old Jetpack Stats interface for:
247 237 // - When the "enable_odyssey_stats" option is disabled.
248 238 // - 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' );
239 + $hook = Admin_Menu::add_menu(
240 + __( 'Stats', 'jetpack' ),
241 + __( 'Stats', 'jetpack' ),
242 + 'view_stats',
243 + 'stats',
244 + 'jetpack_admin_ui_stats_report_page_wrapper',
245 + null,
246 + array(
247 + 'product' => 'stats',
248 + 'key' => 'jetpack-stats',
249 + )
250 + );
250 251 add_action( "load-$hook", 'stats_reports_load' );
251 252 } else {
252 253 // 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' ) );
254 + Stats_Dashboard::init();
256 255 }
257 256 }
258 257
259 258 /**
@@ -293,9 +292,8 @@
293 292 // Detect if JS is on. If so, remove cookie so next page load is via JS.
294 293 add_action( 'admin_print_footer_scripts', 'stats_js_remove_stnojs_cookie' );
295 294 } elseif ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
296 295 // Normal page load. Load page content via JS.
297 - add_action( 'admin_print_footer_scripts', 'stats_js_load_page_via_ajax' );
298 296 add_action( 'admin_print_footer_scripts', 'stats_script_dismiss_nudge_handler' );
299 297 }
300 298 }
301 299
@@ -314,10 +312,10 @@
314 312 element.classList.toggle( "is-hidden" );
315 313 // Send an AJAX request.
316 314 // Note we can provide a 'postponed_for' parameter to set the delay.
317 315 // 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' ) ); ?>;
316 + let nonce = <?php echo wp_json_encode( wp_create_nonce( 'wp_rest' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?>;
317 + 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 318 let data = {
321 319 id: 'opt_in_new_stats',
322 320 status: 'postponed',
323 321 };
@@ -345,16 +343,8 @@
345 343 max-width: 1040px;
346 344 margin: 0 auto;
347 345 overflow: hidden;
348 346 }
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 347 </style>
358 348 <?php
359 349 }
360 350
@@ -368,9 +358,9 @@
368 358 $parsed = wp_parse_url( admin_url() );
369 359 ?>
370 360 <script type="text/javascript">
371 361 /* <![CDATA[ */
372 -document.cookie = 'stnojs=0; expires=Wed, 9 Mar 2011 16:55:50 UTC; path=<?php echo esc_js( $parsed['path'] ); ?>';
362 +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 363 /* ]]> */
374 364 </script>
375 365 <?php
376 366 }
@@ -375,33 +365,8 @@
375 365 <?php
376 366 }
377 367
378 368 /**
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 369 * Jetpack Admin Page Wrapper.
405 370 */
406 371 function jetpack_admin_ui_stats_report_page_wrapper() {
407 372 if ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -419,19 +384,109 @@
419 384 */
420 385 function stats_reports_page( $main_chart_only = false ) {
421 386 if ( isset( $_GET['dashboard'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
422 387 stats_dashboard_widget_content();
423 - exit; // @phan-suppress-current-line PhanPluginUnreachableCode -- Safer to include it even though stats_dashboard_widget_content() never returns.
388 + exit( 0 ); // @phan-suppress-current-line PhanPluginUnreachableCode -- Safer to include it even though stats_dashboard_widget_content() never returns.
424 389 }
425 390
426 - $blog_id = Stats_Options::get_option( 'blog_id' );
391 + $blog_id = Stats_Options::get_option( 'blog_id' );
392 + $learn_url = Redirect::get_url( 'jetpack-stats-learn-more' );
393 + $redirect_url = admin_url( 'admin.php?page=stats&enable_new_stats=1' );
394 + $stats_bg_url = plugins_url( 'images/odyssey-upgrade/background.png', JETPACK__PLUGIN_FILE );
395 + $stats_bg_gradient_url = plugins_url( 'images/odyssey-upgrade/gradient.png', JETPACK__PLUGIN_FILE );
427 396
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.
397 + if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
432 398 ?>
433 399
400 + <style>
401 + .stats-odyssey-notice {
402 + display: flex;
403 + font-size: var( --font-body );
404 +
405 + border: 1px solid var( --jp-gray-5 );
406 + border-left-color: var( --jp-black );
407 + border-left-width: 6px;
408 + border-radius: 4px;
409 +
410 + margin-top: 24px;
411 + background: white;
412 + position: relative;
413 + }
414 + .stats-odyssey-notice--content__highlighted {
415 + border-left-color: var( --jp-red );
416 + }
417 + .stats-odyssey-notice--content {
418 + padding: 24px 0 24px 30px;
419 + font-size: 2em;
420 + width: 100%;
421 + }
422 + .stats-odyssey-notice--content-header {
423 + font-size: 24px;
424 + line-height: 32px;
425 + margin: 0;
426 + margin-bottom: 8px;
427 + }
428 + .stats-odyssey-notice--content-text {
429 + font-size: 16px;
430 + margin: 0;
431 + }
432 + .stats-odyssey-notice--image-container {
433 + background-image: url("<?php echo esc_url( $stats_bg_url ); ?>"), url("<?php echo esc_url( $stats_bg_gradient_url ); ?>");
434 + background-size: cover;
435 + padding-right: 28px;
436 + width: 100%;
437 + }
438 + .stats-odyssey-notice--close-button {
439 + position: absolute;
440 + top: 1rem;
441 + right: 1rem;
442 + background-color: transparent;
443 + border: none;
444 + cursor: pointer;
445 + }
446 + .stats-odyssey-notice--action-bar {
447 + display: flex;
448 + align-items: center;
449 + margin-top: 24px;
450 + }
451 + .stats-odyssey-notice--primary-button {
452 + margin-right: 18px;
453 + padding-left: 20px;
454 + padding-right: 20px;
455 + font-size: 16px;
456 + border-color: black;
457 + background-color: black;
458 + }
459 + .stats-odyssey-notice--primary-button:hover {
460 + border-color: #3c434a;
461 + background-color: #3c434a;
462 + }
463 + .is-primary-link {
464 + color: white;
465 + text-decoration: none;
466 + }
467 + .is-primary-link:active {
468 + color: white;
469 + }
470 + .is-primary-link:focus {
471 + color: white;
472 + box-shadow: none;
473 + outline: none;
474 + }
475 + .is-primary-link:hover {
476 + color: white;
477 + }
478 + .is-secondary-link {
479 + color: black;
480 + font-size: var( --font-body );
481 + }
482 + .is-secondary-link:hover {
483 + color: black;
484 + }
485 + .is-hidden {
486 + display: none;
487 + }
488 + </style>
434 489 <div id="jp-stats-wrap">
435 490 <div class="wrap">
436 491 <h1><?php esc_html_e( 'Jetpack Stats', 'jetpack' ); ?>
437 492 <?php
@@ -439,9 +494,9 @@
439 494 $i18n_headers = jetpack_get_module_i18n( 'stats' );
440 495 ?>
441 496 <a
442 497 style="font-size:13px;"
443 - href="<?php echo esc_url( admin_url( 'admin.php?page=jetpack#/settings?term=' . rawurlencode( $i18n_headers['name'] ) ) ); ?>"
498 + href="<?php echo esc_url( admin_url( 'admin.php?page=jetpack-settings#/settings?term=' . rawurlencode( $i18n_headers['name'] ?? '' ) ) ); ?>"
444 499 >
445 500 <?php esc_html_e( 'Configure', 'jetpack' ); ?>
446 501 </a>
447 502 <?php
@@ -446,27 +501,27 @@
446 501 </a>
447 502 <?php
448 503 endif;
449 504
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 505 ?>
462 506 </h2>
463 507 </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>
508 + <div class="wrap">
509 + <div class="stats-odyssey-notice stats-odyssey-notice--content__highlighted">
510 + <div class="stats-odyssey-notice--content">
511 + <h2 class="stats-odyssey-notice--content-header"><?php esc_html_e( 'Deprecated Jetpack Stats Experience', 'jetpack' ); ?></h2>
512 + <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>
513 + <div class="stats-odyssey-notice--action-bar">
514 + <button class="dops-button stats-odyssey-notice--primary-button">
515 + <a class="is-primary-link" href="<?php echo esc_url( $redirect_url ); ?>"><?php esc_html_e( 'Switch to new Stats', 'jetpack' ); ?></a>
516 + </button>
517 + <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>
518 + </div>
519 + </div>
520 + <div class="stats-odyssey-notice--image-container"></div>
521 + </div>
468 522 </div>
523 + <p></p>
469 524 </div>
470 525 <?php
471 526 return;
472 527 }
@@ -529,15 +584,14 @@
529 584 }
530 585 }
531 586 }
532 587
588 + $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
533 589 if ( isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
534 590 if ( preg_match( '/^[a-z0-9-]+$/', $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
535 591 $chart = sanitize_title( $_GET['chart'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
536 592 $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-includes/charts/{$chart}.php";
537 593 }
538 - } else {
539 - $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
540 594 }
541 595
542 596 $url = add_query_arg( $q, $url );
543 597 $method = 'GET';
@@ -545,34 +599,19 @@
545 599 $user_id = 0; // Means use the blog token.
546 600
547 601 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
548 602 $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'] ) ) {
603 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
550 604 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 - }
605 + } elseif ( ! empty( $get['headers']['content-type'] ) ) {
606 + $type = $get['headers']['content-type'];
607 + if ( str_starts_with( $type, 'image' ) ) {
608 + $img = $get['body'];
609 + header( 'Content-Type: ' . $type );
610 + header( 'Content-Length: ' . strlen( $img ) );
611 + echo $img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
612 + die( 0 );
561 613 }
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 614 }
576 615
577 616 if ( isset( $_GET['page'] ) && 'stats' === $_GET['page'] && ! isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
578 617 $tracking = new Tracking();
@@ -579,246 +618,13 @@
579 618 $tracking->record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) );
580 619 }
581 620
582 621 if ( isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
583 - die;
622 + die( 0 );
584 623 }
585 624 }
586 625
587 626 /**
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 627 * Stats Convert Image URLs.
822 628 *
823 629 * @access public
824 630 * @param mixed $html HTML.
@@ -919,63 +725,30 @@
919 725
920 726 /**
921 727 * Stats Admin Bar Head.
922 728 *
729 + * @deprecated 16.3
730 + *
923 731 * @access public
924 732 * @return void
925 733 */
926 734 function stats_admin_bar_head() {
927 - if ( ! Stats_Options::get_option( 'admin_bar' ) ) {
928 - return;
929 - }
930 -
931 - if ( ! current_user_can( 'view_stats' ) ) {
932 - return;
933 - }
934 -
935 - if ( ! is_admin_bar_showing() ) {
936 - return;
937 - }
938 -
939 - add_action( 'admin_bar_menu', 'stats_admin_bar_menu', 100 );
940 - ?>
941 -
942 -<style data-ampdevmode type='text/css'>
943 -#wpadminbar .quicklinks li#wp-admin-bar-stats {
944 - height: 32px;
735 + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Stats_Admin\Admin_Bar::maybe_add_chart' );
736 + Stats_Admin_Bar::maybe_add_chart();
945 737 }
946 -#wpadminbar .quicklinks li#wp-admin-bar-stats a {
947 - height: 32px;
948 - padding: 0;
949 -}
950 -#wpadminbar .quicklinks li#wp-admin-bar-stats a div {
951 - height: 32px;
952 - width: 95px;
953 - overflow: hidden;
954 - margin: 0 10px;
955 -}
956 -#wpadminbar .quicklinks li#wp-admin-bar-stats a:hover div {
957 - width: auto;
958 - margin: 0 8px 0 10px;
959 -}
960 -#wpadminbar .quicklinks li#wp-admin-bar-stats a img {
961 - height: 24px;
962 - margin: 4px 0;
963 - max-width: none;
964 - border: none;
965 -}
966 -</style>
967 - <?php
968 -}
969 738
970 739 /**
971 740 * Gets the image source of the given stats chart.
972 741 *
742 + * @deprecated 16.3
743 + *
973 744 * @param string $chart Name of the chart.
974 745 * @param array $args Extra list of argument to use in the image source.
975 746 * @return string An image source.
976 747 */
977 748 function stats_get_image_chart_src( $chart, $args = array() ) {
749 + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Stats_Admin\Admin_Bar::get_chart_src' );
750 +
978 751 $url = add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) );
979 752
980 753 return add_query_arg(
981 754 array_merge(
@@ -992,41 +765,30 @@
992 765
993 766 /**
994 767 * Stats AdminBar.
995 768 *
769 + * @deprecated 16.3
770 + *
996 771 * @access public
997 772 * @param mixed $wp_admin_bar WPAdminBar.
998 773 * @return void
999 774 */
1000 775 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' ) );
1005 -
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 - );
1011 -
1012 - $wp_admin_bar->add_menu( $menu );
776 + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Stats_Admin\Admin_Bar::add_chart_node' );
777 + Stats_Admin_Bar::add_chart_node( $wp_admin_bar );
1013 778 }
1014 779
1015 780 /**
781 + * Adds a Stats link to the site-name admin bar submenu, alongside Dashboard.
1016 782 *
1017 - * Deprecated. The stats module should not update blog details. This is handled by Sync.
783 + * @deprecated 16.3
1018 784 *
1019 - * Stats Update Blog.
1020 - *
1021 785 * @access public
1022 786 * @return void
1023 - *
1024 - * @deprecated since 10.3.
1025 787 */
1026 -function stats_update_blog() {
1027 - _deprecated_function( __METHOD__, 'jetpack-10.3' );
1028 - XMLRPC_Async_Call::add_call( 'jetpack.updateBlog', 0, stats_get_blog() );
788 +function stats_add_link_to_admin_bar_site_menu() {
789 + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Stats_Admin\Admin_Bar::add_site_menu_link' );
790 + Stats_Admin_Bar::add_site_menu_link();
1029 791 }
1030 792
1031 793 /**
1032 794 * Stats Get Blog.
@@ -1257,9 +1019,9 @@
1257 1019 $user_id = 0; // Means use the blog token.
1258 1020
1259 1021 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1260 1022 $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'] ) ) {
1023 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1262 1024 stats_print_wp_remote_error( $get, $url );
1263 1025 } else {
1264 1026 $body = stats_convert_post_titles( $get['body'] );
1265 1027 $body = stats_convert_chart_urls( $body );
@@ -1362,9 +1124,9 @@
1362 1124 </div>
1363 1125 </div>
1364 1126 </div>
1365 1127 <?php
1366 - exit;
1128 + exit( 0 );
1367 1129 }
1368 1130
1369 1131 /**
1370 1132 * Stats Print WP Remote Error.
@@ -1376,9 +1138,9 @@
1376 1138 */
1377 1139 function stats_print_wp_remote_error( $get, $url ) {
1378 1140 $state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 );
1379 1141 $previous_error = Jetpack::state( $state_name );
1380 - $error = md5( wp_json_encode( compact( 'get', 'url' ) ) );
1142 + $error = md5( wp_json_encode( compact( 'get', 'url' ), JSON_UNESCAPED_SLASHES ) );
1381 1143 Jetpack::state( $state_name, $error );
1382 1144 if ( $error !== $previous_error ) {
1383 1145 ?>
1384 1146 <div class="wrap">
@@ -1438,8 +1200,9 @@
1438 1200 * @type string $days The length of the desired time frame. Default is 30. Maximum 90 days.
1439 1201 * @type int $limit The maximum number of records to return. Default is 10. Maximum 100.
1440 1202 * @type int $post_id The ID of the post to retrieve stats data for
1441 1203 * @type string $summarize If present, summarizes all matching records. Default Null.
1204 + * @type int $blog_id The WordPress.com blog ID to retrieve stats data for. Default is the current blog.
1442 1205 *
1443 1206 * }
1444 1207 *
1445 1208 * @return array {
@@ -1461,13 +1224,13 @@
1461 1224 'days' => false,
1462 1225 'limit' => 3,
1463 1226 'post_id' => false,
1464 1227 'summarize' => '',
1228 + 'blog_id' => Jetpack_Options::get_option( 'id' ),
1465 1229 );
1466 1230
1467 - $args = wp_parse_args( $args, $defaults );
1468 - $args['table'] = $table;
1469 - $args['blog_id'] = Jetpack_Options::get_option( 'id' );
1231 + $args = wp_parse_args( $args, $defaults );
1232 + $args['table'] = $table;
1470 1233
1471 1234 $stats_csv_url = add_query_arg( $args, 'https://stats.wordpress.com/csv.php' );
1472 1235
1473 1236 $key = md5( $stats_csv_url );
@@ -1537,9 +1300,9 @@
1537 1300 $user_id = 0; // Blog token.
1538 1301
1539 1302 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1540 1303 $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'] ) ) {
1304 + if ( is_wp_error( $get ) || $get_code === '' || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) {
1542 1305 return array(); // @todo: return an error?
1543 1306 } else {
1544 1307 return stats_str_getcsv( $get['body'] );
1545 1308 }
@@ -1553,10 +1316,16 @@
1553 1316 * @param mixed $csv CSV.
1554 1317 * @return array
1555 1318 */
1556 1319 function stats_str_getcsv( $csv ) {
1557 - $lines = str_getcsv( $csv, "\n" );
1558 - return array_map( 'str_getcsv', $lines );
1320 + // @todo Correctly handle embedded newlines. Note, despite claims online, `str_getcsv( $csv, "\n" )` does not actually work.
1321 + $lines = explode( "\n", rtrim( $csv, "\n" ) );
1322 + return array_map(
1323 + function ( $line ) {
1324 + return str_getcsv( $line, ',', '"', '' );
1325 + },
1326 + $lines
1327 + );
1559 1328 }
1560 1329
1561 1330 /**
1562 1331 * Abstract out building the rest api stats path.
@@ -1583,9 +1352,9 @@
1583 1352 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
1584 1353 $endpoint = jetpack_stats_api_path( $resource );
1585 1354 $api_version = '1.1';
1586 1355 $args = wp_parse_args( $args, array() );
1587 - $cache_key = md5( implode( '|', array( $endpoint, $api_version, wp_json_encode( $args ) ) ) );
1356 + $cache_key = md5( implode( '|', array( $endpoint, $api_version, wp_json_encode( $args, JSON_UNESCAPED_SLASHES ) ) ) );
1588 1357
1589 1358 $transient_name = "jetpack_restapi_stats_cache_{$cache_key}";
1590 1359
1591 1360 $stats_cache = get_transient( $transient_name );
@@ -1619,107 +1388,8 @@
1619 1388 // To reduce size in storage: store with time as key, store JSON encoded data (unless error).
1620 1389 set_transient( $transient_name, array( time() => $data ), 5 * MINUTE_IN_SECONDS );
1621 1390
1622 1391 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 1392 }
1723 1393
1724 1394 /**
1725 1395 * Add the Jetpack plugin version to the stats tracking data.