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