WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/04/2010 04:58:43 PM (10 years ago)
Author:
ryan
Message:

More custom post type support. Props scribu. see #9674

File:
1 edited

Legend:

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

    r12584 r12597  
    1616 */
    1717function create_initial_post_types() {
    18     register_post_type( 'post', array('exclude_from_search' => false) );
    19     register_post_type( 'page', array('exclude_from_search' => false) );
    20     register_post_type( 'attachment', array('exclude_from_search' => false) );
    21     register_post_type( 'revision', array('exclude_from_search' => true) );
     18    register_post_type( 'post', array('label' => __('Posts'), 'exclude_from_search' => false, '_builtin' => true, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false) );
     19    register_post_type( 'page', array('label' => __('Pages'),'exclude_from_search' => false, '_builtin' => true, '_edit_link' => 'page.php?post=%d', 'capability_type' => 'page', 'hierarchical' => true) );
     20    register_post_type( 'attachment', array('label' => __('Media'), 'exclude_from_search' => false, '_builtin' => true, '_edit_link' => 'media.php?attachment_id=%d', 'capability_type' => 'post', 'hierarchical' => false) );
     21    register_post_type( 'revision', array('label' => __('Revisions'),'exclude_from_search' => true, '_builtin' => true, '_edit_link' => 'revision.php?revision=%d', 'capability_type' => 'post', 'hierarchical' => false) );
    2222}
    2323add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
     
    443443
    444444/**
     445 * Retrieve a post type object by name
     446 *
     447 * @package WordPress
     448 * @subpackage Post
     449 * @since 3.0
     450 * @uses $wp_post_types
     451 * @see register_post_type
     452 * @see get_post_types
     453 *
     454 * @param string $post_type The name of a registered post type
     455 * @return object A post type object
     456 */
     457function get_post_type_object( $post_type ) {
     458    global $wp_post_types;
     459
     460    if ( empty($wp_post_types[$post_type]) )
     461        return null;
     462
     463    return $wp_post_types[$post_type];
     464}
     465
     466/**
    445467 * Get a list of all registered post type objects.
    446468 *
     
    492514 * Optional $args contents:
    493515 *
     516 * label - A descriptive name for the post type marked for translation. Defaults to $post_type.
     517 * public - Whether posts of this type should be shown in the admin UI. Defaults to true.
    494518 * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true.
     519 * inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
     520 * capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
     521 * hierarchical - Whether the post type is hierarchical. Defaults to false.
    495522 *
    496523 * @package WordPress
     
    508535        $wp_post_types = array();
    509536
    510     $defaults = array('exclude_from_search' => true);
     537    // Args prefixed with an underscore are reserved for internal use.
     538    $defaults = array('label' => false, 'exclude_from_search' => true, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, '_show' => false);
    511539    $args = wp_parse_args($args, $defaults);
     540    $args = (object) $args;
    512541
    513542    $post_type = sanitize_user($post_type, true);
    514     $args['name'] = $post_type;
    515     $wp_post_types[$post_type] = (object) $args;
     543    $args->name = $post_type;
     544
     545    if ( false === $args->label )
     546        $args->label = $post_type;
     547
     548    if ( empty($args->capability_type) ) {
     549        $args->edit_cap = '';
     550        $args->read_cap = '';
     551    } else {
     552        $args->edit_cap = 'edit_' . $args->capability_type;
     553        $args->read_cap = 'read_' . $args->capability_type;
     554    }
     555
     556    if ( !$args->_builtin && $args->public )
     557        $args->_show = true;
     558
     559    $wp_post_types[$post_type] = $args;
     560
     561    return $args;
    516562}
    517563
     
    10191065    $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
    10201066    if ( 'readable' == $perm && is_user_logged_in() ) {
    1021         if ( !current_user_can("read_private_{$type}s") ) {
     1067        $post_type_object = get_post_type_object($type);
     1068        if ( !current_user_can("read_private_{$post_type_object->capability_type}s") ) {
    10221069            $cache_key .= '_' . $perm . '_' . $user->ID;
    10231070            $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
Note: See TracChangeset for help on using the changeset viewer.