Make WordPress Core


Ignore:
Timestamp:
01/15/2016 12:19:15 PM (10 years ago)
Author:
swissspidy
Message:

Post Types: Introduce unregister_post_type().

This new function can be used to completely unregister non built-in post types.

Fixes #14761.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r36307 r36316  
    11881188
    11891189/**
     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 */
     1205function 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/**
    11901272 * Build an object with all post type capabilities out of a post type object
    11911273 *
     
    12941376 * @access private
    12951377 *
    1296  * @staticvar array $meta_caps
    1297  *
    1298  * @param array|void $capabilities Post type meta capabilities.
     1378 * @global array $post_type_meta_caps Used to store meta capabilities.
     1379 *
     1380 * @param array $capabilities Post type meta capabilities.
    12991381 */
    13001382function _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
    13041385    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        }
    13071389    }
    13081390}
Note: See TracChangeset for help on using the changeset viewer.