Нет описания

class-wc-plugins-screen-updates.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Manages WooCommerce plugin updating on the Plugins screen.
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 3.2.0
  7. */
  8. use Automattic\Jetpack\Constants;
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit;
  11. }
  12. if ( ! class_exists( 'WC_Plugin_Updates' ) ) {
  13. include_once dirname( __FILE__ ) . '/class-wc-plugin-updates.php';
  14. }
  15. /**
  16. * Class WC_Plugins_Screen_Updates
  17. */
  18. class WC_Plugins_Screen_Updates extends WC_Plugin_Updates {
  19. /**
  20. * The upgrade notice shown inline.
  21. *
  22. * @var string
  23. */
  24. protected $upgrade_notice = '';
  25. /**
  26. * Constructor.
  27. */
  28. public function __construct() {
  29. add_action( 'in_plugin_update_message-woocommerce/woocommerce.php', array( $this, 'in_plugin_update_message' ), 10, 2 );
  30. }
  31. /**
  32. * Show plugin changes on the plugins screen. Code adapted from W3 Total Cache.
  33. *
  34. * @param array $args Unused parameter.
  35. * @param stdClass $response Plugin update response.
  36. */
  37. public function in_plugin_update_message( $args, $response ) {
  38. $version_type = Constants::get_constant( 'WC_SSR_PLUGIN_UPDATE_RELEASE_VERSION_TYPE' );
  39. if ( ! is_string( $version_type ) ) {
  40. $version_type = 'none';
  41. }
  42. $this->new_version = $response->new_version;
  43. $this->upgrade_notice = $this->get_upgrade_notice( $response->new_version );
  44. $this->major_untested_plugins = $this->get_untested_plugins( $response->new_version, $version_type );
  45. $current_version_parts = explode( '.', Constants::get_constant( 'WC_VERSION' ) );
  46. $new_version_parts = explode( '.', $this->new_version );
  47. // If user has already moved to the minor version, we don't need to flag up anything.
  48. if ( version_compare( $current_version_parts[0] . '.' . $current_version_parts[1], $new_version_parts[0] . '.' . $new_version_parts[1], '=' ) ) {
  49. return;
  50. }
  51. if ( ! empty( $this->major_untested_plugins ) ) {
  52. $this->upgrade_notice .= $this->get_extensions_inline_warning_major();
  53. }
  54. if ( ! empty( $this->major_untested_plugins ) ) {
  55. $this->upgrade_notice .= $this->get_extensions_modal_warning();
  56. add_action( 'admin_print_footer_scripts', array( $this, 'plugin_screen_modal_js' ) );
  57. }
  58. echo apply_filters( 'woocommerce_in_plugin_update_message', $this->upgrade_notice ? '</p>' . wp_kses_post( $this->upgrade_notice ) . '<p class="dummy">' : '' ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
  59. }
  60. /**
  61. * Get the upgrade notice from WordPress.org.
  62. *
  63. * @param string $version WooCommerce new version.
  64. * @return string
  65. */
  66. protected function get_upgrade_notice( $version ) {
  67. $transient_name = 'wc_upgrade_notice_' . $version;
  68. $upgrade_notice = get_transient( $transient_name );
  69. if ( false === $upgrade_notice ) {
  70. $response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/woocommerce/trunk/readme.txt' );
  71. if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
  72. $upgrade_notice = $this->parse_update_notice( $response['body'], $version );
  73. set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS );
  74. }
  75. }
  76. return $upgrade_notice;
  77. }
  78. /**
  79. * Parse update notice from readme file.
  80. *
  81. * @param string $content WooCommerce readme file content.
  82. * @param string $new_version WooCommerce new version.
  83. * @return string
  84. */
  85. private function parse_update_notice( $content, $new_version ) {
  86. $version_parts = explode( '.', $new_version );
  87. $check_for_notices = array(
  88. $version_parts[0] . '.0', // Major.
  89. $version_parts[0] . '.0.0', // Major.
  90. $version_parts[0] . '.' . $version_parts[1], // Minor.
  91. $version_parts[0] . '.' . $version_parts[1] . '.' . $version_parts[2], // Patch.
  92. );
  93. $notice_regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( $new_version ) . '\s*=|$)~Uis';
  94. $upgrade_notice = '';
  95. foreach ( $check_for_notices as $check_version ) {
  96. if ( version_compare( Constants::get_constant( 'WC_VERSION' ), $check_version, '>' ) ) {
  97. continue;
  98. }
  99. $matches = null;
  100. if ( preg_match( $notice_regexp, $content, $matches ) ) {
  101. $notices = (array) preg_split( '~[\r\n]+~', trim( $matches[2] ) );
  102. if ( version_compare( trim( $matches[1] ), $check_version, '=' ) ) {
  103. $upgrade_notice .= '<p class="wc_plugin_upgrade_notice">';
  104. foreach ( $notices as $index => $line ) {
  105. $upgrade_notice .= preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line );
  106. }
  107. $upgrade_notice .= '</p>';
  108. }
  109. break;
  110. }
  111. }
  112. return wp_kses_post( $upgrade_notice );
  113. }
  114. /**
  115. * JS for the modal window on the plugins screen.
  116. */
  117. public function plugin_screen_modal_js() {
  118. ?>
  119. <script>
  120. ( function( $ ) {
  121. var $update_box = $( '#woocommerce-update' );
  122. var $update_link = $update_box.find('a.update-link').first();
  123. var update_url = $update_link.attr( 'href' );
  124. // Set up thickbox.
  125. $update_link.removeClass( 'update-link' );
  126. $update_link.addClass( 'wc-thickbox' );
  127. $update_link.attr( 'href', '#TB_inline?height=600&width=550&inlineId=wc_untested_extensions_modal' );
  128. // Trigger the update if the user accepts the modal's warning.
  129. $( '#wc_untested_extensions_modal .accept' ).on( 'click', function( evt ) {
  130. evt.preventDefault();
  131. tb_remove();
  132. $update_link.removeClass( 'wc-thickbox open-plugin-details-modal' );
  133. $update_link.addClass( 'update-link' );
  134. $update_link.attr( 'href', update_url );
  135. $update_link.trigger( 'click' );
  136. });
  137. $( '#wc_untested_extensions_modal .cancel' ).on( 'click', function( evt ) {
  138. evt.preventDefault();
  139. tb_remove();
  140. });
  141. })( jQuery );
  142. </script>
  143. <?php
  144. $this->generic_modal_js();
  145. }
  146. }
  147. new WC_Plugins_Screen_Updates();