[wp-trac] [WordPress Trac] #54355: Serialization of 'Closure' is not allowed
WordPress Trac
noreply at wordpress.org
Mon Nov 1 14:18:16 UTC 2021
#54355: Serialization of 'Closure' is not allowed
---------------------------+-----------------------------
Reporter: cambridgeandy | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 5.8.1
Severity: normal | Keywords:
Focuses: |
---------------------------+-----------------------------
Hi,
I'm using an object to manage my initialization.
I'm looking at the documentation at
https://developer.wordpress.org/reference/functions/register_activation_hook/
When I register a hook using a closure (callable) that invokes a method on
the object I get an error saying that "Serialization of 'Closure' is not
allowed thrown at line [599] in file [/var/www/html/wp-
includes/functions.php]"
Here is my offending code:
{{{#!php
register_activation_hook(WPMP_BASE_FILE_NAME, function() use
($initialize_service) {
$initialize_service->activate();
});`
}}}
Looking at the code in `functions.php` I can see that there are checks if
the data is an array or an object:
{{{
if ( is_array( $data ) || is_object( $data ) ) {
return serialize( $data );
}}}
The issue is that the Closure is seen as an object, but cannot be
serialized (see https://3v4l.org/s23qj)
A potential fix could be:
{{{
function maybe_serialize( $data ) {
if ( is_array( $data ) || is_object( $data ) ) {
try {
return serialize( $data );
} catch (\Throwable $e) {
return $data;
}
}
}}}
However, this requires PHP7.0 which would violate the requirement for WP
core to support 5.6
--
Ticket URL: <https://core.trac.wordpress.org/ticket/54355>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list