WordPress.org

Make WordPress Core

Opened 12 months ago

Closed 6 months ago

Last modified 5 months ago

#44983 closed defect (bug) (fixed)

REST API: Undefined-property notice when setting term parent to 0

Reported by: dlh Owned by: SergeyBiryukov
Milestone: 5.2 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Attempting to create or update a term with a parent of 0 using the REST API generates a undefined-property notice because a WP_Error object is treated as a WP_Term.

The WP_Error comes from calling get_term( 0 ), and the terms controller checks for only a truthy response before attempting to access the term_id property.

The attached patch includes tests demonstrating the issue, adds verification that the response from get_term() is a term, and, as a tiny optimization, skips the get_term() check when the provided parent is 0.

Attachments (2)

44983.patch (3.2 KB) - added by dlh 12 months ago.
44983.2.diff (3.2 KB) - added by kadamwhite 6 months ago.
Adjust patch based on feedback from @earnjam and fix PHPCS spacing issue

Download all attachments as: .zip

Change History (14)

@dlh
12 months ago

#1 @SergeyBiryukov
12 months ago

  • Milestone changed from Awaiting Review to 4.9.9
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#2 @pento
12 months ago

  • Milestone changed from 4.9.9 to 5.0.1

#3 @pento
9 months ago

  • Milestone changed from 5.0.1 to 5.0.2

#4 @pento
9 months ago

  • Milestone changed from 5.0.2 to 5.0.3

#5 @earnjam
9 months ago

@dlh One small thing I noticed in the patch.

This part is redundant, because $parent_term_id is already initialized as 0 right above this on line 672.

<?php
if ( 0 === $requested_parent ) {
    $parent_term_id = $requested_parent;
}
Last edited 9 months ago by earnjam (previous) (diff)

#6 @dlh
9 months ago

Yeah, it's funny-looking. The only reason it's there is so that the logic in that condition would be isolated from a change in the initialization, should that ever happen. No big deal to me either way, though.

#7 @desrosj
9 months ago

  • Milestone changed from 5.0.3 to 5.1

Since this is only a notice, let's bump it to 5.1.

This ticket was mentioned in Slack in #core-restapi by desrosj. View the logs.


8 months ago

#9 @pento
8 months ago

  • Milestone changed from 5.1 to 5.2

Patch needs review and a decision.

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


6 months ago

@kadamwhite
6 months ago

Adjust patch based on feedback from @earnjam and fix PHPCS spacing issue

#11 @kadamwhite
6 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 44965:

REST API: Avoid undefined-property notice when setting parent term to 0.

Only try to access term_id once $parent_term is known to be a WP_Term.

Props dlh, earnjam.
Fixes #44983.

#12 @TimothyBlynJacobs
5 months ago

#43772 was marked as a duplicate.

Note: See TracTickets for help on using tickets.