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. private $results = array(); 
  4.  
  5. private $expected = array(); 
  6.  
  7. private $success = array( 
  8. 'plugin' => array(),  
  9. 'theme' => array(),  
  10. ); 
  11.  
  12. private $failed = array( 
  13. 'plugin' => array(),  
  14. 'theme' => array(),  
  15. ); 
  16.  
  17. private static $instance = null; 
  18.  
  19. static function init() { 
  20. if ( is_null( self::$instance ) ) { 
  21. self::$instance = new Jetpack_Autoupdate; 
  22. return self::$instance; 
  23.  
  24. private function __construct() { 
  25. if ( Jetpack::is_module_active( 'manage' ) ) { 
  26. add_filter( 'auto_update_plugin', array( $this, 'autoupdate_plugin' ), 10, 2 ); 
  27. add_filter( 'auto_update_theme', array( $this, 'autoupdate_theme' ), 10, 2 ); 
  28. add_filter( 'auto_update_core', array( $this, 'autoupdate_core' ), 10, 2 ); 
  29. add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 999, 1 ); 
  30.  
  31. public function autoupdate_plugin( $update, $item ) { 
  32. $autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); 
  33. if ( in_array( $item->plugin, $autoupdate_plugin_list ) ) { 
  34. $this->expect( $item->plugin, 'plugin' ); 
  35. return true; 
  36. return $update; 
  37.  
  38. public function autoupdate_theme( $update, $item ) { 
  39. $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() ); 
  40. if ( in_array( $item->theme , $autoupdate_theme_list) ) { 
  41. $this->expect( $item->theme, 'theme' ); 
  42. return true; 
  43. return $update; 
  44.  
  45. public function autoupdate_core( $update, $item ) { 
  46. $autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false ); 
  47. if ( $autoupdate_core ) { 
  48. return $autoupdate_core; 
  49. return $update; 
  50.  
  51. /** 
  52. * Stores the an item identifier to the expected array. 
  53. * @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme' 
  54. * @param string $type 'plugin' or 'theme' 
  55. */ 
  56. private function expect( $item, $type ) { 
  57. if ( ! isset( $this->expected[ $type ] ) ) { 
  58. $this->expected[ $type ] = array(); 
  59. $this->expected[ $type ][] = $item; 
  60.  
  61. /** 
  62. * On completion of an automatic update, let's store the results. 
  63. * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty. 
  64. */ 
  65. public function automatic_updates_complete( $results ) { 
  66. if ( empty( $this->expected ) ) { 
  67. return; 
  68. $this->results = empty( $results ) ? self::get_possible_failures() : $results; 
  69.  
  70. add_action( 'shutdown', array( $this, 'bump_stats' ) ); 
  71.  
  72. Jetpack::init(); 
  73.  
  74. $items_to_log = array( 'plugin', 'theme' ); 
  75. foreach( $items_to_log as $items ) { 
  76. $this->log_items( $items ); 
  77.  
  78. Jetpack::log( 'autoupdates', $this->get_log() ); 
  79.  
  80. public function get_log() { 
  81. return array( 
  82. 'results' => $this->results,  
  83. 'failed' => $this->failed,  
  84. 'success' => $this->success 
  85. ); 
  86.  
  87. /** 
  88. * Iterates through expected items ( plugins or themes ) and compares them to actual results. 
  89. * @param $items 'plugin' or 'theme' 
  90. */ 
  91. private function log_items( $items ) { 
  92.  
  93. if ( ! isset( $this->expected[ $items ] ) ) { 
  94. return; 
  95.  
  96. $item_results = $this->get_successful_updates( $items ); 
  97.  
  98. if ( is_array( $this->expected[ $items ] ) ) { 
  99. foreach( $this->expected[ $items ] as $item ) { 
  100. if ( in_array( $item, $item_results ) ) { 
  101. $this->success[ $items ][] = $item; 
  102. } else { 
  103. $this->failed[ $items ][] = $item; 
  104.  
  105. public function bump_stats() { 
  106. $instance = Jetpack::init(); 
  107. $log = array(); 
  108. // Bump numbers 
  109. if ( ! empty( $this->success['plugin'] ) ) { 
  110. $instance->stat( 'autoupdates/plugin-success', count( $this->success['plugin'] ) ); 
  111. $log['plugins_success'] = $this->success['plugin']; 
  112.  
  113. if ( ! empty( $this->failed['plugin'] ) ) { 
  114. $instance->stat( 'autoupdates/plugin-fail', count( $this->failed['plugin'] ) ); 
  115. $log['plugins_failed'] = $this->failed['plugin']; 
  116.  
  117. if ( ! empty( $this->success['theme'] ) ) { 
  118. $instance->stat( 'autoupdates/theme-success', count( $this->success['theme'] ) ); 
  119. $log['themes_success'] = $this->success['theme']; 
  120.  
  121. if ( ! empty( $this->failed['theme'] ) ) { 
  122. $instance->stat( 'autoupdates/theme-fail', count( $this->failed['theme'] ) ); 
  123. $log['themes_failed'] = $this->failed['theme']; 
  124.  
  125. $instance->do_stats( 'server_side' ); 
  126.  
  127. // Send a more detailed log to logstash 
  128. if ( ! empty( $log ) ) { 
  129. Jetpack::load_xml_rpc_client(); 
  130. $xml = new Jetpack_IXR_Client( array( 
  131. 'user_id' => get_current_user_id() 
  132. ) ); 
  133. $log['blog_id'] = Jetpack_Options::get_option( 'id' ); 
  134. $xml->query( 'jetpack.debug_autoupdate', $log ); 
  135.  
  136. /** 
  137. * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items 
  138. * @param string $type 'plugin' or 'theme' 
  139. * @return array 
  140. */ 
  141. private function get_successful_updates( $type ) { 
  142. $successful_updates = array(); 
  143.  
  144. if ( ! isset( $this->results[ $type ] ) ) { 
  145. return $successful_updates; 
  146.  
  147. foreach( $this->results[ $type ] as $result ) { 
  148. if ( $result->result ) { 
  149. switch( $type ) { 
  150. case 'theme': 
  151. $successful_updates[] = $result->item->theme; 
  152. break; 
  153. case 'plugin': 
  154. $successful_updates[] = $result->item->plugin; 
  155.  
  156. return $successful_updates; 
  157.  
  158. static function get_possible_failures() { 
  159. $result = array(); 
  160. // Lets check some reasons why it might not be working as expected 
  161. include_once( ABSPATH . '/wp-admin/includes/admin.php' ); 
  162. include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' ); 
  163. $upgrader = new WP_Automatic_Updater; 
  164.  
  165. if ( $upgrader->is_disabled() ) { 
  166. $result[] = 'autoupdates-disabled'; 
  167. if ( ! is_main_site() ) { 
  168. $result[] = 'is-not-main-site'; 
  169. if ( ! is_main_network() ) { 
  170. $result[] = 'is-not-main-network'; 
  171. if ( $upgrader->is_vcs_checkout( ABSPATH ) ) { 
  172. $result[] = 'site-on-vcs'; 
  173. if ( $upgrader->is_vcs_checkout( WP_PLUGIN_DIR ) ) { 
  174. $result[] = 'plugin-directory-on-vcs'; 
  175. if ( $upgrader->is_vcs_checkout( WP_CONTENT_DIR ) ) { 
  176. $result[] = 'content-directory-on-vcs'; 
  177. $lock = get_option( 'auto_updater.lock' ); 
  178. if ( $lock > ( time() - HOUR_IN_SECONDS ) ) { 
  179. $result[] = 'lock-is-set'; 
  180. $skin = new Automatic_Upgrader_Skin; 
  181. include_once( ABSPATH . 'wp-admin/includes/file.php' ); 
  182. include_once( ABSPATH . 'wp-admin/includes/template.php' ); 
  183. if ( ! $skin->request_filesystem_credentials( false, ABSPATH, false ) ) { 
  184. $result[] = 'no-system-write-access'; 
  185. if ( ! $skin->request_filesystem_credentials( false, WP_PLUGIN_DIR, false ) ) { 
  186. $result[] = 'no-plugin-directory-write-access'; 
  187. if ( ! $skin->request_filesystem_credentials( false, WP_CONTENT_DIR, false ) ) { 
  188. $result[] = 'no-wp-content-directory-write-access'; 
  189. return $result; 
  190.