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