Skip serialization for customer meta and be more defensive around loading it. (#47514)

* skip data serialization for customer meta

* keep casting as is

* skip old comment

* Add changefile(s) from automation for the following project(s): woocommerce

* actually leave the comment, it still make sense

* reduce code needed

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Seghir Nadir 2024-05-16 15:24:36 +02:00 committed by GitHub
parent a7bbdb99a2
commit a13013e9a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 23 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix warning when loading guest sessions from previous sessions.

View File

@ -94,29 +94,22 @@ class WC_Customer_Data_Store_Session extends WC_Data_Store_WP implements WC_Cust
* @param array $allowed_keys The allowed meta data keys.
* @param WC_Customer $customer The customer object.
*/
$allowed_keys = apply_filters( 'woocommerce_customer_allowed_session_meta_keys', array(), $customer );
$session_value = maybe_serialize(
array_map(
function ( $meta_data ) {
// Data comes to us a WC_Meta_Data, we cast it to an array to ensure it is serializable.
return array(
'key' => $meta_data->key,
'value' => $meta_data->value,
);
},
array_filter(
$customer->get_meta_data(),
function ( $meta_data ) use ( $allowed_keys ) {
return in_array( $meta_data->key, $allowed_keys, true );
}
)
)
);
$allowed_keys = apply_filters( 'woocommerce_customer_allowed_session_meta_keys', array(), $customer );
$session_value = array();
foreach ( $customer->get_meta_data() as $meta_data ) {
if ( in_array( $meta_data->key, $allowed_keys, true ) ) {
$session_value[] = array(
'key' => $meta_data->key,
'value' => $meta_data->value,
);
}
}
$data['meta_data'] = $session_value;
} else {
$session_value = $customer->{"get_$function_key"}( 'edit' );
$session_value = $customer->{"get_$function_key"}( 'edit' );
$data[ $session_key ] = (string) $session_value;
}
$data[ $session_key ] = (string) $session_value;
}
WC()->session->set( 'customer', $data );
}
@ -147,9 +140,8 @@ class WC_Customer_Data_Store_Session extends WC_Data_Store_WP implements WC_Cust
}
if ( ! empty( $data[ $session_key ] ) && is_callable( array( $customer, "set_{$function_key}" ) ) ) {
if ( 'meta_data' === $session_key ) {
$meta_data_values = maybe_unserialize( $data[ $session_key ] );
if ( $meta_data_values ) {
foreach ( $meta_data_values as $meta_data_value ) {
if ( is_array( $data[ $session_key ] ) ) {
foreach ( $data[ $session_key ] as $meta_data_value ) {
if ( ! isset( $meta_data_value['key'], $meta_data_value['value'] ) ) {
continue;
}