Ticket #14761: 14761.5.diff
File 14761.5.diff, 13.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 72ef164..4a84545 100644
function register_post_type( $post_type, $args = array() ) { 1187 1187 } 1188 1188 1189 1189 /** 1190 * Unregister a post type. 1191 * 1192 * Can not be used to unregister built-in post types. 1193 * 1194 * @since 4.5.0 1195 * 1196 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1197 * @global WP $wp Current WordPress environment instance. 1198 * @global array $_wp_post_type_features Used to remove post type features. 1199 * @global array $post_type_meta_caps Used to remove meta capabilities. 1200 * @global array $wp_post_types List of post types. 1201 * 1202 * @param string $post_type Post type key. 1203 * @return bool|WP_Error True on success, WP_Error on failure. 1204 */ 1205 function unregister_post_type( $post_type ) { 1206 if ( ! post_type_exists( $post_type ) ) { 1207 return new WP_Error( 'invalid_post_type', __( 'Invalid post type' ) ); 1208 } 1209 1210 $post_type_args = get_post_type_object( $post_type ); 1211 1212 // Do not allow unregistering internal post types. 1213 if ( $post_type_args->_builtin ) { 1214 return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) ); 1215 } 1216 1217 global $wp, $wp_rewrite, $_wp_post_type_features, $post_type_meta_caps, $wp_post_types; 1218 1219 // Remove query var. 1220 if ( false !== $post_type_args->query_var ) { 1221 $wp->remove_query_var( $post_type_args->query_var ); 1222 } 1223 1224 // Remove any rewrite rules, permastructs, and rules. 1225 if ( false !== $post_type_args->rewrite ) { 1226 remove_rewrite_tag( "%$post_type%" ); 1227 remove_permastruct( $post_type ); 1228 foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) { 1229 if ( false !== strpos( $query, "index.php?post_type=$post_type" ) ) { 1230 unset( $wp_rewrite->extra_rules_top[ $regex ] ); 1231 } 1232 } 1233 } 1234 1235 // Remove registered custom meta capabilities. 1236 foreach ( $post_type_args->cap as $cap ) { 1237 unset( $post_type_meta_caps[ $cap ] ); 1238 } 1239 1240 // Remove all post type support. 1241 unset( $_wp_post_type_features[ $post_type ] ); 1242 1243 // Unregister the post type meta box if a custom callback was specified. 1244 if ( $post_type_args->register_meta_box_cb ) { 1245 remove_action( 'add_meta_boxes_' . $post_type, $post_type_args->register_meta_box_cb ); 1246 } 1247 1248 // Remove the post type from all taxonomies. 1249 foreach ( get_object_taxonomies( $post_type ) as $taxonomy ) { 1250 unregister_taxonomy_for_object_type( $taxonomy, $post_type ); 1251 } 1252 1253 // Remove the future post hook action. 1254 remove_action( 'future_' . $post_type, '_future_post_hook', 5 ); 1255 1256 // Remove the post type. 1257 unset( $wp_post_types[ $post_type ] ); 1258 1259 /** 1260 * Fires after a post type was unregistered. 1261 * 1262 * @since 4.5.0 1263 * 1264 * @param string $post_type Post type key. 1265 */ 1266 do_action( 'unregistered_post_type', $post_type ); 1267 1268 return true; 1269 } 1270 1271 /** 1190 1272 * Build an object with all post type capabilities out of a post type object 1191 1273 * 1192 1274 * Post type capabilities use the 'capability_type' argument as a base, if the … … function get_post_type_capabilities( $args ) { 1293 1375 * @since 3.1.0 1294 1376 * @access private 1295 1377 * 1296 * @ staticvar array $meta_caps1378 * @global array $post_type_meta_caps Used to store meta capabilities. 1297 1379 * 1298 * @param array |void$capabilities Post type meta capabilities.1380 * @param array $capabilities Post type meta capabilities. 1299 1381 */ 1300 1382 function _post_type_meta_capabilities( $capabilities = null ) { 1301 static $meta_caps = array(); 1302 if ( null === $capabilities ) 1303 return $meta_caps; 1383 global $post_type_meta_caps; 1384 1304 1385 foreach ( $capabilities as $core => $custom ) { 1305 if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) 1306 $meta_caps[ $custom ] = $core; 1386 if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) { 1387 $post_type_meta_caps[ $custom ] = $core; 1388 } 1307 1389 } 1308 1390 } 1309 1391 -
tests/phpunit/includes/utils.php
diff --git tests/phpunit/includes/utils.php tests/phpunit/includes/utils.php index 00d1b32..26996ac 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..1528e09 100644
class Tests_Post_Types extends WP_UnitTestCase { 20 20 } 21 21 22 22 /** 23 * @ticket 3113423 * @ticket 31134 24 24 * 25 25 * @expectedIncorrectUsage register_post_type 26 26 */ … … class Tests_Post_Types extends WP_UnitTestCase { 30 30 } 31 31 32 32 /** 33 * @ticket 3113433 * @ticket 31134 34 34 * 35 35 * @expectedIncorrectUsage register_post_type 36 36 */ … … class Tests_Post_Types extends WP_UnitTestCase { 119 119 public function filter_post_type_labels( $labels ) { 120 120 unset( $labels->featured_image ); 121 121 unset( $labels->set_featured_image ); 122 122 123 return $labels; 123 124 } 124 125 … … class Tests_Post_Types extends WP_UnitTestCase { 159 160 160 161 _unregister_post_type( 'foo' ); 161 162 } 163 164 /** 165 * @ticket 14761 166 */ 167 public function test_unregister_post_type() { 168 register_post_type( 'foo' ); 169 $this->assertTrue( unregister_post_type( 'foo' ) ); 170 } 171 172 /** 173 * @ticket 14761 174 */ 175 public function test_unregister_post_type_unknown_post_type() { 176 $this->assertWPError( unregister_post_type( 'foo' ) ); 177 } 178 179 /** 180 * @ticket 14761 181 */ 182 public function test_unregister_post_type_twice() { 183 register_post_type( 'foo' ); 184 $this->assertTrue( unregister_post_type( 'foo' ) ); 185 $this->assertWPError( unregister_post_type( 'foo' ) ); 186 } 187 188 /** 189 * @ticket 14761 190 */ 191 public function test_unregister_post_type_disallow_builtin_post_type() { 192 $this->assertWPError( unregister_post_type( 'post' ) ); 193 $this->assertWPError( unregister_post_type( 'page' ) ); 194 $this->assertWPError( unregister_post_type( 'attachment' ) ); 195 $this->assertWPError( unregister_post_type( 'revision' ) ); 196 $this->assertWPError( unregister_post_type( 'nav_menu_item' ) ); 197 } 198 199 /** 200 * @ticket 14761 201 */ 202 public function test_unregister_post_type_removes_query_vars() { 203 global $wp; 204 205 register_post_type( 'foo', array( 206 'public' => true, 207 'query_var' => 'bar', 208 ) ); 209 210 $this->assertInternalType( 'int', array_search( 'bar', $wp->public_query_vars ) ); 211 $this->assertTrue( unregister_post_type( 'foo' ) ); 212 $this->assertFalse( array_search( 'bar', $wp->public_query_vars ) ); 213 } 214 215 /** 216 * @ticket 14761 217 */ 218 public function test_unregister_post_type_removes_rewrite_tags() { 219 $this->set_permalink_structure( '/%postname%' ); 220 221 global $wp_rewrite; 222 223 register_post_type( 'foo', array( 224 'public' => true, 225 'query_var' => 'bar', 226 ) ); 227 228 $count_before = count( $wp_rewrite->rewritereplace ); 229 230 $this->assertContains( '%foo%', $wp_rewrite->rewritecode ); 231 $this->assertContains( 'bar=', $wp_rewrite->queryreplace ); 232 $this->assertTrue( unregister_post_type( 'foo' ) ); 233 $this->assertNotContains( '%foo%', $wp_rewrite->rewritecode ); 234 $this->assertNotContains( 'bar=', $wp_rewrite->queryreplace ); 235 $this->assertSame( -- $count_before, count( $wp_rewrite->rewritereplace ) ); // Array was reduced by one value. 236 } 237 238 /** 239 * @ticket 14761 240 */ 241 public function test_unregister_post_type_removes_rewrite_rules() { 242 $this->set_permalink_structure( '/%postname%' ); 243 244 global $wp_rewrite; 245 246 register_post_type( 'foo', array( 247 'public' => true, 248 'has_archive' => true, 249 ) ); 250 251 $this->assertContains( 'index.php?post_type=foo', $wp_rewrite->extra_rules_top ); 252 $this->assertTrue( unregister_post_type( 'foo' ) ); 253 $this->assertNotContains( 'index.php?post_type=foo', $wp_rewrite->extra_rules_top ); 254 } 255 256 /** 257 * @ticket 14761 258 */ 259 public function test_unregister_post_type_removes_custom_meta_capabilities() { 260 global $post_type_meta_caps; 261 262 register_post_type( 'foo', array( 263 'public' => true, 264 'capability_type' => 'bar', 265 'map_meta_cap' => true, 266 ) ); 267 268 $this->assertSame( 'read_post', $post_type_meta_caps['read_bar'] ); 269 $this->assertSame( 'delete_post', $post_type_meta_caps['delete_bar'] ); 270 $this->assertSame( 'edit_post', $post_type_meta_caps['edit_bar'] ); 271 272 $this->assertTrue( unregister_post_type( 'foo' ) ); 273 274 $this->assertFalse( isset( $post_type_meta_caps['read_bar'] ) ); 275 $this->assertFalse( isset( $post_type_meta_caps['delete_bar'] ) ); 276 $this->assertFalse( isset( $post_type_meta_caps['edit_bar'] ) ); 277 } 278 279 /** 280 * @ticket 14761 281 */ 282 public function test_unregister_post_type_removes_post_type_supports() { 283 global $_wp_post_type_features; 284 285 register_post_type( 'foo', array( 286 'public' => true, 287 'supports' => array( 'editor', 'author', 'title' ), 288 ) ); 289 290 $this->assertEqualSetsWithIndex( 291 array( 292 'editor' => true, 293 'author' => true, 294 'title' => true, 295 ), 296 $_wp_post_type_features['foo'] 297 ); 298 $this->assertTrue( unregister_post_type( 'foo' ) ); 299 $this->assertFalse( isset( $_wp_post_type_features['foo'] ) ); 300 } 301 302 /** 303 * @ticket 14761 304 */ 305 public function test_unregister_post_type_removes_post_type_from_taxonomies() { 306 global $wp_taxonomies; 307 308 register_post_type( 'foo', array( 309 'public' => true, 310 'taxonomies' => array( 'category', 'post_tag' ), 311 ) ); 312 313 $this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['category']->object_type, true ) ); 314 $this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) ); 315 $this->assertTrue( unregister_post_type( 'foo' ) ); 316 $this->assertFalse( array_search( 'foo', $wp_taxonomies['category']->object_type, true ) ); 317 $this->assertFalse( array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) ); 318 $this->assertEmpty( get_object_taxonomies( 'foo' ) ); 319 } 320 321 /** 322 * @ticket 14761 323 */ 324 public function test_unregister_post_type_removes_the_future_post_hooks() { 325 global $wp_filter; 326 327 register_post_type( 'foo', array( 328 'public' => true, 329 ) ); 330 331 $this->assertSame( 1, count( $wp_filter['future_foo'] ) ); 332 $this->assertTrue( unregister_post_type( 'foo' ) ); 333 $this->assertSame( array(), $wp_filter['future_foo'] ); 334 } 335 336 /** 337 * @ticket 14761 338 */ 339 public function test_unregister_post_type_removes_meta_box_callback() { 340 global $wp_filter; 341 342 register_post_type( 'foo', array( 343 'public' => true, 344 'register_meta_box_cb' => '__return_empty_string', 345 ) ); 346 347 $this->assertSame( 1, count( $wp_filter['add_meta_boxes_foo'] ) ); 348 $this->assertTrue( unregister_post_type( 'foo' ) ); 349 $this->assertSame( array(), $wp_filter['add_meta_boxes_foo'] ); 350 } 351 352 /** 353 * @ticket 14761 354 */ 355 public function test_unregister_post_type_removes_post_type_from_global() { 356 global $wp_post_types; 357 358 register_post_type( 'foo', array( 359 'public' => true, 360 ) ); 361 362 $this->assertInternalType( 'object', $wp_post_types['foo'] ); 363 $this->assertInternalType( 'object', get_post_type_object( 'foo' ) ); 364 365 $this->assertTrue( unregister_post_type( 'foo' ) ); 366 367 $this->assertFalse( isset( $wp_post_types['foo'] ) ); 368 $this->assertNull( get_post_type_object( 'foo' ) ); 369 } 370 371 /** 372 * @ticket 14761 373 */ 374 public function test_post_type_does_not_exist_after_unregister_post_type() { 375 register_post_type( 'foo', array( 376 'public' => true, 377 ) ); 378 379 $this->assertTrue( unregister_post_type( 'foo' ) ); 380 381 $this->assertFalse( post_type_exists( 'foo' ) ); 382 } 162 383 }