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