Make WordPress Core


Ignore:
Timestamp:
05/12/2010 08:45:18 PM (14 years ago)
Author:
nacin
Message:

Move post type object capabilities to a 'cap' object. Allow them to be initialized via the 'capabilities' key (an array) when registering support for the post type. Caps are now referred to by the name of the cap as if it was a post, i.e. ->cap->edit_post. see #13358.

File:
1 edited

Legend:

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

    r14584 r14585  
    775775 * - inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
    776776 * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
    777  * - edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post).
    778  * - edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
    779  * - edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
    780  * - publish_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
    781  * - read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post).
    782  * - delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post).
     777 * - capabilities - Array of capabilities for this post type. You can see accepted values in {@link get_post_type_capabilities()}. By default the capability_type is used to construct capabilities.
    783778 * - hierarchical - Whether the post type is hierarchical. Defaults to false.
    784779 * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
     
    803798    $defaults = array(
    804799        'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
    805         '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false,
     800        '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false,
    806801        'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
    807802        'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
     
    828823    if ( empty($args->capability_type) )
    829824        $args->capability_type = 'post';
    830     if ( empty($args->edit_cap) )
    831         $args->edit_cap = 'edit_' . $args->capability_type;
    832     if ( empty($args->edit_type_cap) )
    833         $args->edit_type_cap = 'edit_' . $args->capability_type . 's';
    834     if ( empty($args->edit_others_cap) )
    835         $args->edit_others_cap = 'edit_others_' . $args->capability_type . 's';
    836     if ( empty($args->publish_cap) )
    837         $args->publish_cap = 'publish_' . $args->capability_type . 's';
    838     if ( empty($args->read_cap) )
    839         $args->read_cap = 'read_' . $args->capability_type;
    840     if ( empty($args->read_private_cap) )
    841         $args->read_private_cap = 'read_private_' . $args->capability_type . 's';
    842     if ( empty($args->delete_cap) )
    843         $args->delete_cap = 'delete_' . $args->capability_type;
     825
     826    $args->cap = get_post_type_capabilities( $args );
    844827
    845828    if ( ! empty($args->supports) ) {
     
    891874
    892875    return $args;
     876}
     877
     878/**
     879 * Builds an object with all post type capabilities out of a post type object
     880 *
     881 * Accepted keys of the capabilities array in the post type object:
     882 * - edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post).
     883 * - edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
     884 * - edit_others_posts - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
     885 * - publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
     886 * - read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post).
     887 * - read_private_posts - The capability that controls reading private posts. Defaults to "read_ . $capability_type . s" (read_private_posts).
     888 * - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post).
     889 *
     890 * @since 3.0.0
     891 * @param object $args
     892 * @return object object with all the capabilities as member variables
     893 */
     894function get_post_type_capabilities( $args ) {
     895    $defaults = array(
     896        'edit_post'          => 'edit_'         . $args->capabilities['capability_type'],
     897        'edit_posts'         => 'edit_'         . $args->capabilities['capability_type'] . 's',
     898        'edit_others_posts'  => 'edit_others_'  . $args->capabilities['capability_type'] . 's',
     899        'publish_posts'      => 'publish_'      . $args->capabilities['capability_type'] . 's',
     900        'read_post'          => 'edit_'         . $args->capabilities['capability_type'],
     901        'read_private_posts' => 'read_private_' . $args->capabilities['capability_type'] . 's',
     902        'delete_post'        => 'delete_'       . $args->capabilities['capability_type'],
     903    );
     904    $labels = array_merge( $defaults, $args->capabilities );
     905    return (object) $labels;
    893906}
    894907
     
    15261539    if ( 'readable' == $perm && is_user_logged_in() ) {
    15271540        $post_type_object = get_post_type_object($type);
    1528         if ( !current_user_can( $post_type_object->read_private_cap ) ) {
     1541        if ( !current_user_can( $post_type_object->cap->read_private_posts ) ) {
    15291542            $cache_key .= '_' . $perm . '_' . $user->ID;
    15301543            $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
Note: See TracChangeset for help on using the changeset viewer.