PluginProbe
MultiSafepay plugin for WooCommerce / trunk
MultiSafepay plugin for WooCommerce vtrunk
6.11.1 6.12.0 6.13.0 6.2.0 6.2.1 6.3.0 6.3.1 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.6.0 6.6.1 6.6.2 6.7.0 6.7.1 6.7.2 6.7.3 6.8.0 6.8.1 6.8.2 6.8.3 6.9.0 All 84 releases
multisafepay / src / Utils / Hpos.php

Hpos.php in MultiSafepay plugin for WooCommerce trunk, at src/Utils/Hpos.php

94 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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