[wp-trac] [WordPress Trac] #60355: Can't save object to metadata with `readonly` properties
WordPress Trac
noreply at wordpress.org
Fri Jan 26 15:45:41 UTC 2024
#60355: Can't save object to metadata with `readonly` properties
--------------------------+-----------------------------
Reporter: Cawa-93 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Formatting | Version:
Severity: major | Keywords:
Focuses: |
--------------------------+-----------------------------
If I have class with `readonly` property
{{{#!php
<?php
class Foo {
__constructor(public readonly int $prop) {}
}}}
It throws an exception if you try to save it to medatata:
{{{#!php
update_metadata('post', 1, 'key', new Foo(1));
}}}
{{{#!php
Error:
Cannot modify readonly property App\Namespace\Foo::$prop
at /var/www/html/web/wp/wp-includes/formatting.php:5148
at map_deep(object(Foo), 'stripslashes_from_strings_only')
(/var/www/html/web/wp/wp-includes/formatting.php:5148)
at map_deep(object(Foo), 'stripslashes_from_strings_only')
(/var/www/html/web/wp/wp-includes/formatting.php:5143)
at map_deep(array(object(Foo), object(Foo), object(Foo)),
'stripslashes_from_strings_only')
(/var/www/html/web/wp/wp-includes/formatting.php:2855)
at stripslashes_deep(array(object(Foo), object(Foo), object(Foo)))
(/var/www/html/web/wp/wp-includes/formatting.php:5804)
at wp_unslash(array(object(Foo), object(Foo), object(Foo)))
(/var/www/html/web/wp/wp-includes/meta.php:208)
at update_metadata('post', 1, 'key', array(object(Foo), object(Foo),
object(Foo)), '')
(/var/www/html/web/wp/wp-includes/post.php:2558)
}}}
The issue cause in `map_deep` function since it tries to overwrite
properties
{{{#!php
$object_vars = get_object_vars( $value );
foreach ( $object_vars as $property_name => $property_value ) {
$value->$property_name = map_deep( $property_value, $callback
); // <---------
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/60355>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list