Ticket #14761: 14761.3.diff
File 14761.3.diff, 11.3 KB (added by , 9 years ago) |
---|
-
src/wp-includes/capabilities.php
diff --git src/wp-includes/capabilities.php src/wp-includes/capabilities.php index 7c570c4..d8f52fe 100644
16 16 * 17 17 * @since 2.0.0 18 18 * 19 * @global array $post_type_meta_caps Used to get post type meta capabilities. 20 * 19 21 * @param string $cap Capability name. 20 22 * @param int $user_id User ID. 21 23 * @param int $object_id Optional. ID of the specific object to check against if `$cap` is a "meta" cap. … … function map_meta_cap( $cap, $user_id ) { 377 379 break; 378 380 default: 379 381 // Handle meta capabilities for custom post types. 380 $post_type_meta_caps = _post_type_meta_capabilities();382 global $post_type_meta_caps; 381 383 if ( isset( $post_type_meta_caps[ $cap ] ) ) { 382 384 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args ); 383 385 return call_user_func_array( 'map_meta_cap', $args ); -
src/wp-includes/post.php
diff --git src/wp-includes/post.php src/wp-includes/post.php index 0fb1dfa..da0e4c4 100644
function register_post_type( $post_type, $args = array() ) { 1187 1187 } 1188 1188 1189 1189 /** 1190 * Unregister a post type. 1191 * 1192 * @since 4.5.0 1193 * 1194 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1195 * @global WP $wp Current WordPress environment instance. 1196 * @global array $_wp_post_type_features Used to remove post type features. 1197 * @global array $post_type_meta_caps Used to remove meta capabilities. 1198 * @global array $wp_post_types List of post types. 1199 * 1200 * @param string $post_type Post type key. 1201 * @return bool|WP_Error True on success, WP_Error on failure. 1202 */ 1203 function unregister_post_type( $post_type ) { 1204 if ( ! post_type_exists( $post_type ) ) { 1205 return new WP_Error( 'invalid_post_type', __( 'Invalid post type' ) ); 1206 } 1207 1208 $post_type_args = get_post_type_object( $post_type ); 1209 1210 // Do not allow unregistering internal post types. 1211 if ( $post_type_args->_builtin ) { 1212 return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) ); 1213 } 1214 1215 global $wp, $wp_rewrite, $_wp_post_type_features, $post_type_meta_caps, $wp_post_types; 1216 1217 // Remove query var. 1218 if ( false !== $post_type_args->query_var ) { 1219 $wp->remove_query_var( $post_type_args->query_var ); 1220 } 1221 1222 // Remove any rewrite rules, permastructs, and rules. 1223 if ( false !== $post_type_args->rewrite ) { 1224 remove_rewrite_tag( "%$post_type%" ); 1225 remove_permastruct( $post_type ); 1226 foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) { 1227 if ( false !== strpos( $query, "index.php?post_type=$post_type" ) ) { 1228 unset( $wp_rewrite->extra_rules_top[ $regex ] ); 1229 } 1230 } 1231 } 1232 1233 // Remove registered custom meta capabilities. 1234 foreach ( $post_type_args->cap as $cap ) { 1235 unset( $post_type_meta_caps[ $cap ] ); 1236 } 1237 1238 // Remove all post type support. 1239 unset( $_wp_post_type_features[ $post_type ] ); 1240 1241 // Unregister the post type meta box if a custom callback was specified. 1242 if ( $post_type_args->register_meta_box_cb ) { 1243 remove_action( 'add_meta_boxes_' . $post_type, $post_type_args->register_meta_box_cb ); 1244 } 1245 1246 // Remove the post type from all taxonomies. 1247 foreach ( get_object_taxonomies( $post_type ) as $taxonomy ) { 1248 unregister_taxonomy_for_object_type( $taxonomy, $post_type ); 1249 } 1250 1251 // Remove the future post hook action. 1252 remove_action( 'future_' . $post_type, '_future_post_hook', 5 ); 1253 1254 // Remove the post type. 1255 unset( $wp_post_types[ $post_type ] ); 1256 1257 /** 1258 * Fires after a post type is unregistered. 1259 * 1260 * @since 4.5.0 1261 * 1262 * @param string $post_type Post type. 1263 */ 1264 do_action( 'unregistered_post_type', $post_type ); 1265 1266 return true; 1267 } 1268 1269 /** 1190 1270 * Build an object with all post type capabilities out of a post type object 1191 1271 * 1192 1272 * Post type capabilities use the 'capability_type' argument as a base, if the … … function get_post_type_capabilities( $args ) { 1293 1373 * @since 3.1.0 1294 1374 * @access private 1295 1375 * 1296 * @ staticvar array $meta_caps1376 * @global array $post_type_meta_caps Used to store meta capabilities. 1297 1377 * 1298 * @param array |void$capabilities Post type meta capabilities.1378 * @param array $capabilities Post type meta capabilities. 1299 1379 */ 1300 1380 function _post_type_meta_capabilities( $capabilities = null ) { 1301 static $meta_caps = array(); 1302 if ( null === $capabilities ) 1303 return $meta_caps; 1381 global $post_type_meta_caps; 1382 1304 1383 foreach ( $capabilities as $core => $custom ) { 1305 if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) 1306 $meta_caps[ $custom ] = $core; 1384 if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) { 1385 $post_type_meta_caps[ $custom ] = $core; 1386 } 1307 1387 } 1308 1388 } 1309 1389 -
tests/phpunit/includes/utils.php
diff --git tests/phpunit/includes/utils.php tests/phpunit/includes/utils.php index 0a6dfc1..5d595d5 100644
if ( !function_exists( 'str_getcsv' ) ) { 323 323 * Removes the post type and its taxonomy associations. 324 324 */ 325 325 function _unregister_post_type( $cpt_name ) { 326 unset( $GLOBALS['wp_post_types'][ $cpt_name ] ); 327 unset( $GLOBALS['_wp_post_type_features'][ $cpt_name ] ); 328 329 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy ) { 330 if ( false !== $key = array_search( $cpt_name, $taxonomy->object_type ) ) { 331 unset( $taxonomy->object_type[$key] ); 332 } 333 } 326 unregister_post_type( $cpt_name ); 334 327 } 335 328 336 329 function _unregister_taxonomy( $taxonomy_name ) { … … class wpdb_exposed_methods_for_testing extends wpdb { 398 391 */ 399 392 function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) { 400 393 $saved_config = ini_get( 'pcre.backtrack_limit' ); 401 394 402 395 // Attempt to prevent PHP crashes. Adjust these lower when needed. 403 396 if ( version_compare( phpversion(), '5.4.8', '>' ) ) { 404 397 $limit = 1000000; … … function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) { 410 403 for( $i = 4; $i <= $limit; $i *= 2 ) { 411 404 412 405 ini_set( 'pcre.backtrack_limit', $i ); 413 406 414 407 switch( $strategy ) { 415 408 case 'split': 416 409 preg_split( $pattern, $subject ); -
tests/phpunit/tests/post/types.php
diff --git tests/phpunit/tests/post/types.php tests/phpunit/tests/post/types.php index 02d4590..9d02401 100644
class Tests_Post_Types extends WP_UnitTestCase { 159 159 160 160 _unregister_post_type( 'foo' ); 161 161 } 162 163 /** 164 * @ticket 14761 165 */ 166 public function test_unregister_post_type() { 167 register_post_type( 'foo' ); 168 $this->assertTrue( unregister_post_type( 'foo' ) ); 169 } 170 171 /** 172 * @ticket 14761 173 */ 174 public function test_unregister_post_type_unknown_post_type() { 175 $this->assertWPError( unregister_post_type( 'foo' ) ); 176 } 177 178 /** 179 * @ticket 14761 180 */ 181 public function test_unregister_post_type_twice() { 182 register_post_type( 'foo' ); 183 $this->assertTrue( unregister_post_type( 'foo' ) ); 184 $this->assertWPError( unregister_post_type( 'foo' ) ); 185 } 186 187 /** 188 * @ticket 14761 189 */ 190 public function test_unregister_post_type_disallow_builtin_post_type() { 191 $this->assertWPError( unregister_post_type( 'post' ) ); 192 } 193 194 /** 195 * @ticket 14761 196 */ 197 public function test_unregister_post_type_removes_query_vars() { 198 global $wp; 199 200 register_post_type( 'foo', array( 201 'public' => true, 202 'query_var' => 'bar', 203 ) ); 204 205 $this->assertInternalType( 'int', array_search( 'bar', $wp->public_query_vars ) ); 206 $this->assertTrue( unregister_post_type( 'foo' ) ); 207 $this->assertFalse( array_search( 'bar', $wp->public_query_vars ) ); 208 } 209 210 /** 211 * @ticket 14761 212 */ 213 public function test_unregister_post_type_removes_rewrite_rules() { 214 $this->set_permalink_structure( '/%postname%' ); 215 216 global $wp_rewrite; 217 218 register_post_type( 'foo', array( 219 'public' => true, 220 'query_var' => 'bar', 221 ) ); 222 223 $count_before = count( $wp_rewrite->rewritereplace ); 224 225 $this->assertInternalType( 'int', array_search( '%foo%', $wp_rewrite->rewritecode ) ); 226 $this->assertInternalType( 'int', array_search( 'bar=', $wp_rewrite->queryreplace ) ); 227 $this->assertTrue( unregister_post_type( 'foo' ) ); 228 $this->assertFalse( array_search( '%foo%', $wp_rewrite->rewritecode ) ); 229 $this->assertFalse( array_search( 'bar=', $wp_rewrite->queryreplace ) ); 230 $this->assertSame( --$count_before, count( $wp_rewrite->rewritereplace ) ); // Array was reduced by one value. 231 } 232 233 /** 234 * @ticket 14761 235 */ 236 public function test_unregister_post_type_removes_custom_meta_capabilities() { 237 global $post_type_meta_caps; 238 239 register_post_type( 'foo', array( 240 'public' => true, 241 'capability_type' => 'bar', 242 ) ); 243 244 $post_type_args = get_post_type_object( 'foo' ); 245 $this->assertTrue( unregister_post_type( 'foo' ) ); 246 247 foreach ( $post_type_args->cap as $cap ) { 248 $this->assertFalse( isset( $post_type_meta_caps[ $cap ] ) ); 249 } 250 } 251 252 /** 253 * @ticket 14761 254 */ 255 public function test_unregister_post_type_removes_post_type_supports() { 256 global $_wp_post_type_features; 257 258 register_post_type( 'foo', array( 259 'public' => true, 260 'supports' => array( 'editor', 'author', 'title' ), 261 ) ); 262 263 $this->assertSame( 3, count( $_wp_post_type_features['foo'] ) ); 264 $this->assertTrue( unregister_post_type( 'foo' ) ); 265 $this->assertFalse( isset( $_wp_post_type_features['foo'] ) ); 266 } 267 268 /** 269 * @ticket 14761 270 */ 271 public function test_unregister_post_type_removes_post_type_from_taxonomies() { 272 global $wp_taxonomies; 273 274 register_post_type( 'foo', array( 275 'public' => true, 276 'taxonomies' => array( 'category', 'post_tag' ), 277 ) ); 278 279 $this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['category']->object_type, true ) ); 280 $this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) ); 281 $this->assertTrue( unregister_post_type( 'foo' ) ); 282 $this->assertFalse( array_search( 'foo', $wp_taxonomies['category']->object_type, true ) ); 283 $this->assertFalse( array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) ); 284 } 285 286 /** 287 * @ticket 14761 288 */ 289 public function test_unregister_post_type_removes_the_future_post_hooks() { 290 global $wp_filter; 291 292 register_post_type( 'foo', array( 293 'public' => true, 294 ) ); 295 296 add_action( 'future_foo', 'var_dump' ); 297 298 $this->assertSame( 2, count( $wp_filter['future_foo'] ) ); 299 $this->assertTrue( unregister_post_type( 'foo' ) ); 300 $this->assertSame( array(), $wp_filter['future_foo'] ); 301 } 302 303 /** 304 * @ticket 14761 305 */ 306 public function test_unregister_post_type_removes_meta_boxes() { 307 global $wp_filter; 308 309 register_post_type( 'foo', array( 310 'public' => true, 311 ) ); 312 313 add_action( 'add_meta_boxes_foo', 'var_dump' ); 314 315 $this->assertSame( 1, count( $wp_filter['add_meta_boxes_foo'] ) ); 316 $this->assertTrue( unregister_post_type( 'foo' ) ); 317 $this->assertSame( array(), $wp_filter['add_meta_boxes_foo'] ); 318 } 319 320 /** 321 * @ticket 14761 322 */ 323 public function test_unregister_post_type_removes_post_type_from_global() { 324 global $wp_post_types; 325 326 register_post_type( 'foo', array( 327 'public' => true, 328 ) ); 329 330 $this->assertInternalType( 'object', $wp_post_types['foo'] ); 331 $this->assertInternalType( 'object', get_post_type_object( 'foo' ) ); 332 333 $this->assertTrue( unregister_post_type( 'foo' ) ); 334 335 $this->assertFalse( isset( $wp_post_types['foo'] ) ); 336 $this->assertNull( get_post_type_object( 'foo' ) ); 337 } 162 338 }