-
Notifications
You must be signed in to change notification settings - Fork 31
/
compatibility.php
526 lines (424 loc) · 13.7 KB
/
compatibility.php
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
/**
* WooCommerce Plugin Compatibility
*
* This source file is subject to the GNU General Public License v3.0
* that is bundled with this package in the file license.txt.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/gpl-3.0.html
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade the plugin to newer
* versions in the future. If you wish to customize the plugin for your
* needs please refer to http://www.skyverge.com
*
* @author SkyVerge
* @copyright Copyright (c) 2013-2014, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
if ( !defined( 'ABSPATH' ) )
exit; // Exit if accessed directly
if ( !class_exists( 'WC_GFPA_Compatibility' ) ) :
/**
* WooCommerce Compatibility Utility Class
*
* The unfortunate purpose of this class is to provide a single point of
* compatibility functions for dealing with supporting multiple versions
* of WooCommerce.
*
* The recommended procedure is to rename this file/class, replacing "my plugin"
* with the particular plugin name, so as to avoid clashes between plugins.
* Over time we expect to remove methods from this class, using the current
* ones directly, as support for older versions of WooCommerce is dropped.
*
* Current Compatibility: 2.1.x - 2.2
*
* @version 2.0
*/
class WC_GFPA_Compatibility {
/**
* Get the WC Order instance for a given order ID or order post
*
* Introduced in WC 2.2 as part of the Order Factory so the 2.1 version is
* not an exact replacement.
*
* If no param is passed, it will use the global post. Otherwise pass an
* the order post ID or post object.
*
* @since 2.0.0
* @param bool|int|string|\WP_Post $the_order
* @return bool|\WC_Order
*/
public static function wc_get_order( $the_order = false ) {
if ( self::is_wc_version_gte_2_2() ) {
return wc_get_order( $the_order );
} else {
global $post;
if ( false === $the_order ) {
$order_id = $post->ID;
} elseif ( $the_order instanceof WP_Post ) {
$order_id = $the_order->ID;
} elseif ( is_numeric( $the_order ) ) {
$order_id = $the_order;
}
return new WC_Order( $order_id );
}
}
/**
* Get all order statuses
*
* Introduced in WC 2.2
*
* @since 2.0.0
* @return array
*/
public static function wc_get_order_statuses() {
if ( self::is_wc_version_gte_2_2() ) {
return wc_get_order_statuses();
} else {
// get available order statuses
$order_status_terms = get_terms( 'shop_order_status', array('hide_empty' => false) );
if ( is_wp_error( $order_status_terms ) ) {
$order_status_terms = array();
}
$order_statuses = array();
foreach ( $order_status_terms as $term ) {
$order_statuses[$term->slug] = $term->name;
}
return $order_statuses;
}
}
/**
* Get the order status
*
* Order statuses changed from a taxonomy in 2.1 to using `post_status`
* in 2.2
*
* @since 2.0.0
* @param WC_Order $order
* @return mixed|string
*/
public static function get_order_status( WC_Order $order ) {
return self::is_wc_version_gte_2_2() ? $order->get_status() : $order->status;
}
/**
* Check if the order has the given status
*
* @param WC_Order $order
* @param string|array $status single status or array of statuses to check
* @since 2.0.0
* @return bool
*/
public static function order_has_status( $order, $status ) {
if ( self::is_wc_version_gte_2_2() ) {
return $order->has_status( $status );
} else {
if ( is_array( $status ) ) {
return in_array( $order->status, $status );
} else {
return $status === $order->status;
}
}
}
/**
* Transparently backport the `post_status` WP Query arg used by WC 2.2
* for order statuses to the `shop_order_status` taxonomy query arg used by
* WC 2.1
*
* @since 2.0.0
* @param array $args WP_Query args
* @return array
*/
public static function backport_order_status_query_args( $args ) {
if ( !self::is_wc_version_gte_2_2() ) {
// convert post status arg to taxonomy query compatible with WC 2.1
if ( !empty( $args['post_status'] ) ) {
$order_statuses = array();
foreach ( (array) $args['post_status'] as $order_status ) {
$order_statuses[] = str_replace( 'wc-', '', $order_status );
}
$args['post_status'] = 'publish';
$tax_query = array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => $order_statuses,
'operator' => 'IN',
)
);
$args['tax_query'] = array_merge( ( isset( $args['tax_query'] ) ? $args['tax_query'] : array() ), $tax_query );
}
}
return $args;
}
/**
* Get the user ID for an order
*
* @since 2.0.0
* @param \WC_Order $order
* @return int
*/
public static function get_order_user_id( WC_Order $order ) {
if ( self::is_wc_version_gte_2_2() ) {
return $order->get_user_id();
} else {
return $order->customer_user ? $order->customer_user : 0;
}
}
/**
* Get the user for an order
*
* @since 2.0.0
* @param \WC_Order $order
* @return bool|WP_User
*/
public static function get_order_user( WC_Order $order ) {
if ( self::is_wc_version_gte_2_2() ) {
return $order->get_user();
} else {
return self::get_order_user_id( $order ) ? get_user_by( 'id', self::get_order_user_id( $order ) ) : false;
}
}
/**
* Get the WC Product instance for a given product ID or post
*
* get_product() is soft-deprecated in WC 2.2
*
* @since 2.0.0
* @param bool|int|string|\WP_Post $the_product
* @param array $args
* @return WC_Product
*/
public static function wc_get_product( $the_product = false, $args = array() ) {
if ( self::is_wc_version_gte_2_2() ) {
return wc_get_product( $the_product, $args );
} else {
return get_product( $the_product, $args );
}
}
/**
* Return an array of formatted item meta in format:
*
* array(
* $meta_key => array(
* 'label' => $label,
* 'value' => $value
* )
* )
*
* e.g.
*
* array(
* 'pa_size' => array(
* 'label' => 'Size',
* 'value' => 'Medium',
* )
* )
*
* Backports the get_formatted() method to WC 2.1
*
* @since 2.2.0
* @see WC_Order_Item_Meta::get_formatted()
* @param \WC_Order_Item_Meta $item_meta order item meta class instance
* @param string $hide_prefix exclude meta when key is prefixed with this, defaults to `_`
* @return array
*/
public static function get_formatted_item_meta( WC_Order_Item_Meta $item_meta, $hide_prefix = '_' ) {
if ( self::is_wc_version_gte_2_2() ) {
return $item_meta->get_formatted( $hide_prefix );
} else {
if ( empty( $item_meta->meta ) ) {
return array();
}
$formatted_meta = array();
foreach ( (array) $item_meta->meta as $meta_key => $meta_values ) {
if ( empty( $meta_values ) || !is_array( $meta_values ) || (!empty( $hide_prefix ) && substr( $meta_key, 0, 1 ) == $hide_prefix ) ) {
continue;
}
foreach ( $meta_values as $meta_value ) {
// Skip serialised meta
if ( is_serialized( $meta_value ) ) {
continue;
}
$attribute_key = urldecode( str_replace( 'attribute_', '', $meta_key ) );
// If this is a term slug, get the term's nice name
if ( taxonomy_exists( $attribute_key ) ) {
$term = get_term_by( 'slug', $meta_value, $attribute_key );
if ( !is_wp_error( $term ) && is_object( $term ) && $term->name ) {
$meta_value = $term->name;
}
// If we have a product, and its not a term, try to find its non-sanitized name
} elseif ( $item_meta->product ) {
$product_attributes = $item_meta->product->get_attributes();
if ( isset( $product_attributes[$attribute_key] ) ) {
$meta_key = wc_attribute_label( $product_attributes[$attribute_key]['name'] );
}
}
$formatted_meta[$meta_key] = array(
'label' => wc_attribute_label( $attribute_key ),
'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ),
);
}
}
return $formatted_meta;
}
}
/**
* Get the full path to the log file for a given $handle
*
* @since 2.0.0
* @param string $handle log handle
* @return string
*/
public static function wc_get_log_file_path( $handle ) {
if ( self::is_wc_version_gte_2_2() ) {
return wc_get_log_file_path( $handle );
} else {
return sprintf( '%s/plugins/woocommerce/logs/%s-%s.txt', WP_CONTENT_DIR, $handle, sanitize_file_name( wp_hash( $handle ) ) );
}
}
/**
* Gets a product meta field value, regardless of product type
*
* @since 2.0
* @param WC_Product $product the product
* @param string $field_name the field name
* @return mixed meta value
*/
public static function get_product_meta( $product, $field_name ) {
// Should be able to drop this when we drop support for WC 2.1
// even in WC >= 2.0 product variations still use the product_custom_fields array apparently
if ( $product->variation_id && isset( $product->product_custom_fields['_' . $field_name][0] ) && '' !== $product->product_custom_fields['_' . $field_name][0] ) {
return $product->product_custom_fields['_' . $field_name][0];
}
// use magic __get
return $product->$field_name;
}
public static function wc_error_count() {
if ( self::is_wc_version_gt( '2.0' ) ) {
return wc_notice_count( 'error' );
} else {
global $woocommerce;
return $woocommerce->error_count();
}
}
/**
* Helper method to get the version of the currently installed WooCommerce
*
* @since 2.0.0
* @return string woocommerce version number or null
*/
private static function get_wc_version() {
return defined( 'WC_VERSION' ) && WC_VERSION ? WC_VERSION : WC()->version;
}
/**
* Returns true if the installed version of WooCommerce is 2.2 or greater
*
* @since 2.2.0
* @return boolean true if the installed version of WooCommerce is 2.2 or greater
*/
public static function is_wc_version_gte_2_2() {
return self::get_wc_version() && version_compare( self::get_wc_version(), '2.2', '>=' );
}
/**
* Returns true if the installed version of WooCommerce is 2.5 or greater
*
* @since 2.4.0
* @return boolean true if the installed version of WooCommerce is 2.5 or greater
*/
public static function is_wc_version_gte_2_4() {
return self::get_wc_version() && version_compare( self::get_wc_version(), '2.4', '>=' );
}
/**
* Returns true if the installed version of WooCommerce is 2.5 or greater
*
* @since 2.5.0
* @return boolean true if the installed version of WooCommerce is 2.5 or greater
*/
public static function is_wc_version_gte_2_5() {
return self::get_wc_version() && version_compare( self::get_wc_version(), '2.5', '>=' );
}
/**
* Returns true if the installed version of WooCommerce is 2.7 or greater
*
* @since 3.0.0
* @return boolean true if the installed version of WooCommerce is 2.7 or greater
*/
public static function is_wc_version_gte_2_7() {
return self::get_wc_version() && version_compare( self::get_wc_version(), '2.7', '>=' );
}
/**
* Returns true if the installed version of WooCommerce is less than 2.2
*
* @since 2.2.0
* @return boolean true if the installed version of WooCommerce is less than 2.2
*/
public static function is_wc_version_lt_2_2() {
return self::get_wc_version() && version_compare( self::get_wc_version(), '2.2', '<' );
}
/**
* Returns true if the installed version of WooCommerce is 2.7 or greater
*
* @since 3.0.0
* @return boolean true if the installed version of WooCommerce is 2.7 or greater
*/
public static function is_wc_version_gte_3_2() {
return self::get_wc_version() && version_compare( self::get_wc_version(), '3.2', '>=' );
}
/**
* Returns true if the installed version of WooCommerce is greater than $version
*
* @since 2.0.0
* @param string $version the version to compare
* @return boolean true if the installed version of WooCommerce is > $version
*/
public static function is_wc_version_gt( $version ) {
return self::get_wc_version() && version_compare( self::get_wc_version(), $version, '>' );
}
}
endif; // Class exists check
if ( !function_exists( 'wc_is_21x' ) ) {
function wc_is_21x() {
return function_exists( 'wc_add_notice' );
}
}
if ( !function_exists( 'is_wc_version_gte_2_2' ) ) {
/**
* Returns true if the installed version of WooCommerce is 2.2 or greater
*
* @since 1.0
* @return boolean true if the installed version of WooCommerce is 2.1 or greater
*/
function is_wc_version_gte_2_2() {
global $woocommerce;
return version_compare( $woocommerce->version, '2.2.0', 'ge' );
}
}
if ( !function_exists( 'is_wc_version_gte_2_3' ) ) {
/**
* Returns true if the installed version of WooCommerce is 2.3 or greater
*
* @since 1.0
* @return boolean true if the installed version of WooCommerce is 2.1 or greater
*/
function is_wc_version_gte_2_3() {
global $woocommerce;
return version_compare( $woocommerce->version, '2.3.0', 'ge' );
}
}
if ( !function_exists( 'is_wc_version_gte_2_3' ) ) {
/**
* Returns true if the installed version of WooCommerce is 2.3 or greater
*
* @since 1.0
* @return boolean true if the installed version of WooCommerce is 2.1 or greater
*/
function is_wc_version_gte_2_3() {
global $woocommerce;
return version_compare( $woocommerce->version, '2.3.0', 'ge' );
}
}