false); $time = current_time('mysql'); if ( $post = get_post($post_id) ) { if ( substr( $post->post_date, 0, 4 ) > 0 ) $time = $post->post_date; } $file = wp_handle_upload($_FILES[$file_id], $overrides, $time); if ( isset($file['error']) ) return new WP_Error( 'upload_error', $file['error'] ); $url = $file['url']; $type = $file['type']; $file = $file['file']; $title = preg_replace('/\.[^.]+$/', '', basename($file)); $content = ''; // use image exif/iptc data for title and caption defaults if possible if ( $image_meta = @wp_read_image_metadata($file) ) { if ( trim($image_meta['title']) ) $title = $image_meta['title']; if ( trim($image_meta['caption']) ) $content = $image_meta['caption']; } // Construct the attachment array $attachment = array_merge( array( 'post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, ), $post_data ); // Save the data $id = wp_insert_attachment($attachment, $file, $post_id); if ( !is_wp_error($id) ) { wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); } return $id; } /** * {@internal Missing Short Description}} * * @since unknown * * @return unknown */ function media_upload_form_handler() { check_admin_referer('media-form'); 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']; if ( isset($attachment['menu_order']) ) $post['menu_order'] = $attachment['menu_order']; if ( isset($attachment['post_parent']) ) $post['post_parent'] = $attachment['post_parent']; $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); } return $errors; } /** * {@internal Missing Short Description}} * * @since unknown * * @param unknown_type $form_fields * @param unknown_type $post * @return unknown */ function media_single_attachment_fields_to_edit( $form_fields, $post ) { unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']); return $form_fields; } /** * {@internal Missing Short Description}} * * @since unknown * * @param unknown_type $post * @param unknown_type $attachment * @return unknown */ 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); /** * {@internal Missing Short Description}} * * @since unknown * * @param unknown_type $post * @param unknown_type $errors * @return unknown */ 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' => __('Caption'), 'value' => $edit_post->post_excerpt, ), 'post_content' => array( 'label' => __('Description'), 'value' => $edit_post->post_content, 'input' => 'textarea', ), 'menu_order' => array( 'label' => __('Order'), 'value' => $edit_post->menu_order ), ); 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; } /** * Retrieve HTML form for modifying the image attachment. * * @since unknown * * @param int $attachment_id Attachment ID for modification. * @param string|array $args Optional. Override defaults. * @return string HTML form for attachment. */ function get_media_item( $attachment_id, $args = null ) { global $redir_tab; $default_args = array( 'errors' => null, 'delete' => true, 'toggle' => true, 'show_title' => true ); $args = wp_parse_args( $args, $default_args ); extract( $args, EXTR_SKIP ); global $post_mime_types; if ( ( $attachment_id = intval($attachment_id) ) && $thumb_url = get_attachment_icon_src( $attachment_id ) ) $thumb_url = $thumb_url[0]; else return false; $toggle_on = __('Show'); $toggle_off = __('Hide'); $post = get_post($attachment_id); $filename = basename($post->guid); $title = attribute_escape($post->post_title); if ( $_tags = get_the_tags($attachment_id) ) { foreach ( $_tags as $tag ) $tags[] = $tag->name; $tags = attribute_escape(join(', ', $tags)); } $type = ''; if ( isset($post_mime_types) ) { $keys = array_keys(wp_match_mime_types(array_keys($post_mime_types), $post->post_mime_type)); $type = array_shift($keys); $type = ""; } $form_fields = get_attachment_fields_to_edit($post, $errors); if ( $toggle ) { $class = empty($errors) ? 'startclosed' : 'startopen'; $toggle_links = " $toggle_on $toggle_off"; } else { $class = 'form-table'; $toggle_links = ''; } $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case $display_title = $show_title ? "
" . wp_html_excerpt($display_title, 60) . "
" : ''; $gallery = ( (isset($_REQUEST['tab']) && 'gallery' == $_REQUEST['tab']) || (isset($redir_tab) && 'gallery' == $redir_tab) ) ? true : false; $order = ''; foreach ( $form_fields as $key => $val ) { if ( 'menu_order' == $key ) { if ( $gallery ) $order = ''; else $order = ''; unset($form_fields['menu_order']); break; } } $item = " $type $toggle_links $order $display_title \n"; $defaults = array( 'input' => 'text', 'required' => false, 'value' => '', 'extra_rows' => array(), ); $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'] ? '*' : ''; $aria_required = $field['required'] ? " aria-required='true' " : ''; $class = $id; $class .= $field['required'] ? ' form-required' : ''; $item .= "\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['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\n"; $item .= "\t
$filename
$post->post_mime_type
" . mysql2date($post->post_date, get_option('time_format')) . "
" . apply_filters('media_meta', '', $post) . "
"; if ( !empty($field[$field['input']]) ) $item .= $field[$field['input']]; elseif ( $field['input'] == 'textarea' ) { $item .= ""; } else { $item .= ""; } if ( !empty($field['helps']) ) $item .= "

" . join( "

\n

", array_unique((array) $field['helps']) ) . '

'; $item .= "
$html
{$form_fields['_final']}
\n"; foreach ( $hidden_fields as $name => $value ) $item .= "\t\n"; if ( $post->post_parent < 1 && (int) $_REQUEST['post_id'] ) { $parent = (int) $_REQUEST['post_id']; $parent_name = "attachments[$attachment_id][post_parent]"; $item .= "\t\n"; } return $item; } /** * {@internal Missing Short Description}} * * @since unknown */ function media_upload_header() { ?>
get_error_message(); ?>