| 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 |
* @throws \JsonException |
| 110 |
* @since 4.6.0-dev |
| 111 |
*/ |
| 112 |
public static function get_meta($object, $key = '', $single = true, $context = 'edit') |
| 113 |
{ |
| 114 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 115 |
$value = $object->get_meta($key, $single, $context); |
| 116 |
} else { |
| 117 |
$object_id = is_callable([$object, 'get_id']) |
| 118 |
? $object->get_id() |
| 119 |
: $object->id; |
| 120 |
$value = get_post_meta($object_id, $key, $single); |
| 121 |
} |
| 122 |
|
| 123 |
$value = self::removeSerialization($object, $key, $value); |
| 124 |
|
| 125 |
if (is_string($value)) { |
| 126 |
$decoded = json_decode($value, true); |
| 127 |
// json_decode returns null if there was a syntax error, meaning input was not valid JSON. |
| 128 |
$value = $decoded ?? $value; |
| 129 |
} |
| 130 |
|
| 131 |
return $value; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Check if an object has given meta key. |
| 136 |
* |
| 137 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 138 |
* @param string $key the meta key |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
public static function has_meta(WC_Data $object, string $key): bool |
| 143 |
{ |
| 144 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 145 |
return $object->meta_exists($key); |
| 146 |
} |
| 147 |
|
| 148 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 149 |
return count(get_post_meta($object_id, $key)) > 0; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Stores an object meta value. |
| 154 |
* |
| 155 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 156 |
* @param string $key the meta key |
| 157 |
* @param string $value the meta value |
| 158 |
* @param bool $unique Optional. Whether the meta should be unique. |
| 159 |
* |
| 160 |
* @since 4.6.0-dev |
| 161 |
*/ |
| 162 |
public static function add_meta_data($object, $key, $value, $unique = false) |
| 163 |
{ |
| 164 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 165 |
$object->add_meta_data($key, $value, $unique); |
| 166 |
|
| 167 |
$object->save_meta_data(); |
| 168 |
} else { |
| 169 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 170 |
add_post_meta($object_id, $key, maybe_serialize($value), $unique); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Updates an object's stored meta value. |
| 176 |
* |
| 177 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 178 |
* @param string $key the meta key |
| 179 |
* @param string|array $value the meta value, will be encoded if it's an array |
| 180 |
* @param int|string $meta_id Optional. The specific meta ID to update |
| 181 |
* |
| 182 |
* @since 4.6.0-dev |
| 183 |
*/ |
| 184 |
public static function update_meta_data($object, $key, $value, $meta_id = ''): void |
| 185 |
{ |
| 186 |
if (is_array($value)) { |
| 187 |
$value = json_encode($value); |
| 188 |
} |
| 189 |
|
| 190 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 191 |
$object->update_meta_data($key, $value, $meta_id); |
| 192 |
|
| 193 |
$object->save_meta_data(); |
| 194 |
} else { |
| 195 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 196 |
|
| 197 |
update_post_meta($object_id, $key, $value); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Deletes an object's stored meta value. |
| 203 |
* |
| 204 |
* @param WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 205 |
* @param string $key the meta key |
| 206 |
* |
| 207 |
* @since 4.6.0-dev |
| 208 |
*/ |
| 209 |
public static function delete_meta_data($object, $key) |
| 210 |
{ |
| 211 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 212 |
$object->delete_meta_data($key); |
| 213 |
|
| 214 |
$object->save_meta_data(); |
| 215 |
} else { |
| 216 |
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id; |
| 217 |
delete_post_meta($object_id, $key); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Fix data stored as objects or serialized strings. Converts everything to array and updates it in the meta data. |
| 223 |
* |
| 224 |
* @param \WC_Data $object |
| 225 |
* @param string $key |
| 226 |
* @param mixed $value |
| 227 |
* |
| 228 |
* @return mixed |
| 229 |
* @throws \JsonException |
| 230 |
*/ |
| 231 |
private static function removeSerialization(WC_Data $object, string $key, $value) |
| 232 |
{ |
| 233 |
if (is_serialized($value)) { |
| 234 |
$value = @unserialize(trim($value)); |
| 235 |
|
| 236 |
self::update_meta_data($object, $key, $value); |
| 237 |
} |
| 238 |
|
| 239 |
if (is_object($value)) { |
| 240 |
if (is_callable([$value, 'toArray'])) { |
| 241 |
$value = $value->toArray(); |
| 242 |
} else { |
| 243 |
$value = (array) $value; |
| 244 |
} |
| 245 |
|
| 246 |
self::update_meta_data($object, $key, $value); |
| 247 |
} |
| 248 |
|
| 249 |
return $value; |
| 250 |
} |
| 251 |
} |
| 252 |
|