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

git-svn-id: http://svn.automattic.com/wordpress/trunk@12597 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan
2010-01-04 16:58:43 +00:00
parent e51341dc1d
commit da89de4958
20 changed files with 292 additions and 102 deletions

View File

@@ -779,8 +779,9 @@ function map_meta_cap( $cap, $user_id ) {
$author_data = get_userdata( $user_id );
//echo "post ID: {$args[0]}<br />";
$post = get_post( $args[0] );
if ( 'page' == $post->post_type ) {
$args = array_merge( array( 'delete_page', $user_id ), $args );
$post_type = get_post_type_object( $post->post_type );
if ( $post_type && 'post' != $post_type->capability_type ) {
$args = array_merge( array( 'delete_' . $post_type->capability_type, $user_id ), $args );
return call_user_func_array( 'map_meta_cap', $args );
}
@@ -855,8 +856,9 @@ function map_meta_cap( $cap, $user_id ) {
$author_data = get_userdata( $user_id );
//echo "post ID: {$args[0]}<br />";
$post = get_post( $args[0] );
if ( 'page' == $post->post_type ) {
$args = array_merge( array( 'edit_page', $user_id ), $args );
$post_type = get_post_type_object( $post->post_type );
if ( $post_type && 'post' != $post_type->capability_type ) {
$args = array_merge( array( 'edit_' . $post_type->capability_type, $user_id ), $args );
return call_user_func_array( 'map_meta_cap', $args );
}
$post_author_data = get_userdata( $post->post_author );
@@ -913,8 +915,9 @@ function map_meta_cap( $cap, $user_id ) {
break;
case 'read_post':
$post = get_post( $args[0] );
if ( 'page' == $post->post_type ) {
$args = array_merge( array( 'read_page', $user_id ), $args );
$post_type = get_post_type_object( $post->post_type );
if ( $post_type && 'post' != $post_type->capability_type ) {
$args = array_merge( array( 'read_' . $post_type->capability_type, $user_id ), $args );
return call_user_func_array( 'map_meta_cap', $args );
}

View File

@@ -678,39 +678,18 @@ function get_edit_post_link( $id = 0, $context = 'display' ) {
return;
if ( 'display' == $context )
$action = 'action=edit&amp;';
$action = '&amp;action=edit';
else
$action = 'action=edit&';
$action = '&action=edit';
switch ( $post->post_type ) :
case 'page' :
if ( !current_user_can( 'edit_page', $post->ID ) )
return;
$file = 'page';
$var = 'post';
break;
case 'attachment' :
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
$file = 'media';
$var = 'attachment_id';
break;
case 'revision' :
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
$file = 'revision';
$var = 'revision';
$action = '';
break;
default :
if ( !current_user_can( 'edit_post', $post->ID ) )
return apply_filters( 'get_edit_post_link', '', $post->ID, $context );
$file = 'post';
$var = 'post';
break;
endswitch;
$post_type_object = get_post_type_object( $post->post_type );
if ( !$post_type_object )
return;
return apply_filters( 'get_edit_post_link', admin_url("$file.php?{$action}$var=$post->ID"), $post->ID, $context );
if ( !current_user_can( $post_type_object->edit_cap, $post->ID ) )
return;
return apply_filters( 'get_edit_post_link', admin_url( sprintf($post_type_object->_edit_link . $action, $post->ID) ), $post->ID, $context );
}
/**

View File

@@ -15,10 +15,10 @@
* Creates the initial post types when 'init' action is fired.
*/
function create_initial_post_types() {
register_post_type( 'post', array('exclude_from_search' => false) );
register_post_type( 'page', array('exclude_from_search' => false) );
register_post_type( 'attachment', array('exclude_from_search' => false) );
register_post_type( 'revision', array('exclude_from_search' => true) );
register_post_type( 'post', array('label' => __('Posts'), 'exclude_from_search' => false, '_builtin' => true, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false) );
register_post_type( 'page', array('label' => __('Pages'),'exclude_from_search' => false, '_builtin' => true, '_edit_link' => 'page.php?post=%d', 'capability_type' => 'page', 'hierarchical' => true) );
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) );
register_post_type( 'revision', array('label' => __('Revisions'),'exclude_from_search' => true, '_builtin' => true, '_edit_link' => 'revision.php?revision=%d', 'capability_type' => 'post', 'hierarchical' => false) );
}
add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
@@ -441,6 +441,28 @@ function get_post_type($post = false) {
return false;
}
/**
* Retrieve a post type object by name
*
* @package WordPress
* @subpackage Post
* @since 3.0
* @uses $wp_post_types
* @see register_post_type
* @see get_post_types
*
* @param string $post_type The name of a registered post type
* @return object A post type object
*/
function get_post_type_object( $post_type ) {
global $wp_post_types;
if ( empty($wp_post_types[$post_type]) )
return null;
return $wp_post_types[$post_type];
}
/**
* Get a list of all registered post type objects.
*
@@ -491,7 +513,12 @@ function get_post_types( $args = array(), $output = 'names' ) {
*
* Optional $args contents:
*
* label - A descriptive name for the post type marked for translation. Defaults to $post_type.
* public - Whether posts of this type should be shown in the admin UI. Defaults to true.
* exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true.
* inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
* capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
* hierarchical - Whether the post type is hierarchical. Defaults to false.
*
* @package WordPress
* @subpackage Post
@@ -507,12 +534,31 @@ function register_post_type($post_type, $args = array()) {
if (!is_array($wp_post_types))
$wp_post_types = array();
$defaults = array('exclude_from_search' => true);
// Args prefixed with an underscore are reserved for internal use.
$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);
$args = wp_parse_args($args, $defaults);
$args = (object) $args;
$post_type = sanitize_user($post_type, true);
$args['name'] = $post_type;
$wp_post_types[$post_type] = (object) $args;
$args->name = $post_type;
if ( false === $args->label )
$args->label = $post_type;
if ( empty($args->capability_type) ) {
$args->edit_cap = '';
$args->read_cap = '';
} else {
$args->edit_cap = 'edit_' . $args->capability_type;
$args->read_cap = 'read_' . $args->capability_type;
}
if ( !$args->_builtin && $args->public )
$args->_show = true;
$wp_post_types[$post_type] = $args;
return $args;
}
/**
@@ -1018,7 +1064,8 @@ function wp_count_posts( $type = 'post', $perm = '' ) {
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
if ( 'readable' == $perm && is_user_logged_in() ) {
if ( !current_user_can("read_private_{$type}s") ) {
$post_type_object = get_post_type_object($type);
if ( !current_user_can("read_private_{$post_type_object->capability_type}s") ) {
$cache_key .= '_' . $perm . '_' . $user->ID;
$query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
}

View File

@@ -2062,8 +2062,13 @@ class WP_Query {
if ( is_array($post_type) )
$post_type_cap = 'multiple_post_type';
else
$post_type_cap = $post_type;
else {
$post_type_object = get_post_type_object ( $post_type );
if ( !empty($post_type_object) )
$post_type_cap = $post_type_object->capability_type;
else
$post_type_cap = $post_type;
}
$exclude_post_types = '';
foreach ( get_post_types( array('exclude_from_search' => true) ) as $_wp_post_type )

View File

@@ -180,20 +180,46 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
$wp->add_query_var($args['query_var']);
}
if ( false !== $args['rewrite'] && !empty($wp_rewrite) ) {
if ( false !== $args['rewrite'] && '' != get_option('permalink_structure') ) {
if ( !is_array($args['rewrite']) )
$args['rewrite'] = array();
if ( !isset($args['rewrite']['slug']) )
$args['rewrite']['slug'] = sanitize_title_with_dashes($taxonomy);
$wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=$term");
$wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%");
$wp_rewrite->add_permastruct($taxonomy, "/{$args['rewrite']['slug']}/%$taxonomy%");
}
$args['name'] = $taxonomy;
$args['object_type'] = $object_type;
$args['object_type'] = (array) $object_type;
$wp_taxonomies[$taxonomy] = (object) $args;
}
/**
* Add an already registered taxonomy to an object type.
*
* @package WordPress
* @subpackage Taxonomy
* @since 3.0
* @uses $wp_taxonomies Modifies taxonomy object
*
* @param string $taxonomy Name of taxonomy object
* @param array|string $object_type Name of the object type
* @return bool True if successful, false if not
*/
function register_taxonomy_for_object_type( $taxonomy, $object_type) {
global $wp_taxonomies;
if ( !isset($wp_taxonomies[$taxonomy]) )
return false;
if ( ! get_post_type_object($object_type) )
return false;
$wp_taxonomies[$taxonomy]->object_type[] = $object_type;
return true;
}
//
// Term API
//
@@ -2378,4 +2404,26 @@ function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
return false;
}
/**
* Determine if the given object type is associated with the given taxonomy.
*
* @since 3.0
* @uses get_object_taxonomies()
*
* @param string $object_type Object type string
* @param string $taxonomy. Single taxonomy name
* @return bool True if object is associated with the taxonomy, otherwise false.
*/
function is_object_in_taxonomy($object_type, $taxonomy) {
$taxonomies = get_object_taxonomies($object_type);
if ( empty($taxonomies) )
return false;
if ( in_array($taxonomy, $taxonomies) )
return true;
return false;
}
?>