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