stats/streak/ * * @param array $args Optional query parameters. * @return array|WP_Error */ public function get_streak( $args = array() ) { $this->resource = 'streak'; return $this->fetch_stats( $args ); } /** * Get the highlights for the site. * * @link https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/highlights/ * * @param array $args Optional query parameters. * @return array|WP_Error */ public function get_highlights( $args = array() ) { $this->resource = 'highlights'; return $this->fetch_stats( $args ); } /** * Get the number of visits for the site. * * @param array $args Optional query parameters. * @return array|WP_Error */ public function get_insights( $args = array() ) { $this->resource = 'insights'; return $this->fetch_stats( $args ); } /** * Build WPCOM REST API endpoint. * * @return string */ protected function build_endpoint() { $resource = ltrim( $this->resource, '/' ); return sprintf( '/sites/%d/stats/%s', Jetpack_Options::get_option( 'id' ), $resource ); } /** * Fetches stats data from WPCOM or local Cache. Caches locally for 5 minutes. * * @param array $args Optional query parameters. * * @return array|WP_Error */ protected function fetch_stats( $args = array() ) { $endpoint = $this->build_endpoint(); $api_version = self::STATS_REST_API_VERSION; $cache_key = md5( implode( '|', array( $endpoint, $api_version, wp_json_encode( $args ) ) ) ); $transient_name = self::STATS_CACHE_TRANSIENT_PREFIX . $cache_key; $stats_cache = get_transient( $transient_name ); 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 array_merge( array( 'cached_at' => $time ), (array) json_decode( $data, true ) ); } $wpcom_stats = $this->fetch_remote_stats( $endpoint, $args ); // To reduce size in storage: store with time as key, store JSON encoded data. $cached_value = is_wp_error( $wpcom_stats ) ? $wpcom_stats : wp_json_encode( $wpcom_stats ); /** * Filters the expiration time for the stats cache. * * @module stats * * @since 0.10.0 * * @param int $expiration The expiration time in minutes. */ $expiration = apply_filters( 'jetpack_fetch_stats_cache_expiration', self::STATS_CACHE_EXPIRATION_IN_MINUTES * MINUTE_IN_SECONDS ); set_transient( $transient_name, array( time() => $cached_value ), $expiration ); return $wpcom_stats; } /** * Fetches stats data from WPCOM or local Cache. Caches locally for 5 minutes. * * Unlike the above function, this caches data in the post meta table. As such, * it prevents wp_options from blowing up when retrieving views for large numbers * of posts at the same time. However, the final response is the same as above. * * @param array $args Query parameters. * @param int $post_id Post ID to acquire stats for. * * @return array|WP_Error */ protected function fetch_post_stats( $args, $post_id ) { $endpoint = $this->build_endpoint(); $meta_name = '_' . self::STATS_CACHE_TRANSIENT_PREFIX; $stats_cache = get_post_meta( $post_id, $meta_name ); if ( $stats_cache ) { $data = reset( $stats_cache ); if ( ! is_array( $data ) || empty( $data ) || is_wp_error( $data ) ) { return $data; } $time = key( $data ); $views = $data[ $time ] ?? null; // Bail if data is malformed. if ( ! is_numeric( $time ) || ! is_array( $views ) ) { return $data; } /** This filter is already documented in projects/packages/stats/src/class-wpcom-stats.php */ $expiration = apply_filters( 'jetpack_fetch_stats_cache_expiration', self::STATS_CACHE_EXPIRATION_IN_MINUTES * MINUTE_IN_SECONDS ); if ( ( time() - $time ) < $expiration ) { return array_merge( array( 'cached_at' => $time ), $views ); } } $wpcom_stats = $this->fetch_remote_stats( $endpoint, $args ); update_post_meta( $post_id, $meta_name, array( time() => $wpcom_stats ) ); return $wpcom_stats; } /** * Fetches stats data from WPCOM. * * @link https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/ * @param string $endpoint The stats endpoint. * @param array $args The query arguments. * @return array|WP_Error */ protected function fetch_remote_stats( $endpoint, $args ) { if ( is_array( $args ) && ! empty( $args ) ) { $endpoint .= '?' . http_build_query( $args ); } $response = Client::wpcom_json_api_request_as_blog( $endpoint, self::STATS_REST_API_VERSION, array( 'timeout' => 20 ) ); $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); if ( is_wp_error( $response ) || 200 !== $response_code || empty( $response_body ) ) { return is_wp_error( $response ) ? $response : new WP_Error( 'stats_error', 'Failed to fetch Stats from WPCOM' ); } return json_decode( $response_body, true ); } /** * Convert stats array to object after sanity checking the array is valid. * * @since 0.11.0 * * @param array $stats_array The stats array. * @return WP_Error|object|null */ public 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 ); } }