diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index e48baaa355..5f590d7099 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -383,7 +383,9 @@ document.body.className = document.body.className.replace('no-js', 'js'); * @param string $editor_id */ function media_buttons($editor_id = 'content') { - wp_enqueue_media(); + wp_enqueue_media( array( + 'post' => get_post() + ) ); $context = apply_filters('media_buttons_context', __('Upload/Insert %s')); diff --git a/wp-admin/js/media-upload.js b/wp-admin/js/media-upload.js index c686088dbd..bf5f06acbf 100644 --- a/wp-admin/js/media-upload.js +++ b/wp-admin/js/media-upload.js @@ -212,7 +212,18 @@ var tb_position; var galleries = {}; return { - attachments: function( shortcode, parent ) { + defaults: { + order: 'ASC', + orderby: 'post__in', + id: wp.media.view.settings.postId, + itemtag: 'dl', + icontag: 'dt', + captiontag: 'dd', + columns: 3, + size: 'thumbnail' + }, + + attachments: function( shortcode ) { var shortcodeString = shortcode.string(), result = galleries[ shortcodeString ], attrs, args, query, others; @@ -240,7 +251,7 @@ var tb_position; args.post__not_in = attrs.exclude.split(','); if ( ! args.post__in ) - args.parent = attrs.id || parent; + args.parent = attrs.id; // Collect the attributes that were not included in `args`. others = {}; @@ -283,6 +294,40 @@ var tb_position; galleries[ shortcode.string() ] = clone; return shortcode; + }, + + edit: function( content ) { + var shortcode = wp.shortcode.next( 'gallery', content ), + defaultPostId = wp.media.gallery.defaults.postId, + attachments, selection; + + // Bail if we didn't match the shortcode or all of the content. + if ( ! shortcode || shortcode.content !== content ) + return; + + // Ignore the rest of the match object. + shortcode = shortcode.shortcode; + + if ( _.isUndefined( shortcode.get('id') ) && ! _.isUndefined( defaultPostId ) ) + shortcode.set( 'id', defaultPostId ); + + attachments = wp.media.gallery.attachments( shortcode ); + + selection = new wp.media.model.Selection( attachments.models, { + props: attachments.props.toJSON(), + multiple: true + }); + + selection.gallery = attachments.gallery; + + return wp.media({ + frame: 'post', + state: 'gallery-edit', + title: wp.media.view.l10n.editGalleryTitle, + editing: true, + multiple: true, + selection: selection + }); } }; }()); diff --git a/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js b/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js index 9b2d028098..f8062150e3 100644 --- a/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js +++ b/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js @@ -10,16 +10,24 @@ // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...'); ed.addCommand('WP_Gallery', function() { - var el = ed.selection.getNode(), post_id, vp = tinymce.DOM.getViewPort(), - H = vp.h - 80, W = ( 640 < vp.w ) ? 640 : vp.w; + var el = ed.selection.getNode(), + gallery = wp.media.gallery, + frame; - if ( el.nodeName != 'IMG' ) return; - if ( ed.dom.getAttrib(el, 'class').indexOf('wpGallery') == -1 ) return; + // Check if the `wp.media.gallery` API exists. + if ( typeof wp === 'undefined' || ! wp.media || ! wp.media.gallery ) + return; - post_id = tinymce.DOM.get('post_ID').value; - tb_show('', tinymce.documentBaseURL + 'media-upload.php?post_id='+post_id+'&tab=gallery&TB_iframe=true&width='+W+'&height='+H); + // Make sure we've selected a gallery node. + if ( el.nodeName != 'IMG' || ed.dom.getAttrib(el, 'class').indexOf('wpGallery') == -1 ) + return; - tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' ); + frame = gallery.edit( '[' + ed.dom.getAttrib( el, 'title' ) + ']' ); + + frame.get('gallery-edit').on( 'update', function( selection ) { + var shortcode = gallery.shortcode( selection ).string().slice( 1, -1 ); + ed.dom.setAttrib( el, 'title', shortcode ); + }); }); ed.onMouseDown.add(function(ed, e) { diff --git a/wp-includes/media.php b/wp-includes/media.php index a460f52745..64810e19f3 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1300,7 +1300,12 @@ function wp_prepare_attachment_for_js( $attachment ) { * * @since 3.5.0 */ -function wp_enqueue_media() { +function wp_enqueue_media( $args = array() ) { + $defaults = array( + 'post' => null, + ); + $args = wp_parse_args( $args, $defaults ); + // We're going to pass the old thickbox media tabs to `media_upload_tabs` // to ensure plugins will work. We will then unset those tabs. $tabs = array( @@ -1321,6 +1326,9 @@ function wp_enqueue_media() { ), admin_url('media-upload.php') ), ); + if ( isset( $args['post'] ) ) + $settings['postId'] = get_post( $args['post'] )->ID; + wp_localize_script( 'media-views', '_wpMediaViewsL10n', array( // Settings 'settings' => $settings,