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