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