Нема описа

class-wc-plugin-updates.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Class for displaying plugin warning notifications and determining 3rd party plugin compatibility.
  4. *
  5. * @package WooCommerce\Admin
  6. * @version 3.2.0
  7. */
  8. use Automattic\Jetpack\Constants;
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit;
  11. }
  12. /**
  13. * WC_Admin_Plugin_Updates Class.
  14. */
  15. class WC_Plugin_Updates {
  16. /**
  17. * This is the header used by extensions to show requirements.
  18. *
  19. * @var string
  20. */
  21. const VERSION_REQUIRED_HEADER = 'WC requires at least';
  22. /**
  23. * This is the header used by extensions to show testing.
  24. *
  25. * @var string
  26. */
  27. const VERSION_TESTED_HEADER = 'WC tested up to';
  28. /**
  29. * The version for the update to WooCommerce.
  30. *
  31. * @var string
  32. */
  33. protected $new_version = '';
  34. /**
  35. * Array of plugins lacking testing with the major version.
  36. *
  37. * @var array
  38. */
  39. protected $major_untested_plugins = array();
  40. /**
  41. * Common JS for initializing and managing thickbox-based modals.
  42. */
  43. protected function generic_modal_js() {
  44. ?>
  45. <script>
  46. ( function( $ ) {
  47. // Initialize thickbox.
  48. tb_init( '.wc-thickbox' );
  49. var old_tb_position = false;
  50. // Make the WC thickboxes look good when opened.
  51. $( '.wc-thickbox' ).on( 'click', function( evt ) {
  52. var $overlay = $( '#TB_overlay' );
  53. if ( ! $overlay.length ) {
  54. $( 'body' ).append( '<div id="TB_overlay"></div><div id="TB_window" class="wc_untested_extensions_modal_container"></div>' );
  55. } else {
  56. $( '#TB_window' ).removeClass( 'thickbox-loading' ).addClass( 'wc_untested_extensions_modal_container' );
  57. }
  58. // WP overrides the tb_position function. We need to use a different tb_position function than that one.
  59. // This is based on the original tb_position.
  60. if ( ! old_tb_position ) {
  61. old_tb_position = tb_position;
  62. }
  63. tb_position = function() {
  64. $( '#TB_window' ).css( { marginLeft: '-' + parseInt( ( TB_WIDTH / 2 ), 10 ) + 'px', width: TB_WIDTH + 'px' } );
  65. $( '#TB_window' ).css( { marginTop: '-' + parseInt( ( TB_HEIGHT / 2 ), 10 ) + 'px' } );
  66. };
  67. });
  68. // Reset tb_position to WP default when modal is closed.
  69. $( 'body' ).on( 'thickbox:removed', function() {
  70. if ( old_tb_position ) {
  71. tb_position = old_tb_position;
  72. }
  73. });
  74. })( jQuery );
  75. </script>
  76. <?php
  77. }
  78. /*
  79. |--------------------------------------------------------------------------
  80. | Message Helpers
  81. |--------------------------------------------------------------------------
  82. |
  83. | Methods for getting messages.
  84. */
  85. /**
  86. * Get the inline warning notice for major version updates.
  87. *
  88. * @return string
  89. */
  90. protected function get_extensions_inline_warning_major() {
  91. $upgrade_type = 'major';
  92. $plugins = $this->major_untested_plugins;
  93. $version_parts = explode( '.', $this->new_version );
  94. $new_version = $version_parts[0] . '.0';
  95. if ( empty( $plugins ) ) {
  96. return;
  97. }
  98. /* translators: %s: version number */
  99. $message = sprintf( __( "<strong>Heads up!</strong> The versions of the following plugins you're running haven't been tested with WooCommerce %s. Please update them or confirm compatibility before updating WooCommerce, or you may experience issues:", 'woocommerce' ), $new_version );
  100. ob_start();
  101. include __DIR__ . '/views/html-notice-untested-extensions-inline.php';
  102. return ob_get_clean();
  103. }
  104. /**
  105. * Get the warning notice for the modal window.
  106. *
  107. * @return string
  108. */
  109. protected function get_extensions_modal_warning() {
  110. $version_parts = explode( '.', $this->new_version );
  111. $new_version = $version_parts[0] . '.0';
  112. $plugins = $this->major_untested_plugins;
  113. ob_start();
  114. include __DIR__ . '/views/html-notice-untested-extensions-modal.php';
  115. return ob_get_clean();
  116. }
  117. /*
  118. |--------------------------------------------------------------------------
  119. | Data Helpers
  120. |--------------------------------------------------------------------------
  121. |
  122. | Methods for getting & manipulating data.
  123. */
  124. /**
  125. * Get installed plugins that have a tested version lower than the input version.
  126. *
  127. * In case of testing major version compatibility and if current WC version is >= major version part
  128. * of the $new_version, no plugins are returned, even if they don't explicitly declare compatibility
  129. * with the $new_version.
  130. *
  131. * @param string $new_version WooCommerce version to test against.
  132. * @param string $release 'major', 'minor', or 'none'.
  133. * @return array of plugin info arrays
  134. */
  135. public function get_untested_plugins( $new_version, $release ) {
  136. // Since 5.0 all versions are backwards compatible.
  137. if ( 'none' === $release ) {
  138. return array();
  139. }
  140. $extensions = array_merge( $this->get_plugins_with_header( self::VERSION_TESTED_HEADER ), $this->get_plugins_for_woocommerce() );
  141. $untested = array();
  142. $new_version_parts = explode( '.', $new_version );
  143. $version = $new_version_parts[0];
  144. if ( 'minor' === $release ) {
  145. $version .= '.' . $new_version_parts[1];
  146. }
  147. foreach ( $extensions as $file => $plugin ) {
  148. if ( ! empty( $plugin[ self::VERSION_TESTED_HEADER ] ) ) {
  149. $plugin_version_parts = explode( '.', $plugin[ self::VERSION_TESTED_HEADER ] );
  150. if ( ! is_numeric( $plugin_version_parts[0] )
  151. || ( 'minor' === $release && ! isset( $plugin_version_parts[1] ) )
  152. || ( 'minor' === $release && ! is_numeric( $plugin_version_parts[1] ) )
  153. ) {
  154. continue;
  155. }
  156. $plugin_version = $plugin_version_parts[0];
  157. if ( 'minor' === $release ) {
  158. $plugin_version .= '.' . $plugin_version_parts[1];
  159. }
  160. if ( version_compare( $plugin_version, $version, '<' ) ) {
  161. $untested[ $file ] = $plugin;
  162. }
  163. } else {
  164. $plugin[ self::VERSION_TESTED_HEADER ] = __( 'unknown', 'woocommerce' );
  165. $untested[ $file ] = $plugin;
  166. }
  167. }
  168. return $untested;
  169. }
  170. /**
  171. * Get plugins that have a valid value for a specific header.
  172. *
  173. * @param string $header Plugin header to search for.
  174. * @return array Array of plugins that contain the searched header.
  175. */
  176. protected function get_plugins_with_header( $header ) {
  177. $plugins = get_plugins();
  178. $matches = array();
  179. foreach ( $plugins as $file => $plugin ) {
  180. if ( ! empty( $plugin[ $header ] ) ) {
  181. $matches[ $file ] = $plugin;
  182. }
  183. }
  184. return apply_filters( 'woocommerce_get_plugins_with_header', $matches, $header, $plugins );
  185. }
  186. /**
  187. * Get plugins which "maybe" are for WooCommerce.
  188. *
  189. * @return array of plugin info arrays
  190. */
  191. protected function get_plugins_for_woocommerce() {
  192. $plugins = get_plugins();
  193. $matches = array();
  194. foreach ( $plugins as $file => $plugin ) {
  195. if ( 'WooCommerce' !== $plugin['Name'] && ( stristr( $plugin['Name'], 'woocommerce' ) || stristr( $plugin['Description'], 'woocommerce' ) ) ) {
  196. $matches[ $file ] = $plugin;
  197. }
  198. }
  199. return apply_filters( 'woocommerce_get_plugins_for_woocommerce', $matches, $plugins );
  200. }
  201. }