Use the regular post type UI for editing single media items (attachments).

* Attachments now go through post.php, edit_post(), the like, and have show_ui set to true.
 * Taxonomies attached to the media library now appear in the admin menu (if show_ui).
 * Editing, cropping, uploading, etc. is still very rough, but mostly functional.

API-wise:
 * New function: get_taxonomies_for_attachments(). Like get_taxonomies(), for taxonomies specifically registered against attachments.
 * Brings taxonomy support from the posts list table to the media list table. Expect them to converge soon.
 * wp_insert_attachment() now handles taxonomies like wp_insert_post(). Also expect them to converge soon.
 * New edit_form_after_title hook.

props helenyhou, ocean90. see #21391.



git-svn-id: http://core.svn.wordpress.org/trunk@21948 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin
2012-09-21 22:52:54 +00:00
parent ffaa2d0330
commit 33af30eb7f
15 changed files with 350 additions and 65 deletions

View File

@@ -487,21 +487,19 @@ function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
$cpts = (array) get_post_types( array( 'show_in_admin_bar' => true ), 'objects' );
if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->edit_posts ) ) {
if ( isset( $cpts['post'] ) && current_user_can( $cpts['post']->cap->edit_posts ) )
$actions[ 'post-new.php' ] = array( $cpts['post']->labels->name_admin_bar, 'new-post' );
unset( $cpts['post'] );
}
if ( current_user_can( 'upload_files' ) )
$actions[ 'media-new.php' ] = array( _x( 'Media', 'add new from admin bar' ), 'new-media' );
if ( isset( $cpts['attachment'] ) && current_user_can( 'upload_files' ) )
$actions[ 'media-new.php' ] = array( $cpts['attachment']->labels->name_admin_bar, 'new-media' );
if ( current_user_can( 'manage_links' ) )
$actions[ 'link-add.php' ] = array( _x( 'Link', 'add new from admin bar' ), 'new-link' );
if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->edit_posts ) ) {
if ( isset( $cpts['page'] ) && current_user_can( $cpts['page']->cap->edit_posts ) )
$actions[ 'post-new.php?post_type=page' ] = array( $cpts['page']->labels->name_admin_bar, 'new-page' );
unset( $cpts['page'] );
}
unset( $cpts['post'], $cpts['page'], $cpts['attachment'] );
// Add any additional custom post types.
foreach ( $cpts as $cpt ) {

View File

@@ -106,7 +106,7 @@ function get_permalink( $id = 0, $leavename = false ) {
if ( $post->post_type == 'page' )
return get_page_link($post->ID, $leavename, $sample);
elseif ( $post->post_type == 'attachment' )
return get_attachment_link($post->ID);
return get_attachment_link( $post->ID, $leavename );
elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
return get_post_permalink($post->ID, $leavename, $sample);
@@ -292,9 +292,10 @@ function _get_page_link( $post = false, $leavename = false, $sample = false ) {
* @since 2.0.0
*
* @param mixed $post Optional. Post ID or object.
* @param bool $leavename Optional. Leave name.
* @return string
*/
function get_attachment_link( $post = null ) {
function get_attachment_link( $post = null, $leavename = false ) {
global $wp_rewrite;
$link = false;
@@ -314,7 +315,10 @@ function get_attachment_link( $post = null ) {
$name = $post->post_name;
if ( strpos($parentlink, '?') === false )
$link = user_trailingslashit( trailingslashit($parentlink) . $name );
$link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
if ( ! $leavename )
$link = str_replace( '%postname%', $name, $link );
}
if ( ! $link )

View File

@@ -985,6 +985,35 @@ function get_attachment_taxonomies($attachment) {
return array_unique($taxonomies);
}
/**
* Return all of the taxonomy names that are registered for attachments.
*
* Handles mime-type-specific taxonomies such as attachment:image and attachment:video.
*
* @since 3.5.0
* @see get_attachment_taxonomies()
* @uses get_taxonomies()
*
* @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
* @return array The names of all taxonomy of $object_type.
*/
function get_taxonomies_for_attachments( $output = 'names' ) {
$taxonomies = array();
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
foreach ( $taxonomy->object_type as $object_type ) {
if ( 'attachment' == $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
if ( 'names' == $output )
$taxonomies[] = $taxonomy->name;
else
$taxonomies[ $taxonomy->name ] = $taxonomy;
break;
}
}
}
return $taxonomies;
}
/**
* Check if the installed version of GD supports particular image type
*

View File

@@ -52,13 +52,16 @@ function create_initial_post_types() {
register_post_type( 'attachment', array(
'labels' => array(
'name' => __( 'Media' ),
'edit_item' => __( 'Edit Media' ),
'name' => _x('Media', 'post type general name'),
'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),
'add_new' => _x( 'Add New', 'add new media' ),
'edit_item' => __( 'Edit Media' ),
'view_item' => __( 'View Attachment Page' ),
),
'public' => true,
'show_ui' => false,
'show_ui' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
@@ -66,7 +69,7 @@ function create_initial_post_types() {
'query_var' => false,
'show_in_nav_menus' => false,
'delete_with_user' => true,
'supports' => array( 'comments', 'author' ),
'supports' => array( 'title', 'author', 'comments' ),
) );
register_post_type( 'revision', array(
@@ -3768,13 +3771,12 @@ function wp_insert_attachment($object, $file = false, $parent = 0) {
if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
$post_status = 'inherit';
if ( !empty($post_category) )
$post_category = array_filter($post_category); // Filter out empty terms
// Make sure we set a valid category.
if ( !isset($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
// 'post' requires at least one category.
if ( 'post' == $post_type )
$post_category = array( get_option('default_category') );
else
$post_category = array();
if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
$post_category = array();
}
// Are we updating or creating?
@@ -3859,7 +3861,22 @@ function wp_insert_attachment($object, $file = false, $parent = 0) {
$wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
}
wp_set_post_categories($post_ID, $post_category);
if ( is_object_in_taxonomy($post_type, 'category') )
wp_set_post_categories( $post_ID, $post_category );
if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
wp_set_post_tags( $post_ID, $tags_input );
// support for all custom taxonomies
if ( !empty($tax_input) ) {
foreach ( $tax_input as $taxonomy => $tags ) {
$taxonomy_obj = get_taxonomy($taxonomy);
if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
$tags = array_filter($tags);
if ( current_user_can($taxonomy_obj->cap->assign_terms) )
wp_set_post_terms( $post_ID, $tags, $taxonomy );
}
}
if ( $file )
update_attached_file( $post_ID, $file );