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.


git-svn-id: https://develop.svn.wordpress.org/trunk@38041 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt 2016-07-13 04:45:50 +00:00
parent 6830e90949
commit da154081f6
2 changed files with 43 additions and 4 deletions

View File

@ -1032,6 +1032,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
// There used to be individual args for sanitize and auth callbacks
$has_old_sanitize_cb = false;
$has_old_auth_cb = false;
if ( is_callable( $args ) ) {
$args = array(
@ -1045,6 +1046,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
if ( is_callable( $deprecated ) ) {
$args['auth_callback'] = $deprecated;
$has_old_auth_cb = true;
}
$args = wp_parse_args( $args, $defaults );
@ -1062,7 +1064,7 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
// Object subtype is required if using the args style of registration
if ( ! $has_old_sanitize_cb && empty( $args['object_subtype'] ) ) {
if ( ! $has_old_sanitize_cb && ! $has_old_auth_cb && empty( $args['object_subtype'] ) ) {
return new WP_Error( 'register_meta_failed', __( 'Meta must be registered against an object subtype.' ) );
}
@ -1081,11 +1083,16 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
$object_subtype = $args['object_subtype'];
}
// Back-compat: old sanitize and auth callbacks applied to all of an object type
if ( $has_old_sanitize_cb ) {
// Back-compat: old sanitize and auth callbacks are applied to all of an object type.
if ( $has_old_sanitize_cb && is_callable( $args['sanitize_callback'] ) ) {
add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 );
}
if ( $has_old_auth_cb && is_callable( $args['auth_callback'] ) ) {
add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
} else {
}
if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb) {
if ( is_callable( $args['sanitize_callback'] ) ) {
add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 );
}

View File

@ -32,10 +32,42 @@ class Tests_Meta_Register_Meta extends WP_UnitTestCase {
return $meta_key . ' new sanitized';
}
public function _old_auth_meta_cb( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
return $allowed;
}
public function _new_auth_meta_cb( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) {
return $allowed;
}
public function test_register_meta_back_compat_with_auth_callback_and_no_sanitize_callback_has_old_style_auth_filter() {
register_meta( 'post', 'flight_number', null, array( $this, '_old_auth_meta_cb' ) );
$has_filter = has_filter( 'auth_post_meta_flight_number', array( $this, '_old_auth_meta_cb' ) );
remove_filter( 'auth_post_meta_flight_number', array( $this, '_old_auth_meta_cb' ) );
// The filter should have been added with a priority of 10.
$this->assertEquals( 10, $has_filter );
}
public function test_register_meta_back_compat_with_sanitize_callback_and_no_auth_callback_has_old_style_sanitize_filter() {
register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ) );
$has_filter = has_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb' ) );
remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb' ) );
$this->assertEquals( 10, $has_filter );
}
public function test_register_meta_back_compat_with_auth_and_sanitize_callback_has_old_style_filters() {
register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ), array( $this, '_old_auth_meta_cb' ) );
$has_filters = array();
$has_filters['auth'] = has_filter( 'auth_post_meta_flight_number', array( $this, '_old_auth_meta_cb' ) );
$has_filters['sanitize'] = has_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb' ) );
remove_filter( 'auth_post_meta_flight_number', array( $this, '_old_auth_meta_cb' ) );
remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb' ) );
$this->assertEquals( array( 'auth' => 10, 'sanitize' => 10 ), $has_filters );
}
public function test_register_meta_with_valid_object_type_and_object_subtype_returns_true() {
$result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
unregister_meta_key( 'post', 'post', 'flight_number' );