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