Ticket #35658: 35658.49.diff
File 35658.49.diff, 31.4 KB (added by , 8 years ago) |
---|
-
src/wp-includes/functions.php
5358 5358 } 5359 5359 5360 5360 /** 5361 * Check if an object type exists. By default, these are `post`, `comment`, `user`, and `term`.5362 *5363 * @since 4.6.05364 *5365 * @param string $object_type Object type to check.5366 * @return bool True if the object type exists, false if not.5367 */5368 function wp_object_type_exists( $object_type ) {5369 /**5370 * Filters WordPress object types.5371 *5372 * @since 4.6.05373 *5374 * @param array $types Array of object types.5375 */5376 $types = apply_filters( 'wp_object_types', array( 'post', 'comment', 'user', 'term' ) );5377 5378 if ( in_array( $object_type, $types ) ) {5379 return true;5380 }5381 5382 return false;5383 }5384 5385 /**5386 5361 * Attempts to raise the PHP memory limit for memory intensive processes. 5387 5362 * 5388 5363 * Only allows raising the existing limit and prevents lowering it. -
src/wp-includes/meta.php
936 936 * Sanitize meta value. 937 937 * 938 938 * @since 3.1.3 939 * @since 4.6.0 Added the `$object_subtype` parameter.940 939 * 941 940 * @param string $meta_key Meta key. 942 941 * @param mixed $meta_value Meta value to sanitize. 943 942 * @param string $object_type Type of object the meta is registered to. 944 * @param string $object_subtype Optional. Subtype of object. Will inherit the object type by default.945 943 * 946 944 * @return mixed Sanitized $meta_value. 947 945 */ 948 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { 949 if ( ! empty( $object_subtype ) ) { 950 /** 951 * Filters the sanitization of a specific meta key of a specific meta type and subtype. 952 * 953 * The dynamic portions of the hook name, `$meta_type`, `$meta_subtype`, 954 * and `$meta_key`, refer to the metadata object type (comment, post, or user) 955 * the object subtype, and the meta key value, respectively. 956 * 957 * @since 4.6.0 958 * 959 * @param mixed $meta_value Meta value to sanitize. 960 * @param string $meta_key Meta key. 961 * @param string $object_type Object type. 962 * @param string $object_subtype Object subtype. 963 */ 964 $meta_value = apply_filters( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype ); 965 } 966 946 function sanitize_meta( $meta_key, $meta_value, $object_type ) { 967 947 /** 968 948 * Filters the sanitization of a specific meta key of a specific meta type. 969 949 * … … 972 952 * key value, respectively. 973 953 * 974 954 * @since 3.3.0 975 * @since 4.6.0 Added the `$object_subtype` parameter.976 955 * 977 956 * @param mixed $meta_value Meta value to sanitize. 978 957 * @param string $meta_key Meta key. 979 958 * @param string $object_type Object type. 980 * @param string $object_subtype Object subtype.981 959 */ 982 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type , $object_subtype);960 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type ); 983 961 } 984 962 985 963 /** … … 995 973 * @param array $args { 996 974 * Data used to describe the meta key when registered. 997 975 * 998 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type.999 976 * @type string $type The type of data associated with this meta key. 1000 977 * @type string $description A description of the data attached to this meta key. 1001 978 * @type bool $single Whether the meta key has one value per object, or an array of values per object. … … 1005 982 * } 1006 983 * @param string|array $deprecated Deprecated. Use `$args` instead. 1007 984 * 1008 * @return bool |WP_error True if the meta key was successfully registered in the global array, WP_Errorif not.985 * @return bool True if the meta key was successfully registered in the global array, false if not. 1009 986 * Registering a meta key with distinct sanitize and auth callbacks will fire those 1010 * callbacks, but will not add to the global registry as it requires a subtype.987 * callbacks, but will not add to the global registry. 1011 988 */ 1012 989 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { 1013 990 global $wp_meta_keys; … … 1016 993 $wp_meta_keys = array(); 1017 994 } 1018 995 1019 if ( ! wp_object_type_exists( $object_type ) ) {1020 return new WP_Error( 'register_meta_failed', __( 'Meta can only be registered against a core object type.' ) );1021 }1022 1023 996 $defaults = array( 1024 'object_subtype' => '',1025 997 'type' => 'string', 1026 998 'description' => '', 1027 999 'single' => false, … … 1063 1035 */ 1064 1036 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1065 1037 1066 // Object subtype is required if using the args style of registration1067 if ( ! $has_old_sanitize_cb && ! $has_old_auth_cb && empty( $args['object_subtype'] ) ) {1068 return new WP_Error( 'register_meta_failed', __( 'Meta must be registered against an object subtype.' ) );1069 }1070 1071 1038 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. 1072 1039 if ( empty( $args['auth_callback'] ) ) { 1073 1040 if ( is_protected_meta( $meta_key, $object_type ) ) { … … 1077 1044 } 1078 1045 } 1079 1046 1080 $object_subtype = '';1081 1082 if ( ! empty( $args['object_subtype'] ) ) {1083 $object_subtype = $args['object_subtype'];1084 }1085 1086 1047 // 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'] ) ) {1088 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4);1048 if ( is_callable( $args['sanitize_callback'] ) ) { 1049 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); 1089 1050 } 1090 1051 1091 if ( $has_old_auth_cb &&is_callable( $args['auth_callback'] ) ) {1052 if ( is_callable( $args['auth_callback'] ) ) { 1092 1053 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1093 1054 } 1094 1055 1095 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb) { 1096 if ( is_callable( $args['sanitize_callback'] ) ) { 1097 add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 ); 1098 } 1056 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1057 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1058 $wp_meta_keys[ $object_type ][ $meta_key ] = $args; 1099 1059 1100 if ( is_callable( $args['auth_callback'] ) ) {1101 add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );1102 }1103 }1104 1105 // Global registry only contains meta keys registered in the new way with a subtype.1106 if ( ! empty( $object_subtype ) ) {1107 $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;1108 1109 1060 return true; 1110 1061 } 1111 1062 1112 return new WP_Error( 'register_meta_failed', __( 'Sanitize and auth callbacks registered; meta key not registered.' ) );1063 return false; 1113 1064 } 1114 1065 1115 1066 /** … … 1118 1069 * @since 4.6.0 1119 1070 * 1120 1071 * @param string $object_type The type of object. 1121 * @param string $object_subtype The subtype of the object type.1122 1072 * @param string $meta_key The meta key. 1123 1073 * 1124 * @return bool|WP_error True if the meta key is registered to the object type and subtype. False if not. 1125 * WP_Error if an invalid object type is passed. 1074 * @return bool True if the meta key is registered to the object type. False if not. 1126 1075 */ 1127 function registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) {1076 function registered_meta_key_exists( $object_type, $meta_key ) { 1128 1077 global $wp_meta_keys; 1129 1078 1130 1079 if ( ! is_array( $wp_meta_keys ) ) { … … 1131 1080 return false; 1132 1081 } 1133 1082 1134 // Only specific core object types are supported.1135 if ( ! wp_object_type_exists( $object_type ) ) {1136 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) );1137 }1138 1139 1083 if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { 1140 1084 return false; 1141 1085 } 1142 1086 1143 if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1144 return false; 1145 } 1146 1147 if ( isset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ) ) { 1087 if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) { 1148 1088 return true; 1149 1089 } 1150 1090 … … 1157 1097 * @since 4.6.0 1158 1098 * 1159 1099 * @param string $object_type The type of object. 1160 * @param string $object_subtype The subtype of the object type.1161 1100 * @param string $meta_key The meta key. 1162 1101 * 1163 * @return bool |WP_Error True if successful. WP_Error if the meta key is invalid.1102 * @return bool True if successful. False if the meta key was not registered. 1164 1103 */ 1165 function unregister_meta_key( $object_type, $ object_subtype, $meta_key ) {1104 function unregister_meta_key( $object_type, $meta_key ) { 1166 1105 global $wp_meta_keys; 1167 1106 1168 if ( ! registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) ) {1169 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) );1107 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1108 return false; 1170 1109 } 1171 1110 1172 $args = $wp_meta_keys[ $object_type ][ $ object_subtype ][ $meta_key ];1111 $args = $wp_meta_keys[ $object_type ][ $meta_key ]; 1173 1112 1174 1113 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 1175 remove_filter( "sanitize_{$object_type}_ {$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'] );1114 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); 1176 1115 } 1177 1116 1178 1117 if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) { 1179 remove_filter( "auth_{$object_type}_ {$object_subtype}_meta_{$meta_key}", $args['auth_callback'] );1118 remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] ); 1180 1119 } 1181 1120 1182 unset( $wp_meta_keys[ $object_type ][ $ object_subtype ][ $meta_key ] );1121 unset( $wp_meta_keys[ $object_type ][ $meta_key ] ); 1183 1122 1184 1123 // Do some clean up 1185 if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {1186 unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );1187 }1188 1189 1124 if ( empty( $wp_meta_keys[ $object_type ] ) ) { 1190 1125 unset( $wp_meta_keys[ $object_type ] ); 1191 1126 } … … 1194 1129 } 1195 1130 1196 1131 /** 1197 * Retrieves a list of registered meta keys for an object type and optionally subtype.1132 * Retrieves a list of registered meta keys for an object type. 1198 1133 * 1199 1134 * @since 4.6.0 1200 1135 * 1201 * @param string $object_type The type of object. Post, comment, user, term. 1202 * @param string $object_subtype Optional. A subtype of the object (e.g. custom post type). 1136 * @param string $object_type The type of object. Post, comment, user, term. 1203 1137 * 1204 1138 * @return array List of registered meta keys. 1205 1139 */ 1206 function get_registered_meta_keys( $object_type , $object_subtype = '') {1140 function get_registered_meta_keys( $object_type ) { 1207 1141 global $wp_meta_keys; 1208 1142 1209 1143 if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { … … 1210 1144 return array(); 1211 1145 } 1212 1146 1213 if ( empty( $object_subtype ) && isset( $wp_meta_keys[ $object_type ] ) ) { 1214 return $wp_meta_keys[ $object_type ]; 1215 } 1216 1217 if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1218 return array(); 1219 } 1220 1221 return $wp_meta_keys[ $object_type ][ $object_subtype ]; 1147 return $wp_meta_keys[ $object_type ]; 1222 1148 } 1223 1149 1224 1150 /** … … 1226 1152 * 1227 1153 * @since 4.6.0 1228 1154 * 1229 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) 1230 * @param string $object_subtype The subtype of the object's type to request metadata for. (e.g. custom post type) 1231 * @param int $object_id ID of the object the metadata is for. 1232 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered 1233 * metadata for the specified object. 1155 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) 1156 * @param int $object_id ID of the object the metadata is for. 1157 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered 1158 * metadata for the specified object. 1234 1159 * 1235 * @return mixed|WP_Error 1160 * @return mixed A single value or array of values for a key if specified. An array of all registered keys 1161 * and values for an object ID if not. 1236 1162 */ 1237 function get_registered_metadata( $object_type, $object_subtype, $object_id, $meta_key = '' ) { 1238 global $wp_meta_keys; 1239 1240 if ( ! is_array( $wp_meta_keys ) ) { 1241 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); 1242 } 1243 1244 if ( ! wp_object_type_exists( $object_type ) ) { 1245 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) ); 1246 } 1247 1163 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { 1248 1164 if ( ! empty( $meta_key ) ) { 1249 if ( ! registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) ) {1250 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) );1165 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1166 return false; 1251 1167 } 1252 $meta_keys = get_registered_meta_keys( $object_type , $object_subtype);1168 $meta_keys = get_registered_meta_keys( $object_type ); 1253 1169 $meta_key_data = $meta_keys[ $meta_key ]; 1254 1170 1255 1171 $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] ); … … 1259 1175 1260 1176 $data = get_metadata( $object_type, $object_id ); 1261 1177 1262 $meta_keys = get_registered_meta_keys( $object_type , $object_subtype);1178 $meta_keys = get_registered_meta_keys( $object_type ); 1263 1179 $registered_data = array(); 1264 1180 1265 1181 // Someday, array_filter() -
tests/phpunit/tests/meta/registerMeta.php
3 3 * @group meta 4 4 */ 5 5 class Tests_Meta_Register_Meta extends WP_UnitTestCase { 6 protected static $editor_id;7 6 protected static $post_id; 8 protected static $comment_id;9 7 10 8 public static function wpSetUpBeforeClass( $factory ) { 11 self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );12 9 self::$post_id = $factory->post->create(); 13 self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );14 10 } 15 11 16 12 public static function wpTearDownAfterClass() { 17 self::delete_user( self::$editor_id );18 wp_delete_comment( self::$comment_id, true );19 13 wp_delete_post( self::$post_id, true ); 20 14 } 21 15 22 function setUp() {23 parent::setUp();24 wp_set_current_user( self::$editor_id );25 }26 27 16 public function _old_sanitize_meta_cb( $meta_value, $meta_key, $meta_type ) { 28 17 return $meta_key . ' old sanitized'; 29 18 } 30 19 31 public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type , $object_subtype) {20 public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type ) { 32 21 return $meta_key . ' new sanitized'; 33 22 } 34 23 … … 68 57 $this->assertEquals( array( 'auth' => 10, 'sanitize' => 10 ), $has_filters ); 69 58 } 70 59 71 public function test_register_meta_with_ valid_object_type_and_object_subtype_returns_true() {72 $result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post') );73 unregister_meta_key( 'post', ' post', 'flight_number' );60 public function test_register_meta_with_post_object_type_returns_true() { 61 $result = register_meta( 'post', 'flight_number', array() ); 62 unregister_meta_key( 'post', 'flight_number' ); 74 63 75 64 $this->assertTrue( $result ); 76 65 } 77 66 78 public function test_register_meta_with_post_object_type_ and_subtype_populates_wp_meta_keys() {67 public function test_register_meta_with_post_object_type_populates_wp_meta_keys() { 79 68 global $wp_meta_keys; 80 69 81 register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post') );70 register_meta( 'post', 'flight_number', array() ); 82 71 $actual = $wp_meta_keys; 83 unregister_meta_key( 'post', ' post', 'flight_number' );72 unregister_meta_key( 'post', 'flight_number' ); 84 73 85 74 $expected = array( 86 75 'post' => array( 87 'post' => array( 88 'flight_number' => array( 89 'object_subtype' => 'post', 90 'type' => 'string', 91 'description' => '', 92 'single' => false, 93 'sanitize_callback' => null, 94 'auth_callback' => '__return_true', 95 'show_in_rest' => false, 96 ), 76 'flight_number' => array( 77 'type' => 'string', 78 'description' => '', 79 'single' => false, 80 'sanitize_callback' => null, 81 'auth_callback' => '__return_true', 82 'show_in_rest' => false, 97 83 ), 98 84 ), 99 85 ); … … 101 87 $this->assertEquals( $actual, $expected ); 102 88 } 103 89 104 public function test_register_meta_with_ post_object_type_and_unregistered_subtype_populates_wp_meta_keys() {90 public function test_register_meta_with_term_object_type_populates_wp_meta_keys() { 105 91 global $wp_meta_keys; 106 107 register_meta( 'post', 'flight_number', array( 'object_subtype' => 'not_a_post_type' ) ); 92 register_meta( 'term', 'category_icon', array() ); 108 93 $actual = $wp_meta_keys; 109 unregister_meta_key( ' post', 'not_a_post_type', 'flight_number' );94 unregister_meta_key( 'term', 'category_icon' ); 110 95 111 96 $expected = array( 112 'post' => array(113 'not_a_post_type' => array(114 'flight_number' => array(115 'object_subtype' => 'not_a_post_type',116 'type' => 'string',117 'description' => '',118 'single' => false,119 'sanitize_callback' => null,120 'auth_callback' => '__return_true',121 'show_in_rest' => false,122 ),123 ),124 ),125 );126 127 $this->assertEquals( $actual, $expected );128 }129 130 public function test_register_meta_with_term_object_type_and_category_subtype_populates_wp_meta_keys() {131 global $wp_meta_keys;132 register_meta( 'term', 'category_icon', array( 'object_subtype' => 'category' ) );133 $actual = $wp_meta_keys;134 unregister_meta_key( 'term', 'category', 'category_icon' );135 136 $expected = array(137 97 'term' => array( 138 'category' => array( 139 'category_icon' => array( 140 'object_subtype' => 'category', 141 'type' => 'string', 142 'description' => '', 143 'single' => false, 144 'sanitize_callback' => null, 145 'auth_callback' => '__return_true', 146 'show_in_rest' => false, 147 ), 98 'category_icon' => array( 99 'type' => 'string', 100 'description' => '', 101 'single' => false, 102 'sanitize_callback' => null, 103 'auth_callback' => '__return_true', 104 'show_in_rest' => false, 148 105 ), 149 106 ), 150 107 ); … … 152 109 $this->assertEquals( $actual, $expected ); 153 110 } 154 111 155 public function test_register_meta_with_comment_object_type_and_subtype_populates_wp_meta_keys() {156 global $wp_meta_keys;157 register_meta( 'comment', 'comment_rating', array( 'object_subtype' => 'comment' ) );158 $actual = $wp_meta_keys;159 unregister_meta_key( 'comment', 'comment', 'comment_rating' );160 161 $expected = array(162 'comment' => array(163 'comment' => array(164 'comment_rating' => array(165 'object_subtype' => 'comment',166 'type' => 'string',167 'description' => '',168 'single' => false,169 'sanitize_callback' => null,170 'auth_callback' => '__return_true',171 'show_in_rest' => false,172 ),173 ),174 ),175 );176 177 $this->assertEquals( $actual, $expected );178 }179 180 112 public function test_register_meta_with_deprecated_sanitize_callback_does_not_populate_wp_meta_keys() { 181 113 global $wp_meta_keys; 182 114 … … 188 120 $this->assertEquals( array(), $actual ); 189 121 } 190 122 191 public function test_register_meta_with_deprecated_sanitize_callback_param_returns_ wp_error() {123 public function test_register_meta_with_deprecated_sanitize_callback_param_returns_false() { 192 124 $actual = register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ) ); 193 125 194 126 remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb') ); 195 127 remove_filter( 'auth_post_meta_flight_number', '__return_true'); 196 128 197 $this->assertWPError( $actual ); 198 $this->assertEquals( 'register_meta_failed', $actual->get_error_code() ); 129 $this->assertFalse( $actual ); 199 130 } 200 131 201 132 public function test_register_meta_with_deprecated_sanitize_callback_parameter_passes_through_filter() { … … 210 141 211 142 public function test_register_meta_with_current_sanitize_callback_populates_wp_meta_keys() { 212 143 global $wp_meta_keys; 213 register_meta( 'post', 'flight_number', array( ' object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );144 register_meta( 'post', 'flight_number', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); 214 145 $actual = $wp_meta_keys; 215 unregister_meta_key( 'post', ' post', 'flight_number' );146 unregister_meta_key( 'post', 'flight_number' ); 216 147 217 148 $expected = array( 218 149 'post' => array( 219 'post' => array( 220 'flight_number' => array( 221 'object_subtype' => 'post', 222 'type' => 'string', 223 'description' => '', 224 'single' => false, 225 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ), 226 'auth_callback' => '__return_true', 227 'show_in_rest' => false, 228 ), 150 'flight_number' => array( 151 'type' => 'string', 152 'description' => '', 153 'single' => false, 154 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ), 155 'auth_callback' => '__return_true', 156 'show_in_rest' => false, 229 157 ), 230 158 ), 231 159 ); … … 233 161 } 234 162 235 163 public function test_register_meta_with_current_sanitize_callback_returns_true() { 236 $result = register_meta( 'post', 'flight_number', array( ' object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );237 unregister_meta_key( 'post', ' post', 'flight_number' );164 $result = register_meta( 'post', 'flight_number', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); 165 unregister_meta_key( 'post', 'flight_number' ); 238 166 239 167 $this->assertTrue( $result ); 240 168 } 241 169 242 170 public function test_register_meta_with_new_sanitize_callback_parameter() { 243 register_meta( 'post', 'new_sanitized_key', array( ' object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );244 $meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post' , 'post');171 register_meta( 'post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); 172 $meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post' ); 245 173 246 unregister_meta_key( 'post', ' post', 'new_sanitized_key' );174 unregister_meta_key( 'post', 'new_sanitized_key' ); 247 175 248 176 $this->assertEquals( 'new_sanitized_key new sanitized', $meta ); 249 177 } 250 178 251 179 public function test_register_meta_unregistered_meta_key_removes_sanitize_filter() { 252 register_meta( 'post', 'new_sanitized_key', array( ' object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );253 unregister_meta_key( 'post', ' post', 'new_sanitized_key' );180 register_meta( 'post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); 181 unregister_meta_key( 'post', 'new_sanitized_key' ); 254 182 255 $has_filter = has_filter( 'sanitize_post_ post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) );183 $has_filter = has_filter( 'sanitize_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) ); 256 184 257 185 $this->assertFalse( $has_filter ); 258 186 } 259 187 260 188 public function test_register_meta_unregistered_meta_key_removes_auth_filter() { 261 register_meta( 'post', 'new_auth_key', array( ' object_subtype' => 'post', 'auth_callback' => array( $this, '_new_auth_meta_cb' ) ) );262 unregister_meta_key( 'post', ' post', 'new_auth_key' );189 register_meta( 'post', 'new_auth_key', array( 'auth_callback' => array( $this, '_new_auth_meta_cb' ) ) ); 190 unregister_meta_key( 'post', 'new_auth_key' ); 263 191 264 $has_filter = has_filter( 'auth_post_ post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) );192 $has_filter = has_filter( 'auth_post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) ); 265 193 266 194 $this->assertFalse( $has_filter ); 267 195 } … … 268 196 269 197 public function test_unregister_meta_key_clears_key_from_wp_meta_keys() { 270 198 global $wp_meta_keys; 271 register_meta( 'post', 'registered_key', array( 'object_subtype' => 'post') );272 unregister_meta_key( 'post', ' post', 'registered_key' );199 register_meta( 'post', 'registered_key', array() ); 200 unregister_meta_key( 'post', 'registered_key' ); 273 201 274 202 $this->assertEquals( array(), $wp_meta_keys ); 275 203 } 276 204 277 public function test_unregister_meta_key_with_invalid_key_returns_ wp_error() {278 $this->assert WPError( unregister_meta_key( 'post','post', 'not_a_registered_key' ) );205 public function test_unregister_meta_key_with_invalid_key_returns_false() { 206 $this->assertFalse( unregister_meta_key( 'post', 'not_a_registered_key' ) ); 279 207 } 280 208 281 209 public function test_get_registered_meta_keys() { 282 register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post') );283 register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post') );210 register_meta( 'post', 'registered_key1', array() ); 211 register_meta( 'post', 'registered_key2', array() ); 284 212 285 $meta_keys = get_registered_meta_keys( 'post' , 'post');213 $meta_keys = get_registered_meta_keys( 'post' ); 286 214 287 unregister_meta_key( 'post', ' post', 'registered_key1' );288 unregister_meta_key( 'post', ' post', 'registered_key2' );215 unregister_meta_key( 'post', 'registered_key1' ); 216 unregister_meta_key( 'post', 'registered_key2' ); 289 217 290 218 $this->assertArrayHasKey( 'registered_key1', $meta_keys ); 291 219 $this->assertArrayHasKey( 'registered_key2', $meta_keys ); 292 220 } 293 221 294 public function test_get_registered_meta_keys_with_subtype_without_registered_keys_is_empty() {295 register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );296 register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );297 298 $meta_keys = get_registered_meta_keys( 'post', 'page' );299 300 unregister_meta_key( 'post', 'post', 'registered_key1' );301 unregister_meta_key( 'post', 'post', 'registered_key2' );302 303 $this->assertEmpty( $meta_keys );304 }305 306 222 public function test_get_registered_meta_keys_with_invalid_type_is_empty() { 307 register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post') );308 register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post') );223 register_meta( 'post', 'registered_key1', array() ); 224 register_meta( 'post', 'registered_key2', array() ); 309 225 310 226 $meta_keys = get_registered_meta_keys( 'invalid-type' ); 311 227 312 unregister_meta_key( 'post', ' post', 'registered_key1' );313 unregister_meta_key( 'post', ' post', 'registered_key2' );228 unregister_meta_key( 'post', 'registered_key1' ); 229 unregister_meta_key( 'post', 'registered_key2' ); 314 230 315 231 $this->assertEmpty( $meta_keys ); 316 232 } 317 233 318 public function test_get_registered_meta_keys_has_count() { 319 register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) ); 320 register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'page' ) ); 234 public function test_get_registered_meta_keys_description_arg() { 235 register_meta( 'post', 'registered_key1', array( 'description' => 'I\'m just a field, take a good look at me' ) ); 321 236 322 237 $meta_keys = get_registered_meta_keys( 'post' ); 323 238 324 unregister_meta_key( 'post', 'post', 'registered_key1' ); 325 unregister_meta_key( 'post', 'page', 'registered_key2' ); 239 unregister_meta_key( 'post', 'registered_key1' ); 326 240 327 $this->assertCount( 2, $meta_keys );328 }329 330 public function test_get_registered_meta_keys_description_arg() {331 register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'description' => 'I\'m just a field, take a good look at me' ) );332 333 $meta_keys = get_registered_meta_keys( 'post', 'post' );334 335 unregister_meta_key( 'post', 'post', 'registered_key1' );336 337 241 $this->assertEquals( 'I\'m just a field, take a good look at me', $meta_keys['registered_key1']['description'] ); 338 242 } 339 243 340 244 public function test_get_registered_meta_keys_invalid_arg() { 341 register_meta( 'post', 'registered_key1', array( ' object_subtype' => 'post', 'invalid_arg' => 'invalid' ) );245 register_meta( 'post', 'registered_key1', array( 'invalid_arg' => 'invalid' ) ); 342 246 343 $meta_keys = get_registered_meta_keys( 'post' , 'post');247 $meta_keys = get_registered_meta_keys( 'post' ); 344 248 345 unregister_meta_key( 'post', ' post', 'registered_key1' );249 unregister_meta_key( 'post', 'registered_key1' ); 346 250 347 251 $this->assertArrayNotHasKey( 'invalid_arg', $meta_keys['registered_key1'] ); 348 252 } 349 253 350 254 public function test_get_registered_metadata() { 351 register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post') );255 register_meta( 'post', 'flight_number', array() ); 352 256 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 353 257 354 $meta = get_registered_metadata( 'post', 'post',self::$post_id );258 $meta = get_registered_metadata( 'post', self::$post_id ); 355 259 356 unregister_meta_key( 'post', ' post', 'flight_number' );260 unregister_meta_key( 'post', 'flight_number' ); 357 261 358 262 $this->assertEquals( 'Oceanic 815', $meta['flight_number'][0] ); 359 263 } 360 264 361 265 public function test_get_registered_metadata_by_key() { 362 register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post') );266 register_meta( 'post', 'flight_number', array() ); 363 267 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 364 268 365 $meta = get_registered_metadata( 'post', 'post',self::$post_id, 'flight_number' );269 $meta = get_registered_metadata( 'post', self::$post_id, 'flight_number' ); 366 270 367 unregister_meta_key( 'post', ' post', 'flight_number' );271 unregister_meta_key( 'post', 'flight_number' ); 368 272 369 273 $this->assertEquals( 'Oceanic 815', $meta[0] ); 370 274 } 371 275 372 276 public function test_get_registered_metadata_by_key_single() { 373 register_meta( 'post', 'flight_number', array( ' object_subtype' => 'post', 'single' => true ) );277 register_meta( 'post', 'flight_number', array( 'single' => true ) ); 374 278 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 375 279 376 $meta = get_registered_metadata( 'post', 'post',self::$post_id, 'flight_number' );280 $meta = get_registered_metadata( 'post', self::$post_id, 'flight_number' ); 377 281 378 unregister_meta_key( 'post', ' post', 'flight_number' );282 unregister_meta_key( 'post', 'flight_number' ); 379 283 380 284 $this->assertEquals( 'Oceanic 815', $meta ); 381 285 } 382 286 383 287 public function test_get_registered_metadata_by_invalid_key() { 384 register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post') );288 register_meta( 'post', 'flight_number', array() ); 385 289 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 386 290 387 $meta = get_registered_metadata( 'post', 'post',self::$post_id, 'flight_pilot' );291 $meta = get_registered_metadata( 'post', self::$post_id, 'flight_pilot' ); 388 292 389 unregister_meta_key( 'post', ' post', 'flight_number' );293 unregister_meta_key( 'post', 'flight_number' ); 390 294 391 $this->assert WPError( $meta );295 $this->assertFalse( $meta ); 392 296 } 393 297 394 public function test_get_registered_metadata_invalid_object_type() { 395 register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) ); 396 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 298 public function test_get_registered_metadata_invalid_object_type_returns_empty_array() { 299 $meta = get_registered_metadata( 'invalid-type', self::$post_id ); 397 300 398 $meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id ); 399 400 unregister_meta_key( 'post', 'post', 'flight_number' ); 401 402 $this->assertWPError( $meta ); 301 $this->assertEmpty( $meta ); 403 302 } 404 405 public function test_get_registered_metadata_invalid() {406 $meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id );407 408 $this->assertWPError( $meta );409 }410 303 }