Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove serialization of meta data #548

Merged
merged 1 commit into from Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion includes/admin/class-wcmp-api.php
@@ -1,6 +1,7 @@
<?php

use MyParcelNL\Sdk\src\Helper\MyParcelCollection;
use WPO\WC\MyParcel\Compatibility\Order as WCX_Order;
use WPO\WC\MyParcel\Compatibility\WC_Core;
use WPO\WC\MyParcel\Compatibility\WCMP_ChannelEngine_Compatibility as ChannelEngine;

Expand Down Expand Up @@ -186,7 +187,7 @@ private function updateOrderBarcode(array $orderIds, MyParcelCollection $collect
{
foreach ($orderIds as $orderId) {
$order = WC_Core::get_order($orderId);
$lastShipmentIds = unserialize($order->get_meta('_myparcel_last_shipment_ids'));
$lastShipmentIds = WCX_Order::get_meta($order, WCMYPA_Admin::META_LAST_SHIPMENT_IDS);

if (is_bool($lastShipmentIds)) {
continue;
Expand Down
59 changes: 52 additions & 7 deletions includes/compatibility/abstract-wc-data-compatibility.php
Expand Up @@ -106,18 +106,29 @@ public static function set_props($object, $props, $compat_props = [])
* @param string $context if 'view' then the value will be filtered
*
* @return mixed
* @throws \JsonException
* @since 4.6.0-dev
*/
public static function get_meta($object, $key = '', $single = true, $context = 'edit')
{
if (WC_Core::is_wc_version_gte_3_0()) {
$value = $object->get_meta($key, $single, $context);
} else {
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id;
$object_id = is_callable([$object, 'get_id'])
? $object->get_id()
: $object->id;
$value = get_post_meta($object_id, $key, $single);
}

return maybe_unserialize($value);
$value = self::removeSerialization($object, $key, $value);
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved

if (is_string($value)) {
$decoded = json_decode($value, true);
// json_decode returns null if there was a syntax error, meaning input was not valid JSON.
$value = $decoded ?? $value;
}

return $value;
}

/**
Expand All @@ -132,10 +143,10 @@ public static function has_meta(WC_Data $object, string $key): bool
{
if (WC_Core::is_wc_version_gte_3_0()) {
return $object->meta_exists($key);
} else {
$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id;
return count(get_post_meta($object_id, $key)) > 0;
}

$object_id = is_callable([$object, 'get_id']) ? $object->get_id() : $object->id;
return count(get_post_meta($object_id, $key)) > 0;
}

/**
Expand Down Expand Up @@ -168,11 +179,14 @@ public static function add_meta_data($object, $key, $value, $unique = false)
* @param string|array $value the meta value, will be encoded if it's an array
* @param int|string $meta_id Optional. The specific meta ID to update
*
* @throws \JsonException
* @since 4.6.0-dev
*/
public static function update_meta_data($object, $key, $value, $meta_id = '')
public static function update_meta_data($object, $key, $value, $meta_id = ''): void
{
$value = maybe_serialize($value);
if (is_array($value)) {
$value = json_encode($value, JSON_THROW_ON_ERROR);
}

if (WC_Core::is_wc_version_gte_3_0()) {
$object->update_meta_data($key, $value, $meta_id);
Expand Down Expand Up @@ -204,4 +218,35 @@ public static function delete_meta_data($object, $key)
delete_post_meta($object_id, $key);
}
}

/**
* Fix data stored as objects or serialized strings. Converts everything to array and updates it in the meta data.
*
* @param \WC_Data $object
* @param string $key
* @param mixed $value
*
* @return mixed
* @throws \JsonException
*/
private static function removeSerialization(WC_Data $object, string $key, $value)
{
if (is_serialized($value)) {
$value = @unserialize(trim($value));

self::update_meta_data($object, $key, $value);
EdieLemoine marked this conversation as resolved.
Show resolved Hide resolved
}

if (is_object($value)) {
if (is_callable([$value, 'toArray'])) {
$value = $value->toArray();
} else {
$value = (array) $value;
}

self::update_meta_data($object, $key, $value);
}

return $value;
}
}