<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[15890] trunk/wp-includes:
  Rough first pass on map_meta_cap for custom post types.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15890">15890</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-10-21 14:40:04 +0000 (Thu, 21 Oct 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Rough first pass on map_meta_cap for custom post types. see <a href="http://trac.wordpress.org/ticket/14122">#14122</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludescapabilitiesphp">trunk/wp-includes/capabilities.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludescapabilitiesphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/capabilities.php (15889 => 15890)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/capabilities.php        2010-10-21 14:20:40 UTC (rev 15889)
+++ trunk/wp-includes/capabilities.php        2010-10-21 14:40:04 UTC (rev 15890)
</span><span class="lines">@@ -817,11 +817,12 @@
</span><span class="cx">                         $caps[] = 'edit_users'; // Explicit due to primitive fall through
</span><span class="cx">                 break;
</span><span class="cx">         case 'delete_post':
</span><ins>+        case 'delete_page':
</ins><span class="cx">                 $author_data = get_userdata( $user_id );
</span><span class="cx">                 //echo &quot;post ID: {$args[0]}&lt;br /&gt;&quot;;
</span><span class="cx">                 $post = get_post( $args[0] );
</span><span class="cx">                 $post_type = get_post_type_object( $post-&gt;post_type );
</span><del>-                if ( $post_type &amp;&amp; 'post' != $post_type-&gt;capability_type ) {
</del><ins>+                if ( 'delete_post' == $cap &amp;&amp; $post_type &amp;&amp; 'post' != $post_type-&gt;capability_type &amp;&amp; ! $post_type-&gt;map_meta_cap ) {
</ins><span class="cx">                         $args = array_merge( array( $post_type-&gt;cap-&gt;delete_post, $user_id ), $args );
</span><span class="cx">                         return call_user_func_array( 'map_meta_cap', $args );
</span><span class="cx">                 }
</span><span class="lines">@@ -837,69 +838,34 @@
</span><span class="cx">                 if ( is_object( $post_author_data ) &amp;&amp; $user_id == $post_author_data-&gt;ID ) {
</span><span class="cx">                         // If the post is published...
</span><span class="cx">                         if ( 'publish' == $post-&gt;post_status ) {
</span><del>-                                $caps[] = 'delete_published_posts';
</del><ins>+                                $caps[] = $post_type-&gt;cap-&gt;delete_published_posts;
</ins><span class="cx">                         } elseif ( 'trash' == $post-&gt;post_status ) {
</span><span class="cx">                                 if ('publish' == get_post_meta($post-&gt;ID, '_wp_trash_meta_status', true) )
</span><del>-                                        $caps[] = 'delete_published_posts';
</del><ins>+                                        $caps[] = $post_type-&gt;cap-&gt;delete_published_posts;
</ins><span class="cx">                         } else {
</span><span class="cx">                                 // If the post is draft...
</span><del>-                                $caps[] = 'delete_posts';
</del><ins>+                                $caps[] = $post_type-&gt;cap-&gt;delete_posts;
</ins><span class="cx">                         }
</span><span class="cx">                 } else {
</span><span class="cx">                         // The user is trying to edit someone else's post.
</span><del>-                        $caps[] = 'delete_others_posts';
</del><ins>+                        $caps[] = $post_type-&gt;cap-&gt;delete_others_posts;
</ins><span class="cx">                         // The post is published, extra cap required.
</span><span class="cx">                         if ( 'publish' == $post-&gt;post_status )
</span><del>-                                $caps[] = 'delete_published_posts';
</del><ins>+                                $caps[] = $post_type-&gt;cap-&gt;delete_published_posts;
</ins><span class="cx">                         elseif ( 'private' == $post-&gt;post_status )
</span><del>-                                $caps[] = 'delete_private_posts';
</del><ins>+                                $caps[] = $post_type-&gt;cap-&gt;delete_private_posts;
</ins><span class="cx">                 }
</span><span class="cx">                 break;
</span><del>-        case 'delete_page':
-                $author_data = get_userdata( $user_id );
-                //echo &quot;post ID: {$args[0]}&lt;br /&gt;&quot;;
-                $page = get_page( $args[0] );
-                $page_author_data = get_userdata( $page-&gt;post_author );
-                //echo &quot;current user id : $user_id, page author id: &quot; . $page_author_data-&gt;ID . &quot;&lt;br /&gt;&quot;;
-                // If the user is the author...
-
-                if ('' != $page-&gt;post_author) {
-                        $page_author_data = get_userdata( $page-&gt;post_author );
-                } else {
-                        //No author set yet so default to current user for cap checks
-                        $page_author_data = $author_data;
-                }
-
-                if ( is_object( $page_author_data ) &amp;&amp; $user_id == $page_author_data-&gt;ID ) {
-                        // If the page is published...
-                        if ( $page-&gt;post_status == 'publish' ) {
-                                $caps[] = 'delete_published_pages';
-                        } elseif ( 'trash' == $page-&gt;post_status ) {
-                                if ('publish' == get_post_meta($page-&gt;ID, '_wp_trash_meta_status', true) )
-                                        $caps[] = 'delete_published_pages';
-                        } else {
-                                // If the page is draft...
-                                $caps[] = 'delete_pages';
-                        }
-                } else {
-                        // The user is trying to edit someone else's page.
-                        $caps[] = 'delete_others_pages';
-                        // The page is published, extra cap required.
-                        if ( $page-&gt;post_status == 'publish' )
-                                $caps[] = 'delete_published_pages';
-                        elseif ( $page-&gt;post_status == 'private' )
-                                $caps[] = 'delete_private_pages';
-                }
-                break;
</del><span class="cx">                 // edit_post breaks down to edit_posts, edit_published_posts, or
</span><span class="cx">                 // edit_others_posts
</span><span class="cx">         case 'edit_post':
</span><ins>+        case 'edit_page':
</ins><span class="cx">                 $author_data = get_userdata( $user_id );
</span><span class="cx">                 //echo &quot;post ID: {$args[0]}&lt;br /&gt;&quot;;
</span><span class="cx">                 $post = get_post( $args[0] );
</span><span class="cx"> 
</span><span class="cx">                 $post_type = get_post_type_object( $post-&gt;post_type );
</span><del>-                if ( $post_type &amp;&amp; 'post' != $post_type-&gt;capability_type ) {
</del><ins>+                if ( 'edit_post' == $cap &amp;&amp; $post_type &amp;&amp; 'post' != $post_type-&gt;capability_type &amp;&amp; ! $post_type-&gt;map_meta_cap ) {
</ins><span class="cx">                         $args = array_merge( array( $post_type-&gt;cap-&gt;edit_post, $user_id ), $args );
</span><span class="cx">                         return call_user_func_array( 'map_meta_cap', $args );
</span><span class="cx">                 }
</span><span class="lines">@@ -909,87 +875,45 @@
</span><span class="cx">                 if ( is_object( $post_author_data ) &amp;&amp; $user_id == $post_author_data-&gt;ID ) {
</span><span class="cx">                         // If the post is published...
</span><span class="cx">                         if ( 'publish' == $post-&gt;post_status ) {
</span><del>-                                $caps[] = 'edit_published_posts';
</del><ins>+                                $caps[] = $post_type-&gt;cap-&gt;edit_published_posts;
</ins><span class="cx">                         } elseif ( 'trash' == $post-&gt;post_status ) {
</span><span class="cx">                                 if ('publish' == get_post_meta($post-&gt;ID, '_wp_trash_meta_status', true) )
</span><del>-                                        $caps[] = 'edit_published_posts';
</del><ins>+                                        $caps[] = $post_type-&gt;cap-&gt;edit_published_posts;
</ins><span class="cx">                         } else {
</span><span class="cx">                                 // If the post is draft...
</span><del>-                                $caps[] = 'edit_posts';
</del><ins>+                                $caps[] = $post_type-&gt;cap-&gt;edit_posts;
</ins><span class="cx">                         }
</span><span class="cx">                 } else {
</span><span class="cx">                         // The user is trying to edit someone else's post.
</span><del>-                        $caps[] = 'edit_others_posts';
</del><ins>+                        $caps[] = $post_type-&gt;cap-&gt;edit_others_posts;
</ins><span class="cx">                         // The post is published, extra cap required.
</span><span class="cx">                         if ( 'publish' == $post-&gt;post_status )
</span><del>-                                $caps[] = 'edit_published_posts';
</del><ins>+                                $caps[] = $post_type-&gt;cap-&gt;edit_published_posts;
</ins><span class="cx">                         elseif ( 'private' == $post-&gt;post_status )
</span><del>-                                $caps[] = 'edit_private_posts';
</del><ins>+                                $caps[] = $post_type-&gt;cap-&gt;edit_private_posts;
</ins><span class="cx">                 }
</span><span class="cx">                 break;
</span><del>-        case 'edit_page':
-                $author_data = get_userdata( $user_id );
-                //echo &quot;post ID: {$args[0]}&lt;br /&gt;&quot;;
-                $page = get_page( $args[0] );
-                $page_author_data = get_userdata( $page-&gt;post_author );
-                //echo &quot;current user id : $user_id, page author id: &quot; . $page_author_data-&gt;ID . &quot;&lt;br /&gt;&quot;;
-                // If the user is the author...
-                if ( is_object( $page_author_data ) &amp;&amp; $user_id == $page_author_data-&gt;ID ) {
-                        // If the page is published...
-                        if ( 'publish' == $page-&gt;post_status ) {
-                                $caps[] = 'edit_published_pages';
-                        } elseif ( 'trash' == $page-&gt;post_status ) {
-                                if ('publish' == get_post_meta($page-&gt;ID, '_wp_trash_meta_status', true) )
-                                        $caps[] = 'edit_published_pages';
-                        } else {
-                                // If the page is draft...
-                                $caps[] = 'edit_pages';
-                        }
-                } else {
-                        // The user is trying to edit someone else's page.
-                        $caps[] = 'edit_others_pages';
-                        // The page is published, extra cap required.
-                        if ( 'publish' == $page-&gt;post_status )
-                                $caps[] = 'edit_published_pages';
-                        elseif ( 'private' == $page-&gt;post_status )
-                                $caps[] = 'edit_private_pages';
-                }
-                break;
</del><span class="cx">         case 'read_post':
</span><ins>+        case 'read_page':
</ins><span class="cx">                 $post = get_post( $args[0] );
</span><span class="cx">                 $post_type = get_post_type_object( $post-&gt;post_type );
</span><del>-                if ( $post_type &amp;&amp; 'post' != $post_type-&gt;capability_type ) {
</del><ins>+                if ( 'read_post' == $cap &amp;&amp; $post_type &amp;&amp; 'post' != $post_type-&gt;capability_type &amp;&amp; ! $post_type-&gt;map_meta_cap ) {
</ins><span class="cx">                         $args = array_merge( array( $post_type-&gt;cap-&gt;read_post, $user_id ), $args );
</span><span class="cx">                         return call_user_func_array( 'map_meta_cap', $args );
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 if ( 'private' != $post-&gt;post_status ) {
</span><del>-                        $caps[] = 'read';
</del><ins>+                        $caps[] = $post_type-&gt;cap-&gt;read;
</ins><span class="cx">                         break;
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 $author_data = get_userdata( $user_id );
</span><span class="cx">                 $post_author_data = get_userdata( $post-&gt;post_author );
</span><span class="cx">                 if ( is_object( $post_author_data ) &amp;&amp; $user_id == $post_author_data-&gt;ID )
</span><del>-                        $caps[] = 'read';
</del><ins>+                        $caps[] = $post_type-&gt;cap-&gt;read;
</ins><span class="cx">                 else
</span><del>-                        $caps[] = 'read_private_posts';
</del><ins>+                        $caps[] = $post_type-&gt;cap-&gt;read_private_posts;
</ins><span class="cx">                 break;
</span><del>-        case 'read_page':
-                $page = get_page( $args[0] );
-
-                if ( 'private' != $page-&gt;post_status ) {
-                        $caps[] = 'read';
-                        break;
-                }
-
-                $author_data = get_userdata( $user_id );
-                $page_author_data = get_userdata( $page-&gt;post_author );
-                if ( is_object( $page_author_data ) &amp;&amp; $user_id == $page_author_data-&gt;ID )
-                        $caps[] = 'read';
-                else
-                        $caps[] = 'read_private_pages';
-                break;
</del><span class="cx">         case 'edit_comment':
</span><span class="cx">                 $comment = get_comment( $args[0] );
</span><span class="cx">                 $post = get_post( $comment-&gt;comment_post_ID );
</span><span class="lines">@@ -1050,6 +974,13 @@
</span><span class="cx">                         $caps[] = $cap;
</span><span class="cx">                 break;
</span><span class="cx">         default:
</span><ins>+                // Handle meta capabilities for custom post types.
+                $post_type_meta_caps = _post_type_meta_capabilities();
+                if ( isset( $post_type_meta_caps[ $cap ] ) ) {
+                        $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
+                        return call_user_func_array( 'map_meta_cap', $args );
+                }
+
</ins><span class="cx">                 // If no meta caps match, return the original cap.
</span><span class="cx">                 $caps[] = $cap;
</span><span class="cx">         }
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (15889 => 15890)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2010-10-21 14:20:40 UTC (rev 15889)
+++ trunk/wp-includes/post.php        2010-10-21 14:40:04 UTC (rev 15890)
</span><span class="lines">@@ -20,6 +20,7 @@
</span><span class="cx">                 '_builtin' =&gt; true, /* internal use only. don't use this when registering your own post type. */
</span><span class="cx">                 '_edit_link' =&gt; 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
</span><span class="cx">                 'capability_type' =&gt; 'post',
</span><ins>+                'map_meta_cap' =&gt; true,
</ins><span class="cx">                 'hierarchical' =&gt; false,
</span><span class="cx">                 'rewrite' =&gt; false,
</span><span class="cx">                 'query_var' =&gt; false,
</span><span class="lines">@@ -31,6 +32,7 @@
</span><span class="cx">                 '_builtin' =&gt; true, /* internal use only. don't use this when registering your own post type. */
</span><span class="cx">                 '_edit_link' =&gt; 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
</span><span class="cx">                 'capability_type' =&gt; 'page',
</span><ins>+                'map_meta_cap' =&gt; true,
</ins><span class="cx">                 'hierarchical' =&gt; true,
</span><span class="cx">                 'rewrite' =&gt; false,
</span><span class="cx">                 'query_var' =&gt; false,
</span><span class="lines">@@ -836,7 +838,8 @@
</span><span class="cx">  * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
</span><span class="cx">  * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
</span><span class="cx">  * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to &quot;post&quot;.
</span><del>- * - 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.
</del><ins>+ * - 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.
+ * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
</ins><span class="cx">  * - hierarchical - Whether the post type is hierarchical. Defaults to false.
</span><span class="cx">  * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
</span><span class="cx">  * - register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form.  Do remove_meta_box() and add_meta_box() calls in the callback.
</span><span class="lines">@@ -866,7 +869,8 @@
</span><span class="cx">         // Args prefixed with an underscore are reserved for internal use.
</span><span class="cx">         $defaults = array(
</span><span class="cx">                 'labels' =&gt; array(), 'description' =&gt; '', 'publicly_queryable' =&gt; null, 'exclude_from_search' =&gt; null,
</span><del>-                '_builtin' =&gt; false, '_edit_link' =&gt; 'post.php?post=%d', 'capability_type' =&gt; 'post', 'capabilities' =&gt; array(), 'hierarchical' =&gt; false,
</del><ins>+                'capability_type' =&gt; 'post', 'capabilities' =&gt; array(), 'map_meta_cap' =&gt; false,
+                '_builtin' =&gt; false, '_edit_link' =&gt; 'post.php?post=%d', 'hierarchical' =&gt; false,
</ins><span class="cx">                 'public' =&gt; false, 'rewrite' =&gt; true, 'query_var' =&gt; true, 'supports' =&gt; array(), 'register_meta_box_cb' =&gt; null,
</span><span class="cx">                 'taxonomies' =&gt; array(), 'show_ui' =&gt; null, 'menu_position' =&gt; null, 'menu_icon' =&gt; null,
</span><span class="cx">                 'permalink_epmask' =&gt; EP_PERMALINK, 'can_export' =&gt; true, 'show_in_nav_menus' =&gt; null, 'show_in_menu' =&gt; null,
</span><span class="lines">@@ -978,25 +982,63 @@
</span><span class="cx">  * - read_private_posts - The capability that controls reading private posts. Defaults to &quot;read_private . $capability_type . s&quot; (read_private_posts).
</span><span class="cx">  * - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to &quot;delete_ . $capability_type&quot; (delete_post).
</span><span class="cx">  *
</span><ins>+ * @see map_meta_cap()
</ins><span class="cx">  * @since 3.0.0
</span><ins>+ *
</ins><span class="cx">  * @param object $args
</span><span class="cx">  * @return object object with all the capabilities as member variables
</span><span class="cx">  */
</span><span class="cx"> function get_post_type_capabilities( $args ) {
</span><del>-        $defaults = array(
</del><ins>+        global $_post_type_meta_capabilities;
+
+        $default_capabilities = array(
+                // Meta capabilities are generally mapped to primitive capabilities depending on the context
+                // (which would be the post being edited/deleted/read), instead of granted to users or roles:
</ins><span class="cx">                 'edit_post'          =&gt; 'edit_'         . $args-&gt;capability_type,
</span><ins>+                'read_post'          =&gt; 'read_'         . $args-&gt;capability_type,
+                'delete_post'        =&gt; 'delete_'       . $args-&gt;capability_type,
+                // Primitive capabilities that are used outside of map_meta_cap():
</ins><span class="cx">                 'edit_posts'         =&gt; 'edit_'         . $args-&gt;capability_type . 's',
</span><span class="cx">                 'edit_others_posts'  =&gt; 'edit_others_'  . $args-&gt;capability_type . 's',
</span><span class="cx">                 'publish_posts'      =&gt; 'publish_'      . $args-&gt;capability_type . 's',
</span><del>-                'read_post'          =&gt; 'read_'         . $args-&gt;capability_type,
</del><span class="cx">                 'read_private_posts' =&gt; 'read_private_' . $args-&gt;capability_type . 's',
</span><del>-                'delete_post'        =&gt; 'delete_'       . $args-&gt;capability_type,
</del><span class="cx">         );
</span><del>-        $labels = array_merge( $defaults, $args-&gt;capabilities );
-        return (object) $labels;
</del><ins>+        // Primitive capabilities that are used within map_meta_cap():
+        if ( $args-&gt;map_meta_cap ) {
+                $default_capabilities_for_mapping = array(
+                        'read'                   =&gt; 'read',
+                        'delete_posts'           =&gt; 'delete_'           . $args-&gt;capability_type . 's',
+                        'delete_private_posts'   =&gt; 'delete_private_'   . $args-&gt;capability_type . 's',
+                        'delete_published_posts' =&gt; 'delete_published_' . $args-&gt;capability_type . 's', 
+                        'delete_others_posts'    =&gt; 'delete_others_'    . $args-&gt;capability_type . 's',
+                        'edit_private_posts'     =&gt; 'edit_private_'     . $args-&gt;capability_type . 's',
+                        'edit_published_posts'   =&gt; 'edit_published_'   . $args-&gt;capability_type . 's',
+                );
+                $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
+        }
+        $capabilities = array_merge( $default_capabilities, $args-&gt;capabilities );
+        if ( $args-&gt;map_meta_cap )
+                _post_type_meta_capabilities( $capabilities );
+        return (object) $capabilities;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Stores or returns a list of post type meta caps for map_meta_cap().
+ *
+ * @since 3.1.0
+ * @access private
+ */
+function _post_type_meta_capabilities( $capabilities = null ) {
+        static $meta_caps = array();
+        if ( null === $capabilities )
+                return $meta_caps;
+        foreach ( $capabilities as $core =&gt; $custom ) {
+                if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
+                        $meta_caps[ $custom ] = $core;
+        }
+}
+
+/**
</ins><span class="cx">  * Builds an object with all post type labels out of a post type object
</span><span class="cx">  *
</span><span class="cx">  * Accepted keys of the label array in the post type object:
</span></span></pre>
</div>
</div>

</body>
</html>