class-wc-helper-updater.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * WC_Helper_Updater Class
  7. *
  8. * Contains the logic to fetch available updates and hook into Core's update
  9. * routines to serve WooCommerce.com-provided packages.
  10. */
  11. class WC_Helper_Updater {
  12. /**
  13. * Loads the class, runs on init.
  14. */
  15. public static function load() {
  16. add_action( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'transient_update_plugins' ), 21, 1 );
  17. add_action( 'pre_set_site_transient_update_themes', array( __CLASS__, 'transient_update_themes' ), 21, 1 );
  18. add_action( 'upgrader_process_complete', array( __CLASS__, 'upgrader_process_complete' ) );
  19. }
  20. /**
  21. * Runs in a cron thread, or in a visitor thread if triggered
  22. * by _maybe_update_plugins(), or in an auto-update thread.
  23. *
  24. * @param object $transient The update_plugins transient object.
  25. *
  26. * @return object The same or a modified version of the transient.
  27. */
  28. public static function transient_update_plugins( $transient ) {
  29. $update_data = self::get_update_data();
  30. foreach ( WC_Helper::get_local_woo_plugins() as $plugin ) {
  31. if ( empty( $update_data[ $plugin['_product_id'] ] ) ) {
  32. continue;
  33. }
  34. $data = $update_data[ $plugin['_product_id'] ];
  35. $filename = $plugin['_filename'];
  36. $item = array(
  37. 'id' => 'woocommerce-com-' . $plugin['_product_id'],
  38. 'slug' => 'woocommerce-com-' . $data['slug'],
  39. 'plugin' => $filename,
  40. 'new_version' => $data['version'],
  41. 'url' => $data['url'],
  42. 'package' => '',
  43. 'upgrade_notice' => $data['upgrade_notice'],
  44. );
  45. if ( self::_has_active_subscription( $plugin['_product_id'] ) ) {
  46. $item['package'] = $data['package'];
  47. }
  48. if ( version_compare( $plugin['Version'], $data['version'], '<' ) ) {
  49. $transient->response[ $filename ] = (object) $item;
  50. unset( $transient->no_update[ $filename ] );
  51. } else {
  52. $transient->no_update[ $filename ] = (object) $item;
  53. unset( $transient->response[ $filename ] );
  54. }
  55. }
  56. return $transient;
  57. }
  58. /**
  59. * Runs on pre_set_site_transient_update_themes, provides custom
  60. * packages for WooCommerce.com-hosted extensions.
  61. *
  62. * @param object $transient The update_themes transient object.
  63. *
  64. * @return object The same or a modified version of the transient.
  65. */
  66. public static function transient_update_themes( $transient ) {
  67. $update_data = self::get_update_data();
  68. foreach ( WC_Helper::get_local_woo_themes() as $theme ) {
  69. if ( empty( $update_data[ $theme['_product_id'] ] ) ) {
  70. continue;
  71. }
  72. $data = $update_data[ $theme['_product_id'] ];
  73. $slug = $theme['_stylesheet'];
  74. $item = array(
  75. 'theme' => $slug,
  76. 'new_version' => $data['version'],
  77. 'url' => $data['url'],
  78. 'package' => '',
  79. );
  80. if ( self::_has_active_subscription( $theme['_product_id'] ) ) {
  81. $item['package'] = $data['package'];
  82. }
  83. if ( version_compare( $theme['Version'], $data['version'], '<' ) ) {
  84. $transient->response[ $slug ] = $item;
  85. } else {
  86. unset( $transient->response[ $slug ] );
  87. $transient->checked[ $slug ] = $data['version'];
  88. }
  89. }
  90. return $transient;
  91. }
  92. /**
  93. * Get update data for all extensions.
  94. *
  95. * Scans through all subscriptions for the connected user, as well
  96. * as all Woo extensions without a subscription, and obtains update
  97. * data for each product.
  98. *
  99. * @return array Update data {product_id => data}
  100. */
  101. public static function get_update_data() {
  102. $payload = array();
  103. // Scan subscriptions.
  104. foreach ( WC_Helper::get_subscriptions() as $subscription ) {
  105. $payload[ $subscription['product_id'] ] = array(
  106. 'product_id' => $subscription['product_id'],
  107. 'file_id' => '',
  108. );
  109. }
  110. // Scan local plugins which may or may not have a subscription.
  111. foreach ( WC_Helper::get_local_woo_plugins() as $data ) {
  112. if ( ! isset( $payload[ $data['_product_id'] ] ) ) {
  113. $payload[ $data['_product_id'] ] = array(
  114. 'product_id' => $data['_product_id'],
  115. );
  116. }
  117. $payload[ $data['_product_id'] ]['file_id'] = $data['_file_id'];
  118. }
  119. // Scan local themes
  120. foreach ( WC_Helper::get_local_woo_themes() as $data ) {
  121. if ( ! isset( $payload[ $data['_product_id'] ] ) ) {
  122. $payload[ $data['_product_id'] ] = array(
  123. 'product_id' => $data['_product_id'],
  124. );
  125. }
  126. $payload[ $data['_product_id'] ]['file_id'] = $data['_file_id'];
  127. }
  128. return self::_update_check( $payload );
  129. }
  130. /**
  131. * Run an update check API call.
  132. *
  133. * The call is cached based on the payload (product ids, file ids). If
  134. * the payload changes, the cache is going to miss.
  135. *
  136. * @return array Update data for each requested product.
  137. */
  138. private static function _update_check( $payload ) {
  139. ksort( $payload );
  140. $hash = md5( json_encode( $payload ) );
  141. $cache_key = '_woocommerce_helper_updates';
  142. if ( false !== ( $data = get_transient( $cache_key ) ) ) {
  143. if ( hash_equals( $hash, $data['hash'] ) ) {
  144. return $data['products'];
  145. }
  146. }
  147. $data = array(
  148. 'hash' => $hash,
  149. 'updated' => time(),
  150. 'products' => array(),
  151. 'errors' => array(),
  152. );
  153. $request = WC_Helper_API::post(
  154. 'update-check', array(
  155. 'body' => json_encode( array( 'products' => $payload ) ),
  156. 'authenticated' => true,
  157. )
  158. );
  159. if ( wp_remote_retrieve_response_code( $request ) !== 200 ) {
  160. $data['errors'][] = 'http-error';
  161. } else {
  162. $data['products'] = json_decode( wp_remote_retrieve_body( $request ), true );
  163. }
  164. set_transient( $cache_key, $data, 12 * HOUR_IN_SECONDS );
  165. return $data['products'];
  166. }
  167. /**
  168. * Check for an active subscription.
  169. *
  170. * Checks a given product id against all subscriptions on
  171. * the current site. Returns true if at least one active
  172. * subscription is found.
  173. *
  174. * @param int $product_id The product id to look for.
  175. *
  176. * @return bool True if active subscription found.
  177. */
  178. private static function _has_active_subscription( $product_id ) {
  179. if ( ! isset( $auth ) ) {
  180. $auth = WC_Helper_Options::get( 'auth' );
  181. }
  182. if ( ! isset( $subscriptions ) ) {
  183. $subscriptions = WC_Helper::get_subscriptions();
  184. }
  185. if ( empty( $auth['site_id'] ) || empty( $subscriptions ) ) {
  186. return false;
  187. }
  188. // Check for an active subscription.
  189. foreach ( $subscriptions as $subscription ) {
  190. if ( $subscription['product_id'] != $product_id ) {
  191. continue;
  192. }
  193. if ( in_array( absint( $auth['site_id'] ), $subscription['connections'] ) ) {
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. /**
  200. * Get the number of products that have updates.
  201. *
  202. * @return int The number of products with updates.
  203. */
  204. public static function get_updates_count() {
  205. $cache_key = '_woocommerce_helper_updates_count';
  206. if ( false !== ( $count = get_transient( $cache_key ) ) ) {
  207. return $count;
  208. }
  209. // Don't fetch any new data since this function in high-frequency.
  210. if ( ! get_transient( '_woocommerce_helper_subscriptions' ) ) {
  211. return 0;
  212. }
  213. if ( ! get_transient( '_woocommerce_helper_updates' ) ) {
  214. return 0;
  215. }
  216. $count = 0;
  217. $update_data = self::get_update_data();
  218. if ( empty( $update_data ) ) {
  219. set_transient( $cache_key, $count, 12 * HOUR_IN_SECONDS );
  220. return $count;
  221. }
  222. // Scan local plugins.
  223. foreach ( WC_Helper::get_local_woo_plugins() as $plugin ) {
  224. if ( empty( $update_data[ $plugin['_product_id'] ] ) ) {
  225. continue;
  226. }
  227. if ( version_compare( $plugin['Version'], $update_data[ $plugin['_product_id'] ]['version'], '<' ) ) {
  228. $count++;
  229. }
  230. }
  231. // Scan local themes.
  232. foreach ( WC_Helper::get_local_woo_themes() as $theme ) {
  233. if ( empty( $update_data[ $theme['_product_id'] ] ) ) {
  234. continue;
  235. }
  236. if ( version_compare( $theme['Version'], $update_data[ $theme['_product_id'] ]['version'], '<' ) ) {
  237. $count++;
  238. }
  239. }
  240. set_transient( $cache_key, $count, 12 * HOUR_IN_SECONDS );
  241. return $count;
  242. }
  243. /**
  244. * Return the updates count markup.
  245. *
  246. * @return string Updates count markup, empty string if no updates avairable.
  247. */
  248. public static function get_updates_count_html() {
  249. $count = self::get_updates_count();
  250. if ( ! $count ) {
  251. return '';
  252. }
  253. $count_html = sprintf( '<span class="update-plugins count-%d"><span class="update-count">%d</span></span>', $count, number_format_i18n( $count ) );
  254. return $count_html;
  255. }
  256. /**
  257. * Flushes cached update data.
  258. */
  259. public static function flush_updates_cache() {
  260. delete_transient( '_woocommerce_helper_updates' );
  261. delete_transient( '_woocommerce_helper_updates_count' );
  262. delete_site_transient( 'update_plugins' );
  263. delete_site_transient( 'update_themes' );
  264. }
  265. /**
  266. * Fires when a user successfully updated a theme or a plugin.
  267. */
  268. public static function upgrader_process_complete() {
  269. delete_transient( '_woocommerce_helper_updates_count' );
  270. }
  271. }
  272. WC_Helper_Updater::load();