2019-11-15 23:26:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Automattic\Jetpack\Connection\Manager;
|
2020-09-15 14:30:05 +02:00
|
|
|
use Automattic\Jetpack\Heartbeat;
|
2019-11-15 23:26:29 +01:00
|
|
|
|
|
|
|
|
class Jetpack_Heartbeat {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Holds the singleton instance of this class
|
|
|
|
|
*
|
|
|
|
|
* @since 2.3.3
|
|
|
|
|
* @var Jetpack_Heartbeat
|
|
|
|
|
*/
|
|
|
|
|
private static $instance = false;
|
|
|
|
|
|
2020-09-15 14:30:05 +02:00
|
|
|
/**
|
|
|
|
|
* Holds the singleton instance of the proxied class
|
|
|
|
|
*
|
|
|
|
|
* @since 8.9.0
|
|
|
|
|
* @var Automattic\Jetpack\Heartbeat
|
|
|
|
|
*/
|
|
|
|
|
private static $proxied_instance = false;
|
2019-11-15 23:26:29 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Singleton
|
|
|
|
|
*
|
|
|
|
|
* @since 2.3.3
|
|
|
|
|
* @static
|
|
|
|
|
* @return Jetpack_Heartbeat
|
|
|
|
|
*/
|
|
|
|
|
public static function init() {
|
|
|
|
|
if ( ! self::$instance ) {
|
2020-09-15 14:30:05 +02:00
|
|
|
self::$instance = new Jetpack_Heartbeat();
|
|
|
|
|
self::$proxied_instance = Heartbeat::init();
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor for singleton
|
|
|
|
|
*
|
|
|
|
|
* @since 2.3.3
|
|
|
|
|
* @return Jetpack_Heartbeat
|
|
|
|
|
*/
|
|
|
|
|
private function __construct() {
|
2020-09-15 14:30:05 +02:00
|
|
|
add_filter( 'jetpack_heartbeat_stats_array', array( $this, 'add_stats_to_heartbeat' ) );
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method that gets executed on the wp-cron call
|
|
|
|
|
*
|
2020-09-15 14:30:05 +02:00
|
|
|
* @deprecated since 8.9.0
|
|
|
|
|
* @see Automattic\Jetpack\Heartbeat::cron_exec()
|
|
|
|
|
*
|
2019-11-15 23:26:29 +01:00
|
|
|
* @since 2.3.3
|
|
|
|
|
* @global string $wp_version
|
|
|
|
|
*/
|
|
|
|
|
public function cron_exec() {
|
2020-09-15 14:30:05 +02:00
|
|
|
_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::cron_exec' );
|
|
|
|
|
return self::$proxied_instance->cron_exec();
|
2019-11-15 23:26:29 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-26 12:53:20 +01:00
|
|
|
/**
|
|
|
|
|
* Generates heartbeat stats data.
|
|
|
|
|
*
|
|
|
|
|
* @param string $prefix Prefix to add before stats identifier.
|
|
|
|
|
*
|
|
|
|
|
* @return array The stats array.
|
|
|
|
|
*/
|
2019-11-15 23:26:29 +01:00
|
|
|
public static function generate_stats_array( $prefix = '' ) {
|
|
|
|
|
$return = array();
|
|
|
|
|
|
|
|
|
|
$return[ "{$prefix}version" ] = JETPACK__VERSION;
|
|
|
|
|
$return[ "{$prefix}wp-version" ] = get_bloginfo( 'version' );
|
|
|
|
|
$return[ "{$prefix}php-version" ] = PHP_VERSION;
|
2020-11-18 09:10:44 +01:00
|
|
|
$return[ "{$prefix}branch" ] = (float) JETPACK__VERSION;
|
|
|
|
|
$return[ "{$prefix}wp-branch" ] = (float) get_bloginfo( 'version' );
|
|
|
|
|
$return[ "{$prefix}php-branch" ] = (float) PHP_VERSION;
|
2019-11-15 23:26:29 +01:00
|
|
|
$return[ "{$prefix}public" ] = Jetpack_Options::get_option( 'public' );
|
|
|
|
|
$return[ "{$prefix}ssl" ] = Jetpack::permit_ssl();
|
|
|
|
|
$return[ "{$prefix}is-https" ] = is_ssl() ? 'https' : 'http';
|
|
|
|
|
$return[ "{$prefix}language" ] = get_bloginfo( 'language' );
|
|
|
|
|
$return[ "{$prefix}charset" ] = get_bloginfo( 'charset' );
|
|
|
|
|
$return[ "{$prefix}is-multisite" ] = is_multisite() ? 'multisite' : 'singlesite';
|
|
|
|
|
$return[ "{$prefix}identitycrisis" ] = Jetpack::check_identity_crisis() ? 'yes' : 'no';
|
|
|
|
|
$return[ "{$prefix}plugins" ] = implode( ',', Jetpack::get_active_plugins() );
|
2020-01-26 12:53:20 +01:00
|
|
|
if ( function_exists( 'get_mu_plugins' ) ) {
|
|
|
|
|
$return[ "{$prefix}mu-plugins" ] = implode( ',', array_keys( get_mu_plugins() ) );
|
|
|
|
|
}
|
2019-11-15 23:26:29 +01:00
|
|
|
$return[ "{$prefix}manage-enabled" ] = true;
|
|
|
|
|
|
2021-08-17 08:33:07 +02:00
|
|
|
if ( function_exists( 'get_space_used' ) ) { // Only available in multisite.
|
|
|
|
|
$space_used = get_space_used();
|
|
|
|
|
} else {
|
|
|
|
|
// This is the same as `get_space_used`, except it does not apply the short-circuit filter.
|
|
|
|
|
$upload_dir = wp_upload_dir();
|
|
|
|
|
$space_used = get_dirsize( $upload_dir['basedir'] ) / MB_IN_BYTES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$return[ "{$prefix}space-used" ] = $space_used;
|
|
|
|
|
|
2019-11-15 23:26:29 +01:00
|
|
|
$xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
|
|
|
|
|
if ( $xmlrpc_errors ) {
|
|
|
|
|
$return[ "{$prefix}xmlrpc-errors" ] = implode( ',', array_keys( $xmlrpc_errors ) );
|
|
|
|
|
Jetpack_Options::delete_option( 'xmlrpc_errors' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Missing the connection owner?
|
|
|
|
|
$connection_manager = new Manager();
|
|
|
|
|
$return[ "{$prefix}missing-owner" ] = $connection_manager->is_missing_connection_owner();
|
|
|
|
|
|
2020-01-26 12:53:20 +01:00
|
|
|
// is-multi-network can have three values, `single-site`, `single-network`, and `multi-network`.
|
2019-11-15 23:26:29 +01:00
|
|
|
$return[ "{$prefix}is-multi-network" ] = 'single-site';
|
|
|
|
|
if ( is_multisite() ) {
|
|
|
|
|
$return[ "{$prefix}is-multi-network" ] = Jetpack::is_multi_network() ? 'multi-network' : 'single-network';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! empty( $_SERVER['SERVER_ADDR'] ) || ! empty( $_SERVER['LOCAL_ADDR'] ) ) {
|
|
|
|
|
$ip = ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
|
|
|
|
|
$ip_arr = array_map( 'intval', explode( '.', $ip ) );
|
2020-01-26 12:53:20 +01:00
|
|
|
if ( 4 === count( $ip_arr ) ) {
|
2019-11-15 23:26:29 +01:00
|
|
|
$return[ "{$prefix}ip-2-octets" ] = implode( '.', array_slice( $ip_arr, 0, 2 ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( Jetpack::get_available_modules() as $slug ) {
|
|
|
|
|
$return[ "{$prefix}module-{$slug}" ] = Jetpack::is_module_active( $slug ) ? 'on' : 'off';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-15 14:30:05 +02:00
|
|
|
/**
|
|
|
|
|
* Registers jetpack.getHeartbeatData xmlrpc method
|
|
|
|
|
*
|
|
|
|
|
* @deprecated since 8.9.0
|
|
|
|
|
* @see Automattic\Jetpack\Heartbeat::jetpack_xmlrpc_methods()
|
|
|
|
|
*
|
|
|
|
|
* @param array $methods The list of methods to be filtered.
|
|
|
|
|
* @return array $methods
|
|
|
|
|
*/
|
2019-11-15 23:26:29 +01:00
|
|
|
public static function jetpack_xmlrpc_methods( $methods ) {
|
2020-09-15 14:30:05 +02:00
|
|
|
_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::jetpack_xmlrpc_methods' );
|
|
|
|
|
return Heartbeat::jetpack_xmlrpc_methods( $methods );
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-15 14:30:05 +02:00
|
|
|
/**
|
|
|
|
|
* Handles the response for the jetpack.getHeartbeatData xmlrpc method
|
|
|
|
|
*
|
|
|
|
|
* @deprecated since 8.9.0
|
|
|
|
|
* @see Automattic\Jetpack\Heartbeat::xmlrpc_data_response()
|
|
|
|
|
*
|
|
|
|
|
* @param array $params The parameters received in the request.
|
|
|
|
|
* @return array $params all the stats that hearbeat handles.
|
|
|
|
|
*/
|
2019-11-15 23:26:29 +01:00
|
|
|
public static function xmlrpc_data_response( $params = array() ) {
|
2020-09-15 14:30:05 +02:00
|
|
|
_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::xmlrpc_data_response' );
|
|
|
|
|
return Heartbeat::xmlrpc_data_response( $params );
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-15 14:30:05 +02:00
|
|
|
/**
|
|
|
|
|
* Clear scheduled events
|
|
|
|
|
*
|
|
|
|
|
* @deprecated since 8.9.0
|
|
|
|
|
* @see Automattic\Jetpack\Heartbeat::deactivate()
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2019-11-15 23:26:29 +01:00
|
|
|
public function deactivate() {
|
2020-09-15 14:30:05 +02:00
|
|
|
// Cronjobs are now handled by the Heartbeat package and we don't want to deactivate it here.
|
|
|
|
|
// We are adding jetpack stats to the heartbeat only if the connection is available. so we don't need to disable the cron when disconnecting.
|
|
|
|
|
_deprecated_function( __METHOD__, 'jetpack-8.9.0', 'Automattic\\Jetpack\\Heartbeat::deactivate' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add Jetpack Stats array to Heartbeat if Jetpack is connected
|
|
|
|
|
*
|
|
|
|
|
* @since 8.9.0
|
|
|
|
|
*
|
|
|
|
|
* @param array $stats Jetpack Heartbeat stats.
|
|
|
|
|
* @return array $stats
|
|
|
|
|
*/
|
|
|
|
|
public function add_stats_to_heartbeat( $stats ) {
|
|
|
|
|
|
2021-04-27 08:32:47 +02:00
|
|
|
if ( ! Jetpack::is_connection_ready() ) {
|
2020-09-15 14:30:05 +02:00
|
|
|
return $stats;
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-15 14:30:05 +02:00
|
|
|
$jetpack_stats = self::generate_stats_array();
|
|
|
|
|
|
|
|
|
|
return array_merge( $stats, $jetpack_stats );
|
2019-11-15 23:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|