PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
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 +246 -600 12.0.3 → 16.3-a.7 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,27 @@
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_Bar as Stats_Admin_Bar;
26 +use Automattic\Jetpack\Stats_Admin\Admin_Post_List_Column;
25 27 use Automattic\Jetpack\Stats_Admin\Dashboard as Stats_Dashboard;
26 28 use Automattic\Jetpack\Stats_Admin\Main as Stats_Main;
27 -use Automattic\Jetpack\Stats_Admin\Notices as Stats_Notices;
28 29 use Automattic\Jetpack\Status\Host;
29 30 use Automattic\Jetpack\Tracking;
30 31
32 +if ( ! defined( 'ABSPATH' ) ) {
33 + exit( 0 );
34 +}
35 +
31 36 if ( defined( 'STATS_DASHBOARD_SERVER' ) ) {
32 37 return;
33 38 }
34 39
@@ -51,20 +56,13 @@
51 56 */
52 57 function stats_load() {
53 58 Jetpack::enable_module_configurable( __FILE__ );
54 59
55 - add_action( 'wp_head', 'stats_admin_bar_head', 100 );
60 + Stats_Admin_Bar::init();
61 + Admin_Post_List_Column::register();
56 62
57 63 add_action( 'jetpack_admin_menu', 'stats_admin_menu' );
58 64
59 - add_filter( 'pre_option_db_version', 'stats_ignore_db_version' );
60 -
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 65 // Filter for adding the Jetpack plugin version to tracking stats.
68 66 add_filter( 'stats_array', 'filter_stats_array_add_jp_version' );
69 67
70 68 require_once __DIR__ . '/stats/class-jetpack-stats-upgrade-nudges.php';
@@ -83,24 +81,18 @@
83 81 }
84 82
85 83 /**
86 84 * Prevent sparkline img requests being redirected to upgrade.php.
87 - * See wp-admin/admin.php where it checks $wp_db_version.
88 85 *
86 + * @deprecated 16.3
87 + *
89 88 * @access public
90 89 * @param mixed $version Version.
91 90 * @return string $version.
92 91 */
93 92 function stats_ignore_db_version( $version ) {
94 - if (
95 - is_admin() &&
96 - isset( $_GET['page'] ) && 'stats' === $_GET['page'] && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
97 - isset( $_GET['chart'] ) && strpos( $_GET['chart'], 'admin-bar-hours' ) === 0 // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
98 - ) {
99 - global $wp_db_version;
100 - return $wp_db_version;
101 - }
102 - 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 );
103 95 }
104 96
105 97 /**
106 98 * Maps view_stats cap to read cap as needed.
@@ -134,9 +126,9 @@
134 126 * Stats Build View Data.
135 127 *
136 128 * @deprecated 11.5
137 129 * @access public
138 - * @return array.
130 + * @return array
139 131 */
140 132 function stats_build_view_data() {
141 133 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Tracking_Pixel::build_view_data' );
142 134 return Stats_Tracking_Pixel::build_view_data();
@@ -142,50 +134,14 @@
142 134 return Stats_Tracking_Pixel::build_view_data();
143 135 }
144 136
145 137 /**
146 - * Stats Footer.
147 - *
148 - * @deprecated 11.5
149 - * @access public
150 - * @return void
151 - */
152 -function stats_footer() {
153 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Tracking_Pixel::add_to_footer' );
154 - Stats_Tracking_Pixel::add_to_footer();
155 -}
156 -
157 -/**
158 - * Render the stats footer
159 - *
160 - * @deprecated 11.5
161 - *
162 - * @param array $data Array of data for the JS stats tracker.
163 - */
164 -function stats_render_footer( $data ) {
165 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Tracking_Pixel::render_footer' );
166 - Stats_Tracking_Pixel::render_footer( $data );
167 -}
168 -
169 -/**
170 - * Render the stats footer for AMP output.
171 - *
172 - * @deprecated 11.5
173 - *
174 - * @param array $data Array of data for the AMP pixel tracker.
175 - */
176 -function stats_render_amp_footer( $data ) {
177 - _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Tracking_Pixel::render_amp_footer' );
178 - Stats_Tracking_Pixel::render_amp_footer( $data );
179 -}
180 -
181 -/**
182 138 * Stats Get Options.
183 139 *
184 140 * @deprecated 11.5
185 141 *
186 142 * @access public
187 - * @return array.
143 + * @return array
188 144 */
189 145 function stats_get_options() {
190 146 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::get_options' );
191 147 return Stats_Options::get_options();
@@ -197,9 +153,9 @@
197 153 * @deprecated 11.5
198 154 *
199 155 * @access public
200 156 * @param mixed $option Option.
201 - * @return mixed|null.
157 + * @return mixed|null
202 158 */
203 159 function stats_get_option( $option ) {
204 160 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::get_option' );
205 161 return Stats_Options::get_option( $option );
@@ -212,9 +168,9 @@
212 168 *
213 169 * @access public
214 170 * @param mixed $option Option.
215 171 * @param mixed $value Value.
216 - * @return bool.
172 + * @return bool
217 173 */
218 174 function stats_set_option( $option, $value ) {
219 175 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Automattic\Jetpack\Stats\Options::set_option' );
220 176 return Stats_Options::set_option( $option, $value );
@@ -263,9 +219,9 @@
263 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 );
264 220 $relative_pos = strpos( $redirect_url, '/wp-admin/' );
265 221 if ( false !== $relative_pos ) {
266 222 wp_safe_redirect( admin_url( substr( $redirect_url, $relative_pos + 10 ) ) );
267 - exit;
223 + exit( 0 );
268 224 }
269 225 }
270 226
271 227 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -275,20 +231,28 @@
275 231 Stats_Main::update_new_stats_status( true );
276 232 }
277 233
278 234 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
279 - if ( ( new Host() )->is_woa_site() || ! Stats_Options::get_option( 'enable_odyssey_stats' ) || isset( $_GET['noheader'] ) ) {
235 + if ( ! Stats_Options::get_option( 'enable_odyssey_stats' ) || isset( $_GET['noheader'] ) ) {
280 236 // Show old Jetpack Stats interface for:
281 - // - Atomic sites.
282 237 // - When the "enable_odyssey_stats" option is disabled.
283 238 // - When being shown in the adminbar outside of wp-admin.
284 - $hook = add_submenu_page( 'jetpack', __( '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 + );
285 251 add_action( "load-$hook", 'stats_reports_load' );
286 252 } else {
287 253 // Enable the new Odyssey Stats experience.
288 - $stats_dashboard = new Stats_Dashboard();
289 - $hook = add_submenu_page( 'jetpack', __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'stats', array( $stats_dashboard, 'render' ) );
290 - add_action( "load-$hook", array( $stats_dashboard, 'admin_init' ) );
254 + Stats_Dashboard::init();
291 255 }
292 256 }
293 257
294 258 /**
@@ -328,9 +292,8 @@
328 292 // Detect if JS is on. If so, remove cookie so next page load is via JS.
329 293 add_action( 'admin_print_footer_scripts', 'stats_js_remove_stnojs_cookie' );
330 294 } elseif ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
331 295 // Normal page load. Load page content via JS.
332 - add_action( 'admin_print_footer_scripts', 'stats_js_load_page_via_ajax' );
333 296 add_action( 'admin_print_footer_scripts', 'stats_script_dismiss_nudge_handler' );
334 297 }
335 298 }
336 299
@@ -349,10 +312,10 @@
349 312 element.classList.toggle( "is-hidden" );
350 313 // Send an AJAX request.
351 314 // Note we can provide a 'postponed_for' parameter to set the delay.
352 315 // Without a parameter it defaults to 30 days which is what we want here.
353 - let nonce = <?php echo wp_json_encode( wp_create_nonce( 'wp_rest' ) ); ?>;
354 - 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 ); ?>;
355 318 let data = {
356 319 id: 'opt_in_new_stats',
357 320 status: 'postponed',
358 321 };
@@ -380,16 +343,8 @@
380 343 max-width: 1040px;
381 344 margin: 0 auto;
382 345 overflow: hidden;
383 346 }
384 -
385 -#stats-loading-wrap p {
386 - text-align: center;
387 - font-size: 2em;
388 - margin-bottom: 3em;
389 - height: 64px;
390 - line-height: 64px;
391 -}
392 347 </style>
393 348 <?php
394 349 }
395 350
@@ -403,9 +358,9 @@
403 358 $parsed = wp_parse_url( admin_url() );
404 359 ?>
405 360 <script type="text/javascript">
406 361 /* <![CDATA[ */
407 -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 ); ?>;
408 363 /* ]]> */
409 364 </script>
410 365 <?php
411 366 }
@@ -410,33 +365,8 @@
410 365 <?php
411 366 }
412 367
413 368 /**
414 - * Normal page load. Load page content via JS.
415 - *
416 - * @access public
417 - * @return void
418 - */
419 -function stats_js_load_page_via_ajax() {
420 - ?>
421 -<script type="text/javascript">
422 -/* <![CDATA[ */
423 -if ( -1 == document.location.href.indexOf( 'noheader' ) ) {
424 - jQuery( function( $ ) {
425 - const loadStatsUrl = new URL( document.location.href );
426 - loadStatsUrl.searchParams.append( 'noheader', 1 );
427 - $.get( loadStatsUrl.toString(), function( responseText ) {
428 - $( '#stats-loading-wrap' ).replaceWith( responseText );
429 - $( '#jp-stats-wrap' )[0].dispatchEvent( new Event( 'stats-loaded' ) );
430 - } );
431 - } );
432 -}
433 -/* ]]> */
434 -</script>
435 - <?php
436 -}
437 -
438 -/**
439 369 * Jetpack Admin Page Wrapper.
440 370 */
441 371 function jetpack_admin_ui_stats_report_page_wrapper() {
442 372 if ( ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -453,19 +383,110 @@
453 383 * @param bool $main_chart_only (default: false) Main Chart Only.
454 384 */
455 385 function stats_reports_page( $main_chart_only = false ) {
456 386 if ( isset( $_GET['dashboard'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
457 - return stats_dashboard_widget_content();
387 + stats_dashboard_widget_content();
388 + exit( 0 ); // @phan-suppress-current-line PhanPluginUnreachableCode -- Safer to include it even though stats_dashboard_widget_content() never returns.
458 389 }
459 390
460 - $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 );
461 396
462 - if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
463 - $nojs_url = add_query_arg( 'nojs', '1' );
464 - $http = is_ssl() ? 'https' : 'http';
465 - // Loading message. No JS fallback message.
397 + if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
466 398 ?>
467 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>
468 489 <div id="jp-stats-wrap">
469 490 <div class="wrap">
470 491 <h1><?php esc_html_e( 'Jetpack Stats', 'jetpack' ); ?>
471 492 <?php
@@ -473,9 +494,9 @@
473 494 $i18n_headers = jetpack_get_module_i18n( 'stats' );
474 495 ?>
475 496 <a
476 497 style="font-size:13px;"
477 - 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'] ?? '' ) ) ); ?>"
478 499 >
479 500 <?php esc_html_e( 'Configure', 'jetpack' ); ?>
480 501 </a>
481 502 <?php
@@ -480,27 +501,27 @@
480 501 </a>
481 502 <?php
482 503 endif;
483 504
484 - /**
485 - * Sets external resource URL.
486 - *
487 - * @module stats
488 - *
489 - * @since 1.4.0
490 - * @todo Clean up various uses of this filter. It's seemingly filtering different types of images in different places.
491 - *
492 - * @param string $args URL of external resource.
493 - */
494 - $static_url = apply_filters( 'jetpack_static_url', "{$http}://en.wordpress.com/i/loading/loading-64.gif" );
495 505 ?>
496 506 </h2>
497 507 </div>
498 - <div id="stats-loading-wrap" class="wrap">
499 - <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>
500 - <p class="hide-if-js"><?php esc_html_e( 'Jetpack Stats work better with JavaScript enabled.', 'jetpack' ); ?><br />
501 - <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>
502 522 </div>
523 + <p></p>
503 524 </div>
504 525 <?php
505 526 return;
506 527 }
@@ -557,21 +578,20 @@
557 578 }
558 579 } elseif ( null === $vals ) {
559 580 $q[ $var ] = '';
560 581 } elseif ( 'data' === $vals ) {
561 - if ( 'index.php' === substr( $val, 0, 9 ) ) {
582 + if ( str_starts_with( $val, 'index.php' ) ) {
562 583 $q[ $var ] = $val;
563 584 }
564 585 }
565 586 }
566 587
588 + $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
567 589 if ( isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
568 590 if ( preg_match( '/^[a-z0-9-]+$/', $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
569 591 $chart = sanitize_title( $_GET['chart'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
570 592 $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-includes/charts/{$chart}.php";
571 593 }
572 - } else {
573 - $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php';
574 594 }
575 595
576 596 $url = add_query_arg( $q, $url );
577 597 $method = 'GET';
@@ -579,34 +599,19 @@
579 599 $user_id = 0; // Means use the blog token.
580 600
581 601 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
582 602 $get_code = wp_remote_retrieve_response_code( $get );
583 - 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'] ) ) {
584 604 stats_print_wp_remote_error( $get, $url );
585 - } else {
586 - if ( ! empty( $get['headers']['content-type'] ) ) {
587 - $type = $get['headers']['content-type'];
588 - if ( substr( $type, 0, 5 ) === 'image' ) {
589 - $img = $get['body'];
590 - header( 'Content-Type: ' . $type );
591 - header( 'Content-Length: ' . strlen( $img ) );
592 - echo $img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
593 - die();
594 - }
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 );
595 613 }
596 - $body = stats_convert_post_titles( $get['body'] );
597 - $body = stats_convert_chart_urls( $body );
598 - $body = stats_convert_image_urls( $body );
599 - $body = stats_convert_admin_urls( $body );
600 -
601 - // The response can contain either the content to display OR
602 - // the scripts for the chart UI. The following calls inspect the
603 - // response, insert the Odyssey nudge as needed, and make sure
604 - // everything is output correctly.
605 - stats_print_header_section( $body );
606 - stats_print_odyssey_nudge( $body );
607 - stats_print_content_section( $body );
608 - stats_print_chart_scripts( $body );
609 614 }
610 615
611 616 if ( isset( $_GET['page'] ) && 'stats' === $_GET['page'] && ! isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
612 617 $tracking = new Tracking();
@@ -613,246 +618,13 @@
613 618 $tracking->record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) );
614 619 }
615 620
616 621 if ( isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
617 - die;
622 + die( 0 );
618 623 }
619 624 }
620 625
621 626 /**
622 - * Legacy Stats: Print Header Section
623 - *
624 - * @access public
625 - * @param mixed $html HTML.
626 - * @return void
627 - */
628 -function stats_print_header_section( $html ) {
629 - $header = stats_parse_header_section( $html );
630 - if ( $header !== '' ) {
631 - echo $header; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
632 - }
633 -}
634 -
635 -/**
636 - * Legacy Stats: Print Content Section
637 - *
638 - * @access public
639 - * @param mixed $html HTML.
640 - * @return void
641 - */
642 -function stats_print_content_section( $html ) {
643 - $content = stats_parse_content_section( $html );
644 - if ( $content !== '' ) {
645 - echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
646 - }
647 -}
648 -
649 -/**
650 - * Legacy Stats: Print Chart Scripts
651 - *
652 - * @access public
653 - * @param mixed $html HTML.
654 - * @return void
655 - */
656 -function stats_print_chart_scripts( $html ) {
657 - if ( is_chart_scripts( $html ) ) {
658 - echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
659 - }
660 -}
661 -
662 -/**
663 - * Legacy Stats: Test for presence of chart scripts
664 - *
665 - * @access public
666 - * @param mixed $html Input HTML that may or may not include the chart scripts.
667 - * @return bool
668 - */
669 -function is_chart_scripts( $html ) {
670 - $pos = strpos( $html, STATS_CONTENT_MARKER );
671 - return $pos === false;
672 -}
673 -
674 -/**
675 - * Legacy Stats: Parse Header Section
676 - *
677 - * Returns the section of the content up to and including the date header.
678 - *
679 - * @access public
680 - * @param mixed $html HTML.
681 - * @return string
682 - */
683 -function stats_parse_header_section( $html ) {
684 - $head = strstr( $html, STATS_CONTENT_MARKER, true );
685 - // Enforce a string result instead of string|false.
686 - if ( $head === false ) {
687 - return '';
688 - }
689 - return $head;
690 -}
691 -
692 -/**
693 - * Legacy Stats: Print Content Section
694 - *
695 - * Returns the section of the content excluding the date header.
696 - *
697 - * @access public
698 - * @param mixed $html HTML.
699 - * @return string
700 - */
701 -function stats_parse_content_section( $html ) {
702 - $body = strstr( $html, STATS_BODY_MARKER );
703 - // Enforce a string result instead of string|false.
704 - if ( $body === false ) {
705 - return '';
706 - }
707 - return $body;
708 -}
709 -
710 -/**
711 - * Legacy Stats: Determine if we need to show the Odyssey upgrade nudge.
712 - *
713 - * @access public
714 - * @return boolean
715 - */
716 -function stats_should_show_odyssey_nudge() {
717 - $stats_notices = ( new Stats_Notices() )->get_notices_to_show();
718 - return isset( $stats_notices[ Stats_Notices::OPT_IN_NEW_STATS_NOTICE_ID ] )
719 - && $stats_notices[ Stats_Notices::OPT_IN_NEW_STATS_NOTICE_ID ];
720 -}
721 -
722 -/**
723 - * Legacy Stats: Print the Odyssey upgrade nudge.
724 - *
725 - * @access public
726 - * @param mixed $html HTML.
727 - * @return void
728 - */
729 -function stats_print_odyssey_nudge( $html ) {
730 - if ( ! stats_should_show_odyssey_nudge() ) {
731 - return;
732 - }
733 - $pos = strpos( $html, STATS_CONTENT_MARKER );
734 - if ( $pos === false ) {
735 - return;
736 - }
737 - $learn_url = Redirect::get_url( 'jetpack-stats-learn-more' );
738 - $redirect_url = admin_url( 'admin.php?page=stats&enable_new_stats=1' );
739 - ?>
740 - <style>
741 - .stats-odyssey-notice {
742 - display: flex;
743 - font-size: var( --font-body );
744 -
745 - border: 1px solid var( --jp-gray-5 );
746 - border-left-color: var( --jp-black );
747 - border-left-width: 6px;
748 - border-radius: 4px;
749 -
750 - margin-top: 24px;
751 - background: white;
752 - position: relative;
753 - }
754 - .stats-odyssey-notice--content {
755 - padding: 24px 0 24px 30px;
756 - font-size: 2em;
757 - width: 100%;
758 - }
759 - .stats-odyssey-notice--content-header {
760 - font-size: 24px;
761 - line-height: 32px;
762 - margin: 0;
763 - margin-bottom: 8px;
764 - }
765 - .stats-odyssey-notice--content-text {
766 - font-size: 16px;
767 - margin: 0;
768 - }
769 - .stats-odyssey-notice--image-container {
770 - background-image: url("/wp-content/plugins/jetpack/images/odyssey-upgrade/background.png"), url("/wp-content/plugins/jetpack/images/odyssey-upgrade/gradient.png");
771 - background-size: cover;
772 - padding-right: 28px;
773 - width: 100%;
774 - }
775 - .stats-odyssey-notice--close-button {
776 - position: absolute;
777 - top: 1rem;
778 - right: 1rem;
779 - background-color: transparent;
780 - border: none;
781 - cursor: pointer;
782 - }
783 - .stats-odyssey-notice--action-bar {
784 - display: flex;
785 - align-items: center;
786 - margin-top: 24px;
787 - }
788 - .stats-odyssey-notice--primary-button {
789 - margin-right: 18px;
790 - padding-left: 20px;
791 - padding-right: 20px;
792 - font-size: 16px;
793 - border-color: black;
794 - background-color: black;
795 - }
796 - .stats-odyssey-notice--primary-button:hover {
797 - border-color: #3c434a;
798 - background-color: #3c434a;
799 - }
800 - .is-primary-link {
801 - color: white;
802 - text-decoration: none;
803 - }
804 - .is-primary-link:active {
805 - color: white;
806 - }
807 - .is-primary-link:focus {
808 - color: white;
809 - box-shadow: none;
810 - outline: none;
811 - }
812 - .is-primary-link:hover {
813 - color: white;
814 - }
815 - .is-secondary-link {
816 - color: black;
817 - font-size: var( --font-body );
818 - }
819 - .is-secondary-link:hover {
820 - color: black;
821 - }
822 - .is-hidden {
823 - display: none;
824 - }
825 - </style>
826 - <div id="stats-odyssey-nudge-main" class="stats-odyssey-notice">
827 - <div class="stats-odyssey-notice--content">
828 - <h2 class="stats-odyssey-notice--content-header"><?php esc_html_e( 'Explore the new Jetpack Stats', 'jetpack' ); ?></h2>
829 - <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>
830 - <div class="stats-odyssey-notice--action-bar">
831 - <button class="dops-button stats-odyssey-notice--primary-button">
832 - <a class="is-primary-link" href="<?php echo esc_url( $redirect_url ); ?>"><?php esc_html_e( 'Switch to new Stats', 'jetpack' ); ?></a>
833 - </button>
834 - <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>
835 - </div>
836 - </div>
837 - <div class="stats-odyssey-notice--image-container"></div>
838 - <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>
839 - </div>
840 - <?php
841 -}
842 -
843 -/**
844 - * Stats Convert Admin Urls.
845 - *
846 - * @access public
847 - * @param mixed $html HTML.
848 - * @return string
849 - */
850 -function stats_convert_admin_urls( $html ) {
851 - return str_replace( 'index.php?page=stats', 'admin.php?page=stats', $html );
852 -}
853 -
854 -/**
855 627 * Stats Convert Image URLs.
856 628 *
857 629 * @access public
858 630 * @param mixed $html HTML.
@@ -953,63 +725,30 @@
953 725
954 726 /**
955 727 * Stats Admin Bar Head.
956 728 *
729 + * @deprecated 16.3
730 + *
957 731 * @access public
958 732 * @return void
959 733 */
960 734 function stats_admin_bar_head() {
961 - if ( ! Stats_Options::get_option( 'admin_bar' ) ) {
962 - return;
963 - }
964 -
965 - if ( ! current_user_can( 'view_stats' ) ) {
966 - return;
967 - }
968 -
969 - if ( ! is_admin_bar_showing() ) {
970 - return;
971 - }
972 -
973 - add_action( 'admin_bar_menu', 'stats_admin_bar_menu', 100 );
974 - ?>
975 -
976 -<style data-ampdevmode type='text/css'>
977 -#wpadminbar .quicklinks li#wp-admin-bar-stats {
978 - 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();
979 737 }
980 -#wpadminbar .quicklinks li#wp-admin-bar-stats a {
981 - height: 32px;
982 - padding: 0;
983 -}
984 -#wpadminbar .quicklinks li#wp-admin-bar-stats a div {
985 - height: 32px;
986 - width: 95px;
987 - overflow: hidden;
988 - margin: 0 10px;
989 -}
990 -#wpadminbar .quicklinks li#wp-admin-bar-stats a:hover div {
991 - width: auto;
992 - margin: 0 8px 0 10px;
993 -}
994 -#wpadminbar .quicklinks li#wp-admin-bar-stats a img {
995 - height: 24px;
996 - margin: 4px 0;
997 - max-width: none;
998 - border: none;
999 -}
1000 -</style>
1001 - <?php
1002 -}
1003 738
1004 739 /**
1005 740 * Gets the image source of the given stats chart.
1006 741 *
742 + * @deprecated 16.3
743 + *
1007 744 * @param string $chart Name of the chart.
1008 745 * @param array $args Extra list of argument to use in the image source.
1009 746 * @return string An image source.
1010 747 */
1011 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 +
1012 751 $url = add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) );
1013 752
1014 753 return add_query_arg(
1015 754 array_merge(
@@ -1026,41 +765,30 @@
1026 765
1027 766 /**
1028 767 * Stats AdminBar.
1029 768 *
769 + * @deprecated 16.3
770 + *
1030 771 * @access public
1031 772 * @param mixed $wp_admin_bar WPAdminBar.
1032 773 * @return void
1033 774 */
1034 775 function stats_admin_bar_menu( &$wp_admin_bar ) {
1035 - $img_src = esc_attr( stats_get_image_chart_src( 'admin-bar-hours-scale' ) );
1036 - $img_src_2x = esc_attr( stats_get_image_chart_src( 'admin-bar-hours-scale-2x' ) );
1037 - $alt = esc_attr( __( 'Stats', 'jetpack' ) );
1038 - $title = esc_attr( __( 'Views over 48 hours. Click for more Jetpack Stats.', 'jetpack' ) );
1039 -
1040 - $menu = array(
1041 - 'id' => 'stats',
1042 - 'href' => add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) ), // no menu_page_url() blog-side.
1043 - 'title' => "<div><img src='$img_src' srcset='$img_src 1x, $img_src_2x 2x' width='112' height='24' alt='$alt' title='$title'></div>",
1044 - );
1045 -
1046 - $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 );
1047 778 }
1048 779
1049 780 /**
781 + * Adds a Stats link to the site-name admin bar submenu, alongside Dashboard.
1050 782 *
1051 - * Deprecated. The stats module should not update blog details. This is handled by Sync.
783 + * @deprecated 16.3
1052 784 *
1053 - * Stats Update Blog.
1054 - *
1055 785 * @access public
1056 786 * @return void
1057 - *
1058 - * @deprecated since 10.3.
1059 787 */
1060 -function stats_update_blog() {
1061 - _deprecated_function( __METHOD__, 'jetpack-10.3' );
1062 - 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();
1063 791 }
1064 792
1065 793 /**
1066 794 * Stats Get Blog.
@@ -1250,9 +978,9 @@
1250 978 *
1251 979 * TODO: This should be moved into class-jetpack-stats-dashboard-widget.php.
1252 980 *
1253 981 * @access public
1254 - * @return void
982 + * @return never
1255 983 */
1256 984 function stats_dashboard_widget_content() {
1257 985 $width = isset( $_GET['width'] ) ? intval( $_GET['width'] ) / 2 : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1258 986 $height = isset( $_GET['height'] ) ? intval( $_GET['height'] ) - 36 : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
@@ -1291,9 +1019,9 @@
1291 1019 $user_id = 0; // Means use the blog token.
1292 1020
1293 1021 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1294 1022 $get_code = wp_remote_retrieve_response_code( $get );
1295 - 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'] ) ) {
1296 1024 stats_print_wp_remote_error( $get, $url );
1297 1025 } else {
1298 1026 $body = stats_convert_post_titles( $get['body'] );
1299 1027 $body = stats_convert_chart_urls( $body );
@@ -1318,9 +1046,9 @@
1318 1046 $post_ids[] = $post['post_id'];
1319 1047 }
1320 1048
1321 1049 // Cache.
1322 - get_posts( array( 'include' => join( ',', array_unique( $post_ids ) ) ) );
1050 + get_posts( array( 'include' => implode( ',', array_unique( $post_ids ) ) ) );
1323 1051
1324 1052 $searches = array();
1325 1053 $search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" );
1326 1054 foreach ( $search_terms as $search_term ) {
@@ -1340,63 +1068,65 @@
1340 1068 <?php esc_html_e( 'View detailed stats', 'jetpack' ); ?>
1341 1069 </a>
1342 1070 </div>
1343 1071 </div>
1344 - <div id="top-posts" class="stats-section">
1345 - <div class="stats-section-inner">
1346 - <h3 class="heading"><?php esc_html_e( 'Top Posts', 'jetpack' ); ?></h3>
1347 - <?php
1348 - if ( empty( $top_posts ) ) {
1349 - ?>
1350 - <p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
1072 + <div class="stats-info-content">
1073 + <div id="top-posts" class="stats-section">
1074 + <div class="stats-section-inner">
1075 + <h3 class="heading"><?php esc_html_e( 'Top Posts', 'jetpack' ); ?></h3>
1351 1076 <?php
1352 - } else {
1353 - foreach ( $top_posts as $post ) {
1354 - if ( ! get_post( $post['post_id'] ) ) {
1355 - continue;
1356 - }
1077 + if ( empty( $top_posts ) ) {
1357 1078 ?>
1358 - <p>
1079 + <p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
1359 1080 <?php
1360 - printf(
1361 - esc_html(
1362 - /* Translators: Stats dashboard widget Post list with view count: "Post Title 1 View (or Views if plural)". */
1363 - _n( '%1$s %2$s View', '%1$s %2$s Views', $post['views'], 'jetpack' )
1364 - ),
1365 - '<a href="' . esc_url( get_permalink( $post['post_id'] ) ) . '">' . esc_html( get_the_title( $post['post_id'] ) ) . '</a>',
1366 - esc_html( number_format_i18n( $post['views'] ) )
1367 - );
1081 + } else {
1082 + foreach ( $top_posts as $post ) {
1083 + if ( ! get_post( $post['post_id'] ) ) {
1084 + continue;
1085 + }
1086 + ?>
1087 + <p>
1088 + <?php
1089 + printf(
1090 + esc_html(
1091 + /* Translators: Stats dashboard widget Post list with view count: "Post Title 1 View (or Views if plural)". */
1092 + _n( '%1$s %2$s View', '%1$s %2$s Views', $post['views'], 'jetpack' )
1093 + ),
1094 + '<a href="' . esc_url( get_permalink( $post['post_id'] ) ) . '">' . esc_html( get_the_title( $post['post_id'] ) ) . '</a>',
1095 + esc_html( number_format_i18n( $post['views'] ) )
1096 + );
1097 + ?>
1098 + </p>
1099 + <?php
1100 + }
1101 + }
1102 + ?>
1103 + </div>
1104 + </div>
1105 + <div id="top-search" class="stats-section">
1106 + <div class="stats-section-inner">
1107 + <h3 class="heading"><?php esc_html_e( 'Top Searches', 'jetpack' ); ?></h3>
1108 + <?php
1109 + if ( empty( $searches ) ) {
1368 1110 ?>
1369 - </p>
1111 + <p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
1370 1112 <?php
1113 + } else {
1114 + foreach ( $searches as $search_term_item ) {
1115 + printf(
1116 + '<p>%s</p>',
1117 + esc_html( $search_term_item )
1118 + );
1119 + }
1371 1120 }
1372 - }
1373 - ?>
1374 - </div>
1375 - </div>
1376 - <div id="top-search" class="stats-section">
1377 - <div class="stats-section-inner">
1378 - <h3 class="heading"><?php esc_html_e( 'Top Searches', 'jetpack' ); ?></h3>
1379 - <?php
1380 - if ( empty( $searches ) ) {
1381 1121 ?>
1382 - <p class="nothing"><?php esc_html_e( 'Sorry, nothing to report.', 'jetpack' ); ?></p>
1383 - <?php
1384 - } else {
1385 - foreach ( $searches as $search_term_item ) {
1386 - printf(
1387 - '<p>%s</p>',
1388 - esc_html( $search_term_item )
1389 - );
1390 - }
1391 - }
1392 - ?>
1122 + </div>
1393 1123 </div>
1394 1124 </div>
1395 1125 </div>
1396 1126 </div>
1397 1127 <?php
1398 - exit;
1128 + exit( 0 );
1399 1129 }
1400 1130
1401 1131 /**
1402 1132 * Stats Print WP Remote Error.
@@ -1408,9 +1138,9 @@
1408 1138 */
1409 1139 function stats_print_wp_remote_error( $get, $url ) {
1410 1140 $state_name = 'stats_remote_error_' . substr( md5( $url ), 0, 8 );
1411 1141 $previous_error = Jetpack::state( $state_name );
1412 - $error = md5( wp_json_encode( compact( 'get', 'url' ) ) );
1142 + $error = md5( wp_json_encode( compact( 'get', 'url' ), JSON_UNESCAPED_SLASHES ) );
1413 1143 Jetpack::state( $state_name, $error );
1414 1144 if ( $error !== $previous_error ) {
1415 1145 ?>
1416 1146 <div class="wrap">
@@ -1470,8 +1200,9 @@
1470 1200 * @type string $days The length of the desired time frame. Default is 30. Maximum 90 days.
1471 1201 * @type int $limit The maximum number of records to return. Default is 10. Maximum 100.
1472 1202 * @type int $post_id The ID of the post to retrieve stats data for
1473 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.
1474 1205 *
1475 1206 * }
1476 1207 *
1477 1208 * @return array {
@@ -1493,13 +1224,13 @@
1493 1224 'days' => false,
1494 1225 'limit' => 3,
1495 1226 'post_id' => false,
1496 1227 'summarize' => '',
1228 + 'blog_id' => Jetpack_Options::get_option( 'id' ),
1497 1229 );
1498 1230
1499 - $args = wp_parse_args( $args, $defaults );
1500 - $args['table'] = $table;
1501 - $args['blog_id'] = Jetpack_Options::get_option( 'id' );
1231 + $args = wp_parse_args( $args, $defaults );
1232 + $args['table'] = $table;
1502 1233
1503 1234 $stats_csv_url = add_query_arg( $args, 'https://stats.wordpress.com/csv.php' );
1504 1235
1505 1236 $key = md5( $stats_csv_url );
@@ -1569,9 +1300,9 @@
1569 1300 $user_id = 0; // Blog token.
1570 1301
1571 1302 $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) );
1572 1303 $get_code = wp_remote_retrieve_response_code( $get );
1573 - 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'] ) ) {
1574 1305 return array(); // @todo: return an error?
1575 1306 } else {
1576 1307 return stats_str_getcsv( $get['body'] );
1577 1308 }
@@ -1582,13 +1313,19 @@
1582 1313 *
1583 1314 * @since 9.7.0 Remove custom handling since str_getcsv is available on all servers running this now.
1584 1315 *
1585 1316 * @param mixed $csv CSV.
1586 - * @return array.
1317 + * @return array
1587 1318 */
1588 1319 function stats_str_getcsv( $csv ) {
1589 - $lines = str_getcsv( $csv, "\n" );
1590 - 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 + );
1591 1328 }
1592 1329
1593 1330 /**
1594 1331 * Abstract out building the rest api stats path.
@@ -1608,9 +1345,9 @@
1608 1345 * @access public
1609 1346 * @deprecated 11.5 Use WPCOM_Stats available methodsinstead.
1610 1347 * @param array $args (default: array()) The args that are passed to the endpoint.
1611 1348 * @param string $resource (default: '') Optional sub-endpoint following /stats/.
1612 - * @return array|WP_Error.
1349 + * @return array|WP_Error
1613 1350 */
1614 1351 function stats_get_from_restapi( $args = array(), $resource = '' ) {
1615 1352 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
1616 1353 $endpoint = jetpack_stats_api_path( $resource );
@@ -1615,9 +1352,9 @@
1615 1352 _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' );
1616 1353 $endpoint = jetpack_stats_api_path( $resource );
1617 1354 $api_version = '1.1';
1618 1355 $args = wp_parse_args( $args, array() );
1619 - $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 ) ) ) );
1620 1357
1621 1358 $transient_name = "jetpack_restapi_stats_cache_{$cache_key}";
1622 1359
1623 1360 $stats_cache = get_transient( $transient_name );
@@ -1654,93 +1391,8 @@
1654 1391 return $return;
1655 1392 }
1656 1393
1657 1394 /**
1658 - * Load CSS needed for Stats column width in WP-Admin area.
1659 - *
1660 - * @since 4.7.0
1661 - */
1662 -function jetpack_stats_load_admin_css() {
1663 - ?>
1664 - <style type="text/css">
1665 - .fixed .column-stats {
1666 - width: 5em;
1667 - }
1668 - </style>
1669 - <?php
1670 -}
1671 -
1672 -/**
1673 - * Set header for column that allows to go to WordPress.com to see an entry's stats.
1674 - *
1675 - * @param array $columns An array of column names.
1676 - *
1677 - * @since 4.7.0
1678 - *
1679 - * @return mixed
1680 - */
1681 -function jetpack_stats_post_table( $columns ) {
1682 - // Adds a stats link on the edit posts page.
1683 - if ( ! current_user_can( 'view_stats' ) || ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ) {
1684 - return $columns;
1685 - }
1686 -
1687 - // Array-Fu to add before comments.
1688 - $pos = array_search( 'comments', array_keys( $columns ), true );
1689 -
1690 - // Fallback to the last position if the post type does not support comments.
1691 - if ( ! is_int( $pos ) ) {
1692 - $pos = count( $columns );
1693 - }
1694 -
1695 - // Final fallback, if the array was malformed by another plugin for example.
1696 - if ( ! is_int( $pos ) ) {
1697 - return $columns;
1698 - }
1699 -
1700 - $chunks = array_chunk( $columns, $pos, true );
1701 - $chunks[0]['stats'] = esc_html__( 'Stats', 'jetpack' );
1702 -
1703 - return call_user_func_array( 'array_merge', $chunks );
1704 -}
1705 -
1706 -/**
1707 - * Set content for cell with link to an entry's stats in Odyssey Stats.
1708 - *
1709 - * @param string $column The name of the column to display.
1710 - * @param int $post_id The current post ID.
1711 - *
1712 - * @since 4.7.0
1713 - *
1714 - * @return mixed
1715 - */
1716 -function jetpack_stats_post_table_cell( $column, $post_id ) {
1717 - if ( 'stats' === $column ) {
1718 - if ( 'publish' !== get_post_status( $post_id ) ) {
1719 - printf(
1720 - '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
1721 - esc_html__( 'No stats', 'jetpack' )
1722 - );
1723 - } else {
1724 - $stats_post_url = ! ( new Host() )->is_woa_site() && Stats_Options::get_option( 'enable_odyssey_stats' )
1725 - ? admin_url( sprintf( 'admin.php?page=stats#!/stats/post/%d/%d', $post_id, Jetpack_Options::get_option( 'id', 0 ) ) )
1726 - : Redirect::get_url(
1727 - 'calypso-stats-post',
1728 - array(
1729 - 'path' => $post_id,
1730 - )
1731 - );
1732 -
1733 - printf(
1734 - '<a href="%s" title="%s" class="dashicons dashicons-chart-bar" target="_blank"></a>',
1735 - esc_url( $stats_post_url ),
1736 - esc_html__( 'View stats for this post', 'jetpack' )
1737 - );
1738 - }
1739 - }
1740 -}
1741 -
1742 -/**
1743 1395 * Add the Jetpack plugin version to the stats tracking data.
1744 1396 *
1745 1397 * @param array $kvs The stats array in key values.
1746 1398 * @return array
@@ -1757,14 +1409,8 @@
1757 1409 * @param array $stats_array The stats array.
1758 1410 * @return WP_Error|Object|null
1759 1411 */
1760 1412 function convert_stats_array_to_object( $stats_array ) {
1413 + _deprecated_function( __FUNCTION__, 'jetpack-13.2', 'Automattic\Jetpack\Stats\WPCOM_Stats->convert_stats_array_to_object' );
1761 1414
1762 - if ( is_wp_error( $stats_array ) ) {
1763 - return $stats_array;
1764 - }
1765 - $encoded_array = wp_json_encode( $stats_array );
1766 - if ( ! $encoded_array ) {
1767 - return new WP_Error( 'stats_encoding_error', 'Failed to encode stats array' );
1768 - }
1769 - return json_decode( $encoded_array );
1415 + return ( new WPCOM_Stats() )->convert_stats_array_to_object( $stats_array );
1770 1416 }