Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.59% |
39 / 49 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Heartbeat | |
79.59% |
39 / 49 |
|
50.00% |
2 / 4 |
23.40 | |
0.00% |
0 / 1 |
| init | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| generate_stats_array | |
85.00% |
34 / 40 |
|
0.00% |
0 / 1 |
15.76 | |||
| add_stats_to_heartbeat | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Jetpack Heartbeat. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Connection\Manager; |
| 9 | use Automattic\Jetpack\Heartbeat; |
| 10 | |
| 11 | /** |
| 12 | * Jetpack Heartbeat. |
| 13 | */ |
| 14 | class 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}identitycrisis" ] = Jetpack::check_identity_crisis() ? 'yes' : 'no'; |
| 80 | $return[ "{$prefix}plugins" ] = implode( ',', Jetpack::get_active_plugins() ); |
| 81 | if ( function_exists( 'get_mu_plugins' ) ) { |
| 82 | $return[ "{$prefix}mu-plugins" ] = implode( ',', array_keys( get_mu_plugins() ) ); |
| 83 | } |
| 84 | $return[ "{$prefix}manage-enabled" ] = true; |
| 85 | |
| 86 | if ( function_exists( 'get_space_used' ) ) { // Only available in multisite. |
| 87 | $space_used = get_space_used(); |
| 88 | } else { |
| 89 | // This is the same as `get_space_used`, except it does not apply the short-circuit filter. |
| 90 | $upload_dir = wp_upload_dir(); |
| 91 | $space_used = get_dirsize( $upload_dir['basedir'] ) / MB_IN_BYTES; |
| 92 | } |
| 93 | |
| 94 | $return[ "{$prefix}space-used" ] = $space_used; |
| 95 | |
| 96 | $xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() ); |
| 97 | if ( $xmlrpc_errors ) { |
| 98 | $return[ "{$prefix}xmlrpc-errors" ] = implode( ',', array_keys( $xmlrpc_errors ) ); |
| 99 | Jetpack_Options::delete_option( 'xmlrpc_errors' ); |
| 100 | } |
| 101 | |
| 102 | // Missing the connection owner? |
| 103 | $connection_manager = new Manager(); |
| 104 | $return[ "{$prefix}missing-owner" ] = $connection_manager->is_missing_connection_owner(); |
| 105 | |
| 106 | // is-multi-network can have three values, `single-site`, `single-network`, and `multi-network`. |
| 107 | $return[ "{$prefix}is-multi-network" ] = 'single-site'; |
| 108 | if ( is_multisite() ) { |
| 109 | $return[ "{$prefix}is-multi-network" ] = Jetpack::is_multi_network() ? 'multi-network' : 'single-network'; |
| 110 | } |
| 111 | |
| 112 | if ( ! empty( $_SERVER['SERVER_ADDR'] ) || ! empty( $_SERVER['LOCAL_ADDR'] ) ) { |
| 113 | $ip = ! empty( $_SERVER['SERVER_ADDR'] ) ? wp_unslash( $_SERVER['SERVER_ADDR'] ) : wp_unslash( $_SERVER['LOCAL_ADDR'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized just below. |
| 114 | $ip_arr = array_map( 'intval', explode( '.', $ip ) ); |
| 115 | if ( 4 === count( $ip_arr ) ) { |
| 116 | $return[ "{$prefix}ip-2-octets" ] = implode( '.', array_slice( $ip_arr, 0, 2 ) ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | foreach ( Jetpack::get_available_modules() as $slug ) { |
| 121 | $return[ "{$prefix}module-{$slug}" ] = Jetpack::is_module_active( $slug ) ? 'on' : 'off'; |
| 122 | } |
| 123 | |
| 124 | return $return; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Add Jetpack Stats array to Heartbeat if Jetpack is connected |
| 129 | * |
| 130 | * @since 8.9.0 |
| 131 | * |
| 132 | * @param array $stats Jetpack Heartbeat stats. |
| 133 | * @return array $stats |
| 134 | */ |
| 135 | public function add_stats_to_heartbeat( $stats ) { |
| 136 | |
| 137 | if ( ! Jetpack::is_connection_ready() ) { |
| 138 | return $stats; |
| 139 | } |
| 140 | |
| 141 | $jetpack_stats = self::generate_stats_array(); |
| 142 | |
| 143 | return array_merge( $stats, $jetpack_stats ); |
| 144 | } |
| 145 | } |