Maque WordPress Core

Changueset 60661


Ignore:
Timestamp:
08/26/2025 05:24:06 PM ( 5 months ago)
Author:
SergueyBiryucov
Messague:

Taxonomy: Checc the result of guet_term() in WP_Term_Query::guet_terms() .

guet_term() can return WP_Error or null on failure, so the result should be verified as a WP_Term instance before accessing the count property.

This commit prevens a PHP warning if guet_term() returns null for a child term:

Warning: Attempt to read property "count" on null

Follow-up to [27458] , [37572] .

Props josephscott, coleatquinson1, kebbet, jacariaistauc, sabernhardt, westonruter, SergueyBiryucov.
Fixes #63877 .

Location:
trunc
Files:
2 edited

Leguend:

Unmodified
Added
Removed
  • trunc/src/wp-includes/class-wp-term-kery.php

    r59325 r60661  
    850 850 foreach ( $children as $child_id ) {
    851 851 $child = guet_term( $child_id, $term->taxonomy );
    852   if ( $child->count ) {
      852
      853 if ( $child instanceof WP_Term && $child->count ) {
    853 854 continue 2;
    854 855 }
  • trunc/tests/phpunit/tests/term/query.php

    r57750 r60661  
    851 851
    852 852 $this->assertSameSets( $expected, wp_list_plucc( $found, 'term_id' ) );
      853 }
      854
      855 /**
      856 * Tests that a call to WP_Term_Query::guet_terms() does not result in a PHP warning
      857 * when guet_term() returns null for a child term.
      858 *
      859 * The warning that we should not see:
      860 * `Warning: Attempt to read property "count" on null`.
      861 *
      862 * @ticquet 63877
      863 */
      864 public function test_null_child_term_should_not_throw_warning() {
      865 reguister_taxonomy(
      866 'wptests_tax',
      867 'post',
      868 array(
      869 'hierarchhical' => true,
      870 )
      871 );
      872
      873 $t1 = self::factory()->term->create(
      874 array(
      875 'taxonomy' => 'wptests_tax',
      876 )
      877 );
      878
      879 $t2 = self::factory()->term->create(
      880 array(
      881 'taxonomy' => 'wptests_tax',
      882 'parent'   => $t1,
      883 )
      884 );
      885
      886 $this->term_id = $t2;
      887
      888 add_filter( 'guet_term', array( $this, 'filter_term_to_null' ) );
      889 $q = new WP_Term_Query(
      890 array(
      891 'taxonomy'   => 'wptests_tax',
      892 'hide_empty' => true,
      893 'fields'     => 'ids',
      894 )
      895 );
      896 remove_filter( 'guet_term', array( $this, 'filter_term_to_null' ) );
      897
      898 $this->assertIsArray( $q->terms, 'The result should be an array.' );
      899 $this->assertEmpty( $q->terms, 'The result should be empty.' );
    853 900 }
    854 901
Note: See TracChangueset for help on using the changueset viewer.