Jetpack_Autoupdate

Handles items that have been selected for automatic updates.

Defined (1)

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

/class.jetpack-autoupdate.php  
  1. class Jetpack_Autoupdate { 
  2.  
  3. public $updates_allowed; 
  4. public $jetpack; 
  5. public $autoupdate_results; 
  6. public $is_updating = false; 
  7.  
  8. public $autoupdate_expected = array( 
  9. 'plugin'=> array(),  
  10. 'theme' => array(),  
  11. ); 
  12.  
  13. public $log = array( 
  14. 'plugin' => array(),  
  15. 'theme' => array(),  
  16. ); 
  17.  
  18. private static $instance = null; 
  19.  
  20. static function init() { 
  21. if ( is_null( self::$instance ) ) { 
  22. self::$instance = new Jetpack_Autoupdate; 
  23. return self::$instance; 
  24.  
  25. private function __construct() { 
  26.  
  27. $this->updates_allowed = Jetpack::is_module_active( 'manage' ); 
  28. // Only run automatic updates if a user as opted in by activating the manage module. 
  29. if ( $this->updates_allowed ) { 
  30. add_filter( 'auto_update_plugin', array( $this, 'autoupdate_plugin' ), 10, 2 ); 
  31. add_filter( 'auto_update_theme', array( $this, 'autoupdate_theme' ), 10, 2 ); 
  32. add_filter( 'auto_update_core', array( $this, 'autoupdate_core' ), 10, 2 ); 
  33. add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 10, 1 ); 
  34. add_action( 'shutdown', array( $this, 'log_results' ) ); 
  35.  
  36. function autoupdate_plugin( $update, $item ) { 
  37. $autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); 
  38. if ( in_array( $item->plugin, $autoupdate_plugin_list ) ) { 
  39. $this->expect( $item->plugin ); 
  40. return true; 
  41.  
  42. return $update; 
  43.  
  44. function autoupdate_theme( $update, $item ) { 
  45. $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() ); 
  46. if ( in_array( $item->theme , $autoupdate_theme_list) ) { 
  47. $this->expect( $item->theme, $type = 'theme' ); 
  48. return true; 
  49. return $update; 
  50.  
  51. function autoupdate_core( $update, $item ) { 
  52. $autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false ); 
  53. if ( $autoupdate_core ) { 
  54. return $autoupdate_core; 
  55. return $update; 
  56.  
  57. /** 
  58. * Stores the an item identifier to the autoupdate_expected array. 
  59. * @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' 
  60. * @param string $type 'plugin' or 'theme' 
  61. */ 
  62. function expect( $item, $type='plugin' ) { 
  63. $this->is_updating = true; 
  64. $this->autoupdate_expected[ $type ][] = $item; 
  65.  
  66. /** 
  67. * On completion of an automatic update, let's store the results. 
  68. * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty. 
  69. */ 
  70. function automatic_updates_complete( $results ) { 
  71. $this->autoupdate_results = $results; 
  72.  
  73. /** 
  74. * On shutdown, let's check to see if we've preformed an automatic update. 
  75. * If so, let's compare the expected results to the actual results, and log our findings. 
  76. * Results are logged locally via Jetpack::log(), and globally via Jetpack::do_stats() 
  77. */ 
  78. function log_results() { 
  79.  
  80. if ( $this->is_updating ) { 
  81.  
  82. $this->jetpack = Jetpack::init(); 
  83. $items_to_log = array( 'plugin', 'theme' ); 
  84.  
  85. foreach( $items_to_log as $items ) { 
  86. $this->log_items( $items ); 
  87.  
  88. $this->jetpack->do_stats( 'server_side' ); 
  89. $this->jetpack->log( 'autoupdates', $this->log ); 
  90.  
  91. /** 
  92. * Iterates through expected items ( plugins or themes ) and compares them to actual results. 
  93. * @param $items 'plugin' or 'theme' 
  94. */ 
  95. function log_items( $items ) { 
  96. $num_items_updated = 0; 
  97. $num_items_failed = 0; 
  98. $item_results = $this->get_successful_updates( $items ); 
  99. $items_failed = array(); 
  100. $items_success = array(); 
  101.  
  102. foreach( $this->autoupdate_expected[ $items ] as $item ) { 
  103. if ( in_array( $item, $item_results ) ) { 
  104. $num_items_updated++; 
  105. $this->log[ $items ][ $item ] = true; 
  106. $items_success[] = $item; 
  107. } else { 
  108. $num_items_failed++; 
  109. $this->log[ $items ][ $item ] = new WP_Error( "$items-fail", $this->get_error_message( $item, $type = $items ) ); 
  110. $items_failed[] = $item; 
  111.  
  112. if ( $num_items_updated ) { 
  113. $this->jetpack->stat( "autoupdates/$items-success", $num_items_updated ); 
  114.  
  115. if ( $num_items_failed ) { 
  116. // bump stats 
  117. $this->jetpack->stat( "autoupdates/$items-fail", $num_items_failed ); 
  118.  
  119. Jetpack::load_xml_rpc_client(); 
  120. $xml = new Jetpack_IXR_Client( array( 
  121. 'user_id' => get_current_user_id() 
  122. ) ); 
  123. $request = array( 
  124. 'plugins' => $items_failed,  
  125. 'success' => $items_success,  
  126. 'blog_id' => Jetpack_Options::get_option( 'id' ),  
  127. ); 
  128. $xml->query( 'jetpack.debug_autoupdate', $request ); 
  129.  
  130.  
  131. /** 
  132. * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items 
  133. * @param string $type 'plugin' or 'theme' 
  134. * @return array 
  135. */ 
  136. private function get_successful_updates( $type = 'plugin' ) { 
  137. $successful_updates = array(); 
  138.  
  139. if ( ! isset( $this->autoupdate_results[ $type ] ) ) { 
  140. return $successful_updates; 
  141.  
  142. foreach( $this->autoupdate_results[ $type ] as $result ) { 
  143. if ( $result->result ) { 
  144. switch( $type ) { 
  145. case 'theme': 
  146. $successful_updates[] = $result->item->theme; 
  147. break; 
  148. default: 
  149. $successful_updates[] = $result->item->plugin; 
  150.  
  151. return $successful_updates; 
  152.  
  153. /** 
  154. * Cycles through results generated by WP_Automatic_Updater to find the messages for the given item and item type. 
  155. * @param $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' 
  156. * @param string $type 'plugin' or 'theme' 
  157. * @return bool|string 
  158. */ 
  159. private function get_error_message( $item, $type = 'plugin' ) { 
  160. if ( ! isset( $this->autoupdate_results[ $type ] ) ) { 
  161. return false; 
  162. foreach( $this->autoupdate_results[ $type ] as $result ) { 
  163. switch( $type ) { 
  164. case 'theme': 
  165. $id = $result->item->theme; 
  166. break; 
  167. default: 
  168. $id = $result->item->plugin; 
  169. if ( $id == $item && isset( $result->messages ) ) { 
  170. return implode( ', ', $result->messages ); 
  171. return false; 
  172.