Skip to content

Commit

Permalink
Code Modernization: Fix null to non-nullable deprecation in `term_exi…
Browse files Browse the repository at this point in the history
…sts()`.

The `term_exists()` function expects a string or an integer for the `$term` parameter. It validates for integer, but not for string or `null`.

One of the pre-existing test cases, passed `null` to the function, leading to a `trim(): Passing null to parameter #1 ($string) of type string is deprecated` notice on PHP 8.1.

Fixed now by doing a cursory check on the variable at the start of the function and bowing out early in case the `$term` is `null`.

The issue was discovered via and is already covered by the `Tests_TermExists::test_term_exists_unknown()` test method.

Follow-up to [15220]. [38716].

Props jrf, hellofromTonya.
See #53635.

git-svn-id: https://develop.svn.wordpress.org/trunk@51796 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Sep 10, 2021
1 parent ce168ea commit 9abf35c
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,10 @@ function unregister_term_meta( $taxonomy, $meta_key ) {
function term_exists( $term, $taxonomy = '', $parent = null ) {
global $wpdb;

if ( null === $term ) {
return null;
}

$select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
$tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE ";

Expand Down

0 comments on commit 9abf35c

Please sign in to comment.