Skip to content

Commit 6a5a1ee

Browse files
Unique Frame IDs and Check Media Enqueue
Since we could have multiple frames on a given page, this gives each media "frame" or "view" its own unique ID. Also adds a check to make sure `wp_enqueue_media` has only been called once. See: http://core.trac.wordpress.org/ticket/22843 This is fixed in WP 3.5.1 beta at the time of writing, but since this library should play nice with the current major version we'll keep in the check.
1 parent 981e3f1 commit 6a5a1ee

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

inc/Fields/FieldBase.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,12 @@ protected function attachment($value, $key, $cls, $args)
281281

282282
echo '</div>';
283283

284-
wp_enqueue_media();
284+
// this is a hack that was fix in WP 3.5.1, but we'll keep this
285+
// here to make things play nice with WP 3.5
286+
if (!did_action('wp_enqueue_media')) {
287+
wp_enqueue_media();
288+
}
289+
285290
wp_enqueue_script('pmgcore-media');
286291
}
287292

inc/Functionality/Enqueue.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function register()
4545
self::VER
4646
);
4747

48+
// this depends on the `media-views` and `media-editor` scripts as
49+
// well, but those should be added with `wp_enqueue_media`
4850
wp_register_script(
4951
'pmgcore-media',
5052
PMGCORE_URL . 'js/media.js',

js/media.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
// props: http://mikejolley.com/2012/12/using-the-new-wordpress-3-5-media-uploader-in-plugins/
22
jQuery(document).ready(function($) {
3-
var file_frame;
3+
var frames = {};
4+
5+
if (typeof wp.media.frames.pmgcore == 'undefined') {
6+
wp.media.frames.pmgcore = {};
7+
}
48

59
$('#wpbody').on('click', '.pmgcore-cue-media', function(e) {
610

711
e.preventDefault();
812

913
var parent = $(this).parents('.pmgcore-media-wrap'),
1014
target = $(parent).find('input[type="hidden"]'),
15+
target_id = $(target).attr('id');
1116
media_id = $(target).val();
1217

13-
if (file_frame) {
14-
file_frame.open();
18+
if (frames[target_id]) {
19+
frames[target_id].open();
1520
return;
1621
}
1722

18-
file_frame = wp.media.frames.file_frame = wp.media({
23+
frames[target_id] = wp.media.frames.pmgcore[target_id] = wp.media({
1924
title: $(this).data('title'),
2025
button: {
2126
text: $(this).data('title')
@@ -24,8 +29,8 @@ jQuery(document).ready(function($) {
2429
selection: [media_id]
2530
});
2631

27-
file_frame.on('select', function() {
28-
var att = file_frame.state().get('selection').first(),
32+
frames[target_id].on('select', function() {
33+
var att = frames[target_id].state().get('selection').first(),
2934
type = att.get('type'),
3035
sizes,
3136
size,
@@ -49,10 +54,10 @@ jQuery(document).ready(function($) {
4954

5055
$(parent).find('.pmgcore-attachment-container').html('').append(e);
5156

52-
file_frame.close();
57+
frames[target_id].close();
5358
});
5459

55-
file_frame.open();
60+
frames[target_id].open();
5661
}).on('click', '.pmgcore-remove-media', function(e) {
5762
var $p = $(this).parents('.pmgcore-media-wrap');
5863

0 commit comments

Comments
 (0)