暫無描述

monitor.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Module Name: Monitor
  4. * Module Description: Jetpack’s downtime monitoring will continuously watch your site and alert you the moment that downtime is detected.
  5. * Sort Order: 28
  6. * Recommendation Order: 10
  7. * First Introduced: 2.6
  8. * Requires Connection: Yes
  9. * Requires User Connection: Yes
  10. * Auto Activate: No
  11. * Module Tags: Recommended
  12. * Feature: Security
  13. * Additional Search Queries: monitor, uptime, downtime, monitoring, maintenance, maintenance mode, offline, site is down, site down, down, repair, error
  14. */
  15. use Automattic\Jetpack\Connection\Manager as Connection_Manager;
  16. /**
  17. * Class Jetpack_Monitor
  18. */
  19. class Jetpack_Monitor {
  20. public $module = 'monitor';
  21. function __construct() {
  22. add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) );
  23. add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) );
  24. }
  25. public function activate_module() {
  26. if ( ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ) {
  27. self::update_option_receive_jetpack_monitor_notification( true );
  28. }
  29. }
  30. public function jetpack_modules_loaded() {
  31. Jetpack::enable_module_configurable( $this->module );
  32. }
  33. /**
  34. * Send an API request to check if the monitor is active.
  35. *
  36. * @return mixed
  37. *
  38. * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
  39. */
  40. public function is_active() {
  41. _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
  42. $xml = new Jetpack_IXR_Client( array(
  43. 'user_id' => get_current_user_id()
  44. ) );
  45. $xml->query( 'jetpack.monitor.isActive' );
  46. if ( $xml->isError() ) {
  47. wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
  48. }
  49. return $xml->getResponse();
  50. }
  51. /**
  52. * Whether to receive the notifications.
  53. *
  54. * @param bool $value `true` to enable notifications, `false` to disable them.
  55. *
  56. * @return bool
  57. */
  58. public function update_option_receive_jetpack_monitor_notification( $value ) {
  59. $xml = new Jetpack_IXR_Client( array(
  60. 'user_id' => get_current_user_id()
  61. ) );
  62. $xml->query( 'jetpack.monitor.setNotifications', (bool) $value );
  63. if ( $xml->isError() ) {
  64. wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
  65. }
  66. // To be used only in Jetpack_Core_Json_Api_Endpoints::get_remote_value.
  67. update_option( 'monitor_receive_notifications', (bool) $value );
  68. return true;
  69. }
  70. /**
  71. * Checks the status of notifications for current Jetpack site user.
  72. *
  73. * @since 2.8
  74. * @since 4.1.0 New parameter $die_on_error.
  75. *
  76. * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object.
  77. *
  78. * @return boolean|WP_Error
  79. */
  80. static function user_receives_notifications( $die_on_error = true ) {
  81. $xml = new Jetpack_IXR_Client( array(
  82. 'user_id' => get_current_user_id()
  83. ) );
  84. $xml->query( 'jetpack.monitor.isUserInNotifications' );
  85. if ( $xml->isError() ) {
  86. if ( $die_on_error ) {
  87. wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
  88. } else {
  89. return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage(), array( 'status' => 400 ) );
  90. }
  91. }
  92. return $xml->getResponse();
  93. }
  94. /**
  95. * Send an API request to activate the monitor.
  96. *
  97. * @return bool
  98. *
  99. * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
  100. */
  101. public function activate_monitor() {
  102. _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
  103. $xml = new Jetpack_IXR_Client( array(
  104. 'user_id' => get_current_user_id()
  105. ) );
  106. $xml->query( 'jetpack.monitor.activate' );
  107. if ( $xml->isError() ) {
  108. wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
  109. }
  110. return true;
  111. }
  112. /**
  113. * Send an API request to deactivate the monitor.
  114. *
  115. * @return bool
  116. *
  117. * @deprecated 8.9.0 The method is not being used since Jetpack 4.2.0, to be removed.
  118. */
  119. public function deactivate_monitor() {
  120. _deprecated_function( __METHOD__, 'jetpack-8.9.0' );
  121. $xml = new Jetpack_IXR_Client( array(
  122. 'user_id' => get_current_user_id()
  123. ) );
  124. $xml->query( 'jetpack.monitor.deactivate' );
  125. if ( $xml->isError() ) {
  126. wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) );
  127. }
  128. return true;
  129. }
  130. /*
  131. * Returns date of the last downtime.
  132. *
  133. * @since 4.0.0
  134. * @return date in YYYY-MM-DD HH:mm:ss format
  135. */
  136. public function monitor_get_last_downtime() {
  137. $xml = new Jetpack_IXR_Client();
  138. $xml->query( 'jetpack.monitor.getLastDowntime' );
  139. if ( $xml->isError() ) {
  140. return new WP_Error( 'monitor-downtime', $xml->getErrorMessage() );
  141. }
  142. set_transient( 'monitor_last_downtime', $xml->getResponse(), 10 * MINUTE_IN_SECONDS );
  143. return $xml->getResponse();
  144. }
  145. }
  146. new Jetpack_Monitor;