| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Utils; |
| 4 |
|
| 5 |
use WC_Order; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class HPOS Checker |
| 9 |
* |
| 10 |
* @package MultiSafepay\WooCommerce\Utils |
| 11 |
*/ |
| 12 |
class Hpos { |
| 13 |
|
| 14 |
/** |
| 15 |
* WooCommerce version where HPOS was released |
| 16 |
*/ |
| 17 |
public const HPOS_RELEASE_VERSION = '7.1.0'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Get the WooCommerce version |
| 21 |
* |
| 22 |
* @return string|null |
| 23 |
*/ |
| 24 |
public static function get_woocommerce_version(): ?string { |
| 25 |
if ( defined( 'WC_VERSION' ) && ! empty( WC_VERSION ) ) { |
| 26 |
return WC_VERSION; |
| 27 |
} |
| 28 |
if ( function_exists( 'WC' ) && ! empty( WC()->version ) ) { |
| 29 |
return WC()->version; |
| 30 |
} |
| 31 |
return null; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Check if the WooCommerce version is compatible with HPOS |
| 36 |
* |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
public static function is_active(): bool { |
| 40 |
return ( 'yes' === get_option( 'woocommerce_custom_orders_table_enabled', 'no' ) ) && |
| 41 |
version_compare( self::get_woocommerce_version(), self::HPOS_RELEASE_VERSION, '>=' ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param WC_Order $order |
| 46 |
* @param string $key |
| 47 |
* @param string|array|bool $value |
| 48 |
* |
| 49 |
* @return bool|int |
| 50 |
*/ |
| 51 |
public static function update_meta( WC_Order $order, string $key, $value ) { |
| 52 |
if ( self::is_active() ) { |
| 53 |
$order->update_meta_data( $key, $value ); |
| 54 |
return $order->save(); |
| 55 |
} |
| 56 |
|
| 57 |
return update_post_meta( $order->get_id(), $key, $value ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Queue order meta-data deletion. |
| 62 |
* |
| 63 |
* @param WC_Order $order |
| 64 |
* @param string $key |
| 65 |
* |
| 66 |
* @return int|void |
| 67 |
*/ |
| 68 |
public static function delete_meta( WC_Order $order, string $key ) { |
| 69 |
if ( self::is_active() ) { |
| 70 |
$order->delete_meta_data( $key ); |
| 71 |
return $order->save(); |
| 72 |
} |
| 73 |
|
| 74 |
delete_post_meta( $order->get_id(), $key ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get the order metadata |
| 79 |
* |
| 80 |
* @param WC_Order $order |
| 81 |
* @param string $key |
| 82 |
* @param bool $single |
| 83 |
* |
| 84 |
* @return mixed |
| 85 |
*/ |
| 86 |
public static function get_meta( WC_Order $order, string $key, bool $single = true ) { |
| 87 |
if ( self::is_active() ) { |
| 88 |
return $order->get_meta( $key ); |
| 89 |
} |
| 90 |
|
| 91 |
return get_post_meta( $order->get_id(), $key, $single ); |
| 92 |
} |
| 93 |
} |
| 94 |
|