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