X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/177fd6fefd2e3d5a0ea6591c71d660cabdb3c1a4..a5227bf01edbe6660486c9f5c0f0ed7b7fea3130:/wp-admin/includes/media.php diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 1000d15b..2bebcc86 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -1,23 +1,54 @@ __('Choose File'), // handler action suffix => tab text + 'type' => __('From Computer'), // handler action suffix => tab text + 'type_url' => __('From URL'), 'gallery' => __('Gallery'), - 'library' => __('Media Library'), + 'library' => __('Media Library') ); return apply_filters('media_upload_tabs', $_default_tabs); } +/** + * {@internal Missing Short Description}} + * + * @since unknown + * + * @param unknown_type $tabs + * @return unknown + */ function update_gallery_tab($tabs) { global $wpdb; + if ( !isset($_REQUEST['post_id']) ) { unset($tabs['gallery']); return $tabs; } - if ( intval($_REQUEST['post_id']) ) - $attachments = intval($wpdb->get_var($wpdb->prepare("SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $_REQUEST['post_id']))); + + $post_id = intval($_REQUEST['post_id']); + + if ( $post_id ) + $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) ); + + if ( empty($attachments) ) { + unset($tabs['gallery']); + return $tabs; + } $tabs['gallery'] = sprintf(__('Gallery (%s)'), "$attachments"); @@ -25,74 +56,161 @@ function update_gallery_tab($tabs) { } add_filter('media_upload_tabs', 'update_gallery_tab'); +/** + * {@internal Missing Short Description}} + * + * @since unknown + */ function the_media_upload_tabs() { + global $redir_tab; $tabs = media_upload_tabs(); if ( !empty($tabs) ) { echo "
\n"; } } -function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false, $size='medium') { +/** + * {@internal Missing Short Description}} + * + * @since unknown + * + * @param unknown_type $id + * @param unknown_type $alt + * @param unknown_type $title + * @param unknown_type $align + * @param unknown_type $url + * @param unknown_type $rel + * @param unknown_type $size + * @return unknown + */ +function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') { $html = get_image_tag($id, $alt, $title, $align, $size); - $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : ''; + $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : ''; if ( $url ) - $html = "$html"; + $html = '$html"; - $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url, $size ); + $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt ); return $html; } +/** + * {@internal Missing Short Description}} + * + * @since unknown + * + * @param unknown_type $html + * @param unknown_type $id + * @param unknown_type $alt + * @param unknown_type $title + * @param unknown_type $align + * @param unknown_type $url + * @param unknown_type $size + * @return unknown + */ +function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) { + + if ( empty($caption) || apply_filters( 'disable_captions', '' ) ) + return $html; + + $id = ( 0 < (int) $id ) ? 'attachment_' . $id : ''; + + if ( ! preg_match( '/width="([0-9]+)/', $html, $matches ) ) + return $html; + + $width = $matches[1]; + + $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html ); + if ( empty($align) ) + $align = 'none'; + + $shcode = '[caption id="' . $id . '" align="align' . $align + . '" width="' . $width . '" caption="' . addslashes($caption) . '"]' . $html . '[/caption]'; + + return apply_filters( 'image_add_caption_shortcode', $shcode, $html ); +} +add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 ); + +/** + * {@internal Missing Short Description}} + * + * @since unknown + * + * @param unknown_type $html + */ function media_send_to_editor($html) { - ?> +?> - false); - $file = wp_handle_upload($_FILES[$file_id], $overrides); +/** + * {@internal Missing Short Description}} + * + * This handles the file upload POST itself, creating the attachment post. + * + * @since unknown + * + * @param string $file_id Index into the {@link $_FILES} array of the upload + * @param int $post_id The post ID the media is associated with + * @param array $post_data allows you to overwrite some of the attachment + * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior + * @return int the ID of the attachment + */ +function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) { + + $time = current_time('mysql'); + if ( $post = get_post($post_id) ) { + if ( substr( $post->post_date, 0, 4 ) > 0 ) + $time = $post->post_date; + } + + $name = $_FILES[$file_id]['name']; + $file = wp_handle_upload($_FILES[$file_id], $overrides, $time); if ( isset($file['error']) ) - return new wp_error( 'upload_error', $file['error'] ); + return new WP_Error( 'upload_error', $file['error'] ); + + $name_parts = pathinfo($name); + $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) ); $url = $file['url']; $type = $file['type']; $file = $file['file']; - $title = preg_replace('/\.[^.]+$/', '', basename($file)); + $title = $name; $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']) ) + if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) $title = $image_meta['title']; - if ( trim($image_meta['caption']) ) + if ( trim( $image_meta['caption'] ) ) $content = $image_meta['caption']; } @@ -106,7 +224,7 @@ function media_handle_upload($file_id, $post_id, $post_data = array()) { ), $post_data ); // Save the data - $id = wp_insert_attachment($attachment, $file, $post_parent); + $id = wp_insert_attachment($attachment, $file, $post_id); if ( !is_wp_error($id) ) { wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); } @@ -115,9 +233,68 @@ function media_handle_upload($file_id, $post_id, $post_data = array()) { } +/** + * {@internal Missing Short Description}} + * + * @since unknown + * + * @param unknown_type $file_array + * @param unknown_type $post_id + * @param unknown_type $desc + * @param unknown_type $post_data + * @return unknown + */ +function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) { + $overrides = array('test_form'=>false); + + $file = wp_handle_sideload($file_array, $overrides); + 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'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) + $title = $image_meta['title']; + if ( trim( $image_meta['caption'] ) ) + $content = $image_meta['caption']; + } + + $title = @$desc; -// wrap iframe content (produced by $content_func) in a doctype, html head/body etc -// any additional function args will be passed to content_func + // 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 attachment metadata + $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 $url; + } + return $id; +} + +/** + * {@internal Missing Short Description}} + * + * Wrap iframe content (produced by $content_func) in a doctype, html head/body + * etc any additional function args will be passed to content_func. + * + * @since unknown + * + * @param unknown_type $content_func + */ function wp_iframe($content_func /* ... */) { ?> @@ -126,18 +303,30 @@ function wp_iframe($content_func /* ... */) {