PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / _inc / lib / class-jetpack-blog-stats-helper.php

class-jetpack-blog-stats-helper.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.7, at _inc/lib/class-jetpack-blog-stats-helper.php

92 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Blog Stats block helper.
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Jetpack\Stats\WPCOM_Stats;
9
10 /**
11 * Class Jetpack_Blog_Stats_Helper
12 */
13 class Jetpack_Blog_Stats_Helper {
14 /**
15 * Returns user's blog stats.
16 *
17 * @param array $stats_option Array containing the Blog Stats block attributes.
18 *
19 * @return int
20 */
21 public static function get_stats( $stats_option ) {
22 $stats_option = wp_parse_args(
23 $stats_option,
24 array(
25 'statsOption' => 'blog',
26 'statsData' => 'views',
27 'postId' => get_the_ID(),
28 )
29 );
30
31 if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
32 // Jetpack sites.
33 $wpcom_stats = new WPCOM_Stats();
34
35 if ( $stats_option['statsOption'] === 'post' ) {
36 // Cache in post meta to prevent wp_options blowing up when retrieving views
37 // for multiple posts simultaneously (eg. when inserted into template).
38 $cache_in_meta = true;
39 $data = $wpcom_stats->convert_stats_array_to_object(
40 $wpcom_stats->get_post_views(
41 $stats_option['postId'],
42 array( 'fields' => 'views' ), // No visitor count for posts.
43 $cache_in_meta
44 )
45 );
46
47 if ( isset( $data->views ) ) {
48 return $data->views;
49 }
50 } else {
51 $data = $wpcom_stats->convert_stats_array_to_object(
52 $wpcom_stats->get_stats( array( 'fields' => 'stats' ) )
53 );
54
55 if ( $stats_option['statsData'] === 'views' && isset( $data->stats->views ) ) {
56 return $data->stats->views;
57 }
58
59 if ( $stats_option['statsData'] === 'visitors' && isset( $data->stats->visitors ) ) {
60 return $data->stats->visitors;
61 }
62 }
63 } elseif ( $stats_option['statsOption'] === 'post' ) {
64 // Simple sites.
65 if ( function_exists( 'get_all_time_postviews' ) ) {
66 // This is cached so no need to cache it again.
67 return (int) get_all_time_postviews( $stats_option['postId'] );
68 }
69 } else {
70 // Simple sites.
71 $_blog_id = get_current_blog_id();
72 if ( $stats_option['statsData'] === 'views' && function_exists( 'stats_grandtotal_views' ) ) {
73 // This is cached so no need to cache it again.
74 return stats_grandtotal_views( $_blog_id );
75 }
76
77 if ( $stats_option['statsData'] === 'visitors' && function_exists( 'stats_get_visitors' ) ) {
78 $stats = wp_cache_get( "stats_get_visitors_total_$_blog_id", 'blog-stats-block' );
79 if ( false !== $stats ) {
80 return $stats;
81 }
82 $stats = array_sum( stats_get_visitors( get_current_blog_id(), false, (int) gmdate( 'Y' ) - 2012, 365 ) );
83 wp_cache_set( "stats_get_visitors_total_$_blog_id", $stats, 'blog-stats-block', HOUR_IN_SECONDS );
84
85 return $stats;
86 }
87 }
88
89 return 0;
90 }
91 }
92