diff --git a/wp-admin/js/post-formats.js b/wp-admin/js/post-formats.js
index 4ee97a446a..10aab6174e 100644
--- a/wp-admin/js/post-formats.js
+++ b/wp-admin/js/post-formats.js
@@ -110,10 +110,21 @@ window.wp = window.wp || {};
}
});
- mediaPreview = function (format, url, mime) {
+ mediaPreview = function (attachment) {
+ var dimensions = '', url = attachment.url,
+ mime = attachment.mime,
+ format = attachment.type;
+
+ if ( 'video' === format ) {
+ if ( attachment.width )
+ dimensions += ' width="' + attachment.width + '"';
+ if ( attachment.height )
+ dimensions += ' height="' + attachment.height + '"';
+ }
+
$('#' + format + '-preview').remove();
$holder.parent().prepend( '
' +
- '<' + format + ' class="wp-' + format + '-shortcode" controls="controls" preload="none">' +
+ '<' + format + dimensions + ' class="wp-' + format + '-shortcode" controls="controls" preload="none">' +
'' +
'' + format + '>
' );
$('.wp-' + format + '-shortcode').mediaelementplayer();
@@ -122,25 +133,25 @@ window.wp = window.wp || {};
// When an image is selected, run a callback.
mediaFrame.on( 'select', function () {
// Grab the selected attachment.
- var attachment = mediaFrame.state().get('selection').first(), mime, url, id;
+ var attachment = mediaFrame.state().get('selection').first().toJSON();
- id = attachment.get('id');
- url = attachment.get('url');
- mime = attachment.get('mime');
-
- if ( 0 === mime.indexOf('audio') ) {
- $field.val(url);
+ if ( 0 === attachment.mime.indexOf('audio') ) {
+ $field.val(attachment.url);
// show one preview at a time
- mediaPreview('audio', url, mime);
- } else if ( 0 === mime.indexOf('video') ) {
- $field.val(url);
+ mediaPreview(attachment);
+ } else if ( 0 === attachment.mime.indexOf('video') ) {
+ attachment.src = attachment.url;
+ $field.val(wp.shortcode.string({
+ tag: 'video',
+ attrs: _.pick( attachment, 'src', 'width', 'height' )
+ }));
// show one preview at a time
- mediaPreview('video', url, mime);
+ mediaPreview(attachment);
} else {
// set the hidden input's value
- $field.val(id);
+ $field.val(attachment.id);
// Show the image in the placeholder
- $el.html('');
+ $el.html('');
$holder.removeClass('empty').show();
}
});
diff --git a/wp-includes/js/media-editor.js b/wp-includes/js/media-editor.js
index 561171ea74..1fde56382d 100644
--- a/wp-includes/js/media-editor.js
+++ b/wp-includes/js/media-editor.js
@@ -144,6 +144,12 @@
shortcode = {};
+ if ( attachment.width )
+ shortcode.width = attachment.width;
+
+ if ( attachment.height )
+ shortcode.height = attachment.height;
+
if ( props.mime ) {
switch ( props.mime ) {
case 'video/mp4':
diff --git a/wp-includes/media.php b/wp-includes/media.php
index e74cd02378..97135e7766 100644
--- a/wp-includes/media.php
+++ b/wp-includes/media.php
@@ -1638,6 +1638,11 @@ function wp_prepare_attachment_for_js( $attachment ) {
}
$response = array_merge( $response, array( 'sizes' => $sizes ), $sizes['full'] );
+ } elseif ( $meta && 'video' === $type ) {
+ if ( isset( $meta['width'] ) )
+ $response['width'] = (int) $meta['width'];
+ if ( isset( $meta['height'] ) )
+ $response['height'] = (int) $meta['height'];
}
if ( function_exists('get_compat_media_markup') )
@@ -2014,6 +2019,15 @@ function wp_video_embed( $matches, $attr, $url, $rawattr ) {
if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) {
$dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] );
$dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] );
+ } elseif ( strstr( $url, home_url() ) ) {
+ $id = attachment_url_to_postid( $url );
+ if ( ! empty( $id ) ) {
+ $meta = wp_get_attachment_metadata( $id );
+ if ( ! empty( $meta['width'] ) )
+ $dimensions .= sprintf( 'width="%d" ', (int) $meta['width'] );
+ if ( ! empty( $meta['height'] ) )
+ $dimensions .= sprintf( 'height="%d" ', (int) $meta['height'] );
+ }
}
$video = do_shortcode( '[video ' . $dimensions . 'src="' . $url . '" /]' );
}
@@ -2437,4 +2451,25 @@ function get_the_post_format_image( $attached_size = 'full', &$post = null ) {
*/
function the_post_format_image( $attached_size = 'full' ) {
echo get_the_post_format_image( $attached_size );
+}
+
+/**
+ * Retrieve the post id for an attachment file URL
+ *
+ * @since 3.6.0
+ *
+ * @param string $url Permalink to check.
+ * @return int Post ID, or 0 on failure.
+ */
+function attachment_url_to_postid( $url ) {
+ global $wpdb;
+ if ( preg_match( '#\.[a-zA-Z0-9]+$#', $url ) ) {
+ $id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' " .
+ "AND guid = %s", $url ) );
+
+ if ( ! empty( $id ) )
+ return (int) $id;
+ }
+
+ return 0;
}
\ No newline at end of file