From 8db820753ead69daa8a56cb9e7e584f75162a05e Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 16 Feb 2008 21:54:47 +0000 Subject: [PATCH] Media handling updates from andy. fixes #5874 git-svn-id: http://svn.automattic.com/wordpress/trunk@6876 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/css/media.css | 62 +++-- wp-admin/includes/media.php | 381 ++++++++++++++++++++++----- wp-includes/js/swfupload/handlers.js | 12 +- wp-includes/post-template.php | 5 +- 4 files changed, 375 insertions(+), 85 deletions(-) diff --git a/wp-admin/css/media.css b/wp-admin/css/media.css index 91530a30d8..e499d735ed 100644 --- a/wp-admin/css/media.css +++ b/wp-admin/css/media.css @@ -44,7 +44,7 @@ form.media-upload-form { margin: 0 0 0.5em 0; } -.media-upload-form label.form-help { +.media-upload-form label.form-help, td.help { color: #9a9a9a; font-style: italic; font-weight: normal; @@ -84,25 +84,28 @@ form.media-upload-form { } /* specific to the image upload form */ -.media-upload-form fieldset#image-align label { +.media-upload-form fieldset#image-align label, .align .field label { display: inline; padding: 0 0 0 28px; margin: 0 0; } +.media-upload-form fieldset#image-align input, .align .field input { + margin-left: 15px; +} -#image-align-none-label { +#image-align-none-label, .image-align-none-label { background: url(../images/align-none.png) no-repeat center left; } -#image-align-left-label { +#image-align-left-label, .image-align-left-label { background: url(../images/align-left.png) no-repeat center left; } -#image-align-center-label { +#image-align-center-label, .image-align-center-label { background: url(../images/align-center.png) no-repeat center left; } -#image-align-right-label { +#image-align-right-label, .image-align-right-label { background: url(../images/align-right.png) no-repeat center left; } @@ -153,26 +156,37 @@ span.filename { display: none; border-top: 1px solid #d0d0d0; padding: 5px; + width: 100%; } -.describe fieldset { - width: 470px; - float: right; +.startopen .describe { + display: auto; } -.describe img { - float: left; +.required abbr { + color: red; + text-decoration: none; + border: none; +} + +.describe label { + display: inline; +} +.describe td { + vertical-align: middle; +} +.describe td.A1 { + width: 132px; +} +.describe td.savesend { + text-align: right; } .describe input[type="text"], .describe textarea { - width: 450px; + width: 460px; } -.describe label { - padding-right: 1em; -} -p.delete { - clear: both; - margin: 0; -} -p.delete a { - padding: 4px; +.describe a.delete { + display: block; + text-align: center; + line-height: 24px; + width: 100%; } .describe-toggle-on, .describe-toggle-off { line-height: 36px; @@ -193,3 +207,9 @@ p.delete a { border: none; z-index: 10; } +tr.submit input { + width: 128px; +} +tr.align td.field { + text-align: center; +} diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 959c683d34..336bb0889d 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -174,7 +174,7 @@ function image_upload_handler() { if ( is_wp_error($id) ) wp_iframe( 'image_upload_form', get_option('siteurl') . '/wp-admin/media-upload.php?type=image', $_POST, $id ); else { - image_send_to_editor($id, $_POST['image-alt'], $_POST['image-title'], $_POST['image-align'], $_POST['image-url']); + media_send_to_editor(get_image_send_to_editor($id, $_POST['image-alt'], $_POST['image-title'], $_POST['image-align'], $_POST['image-url'])); } } } @@ -232,7 +232,7 @@ function image_constrain_size_for_editor($width, $height) { return wp_shrink_dimensions( $width, $height, $max_width, $max_height ); } -function image_send_to_editor($id, $alt, $title, $align, $url='') { +function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false) { $img_src = wp_get_attachment_url($id); $meta = wp_get_attachment_metadata($id); @@ -243,14 +243,15 @@ function image_send_to_editor($id, $alt, $title, $align, $url='') { $hwstring = ' width="'.intval($width).'" height="'.intval($height).'"'; } - $html = ''.attribute_escape($alt).''; + $html = ''.attribute_escape($alt).''; + $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : ''; if ( $url ) - $html = ''.$html.''; + $html = "$html"; $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url ); - media_send_to_editor($html); + return $html; } function media_send_to_editor($html) { @@ -262,6 +263,7 @@ top.tb_remove(); --> $attachment ) { - $post = $_post = get_post($attachment_id, ARRAY_A); - $post['post_content'] = $attachment['post_content']; - $post['post_title'] = $attachment['post_title']; - if ( $post != $_post ) - wp_update_post($post); + check_admin_referer('multimedia-form'); - if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t ) - if ( isset($attachment[$t]) ) - wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false); - } - - media_send_to_editor('[gallery]'); - } else { + // Insert multimedia button was clicked + if ( !empty($_FILES) ) { // Upload File button was clicked $id = media_handle_upload('async-upload', $_REQUEST['post_id']); - wp_iframe( 'multimedia_upload_form' ); + if ( is_wp_error($id) ) + $errors['upload_error'] = $id; } + + if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { + $post = $_post = get_post($attachment_id, ARRAY_A); + if ( isset($attachment['post_content']) ) + $post['post_content'] = $attachment['post_content']; + if ( isset($attachment['post_title']) ) + $post['post_title'] = $attachment['post_title']; + if ( isset($attachment['post_excerpt']) ) + $post['post_excerpt'] = $attachment['post_excerpt']; + + $post = apply_filters('attachment_fields_to_save', $post, $attachment); + + if ( isset($post['errors']) ) { + $errors[$attachment_id] = $post['errors']; + unset($post['errors']); + } + + if ( $post != $_post ) + wp_update_post($post); + + foreach ( get_attachment_taxonomies($post) as $t ) + if ( isset($attachment[$t]) ) + wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false); + } + + if ( isset($_POST['insert-multimedia']) ) + return media_send_to_editor('[gallery]'); + + if ( isset($_POST['send']) ) { + $send_id = (int) array_shift(array_keys($_POST['send'])); + $attachment = $_POST['attachments'][$send_id]; + $html = apply_filters('media_send_to_editor', get_the_attachment_link($send_id, 0, array(125,125), !empty($attachment['post_content'])), $send_id, $attachment); + return media_send_to_editor($html); + } + + wp_iframe( 'multimedia_upload_form', $errors ); } -function get_multimedia_items( $post_id ) { +function get_multimedia_items( $post_id, $errors ) { $attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=\"menu_order ASC, ID ASC\""); if ( empty($attachments) ) @@ -467,14 +493,169 @@ function get_multimedia_items( $post_id ) { foreach ( $attachments as $id => $attachment ) { $output .= "\n
"; - $output .= get_multimedia_item($id); + $output .= get_multimedia_item($id, isset($errors[$id]) ? $errors[$id] : null); $output .= "
\n
"; } return $output; } -function get_multimedia_item( $attachment_id ) { +function get_attachment_taxonomies($attachment) { + if ( is_int( $attachment ) ) + $attachment = get_post($attachment); + else if ( is_array($attachment) ) + $attachment = (object) $attachment; + + if ( ! is_object($attachment) ) + return array(); + + $filename = basename($attachment->guid); + + $objects = array('attachment'); + + if ( false !== strpos($filename, '.') ) + $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1); + if ( !empty($attachment->post_mime_type) ) { + $objects[] = 'attachment:' . $attachment->post_mime_type; + if ( false !== strpos($attachment->post_mime_type, '/') ) + foreach ( explode('/', $attachment->post_mime_type) as $token ) + if ( !empty($token) ) + $objects[] = "attachment:$token"; + } + + $taxonomies = array(); + foreach ( $objects as $object ) + if ( $taxes = get_object_taxonomies($object) ) + $taxonomies = array_merge($taxonomies, $taxes); + + return array_unique($taxonomies); +} + +function image_attachment_fields_to_edit($form_fields, $post) { + if ( substr($post->post_mime_type, 0, 5) == 'image' ) { + $form_fields['post_title']['required'] = true; + $form_fields['post_excerpt']['label'] = __('Alternate Text'); + $form_fields['post_content']['label'] = __('Description'); + + if ( strlen(trim($post->post_excerpt)) == 0 ) + $form_fields['post_excerpt']['helps'][] = __('Alternate Text helps people who can not see the image.'); + + $form_fields['_send']['url'] = array( + 'label' => __('Link URL'), + 'input' => 'html', + 'html' => '', + 'helps' => __('If filled, this will override the default link URL.'), + ); + $form_fields['_send']['align'] = array( + 'label' => __('Alignment'), + 'input' => 'html', + 'html' => " + + + + + + + + \n", + ); + } + return $form_fields; +} + +add_filter('attachment_fields_to_edit', 'image_attachment_fields_to_edit', 10, 2); + +function image_attachment_fields_to_save($post, $attachment) { + if ( substr($post['post_mime_type'], 0, 5) == 'image' ) { + if ( strlen(trim($post['post_title'])) == 0 ) { + $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid'])); + $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.'); + } + } + + return $post; +} + +add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2); + +function image_media_send_to_editor($html, $attachment_id, $attachment) { + $post =& get_post($attachment_id); + if ( substr($post->post_mime_type, 0, 5) == 'image' ) { + if ( !empty($attachment['url']) ) + $url = $attachment['url']; + elseif ( $rel = strlen(trim($post->post_content)) ) + $url = get_attachment_link($attachment_id); + else + $url = wp_get_attachment_url($attachment_id); + + if ( isset($attachment['align']) ) + $align = $attachment['align']; + else + $align = 'none'; + + return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel); + } + + return $html; +} + +add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3); + +function get_attachment_fields_to_edit($post, $errors = null) { + if ( is_int($post) ) + $post =& get_post($post); + if ( is_array($post) ) + $post = (object) $post; + + $edit_post = sanitize_post($post, 'edit'); + + $form_fields = array( + 'post_title' => array( + 'label' => __('Title'), + 'value' => $edit_post->post_title, + ), + 'post_excerpt' => array( + 'label' => __('Excerpt'), + 'value' => $edit_post->post_excerpt, + ), + 'post_content' => array( + 'label' => __('Description'), + 'value' => $edit_post->post_content, + 'input' => 'textarea', + 'helps' => array(__('If filled, the default link URL will be the attachment permalink.')), + ), + ); + + foreach ( get_attachment_taxonomies($post) as $taxonomy ) { + $t = (array) get_taxonomy($taxonomy); + if ( empty($t['label']) ) + $t['label'] = $taxonomy; + if ( empty($t['args']) ) + $t['args'] = array(); + + $terms = get_object_term_cache($post->ID, $taxonomy); + if ( empty($terms) ) + $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']); + + $values = array(); + + foreach ( $terms as $term ) + $values[] = $term->name; + $t['value'] = join(', ', $values); + + $form_fields[$taxonomy] = $t; + } + + // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default + // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing ) + $form_fields = array_merge_recursive($form_fields, (array) $errors); + + $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post); + + return $form_fields; +} + +function get_multimedia_item( $attachment_id, $errors = null, $send = true ) { $thumb_url = wp_get_attachment_thumb_url( $attachment_id ); if ( empty($thumb_url) ) $thumb_url = wp_mime_type_icon( $attachment_id ); @@ -499,44 +680,104 @@ function get_multimedia_item( $attachment_id ) { $tags = attribute_escape(join(', ', $tags)); } - $delete_href = wp_nonce_url("post.php?action=delete-post&post=$attachment_id", 'delete-post_' . $attachment_id); - $delete = __('Delete'); + $form_fields = get_attachment_fields_to_edit($post, $errors); + $class = empty($errors) ? 'startclosed' : 'startopen'; $item = " $toggle_on $toggle_off $filename -
- -
-

