Make WordPress Core


Ignore:
Timestamp:
10/21/2010 02:40:04 PM (13 years ago)
Author:
nacin
Message:

Rough first pass on map_meta_cap for custom post types. see #14122.

File:
1 edited

Legend:

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

    r15852 r15890  
    2121        '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    2222        'capability_type' => 'post',
     23        'map_meta_cap' => true,
    2324        'hierarchical' => false,
    2425        'rewrite' => false,
     
    3233        '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    3334        'capability_type' => 'page',
     35        'map_meta_cap' => true,
    3436        'hierarchical' => true,
    3537        'rewrite' => false,
     
    837839 * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
    838840 * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
    839  * - 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.
     841 * - 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 as a base to construct capabilities.
     842 * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
    840843 * - hierarchical - Whether the post type is hierarchical. Defaults to false.
    841844 * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
     
    867870    $defaults = array(
    868871        'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
    869         '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'capabilities' => array(), 'hierarchical' => false,
     872        'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => false,
     873        '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
    870874        'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null,
    871875        'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
     
    979983 * - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type" (delete_post).
    980984 *
     985 * @see map_meta_cap()
    981986 * @since 3.0.0
     987 *
    982988 * @param object $args
    983989 * @return object object with all the capabilities as member variables
    984990 */
    985991function get_post_type_capabilities( $args ) {
    986     $defaults = array(
     992    global $_post_type_meta_capabilities;
     993
     994    $default_capabilities = array(
     995        // Meta capabilities are generally mapped to primitive capabilities depending on the context
     996        // (which would be the post being edited/deleted/read), instead of granted to users or roles:
    987997        'edit_post'          => 'edit_'         . $args->capability_type,
     998        'read_post'          => 'read_'         . $args->capability_type,
     999        'delete_post'        => 'delete_'       . $args->capability_type,
     1000        // Primitive capabilities that are used outside of map_meta_cap():
    9881001        'edit_posts'         => 'edit_'         . $args->capability_type . 's',
    9891002        'edit_others_posts'  => 'edit_others_'  . $args->capability_type . 's',
    9901003        'publish_posts'      => 'publish_'      . $args->capability_type . 's',
    991         'read_post'          => 'read_'         . $args->capability_type,
    9921004        'read_private_posts' => 'read_private_' . $args->capability_type . 's',
    993         'delete_post'        => 'delete_'       . $args->capability_type,
    9941005    );
    995     $labels = array_merge( $defaults, $args->capabilities );
    996     return (object) $labels;
     1006    // Primitive capabilities that are used within map_meta_cap():
     1007    if ( $args->map_meta_cap ) {
     1008        $default_capabilities_for_mapping = array(
     1009            'read'                   => 'read',
     1010            'delete_posts'           => 'delete_'           . $args->capability_type . 's',
     1011            'delete_private_posts'   => 'delete_private_'   . $args->capability_type . 's',
     1012            'delete_published_posts' => 'delete_published_' . $args->capability_type . 's',
     1013            'delete_others_posts'    => 'delete_others_'    . $args->capability_type . 's',
     1014            'edit_private_posts'     => 'edit_private_'     . $args->capability_type . 's',
     1015            'edit_published_posts'   => 'edit_published_'   . $args->capability_type . 's',
     1016        );
     1017        $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
     1018    }
     1019    $capabilities = array_merge( $default_capabilities, $args->capabilities );
     1020    if ( $args->map_meta_cap )
     1021        _post_type_meta_capabilities( $capabilities );
     1022    return (object) $capabilities;
     1023}
     1024
     1025/**
     1026 * Stores or returns a list of post type meta caps for map_meta_cap().
     1027 *
     1028 * @since 3.1.0
     1029 * @access private
     1030 */
     1031function _post_type_meta_capabilities( $capabilities = null ) {
     1032    static $meta_caps = array();
     1033    if ( null === $capabilities )
     1034        return $meta_caps;
     1035    foreach ( $capabilities as $core => $custom ) {
     1036        if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
     1037            $meta_caps[ $custom ] = $core;
     1038    }
    9971039}
    9981040
Note: See TracChangeset for help on using the changeset viewer.