[wp-trac] [WordPress Trac] #57169: Prevent saving of invalid menu_item_parent

WordPress Trac noreply at wordpress.org
Fri Jan 13 00:34:46 UTC 2023


#57169: Prevent saving of invalid menu_item_parent
-----------------------------------+-----------------------
 Reporter:  azaozz                 |       Owner:  azaozz
     Type:  defect (bug)           |      Status:  reopened
 Priority:  normal                 |   Milestone:  6.1.2
Component:  Menus                  |     Version:  6.1
 Severity:  normal                 |  Resolution:
 Keywords:  has-patch fixed-major  |     Focuses:
-----------------------------------+-----------------------

Comment (by SergeyBiryukov):

 Replying to [comment:14 azaozz]:

 > To avoid modifying the object, use `strval()` rather than casting to
 (string).

 Hmm, I might be missing something, but I don't see how this:
 {{{
 if ( strval( $menu_item->ID ) === strval( $menu_item->menu_item_parent ) )
 {
 }}}
 would be different from this:
 {{{
 if ( (string) $menu_item->ID ) === (string) $menu_item->menu_item_parent )
 ) {
 }}}

 In my understanding, casting to a string here should only affect this
 particular comparison and should not modify the object nor change anything
 else.

 A test script like this:
 {{{
 $test = new stdClass;
 $test->id = 123;

 var_dump( (string) $test->id ); // Should be a string.
 var_dump( $test->id );          // Should be an integer.
 }}}
 Results in:
 {{{
 string(3) "123"
 int(123)
 }}}

 It looks like this was discussed on the PR with a remark that `strval()`
 is used elsewhere in core, but that's actually not the case since [49108]
 / #42918, which replaced all instances of `strval()`, `intval()`, and
 `floatval()` with `(string)`, `(int)`, and `(float)`, respectively, except
 when used in `array_map()`:
 {{{
 $widget_ids = array_map( 'strval', (array) $widget_ids );
 }}}

 So bringing back `strval()` here is inconsistent with the rest of core.
 Could we reconsider using `(string)` for this comparison? What would that
 affect in practice?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57169#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list