Make WordPress Core

Changeset 37890


Ignore:
Timestamp:
06/28/2016 04:40:33 PM (8 years ago)
Author:
ocean90
Message:

Posts: Introduce WP_Post_Type and use it in register_post_type() and unregister_post_type().

This changes the global $wp_post_types to an array of WP_Post_Type objects. WP_Post_Type includes methods to handle post type supports, rewrite rules, meta boxes, hooks, and taxonomies.
Each post type argument becomes a property of WP_Post_Type.

Props swissspidy, flixos90.
Fixes #36217.

Location:
trunk
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-advanced.php

    r37674 r37890  
    1212
    1313/**
    14  * @global string  $post_type
    15  * @global object $post_type_object
    16  * @global WP_Post $post
     14 * @global string       $post_type
     15 * @global WP_Post_Type $post_type_object
     16 * @global WP_Post      $post
    1717 */
    1818global $post_type, $post_type_object, $post;
  • trunk/src/wp-admin/edit.php

    r37680 r37890  
    2424
    2525/**
    26  * @global string $post_type
    27  * @global object $post_type_object
     26 * @global string       $post_type
     27 * @global WP_Post_Type $post_type_object
    2828 */
    2929global $post_type, $post_type_object;
  • trunk/src/wp-admin/includes/class-wp-posts-list-table.php

    r37856 r37890  
    7373     * @see WP_List_Table::__construct() for more information on default arguments.
    7474     *
    75      * @global object $post_type_object
    76      * @global wpdb   $wpdb
     75     * @global WP_Post_Type $post_type_object
     76     * @global wpdb         $wpdb
    7777     *
    7878     * @param array $args An associative array of arguments.
  • trunk/src/wp-admin/includes/nav-menu.php

    r37881 r37890  
    523523                 * @see WP_Query::query()
    524524                 *
    525                  * @param array  $posts     The posts for the current post type.
    526                  * @param array  $args      An array of WP_Query arguments.
    527                  * @param object $post_type The current post type object for this menu item meta box.
     525                 * @param array        $posts     The posts for the current post type.
     526                 * @param array        $args      An array of WP_Query arguments.
     527                 * @param WP_Post_Type $post_type The current post type object for this menu item meta box.
    528528                 */
    529529                $posts = apply_filters( "nav_menu_items_{$post_type_name}", $posts, $args, $post_type );
  • trunk/src/wp-includes/class-wp-xmlrpc-server.php

    r37674 r37890  
    884884     * @access protected
    885885     *
    886      * @param object $post_type Post type object.
    887      * @param array  $fields    The subset of post fields to return.
     886     * @param WP_Post_Type $post_type Post type object.
     887     * @param array        $fields    The subset of post fields to return.
    888888     * @return array The prepared post type data.
    889889     */
     
    923923         * @since 3.4.0
    924924         *
    925          * @param array  $_post_type An array of post type data.
    926          * @param object $post_type  Post type object.
     925         * @param array        $_post_type An array of post type data.
     926         * @param WP_Post_Type $post_type  Post type object.
    927927         */
    928928        return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
  • trunk/src/wp-includes/post.php

    r37886 r37890  
    820820
    821821/**
    822  * Retrieve the post type of the current post or of a given post.
     822 * Retrieves the post type of the current post or of a given post.
    823823 *
    824824 * @since 2.1.0
     
    835835
    836836/**
    837  * Retrieve a post type object by name.
     837 * Retrieves a post type object by name.
    838838 *
    839839 * @since 3.0.0
     
    844844 *
    845845 * @param string $post_type The name of a registered post type.
    846  * @return object|null A post type object.
     846 * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise.
    847847 */
    848848function get_post_type_object( $post_type ) {
     
    900900 * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing
    901901 *              screen and post editing screen.
    902  *
    903  * @global array      $wp_post_types List of post types.
    904  * @global WP_Rewrite $wp_rewrite    Used for default feeds.
    905  * @global WP         $wp            Used to add query vars.
     902 * @since 4.6.0 Converted to use `WP_Post_Type`.
     903 *
     904 * @global array $wp_post_types List of post types.
    906905 *
    907906 * @param string $post_type Post type key. Must not exceed 20 characters and may
     
    10121011 *                                             this post type. Default 'post.php?post=%d'.
    10131012 * }
    1014  * @return object|WP_Error The registered post type object, or an error object.
     1013 * @return WP_Post_Type|WP_Error The registered post type object, or an error object.
    10151014 */
    10161015function register_post_type( $post_type, $args = array() ) {
    1017     global $wp_post_types, $wp_rewrite, $wp;
     1016    global $wp_post_types;
    10181017
    10191018    if ( ! is_array( $wp_post_types ) ) {
     
    10231022    // Sanitize post type name
    10241023    $post_type = sanitize_key( $post_type );
    1025     $args      = wp_parse_args( $args );
    1026 
    1027     /**
    1028      * Filters the arguments for registering a post type.
    1029      *
    1030      * @since 4.4.0
    1031      *
    1032      * @param array  $args      Array of arguments for registering a post type.
    1033      * @param string $post_type Post type key.
    1034      */
    1035     $args = apply_filters( 'register_post_type_args', $args, $post_type );
    1036 
    1037     $has_edit_link = ! empty( $args['_edit_link'] );
    1038 
    1039     // Args prefixed with an underscore are reserved for internal use.
    1040     $defaults = array(
    1041         'labels'               => array(),
    1042         'description'          => '',
    1043         'public'               => false,
    1044         'hierarchical'         => false,
    1045         'exclude_from_search'  => null,
    1046         'publicly_queryable'   => null,
    1047         'show_ui'              => null,
    1048         'show_in_menu'         => null,
    1049         'show_in_nav_menus'    => null,
    1050         'show_in_admin_bar'    => null,
    1051         'menu_position'        => null,
    1052         'menu_icon'            => null,
    1053         'capability_type'      => 'post',
    1054         'capabilities'         => array(),
    1055         'map_meta_cap'         => null,
    1056         'supports'             => array(),
    1057         'register_meta_box_cb' => null,
    1058         'taxonomies'           => array(),
    1059         'has_archive'          => false,
    1060         'rewrite'              => true,
    1061         'query_var'            => true,
    1062         'can_export'           => true,
    1063         'delete_with_user'     => null,
    1064         '_builtin'             => false,
    1065         '_edit_link'           => 'post.php?post=%d',
    1066     );
    1067     $args = array_merge( $defaults, $args );
    1068     $args = (object) $args;
    1069 
    1070     $args->name = $post_type;
    10711024
    10721025    if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
     
    10751028    }
    10761029
    1077     // If not set, default to the setting for public.
    1078     if ( null === $args->publicly_queryable )
    1079         $args->publicly_queryable = $args->public;
    1080 
    1081     // If not set, default to the setting for public.
    1082     if ( null === $args->show_ui )
    1083         $args->show_ui = $args->public;
    1084 
    1085     // If not set, default to the setting for show_ui.
    1086     if ( null === $args->show_in_menu || ! $args->show_ui )
    1087         $args->show_in_menu = $args->show_ui;
    1088 
    1089     // If not set, default to the whether the full UI is shown.
    1090     if ( null === $args->show_in_admin_bar )
    1091         $args->show_in_admin_bar = (bool) $args->show_in_menu;
    1092 
    1093     // If not set, default to the setting for public.
    1094     if ( null === $args->show_in_nav_menus )
    1095         $args->show_in_nav_menus = $args->public;
    1096 
    1097     // If not set, default to true if not public, false if public.
    1098     if ( null === $args->exclude_from_search )
    1099         $args->exclude_from_search = !$args->public;
    1100 
    1101     // Back compat with quirky handling in version 3.0. #14122.
    1102     if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
    1103         $args->map_meta_cap = true;
    1104 
    1105     // If not set, default to false.
    1106     if ( null === $args->map_meta_cap )
    1107         $args->map_meta_cap = false;
    1108 
    1109     // If there's no specified edit link and no UI, remove the edit link.
    1110     if ( ! $args->show_ui && ! $has_edit_link ) {
    1111         $args->_edit_link = '';
    1112     }
    1113 
    1114     $args->cap = get_post_type_capabilities( $args );
    1115     unset( $args->capabilities );
    1116 
    1117     if ( is_array( $args->capability_type ) )
    1118         $args->capability_type = $args->capability_type[0];
    1119 
    1120     if ( ! empty( $args->supports ) ) {
    1121         add_post_type_support( $post_type, $args->supports );
    1122         unset( $args->supports );
    1123     } elseif ( false !== $args->supports ) {
    1124         // Add default features
    1125         add_post_type_support( $post_type, array( 'title', 'editor' ) );
    1126     }
    1127 
    1128     if ( false !== $args->query_var ) {
    1129         if ( true === $args->query_var )
    1130             $args->query_var = $post_type;
    1131         else
    1132             $args->query_var = sanitize_title_with_dashes( $args->query_var );
    1133 
    1134         if ( $wp && is_post_type_viewable( $args ) ) {
    1135             $wp->add_query_var( $args->query_var );
    1136         }
    1137     }
    1138 
    1139     if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
    1140         if ( ! is_array( $args->rewrite ) )
    1141             $args->rewrite = array();
    1142         if ( empty( $args->rewrite['slug'] ) )
    1143             $args->rewrite['slug'] = $post_type;
    1144         if ( ! isset( $args->rewrite['with_front'] ) )
    1145             $args->rewrite['with_front'] = true;
    1146         if ( ! isset( $args->rewrite['pages'] ) )
    1147             $args->rewrite['pages'] = true;
    1148         if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
    1149             $args->rewrite['feeds'] = (bool) $args->has_archive;
    1150         if ( ! isset( $args->rewrite['ep_mask'] ) ) {
    1151             if ( isset( $args->permalink_epmask ) )
    1152                 $args->rewrite['ep_mask'] = $args->permalink_epmask;
    1153             else
    1154                 $args->rewrite['ep_mask'] = EP_PERMALINK;
    1155         }
    1156 
    1157         if ( $args->hierarchical )
    1158             add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" );
    1159         else
    1160             add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" );
    1161 
    1162         if ( $args->has_archive ) {
    1163             $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
    1164             if ( $args->rewrite['with_front'] )
    1165                 $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
    1166             else
    1167                 $archive_slug = $wp_rewrite->root . $archive_slug;
    1168 
    1169             add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
    1170             if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
    1171                 $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
    1172                 add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
    1173                 add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
    1174             }
    1175             if ( $args->rewrite['pages'] )
    1176                 add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
    1177         }
    1178 
    1179         $permastruct_args = $args->rewrite;
    1180         $permastruct_args['feed'] = $permastruct_args['feeds'];
    1181         add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
    1182     }
    1183 
    1184     // Register the post type meta box if a custom callback was specified.
    1185     if ( $args->register_meta_box_cb )
    1186         add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );
    1187 
    1188     $args->labels = get_post_type_labels( $args );
    1189     $args->label = $args->labels->name;
    1190 
    1191     $wp_post_types[ $post_type ] = $args;
    1192 
    1193     add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
    1194 
    1195     foreach ( $args->taxonomies as $taxonomy ) {
    1196         register_taxonomy_for_object_type( $taxonomy, $post_type );
    1197     }
     1030    $post_type_object = new WP_Post_Type( $post_type, $args );
     1031    $post_type_object->add_supports();
     1032    $post_type_object->add_rewrite_rules();
     1033    $post_type_object->register_meta_boxes();
     1034
     1035    $wp_post_types[ $post_type ] = $post_type_object;
     1036
     1037    $post_type_object->add_hooks();
     1038    $post_type_object->register_taxonomies();
    11981039
    11991040    /**
     
    12021043     * @since 3.3.0
    12031044     *
    1204      * @param string $post_type Post type.
    1205      * @param object $args      Arguments used to register the post type.
     1045     * @param string       $post_type        Post type.
     1046     * @param WP_Post_Type $post_type_object Arguments used to register the post type.
    12061047     */
    1207     do_action( 'registered_post_type', $post_type, $args );
    1208 
    1209     return $args;
     1048    do_action( 'registered_post_type', $post_type, $post_type_object );
     1049
     1050    return $post_type_object;
    12101051}
    12111052
     
    12161057 *
    12171058 * @since 4.5.0
    1218  *
    1219  * @global WP_Rewrite $wp_rewrite             WordPress rewrite component.
    1220  * @global WP         $wp                     Current WordPress environment instance.
    1221  * @global array      $_wp_post_type_features Used to remove post type features.
    1222  * @global array      $post_type_meta_caps    Used to remove meta capabilities.
    1223  * @global array      $wp_post_types          List of post types.
     1059 * @since 4.6.0 Converted to use `WP_Post_Type`.
     1060 *
     1061 * @global array $wp_post_types List of post types.
    12241062 *
    12251063 * @param string $post_type Post type to unregister.
     
    12271065 */
    12281066function unregister_post_type( $post_type ) {
     1067    global $wp_post_types;
     1068
    12291069    if ( ! post_type_exists( $post_type ) ) {
    12301070        return new WP_Error( 'invalid_post_type', __( 'Invalid post type' ) );
    12311071    }
    12321072
    1233     $post_type_args = get_post_type_object( $post_type );
     1073    $post_type_object = get_post_type_object( $post_type );
    12341074
    12351075    // Do not allow unregistering internal post types.
    1236     if ( $post_type_args->_builtin ) {
     1076    if ( $post_type_object->_builtin ) {
    12371077        return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) );
    12381078    }
    12391079
    1240     global $wp, $wp_rewrite, $_wp_post_type_features, $post_type_meta_caps, $wp_post_types;
    1241 
    1242     // Remove query var.
    1243     if ( false !== $post_type_args->query_var ) {
    1244         $wp->remove_query_var( $post_type_args->query_var );
    1245     }
    1246 
    1247     // Remove any rewrite rules, permastructs, and rules.
    1248     if ( false !== $post_type_args->rewrite ) {
    1249         remove_rewrite_tag( "%$post_type%" );
    1250         remove_permastruct( $post_type );
    1251         foreach ( $wp_rewrite->extra_rules_top as $regex => $query ) {
    1252             if ( false !== strpos( $query, "index.php?post_type=$post_type" ) ) {
    1253                 unset( $wp_rewrite->extra_rules_top[ $regex ] );
    1254             }
    1255         }
    1256     }
    1257 
    1258     // Remove registered custom meta capabilities.
    1259     foreach ( $post_type_args->cap as $cap ) {
    1260         unset( $post_type_meta_caps[ $cap ] );
    1261     }
    1262 
    1263     // Remove all post type support.
    1264     unset( $_wp_post_type_features[ $post_type ] );
    1265 
    1266     // Unregister the post type meta box if a custom callback was specified.
    1267     if ( $post_type_args->register_meta_box_cb ) {
    1268         remove_action( 'add_meta_boxes_' . $post_type, $post_type_args->register_meta_box_cb );
    1269     }
    1270 
    1271     // Remove the post type from all taxonomies.
    1272     foreach ( get_object_taxonomies( $post_type ) as $taxonomy ) {
    1273         unregister_taxonomy_for_object_type( $taxonomy, $post_type );
    1274     }
    1275 
    1276     // Remove the future post hook action.
    1277     remove_action( 'future_' . $post_type, '_future_post_hook', 5 );
    1278 
    1279     // Remove the post type.
     1080    $post_type_object->remove_supports();
     1081    $post_type_object->remove_rewrite_rules();
     1082    $post_type_object->unregister_meta_boxes();
     1083    $post_type_object->remove_hooks();
     1084    $post_type_object->unregister_taxonomies();
     1085
    12801086    unset( $wp_post_types[ $post_type ] );
    12811087
     
    14631269 * @access private
    14641270 *
    1465  * @param object $post_type_object Post type object.
     1271 * @param object|WP_Post_Type $post_type_object Post type object.
    14661272 * @return object Object with all the labels as member variables.
    14671273 */
     
    17181524 * @since 4.5.0 Added the ability to pass a post type name in addition to object.
    17191525 *
    1720  * @param object $post_type Post type name or object.
     1526 * @param string|WP_Post_Type $post_type Post type name or object.
    17211527 * @return bool Whether the post type should be considered viewable.
    17221528 */
  • trunk/src/wp-settings.php

    r37864 r37890  
    159159require( ABSPATH . WPINC . '/class-walker-page.php' );
    160160require( ABSPATH . WPINC . '/class-walker-page-dropdown.php' );
     161require( ABSPATH . WPINC . '/class-wp-post-type.php' );
    161162require( ABSPATH . WPINC . '/class-wp-post.php' );
    162163require( ABSPATH . WPINC . '/post-template.php' );
  • trunk/tests/phpunit/tests/post/types.php

    r37459 r37890  
    3030
    3131        $pobj = get_post_type_object( 'foo' );
    32         $this->assertInstanceOf( 'stdClass', $pobj );
     32        $this->assertInstanceOf( 'WP_Post_Type', $pobj );
    3333        $this->assertEquals( 'foo', $pobj->name );
    3434
     
    555555     *
    556556     * @param array $args register_post_type() arguments.
    557      * @return stdClass Post type object for `$this->post_type`.
     557     * @return WP_Post_Type Post type object for `$this->post_type`.
    558558     */
    559559    public function register_post_type( $args = array() ) {
Note: See TracChangeset for help on using the changeset viewer.