class.jetpack-sync-module-updates.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. class Jetpack_Sync_Module_Updates extends Jetpack_Sync_Module {
  3. const UPDATES_CHECKSUM_OPTION_NAME = 'jetpack_updates_sync_checksum';
  4. private $old_wp_version = null;
  5. function name() {
  6. return 'updates';
  7. }
  8. public function init_listeners( $callable ) {
  9. global $wp_version;
  10. $this->old_wp_version = $wp_version;
  11. add_action( 'set_site_transient_update_plugins', array( $this, 'validate_update_change' ), 10, 3 );
  12. add_action( 'set_site_transient_update_themes', array( $this, 'validate_update_change' ), 10, 3 );
  13. add_action( 'set_site_transient_update_core', array( $this, 'validate_update_change' ), 10, 3 );
  14. add_action( 'jetpack_update_plugins_change', $callable );
  15. add_action( 'jetpack_update_themes_change', $callable );
  16. add_action( 'jetpack_update_core_change', $callable );
  17. add_filter( 'jetpack_sync_before_enqueue_jetpack_update_plugins_change', array(
  18. $this,
  19. 'filter_update_keys',
  20. ), 10, 2 );
  21. add_filter( 'jetpack_sync_before_enqueue_upgrader_process_complete', array(
  22. $this,
  23. 'filter_upgrader_process_complete',
  24. ), 10, 2 );
  25. add_action( 'automatic_updates_complete', $callable );
  26. if ( is_multisite() ) {
  27. add_filter( 'pre_update_site_option_wpmu_upgrade_site', array ( $this, 'update_core_network_event' ), 10, 2 );
  28. add_action( 'jetpack_sync_core_update_network', $callable, 10, 3 );
  29. }
  30. // Send data when update completes
  31. add_action( '_core_updated_successfully', array( $this, 'update_core' ) );
  32. add_action( 'jetpack_sync_core_reinstalled_successfully', $callable );
  33. add_action( 'jetpack_sync_core_autoupdated_successfully', $callable, 10, 2 );
  34. add_action( 'jetpack_sync_core_updated_successfully', $callable, 10, 2 );
  35. }
  36. public function init_full_sync_listeners( $callable ) {
  37. add_action( 'jetpack_full_sync_updates', $callable );
  38. }
  39. public function init_before_send() {
  40. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_updates', array( $this, 'expand_updates' ) );
  41. add_filter( 'jetpack_sync_before_send_jetpack_update_themes_change', array( $this, 'expand_themes' ) );
  42. }
  43. public function update_core_network_event( $wp_db_version, $old_wp_db_version ) {
  44. global $wp_version;
  45. /**
  46. * Sync event for when core wp network updates to a new db version
  47. *
  48. * @since 5.0.0
  49. *
  50. * @param int $wp_db_version the latest wp_db_version
  51. * @param int $old_wp_db_version previous wp_db_version
  52. * @param string $wp_version the latest wp_version
  53. *
  54. */
  55. do_action( 'jetpack_sync_core_update_network', $wp_db_version, $old_wp_db_version, $wp_version );
  56. return $wp_db_version;
  57. }
  58. public function update_core( $new_wp_version ) {
  59. global $pagenow;
  60. if ( isset( $_GET[ 'action' ] ) && 'do-core-reinstall' === $_GET[ 'action' ] ) {
  61. /**
  62. * Sync event that fires when core reinstall was successful
  63. *
  64. * @since 5.0.0
  65. *
  66. * @param string $new_wp_version the updated WordPress version
  67. */
  68. do_action( 'jetpack_sync_core_reinstalled_successfully', $new_wp_version );
  69. return;
  70. }
  71. // Core was autoudpated
  72. if (
  73. 'update-core.php' !== $pagenow &&
  74. ! Jetpack_Constants::is_true( 'REST_API_REQUEST' ) // wp.com rest api calls should never be marked as a core autoupdate
  75. ) {
  76. /**
  77. * Sync event that fires when core autoupdate was successful
  78. *
  79. * @since 5.0.0
  80. *
  81. * @param string $new_wp_version the updated WordPress version
  82. * @param string $old_wp_version the previous WordPress version
  83. */
  84. do_action( 'jetpack_sync_core_autoupdated_successfully', $new_wp_version, $this->old_wp_version );
  85. return;
  86. }
  87. /**
  88. * Sync event that fires when core update was successful
  89. *
  90. * @since 5.0.0
  91. *
  92. * @param string $new_wp_version the updated WordPress version
  93. * @param string $old_wp_version the previous WordPress version
  94. */
  95. do_action( 'jetpack_sync_core_updated_successfully', $new_wp_version, $this->old_wp_version );
  96. return;
  97. }
  98. public function get_update_checksum( $update, $transient ) {
  99. $updates = array();
  100. $no_updated = array();
  101. switch ( $transient ) {
  102. case 'update_plugins':
  103. if ( ! empty( $update->response ) && is_array( $update->response ) ) {
  104. foreach ( $update->response as $plugin_slug => $response ) {
  105. if ( ! empty( $plugin_slug ) && isset( $response->new_version ) ) {
  106. $updates[] = array( $plugin_slug => $response->new_version );
  107. }
  108. }
  109. }
  110. if ( ! empty( $update->no_update ) ) {
  111. $no_updated = array_keys( $update->no_update );
  112. }
  113. if ( ! isset( $no_updated[ 'jetpack/jetpack.php' ] ) && isset( $updates[ 'jetpack/jetpack.php' ] ) ) {
  114. return false;
  115. }
  116. break;
  117. case 'update_themes':
  118. if ( ! empty( $update->response ) && is_array( $update->response ) ) {
  119. foreach ( $update->response as $theme_slug => $response ) {
  120. if ( ! empty( $theme_slug ) && isset( $response['new_version'] ) ) {
  121. $updates[] = array( $theme_slug => $response['new_version'] );
  122. }
  123. }
  124. }
  125. if ( ! empty( $update->checked ) ) {
  126. $no_updated = $update->checked;
  127. }
  128. break;
  129. case 'update_core':
  130. if ( ! empty( $update->updates ) && is_array( $update->updates ) ) {
  131. foreach ( $update->updates as $response ) {
  132. if( ! empty( $response->response ) && $response->response === 'latest' ) {
  133. continue;
  134. }
  135. if ( ! empty( $response->response ) && isset( $response->packages->full ) ) {
  136. $updates[] = array( $response->response => $response->packages->full );
  137. }
  138. }
  139. }
  140. if ( ! empty( $update->version_checked ) ) {
  141. $no_updated = $update->version_checked;
  142. }
  143. if ( empty( $updates ) ) {
  144. return false;
  145. }
  146. break;
  147. }
  148. if ( empty( $updates ) && empty( $no_updated ) ) {
  149. return false;
  150. }
  151. return $this->get_check_sum( array( $no_updated, $updates ) );
  152. }
  153. public function validate_update_change( $value, $expiration, $transient ) {
  154. $new_checksum = $this->get_update_checksum( $value, $transient );
  155. if ( false === $new_checksum ) {
  156. return;
  157. }
  158. $checksums = get_option( self::UPDATES_CHECKSUM_OPTION_NAME, array() );
  159. if ( isset( $checksums[ $transient ] ) && $checksums[ $transient ] === $new_checksum ) {
  160. return;
  161. }
  162. $checksums[ $transient ] = $new_checksum;
  163. update_option( self::UPDATES_CHECKSUM_OPTION_NAME, $checksums );
  164. /**
  165. * jetpack_{$transient}_change
  166. * jetpack_update_plugins_change
  167. * jetpack_update_themes_change
  168. * jetpack_update_core_change
  169. *
  170. * @since 5.1.0
  171. *
  172. * @param array containing info that tells us what needs updating
  173. *
  174. */
  175. do_action( "jetpack_{$transient}_change", $value );
  176. }
  177. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
  178. /**
  179. * Tells the client to sync all updates to the server
  180. *
  181. * @since 4.2.0
  182. *
  183. * @param boolean Whether to expand updates (should always be true)
  184. */
  185. do_action( 'jetpack_full_sync_updates', true );
  186. // The number of actions enqueued, and next module state (true == done)
  187. return array( 1, true );
  188. }
  189. public function estimate_full_sync_actions( $config ) {
  190. return 1;
  191. }
  192. function get_full_sync_actions() {
  193. return array( 'jetpack_full_sync_updates' );
  194. }
  195. public function get_all_updates() {
  196. return array(
  197. 'core' => get_site_transient( 'update_core' ),
  198. 'plugins' => get_site_transient( 'update_plugins' ),
  199. 'themes' => get_site_transient( 'update_themes' ),
  200. );
  201. }
  202. // removes unnecessary keys from synced updates data
  203. function filter_update_keys( $args ) {
  204. $updates = $args[0];
  205. if ( isset( $updates->no_update ) ) {
  206. unset( $updates->no_update );
  207. }
  208. return $args;
  209. }
  210. function filter_upgrader_process_complete( $args ) {
  211. array_shift( $args );
  212. return $args;
  213. }
  214. public function expand_updates( $args ) {
  215. if ( $args[0] ) {
  216. return $this->get_all_updates();
  217. }
  218. return $args;
  219. }
  220. public function expand_themes( $args ) {
  221. if ( ! isset( $args[0], $args[0]->response ) ) {
  222. return $args;
  223. }
  224. if ( ! is_array( $args[0]->response ) ) {
  225. trigger_error( 'Warning: Not an Array as expected but -> ' . wp_json_encode( $args[0]->response ) . ' instead', E_USER_WARNING );
  226. return $args;
  227. }
  228. foreach ( $args[0]->response as $stylesheet => &$theme_data ) {
  229. $theme = wp_get_theme( $stylesheet );
  230. $theme_data['name'] = $theme->name;
  231. }
  232. return $args;
  233. }
  234. public function reset_data() {
  235. delete_option( self::UPDATES_CHECKSUM_OPTION_NAME );
  236. }
  237. }