|
994 | 994 | button: {
|
995 | 995 | text: feedzy.i10n.media_iframe_button
|
996 | 996 | },
|
997 |
| - multiple: false |
998 |
| - } ).on( 'select', function() { // it also has "open" and "close" events |
999 |
| - var attachment = wp_media_uploader.state().get( 'selection' ).first().toJSON(); |
1000 |
| - var attachmentUrl = attachment.url; |
1001 |
| - if ( attachment.sizes.thumbnail ) { |
1002 |
| - attachmentUrl = attachment.sizes.thumbnail.url; |
1003 |
| - } |
1004 |
| - if ( $( '.feedzy-media-preview' ).length ) { |
1005 |
| - $( '.feedzy-media-preview' ).find( 'img' ).attr( 'src', attachmentUrl ); |
1006 |
| - } else { |
1007 |
| - $( '<div class="fz-form-group mb-20 feedzy-media-preview"><img src="' + attachmentUrl + '"></div>' ).insertBefore( button.parent() ); |
1008 |
| - } |
1009 |
| - button.parent().find( '.feedzy-remove-media' ).addClass( 'is-show' ); |
1010 |
| - button.parent().find( 'input:hidden' ).val( attachment.id ).trigger( 'change' ); |
1011 |
| - $( '.feedzy-open-media' ).html( feedzy.i10n.action_btn_text_2 ); |
1012 |
| - } ).open(); |
| 997 | + multiple: true |
| 998 | + } ).on( 'select', function() { // it also has "open" and "close" events |
| 999 | + var selectedAttachments = wp_media_uploader.state().get( 'selection' ); |
| 1000 | + var countSelected = selectedAttachments?.toJSON()?.length; |
| 1001 | + button.parents( '.fz-form-group' ).find( '.feedzy-media-preview' ).remove(); |
| 1002 | + // Display image preview when a single image is selected. |
| 1003 | + if ( 1 === countSelected ) { |
| 1004 | + var attachment = selectedAttachments.first().toJSON(); |
| 1005 | + var attachmentUrl = attachment.url; |
| 1006 | + if ( attachment.sizes.thumbnail ) { |
| 1007 | + attachmentUrl = attachment.sizes.thumbnail.url; |
| 1008 | + } |
| 1009 | + if ( $( '.feedzy-media-preview' ).length ) { |
| 1010 | + $( '.feedzy-media-preview' ).find( 'img' ).attr( 'src', attachmentUrl ); |
| 1011 | + } else { |
| 1012 | + $( '<div class="fz-form-group mb-20 feedzy-media-preview"><img src="' + attachmentUrl + '"></div>' ).insertBefore( button.parent() ); |
| 1013 | + } |
| 1014 | + } else { |
| 1015 | + $( '<div class="fz-form-group mb-20 feedzy-media-preview"><a href="javascript:;" class="btn btn-outline-primary feedzy-images-selected">' + feedzy.i10n.action_btn_text_3.replace( '%d', countSelected ) + '</a></div>' ).insertBefore( button.parent() ); |
| 1016 | + } |
| 1017 | + // Get all selected attachment ids. |
| 1018 | + var ids = selectedAttachments.map( function( attachment ) { |
| 1019 | + return attachment.id; |
| 1020 | + } ).join( ',' ); |
| 1021 | + |
| 1022 | + button.parent().find( '.feedzy-remove-media' ).addClass( 'is-show' ); |
| 1023 | + button.parent().find( 'input:hidden' ).val( ids ).trigger( 'change' ); |
| 1024 | + $( '.feedzy-open-media' ).html( feedzy.i10n.action_btn_text_2 ); |
| 1025 | + } ); |
| 1026 | + |
| 1027 | + wp_media_uploader.on(' open', function() { |
| 1028 | + var selectedVal = button.parent().find( 'input:hidden' ).val(); |
| 1029 | + if ( '' === selectedVal ) { |
| 1030 | + return; |
| 1031 | + } |
| 1032 | + var selection = wp_media_uploader.state().get('selection'); |
| 1033 | + |
| 1034 | + selectedVal.split(',').forEach(function( id ) { |
| 1035 | + var attachment = wp.media.attachment( id ); |
| 1036 | + attachment.fetch(); |
| 1037 | + selection.add(attachment ? [attachment] : []); |
| 1038 | + }); |
| 1039 | + } ); |
| 1040 | + |
| 1041 | + wp_media_uploader.open(); |
1013 | 1042 | });
|
| 1043 | + |
| 1044 | + $(document).on( 'click', '.feedzy-images-selected', function( e ) { |
| 1045 | + $(this) |
| 1046 | + .parents( '.fz-form-group' ) |
| 1047 | + .find( '.feedzy-open-media' ) |
| 1048 | + .trigger( 'click' ); |
| 1049 | + e.preventDefault(); |
| 1050 | + } ); |
1014 | 1051 | }
|
1015 | 1052 | }(jQuery, feedzy));
|
1016 | 1053 |
|
|
0 commit comments