diff --git a/wp-includes/capabilities.php b/wp-includes/capabilities.php index 7c570c4a1d..d8f52fe20c 100644 --- a/wp-includes/capabilities.php +++ b/wp-includes/capabilities.php @@ -16,6 +16,8 @@ * * @since 2.0.0 * + * @global array $post_type_meta_caps Used to get post type meta capabilities. + * * @param string $cap Capability name. * @param int $user_id User ID. * @param int $object_id Optional. ID of the specific object to check against if `$cap` is a "meta" cap. @@ -377,7 +379,7 @@ function map_meta_cap( $cap, $user_id ) { break; default: // Handle meta capabilities for custom post types. - $post_type_meta_caps = _post_type_meta_capabilities(); + global $post_type_meta_caps; if ( isset( $post_type_meta_caps[ $cap ] ) ) { $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args ); return call_user_func_array( 'map_meta_cap', $args ); diff --git a/wp-includes/post.php b/wp-includes/post.php index 690e597c1a..bb7e37a9a2 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -1186,6 +1186,88 @@ function register_post_type( $post_type, $args = array() ) { return $args; } +/** + * Unregister a post type. + * + * Can not be used to unregister built-in post types. + * + * @since 4.5.0 + * + * @global WP_Rewrite $wp_rewrite WordPress rewrite component. + * @global WP $wp Current WordPress environment instance. + * @global array $_wp_post_type_features Used to remove post type features. + * @global array $post_type_meta_caps Used to remove meta capabilities. + * @global array $wp_post_types List of post types. + * + * @param string $post_type Post type key. + * @return bool|WP_Error True on success, WP_Error on failure. + */ +function unregister_post_type( $post_type ) { + if ( ! post_type_exists( $post_type ) ) { + return new WP_Error( 'invalid_post_type', __( 'Invalid post type' ) ); + } + + $post_type_args = get_post_type_object( $post_type ); + + // Do not allow unregistering internal post types. + if ( $post_type_args->_builtin ) { + return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) ); + } + + global $wp, $wp_rewrite, $_wp_post_type_features, $post_type_meta_caps, $wp_post_types; + + // Remove query var. + if ( false !== $post_type_args->query_var ) { + $wp->remove_query_var( $post_type_args->query_var ); + } + + // Remove any rewrite rules, permastructs, and rules. + if ( false !== $post_type_args->rewrite ) { + remove_rewrite_tag( "%$post_type%" ); + remove_permastruct( $post_type ); + foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) { + if ( false !== strpos( $query, "index.php?post_type=$post_type" ) ) { + unset( $wp_rewrite->extra_rules_top[ $regex ] ); + } + } + } + + // Remove registered custom meta capabilities. + foreach ( $post_type_args->cap as $cap ) { + unset( $post_type_meta_caps[ $cap ] ); + } + + // Remove all post type support. + unset( $_wp_post_type_features[ $post_type ] ); + + // Unregister the post type meta box if a custom callback was specified. + if ( $post_type_args->register_meta_box_cb ) { + remove_action( 'add_meta_boxes_' . $post_type, $post_type_args->register_meta_box_cb ); + } + + // Remove the post type from all taxonomies. + foreach ( get_object_taxonomies( $post_type ) as $taxonomy ) { + unregister_taxonomy_for_object_type( $taxonomy, $post_type ); + } + + // Remove the future post hook action. + remove_action( 'future_' . $post_type, '_future_post_hook', 5 ); + + // Remove the post type. + unset( $wp_post_types[ $post_type ] ); + + /** + * Fires after a post type was unregistered. + * + * @since 4.5.0 + * + * @param string $post_type Post type key. + */ + do_action( 'unregistered_post_type', $post_type ); + + return true; +} + /** * Build an object with all post type capabilities out of a post type object * @@ -1293,17 +1375,17 @@ function get_post_type_capabilities( $args ) { * @since 3.1.0 * @access private * - * @staticvar array $meta_caps + * @global array $post_type_meta_caps Used to store meta capabilities. * - * @param array|void $capabilities Post type meta capabilities. + * @param array $capabilities Post type meta capabilities. */ function _post_type_meta_capabilities( $capabilities = null ) { - static $meta_caps = array(); - if ( null === $capabilities ) - return $meta_caps; + global $post_type_meta_caps; + foreach ( $capabilities as $core => $custom ) { - if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) - $meta_caps[ $custom ] = $core; + if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) { + $post_type_meta_caps[ $custom ] = $core; + } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 388f71c2fb..268f9e2a6f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-1452858852281'; +$wp_version = '4.5-alpha-1452860412907'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.