\n";
}
@@ -88,74 +101,140 @@ function the_media_upload_tabs() {
/**
* {@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
+ * @since 2.5.0
+ *
+ * @param integer $id image attachment id
+ * @param string $caption image caption
+ * @param string $alt image alt attribute
+ * @param string $title image title attribute
+ * @param string $align image css alignment property
+ * @param string $url image src url
+ * @param string|bool $rel image rel attribute
+ * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
+ * @return string the html to insert into editor
*/
-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, '', $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 = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url, $size );
+ $html = '$html";
+
+ /**
+ * Filter the image HTML markup to send to the editor.
+ *
+ * @since 2.5.0
+ *
+ * @param string $html The image HTML markup to send.
+ * @param int $id The attachment id.
+ * @param string $caption The image caption.
+ * @param string $title The image title.
+ * @param string $align The image alignment.
+ * @param string $url The image source URL.
+ * @param string $size The image size.
+ * @param string $alt The image alternative, or alt, text.
+ */
+ $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
+ * Adds image shortcode with caption to editor
+ *
+ * @since 2.6.0
+ *
+ * @param string $html
+ * @param integer $id
+ * @param string $caption image caption
+ * @param string $alt image alt attribute
+ * @param string $title image title attribute
+ * @param string $align image css alignment property
+ * @param string $url image src url
+ * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
+ * @return string
*/
-function image_add_caption( $html, $id, $alt, $title, $align, $url, $size ) {
+function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
+
+ /**
+ * Filter the caption text.
+ *
+ * Note: If the caption text is empty, the caption shortcode will not be appended
+ * to the image HTML when inserted into the editor.
+ *
+ * Passing an empty value also prevents the {@see 'image_add_caption_shortcode'}
+ * filter from being evaluated at the end of {@see image_add_caption()}.
+ *
+ * @since 4.1.0
+ *
+ * @param string $caption The original caption text.
+ * @param int $id The attachment ID.
+ */
+ $caption = apply_filters( 'image_add_caption_text', $caption, $id );
+
+ /**
+ * Filter whether to disable captions.
+ *
+ * Prevents image captions from being appended to image HTML when inserted into the editor.
+ *
+ * @since 2.6.0
+ *
+ * @param bool $bool Whether to disable appending captions. Returning true to the filter
+ * will disable captions. Default empty string.
+ */
+ if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
+ return $html;
- if ( empty($alt) || apply_filters( 'disable_captions', '' ) ) return $html;
$id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
- preg_match( '/width="([0-9]+)/', $html, $matches );
- if ( ! isset($matches[1]) ) return $html;
+ if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
+ return $html;
+
$width = $matches[1];
- $html = preg_replace( '/align[^\s\'"]+\s?/', '', $html );
- if ( empty($align) ) $align = 'none';
+ $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
+ $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
- $alt = ! empty($alt) ? addslashes($alt) : '';
+ // Convert any remaining line breaks to .
+ $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', ' ', $caption );
- $shcode = '[caption id="' . $id . '" align="align' . $align
- . '" width="' . $width . '" caption="' . $alt . '"]' . $html . '[/caption]';
+ $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 . '"]' . $html . ' ' . $caption . '[/caption]';
+
+ /**
+ * Filter the image HTML markup including the caption shortcode.
+ *
+ * @since 2.6.0
+ *
+ * @param string $shcode The image HTML markup with caption shortcode.
+ * @param string $html The image HTML markup.
+ */
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}}
+ * Private preg_replace callback used in image_add_caption()
+ *
+ * @access private
+ * @since 3.4.0
+ */
+function _cleanup_image_add_caption( $matches ) {
+ // Remove any line breaks from inside the tags.
+ return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
+}
+
+/**
+ * Adds image html to editor
*
- * @since unknown
+ * @since 2.5.0
*
- * @param unknown_type $html
+ * @param string $html
*/
function media_send_to_editor($html) {
?>
@@ -165,24 +244,22 @@ var win = window.dialogArguments || opener || parent || top;
win.send_to_editor('');
/* ]]> */
- 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) ) {
@@ -190,22 +267,78 @@ function media_handle_upload($file_id, $post_id, $post_data = array()) {
$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'] );
+ $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 ( preg_match( '#^audio#', $type ) ) {
+ $meta = wp_read_audio_metadata( $file );
+
+ if ( ! empty( $meta['title'] ) )
+ $title = $meta['title'];
+
+ $content = '';
+
+ if ( ! empty( $title ) ) {
+
+ if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
+ /* translators: 1: audio track title, 2: album title, 3: artist name */
+ $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
+ } else if ( ! empty( $meta['album'] ) ) {
+ /* translators: 1: audio track title, 2: album title */
+ $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
+ } else if ( ! empty( $meta['artist'] ) ) {
+ /* translators: 1: audio track title, 2: artist name */
+ $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
+ } else {
+ $content .= sprintf( __( '"%s".' ), $title );
+ }
+
+ } else if ( ! empty( $meta['album'] ) ) {
+
+ if ( ! empty( $meta['artist'] ) ) {
+ /* translators: 1: audio album title, 2: artist name */
+ $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
+ } else {
+ $content .= $meta['album'] . '.';
+ }
+
+ } else if ( ! empty( $meta['artist'] ) ) {
+
+ $content .= $meta['artist'] . '.';
+
+ }
+
+ if ( ! empty( $meta['year'] ) )
+ $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
+
+ if ( ! empty( $meta['track_number'] ) ) {
+ $track_number = explode( '/', $meta['track_number'] );
+ if ( isset( $track_number[1] ) )
+ $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
+ else
+ $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
+ }
+
+ if ( ! empty( $meta['genre'] ) )
+ $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
+
+ // Use image exif/iptc data for title and caption defaults if possible.
+ } elseif ( 0 === strpos( $type, 'image/' ) && $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']) )
+ if ( trim( $image_meta['caption'] ) )
$content = $image_meta['caption'];
}
@@ -218,6 +351,10 @@ function media_handle_upload($file_id, $post_id, $post_data = array()) {
'post_content' => $content,
), $post_data );
+ // This should never be set as it would then overwrite an existing attachment.
+ if ( isset( $attachment['ID'] ) )
+ unset( $attachment['ID'] );
+
// Save the data
$id = wp_insert_attachment($attachment, $file, $post_id);
if ( !is_wp_error($id) ) {
@@ -229,20 +366,26 @@ 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);
+ $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_sideload( $file_array, $overrides, $time );
if ( isset($file['error']) )
return new WP_Error( 'upload_error', $file['error'] );
@@ -252,17 +395,18 @@ function media_handle_sideload($file_array, $post_id, $desc = null, $post_data =
$title = preg_replace('/\.[^.]+$/', '', basename($file));
$content = '';
- // use image exif/iptc data for title and caption defaults if possible
+ // 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;
+ if ( isset( $desc ) )
+ $title = $desc;
- // Construct the attachment array
+ // Construct the attachment array.
$attachment = array_merge( array(
'post_mime_type' => $type,
'guid' => $url,
@@ -271,111 +415,221 @@ function media_handle_sideload($file_array, $post_id, $desc = null, $post_data =
'post_content' => $content,
), $post_data );
- // Save the data
+ // This should never be set as it would then overwrite an existing attachment.
+ if ( isset( $attachment['ID'] ) )
+ unset( $attachment['ID'] );
+
+ // 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;
}
/**
- * {@internal Missing Short Description}}
+ * Adds the iframe to display content for the media upload page
*
- * 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 2.5.0
*
- * @since unknown
- *
- * @param unknown_type $content_func
+ * @param string|callable $content_func
*/
function wp_iframe($content_func /* ... */) {
+ _wp_admin_html_begin();
?>
-
- >
-
-
› —
->
+ class="wp-core-ui no-js">
+
+
-
-
-
-
-EOF;
- printf($context, $out);
+function media_buttons($editor_id = 'content') {
+ static $instance = 0;
+ $instance++;
+
+ $post = get_post();
+ if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
+ $post = $GLOBALS['post_ID'];
+
+ wp_enqueue_media( array(
+ 'post' => $post
+ ) );
+
+ $img = ' ';
+
+ $id_attribute = $instance === 1 ? ' id="insert-media-button"' : '';
+ printf( '%s',
+ $id_attribute,
+ esc_attr( $editor_id ),
+ esc_attr__( 'Add Media' ),
+ $img . __( 'Add Media' )
+ );
+ /**
+ * Filter the legacy (pre-3.5.0) media buttons.
+ *
+ * @since 2.5.0
+ * @deprecated 3.5.0 Use 'media_buttons' action instead.
+ *
+ * @param string $string Media buttons context. Default empty.
+ */
+ $legacy_filter = apply_filters( 'media_buttons_context', '' );
+
+ if ( $legacy_filter ) {
+ // #WP22559. Close if a plugin started by closing to open their own tag.
+ if ( 0 === stripos( trim( $legacy_filter ), '' ) )
+ $legacy_filter .= '';
+ echo $legacy_filter;
+ }
}
add_action( 'media_buttons', 'media_buttons' );
-add_action('media_upload_media', 'media_upload_handler');
+
+/**
+ *
+ * @global int $post_ID
+ * @param string $type
+ * @param int $post_id
+ * @param string $tab
+ * @return string
+ */
+function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
+ global $post_ID;
+
+ if ( empty( $post_id ) )
+ $post_id = $post_ID;
+
+ $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
+
+ if ( $type && 'media' != $type )
+ $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
+
+ if ( ! empty( $tab ) )
+ $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
+
+ /**
+ * Filter the upload iframe source URL for a specific media type.
+ *
+ * The dynamic portion of the hook name, `$type`, refers to the type
+ * of media uploaded.
+ *
+ * @since 3.0.0
+ *
+ * @param string $upload_iframe_src The upload iframe source URL by type.
+ */
+ $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
+ * @return mixed void|object WP_Error on failure
*/
function media_upload_form_handler() {
check_admin_referer('media-form');
+ $errors = null;
+
if ( isset($_POST['send']) ) {
$keys = array_keys($_POST['send']);
$send_id = (int) array_shift($keys);
}
-
+
if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
$post = $_post = get_post($attachment_id, ARRAY_A);
+
+ if ( !current_user_can( 'edit_post', $attachment_id ) )
+ continue;
+
if ( isset($attachment['post_content']) )
$post['post_content'] = $attachment['post_content'];
if ( isset($attachment['post_title']) )
@@ -384,12 +638,33 @@ function media_upload_form_handler() {
$post['post_excerpt'] = $attachment['post_excerpt'];
if ( isset($attachment['menu_order']) )
$post['menu_order'] = $attachment['menu_order'];
+
if ( isset($send_id) && $attachment_id == $send_id ) {
if ( isset($attachment['post_parent']) )
$post['post_parent'] = $attachment['post_parent'];
}
- $post = apply_filters('attachment_fields_to_save', $post, $attachment);
+ /**
+ * Filter the attachment fields to be saved.
+ *
+ * @since 2.5.0
+ *
+ * @see wp_get_attachment_metadata()
+ *
+ * @param WP_Post $post The WP_Post object.
+ * @param array $attachment An array of attachment metadata.
+ */
+ $post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
+
+ if ( isset($attachment['image_alt']) ) {
+ $image_alt = wp_unslash( $attachment['image_alt'] );
+ if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
+ $image_alt = wp_strip_all_tags( $image_alt, true );
+
+ // Update_meta expects slashed.
+ update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
+ }
+ }
if ( isset($post['errors']) ) {
$errors[$attachment_id] = $post['errors'];
@@ -399,9 +674,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']) ) { ?>
@@ -416,14 +692,28 @@ function media_upload_form_handler() {
}
if ( isset($send_id) ) {
- $attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
- $html = $attachment['post_title'];
+ $attachment = wp_unslash( $_POST['attachments'][$send_id] );
+
+ $html = isset( $attachment['post_title'] ) ? $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-".attribute_escape($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";
}
- $html = apply_filters('media_send_to_editor', $html, $send_id, $attachment);
+
+ /**
+ * Filter the HTML markup for a media item sent to the editor.
+ *
+ * @since 2.5.0
+ *
+ * @see wp_get_attachment_metadata()
+ *
+ * @param string $html HTML markup for a media item sent to the editor.
+ * @param int $send_id The first key from the $_POST['send'] data.
+ * @param array $attachment Array of attachment metadata.
+ */
+ $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
return media_send_to_editor($html);
}
@@ -433,15 +723,16 @@ function media_upload_form_handler() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
- * @return unknown
+ * @return null|string
*/
-function media_upload_image() {
+function wp_media_upload_handler() {
$errors = array();
$id = 0;
if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
+ check_admin_referer('media-form');
// Upload File button was clicked
$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
unset($_FILES);
@@ -452,164 +743,67 @@ function media_upload_image() {
}
if ( !empty($_POST['insertonlybutton']) ) {
- $src = $_POST['insertonly']['src'];
+ $src = $_POST['src'];
if ( !empty($src) && !strpos($src, '://') )
$src = "http://$src";
- $alt = attribute_escape($_POST['insertonly']['alt']);
- if ( isset($_POST['insertonly']['align']) ) {
- $align = attribute_escape($_POST['insertonly']['align']);
- $class = " class='align$align'";
- }
- if ( !empty($src) )
- $html = "";
- return media_send_to_editor($html);
- }
-
- if ( !empty($_POST) ) {
- $return = media_upload_form_handler();
-
- if ( is_string($return) )
- return $return;
- if ( is_array($return) )
- $errors = $return;
- }
-
- if ( isset($_POST['save']) ) {
- $errors['upload_notice'] = __('Saved.');
- return media_upload_gallery();
- }
-
- if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
- return wp_iframe( 'media_upload_type_url_form', 'image', $errors, $id );
-
- return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
-}
-
-/**
- * {@internal Missing Short Description}}
- *
- * @since unknown
- *
- * @param unknown_type $file
- * @param unknown_type $post_id
- * @param unknown_type $desc
- * @return unknown
- */
-function media_sideload_image($file, $post_id, $desc = null) {
- if (!empty($file) ) {
- $file_array['name'] = basename($file);
- $tmp = download_url($file);
- $file_array['tmp_name'] = $tmp;
- $desc = @$desc;
-
- 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;
-
- if ( is_wp_error($id) ) {
- @unlink($file_array['tmp_name']);
- return $id;
- }
- }
- if ( !empty($src) ) {
- $alt = @$desc;
- $html = "";
- return $html;
- }
-}
-
-/**
- * {@internal Missing Short Description}}
- *
- * @since unknown
- *
- * @return unknown
- */
-function media_upload_audio() {
- $errors = array();
- $id = 0;
-
- if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
- // Upload File button was clicked
- $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
- unset($_FILES);
- if ( is_wp_error($id) ) {
- $errors['upload_error'] = $id;
- $id = false;
+ if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
+ $title = esc_html( wp_unslash( $_POST['title'] ) );
+ if ( empty( $title ) )
+ $title = esc_html( basename( $src ) );
+
+ if ( $title && $src )
+ $html = "$title";
+
+ $type = 'file';
+ if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
+ && ( 'audio' == $ext_type || 'video' == $ext_type ) )
+ $type = $ext_type;
+
+ /**
+ * Filter the URL sent to the editor for a specific media type.
+ *
+ * The dynamic portion of the hook name, `$type`, refers to the type
+ * of media being sent.
+ *
+ * @since 3.3.0
+ *
+ * @param string $html HTML markup sent to the editor.
+ * @param string $src Media source URL.
+ * @param string $title Media title.
+ */
+ $html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
+ } else {
+ $align = '';
+ $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
+ if ( isset($_POST['align']) ) {
+ $align = esc_attr( wp_unslash( $_POST['align'] ) );
+ $class = " class='align$align'";
+ }
+ if ( !empty($src) )
+ $html = "";
+
+ /**
+ * Filter the image URL sent to the editor.
+ *
+ * @since 2.8.0
+ *
+ * @param string $html HTML markup sent to the editor for an image.
+ * @param string $src Image source URL.
+ * @param string $alt Image alternate, or alt, text.
+ * @param string $align The image alignment. Default 'alignnone'. Possible values include
+ * 'alignleft', 'aligncenter', 'alignright', 'alignnone'.
+ */
+ $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
}
- }
- if ( !empty($_POST['insertonlybutton']) ) {
- $href = $_POST['insertonly']['href'];
- if ( !empty($href) && !strpos($href, '://') )
- $href = "http://$href";
- $title = attribute_escape($_POST['insertonly']['title']);
- if ( empty($title) )
- $title = basename($href);
- if ( !empty($title) && !empty($href) )
- $html = "$title";
return media_send_to_editor($html);
}
- if ( !empty($_POST) ) {
- $return = media_upload_form_handler();
-
- if ( is_string($return) )
- return $return;
- if ( is_array($return) )
- $errors = $return;
- }
-
- if ( isset($_POST['save']) ) {
+ if ( isset( $_POST['save'] ) ) {
$errors['upload_notice'] = __('Saved.');
return media_upload_gallery();
- }
-
- if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
- return wp_iframe( 'media_upload_type_url_form', 'audio', $errors, $id );
-
- return wp_iframe( 'media_upload_type_form', 'audio', $errors, $id );
-}
-
-/**
- * {@internal Missing Short Description}}
- *
- * @since unknown
- *
- * @return unknown
- */
-function media_upload_video() {
- $errors = array();
- $id = 0;
-
- if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
- // Upload File button was clicked
- $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
- unset($_FILES);
- if ( is_wp_error($id) ) {
- $errors['upload_error'] = $id;
- $id = false;
- }
- }
-
- if ( !empty($_POST['insertonlybutton']) ) {
- $href = $_POST['insertonly']['href'];
- if ( !empty($href) && !strpos($href, '://') )
- $href = "http://$href";
- $title = attribute_escape($_POST['insertonly']['title']);
- if ( empty($title) )
- $title = basename($href);
- if ( !empty($title) && !empty($href) )
- $html = "$title";
- return media_send_to_editor($html);
- }
-
- if ( !empty($_POST) ) {
+ } elseif ( ! empty( $_POST ) ) {
$return = media_upload_form_handler();
if ( is_string($return) )
@@ -618,76 +812,67 @@ function media_upload_video() {
$errors = $return;
}
- if ( isset($_POST['save']) ) {
- $errors['upload_notice'] = __('Saved.');
- return media_upload_gallery();
+ if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
+ $type = 'image';
+ if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
+ $type = $_GET['type'];
+ return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
}
- if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
- return wp_iframe( 'media_upload_type_url_form', 'video', $errors, $id );
-
- return wp_iframe( 'media_upload_type_form', 'video', $errors, $id );
+ return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
}
/**
- * {@internal Missing Short Description}}
+ * Download an image from the specified URL and attach it to a post.
*
- * @since unknown
+ * @since 2.6.0
*
- * @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_upload_file() {
- $errors = array();
- $id = 0;
-
- if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
- // Upload File button was clicked
- $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
- unset($_FILES);
- if ( is_wp_error($id) ) {
- $errors['upload_error'] = $id;
- $id = false;
+function media_sideload_image( $file, $post_id, $desc = null ) {
+ if ( ! empty( $file ) ) {
+ // Set variables for storage, fix file filename for query strings.
+ preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
+ $file_array = array();
+ $file_array['name'] = basename( $matches[0] );
+
+ // Download file to temp location.
+ $file_array['tmp_name'] = download_url( $file );
+
+ // If error storing temporarily, return the error.
+ if ( is_wp_error( $file_array['tmp_name'] ) ) {
+ return $file_array['tmp_name'];
}
- }
- if ( !empty($_POST['insertonlybutton']) ) {
- $href = $_POST['insertonly']['href'];
- if ( !empty($href) && !strpos($href, '://') )
- $href = "http://$href";
- $title = attribute_escape($_POST['insertonly']['title']);
- if ( empty($title) )
- $title = basename($href);
- if ( !empty($title) && !empty($href) )
- $html = "$title";
- return media_send_to_editor($html);
- }
+ // Do the validation and storage stuff.
+ $id = media_handle_sideload( $file_array, $post_id, $desc );
- if ( !empty($_POST) ) {
- $return = media_upload_form_handler();
+ // If error storing permanently, unlink.
+ if ( is_wp_error( $id ) ) {
+ @unlink( $file_array['tmp_name'] );
+ return $id;
+ }
- if ( is_string($return) )
- return $return;
- if ( is_array($return) )
- $errors = $return;
+ $src = wp_get_attachment_url( $id );
}
- if ( isset($_POST['save']) ) {
- $errors['upload_notice'] = __('Saved.');
- return media_upload_gallery();
+ // 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;
}
-
- if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
- return wp_iframe( 'media_upload_type_url_form', 'file', $errors, $id );
-
- return wp_iframe( 'media_upload_type_form', 'file', $errors, $id );
}
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
- * @return unknown
+ * @return string|null
*/
function media_upload_gallery() {
$errors = array();
@@ -708,9 +893,9 @@ function media_upload_gallery() {
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
- * @return unknown
+ * @return string|null
*/
function media_upload_library() {
$errors = array();
@@ -729,24 +914,27 @@ 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
- * @return unknown
+ * @param WP_Post $post
+ * @param string $checked
+ * @return string
*/
-function image_align_input_fields($post, $checked='') {
+function image_align_input_fields( $post, $checked = '' ) {
- $alignments = array('none' => 'None', 'left' => 'Left', 'center' => 'Center', 'right' => 'Right');
- if ( !array_key_exists($checked, $alignments) )
+ 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[] = "";
+ ( $checked == $name ? " checked='checked'" : "" ) .
+ " />";
}
return join("\n", $out);
}
@@ -754,36 +942,62 @@ 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
- * @return unknown
+ * @param WP_Post $post
+ * @param bool|string $check
+ * @return array
*/
-function image_size_input_fields($post, $checked='') {
-
- // 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'));
-
- foreach ( $size_names as $size => $name) {
+function image_size_input_fields( $post, $check = '' ) {
+
+ /**
+ * Filter the names and labels of the default image sizes.
+ *
+ * @since 3.3.0
+ *
+ * @param array $size_names Array of image sizes and their names. Default values
+ * include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
+ */
+ $size_names = apply_filters( 'image_size_names_choose', array(
+ 'thumbnail' => __( 'Thumbnail' ),
+ 'medium' => __( 'Medium' ),
+ 'large' => __( 'Large' ),
+ 'full' => __( 'Full Size' )
+ ) );
+
+ 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?
+ // 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 .= "";
- // only show the dimensions if that choice is available
+ // If this size is the default but that's not available, don't select it.
+ 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 .= '
';
+ }
}
/**
* {@internal Missing Short Description}}
*
- * @since unknown
+ * @since 2.5.0
*
- * @param unknown_type $errors
+ * @param array $errors
*/
function media_upload_form( $errors = null ) {
- global $type, $tab;
-
- $flash_action_url = admin_url('async-upload.php');
+ global $type, $tab, $is_IE, $is_opera;
- // If Mac and mod_security, no Flash. :(
- $flash = true;
- if ( false !== strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'mac') && apache_mod_loaded('mod_security') )
- $flash = false;
+ if ( ! _device_can_upload() ) {
+ echo '
' . sprintf( __('The web browser on your device cannot be used to upload files. You may be able to use the native app for your device instead.'), 'https://apps.wordpress.org/' ) . '
+ post_mime_type ) ) {
+
+ /**
+ * Filter the audio and video metadata fields to be shown in the publish meta box.
+ *
+ * The key for each item in the array should correspond to an attachment
+ * metadata key, and the value should be the desired label.
+ *
+ * @since 3.7.0
+ *
+ * @param array $fields An array of the attachment metadata keys and labels.
+ */
+ $fields = apply_filters( 'media_submitbox_misc_sections', array(
+ 'length_formatted' => __( 'Length:' ),
+ 'bitrate' => __( 'Bitrate:' ),
+ ) );
+
+ foreach ( $fields as $key => $label ) {
+ if ( empty( $meta[ $key ] ) ) {
+ continue;
+ }
+ ?>
+