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