WordPress.org

Make WordPress Core


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

Meta: Ensure filters are backwards compatible for pre-4.6 style meta registration.

When using register_meta() with the function signature from 4.5 and earlier, the auth_{$type}_meta_{$key} and sanitize_{$type}_meta_{$key} filters are used. Any calls to register_meta() expecting this behavior should continue to work. The new filters, which take advantage of object subtypes, should not be added unless the proper $args array is passed.

See #35658.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/meta/registerMeta.php

    r38040 r38041  
    3333    }
    3434
     35    public function _old_auth_meta_cb( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
     36        return $allowed;
     37    }
     38
    3539    public function _new_auth_meta_cb( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
    3640        return $allowed;
     41    }
     42
     43    public function test_register_meta_back_compat_with_auth_callback_and_no_sanitize_callback_has_old_style_auth_filter() {
     44        register_meta( 'post', 'flight_number', null, array( $this, '_old_auth_meta_cb' ) );
     45        $has_filter = has_filter( 'auth_post_meta_flight_number', array( $this, '_old_auth_meta_cb' ) );
     46        remove_filter( 'auth_post_meta_flight_number', array( $this, '_old_auth_meta_cb' ) );
     47
     48        // The filter should have been added with a priority of 10.
     49        $this->assertEquals( 10, $has_filter );
     50    }
     51
     52    public function test_register_meta_back_compat_with_sanitize_callback_and_no_auth_callback_has_old_style_sanitize_filter() {
     53        register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ) );
     54        $has_filter = has_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb' ) );
     55        remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb' ) );
     56
     57        $this->assertEquals( 10, $has_filter );
     58    }
     59
     60    public function test_register_meta_back_compat_with_auth_and_sanitize_callback_has_old_style_filters() {
     61        register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ), array( $this, '_old_auth_meta_cb' ) );
     62        $has_filters = array();
     63        $has_filters['auth'] = has_filter( 'auth_post_meta_flight_number', array( $this, '_old_auth_meta_cb' ) );
     64        $has_filters['sanitize'] = has_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb' ) );
     65        remove_filter( 'auth_post_meta_flight_number', array( $this, '_old_auth_meta_cb' ) );
     66        remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb' ) );
     67
     68        $this->assertEquals( array( 'auth' => 10, 'sanitize' => 10 ), $has_filters );
    3769    }
    3870
Note: See TracChangeset for help on using the changeset viewer.