class.jetpack-heartbeat.php 4.9 KB

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