暫無描述

class-wc-helper-updater.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. /**
  3. * The update helper for WooCommerce.com plugins.
  4. *
  5. * @class WC_Helper_Updater
  6. * @package WooCommerce\Admin\Helper
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * WC_Helper_Updater Class
  13. *
  14. * Contains the logic to fetch available updates and hook into Core's update
  15. * routines to serve WooCommerce.com-provided packages.
  16. */
  17. class WC_Helper_Updater {
  18. /**
  19. * Loads the class, runs on init.
  20. */
  21. public static function load() {
  22. add_action( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'transient_update_plugins' ), 21, 1 );
  23. add_action( 'pre_set_site_transient_update_themes', array( __CLASS__, 'transient_update_themes' ), 21, 1 );
  24. add_action( 'upgrader_process_complete', array( __CLASS__, 'upgrader_process_complete' ) );
  25. add_action( 'upgrader_pre_download', array( __CLASS__, 'block_expired_updates' ), 10, 2 );
  26. }
  27. /**
  28. * Runs in a cron thread, or in a visitor thread if triggered
  29. * by _maybe_update_plugins(), or in an auto-update thread.
  30. *
  31. * @param object $transient The update_plugins transient object.
  32. *
  33. * @return object The same or a modified version of the transient.
  34. */
  35. public static function transient_update_plugins( $transient ) {
  36. $update_data = self::get_update_data();
  37. foreach ( WC_Helper::get_local_woo_plugins() as $plugin ) {
  38. if ( empty( $update_data[ $plugin['_product_id'] ] ) ) {
  39. continue;
  40. }
  41. $data = $update_data[ $plugin['_product_id'] ];
  42. $filename = $plugin['_filename'];
  43. $item = array(
  44. 'id' => 'woocommerce-com-' . $plugin['_product_id'],
  45. 'slug' => 'woocommerce-com-' . $data['slug'],
  46. 'plugin' => $filename,
  47. 'new_version' => $data['version'],
  48. 'url' => $data['url'],
  49. 'package' => $data['package'],
  50. 'upgrade_notice' => $data['upgrade_notice'],
  51. );
  52. if ( isset( $data['requires_php'] ) ) {
  53. $item['requires_php'] = $data['requires_php'];
  54. }
  55. // We don't want to deliver a valid upgrade package when their subscription has expired.
  56. // To avoid the generic "no_package" error that empty strings give, we will store an
  57. // indication of expiration for the `upgrader_pre_download` filter to error on.
  58. if ( ! self::_has_active_subscription( $plugin['_product_id'] ) ) {
  59. $item['package'] = 'woocommerce-com-expired-' . $plugin['_product_id'];
  60. }
  61. if ( version_compare( $plugin['Version'], $data['version'], '<' ) ) {
  62. $transient->response[ $filename ] = (object) $item;
  63. unset( $transient->no_update[ $filename ] );
  64. } else {
  65. $transient->no_update[ $filename ] = (object) $item;
  66. unset( $transient->response[ $filename ] );
  67. }
  68. }
  69. $translations = self::get_translations_update_data();
  70. $transient->translations = array_merge( isset( $transient->translations ) ? $transient->translations : array(), $translations );
  71. return $transient;
  72. }
  73. /**
  74. * Runs on pre_set_site_transient_update_themes, provides custom
  75. * packages for WooCommerce.com-hosted extensions.
  76. *
  77. * @param object $transient The update_themes transient object.
  78. *
  79. * @return object The same or a modified version of the transient.
  80. */
  81. public static function transient_update_themes( $transient ) {
  82. $update_data = self::get_update_data();
  83. foreach ( WC_Helper::get_local_woo_themes() as $theme ) {
  84. if ( empty( $update_data[ $theme['_product_id'] ] ) ) {
  85. continue;
  86. }
  87. $data = $update_data[ $theme['_product_id'] ];
  88. $slug = $theme['_stylesheet'];
  89. $item = array(
  90. 'theme' => $slug,
  91. 'new_version' => $data['version'],
  92. 'url' => $data['url'],
  93. 'package' => '',
  94. );
  95. if ( self::_has_active_subscription( $theme['_product_id'] ) ) {
  96. $item['package'] = $data['package'];
  97. }
  98. if ( version_compare( $theme['Version'], $data['version'], '<' ) ) {
  99. $transient->response[ $slug ] = $item;
  100. } else {
  101. unset( $transient->response[ $slug ] );
  102. $transient->checked[ $slug ] = $data['version'];
  103. }
  104. }
  105. return $transient;
  106. }
  107. /**
  108. * Get update data for all extensions.
  109. *
  110. * Scans through all subscriptions for the connected user, as well
  111. * as all Woo extensions without a subscription, and obtains update
  112. * data for each product.
  113. *
  114. * @return array Update data {product_id => data}
  115. */
  116. public static function get_update_data() {
  117. $payload = array();
  118. // Scan subscriptions.
  119. foreach ( WC_Helper::get_subscriptions() as $subscription ) {
  120. $payload[ $subscription['product_id'] ] = array(
  121. 'product_id' => $subscription['product_id'],
  122. 'file_id' => '',
  123. );
  124. }
  125. // Scan local plugins which may or may not have a subscription.
  126. foreach ( WC_Helper::get_local_woo_plugins() as $data ) {
  127. if ( ! isset( $payload[ $data['_product_id'] ] ) ) {
  128. $payload[ $data['_product_id'] ] = array(
  129. 'product_id' => $data['_product_id'],
  130. );
  131. }
  132. $payload[ $data['_product_id'] ]['file_id'] = $data['_file_id'];
  133. }
  134. // Scan local themes.
  135. foreach ( WC_Helper::get_local_woo_themes() as $data ) {
  136. if ( ! isset( $payload[ $data['_product_id'] ] ) ) {
  137. $payload[ $data['_product_id'] ] = array(
  138. 'product_id' => $data['_product_id'],
  139. );
  140. }
  141. $payload[ $data['_product_id'] ]['file_id'] = $data['_file_id'];
  142. }
  143. return self::_update_check( $payload );
  144. }
  145. /**
  146. * Get translations updates informations.
  147. *
  148. * Scans through all subscriptions for the connected user, as well
  149. * as all Woo extensions without a subscription, and obtains update
  150. * data for each product.
  151. *
  152. * @return array Update data {product_id => data}
  153. */
  154. public static function get_translations_update_data() {
  155. $payload = array();
  156. $installed_translations = wp_get_installed_translations( 'plugins' );
  157. $locales = array_values( get_available_languages() );
  158. /**
  159. * Filters the locales requested for plugin translations.
  160. *
  161. * @since 3.7.0
  162. * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
  163. *
  164. * @param array $locales Plugin locales. Default is all available locales of the site.
  165. */
  166. $locales = apply_filters( 'plugins_update_check_locales', $locales );
  167. $locales = array_unique( $locales );
  168. // No locales, the respone will be empty, we can return now.
  169. if ( empty( $locales ) ) {
  170. return array();
  171. }
  172. // Scan local plugins which may or may not have a subscription.
  173. $plugins = WC_Helper::get_local_woo_plugins();
  174. $active_woo_plugins = array_intersect( array_keys( $plugins ), get_option( 'active_plugins', array() ) );
  175. /*
  176. * Use only plugins that are subscribed to the automatic translations updates.
  177. */
  178. $active_for_translations = array_filter(
  179. $active_woo_plugins,
  180. function( $plugin ) use ( $plugins ) {
  181. return apply_filters( 'woocommerce_translations_updates_for_' . $plugins[ $plugin ]['slug'], false );
  182. }
  183. );
  184. // Nothing to check for, exit.
  185. if ( empty( $active_for_translations ) ) {
  186. return array();
  187. }
  188. if ( wp_doing_cron() ) {
  189. $timeout = 30;
  190. } else {
  191. // Three seconds, plus one extra second for every 10 plugins.
  192. $timeout = 3 + (int) ( count( $active_for_translations ) / 10 );
  193. }
  194. $request_body = array(
  195. 'locales' => $locales,
  196. 'plugins' => array(),
  197. );
  198. foreach ( $active_for_translations as $active_plugin ) {
  199. $plugin = $plugins[ $active_plugin ];
  200. $request_body['plugins'][ $plugin['slug'] ] = array( 'version' => $plugin['Version'] );
  201. }
  202. $raw_response = wp_remote_post(
  203. 'https://translate.wordpress.com/api/translations-updates/woocommerce',
  204. array(
  205. 'body' => json_encode( $request_body ),
  206. 'headers' => array( 'Content-Type: application/json' ),
  207. 'timeout' => $timeout,
  208. )
  209. );
  210. // Something wrong happened on the translate server side.
  211. $response_code = wp_remote_retrieve_response_code( $raw_response );
  212. if ( 200 !== $response_code ) {
  213. return array();
  214. }
  215. $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
  216. // API error, api returned but something was wrong.
  217. if ( array_key_exists( 'success', $response ) && false === $response['success'] ) {
  218. return array();
  219. }
  220. $translations = array();
  221. foreach ( $response['data'] as $plugin_name => $language_packs ) {
  222. foreach ( $language_packs as $language_pack ) {
  223. // Maybe we have this language pack already installed so lets check revision date.
  224. if ( array_key_exists( $plugin_name, $installed_translations ) && array_key_exists( $language_pack['wp_locale'], $installed_translations[ $plugin_name ] ) ) {
  225. $installed_translation_revision_time = new DateTime( $installed_translations[ $plugin_name ][ $language_pack['wp_locale'] ]['PO-Revision-Date'] );
  226. $new_translation_revision_time = new DateTime( $language_pack['last_modified'] );
  227. // Skip if translation language pack is not newer than what is installed already.
  228. if ( $new_translation_revision_time <= $installed_translation_revision_time ) {
  229. continue;
  230. }
  231. }
  232. $translations[] = array(
  233. 'type' => 'plugin',
  234. 'slug' => $plugin_name,
  235. 'language' => $language_pack['wp_locale'],
  236. 'version' => $language_pack['version'],
  237. 'updated' => $language_pack['last_modified'],
  238. 'package' => $language_pack['package'],
  239. 'autoupdate' => true,
  240. );
  241. }
  242. }
  243. return $translations;
  244. }
  245. /**
  246. * Run an update check API call.
  247. *
  248. * The call is cached based on the payload (product ids, file ids). If
  249. * the payload changes, the cache is going to miss.
  250. *
  251. * @param array $payload Information about the plugin to update.
  252. * @return array Update data for each requested product.
  253. */
  254. private static function _update_check( $payload ) {
  255. ksort( $payload );
  256. $hash = md5( wp_json_encode( $payload ) );
  257. $cache_key = '_woocommerce_helper_updates';
  258. $data = get_transient( $cache_key );
  259. if ( false !== $data ) {
  260. if ( hash_equals( $hash, $data['hash'] ) ) {
  261. return $data['products'];
  262. }
  263. }
  264. $data = array(
  265. 'hash' => $hash,
  266. 'updated' => time(),
  267. 'products' => array(),
  268. 'errors' => array(),
  269. );
  270. $request = WC_Helper_API::post(
  271. 'update-check',
  272. array(
  273. 'body' => wp_json_encode( array( 'products' => $payload ) ),
  274. 'authenticated' => true,
  275. )
  276. );
  277. if ( wp_remote_retrieve_response_code( $request ) !== 200 ) {
  278. $data['errors'][] = 'http-error';
  279. } else {
  280. $data['products'] = json_decode( wp_remote_retrieve_body( $request ), true );
  281. }
  282. set_transient( $cache_key, $data, 12 * HOUR_IN_SECONDS );
  283. return $data['products'];
  284. }
  285. /**
  286. * Check for an active subscription.
  287. *
  288. * Checks a given product id against all subscriptions on
  289. * the current site. Returns true if at least one active
  290. * subscription is found.
  291. *
  292. * @param int $product_id The product id to look for.
  293. *
  294. * @return bool True if active subscription found.
  295. */
  296. private static function _has_active_subscription( $product_id ) {
  297. if ( ! isset( $auth ) ) {
  298. $auth = WC_Helper_Options::get( 'auth' );
  299. }
  300. if ( ! isset( $subscriptions ) ) {
  301. $subscriptions = WC_Helper::get_subscriptions();
  302. }
  303. if ( empty( $auth['site_id'] ) || empty( $subscriptions ) ) {
  304. return false;
  305. }
  306. // Check for an active subscription.
  307. foreach ( $subscriptions as $subscription ) {
  308. if ( $subscription['product_id'] != $product_id ) {
  309. continue;
  310. }
  311. if ( in_array( absint( $auth['site_id'] ), $subscription['connections'] ) ) {
  312. return true;
  313. }
  314. }
  315. return false;
  316. }
  317. /**
  318. * Get the number of products that have updates.
  319. *
  320. * @return int The number of products with updates.
  321. */
  322. public static function get_updates_count() {
  323. $cache_key = '_woocommerce_helper_updates_count';
  324. $count = get_transient( $cache_key );
  325. if ( false !== $count ) {
  326. return $count;
  327. }
  328. // Don't fetch any new data since this function in high-frequency.
  329. if ( ! get_transient( '_woocommerce_helper_subscriptions' ) ) {
  330. return 0;
  331. }
  332. if ( ! get_transient( '_woocommerce_helper_updates' ) ) {
  333. return 0;
  334. }
  335. $count = 0;
  336. $update_data = self::get_update_data();
  337. if ( empty( $update_data ) ) {
  338. set_transient( $cache_key, $count, 12 * HOUR_IN_SECONDS );
  339. return $count;
  340. }
  341. // Scan local plugins.
  342. foreach ( WC_Helper::get_local_woo_plugins() as $plugin ) {
  343. if ( empty( $update_data[ $plugin['_product_id'] ] ) ) {
  344. continue;
  345. }
  346. if ( version_compare( $plugin['Version'], $update_data[ $plugin['_product_id'] ]['version'], '<' ) ) {
  347. $count++;
  348. }
  349. }
  350. // Scan local themes.
  351. foreach ( WC_Helper::get_local_woo_themes() as $theme ) {
  352. if ( empty( $update_data[ $theme['_product_id'] ] ) ) {
  353. continue;
  354. }
  355. if ( version_compare( $theme['Version'], $update_data[ $theme['_product_id'] ]['version'], '<' ) ) {
  356. $count++;
  357. }
  358. }
  359. set_transient( $cache_key, $count, 12 * HOUR_IN_SECONDS );
  360. return $count;
  361. }
  362. /**
  363. * Return the updates count markup.
  364. *
  365. * @return string Updates count markup, empty string if no updates avairable.
  366. */
  367. public static function get_updates_count_html() {
  368. $count = self::get_updates_count();
  369. if ( ! $count ) {
  370. return '';
  371. }
  372. $count_html = sprintf( '<span class="update-plugins count-%d"><span class="update-count">%d</span></span>', $count, number_format_i18n( $count ) );
  373. return $count_html;
  374. }
  375. /**
  376. * Flushes cached update data.
  377. */
  378. public static function flush_updates_cache() {
  379. delete_transient( '_woocommerce_helper_updates' );
  380. delete_transient( '_woocommerce_helper_updates_count' );
  381. delete_site_transient( 'update_plugins' );
  382. delete_site_transient( 'update_themes' );
  383. }
  384. /**
  385. * Fires when a user successfully updated a theme or a plugin.
  386. */
  387. public static function upgrader_process_complete() {
  388. delete_transient( '_woocommerce_helper_updates_count' );
  389. }
  390. /**
  391. * Hooked into the upgrader_pre_download filter in order to better handle error messaging around expired
  392. * plugin updates. Initially we were using an empty string, but the error message that no_package
  393. * results in does not fit the cause.
  394. *
  395. * @since 4.1.0
  396. * @param bool $reply Holds the current filtered response.
  397. * @param string $package The path to the package file for the update.
  398. * @return false|WP_Error False to proceed with the update as normal, anything else to be returned instead of updating.
  399. */
  400. public static function block_expired_updates( $reply, $package ) {
  401. // Don't override a reply that was set already.
  402. if ( false !== $reply ) {
  403. return $reply;
  404. }
  405. // Only for packages with expired subscriptions.
  406. if ( 0 !== strpos( $package, 'woocommerce-com-expired-' ) ) {
  407. return false;
  408. }
  409. return new WP_Error(
  410. 'woocommerce_subscription_expired',
  411. sprintf(
  412. // translators: %s: URL of WooCommerce.com subscriptions tab.
  413. __( 'Please visit the <a href="%s" target="_blank">subscriptions page</a> and renew to continue receiving updates.', 'woocommerce' ),
  414. esc_url( admin_url( 'admin.php?page=wc-addons&section=helper' ) )
  415. )
  416. );
  417. }
  418. }
  419. WC_Helper_Updater::load();