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