| 1 |
<?php |
| 2 |
/** |
| 3 |
* Derived from SkyVerge WooCommerce Plugin Framework https://github.com/skyverge/wc-plugin-framework/ |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WPO\WC\PostNL\Compatibility; |
| 7 |
|
| 8 |
use WC_Data; |
| 9 |
|
| 10 |
defined('ABSPATH') or exit; |
| 11 |
|
| 12 |
if (class_exists('\\WPO\\WC\\PostNL\\Compatibility\\Data')) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* WooCommerce data compatibility class. |
| 18 |
* |
| 19 |
* @since 4.6.0-dev |
| 20 |
*/ |
| 21 |
abstract class Data |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Creates aliases for add_meta_data, update_meta_data and delete_meta_data without the _data box number |
| 25 |
* |
| 26 |
* @param string $name static function name |
| 27 |
* @param array $arguments function arguments |
| 28 |
*/ |
| 29 |
public static function __callStatic($name, $arguments) |
| 30 |
{ |
| 31 |
if (substr($name, -strlen('_meta')) == '_meta' && method_exists(__CLASS__, $name . '_data')) { |
| 32 |
call_user_func_array([__CLASS__, $name . '_data'], $arguments); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Gets an object property. |
| 38 |
* |
| 39 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 40 |
* @param string $prop the property name |
| 41 |
* @param string $context if 'view' then the value will be filtered |
| 42 |
* @param array $compat_props Compatibility properties. |
| 43 |
* |
| 44 |
* @return mixed |
| 45 |
* @since 4.6.0-dev |
| 46 |
*/ |
| 47 |
public static function get_prop($object, $prop, $context = 'edit', $compat_props = []) |
| 48 |
{ |
| 49 |
$value = ''; |
| 50 |
|
| 51 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 52 |
if (is_callable([$object, "get_{$prop}"])) { |
| 53 |
$value = $object->{"get_{$prop}"}($context); |
| 54 |
} |
| 55 |
} else { |
| 56 |
// backport the property name |
| 57 |
if (isset($compat_props[$prop])) { |
| 58 |
$prop = $compat_props[$prop]; |
| 59 |
} |
| 60 |
|
| 61 |
// if this is the 'view' context and there is an accessor method, use it |
| 62 |
if (is_callable([$object, "get_{$prop}"]) && 'view' === $context) { |
| 63 |
$value = $object->{"get_{$prop}"}(); |
| 64 |
} else { |
| 65 |
$value = $object->$prop; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return $value; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Sets an object's properties. |
| 74 |
* Note that this does not save any data to the database. |
| 75 |
* |
| 76 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 77 |
* @param array $props the new properties as $key => $value |
| 78 |
* @param array $compat_props Compatibility properties. |
| 79 |
* |
| 80 |
* @return WC_Data |
| 81 |
* @since 4.6.0-dev |
| 82 |
*/ |
| 83 |
public static function set_props($object, $props, $compat_props = []) |
| 84 |
{ |
| 85 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 86 |
$object->set_props($props); |
| 87 |
} else { |
| 88 |
foreach ($props as $prop => $value) { |
| 89 |
if (isset($compat_props[$prop])) { |
| 90 |
$prop = $compat_props[$prop]; |
| 91 |
} |
| 92 |
|
| 93 |
$object->$prop = $value; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $object; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Gets an object's stored meta value. |
| 102 |
* |
| 103 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 104 |
* @param string $key the meta key |
| 105 |
* @param bool $single whether to get the meta as a single item. Defaults to `true` |
| 106 |
* @param string $context if 'view' then the value will be filtered |
| 107 |
* |
| 108 |
* @return mixed |
| 109 |
* @since 4.6.0-dev |
| 110 |
*/ |
| 111 |
public static function get_meta($object, $key = '', $single = true, $context = 'edit') |
| 112 |
{ |
| 113 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 114 |
$value = $object->get_meta($key, $single, $context); |
| 115 |
} else { |
| 116 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 117 |
$value = get_post_meta($object_id, $key, $single); |
| 118 |
} |
| 119 |
|
| 120 |
return maybe_unserialize($value); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Check if an object has given meta key. |
| 125 |
* |
| 126 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 127 |
* @param string $key the meta key |
| 128 |
* |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
public static function has_meta(WC_Data $object, string $key): bool |
| 132 |
{ |
| 133 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 134 |
return $object->meta_exists($key); |
| 135 |
} else { |
| 136 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 137 |
return count(get_post_meta($object_id, $key)) > 0; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Stores an object meta value. |
| 143 |
* |
| 144 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 145 |
* @param string $key the meta key |
| 146 |
* @param string $value the meta value |
| 147 |
* @param bool $unique Optional. Whether the meta should be unique. |
| 148 |
* |
| 149 |
* @since 4.6.0-dev |
| 150 |
*/ |
| 151 |
public static function add_meta_data($object, $key, $value, $unique = false) |
| 152 |
{ |
| 153 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 154 |
$object->add_meta_data($key, $value, $unique); |
| 155 |
|
| 156 |
$object->save_meta_data(); |
| 157 |
} else { |
| 158 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 159 |
add_post_meta($object_id, $key, maybe_serialize($value), $unique); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Updates an object's stored meta value. |
| 165 |
* |
| 166 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 167 |
* @param string $key the meta key |
| 168 |
* @param string|array $value the meta value, will be encoded if it's an array |
| 169 |
* @param int|string $meta_id Optional. The specific meta ID to update |
| 170 |
* |
| 171 |
* @since 4.6.0-dev |
| 172 |
*/ |
| 173 |
public static function update_meta_data($object, $key, $value, $meta_id = '') |
| 174 |
{ |
| 175 |
$value = maybe_serialize($value); |
| 176 |
|
| 177 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 178 |
$object->update_meta_data($key, $value, $meta_id); |
| 179 |
|
| 180 |
$object->save_meta_data(); |
| 181 |
} else { |
| 182 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 183 |
|
| 184 |
update_post_meta($object_id, $key, $value); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Deletes an object's stored meta value. |
| 190 |
* |
| 191 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 192 |
* @param string $key the meta key |
| 193 |
* |
| 194 |
* @since 4.6.0-dev |
| 195 |
*/ |
| 196 |
public static function delete_meta_data($object, $key) |
| 197 |
{ |
| 198 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 199 |
$object->delete_meta_data($key); |
| 200 |
|
| 201 |
$object->save_meta_data(); |
| 202 |
} else { |
| 203 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 204 |
delete_post_meta($object_id, $key); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|