Make WordPress Core

Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#44983 closed defect (bug) (fixed)

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

Reported by: dlh's profile dlh Owned by: sergeybiryukov's profile 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 6 years ago.
44983.2.diff (3.2 KB) - added by kadamwhite 5 years ago.
Adjust patch based on feedback from @earnjam and fix PHPCS spacing issue

Download all attachments as: .zip

Change History (14)

@dlh
6 years ago

#1 @SergeyBiryukov
6 years ago

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

#2 @pento
6 years ago

  • Milestone changed from 4.9.9 to 5.0.1

#3 @pento
6 years ago

  • Milestone changed from 5.0.1 to 5.0.2

#4 @pento
6 years ago

  • Milestone changed from 5.0.2 to 5.0.3

#5 @earnjam
6 years 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 6 years ago by earnjam (previous) (diff)

#6 @dlh
6 years 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
6 years 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.


6 years ago

#9 @pento
6 years 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.


5 years ago

@kadamwhite
5 years ago

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

#11 @kadamwhite
5 years 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 years ago

#43772 was marked as a duplicate.

Note: See TracTickets for help on using tickets.