WordPress.org

Make WordPress Core

Changeset 38040


Ignore:
Timestamp:
07/13/2016 04:12:41 AM (4 years ago)
Author:
jeremyfelt
Message:

Meta: Remove filters when meta is unregistered.

If auth and/or sanitize callbacks are specified in the arguments for
register_meta(), filters are added to handle these callbacks. These
should be removed when calling unregister_meta_key() to avoid
unintentional filtering.

See #35658.

Location:
trunk
Files:
2 edited

Legend:

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

    r38027 r38040  
    11631163    }
    11641164
     1165    $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
     1166
     1167    if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
     1168        remove_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'] );
     1169    }
     1170
     1171    if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
     1172        remove_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'] );
     1173    }
     1174
    11651175    unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
    11661176
  • trunk/tests/phpunit/tests/meta/registerMeta.php

    r37995 r38040  
    3333    }
    3434
     35    public function _new_auth_meta_cb( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
     36        return $allowed;
     37    }
     38
    3539    public function test_register_meta_with_valid_object_type_and_object_subtype_returns_true() {
    3640        $result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
     
    201205        unregister_meta_key( 'post', 'post', 'flight_number' );
    202206
    203         remove_filter( 'sanitize_post_post_meta_flight_number', array( $this, '_new_sanitize_meta_cb') );
    204         remove_filter( 'auth_post_post_meta_flight_number', '__return_true');
    205 
    206207        $this->assertTrue( $result );
    207208    }
     
    213214        unregister_meta_key( 'post', 'post', 'new_sanitized_key' );
    214215
    215         remove_filter( 'sanitize_post_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb') );
    216         remove_filter( 'auth_post_post_meta_new_sanitized_key', '__return_true');
    217 
    218216        $this->assertEquals( 'new_sanitized_key new sanitized', $meta );
     217    }
     218
     219    public function test_register_meta_unregistered_meta_key_removes_sanitize_filter() {
     220        register_meta( 'post', 'new_sanitized_key', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
     221        unregister_meta_key( 'post', 'post', 'new_sanitized_key' );
     222
     223        $has_filter = has_filter( 'sanitize_post_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) );
     224
     225        $this->assertFalse( $has_filter );
     226    }
     227
     228    public function test_register_meta_unregistered_meta_key_removes_auth_filter() {
     229        register_meta( 'post', 'new_auth_key', array( 'object_subtype' => 'post', 'auth_callback' => array( $this, '_new_auth_meta_cb' ) ) );
     230        unregister_meta_key( 'post', 'post', 'new_auth_key' );
     231
     232        $has_filter = has_filter( 'auth_post_post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) );
     233
     234        $this->assertFalse( $has_filter );
    219235    }
    220236
Note: See TracChangeset for help on using the changeset viewer.