Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.95% covered (warning)
80.95%
34 / 42
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Heartbeat
80.95% covered (warning)
80.95%
34 / 42
50.00% covered (danger)
50.00%
2 / 4
20.24
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 generate_stats_array
87.88% covered (warning)
87.88%
29 / 33
0.00% covered (danger)
0.00%
0 / 1
13.30
 add_stats_to_heartbeat
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Jetpack Heartbeat.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Heartbeat;
9
10/**
11 * Jetpack Heartbeat.
12 */
13class Jetpack_Heartbeat {
14
15    /**
16     * Holds the singleton instance of this class
17     *
18     * @since 2.3.3
19     * @var Jetpack_Heartbeat
20     */
21    private static $instance = false;
22
23    /**
24     * Holds the singleton instance of the proxied class
25     *
26     * @since 8.9.0
27     * @var Automattic\Jetpack\Heartbeat
28     */
29    private static $proxied_instance = false;
30
31    /**
32     * Singleton
33     *
34     * @since 2.3.3
35     * @static
36     * @return Jetpack_Heartbeat
37     */
38    public static function init() {
39        if ( ! self::$instance ) {
40            self::$instance         = new Jetpack_Heartbeat();
41            self::$proxied_instance = Heartbeat::init();
42        }
43
44        return self::$instance;
45    }
46
47    /**
48     * Constructor for singleton
49     *
50     * @since 2.3.3
51     */
52    private function __construct() {
53        add_filter( 'jetpack_heartbeat_stats_array', array( $this, 'add_stats_to_heartbeat' ) );
54    }
55
56    /**
57     * Generates heartbeat stats data.
58     *
59     * @param string $prefix Prefix to add before stats identifier.
60     *
61     * @return array The stats array.
62     */
63    public static function generate_stats_array( $prefix = '' ) {
64        $return = array();
65
66        $return[ "{$prefix}version" ]      = JETPACK__VERSION;
67        $return[ "{$prefix}wp-version" ]   = get_bloginfo( 'version' );
68        $return[ "{$prefix}php-version" ]  = PHP_VERSION;
69        $return[ "{$prefix}branch" ]       = (float) JETPACK__VERSION;
70        $return[ "{$prefix}wp-branch" ]    = (float) get_bloginfo( 'version' );
71        $return[ "{$prefix}php-branch" ]   = (float) PHP_VERSION;
72        $return[ "{$prefix}public" ]       = Jetpack_Options::get_option( 'public' );
73        $return[ "{$prefix}ssl" ]          = Jetpack::permit_ssl();
74        $return[ "{$prefix}is-https" ]     = is_ssl() ? 'https' : 'http';
75        $return[ "{$prefix}language" ]     = get_bloginfo( 'language' );
76        $return[ "{$prefix}charset" ]      = get_bloginfo( 'charset' );
77        $return[ "{$prefix}is-multisite" ] = is_multisite() ? 'multisite' : 'singlesite';
78        $return[ "{$prefix}plugins" ]      = implode( ',', Jetpack::get_active_plugins() );
79        if ( function_exists( 'get_mu_plugins' ) ) {
80            $return[ "{$prefix}mu-plugins" ] = implode( ',', array_keys( get_mu_plugins() ) );
81        }
82        $return[ "{$prefix}manage-enabled" ] = true;
83
84        if ( function_exists( 'get_space_used' ) ) { // Only available in multisite.
85            $space_used = get_space_used();
86        } else {
87            // This is the same as `get_space_used`, except it does not apply the short-circuit filter.
88            $upload_dir = wp_upload_dir();
89            $space_used = get_dirsize( $upload_dir['basedir'] ) / MB_IN_BYTES;
90        }
91
92        $return[ "{$prefix}space-used" ] = $space_used;
93
94        // is-multi-network can have three values, `single-site`, `single-network`, and `multi-network`.
95        $return[ "{$prefix}is-multi-network" ] = 'single-site';
96        if ( is_multisite() ) {
97            $return[ "{$prefix}is-multi-network" ] = Jetpack::is_multi_network() ? 'multi-network' : 'single-network';
98        }
99
100        if ( ! empty( $_SERVER['SERVER_ADDR'] ) || ! empty( $_SERVER['LOCAL_ADDR'] ) ) {
101            $ip     = ! empty( $_SERVER['SERVER_ADDR'] ) ? wp_unslash( $_SERVER['SERVER_ADDR'] ) : wp_unslash( $_SERVER['LOCAL_ADDR'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized just below.
102            $ip_arr = array_map( 'intval', explode( '.', $ip ) );
103            if ( 4 === count( $ip_arr ) ) {
104                $return[ "{$prefix}ip-2-octets" ] = implode( '.', array_slice( $ip_arr, 0, 2 ) );
105            }
106        }
107
108        foreach ( Jetpack::get_available_modules() as $slug ) {
109            $return[ "{$prefix}module-{$slug}" ] = Jetpack::is_module_active( $slug ) ? 'on' : 'off';
110        }
111
112        return $return;
113    }
114
115    /**
116     * Add Jetpack Stats array to Heartbeat if Jetpack is connected
117     *
118     * @since 8.9.0
119     *
120     * @param array $stats Jetpack Heartbeat stats.
121     * @return array $stats
122     */
123    public function add_stats_to_heartbeat( $stats ) {
124
125        if ( ! Jetpack::is_connection_ready() ) {
126            return $stats;
127        }
128
129        $jetpack_stats = self::generate_stats_array();
130
131        return array_merge( $stats, $jetpack_stats );
132    }
133}