Нет описания

class.jetpack-heartbeat.php 6.2KB

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