Make WordPress Core

Changeset 29422


Ignore:
Timestamp:
08/07/2014 02:29:15 AM (9 years ago)
Author:
wonderboymusic
Message:

After [28883], ensure that priorities have callbacks before returning true in has_filter().

Adds unit tests.

Props boonebgorges.
Fixes #29070.

Location:
trunk
Files:
2 edited

Legend:

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

    r28891 r29422  
    108108    global $wp_filter;
    109109
    110     $has = !empty($wp_filter[$tag]);
     110    $has = ! empty( $wp_filter[ $tag ] );
     111
     112    // Make sure at least one priority has a filter callback
     113    if ( $has ) {
     114        $exists = false;
     115        foreach ( $wp_filter[ $tag ] as $callbacks ) {
     116            if ( ! empty( $callbacks ) ) {
     117                $exists = true;
     118                break;
     119            }
     120        }
     121
     122        if ( ! $exists ) {
     123            $has = false;
     124        }
     125    }
     126
    111127    if ( false === $function_to_check || false == $has )
    112128        return $has;
  • trunk/tests/phpunit/tests/filters.php

    r25377 r29422  
    268268        $this->assertEquals( $result, $tag . '_append_append', 'priority 11 and 12 filters should run after priority 10 empties itself' );
    269269    }
     270
     271    /**
     272     * @ticket 29070
     273     */
     274    function test_has_filter_after_remove_all_filters() {
     275        $a = new MockAction();
     276        $tag = rand_str();
     277        $val = rand_str();
     278
     279        // No priority
     280        add_filter( $tag, array( $a, 'filter' ), 11 );
     281        add_filter( $tag, array( $a, 'filter' ), 12 );
     282        $this->assertTrue( has_filter( $tag ) );
     283
     284        remove_all_filters( $tag );
     285        $this->assertFalse( has_filter( $tag ) );
     286
     287        // Remove priorities one at a time
     288        add_filter( $tag, array( $a, 'filter' ), 11 );
     289        add_filter( $tag, array( $a, 'filter' ), 12 );
     290        $this->assertTrue( has_filter( $tag ) );
     291
     292        remove_all_filters( $tag, 11 );
     293        remove_all_filters( $tag, 12 );
     294        $this->assertFalse( has_filter( $tag ) );
     295    }
    270296}
Note: See TracChangeset for help on using the changeset viewer.