X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/9c40b4d36daed9e28e48a5fe9205c32557195a4b..fef8173b8c3bad08f495551e43cfdeac1cae6021:/wp-admin/includes/media.php?ds=sidebyside
diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php
index ab7cc58b..5a13bcc2 100644
--- a/wp-admin/includes/media.php
+++ b/wp-admin/includes/media.php
@@ -9,7 +9,7 @@
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @return unknown
*/
@@ -27,7 +27,7 @@ function media_upload_tabs() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @param unknown_type $tabs
* @return unknown
@@ -40,8 +40,10 @@ function update_gallery_tab($tabs) {
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']);
@@ -57,7 +59,7 @@ add_filter('media_upload_tabs', 'update_gallery_tab');
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*/
function the_media_upload_tabs() {
global $redir_tab;
@@ -87,7 +89,7 @@ function the_media_upload_tabs() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @param unknown_type $id
* @param unknown_type $alt
@@ -98,18 +100,16 @@ function the_media_upload_tabs() {
* @param unknown_type $size
* @return unknown
*/
-function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false, $size='medium') {
-
- $htmlalt = ( empty($alt) ) ? $title : $alt;
+function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
- $html = get_image_tag($id, $htmlalt, $title, $align, $size);
+ $html = get_image_tag($id, $alt, $title, $align, $size);
$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;
}
@@ -117,7 +117,7 @@ function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = fal
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.6.0
*
* @param unknown_type $html
* @param unknown_type $id
@@ -128,36 +128,38 @@ function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = fal
* @param unknown_type $size
* @return unknown
*/
-function image_add_caption( $html, $id, $alt, $title, $align, $url, $size ) {
+function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
- if ( empty($alt) || apply_filters( 'disable_captions', '' ) )
+ if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
return $html;
$id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
- preg_match( '/width="([0-9]+)/', $html, $matches );
- if ( ! isset($matches[1]) )
+ if ( ! preg_match( '/width="([0-9]+)/', $html, $matches ) )
return $html;
$width = $matches[1];
+ $caption = str_replace( array( '>', '<', '"', "'" ),
+ array( '>', '<', '"', ''' ),
+ $caption
+ );
+
$html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
if ( empty($align) )
$align = 'none';
- $alt = ! empty($alt) ? addslashes($alt) : '';
-
$shcode = '[caption id="' . $id . '" align="align' . $align
- . '" width="' . $width . '" caption="' . $alt . '"]' . $html . '[/caption]';
+ . '" 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, 7 );
+add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @param unknown_type $html
*/
@@ -169,7 +171,7 @@ var win = window.dialogArguments || opener || parent || top;
win.send_to_editor('');
/* ]]> */
- ');
*
* This handles the file upload POST itself, creating the attachment post.
*
- * @since unknown
+ * @since 2.5.0
*
- * @param unknown_type $file_id
- * @param unknown_type $post_id
- * @param unknown_type $post_data
- * @return 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);
+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) ) {
@@ -211,9 +213,9 @@ function media_handle_upload($file_id, $post_id, $post_data = array()) {
// 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'];
}
@@ -237,20 +239,20 @@ function media_handle_upload($file_id, $post_id, $post_data = array()) {
}
/**
- * {@internal Missing Short Description}}
+ * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
*
- * @since unknown
+ * @since 2.6.0
*
- * @param unknown_type $file_array
- * @param unknown_type $post_id
- * @param unknown_type $desc
- * @param unknown_type $post_data
- * @return unknown
+ * @param array $file_array Array similar to a {@link $_FILES} upload array
+ * @param int $post_id The post ID the media is associated with
+ * @param string $desc Description of the sideloaded file
+ * @param array $post_data allows you to overwrite some of the attachment
+ * @return int|object The ID of the attachment or a WP_Error on failure
*/
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);
+ $file = wp_handle_sideload($file_array, $overrides);
if ( isset($file['error']) )
return new WP_Error( 'upload_error', $file['error'] );
@@ -262,13 +264,13 @@ function media_handle_sideload($file_array, $post_id, $desc = null, $post_data =
// 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'];
}
- $title = @$desc;
+ $title = isset($desc) ? $desc : '';
// Construct the attachment array
$attachment = array_merge( array(
@@ -279,12 +281,11 @@ function media_handle_sideload($file_array, $post_id, $desc = null, $post_data =
'post_content' => $content,
), $post_data );
- // Save the data
+ // Save the attachment metadata
$id = wp_insert_attachment($attachment, $file, $post_id);
- if ( !is_wp_error($id) ) {
+ if ( !is_wp_error($id) )
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
- return $url;
- }
+
return $id;
}
@@ -294,7 +295,7 @@ function media_handle_sideload($file_array, $post_id, $desc = null, $post_data =
* 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
+ * @since 2.5.0
*
* @param unknown_type $content_func
*/
@@ -309,20 +310,29 @@ function wp_iframe($content_func /* ... */) {
wp_enqueue_style( 'global' );
wp_enqueue_style( 'wp-admin' );
wp_enqueue_style( 'colors' );
-if ( 0 === strpos( $content_func, 'media' ) )
+// Check callback name for 'media'
+if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
+ || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
wp_enqueue_style( 'media' );
wp_enqueue_style( 'ie' );
?>
@@ -344,44 +354,63 @@ if ( is_string($content_func) )
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*/
function media_buttons() {
- global $post_ID, $temp_ID;
- $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
+ $do_image = $do_audio = $do_video = true;
+ if ( is_multisite() ) {
+ $media_buttons = get_site_option( 'mu_media_buttons' );
+ if ( empty($media_buttons['image']) )
+ $do_image = false;
+ if ( empty($media_buttons['audio']) )
+ $do_audio = false;
+ if ( empty($media_buttons['video']) )
+ $do_video = false;
+ }
+ $out = '';
+
+ if ( $do_image )
+ $out .= _media_button(__('Add an Image'), 'images/media-button-image.gif?ver=20100531', 'image');
+ if ( $do_video )
+ $out .= _media_button(__('Add Video'), 'images/media-button-video.gif?ver=20100531', 'video');
+ if ( $do_audio )
+ $out .= _media_button(__('Add Audio'), 'images/media-button-music.gif?ver=20100531', 'audio');
+
+ $out .= _media_button(__('Add Media'), 'images/media-button-other.gif?ver=20100531', 'media');
+
$context = apply_filters('media_buttons_context', __('Upload/Insert %s'));
- $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
- $media_title = __('Add Media');
- $image_upload_iframe_src = apply_filters('image_upload_iframe_src', "$media_upload_iframe_src&type=image");
- $image_title = __('Add an Image');
- $video_upload_iframe_src = apply_filters('video_upload_iframe_src', "$media_upload_iframe_src&type=video");
- $video_title = __('Add Video');
- $audio_upload_iframe_src = apply_filters('audio_upload_iframe_src', "$media_upload_iframe_src&type=audio");
- $audio_title = __('Add Audio');
- $out = <<
-
-
-
-
-EOF;
+
printf($context, $out);
}
add_action( 'media_buttons', 'media_buttons' );
-add_action('media_upload_media', 'media_upload_handler');
+
+function _media_button($title, $icon, $type) {
+ return "";
+}
+
+function get_upload_iframe_src($type) {
+ global $post_ID, $temp_ID;
+ $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
+ $upload_iframe_src = add_query_arg('post_id', $uploading_iframe_ID, 'media-upload.php');
+
+ if ( 'media' != $type )
+ $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
+ $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
+
+ return add_query_arg('TB_iframe', true, $upload_iframe_src);
+}
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @return unknown
*/
function media_upload_form_handler() {
check_admin_referer('media-form');
- $errors = array();
+ $errors = null;
if ( isset($_POST['send']) ) {
$keys = array_keys($_POST['send']);
@@ -406,6 +435,15 @@ function media_upload_form_handler() {
$post = apply_filters('attachment_fields_to_save', $post, $attachment);
+ if ( isset($attachment['image_alt']) ) {
+ $image_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
+ if ( $image_alt != stripslashes($attachment['image_alt']) ) {
+ $image_alt = wp_strip_all_tags( stripslashes($attachment['image_alt']), true );
+ // update_meta expects slashed
+ update_post_meta( $attachment_id, '_wp_attachment_image_alt', addslashes($image_alt) );
+ }
+ }
+
if ( isset($post['errors']) ) {
$errors[$attachment_id] = $post['errors'];
unset($post['errors']);
@@ -414,9 +452,10 @@ function media_upload_form_handler() {
if ( $post != $_post )
wp_update_post($post);
- foreach ( get_attachment_taxonomies($post) as $t )
+ 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-gallery']) || isset($_POST['update-gallery']) ) { ?>
@@ -435,8 +474,9 @@ function media_upload_form_handler() {
$html = $attachment['post_title'];
if ( !empty($attachment['url']) ) {
- if ( strpos($attachment['url'], 'attachment_id') || false !== strpos($attachment['url'], get_permalink($_POST['post_id'])) )
- $rel = " rel='attachment wp-att-" . esc_attr($send_id)."'";
+ $rel = '';
+ if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
+ $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
$html = "$html";
}
@@ -450,7 +490,7 @@ function media_upload_form_handler() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @return unknown
*/
@@ -469,6 +509,8 @@ function media_upload_image() {
}
if ( !empty($_POST['insertonlybutton']) ) {
+ $alt = $align = '';
+
$src = $_POST['insertonly']['src'];
if ( !empty($src) && !strpos($src, '://') )
$src = "http://$src";
@@ -478,8 +520,9 @@ function media_upload_image() {
$class = " class='align$align'";
}
if ( !empty($src) )
- $html = "";
- $html = apply_filters('image_send_to_editor_url', $html, $src, $alt, $align);
+ $html = "";
+
+ $html = apply_filters('image_send_to_editor_url', $html, esc_url_raw($src), $alt, $align);
return media_send_to_editor($html);
}
@@ -504,38 +547,46 @@ function media_upload_image() {
}
/**
- * {@internal Missing Short Description}}
+ * Download an image from the specified URL and attach it to a post.
*
- * @since unknown
+ * @since 2.6.0
*
- * @param unknown_type $file
- * @param unknown_type $post_id
- * @param unknown_type $desc
- * @return unknown
+ * @param string $file The URL of the image to download
+ * @param int $post_id The post ID the media is to be associated with
+ * @param string $desc Optional. Description of the image
+ * @return string|WP_Error Populated HTML img tag on success
*/
function media_sideload_image($file, $post_id, $desc = null) {
- if (!empty($file) ) {
- $file_array['name'] = basename($file);
- $tmp = download_url($file);
+ if ( ! empty($file) ) {
+ // Download file to temp location
+ $tmp = download_url( $file );
+
+ // Set variables for storage
+ // fix file filename for query strings
+ preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
+ $file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
- $desc = @$desc;
- if ( is_wp_error($tmp) ) {
+ // If error storing temporarily, unlink
+ if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
- $id = media_handle_sideload($file_array, $post_id, $desc);
- $src = $id;
-
+ // do the validation and storage stuff
+ $id = media_handle_sideload( $file_array, $post_id, $desc );
+ // If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array['tmp_name']);
return $id;
}
+
+ $src = wp_get_attachment_url( $id );
}
- if ( !empty($src) ) {
- $alt = @$desc;
+ // Finally check to make sure the file has been saved, then return the html
+ if ( ! empty($src) ) {
+ $alt = isset($desc) ? esc_attr($desc) : '';
$html = "";
return $html;
}
@@ -544,7 +595,7 @@ function media_sideload_image($file, $post_id, $desc = null) {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @return unknown
*/
@@ -566,12 +617,16 @@ function media_upload_audio() {
$href = $_POST['insertonly']['href'];
if ( !empty($href) && !strpos($href, '://') )
$href = "http://$href";
+
$title = esc_attr($_POST['insertonly']['title']);
if ( empty($title) )
- $title = basename($href);
+ $title = esc_attr( basename($href) );
+
if ( !empty($title) && !empty($href) )
- $html = "$title";
+ $html = "$title";
+
$html = apply_filters('audio_send_to_editor_url', $html, $href, $title);
+
return media_send_to_editor($html);
}
@@ -598,7 +653,7 @@ function media_upload_audio() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @return unknown
*/
@@ -620,12 +675,16 @@ function media_upload_video() {
$href = $_POST['insertonly']['href'];
if ( !empty($href) && !strpos($href, '://') )
$href = "http://$href";
+
$title = esc_attr($_POST['insertonly']['title']);
- if ( empty($title) )
- $title = basename($href);
+ if ( empty($title) )
+ $title = esc_attr( basename($href) );
+
if ( !empty($title) && !empty($href) )
- $html = "$title";
+ $html = "$title";
+
$html = apply_filters('video_send_to_editor_url', $html, $href, $title);
+
return media_send_to_editor($html);
}
@@ -652,7 +711,7 @@ function media_upload_video() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @return unknown
*/
@@ -674,12 +733,13 @@ function media_upload_file() {
$href = $_POST['insertonly']['href'];
if ( !empty($href) && !strpos($href, '://') )
$href = "http://$href";
+
$title = esc_attr($_POST['insertonly']['title']);
if ( empty($title) )
$title = basename($href);
if ( !empty($title) && !empty($href) )
- $html = "$title";
- $html = apply_filters('file_send_to_editor_url', $html, $href, $title);
+ $html = "$title";
+ $html = apply_filters('file_send_to_editor_url', $html, esc_url_raw($href), $title);
return media_send_to_editor($html);
}
@@ -706,7 +766,7 @@ function media_upload_file() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @return unknown
*/
@@ -729,7 +789,7 @@ function media_upload_gallery() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
* @return unknown
*/
@@ -750,7 +810,7 @@ function media_upload_library() {
/**
* Retrieve HTML for the image alignment radio buttons with the specified one checked.
*
- * @since unknown
+ * @since 2.7.0
*
* @param unknown_type $post
* @param unknown_type $checked
@@ -758,16 +818,19 @@ function media_upload_library() {
*/
function image_align_input_fields( $post, $checked = '' ) {
+ if ( empty($checked) )
+ $checked = get_user_setting('align', 'none');
+
$alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
if ( !array_key_exists( (string) $checked, $alignments ) )
$checked = 'none';
$out = array();
- foreach ($alignments as $name => $label) {
+ foreach ( $alignments as $name => $label ) {
$name = esc_attr($name);
$out[] = "";
+ " />";
}
return join("\n", $out);
}
@@ -775,36 +838,45 @@ function image_align_input_fields( $post, $checked = '' ) {
/**
* Retrieve HTML for the size radio buttons with the specified one checked.
*
- * @since unknown
+ * @since 2.7.0
*
* @param unknown_type $post
- * @param unknown_type $checked
+ * @param unknown_type $check
* @return unknown
*/
-function image_size_input_fields( $post, $checked = '' ) {
+function image_size_input_fields( $post, $check = '' ) {
// get a list of the actual pixel dimensions of each possible intermediate version of this image
- $size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full size'));
+ $size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size'));
- foreach ( $size_names as $size => $name ) {
+ if ( empty($check) )
+ $check = get_user_setting('imgsize', 'medium');
+
+ foreach ( $size_names as $size => $label ) {
$downsize = image_downsize($post->ID, $size);
+ $checked = '';
// is this size selectable?
$enabled = ( $downsize[3] || 'full' == $size );
$css_id = "image-size-{$size}-{$post->ID}";
// if this size is the default but that's not available, don't select it
- if ( $checked && !$enabled )
- $checked = '';
- // if $checked was not specified, default to the first available size that's bigger than a thumbnail
- if ( !$checked && $enabled && 'thumbnail' != $size )
- $checked = $size;
-
- $html = "
ID][image-size]' id='{$css_id}' value='{$size}'".( $checked == $size ? " checked='checked'" : '') ." />";
-
- $html .= "";
+ if ( $size == $check ) {
+ if ( $enabled )
+ $checked = " checked='checked'";
+ else
+ $check = '';
+ } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
+ // if $check is not enabled, default to the first available size that's bigger than a thumbnail
+ $check = $size;
+ $checked = " checked='checked'";
+ }
+
+ $html = "
ID][image-size]' id='{$css_id}' value='{$size}'$checked />";
+
+ $html .= "";
// only show the dimensions if that choice is available
if ( $enabled )
- $html .= " ";
+ $html .= " ";
$html .= '