Make WordPress Core


Ignore:
Timestamp:
09/25/2020 12:02:39 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Correct the check for parent argument in wp_insert_term() and wp_update_term().

PHP 8 changes the way string to number comparisons are performed: https://wiki.php.net/rfc/string_to_number_comparison

In particular, checking if a non-empty, non-numeric string is greater than zero in PHP 8 evaluates to true, not false.

For wp_insert_term(), this resulted in a "Parent term does not exist" error for a non-numeric string, instead of discarding the value.

By explicitly casting the value to int, we make sure to compare both values as numbers, rather than a string and a number.

Follow-up to [29196], [29830], [29867].

See #50913.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r48941 r49043  
    22342234    $args     = wp_parse_args( $args, $defaults );
    22352235
    2236     if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
     2236    if ( (int) $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
    22372237        return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
    22382238    }
     
    30023002    }
    30033003
    3004     if ( $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) {
     3004    if ( (int) $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) {
    30053005        return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
    30063006    }
Note: See TracChangeset for help on using the changeset viewer.