class.jetpack-autoupdate.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Handles items that have been selected for automatic updates.
  4. * Hooks into WP_Automatic_Updater
  5. */
  6. class Jetpack_Autoupdate {
  7. private $results = array();
  8. private $expected = array();
  9. private $success = array(
  10. 'plugin' => array(),
  11. 'theme' => array(),
  12. );
  13. private $failed = array(
  14. 'plugin' => array(),
  15. 'theme' => array(),
  16. );
  17. private static $instance = null;
  18. static function init() {
  19. if ( is_null( self::$instance ) ) {
  20. self::$instance = new Jetpack_Autoupdate;
  21. }
  22. return self::$instance;
  23. }
  24. private function __construct() {
  25. if ( Jetpack::is_module_active( 'manage' ) ) {
  26. add_filter( 'auto_update_theme', array( $this, 'autoupdate_theme' ), 10, 2 );
  27. add_filter( 'auto_update_core', array( $this, 'autoupdate_core' ), 10, 2 );
  28. add_filter( 'auto_update_translation', array( $this, 'autoupdate_translation' ), 10, 2 );
  29. add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 999, 1 );
  30. }
  31. }
  32. public function autoupdate_translation( $update, $item ) {
  33. // Autoupdate all translations
  34. if ( Jetpack_Options::get_option( 'autoupdate_translations', false ) ) {
  35. return true;
  36. }
  37. // Themes
  38. $autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() );
  39. $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
  40. /*
  41. $item = {
  42. "type":"theme",
  43. "slug":"twentyfourteen",
  44. "language":"en_CA",
  45. "version":"1.8",
  46. "updated":"2015-07-18 11:27:20",
  47. "package":"https:\/\/downloads.wordpress.org\/translation\/theme\/twentyfourteen\/1.8\/en_CA.zip",
  48. "autoupdate":true
  49. }
  50. */
  51. if ( ( in_array( $item->slug, $autoupdate_themes_translations )
  52. || in_array( $item->slug, $autoupdate_theme_list ) )
  53. && 'theme' === $item->type
  54. ) {
  55. $this->expect( $item->type . ':' . $item->slug, 'translation' );
  56. return true;
  57. }
  58. // Plugins
  59. $autoupdate_plugin_translations = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() );
  60. $autoupdate_plugin_list = Jetpack_Options::get_option( 'autoupdate_plugins', array() );
  61. $plugin_files = array_unique( array_merge( $autoupdate_plugin_list, $autoupdate_plugin_translations ) );
  62. $plugin_slugs = array_map( array( __CLASS__, 'get_plugin_slug' ), $plugin_files );
  63. if ( in_array( $item->slug, $plugin_slugs )
  64. && 'plugin' === $item->type
  65. ) {
  66. $this->expect( $item->type . ':' . $item->slug, 'translation' );
  67. return true;
  68. }
  69. return $update;
  70. }
  71. public function autoupdate_theme( $update, $item ) {
  72. $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
  73. if ( in_array( $item->theme, $autoupdate_theme_list ) ) {
  74. $this->expect( $item->theme, 'theme' );
  75. return true;
  76. }
  77. return $update;
  78. }
  79. public function autoupdate_core( $update, $item ) {
  80. $autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false );
  81. if ( $autoupdate_core ) {
  82. return $autoupdate_core;
  83. }
  84. return $update;
  85. }
  86. /**
  87. * Stores the an item identifier to the expected array.
  88. *
  89. * @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme'
  90. * @param string $type 'plugin' or 'theme'
  91. */
  92. private function expect( $item, $type ) {
  93. if ( ! isset( $this->expected[ $type ] ) ) {
  94. $this->expected[ $type ] = array();
  95. }
  96. $this->expected[ $type ][] = $item;
  97. }
  98. /**
  99. * On completion of an automatic update, let's store the results.
  100. *
  101. * @param $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty.
  102. */
  103. public function automatic_updates_complete( $results ) {
  104. if ( empty( $this->expected ) ) {
  105. return;
  106. }
  107. $this->results = empty( $results ) ? self::get_possible_failures() : $results;
  108. add_action( 'shutdown', array( $this, 'bump_stats' ) );
  109. Jetpack::init();
  110. $items_to_log = array( 'plugin', 'theme', 'translation' );
  111. foreach ( $items_to_log as $items ) {
  112. $this->log_items( $items );
  113. }
  114. Jetpack::log( 'autoupdates', $this->get_log() );
  115. }
  116. public function get_log() {
  117. return array(
  118. 'results' => $this->results,
  119. 'failed' => $this->failed,
  120. 'success' => $this->success
  121. );
  122. }
  123. /**
  124. * Iterates through expected items ( plugins or themes ) and compares them to actual results.
  125. *
  126. * @param $items 'plugin' or 'theme'
  127. */
  128. private function log_items( $items ) {
  129. if ( ! isset( $this->expected[ $items ] ) ) {
  130. return;
  131. }
  132. $item_results = $this->get_successful_updates( $items );
  133. if ( is_array( $this->expected[ $items ] ) ) {
  134. foreach ( $this->expected[ $items ] as $item ) {
  135. if ( in_array( $item, $item_results ) ) {
  136. $this->success[ $items ][] = $item;
  137. } else {
  138. $this->failed[ $items ][] = $item;
  139. }
  140. }
  141. }
  142. }
  143. public function bump_stats() {
  144. $instance = Jetpack::init();
  145. $log = array();
  146. // Bump numbers
  147. if ( ! empty( $this->success['theme'] ) ) {
  148. $instance->stat( 'autoupdates/theme-success', count( $this->success['theme'] ) );
  149. $log['themes_success'] = $this->success['theme'];
  150. }
  151. if ( ! empty( $this->failed['theme'] ) ) {
  152. $instance->stat( 'autoupdates/theme-fail', count( $this->failed['theme'] ) );
  153. $log['themes_failed'] = $this->failed['theme'];
  154. }
  155. $instance->do_stats( 'server_side' );
  156. // Send a more detailed log to logstash
  157. if ( ! empty( $log ) ) {
  158. Jetpack::load_xml_rpc_client();
  159. $xml = new Jetpack_IXR_Client( array(
  160. 'user_id' => get_current_user_id()
  161. ) );
  162. $log['blog_id'] = Jetpack_Options::get_option( 'id' );
  163. $xml->query( 'jetpack.debug_autoupdate', $log );
  164. }
  165. }
  166. /**
  167. * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items
  168. *
  169. * @param string $type 'plugin' or 'theme'
  170. *
  171. * @return array
  172. */
  173. private function get_successful_updates( $type ) {
  174. $successful_updates = array();
  175. if ( ! isset( $this->results[ $type ] ) ) {
  176. return $successful_updates;
  177. }
  178. foreach ( $this->results[ $type ] as $result ) {
  179. if ( $result->result ) {
  180. switch ( $type ) {
  181. case 'theme':
  182. $successful_updates[] = $result->item->theme;
  183. break;
  184. case 'translation':
  185. $successful_updates[] = $result->item->type . ':' . $result->item->slug;
  186. break;
  187. }
  188. }
  189. }
  190. return $successful_updates;
  191. }
  192. static function get_possible_failures() {
  193. $result = array();
  194. // Lets check some reasons why it might not be working as expected
  195. include_once( ABSPATH . '/wp-admin/includes/admin.php' );
  196. include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
  197. $upgrader = new WP_Automatic_Updater;
  198. if ( $upgrader->is_disabled() ) {
  199. $result[] = 'autoupdates-disabled';
  200. }
  201. if ( ! is_main_site() ) {
  202. $result[] = 'is-not-main-site';
  203. }
  204. if ( ! is_main_network() ) {
  205. $result[] = 'is-not-main-network';
  206. }
  207. if ( $upgrader->is_vcs_checkout( ABSPATH ) ) {
  208. $result[] = 'site-on-vcs';
  209. }
  210. if ( $upgrader->is_vcs_checkout( WP_PLUGIN_DIR ) ) {
  211. $result[] = 'plugin-directory-on-vcs';
  212. }
  213. if ( $upgrader->is_vcs_checkout( WP_CONTENT_DIR ) ) {
  214. $result[] = 'content-directory-on-vcs';
  215. }
  216. $lock = get_option( 'auto_updater.lock' );
  217. if ( $lock > ( time() - HOUR_IN_SECONDS ) ) {
  218. $result[] = 'lock-is-set';
  219. }
  220. $skin = new Automatic_Upgrader_Skin;
  221. include_once( ABSPATH . 'wp-admin/includes/file.php' );
  222. include_once( ABSPATH . 'wp-admin/includes/template.php' );
  223. if ( ! $skin->request_filesystem_credentials( false, ABSPATH, false ) ) {
  224. $result[] = 'no-system-write-access';
  225. }
  226. if ( ! $skin->request_filesystem_credentials( false, WP_PLUGIN_DIR, false ) ) {
  227. $result[] = 'no-plugin-directory-write-access';
  228. }
  229. if ( ! $skin->request_filesystem_credentials( false, WP_CONTENT_DIR, false ) ) {
  230. $result[] = 'no-wp-content-directory-write-access';
  231. }
  232. return $result;
  233. }
  234. static function get_plugin_slug( $plugin_file ) {
  235. $update_plugins = get_site_transient( 'update_plugins' );
  236. if ( isset( $update_plugins->no_update ) ) {
  237. if ( isset( $update_plugins->no_update[ $plugin_file ] ) ) {
  238. $slug = $update_plugins->no_update[ $plugin_file ]->slug;
  239. }
  240. }
  241. if ( empty( $slug ) && isset( $update_plugins->response ) ) {
  242. if ( isset( $update_plugins->response[ $plugin_file ] ) ) {
  243. $slug = $update_plugins->response[ $plugin_file ]->slug;
  244. }
  245. }
  246. // Try to infer from the plugin file if not cached
  247. if ( empty( $slug) ) {
  248. $slug = dirname( $plugin_file );
  249. if ( '.' === $slug ) {
  250. $slug = preg_replace("/(.+)\.php$/", "$1", $plugin_file );
  251. }
  252. }
  253. return $slug;
  254. }
  255. }
  256. Jetpack_Autoupdate::init();