Ticket #35658: 35658.45.diff
File 35658.45.diff, 27.6 KB (added by , 8 years ago) |
---|
-
src/wp-includes/functions.php
5366 5366 * @return bool True if the object type exists, false if not. 5367 5367 */ 5368 5368 function wp_object_type_exists( $object_type ) { 5369 $object_type = explode( ':', $object_type ); 5370 $object_type = $object_type[0]; 5371 5369 5372 /** 5370 5373 * Filters WordPress object types. 5371 5374 * -
src/wp-includes/meta.php
945 945 * 946 946 * @return mixed Sanitized $meta_value. 947 947 */ 948 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { 949 if ( ! empty( $object_subtype ) ) { 948 function sanitize_meta( $meta_key, $meta_value, $object_type ) { 949 if ( registered_meta_key_exists( $object_type, $meta_key ) ) { 950 $object_type = explode( ':', $object_type ); 951 952 if ( isset( $object_type[1] ) ) { 953 $object_subtype = $object_type[1]; 954 } else { 955 $object_subtype = ''; 956 } 957 958 $object_type = $object_type[0]; 959 950 960 /** 951 961 * Filters the sanitization of a specific meta key of a specific meta type and subtype. 952 962 * … … 972 982 * key value, respectively. 973 983 * 974 984 * @since 3.3.0 975 * @since 4.6.0 Added the `$object_subtype` parameter.976 985 * 977 986 * @param mixed $meta_value Meta value to sanitize. 978 987 * @param string $meta_key Meta key. 979 988 * @param string $object_type Object type. 980 * @param string $object_subtype Object subtype.981 989 */ 982 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type , $object_subtype);990 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type ); 983 991 } 984 992 985 993 /** … … 995 1003 * @param array $args { 996 1004 * Data used to describe the meta key when registered. 997 1005 * 998 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type.999 1006 * @type string $type The type of data associated with this meta key. 1000 1007 * @type string $description A description of the data attached to this meta key. 1001 1008 * @type bool $single Whether the meta key has one value per object, or an array of values per object. … … 1009 1016 * Registering a meta key with distinct sanitize and auth callbacks will fire those 1010 1017 * callbacks, but will not add to the global registry as it requires a subtype. 1011 1018 */ 1012 function register_meta( $object_type, $meta_key, $args , $deprecated = null ) {1019 function register_meta( $object_type, $meta_key, $args = array(), $deprecated = null ) { 1013 1020 global $wp_meta_keys; 1014 1021 1015 1022 if ( ! is_array( $wp_meta_keys ) ) { … … 1021 1028 } 1022 1029 1023 1030 $defaults = array( 1024 'object_subtype' => '',1025 1031 'type' => 'string', 1026 1032 'description' => '', 1027 1033 'single' => false, … … 1051 1057 1052 1058 $args = wp_parse_args( $args, $defaults ); 1053 1059 1060 if ( false === strpos( $object_type, ':' ) && ! empty( $args['object_subtype'] ) ) { 1061 $object_type .= ':' . $args['object_subtype']; 1062 unset( $args['object_subtype'] ); 1063 } 1064 1054 1065 /** 1055 1066 * Filters the registration arguments when registering meta. 1056 1067 * … … 1063 1074 */ 1064 1075 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1065 1076 1077 $object_type = explode( ':', $object_type ); 1078 1079 if ( 2 < count( $object_type ) ) { 1080 // invalid object type. 1081 } 1082 1083 if ( isset( $object_type[1] ) ) { 1084 $object_subtype = $object_type[1]; 1085 } else { 1086 $object_subtype = ''; 1087 } 1088 1089 $object_type = $object_type[0]; 1090 1066 1091 // Object subtype is required if using the args style of registration 1067 if ( ! $has_old_sanitize_cb && ! $has_old_auth_cb && empty( $ args['object_subtype']) ) {1092 if ( ! $has_old_sanitize_cb && ! $has_old_auth_cb && empty( $object_subtype ) ) { 1068 1093 return new WP_Error( 'register_meta_failed', __( 'Meta must be registered against an object subtype.' ) ); 1069 1094 } 1070 1095 … … 1077 1102 } 1078 1103 } 1079 1104 1080 $object_subtype = '';1081 1082 if ( ! empty( $args['object_subtype'] ) ) {1083 $object_subtype = $args['object_subtype'];1084 }1085 1086 1105 // Back-compat: old sanitize and auth callbacks are applied to all of an object type. 1087 1106 if ( $has_old_sanitize_cb && is_callable( $args['sanitize_callback'] ) ) { 1088 1107 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 ); … … 1104 1123 1105 1124 // Global registry only contains meta keys registered in the new way with a subtype. 1106 1125 if ( ! empty( $object_subtype ) ) { 1107 $wp_meta_keys[ $object_type ][$object_subtype ][ $meta_key ] = $args;1126 $wp_meta_keys[ $object_type . ':' . $object_subtype ][ $meta_key ] = $args; 1108 1127 1109 1128 return true; 1110 1129 } … … 1118 1137 * @since 4.6.0 1119 1138 * 1120 1139 * @param string $object_type The type of object. 1121 * @param string $object_subtype The subtype of the object type.1122 1140 * @param string $meta_key The meta key. 1123 1141 * 1124 1142 * @return bool|WP_error True if the meta key is registered to the object type and subtype. False if not. 1125 1143 * WP_Error if an invalid object type is passed. 1126 1144 */ 1127 function registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) {1145 function registered_meta_key_exists( $object_type, $meta_key ) { 1128 1146 global $wp_meta_keys; 1129 1147 1130 1148 if ( ! is_array( $wp_meta_keys ) ) { … … 1140 1158 return false; 1141 1159 } 1142 1160 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 ] ) ) { 1161 if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) { 1148 1162 return true; 1149 1163 } 1150 1164 … … 1157 1171 * @since 4.6.0 1158 1172 * 1159 1173 * @param string $object_type The type of object. 1160 * @param string $object_subtype The subtype of the object type.1161 1174 * @param string $meta_key The meta key. 1162 1175 * 1163 1176 * @return bool|WP_Error True if successful. WP_Error if the meta key is invalid. 1164 1177 */ 1165 function unregister_meta_key( $object_type, $ object_subtype, $meta_key ) {1178 function unregister_meta_key( $object_type, $meta_key ) { 1166 1179 global $wp_meta_keys; 1167 1180 1168 if ( ! registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) ) {1181 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1169 1182 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) ); 1170 1183 } 1171 1184 1172 $args = $wp_meta_keys[ $object_type ][ $ object_subtype ][ $meta_key ];1185 $args = $wp_meta_keys[ $object_type ][ $meta_key ]; 1173 1186 1187 $object_type = explode( ':', $object_type ); 1188 1189 if ( isset( $object_type[1] ) ) { 1190 $object_subtype = $object_type[1]; 1191 } else { 1192 $object_subtype = ''; 1193 } 1194 1195 $object_type = $object_type[0]; 1196 1174 1197 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 1175 1198 remove_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'] ); 1176 1199 } … … 1179 1202 remove_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'] ); 1180 1203 } 1181 1204 1182 unset( $wp_meta_keys[ $object_type ][$object_subtype ][ $meta_key ] );1205 unset( $wp_meta_keys[ $object_type . ':' . $object_subtype ][ $meta_key ] ); 1183 1206 1184 1207 // Do some clean up 1185 if ( empty( $wp_meta_keys[ $object_type ][$object_subtype ] ) ) {1186 unset( $wp_meta_keys[ $object_type ][$object_subtype ] );1208 if ( empty( $wp_meta_keys[ $object_type . ':' . $object_subtype ] ) ) { 1209 unset( $wp_meta_keys[ $object_type . ':' . $object_subtype ] ); 1187 1210 } 1188 1211 1189 if ( empty( $wp_meta_keys[ $object_type ] ) ) {1190 unset( $wp_meta_keys[ $object_type ] );1191 }1192 1193 1212 return true; 1194 1213 } 1195 1214 … … 1198 1217 * 1199 1218 * @since 4.6.0 1200 1219 * 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). 1220 * @param string $object_type The type of object. Post, comment, user, term. 1203 1221 * 1204 1222 * @return array List of registered meta keys. 1205 1223 */ 1206 function get_registered_meta_keys( $object_type , $object_subtype = '') {1224 function get_registered_meta_keys( $object_type ) { 1207 1225 global $wp_meta_keys; 1208 1226 1209 1227 if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { 1210 1228 return array(); 1211 1229 } 1212 1230 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 ]; 1231 return $wp_meta_keys[ $object_type ]; 1222 1232 } 1223 1233 1224 1234 /** … … 1227 1237 * @since 4.6.0 1228 1238 * 1229 1239 * @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 1240 * @param int $object_id ID of the object the metadata is for. 1232 1241 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered 1233 1242 * metadata for the specified object. 1234 1243 * 1235 1244 * @return mixed|WP_Error 1236 1245 */ 1237 function get_registered_metadata( $object_type, $object_ subtype, $object_id, $meta_key = '' ) {1246 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { 1238 1247 global $wp_meta_keys; 1239 1248 1240 1249 if ( ! is_array( $wp_meta_keys ) ) { … … 1245 1254 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) ); 1246 1255 } 1247 1256 1257 $base_object_type = explode( ':', $object_type ); 1258 $base_object_type = $base_object_type[0]; 1259 1248 1260 if ( ! empty( $meta_key ) ) { 1249 if ( ! registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) ) {1261 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1250 1262 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); 1251 1263 } 1252 $meta_keys = get_registered_meta_keys( $object_type , $object_subtype);1264 $meta_keys = get_registered_meta_keys( $object_type ); 1253 1265 $meta_key_data = $meta_keys[ $meta_key ]; 1254 1266 1255 $data = get_metadata( $ object_type, $object_id, $meta_key, $meta_key_data['single'] );1267 $data = get_metadata( $base_object_type, $object_id, $meta_key, $meta_key_data['single'] ); 1256 1268 1257 1269 return $data; 1258 1270 } 1259 1271 1260 $data = get_metadata( $ object_type, $object_id );1272 $data = get_metadata( $base_object_type, $object_id ); 1261 1273 1262 $meta_keys = get_registered_meta_keys( $object_type , $object_subtype);1274 $meta_keys = get_registered_meta_keys( $object_type ); 1263 1275 $registered_data = array(); 1264 1276 1265 1277 // Someday, array_filter() -
tests/phpunit/tests/meta/registerMeta.php
69 69 } 70 70 71 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' );72 $result = register_meta( 'post:post', 'flight_number' ); 73 unregister_meta_key( 'post:post', 'flight_number' ); 74 74 75 75 $this->assertTrue( $result ); 76 76 } … … 78 78 public function test_register_meta_with_post_object_type_and_subtype_populates_wp_meta_keys() { 79 79 global $wp_meta_keys; 80 80 81 register_meta( 'post ', 'flight_number', array( 'object_subtype' => 'post' ));81 register_meta( 'post:post', 'flight_number' ); 82 82 $actual = $wp_meta_keys; 83 unregister_meta_key( 'post ', 'post', 'flight_number' );83 unregister_meta_key( 'post:post', 'flight_number' ); 84 84 85 85 $expected = array( 86 '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 ), 86 'post:post' => array( 87 'flight_number' => array( 88 'type' => 'string', 89 'description' => '', 90 'single' => false, 91 'sanitize_callback' => null, 92 'auth_callback' => '__return_true', 93 'show_in_rest' => false, 97 94 ), 98 95 ), 99 96 ); … … 104 101 public function test_register_meta_with_post_object_type_and_unregistered_subtype_populates_wp_meta_keys() { 105 102 global $wp_meta_keys; 106 103 107 register_meta( 'post ', 'flight_number', array( 'object_subtype' => 'not_a_post_type' ));104 register_meta( 'post:not_a_post_type', 'flight_number' ); 108 105 $actual = $wp_meta_keys; 109 unregister_meta_key( 'post ', 'not_a_post_type', 'flight_number' );106 unregister_meta_key( 'post:not_a_post_type', 'flight_number' ); 110 107 111 108 $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 ), 109 'post:not_a_post_type' => array( 110 'flight_number' => array( 111 'type' => 'string', 112 'description' => '', 113 'single' => false, 114 'sanitize_callback' => null, 115 'auth_callback' => '__return_true', 116 'show_in_rest' => false, 123 117 ), 124 118 ), 125 119 ); … … 129 123 130 124 public function test_register_meta_with_term_object_type_and_category_subtype_populates_wp_meta_keys() { 131 125 global $wp_meta_keys; 132 register_meta( 'term ', 'category_icon', array( 'object_subtype' => 'category' ));126 register_meta( 'term:category', 'category_icon' ); 133 127 $actual = $wp_meta_keys; 134 unregister_meta_key( 'term ', 'category', 'category_icon' );128 unregister_meta_key( 'term:category', 'category_icon' ); 135 129 136 130 $expected = array( 137 '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 ), 131 'term:category' => array( 132 'category_icon' => array( 133 'type' => 'string', 134 'description' => '', 135 'single' => false, 136 'sanitize_callback' => null, 137 'auth_callback' => '__return_true', 138 'show_in_rest' => false, 148 139 ), 149 140 ), 150 141 ); … … 154 145 155 146 public function test_register_meta_with_comment_object_type_and_subtype_populates_wp_meta_keys() { 156 147 global $wp_meta_keys; 157 register_meta( 'comment ', 'comment_rating', array( 'object_subtype' => 'comment' ));148 register_meta( 'comment:comment', 'comment_rating' ); 158 149 $actual = $wp_meta_keys; 159 unregister_meta_key( 'comment ', 'comment', 'comment_rating' );150 unregister_meta_key( 'comment:comment', 'comment_rating' ); 160 151 161 152 $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 ), 153 'comment:comment' => array( 154 'comment_rating' => array( 155 'type' => 'string', 156 'description' => '', 157 'single' => false, 158 'sanitize_callback' => null, 159 'auth_callback' => '__return_true', 160 'show_in_rest' => false, 173 161 ), 174 162 ), 175 163 ); … … 200 188 201 189 public function test_register_meta_with_deprecated_sanitize_callback_parameter_passes_through_filter() { 202 190 register_meta( 'post', 'old_sanitized_key', array( $this, '_old_sanitize_meta_cb' ) ); 203 $meta = sanitize_meta( 'old_sanitized_key', 'unsanitized', 'post' , 'post');191 $meta = sanitize_meta( 'old_sanitized_key', 'unsanitized', 'post' ); 204 192 205 193 remove_filter( 'sanitize_post_meta_flight_number', array( $this, '_old_sanitize_meta_cb') ); 206 194 remove_filter( 'auth_post_meta_flight_number', '__return_true'); … … 210 198 211 199 public function test_register_meta_with_current_sanitize_callback_populates_wp_meta_keys() { 212 200 global $wp_meta_keys; 213 register_meta( 'post ', 'flight_number', array( 'object_subtype' => 'post','sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );201 register_meta( 'post:post', 'flight_number', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); 214 202 $actual = $wp_meta_keys; 215 unregister_meta_key( 'post ', 'post', 'flight_number' );203 unregister_meta_key( 'post:post', 'flight_number' ); 216 204 217 205 $expected = array( 218 '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 ), 206 'post:post' => array( 207 'flight_number' => array( 208 'type' => 'string', 209 'description' => '', 210 'single' => false, 211 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ), 212 'auth_callback' => '__return_true', 213 'show_in_rest' => false, 229 214 ), 230 215 ), 231 216 ); … … 233 218 } 234 219 235 220 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' );221 $result = register_meta( 'post:post', 'flight_number', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); 222 unregister_meta_key( 'post:post', 'flight_number' ); 238 223 239 224 $this->assertTrue( $result ); 240 225 } 241 226 242 227 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' );228 register_meta( 'post:post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); 229 $meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post:post' ); 245 230 246 unregister_meta_key( 'post ', 'post', 'new_sanitized_key' );231 unregister_meta_key( 'post:post', 'new_sanitized_key' ); 247 232 248 233 $this->assertEquals( 'new_sanitized_key new sanitized', $meta ); 249 234 } 250 235 251 236 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' );237 register_meta( 'post:post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); 238 unregister_meta_key( 'post:post', 'new_sanitized_key' ); 254 239 255 240 $has_filter = has_filter( 'sanitize_post_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) ); 256 241 … … 258 243 } 259 244 260 245 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' );246 register_meta( 'post:post', 'new_auth_key', array( 'auth_callback' => array( $this, '_new_auth_meta_cb' ) ) ); 247 unregister_meta_key( 'post:post', 'new_auth_key' ); 263 248 264 249 $has_filter = has_filter( 'auth_post_post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) ); 265 250 … … 268 253 269 254 public function test_unregister_meta_key_clears_key_from_wp_meta_keys() { 270 255 global $wp_meta_keys; 271 register_meta( 'post ', 'registered_key', array( 'object_subtype' => 'post' ));272 unregister_meta_key( 'post ', 'post', 'registered_key' );256 register_meta( 'post:post', 'registered_key' ); 257 unregister_meta_key( 'post:post', 'registered_key' ); 273 258 274 259 $this->assertEquals( array(), $wp_meta_keys ); 275 260 } 276 261 277 262 public function test_unregister_meta_key_with_invalid_key_returns_wp_error() { 278 $this->assertWPError( unregister_meta_key( 'post ', 'post', 'not_a_registered_key' ) );263 $this->assertWPError( unregister_meta_key( 'post:post', 'not_a_registered_key' ) ); 279 264 } 280 265 281 266 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' ));267 register_meta( 'post:post', 'registered_key1' ); 268 register_meta( 'post:post', 'registered_key2' ); 284 269 285 $meta_keys = get_registered_meta_keys( 'post ', 'post' );270 $meta_keys = get_registered_meta_keys( 'post:post' ); 286 271 287 unregister_meta_key( 'post ', 'post', 'registered_key1' );288 unregister_meta_key( 'post ', 'post', 'registered_key2' );272 unregister_meta_key( 'post:post', 'registered_key1' ); 273 unregister_meta_key( 'post:post', 'registered_key2' ); 289 274 290 275 $this->assertArrayHasKey( 'registered_key1', $meta_keys ); 291 276 $this->assertArrayHasKey( 'registered_key2', $meta_keys ); 292 277 } 293 278 294 279 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' ));280 register_meta( 'post:post', 'registered_key1' ); 281 register_meta( 'post:post', 'registered_key2' ); 297 282 298 $meta_keys = get_registered_meta_keys( 'post ', 'page' );283 $meta_keys = get_registered_meta_keys( 'post:page' ); 299 284 300 unregister_meta_key( 'post ', 'post', 'registered_key1' );301 unregister_meta_key( 'post ', 'post', 'registered_key2' );285 unregister_meta_key( 'post:post', 'registered_key1' ); 286 unregister_meta_key( 'post:post', 'registered_key2' ); 302 287 303 288 $this->assertEmpty( $meta_keys ); 304 289 } 305 290 306 291 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' ));292 register_meta( 'post:post', 'registered_key1' ); 293 register_meta( 'post:post', 'registered_key2' ); 309 294 310 295 $meta_keys = get_registered_meta_keys( 'invalid-type' ); 311 296 312 unregister_meta_key( 'post ', 'post', 'registered_key1' );313 unregister_meta_key( 'post ', 'post', 'registered_key2' );297 unregister_meta_key( 'post:post', 'registered_key1' ); 298 unregister_meta_key( 'post:post', 'registered_key2' ); 314 299 315 300 $this->assertEmpty( $meta_keys ); 316 301 } 317 302 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' ));303 public function test_get_registered_meta_keys_with_only_base_type_has_0_count() { 304 register_meta( 'post:post', 'registered_key1' ); 305 register_meta( 'post:page', 'registered_key2' ); 321 306 322 307 $meta_keys = get_registered_meta_keys( 'post' ); 323 308 324 unregister_meta_key( 'post ', 'post', 'registered_key1' );325 unregister_meta_key( 'post ', 'page', 'registered_key2' );309 unregister_meta_key( 'post:post', 'registered_key1' ); 310 unregister_meta_key( 'post:page', 'registered_key2' ); 326 311 327 $this->assertCount( 2, $meta_keys );312 $this->assertCount( 0, $meta_keys ); 328 313 } 329 314 330 315 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' ) );316 register_meta( 'post:post', 'registered_key1', array( 'description' => 'I\'m just a field, take a good look at me' ) ); 332 317 333 $meta_keys = get_registered_meta_keys( 'post ', 'post' );318 $meta_keys = get_registered_meta_keys( 'post:post' ); 334 319 335 unregister_meta_key( 'post ', 'post', 'registered_key1' );320 unregister_meta_key( 'post:post', 'registered_key1' ); 336 321 337 322 $this->assertEquals( 'I\'m just a field, take a good look at me', $meta_keys['registered_key1']['description'] ); 338 323 } 339 324 340 325 public function test_get_registered_meta_keys_invalid_arg() { 341 register_meta( 'post ', 'registered_key1', array( 'object_subtype' => 'post','invalid_arg' => 'invalid' ) );326 register_meta( 'post:post', 'registered_key1', array( 'invalid_arg' => 'invalid' ) ); 342 327 343 $meta_keys = get_registered_meta_keys( 'post ', 'post' );328 $meta_keys = get_registered_meta_keys( 'post:post' ); 344 329 345 unregister_meta_key( 'post ', 'post', 'registered_key1' );330 unregister_meta_key( 'post:post', 'registered_key1' ); 346 331 347 332 $this->assertArrayNotHasKey( 'invalid_arg', $meta_keys['registered_key1'] ); 348 333 } 349 334 350 335 public function test_get_registered_metadata() { 351 register_meta( 'post ', 'flight_number', array( 'object_subtype' => 'post' ));336 register_meta( 'post:post', 'flight_number' ); 352 337 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 353 338 354 $meta = get_registered_metadata( 'post ', 'post', self::$post_id );339 $meta = get_registered_metadata( 'post:post', self::$post_id ); 355 340 356 unregister_meta_key( 'post ', 'post', 'flight_number' );341 unregister_meta_key( 'post:post', 'flight_number' ); 357 342 358 343 $this->assertEquals( 'Oceanic 815', $meta['flight_number'][0] ); 359 344 } 360 345 361 346 public function test_get_registered_metadata_by_key() { 362 register_meta( 'post ', 'flight_number', array( 'object_subtype' => 'post' ));347 register_meta( 'post:post', 'flight_number' ); 363 348 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 364 349 365 $meta = get_registered_metadata( 'post ', 'post', self::$post_id, 'flight_number' );350 $meta = get_registered_metadata( 'post:post', self::$post_id, 'flight_number' ); 366 351 367 unregister_meta_key( 'post ', 'post', 'flight_number' );352 unregister_meta_key( 'post:post', 'flight_number' ); 368 353 369 354 $this->assertEquals( 'Oceanic 815', $meta[0] ); 370 355 } 371 356 372 357 public function test_get_registered_metadata_by_key_single() { 373 register_meta( 'post ', 'flight_number', array( 'object_subtype' => 'post','single' => true ) );358 register_meta( 'post:post', 'flight_number', array( 'single' => true ) ); 374 359 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 375 360 376 $meta = get_registered_metadata( 'post ', 'post', self::$post_id, 'flight_number' );361 $meta = get_registered_metadata( 'post:post', self::$post_id, 'flight_number' ); 377 362 378 unregister_meta_key( 'post ', 'post', 'flight_number' );363 unregister_meta_key( 'post:post', 'flight_number' ); 379 364 380 365 $this->assertEquals( 'Oceanic 815', $meta ); 381 366 } 382 367 383 368 public function test_get_registered_metadata_by_invalid_key() { 384 register_meta( 'post ', 'flight_number', array( 'object_subtype' => 'post' ));369 register_meta( 'post:post', 'flight_number' ); 385 370 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 386 371 387 $meta = get_registered_metadata( 'post ', 'post', self::$post_id, 'flight_pilot' );372 $meta = get_registered_metadata( 'post:post', self::$post_id, 'flight_pilot' ); 388 373 389 unregister_meta_key( 'post ', 'post', 'flight_number' );374 unregister_meta_key( 'post:post', 'flight_number' ); 390 375 391 376 $this->assertWPError( $meta ); 392 377 } 393 378 394 379 public function test_get_registered_metadata_invalid_object_type() { 395 register_meta( 'post ', 'flight_number', array( 'object_subtype' => 'post' ));380 register_meta( 'post:post', 'flight_number' ); 396 381 add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); 397 382 398 $meta = get_registered_metadata( 'invalid-type ', 'invalid-subtype', self::$post_id );383 $meta = get_registered_metadata( 'invalid-type:invalid-subtype', self::$post_id ); 399 384 400 unregister_meta_key( 'post ', 'post', 'flight_number' );385 unregister_meta_key( 'post:post', 'flight_number' ); 401 386 402 387 $this->assertWPError( $meta ); 403 388 } 404 389 405 390 public function test_get_registered_metadata_invalid() { 406 $meta = get_registered_metadata( 'invalid-type ', 'invalid-subtype', self::$post_id );391 $meta = get_registered_metadata( 'invalid-type:invalid-subtype', self::$post_id ); 407 392 408 393 $this->assertWPError( $meta ); 409 394 }