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