Ticket #35658: 35658.46.diff
File 35658.46.diff, 34.4 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
44 44 return false; 45 45 } 46 46 47 $column = sanitize_key($meta_type . '_id'); 47 $meta_type_column = explode( ':', $meta_type ); 48 $column = sanitize_key( $meta_type_column[0] . '_id' ); 48 49 49 50 // expected_slashed ($meta_key) 50 51 $meta_key = wp_unslash($meta_key); … … 157 158 return false; 158 159 } 159 160 160 $column = sanitize_key($meta_type . '_id'); 161 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 161 $meta_type_column = explode( ':', $meta_type ); 162 $column = sanitize_key( $meta_type_column[0] . '_id' ); 163 $id_column = 'user' == $meta_type_column[0] ? 'umeta_id' : 'meta_id'; 162 164 163 165 // expected_slashed ($meta_key) 164 166 $raw_meta_key = $meta_key; … … 321 323 return false; 322 324 } 323 325 324 $type_column = sanitize_key($meta_type . '_id'); 325 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 326 $meta_type_column = explode( ':', $meta_type ); 327 $type_column = sanitize_key( $meta_type_column[0] . '_id' ); 328 $id_column = 'user' == $meta_type_column[0] ? 'umeta_id' : 'meta_id'; 326 329 // expected_slashed ($meta_key) 327 330 $meta_key = wp_unslash($meta_key); 328 331 $meta_value = wp_unslash($meta_value); … … 576 579 return false; 577 580 } 578 581 579 $table = _get_meta_table( $meta_type ); 582 $meta_type_column = explode( ':', $meta_type ); 583 $table = _get_meta_table( $meta_type_column[0] ); 580 584 if ( ! $table ) { 581 585 return false; 582 586 } 583 587 584 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';588 $id_column = ( 'user' == $meta_type_column[0] ) ? 'umeta_id' : 'meta_id'; 585 589 586 590 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); 587 591 … … 620 624 return false; 621 625 } 622 626 623 $table = _get_meta_table( $meta_type ); 627 $meta_type_column = explode( ':', $meta_type ); 628 $table = _get_meta_table( $meta_type_column[0] ); 624 629 if ( ! $table ) { 625 630 return false; 626 631 } 627 632 628 $column = sanitize_key( $meta_type . '_id');629 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';633 $column = sanitize_key( $meta_type_column[0] . '_id' ); 634 $id_column = 'user' == $meta_type_column[0] ? 'umeta_id' : 'meta_id'; 630 635 631 636 // Fetch the meta and go on if it's found. 632 637 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { … … 711 716 return false; 712 717 } 713 718 714 $table = _get_meta_table( $meta_type ); 719 $meta_type_column = explode( ':', $meta_type ); 720 $table = _get_meta_table( $meta_type_column[0] ); 715 721 if ( ! $table ) { 716 722 return false; 717 723 } 718 724 719 725 // object and id columns 720 $column = sanitize_key( $meta_type. '_id');721 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';726 $column = sanitize_key( $meta_type_column[0] . '_id'); 727 $id_column = 'user' == $meta_type_column[0] ? 'umeta_id' : 'meta_id'; 722 728 723 729 // Fetch the meta and go on if it's found. 724 730 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { … … 792 798 return false; 793 799 } 794 800 795 $table = _get_meta_table( $meta_type ); 801 $meta_type_column = explode( ':', $meta_type ); 802 $table = _get_meta_table( $meta_type_column[0] ); 796 803 if ( ! $table ) { 797 804 return false; 798 805 } 799 806 800 $column = sanitize_key( $meta_type . '_id');807 $column = sanitize_key( $meta_type_column[0] . '_id' ); 801 808 802 809 if ( !is_array($object_ids) ) { 803 810 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids); … … 806 813 807 814 $object_ids = array_map('intval', $object_ids); 808 815 809 $cache_key = $meta_type . '_meta';816 $cache_key = $meta_type_column[0] . '_meta'; 810 817 $ids = array(); 811 818 $cache = array(); 812 819 foreach ( $object_ids as $id ) { … … 822 829 823 830 // Get meta info 824 831 $id_list = join( ',', $ids ); 825 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';832 $id_column = 'user' == $meta_type_column[0] ? 'umeta_id' : 'meta_id'; 826 833 $meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A ); 827 834 828 835 if ( !empty($meta_list) ) { … … 883 890 * @return array Associative array of `JOIN` and `WHERE` SQL. 884 891 */ 885 892 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { 893 $type = explode( ':', $type ); 886 894 $meta_query_obj = new WP_Meta_Query( $meta_query ); 887 return $meta_query_obj->get_sql( $type , $primary_table, $primary_id_column, $context );895 return $meta_query_obj->get_sql( $type[0], $primary_table, $primary_id_column, $context ); 888 896 } 889 897 890 898 /** … … 900 908 function _get_meta_table($type) { 901 909 global $wpdb; 902 910 903 $table_name = $type . 'meta'; 911 $type = explode( ':', $type ); 912 $table_name = $type[0] . 'meta'; 904 913 905 914 if ( empty($wpdb->$table_name) ) 906 915 return false; … … 936 945 * Sanitize meta value. 937 946 * 938 947 * @since 3.1.3 939 * @since 4.6.0 Added the `$object_subtype` parameter.940 948 * 941 949 * @param string $meta_key Meta key. 942 950 * @param mixed $meta_value Meta value to sanitize. 943 951 * @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 952 * 946 953 * @return mixed Sanitized $meta_value. 947 954 */ 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 955 function sanitize_meta( $meta_key, $meta_value, $object_type ) { 967 956 /** 968 957 * Filters the sanitization of a specific meta key of a specific meta type. 969 958 * … … 972 961 * key value, respectively. 973 962 * 974 963 * @since 3.3.0 975 * @since 4.6.0 Added the `$object_subtype` parameter.976 964 * 977 965 * @param mixed $meta_value Meta value to sanitize. 978 966 * @param string $meta_key Meta key. 979 967 * @param string $object_type Object type. 980 * @param string $object_subtype Object subtype.981 968 */ 982 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type , $object_subtype);969 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type ); 983 970 } 984 971 985 972 /** … … 995 982 * @param array $args { 996 983 * Data used to describe the meta key when registered. 997 984 * 998 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type.999 985 * @type string $type The type of data associated with this meta key. 1000 986 * @type string $description A description of the data attached to this meta key. 1001 987 * @type bool $single Whether the meta key has one value per object, or an array of values per object. … … 1009 995 * Registering a meta key with distinct sanitize and auth callbacks will fire those 1010 996 * callbacks, but will not add to the global registry as it requires a subtype. 1011 997 */ 1012 function register_meta( $object_type, $meta_key, $args , $deprecated = null ) {998 function register_meta( $object_type, $meta_key, $args = array(), $deprecated = null ) { 1013 999 global $wp_meta_keys; 1014 1000 1015 1001 if ( ! is_array( $wp_meta_keys ) ) { … … 1021 1007 } 1022 1008 1023 1009 $defaults = array( 1024 'object_subtype' => '',1025 1010 'type' => 'string', 1026 1011 'description' => '', 1027 1012 'single' => false, … … 1063 1048 */ 1064 1049 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1065 1050 1066 // 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'] ) ) { 1068 return new WP_Error( 'register_meta_failed', __( 'Meta must be registered against an object subtype.' ) ); 1051 // Check for a valid (WP) CURIE string. 1052 $object_type = trim( $object_type, ':' ); 1053 1054 // New object type style cannot be used with old function signature. 1055 if ( strpos( $object_type, ':' ) && ( $has_old_auth_cb || $has_old_sanitize_cb ) ) { 1056 // error. 1057 return false; 1069 1058 } 1070 1059 1071 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. 1060 /** 1061 * If `auth_callback` is not provided, fall back to `is_protected_meta()`. 1062 * 1063 * To maintain back-compat, this should be processed with only the base object 1064 * type and not CURIE syntax. 1065 */ 1072 1066 if ( empty( $args['auth_callback'] ) ) { 1073 if ( is_protected_meta( $meta_key, $object_type ) ) { 1067 $object_type_protected = explode( ':', $object_type ); 1068 if ( is_protected_meta( $meta_key, $object_type_protected[0] ) ) { 1074 1069 $args['auth_callback'] = '__return_false'; 1075 1070 } else { 1076 1071 $args['auth_callback'] = '__return_true'; … … 1077 1072 } 1078 1073 } 1079 1074 1080 $object_subtype = ''; 1081 1082 if ( ! empty( $args['object_subtype'] ) ) { 1083 $object_subtype = $args['object_subtype']; 1075 // Provide a fallback for new object type styles if using new function signature. 1076 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb && ! strpos( $object_type, ':' ) ) { 1077 if ( 'term' === $object_type ) { 1078 $object_type = 'term:category'; 1079 } else { 1080 $object_type = $object_type . ':' . $object_type; 1081 } 1084 1082 } 1085 1083 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'] ) ) { 1088 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 ); 1084 if ( is_callable( $args['sanitize_callback'] ) ) { 1085 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); 1089 1086 } 1090 1087 1091 if ( $has_old_auth_cb &&is_callable( $args['auth_callback'] ) ) {1088 if ( is_callable( $args['auth_callback'] ) ) { 1092 1089 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1093 1090 } 1094 1091 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 }1099 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 1092 // 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;1093 if ( strpos( $object_type, ':' ) ) { 1094 $wp_meta_keys[ $object_type ][ $meta_key ] = $args; 1108 1095 1109 1096 return true; 1110 1097 } … … 1118 1105 * @since 4.6.0 1119 1106 * 1120 1107 * @param string $object_type The type of object. 1121 * @param string $object_subtype The subtype of the object type.1122 1108 * @param string $meta_key The meta key. 1123 1109 * 1124 * @return bool|WP_error True if the meta key is registered to the object type and subtype. False if not.1110 * @return bool|WP_error True if the meta key is registered to the object type. False if not. 1125 1111 * WP_Error if an invalid object type is passed. 1126 1112 */ 1127 function registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) {1113 function registered_meta_key_exists( $object_type, $meta_key ) { 1128 1114 global $wp_meta_keys; 1129 1115 1130 1116 if ( ! is_array( $wp_meta_keys ) ) { … … 1140 1126 return false; 1141 1127 } 1142 1128 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 ] ) ) { 1129 if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) { 1148 1130 return true; 1149 1131 } 1150 1132 … … 1157 1139 * @since 4.6.0 1158 1140 * 1159 1141 * @param string $object_type The type of object. 1160 * @param string $object_subtype The subtype of the object type.1161 1142 * @param string $meta_key The meta key. 1162 1143 * 1163 1144 * @return bool|WP_Error True if successful. WP_Error if the meta key is invalid. 1164 1145 */ 1165 function unregister_meta_key( $object_type, $ object_subtype, $meta_key ) {1146 function unregister_meta_key( $object_type, $meta_key ) { 1166 1147 global $wp_meta_keys; 1167 1148 1168 if ( ! registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) ) {1149 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1169 1150 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) ); 1170 1151 } 1171 1152 1172 $args = $wp_meta_keys[ $object_type ][ $ object_subtype ][ $meta_key ];1153 $args = $wp_meta_keys[ $object_type ][ $meta_key ]; 1173 1154 1174 1155 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 1175 remove_filter( "sanitize_{$object_type}_ {$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'] );1156 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); 1176 1157 } 1177 1158 1178 1159 if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) { 1179 remove_filter( "auth_{$object_type}_ {$object_subtype}_meta_{$meta_key}", $args['auth_callback'] );1160 remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] ); 1180 1161 } 1181 1162 1182 unset( $wp_meta_keys[ $object_type ][ $ object_subtype ][ $meta_key ] );1163 unset( $wp_meta_keys[ $object_type ][ $meta_key ] ); 1183 1164 1184 1165 // 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 1166 if ( empty( $wp_meta_keys[ $object_type ] ) ) { 1190 1167 unset( $wp_meta_keys[ $object_type ] ); 1191 1168 } … … 1194 1171 } 1195 1172 1196 1173 /** 1197 * Retrieves a list of registered meta keys for an object type and optionally subtype.1174 * Retrieves a list of registered meta keys for an object type. 1198 1175 * 1199 1176 * @since 4.6.0 1200 1177 * 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). 1178 * @param string $object_type The type of object. Post, comment, user, term. 1203 1179 * 1204 1180 * @return array List of registered meta keys. 1205 1181 */ 1206 function get_registered_meta_keys( $object_type , $object_subtype = '') {1182 function get_registered_meta_keys( $object_type ) { 1207 1183 global $wp_meta_keys; 1208 1184 1209 1185 if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { … … 1210 1186 return array(); 1211 1187 } 1212 1188 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 ]; 1189 return $wp_meta_keys[ $object_type ]; 1222 1190 } 1223 1191 1224 1192 /** … … 1227 1195 * @since 4.6.0 1228 1196 * 1229 1197 * @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 1198 * @param int $object_id ID of the object the metadata is for. 1232 1199 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered 1233 1200 * metadata for the specified object. … … 1234 1201 * 1235 1202 * @return mixed|WP_Error 1236 1203 */ 1237 function get_registered_metadata( $object_type, $object_ subtype, $object_id, $meta_key = '' ) {1204 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { 1238 1205 global $wp_meta_keys; 1239 1206 1240 1207 if ( ! is_array( $wp_meta_keys ) ) { … … 1246 1213 } 1247 1214 1248 1215 if ( ! empty( $meta_key ) ) { 1249 if ( ! registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) ) {1216 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1250 1217 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); 1251 1218 } 1252 $meta_keys = get_registered_meta_keys( $object_type , $object_subtype);1219 $meta_keys = get_registered_meta_keys( $object_type ); 1253 1220 $meta_key_data = $meta_keys[ $meta_key ]; 1254 1221 1255 1222 $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] ); … … 1259 1226 1260 1227 $data = get_metadata( $object_type, $object_id ); 1261 1228 1262 $meta_keys = get_registered_meta_keys( $object_type , $object_subtype);1229 $meta_keys = get_registered_meta_keys( $object_type ); 1263 1230 $registered_data = array(); 1264 1231 1265 1232 // Someday, array_filter() -
tests/phpunit/tests/meta/registerMeta.php
28 28 return $meta_key . ' old sanitized'; 29 29 } 30 30 31 public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type , $object_subtype) {31 public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type ) { 32 32 return $meta_key . ' new sanitized'; 33 33 } 34 34 … … 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 $has_filter = has_filter( 'sanitize_post _post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) );240 $has_filter = has_filter( 'sanitize_post:post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) ); 256 241 257 242 $this->assertFalse( $has_filter ); 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 $has_filter = has_filter( 'auth_post _post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) );249 $has_filter = has_filter( 'auth_post:post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) ); 265 250 266 251 $this->assertFalse( $has_filter ); 267 252 } … … 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 }