<!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>[15934] trunk/wp-includes/post.php: Allow capability_type to be an array,
for odd plural situations such as story/storys/stories.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15934">15934</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-10-23 18:03:26 +0000 (Sat, 23 Oct 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Allow capability_type to be an array, for odd plural situations such as story/storys/stories. After registration it reverts to a singular string. Lots of documentation for meta capabilities and post types, also some cleanups for register_post_type documentation. see <a href="http://trac.wordpress.org/ticket/14122">#14122</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (15933 => 15934)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2010-10-23 17:51:18 UTC (rev 15933)
+++ trunk/wp-includes/post.php        2010-10-23 18:03:26 UTC (rev 15934)
</span><span class="lines">@@ -822,37 +822,50 @@
</span><span class="cx"> /**
</span><span class="cx"> * Register a post type. Do not use before init.
</span><span class="cx"> *
</span><del>- * A simple function for creating or modifying a post type based on the
</del><ins>+ * A function for creating or modifying a post type based on the
</ins><span class="cx"> * parameters given. The function will accept an array (second optional
</span><span class="cx"> * parameter), along with a string for the post type name.
</span><span class="cx"> *
</span><del>- *
</del><span class="cx"> * Optional $args contents:
</span><span class="cx"> *
</span><span class="cx"> * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
</span><span class="cx"> * - description - A short descriptive summary of what the post type is. Defaults to blank.
</span><span class="cx"> * - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
</span><del>- * - exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
- * - publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as.
- * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
- * - show_in_menu - Where to show the post type in the admin menu. True for a top level menu, false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'. show_ui must be true.
</del><ins>+ * - exclude_from_search - Whether to exclude posts with this post type from search results.
+ * Defaults to true if the type is not public, false if the type is public.
+ * - publicly_queryable - Whether post_type queries can be performed from the front page.
+ * Defaults to whatever public is set as.
+ * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true
+ * if the type is public, false if the type is not public.
+ * - show_in_menu - Where to show the post type in the admin menu. True for a top level menu,
+ * false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'.
+ * show_ui must be true.
</ins><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><del>- * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
- * - 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.
</del><ins>+ * - capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to 'post'.
+ * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
+ * capabilities, e.g. array('story', 'stories').
+ * - capabilities - Array of capabilities for this post type. By default the capability_type is used
+ * as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}.
</ins><span class="cx"> * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
</span><span class="cx"> * - hierarchical - Whether the post type is hierarchical. Defaults to false.
</span><del>- * - supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
- * - 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.
- * - taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
- * - labels - An array of labels for this post type. You can see accepted values in {@link get_post_type_labels()}. By default post labels are used for non-hierarchical types and page labels for hierarchical ones.
</del><ins>+ * - supports - An alias for calling add_post_type_support() directly. See {@link add_post_type_support()}
+ * for documentation. Defaults to none.
+ * - 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.
+ * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
+ * Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or
+ * register_taxonomy_for_object_type().
+ * - labels - An array of labels for this post type. By default post labels are used for non-hierarchical
+ * types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}.
</ins><span class="cx"> * - permalink_epmask - The default rewrite endpoint bitmasks.
</span><del>- * - rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize permastruct; default will use $post_type as slug.
</del><ins>+ * - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct;
+ * default will use $post_type as slug. Other options include 'with_front' and 'feeds'.
</ins><span class="cx"> * - query_var - false to prevent queries, or string to value of the query var to use for this post type
</span><span class="cx"> * - can_export - true allows this post type to be exported.
</span><span class="cx"> * - show_in_nav_menus - true makes this post type available for selection in navigation menus.
</span><del>- * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
- * - _edit_link - URL segement to use for edit link of this post type. Set to 'post.php?post=%d'. THIS IS FOR INTERNAL USE ONLY!
</del><ins>+ * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
+ * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
</ins><span class="cx"> *
</span><span class="cx"> * @since 2.9.0
</span><span class="cx"> * @uses $wp_post_types Inserts new post type object into the list
</span><span class="lines">@@ -908,6 +921,9 @@
</span><span class="cx">         $args->cap = get_post_type_capabilities( $args );
</span><span class="cx">         unset($args->capabilities);
</span><span class="cx">
</span><ins>+        if ( is_array( $args->capability_type ) )
+                $args->capability_type = $args->capability_type[0];
+
</ins><span class="cx">         if ( ! empty($args->supports) ) {
</span><span class="cx">                 add_post_type_support($post_type, $args->supports);
</span><span class="cx">                 unset($args->supports);
</span><span class="lines">@@ -974,45 +990,85 @@
</span><span class="cx"> /**
</span><span class="cx"> * Builds an object with all post type capabilities out of a post type object
</span><span class="cx"> *
</span><del>- * Accepted keys of the capabilities array in the post type object:
- * - edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_ . $capability_type" (edit_post).
- * - edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
- * - 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).
- * - publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
- * - read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_ . $capability_type" (read_post).
- * - read_private_posts - The capability that controls reading private posts. Defaults to "read_private . $capability_type . s" (read_private_posts).
- * - delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type" (delete_post).
</del><ins>+ * Post type capabilities use the 'capability_type' argument as a base, if the
+ * capability is not set in the 'capabilities' argument array or if the
+ * 'capabilities' argument is not supplied.
</ins><span class="cx"> *
</span><ins>+ * The capability_type argument can optionally be registered as an array, with
+ * the first value being singular and the second plural, e.g. array('story, 'stories')
+ * Otherwise, an 's' will be added to the value for the plural form. After
+ * registration, capability_type will always be a string of the singular value.
+ *
+ * By default, seven keys are accepted as part of the capabilities array:
+ *
+ * - edit_post, read_post, and delete_post are meta capabilities, which are then
+ * generally mapped to corresponding primitive capabilities depending on the
+ * context, which would be the post being edited/read/deleted and the user or
+ * role being checked. Thus these capabilities would generally not be granted
+ * directly to users or roles.
+ *
+ * - edit_posts - Controls whether objects of this post type can be edited.
+ * - edit_others_posts - Controls whether objects of this type owned by other users
+ * can be edited. If the post type does not support an author, then this will
+ * behave like edit_posts.
+ * - publish_posts - Controls publishing objects of this post type.
+ * - read_private_posts - Controls whether private objects can be read.
+
+ * These four primitive capabilities are checked in core in various locations.
+ * There are also seven other primitive capabilities which are not referenced
+ * directly in core, except in map_meta_cap(), which takes the three aforementioned
+ * meta capabilities and translates them into one or more primitive capabilities
+ * that must then be checked against the user or role, depending on the context.
+ *
+ * - read - Controls whether objects of this post type can be read.
+ * - delete_posts - Controls whether objects of this post type can be deleted.
+ * - delete_private_posts - Controls whether private objects can be deleted.
+ * - delete_published_posts - Controls whether published objects can be deleted.
+ * - delete_others_posts - Controls whether objects owned by other users can be
+ * can be deleted. If the post type does not support an author, then this will
+ * behave like delete_posts.
+ * - edit_private_posts - Controls whether private objects can be edited.
+ * - edit_published_posts - Controls whether published objects can be deleted.
+ *
+ * These additional capabilities are only used in map_meta_cap(). Thus, they are
+ * only assigned by default if the post type is registered with the 'map_meta_cap'
+ * argument set to true (default is false).
+ *
</ins><span class="cx"> * @see map_meta_cap()
</span><span class="cx"> * @since 3.0.0
</span><span class="cx"> *
</span><del>- * @param object $args
</del><ins>+ * @param object $args Post type registration arguments
</ins><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><ins>+        if ( ! is_array( $args->capability_type ) )
+                $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
+
+        // Singular base for meta capabilities, plural base for primitive capabilities.
+        list( $singular_base, $plural_base ) = $args->capability_type;
+
</ins><span class="cx">         $default_capabilities = array(
</span><del>-                // 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:
-                'edit_post' => 'edit_' . $args->capability_type,
-                'read_post' => 'read_' . $args->capability_type,
-                'delete_post' => 'delete_' . $args->capability_type,
-                // Primitive capabilities that are used outside of map_meta_cap():
-                'edit_posts' => 'edit_' . $args->capability_type . 's',
-                'edit_others_posts' => 'edit_others_' . $args->capability_type . 's',
-                'publish_posts' => 'publish_' . $args->capability_type . 's',
-                'read_private_posts' => 'read_private_' . $args->capability_type . 's',
</del><ins>+                // Meta capabilities
+                'edit_post' => 'edit_' . $singular_base,
+                'read_post' => 'read_' . $singular_base,
+                'delete_post' => 'delete_' . $singular_base,
+                // Primitive capabilities used outside of map_meta_cap():
+                'edit_posts' => 'edit_' . $plural_base,
+                'edit_others_posts' => 'edit_others_' . $plural_base,
+                'publish_posts' => 'publish_' . $plural_base,
+                'read_private_posts' => 'read_private_' . $plural_base,
</ins><span class="cx">         );
</span><span class="cx">
</span><del>-        // Primitive capabilities that are used within map_meta_cap():
</del><ins>+        // Primitive capabilities used within map_meta_cap():
</ins><span class="cx">         if ( $args->map_meta_cap ) {
</span><span class="cx">                 $default_capabilities_for_mapping = array(
</span><span class="cx">                         'read' => 'read',
</span><del>-                        'delete_posts' => 'delete_' . $args->capability_type . 's',
-                        'delete_private_posts' => 'delete_private_' . $args->capability_type . 's',
-                        'delete_published_posts' => 'delete_published_' . $args->capability_type . 's',
-                        'delete_others_posts' => 'delete_others_' . $args->capability_type . 's',
-                        'edit_private_posts' => 'edit_private_' . $args->capability_type . 's',
-                        'edit_published_posts' => 'edit_published_' . $args->capability_type . 's',
</del><ins>+                        'delete_posts' => 'delete_' . $plural_base,
+                        'delete_private_posts' => 'delete_private_' . $plural_base,
+                        'delete_published_posts' => 'delete_published_' . $plural_base,
+                        'delete_others_posts' => 'delete_others_' . $plural_base,
+                        'edit_private_posts' => 'edit_private_' . $plural_base,
+                        'edit_published_posts' => 'edit_published_' . $plural_base,
</ins><span class="cx">                 );
</span><span class="cx">                 $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
</span><span class="cx">         }
</span></span></pre>
</div>
</div>
</body>
</html>