true ) ); } else { stats_reports_page(); } } /** * Stats Report Page. * * @access public * @param bool $main_chart_only (default: false) Main Chart Only. */ function stats_reports_page( $main_chart_only = false ) { if ( isset( $_GET['dashboard'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return stats_dashboard_widget_content(); } $blog_id = Stats_Options::get_option( 'blog_id' ); $stats_url = Redirect::get_url( 'calypso-stats' ); if ( ! $main_chart_only && ! isset( $_GET['noheader'] ) && empty( $_GET['nojs'] ) && empty( $_COOKIE['stnojs'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $nojs_url = add_query_arg( 'nojs', '1' ); $http = is_ssl() ? 'https' : 'http'; // Loading message. No JS fallback message. ?>

<?php esc_attr_e( 'Loading…', 'jetpack' ); ?>


.

'true', 'proxy' => '', 'page' => 'stats', 'day' => $day, 'blog' => $blog_id, 'charset' => get_option( 'blog_charset' ), 'color' => get_user_option( 'admin_color' ), 'ssl' => is_ssl(), 'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ), ); if ( get_locale() !== 'en_US' ) { $q['jp_lang'] = get_locale(); } // Only show the main chart, without extra header data, or metaboxes. $q['main_chart_only'] = $main_chart_only; $args = array( 'view' => array( 'referrers', 'postviews', 'searchterms', 'clicks', 'post', 'table' ), 'numdays' => 'int', 'day' => 'date', 'unit' => array( '1', '7', '31', 'human' ), 'humanize' => array( 'true' ), 'num' => 'int', 'summarize' => null, 'post' => 'int', 'width' => 'int', 'height' => 'int', 'data' => 'data', 'blog_subscribers' => 'int', 'comment_subscribers' => null, 'type' => array( 'wpcom', 'email', 'pending' ), 'pagenum' => 'int', 'masterbar' => null, ); foreach ( $args as $var => $vals ) { if ( ! isset( $_REQUEST[ $var ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended continue; } $val = wp_unslash( $_REQUEST[ $var ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if ( is_array( $vals ) ) { if ( in_array( $val, $vals, true ) ) { $q[ $var ] = $val; } } elseif ( 'int' === $vals ) { $q[ $var ] = (int) $val; } elseif ( 'date' === $vals ) { if ( preg_match( '/^\d{4}-\d{2}-\d{2}$/', $val ) ) { $q[ $var ] = $val; } } elseif ( null === $vals ) { $q[ $var ] = ''; } elseif ( 'data' === $vals ) { if ( 'index.php' === substr( $val, 0, 9 ) ) { $q[ $var ] = $val; } } } if ( isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( preg_match( '/^[a-z0-9-]+$/', $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput $chart = sanitize_title( $_GET['chart'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput $url = 'https://' . STATS_DASHBOARD_SERVER . "/wp-includes/charts/{$chart}.php"; } } else { $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php'; } $url = add_query_arg( $q, $url ); $method = 'GET'; $timeout = 90; $user_id = 0; // Means use the blog token. $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); $get_code = wp_remote_retrieve_response_code( $get ); if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) { stats_print_wp_remote_error( $get, $url ); } else { if ( ! empty( $get['headers']['content-type'] ) ) { $type = $get['headers']['content-type']; if ( substr( $type, 0, 5 ) === 'image' ) { $img = $get['body']; header( 'Content-Type: ' . $type ); header( 'Content-Length: ' . strlen( $img ) ); echo $img; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped die(); } } $body = stats_convert_post_titles( $get['body'] ); $body = stats_convert_chart_urls( $body ); $body = stats_convert_image_urls( $body ); $body = stats_convert_admin_urls( $body ); echo $body; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } if ( isset( $_GET['page'] ) && 'stats' === $_GET['page'] && ! isset( $_GET['chart'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $tracking = new Tracking(); $tracking->record_user_event( 'wpa_page_view', array( 'path' => 'old_stats' ) ); } if ( isset( $_GET['noheader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended die; } } /** * Stats Convert Admin Urls. * * @access public * @param mixed $html HTML. * @return string */ function stats_convert_admin_urls( $html ) { return str_replace( 'index.php?page=stats', 'admin.php?page=stats', $html ); } /** * Stats Convert Image URLs. * * @access public * @param mixed $html HTML. * @return string */ function stats_convert_image_urls( $html ) { $url = set_url_scheme( 'https://' . STATS_DASHBOARD_SERVER ); $html = preg_replace( '|(["\'])(/i/stats.+)\\1|', '$1' . $url . '$2$1', $html ); return $html; } /** * Callback for preg_replace_callback used in stats_convert_chart_urls() * * @since 5.6.0 * * @param array $matches The matches resulting from the preg_replace_callback call. * @return string The admin url for the chart. */ function jetpack_stats_convert_chart_urls_callback( $matches ) { // If there is a query string, change the beginning '?' to a '&' so it fits into the middle of this query string. return 'admin.php?page=stats&noheader&chart=' . $matches[1] . str_replace( '?', '&', $matches[2] ); } /** * Stats Convert Chart URLs. * * @access public * @param mixed $html HTML. * @return string */ function stats_convert_chart_urls( $html ) { $html = preg_replace_callback( '|https?://[-.a-z0-9]+/wp-includes/charts/([-.a-z0-9]+).php(\??)|', 'jetpack_stats_convert_chart_urls_callback', $html ); return $html; } /** * Stats Convert Post Title HTML * * @access public * @param mixed $html HTML. * @return string */ function stats_convert_post_titles( $html ) { global $stats_posts; $pattern = ".*?"; if ( ! preg_match_all( "!$pattern!", $html, $matches ) ) { return $html; } $posts = get_posts( array( 'include' => implode( ',', $matches[1] ), 'post_type' => 'any', 'post_status' => 'any', 'numberposts' => -1, 'suppress_filters' => false, ) ); foreach ( $posts as $post ) { $stats_posts[ $post->ID ] = $post; } $html = preg_replace_callback( "!$pattern!", 'stats_convert_post_title', $html ); return $html; } /** * Stats Convert Post Title Matches. * * @access public * @param mixed $matches Matches. * @return string */ function stats_convert_post_title( $matches ) { global $stats_posts; $post_id = $matches[1]; if ( isset( $stats_posts[ $post_id ] ) ) { return '' . get_the_title( $post_id ) . ''; } return $matches[0]; } /** * CSS to hide the tracking pixel smiley. * It is now hidden for everyone (used to be visible if you had set the hide_smile option). * * @access public * @return void */ function stats_hide_smile_css() { ?> '', 'proxy' => '', 'chart' => $chart, ), $args ), $url ); } /** * Stats AdminBar. * * @access public * @param mixed $wp_admin_bar WPAdminBar. * @return void */ function stats_admin_bar_menu( &$wp_admin_bar ) { $img_src = esc_attr( stats_get_image_chart_src( 'admin-bar-hours-scale' ) ); $img_src_2x = esc_attr( stats_get_image_chart_src( 'admin-bar-hours-scale-2x' ) ); $alt = esc_attr( __( 'Stats', 'jetpack' ) ); $title = esc_attr( __( 'Views over 48 hours. Click for more Jetpack Stats.', 'jetpack' ) ); $menu = array( 'id' => 'stats', 'href' => add_query_arg( 'page', 'stats', admin_url( 'admin.php' ) ), // no menu_page_url() blog-side. 'title' => "
$alt
", ); $wp_admin_bar->add_menu( $menu ); } /** * * Deprecated. The stats module should not update blog details. This is handled by Sync. * * Stats Update Blog. * * @access public * @return void * * @deprecated since 10.3. */ function stats_update_blog() { _deprecated_function( __METHOD__, 'jetpack-10.3' ); XMLRPC_Async_Call::add_call( 'jetpack.updateBlog', 0, stats_get_blog() ); } /** * Stats Get Blog. * * @deprecated 11.5 * * @access public * @return string */ function stats_get_blog() { _deprecated_function( __METHOD__, 'jetpack-11.5' ); return Stats_XMLRPC::init()->get_blog(); } /** * Stats Dashboard Widget Options. * * @access public * @return array */ function stats_dashboard_widget_options() { $defaults = array( 'chart' => 1, 'top' => 1, 'search' => 7, ); $options = get_option( 'stats_dashboard_widget' ); if ( ( ! $options ) || ! is_array( $options ) ) { $options = array(); } // Ignore obsolete option values. $intervals = array( 1, 7, 31, 90, 365 ); foreach ( array( 'top', 'search' ) as $key ) { if ( isset( $options[ $key ] ) && ! in_array( (int) $options[ $key ], $intervals, true ) ) { unset( $options[ $key ] ); } } return array_merge( $defaults, $options ); } /** * Stats Dashboard Widget Control. * * @access public * @return void */ function stats_dashboard_widget_control() { $periods = array( '1' => __( 'day', 'jetpack' ), '7' => __( 'week', 'jetpack' ), '31' => __( 'month', 'jetpack' ), ); $intervals = array( '1' => __( 'the past day', 'jetpack' ), '7' => __( 'the past week', 'jetpack' ), '31' => __( 'the past month', 'jetpack' ), '90' => __( 'the past quarter', 'jetpack' ), '365' => __( 'the past year', 'jetpack' ), ); $defaults = array( 'top' => 1, 'search' => 7, ); $options = stats_dashboard_widget_options(); 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 if ( isset( $periods[ $_POST['chart'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification $options['chart'] = filter_var( wp_unslash( $_POST['chart'] ) ); // phpcs:ignore WordPress.Security.NonceVerification } foreach ( array( 'top', 'search' ) as $key ) { if ( isset( $intervals[ $_POST[ $key ] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification $options[ $key ] = filter_var( wp_unslash( $_POST[ $key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification } else { $options[ $key ] = $defaults[ $key ]; } } update_option( 'stats_dashboard_widget', $options ); } ?>

.

.

.

'true', 'proxy' => '', 'blog' => $blog_id, 'page' => 'stats', 'chart' => '', 'unit' => $options['chart'], 'color' => get_user_option( 'admin_color' ), 'width' => $_width, 'height' => $_height, 'ssl' => is_ssl(), 'j' => sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ), ); $url = 'https://' . STATS_DASHBOARD_SERVER . '/wp-admin/index.php'; $url = add_query_arg( $q, $url ); $method = 'GET'; $timeout = 90; $user_id = 0; // Means use the blog token. $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); $get_code = wp_remote_retrieve_response_code( $get ); if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) { stats_print_wp_remote_error( $get, $url ); } else { $body = stats_convert_post_titles( $get['body'] ); $body = stats_convert_chart_urls( $body ); $body = stats_convert_image_urls( $body ); echo $body; // phpcs:ignore WordPress.Security.EscapeOutput } $post_ids = array(); $csv_end_date = current_time( 'Y-m-d' ); $csv_args = array( 'top' => "&limit=8&end=$csv_end_date", 'search' => "&limit=5&end=$csv_end_date", ); $top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" ); foreach ( $top_posts as $i => $post ) { if ( 0 === $post['post_id'] ) { unset( $top_posts[ $i ] ); continue; } $post_ids[] = $post['post_id']; } // Cache. get_posts( array( 'include' => join( ',', array_unique( $post_ids ) ) ) ); $searches = array(); $search_terms = stats_get_csv( 'searchterms', "days=$options[search]$csv_args[search]" ); foreach ( $search_terms as $search_term ) { if ( 'encrypted_search_terms' === $search_term['searchterm'] ) { continue; } $searches[] = esc_html( $search_term['searchterm'] ); } ?>

' . esc_html( get_the_title( $post['post_id'] ) ) . '', esc_html( number_format_i18n( $post['views'] ) ) ); ?>

%2$s', esc_url( $stats_day_url ), esc_html__( 'View all stats', 'jetpack' ) ); ?>

%s', esc_html__( 'Jetpack Support', 'jetpack' ) ) ); ?>

	User Agent: ""
	Page URL: "http"
	API URL: ""
	get_error_codes() as $code ) {
			foreach ( $get->get_error_messages( $code ) as $message ) {
				?>
				

				
Response code: ""
Content length: ""

		
	
false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '', ); $args = wp_parse_args( $args, $defaults ); $args['table'] = $table; $args['blog_id'] = Jetpack_Options::get_option( 'id' ); $stats_csv_url = add_query_arg( $args, 'https://stats.wordpress.com/csv.php' ); $key = md5( $stats_csv_url ); // Get cache. $stats_cache = get_option( 'stats_cache' ); if ( ! $stats_cache || ! is_array( $stats_cache ) ) { $stats_cache = array(); } // Return or expire this key. if ( isset( $stats_cache[ $key ] ) ) { $time = key( $stats_cache[ $key ] ); if ( time() - $time < 300 ) { return $stats_cache[ $key ][ $time ]; } unset( $stats_cache[ $key ] ); } $stats_rows = array(); do { $stats = stats_get_remote_csv( $stats_csv_url ); if ( ! $stats ) { break; } $labels = array_shift( $stats ); if ( 0 === stripos( $labels[0], 'error' ) ) { break; } $stats_rows = array(); for ( $s = 0; isset( $stats[ $s ] ); $s++ ) { $row = array(); foreach ( $labels as $col => $label ) { $row[ $label ] = $stats[ $s ][ $col ]; } $stats_rows[] = $row; } } while ( 0 ); // Expire old keys. foreach ( $stats_cache as $k => $cache ) { if ( ! is_array( $cache ) || 300 < time() - key( $cache ) ) { unset( $stats_cache[ $k ] ); } } // Set cache. $stats_cache[ $key ] = array( time() => $stats_rows ); update_option( 'stats_cache', $stats_cache ); return $stats_rows; } /** * Stats get remote CSV. * * @access public * @param mixed $url URL. * @return array */ function stats_get_remote_csv( $url ) { $method = 'GET'; $timeout = 90; $user_id = 0; // Blog token. $get = Client::remote_request( compact( 'url', 'method', 'timeout', 'user_id' ) ); $get_code = wp_remote_retrieve_response_code( $get ); if ( is_wp_error( $get ) || ( 2 !== (int) ( $get_code / 100 ) && 304 !== $get_code ) || empty( $get['body'] ) ) { return array(); // @todo: return an error? } else { return stats_str_getcsv( $get['body'] ); } } /** * Recursively run str_getcsv on the stats csv. * * @since 9.7.0 Remove custom handling since str_getcsv is available on all servers running this now. * * @param mixed $csv CSV. * @return array. */ function stats_str_getcsv( $csv ) { $lines = str_getcsv( $csv, "\n" ); return array_map( 'str_getcsv', $lines ); } /** * Abstract out building the rest api stats path. * * @param string $resource Resource. * @return string */ function jetpack_stats_api_path( $resource = '' ) { $resource = ltrim( $resource, '/' ); return sprintf( '/sites/%d/stats/%s', Stats_Options::get_option( 'blog_id' ), $resource ); } /** * Fetches stats data from the REST API. Caches locally for 5 minutes. * * @link: https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/ * @access public * @deprecated 11.5 Use WPCOM_Stats available methodsinstead. * @param array $args (default: array()) The args that are passed to the endpoint. * @param string $resource (default: '') Optional sub-endpoint following /stats/. * @return array|WP_Error. */ function stats_get_from_restapi( $args = array(), $resource = '' ) { _deprecated_function( __METHOD__, 'jetpack-11.5', 'Please checkout the methods available in Automattic\Jetpack\Stats\WPCOM_Stats' ); $endpoint = jetpack_stats_api_path( $resource ); $api_version = '1.1'; $args = wp_parse_args( $args, array() ); $cache_key = md5( implode( '|', array( $endpoint, $api_version, wp_json_encode( $args ) ) ) ); $transient_name = "jetpack_restapi_stats_cache_{$cache_key}"; $stats_cache = get_transient( $transient_name ); // Return or expire this key. if ( $stats_cache ) { $time = key( $stats_cache ); $data = $stats_cache[ $time ]; // WP_Error or string (JSON encoded object). if ( is_wp_error( $data ) ) { return $data; } return (object) array_merge( array( 'cached_at' => $time ), (array) json_decode( $data ) ); } // Do the dirty work. $response = Client::wpcom_json_api_request_as_blog( $endpoint, $api_version, $args ); if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { // WP_Error. $data = is_wp_error( $response ) ? $response : new WP_Error( 'stats_error' ); // WP_Error. $return = $data; } else { // string (JSON encoded object). $data = wp_remote_retrieve_body( $response ); // object (rare: null on JSON failure). $return = json_decode( $data ); } // To reduce size in storage: store with time as key, store JSON encoded data (unless error). set_transient( $transient_name, array( time() => $data ), 5 * MINUTE_IN_SECONDS ); return $return; } /** * Load CSS needed for Stats column width in WP-Admin area. * * @since 4.7.0 */ function jetpack_stats_load_admin_css() { ?> is_user_connected() ) { return $columns; } // Array-Fu to add before comments. $pos = array_search( 'comments', array_keys( $columns ), true ); // Fallback to the last position if the post type does not support comments. if ( ! is_int( $pos ) ) { $pos = count( $columns ); } // Final fallback, if the array was malformed by another plugin for example. if ( ! is_int( $pos ) ) { return $columns; } $chunks = array_chunk( $columns, $pos, true ); $chunks[0]['stats'] = esc_html__( 'Stats', 'jetpack' ); return call_user_func_array( 'array_merge', $chunks ); } /** * Set content for cell with link to an entry's stats in WordPress.com. * * @param string $column The name of the column to display. * @param int $post_id The current post ID. * * @since 4.7.0 * * @return mixed */ function jetpack_stats_post_table_cell( $column, $post_id ) { if ( 'stats' === $column ) { if ( 'publish' !== get_post_status( $post_id ) ) { printf( '%s', esc_html__( 'No stats', 'jetpack' ) ); } else { $stats_post_url = Redirect::get_url( 'calypso-stats-post', array( 'path' => $post_id, ) ); printf( '', esc_url( $stats_post_url ), esc_html__( 'View stats for this post at WordPress.com', 'jetpack' ) ); } } } /** * Add the Jetpack plugin version to the stats tracking data. * * @param array $kvs The stats array in key values. * @return array */ function filter_stats_array_add_jp_version( $kvs ) { $kvs['j'] = sprintf( '%s:%s', JETPACK__API_VERSION, JETPACK__VERSION ); return $kvs; } /** * Convert stats array to object after sanity checking the array is valid. * * @param array $stats_array The stats array. * @return WP_Error|Object|null */ function convert_stats_array_to_object( $stats_array ) { if ( is_wp_error( $stats_array ) ) { return $stats_array; } $encoded_array = wp_json_encode( $stats_array ); if ( ! $encoded_array ) { return new WP_Error( 'stats_encoding_error', 'Failed to encode stats array' ); } return json_decode( $encoded_array ); }