diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index e341bd2bcc..ff32086725 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -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 ); } diff --git a/tests/phpunit/tests/meta/registerMeta.php b/tests/phpunit/tests/meta/registerMeta.php index 594a57374d..0da8b0d38b 100644 --- a/tests/phpunit/tests/meta/registerMeta.php +++ b/tests/phpunit/tests/meta/registerMeta.php @@ -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' );