Jetpack_Heartbeat

The WordPress Core Jetpack Heartbeat class.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-heartbeat.php  
  1. class Jetpack_Heartbeat { 
  2.  
  3. /** 
  4. * Holds the singleton instance of this class 
  5. * @since 2.3.3 
  6. * @var Jetpack_Heartbeat 
  7. */ 
  8. private static $instance = false; 
  9.  
  10. private $cron_name = 'jetpack_v2_heartbeat'; 
  11.  
  12. /** 
  13. * Singleton 
  14. * @since 2.3.3 
  15. * @static 
  16. * @return Jetpack_Heartbeat 
  17. */ 
  18. public static function init() { 
  19. if ( ! self::$instance ) { 
  20. self::$instance = new Jetpack_Heartbeat; 
  21.  
  22. return self::$instance; 
  23.  
  24. /** 
  25. * Constructor for singleton 
  26. * @since 2.3.3 
  27. * @return Jetpack_Heartbeat 
  28. */ 
  29. private function __construct() { 
  30. if ( ! Jetpack::is_active() ) 
  31. return; 
  32.  
  33. // Schedule the task 
  34. add_action( $this->cron_name, array( $this, 'cron_exec' ) ); 
  35.  
  36. if ( ! wp_next_scheduled( $this->cron_name ) ) { 
  37. // Deal with the old pre-3.0 weekly one. 
  38. if ( $timestamp = wp_next_scheduled( 'jetpack_heartbeat' ) ) { 
  39. wp_unschedule_event( $timestamp, 'jetpack_heartbeat' ); 
  40.  
  41. wp_schedule_event( time(), 'daily', $this->cron_name ); 
  42.  
  43. add_filter( 'jetpack_xmlrpc_methods', array( __CLASS__, 'jetpack_xmlrpc_methods' ) ); 
  44.  
  45. /** 
  46. * Method that gets executed on the wp-cron call 
  47. * @since 2.3.3 
  48. * @global string $wp_version 
  49. */ 
  50. public function cron_exec() { 
  51.  
  52. $jetpack = Jetpack::init(); 
  53.  
  54. /** 
  55. * This should run daily. Figuring in for variances in 
  56. * WP_CRON, don't let it run more than every 23 hours at most. 
  57. * i.e. if it ran less than 23 hours ago, fail out. 
  58. */ 
  59. $last = (int) Jetpack_Options::get_option( 'last_heartbeat' ); 
  60. if ( $last && ( $last + DAY_IN_SECONDS - HOUR_IN_SECONDS > time() ) ) { 
  61. return; 
  62.  
  63. /** 
  64. * Check for an identity crisis 
  65. * If one exists: 
  66. * - Bump stat for ID crisis 
  67. * - Email site admin about potential ID crisis 
  68. */ 
  69.  
  70. // Coming Soon! 
  71.  
  72. foreach ( self::generate_stats_array( 'v2-' ) as $key => $value ) { 
  73. $jetpack->stat( $key, $value ); 
  74.  
  75. Jetpack_Options::update_option( 'last_heartbeat', time() ); 
  76.  
  77. $jetpack->do_stats( 'server_side' ); 
  78.  
  79. /** 
  80. * Fires when we synchronize all registered options on heartbeat. 
  81. * @since 3.3.0 
  82. */ 
  83. do_action( 'jetpack_heartbeat' ); 
  84.  
  85. public static function generate_stats_array( $prefix = '' ) { 
  86. $return = array(); 
  87.  
  88. $return["{$prefix}version"] = JETPACK__VERSION; 
  89. $return["{$prefix}wp-version"] = get_bloginfo( 'version' ); 
  90. $return["{$prefix}php-version"] = PHP_VERSION; 
  91. $return["{$prefix}branch"] = floatval( JETPACK__VERSION ); 
  92. $return["{$prefix}wp-branch"] = floatval( get_bloginfo( 'version' ) ); 
  93. $return["{$prefix}php-branch"] = floatval( PHP_VERSION ); 
  94. $return["{$prefix}public"] = Jetpack_Options::get_option( 'public' ); 
  95. $return["{$prefix}ssl"] = Jetpack::permit_ssl(); 
  96. $return["{$prefix}is-https"] = is_ssl() ? 'https' : 'http'; 
  97. $return["{$prefix}language"] = get_bloginfo( 'language' ); 
  98. $return["{$prefix}charset"] = get_bloginfo( 'charset' ); 
  99. $return["{$prefix}is-multisite"] = is_multisite() ? 'multisite' : 'singlesite'; 
  100. $return["{$prefix}identitycrisis"] = Jetpack::check_identity_crisis() ? 'yes' : 'no'; 
  101. $return["{$prefix}plugins"] = implode( ', ', Jetpack::get_active_plugins() ); 
  102. $return["{$prefix}manage-enabled"] = Jetpack::is_module_active( 'manage' ); 
  103.  
  104. // is-multi-network can have three values, `single-site`, `single-network`, and `multi-network` 
  105. $return["{$prefix}is-multi-network"] = 'single-site'; 
  106. if ( is_multisite() ) { 
  107. $return["{$prefix}is-multi-network"] = Jetpack::is_multi_network() ? 'multi-network' : 'single-network'; 
  108.  
  109. if ( ! empty( $_SERVER['SERVER_ADDR'] ) || ! empty( $_SERVER['LOCAL_ADDR'] ) ) { 
  110. $ip = ! empty( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR']; 
  111. $ip_arr = array_map( 'intval', explode( '.', $ip ) ); 
  112. if ( 4 == count( $ip_arr ) ) { 
  113. $return["{$prefix}ip-2-octets"] = implode( '.', array_slice( $ip_arr, 0, 2 ) ); 
  114.  
  115. foreach ( Jetpack::get_available_modules() as $slug ) { 
  116. $return["{$prefix}module-{$slug}"] = Jetpack::is_module_active( $slug ) ? 'on' : 'off'; 
  117.  
  118. return $return; 
  119.  
  120. public static function jetpack_xmlrpc_methods( $methods ) { 
  121. $methods['jetpack.getHeartbeatData'] = array( __CLASS__, 'generate_stats_array' ); 
  122. return $methods; 
  123.  
  124. public function deactivate() { 
  125. // Deal with the old pre-3.0 weekly one. 
  126. if ( $timestamp = wp_next_scheduled( 'jetpack_heartbeat' ) ) { 
  127. wp_unschedule_event( $timestamp, 'jetpack_heartbeat' ); 
  128.  
  129. $timestamp = wp_next_scheduled( $this->cron_name ); 
  130. wp_unschedule_event( $timestamp, $this->cron_name ); 
  131.