Make WordPress Core

Ticket #35658: 35658.38.diff

File 35658.38.diff, 4.3 KB (added by jeremyfelt, 8 years ago)
  • src/wp-includes/meta.php

     
    10321032
    10331033        // There used to be individual args for sanitize and auth callbacks
    10341034        $has_old_sanitize_cb = false;
     1035        $has_old_auth_cb = false;
    10351036
    10361037        if ( is_callable( $args ) ) {
    10371038                $args = array(
     
    10451046
    10461047        if ( is_callable( $deprecated ) ) {
    10471048                $args['auth_callback'] = $deprecated;
     1049                $has_old_auth_cb = true;
    10481050        }
    10491051
    10501052        $args = wp_parse_args( $args, $defaults );
     
    10621064        $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
    10631065
    10641066        // Object subtype is required if using the args style of registration
    1065         if ( ! $has_old_sanitize_cb && empty( $args['object_subtype'] ) ) {
     1067        if ( ! $has_old_sanitize_cb && ! $has_old_auth_cb && empty( $args['object_subtype'] ) ) {
    10661068                return new WP_Error( 'register_meta_failed', __( 'Meta must be registered against an object subtype.' ) );
    10671069        }
    10681070
     
    10811083                $object_subtype = $args['object_subtype'];
    10821084        }
    10831085
    1084         // Back-compat: old sanitize and auth callbacks applied to all of an object type
    1085         if ( $has_old_sanitize_cb ) {
     1086        // Back-compat: old sanitize and auth callbacks are applied to all of an object type.
     1087        if ( $has_old_sanitize_cb && is_callable( $args['sanitize_callback'] ) ) {
    10861088                add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 );
     1089        }
     1090
     1091        if ( $has_old_auth_cb && is_callable( $args['auth_callback'] ) ) {
    10871092                add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
    1088         } else {
     1093        }
     1094
     1095        if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb) {
    10891096                if ( is_callable( $args['sanitize_callback'] ) ) {
    10901097                        add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 );
    10911098                }
  • tests/phpunit/tests/meta/registerMeta.php

     
    3232                return $meta_key . ' new sanitized';
    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;
    3741        }
    3842
     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 );
     69        }
     70
    3971        public function test_register_meta_with_valid_object_type_and_object_subtype_returns_true() {
    4072                $result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
    4173                unregister_meta_key( 'post', 'post', 'flight_number' );