-

-"; + + + + + + + + \n"; - if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t ) { - $tax = get_taxonomy($t); - $t_title = !empty($tax->title) ? $tax->title : $t; - if ( false === $terms = get_object_term_cache( $attachment_id, $t ) ) - $_terms = wp_get_object_terms($attachment_id, $t); - if ( $_terms ) { - foreach ( $_terms as $term ) - $terms[] = $term->name; - $terms = join(', ', $terms); - } else - $terms = ''; - $item .= "

\n"; + $defaults = array( + 'input' => 'text', + 'required' => false, + 'value' => '', + 'extra_rows' => array(), + ); + + $delete_href = wp_nonce_url("post.php?action=delete-post&post=$attachment_id", 'delete-post_' . $attachment_id); + $delete = __('Delete'); + $save = ""; + $send = ""; + + if ( empty($form_fields['save']) && empty($form_fields['_send']) ) { + $form_fields['save'] = array('tr' => "\t\t\n"); + } elseif ( empty($form_fields['save']) ) { + $form_fields['save'] = array('tr' => "\t\t\n"); + foreach ( $form_fields['_send'] as $id => $field ) + $form_fields[$id] = $field; + $form_fields['send'] = array('tr' => "\t\t"); } - $item .= " - -

$delete

- -"; + $hidden_fields = array(); + + foreach ( $form_fields as $id => $field ) { + if ( $id{0} == '_' ) + continue; + + if ( !empty($field['tr']) ) { + $item .= $field['tr']; + continue; + } + + $field = array_merge($defaults, $field); + $name = "attachments[$attachment_id][$id]"; + + if ( $field['input'] == 'hidden' ) { + $hidden_fields[$name] = $field['value']; + continue; + } + + $required = $field['required'] ? '*' : ''; + $item .= "\t\t\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t\n"; + + $extra_rows = array(); + + if ( !empty($field['errors']) ) + foreach ( array_unique((array) $field['errors']) as $error ) + $extra_rows['error'][] = $error; + + if ( !empty($field['helps']) ) + foreach ( array_unique((array) $field['helps']) as $help ) + $extra_rows['help'][] = $help; + + if ( !empty($field['extra_rows']) ) + foreach ( $field['extra_rows'] as $class => $rows ) + foreach ( (array) $rows as $html ) + $extra_rows[$class][] = $html; + + foreach ( $extra_rows as $class => $rows ) + foreach ( $rows as $html ) + $item .= "\t\t\n"; + } + + if ( !empty($form_fields['_final']) ) + $item .= "\t\t\n"; + $item .= "\t
$filename
$post->post_mime_type
" . mysql2date($post->post_date, get_option('time_format')) . "
" . apply_filters('multimedia_meta', '', $post) . "
$delete$save$send
$save
$delete$send
$required"; + if ( !empty($field[$field['input']]) ) + $item .= $field[$field['input']]; + elseif ( $field['input'] == 'textarea' ) { + $item .= ""; + } else { + $item .= ""; + } + $item .= "
$html
{$form_fields['_final']}
\n"; + + foreach ( $hidden_fields as $name => $value ) + $item .= "\t\n"; return $item; } -function multimedia_upload_form( $error = null ) { +function multimedia_upload_form( $errors = null ) { $flash_action_url = get_option('siteurl') . '/wp-admin/async-upload.php?type=multimedia'; $form_action_url = get_option('siteurl') . '/wp-admin/media-upload.php?type=multimedia'; @@ -547,8 +788,8 @@ function multimedia_upload_form( $error = null ) {

- - get_error_message(); ?> + + get_error_message(); ?>