Nessuna descrizione

class.jetpack-autoupdate.php 8.5KB

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