@@ -211,6 +211,8 @@ jQuery(function ($) {
211211 post_status : $ (
212212 'select[name="feedzy_meta_data[import_post_status]"]'
213213 ) . val ( ) ,
214+ fallback_image : $ ( 'input[name="feedzy_meta_data[default_thumbnail_id]"]' ) . val ( ) ,
215+ excluded_post_title : $ ( 'input[name="feedzy_meta_data[exclude_post_title]"]' ) . val ( ) ,
214216 action : 'feedzy' ,
215217 _action : 'wizard_import_feed' ,
216218 } ,
@@ -359,4 +361,116 @@ jQuery(function ($) {
359361
360362 // Init chosen selectbox.
361363 $ ( '.feedzy-chosen' ) . chosen ( { width : '100%' } ) ;
364+
365+ // on upload button click
366+ $ ( 'body' ) . on ( 'click' , '.feedzy-open-media' , function ( e ) {
367+ e . preventDefault ( ) ;
368+ const button = $ ( this ) ,
369+ wp_media_uploader = wp . media ( {
370+ title : feedzySetupWizardData . mediaUploadText . iframeTitle ,
371+ library : {
372+ type : 'image'
373+ } ,
374+ button : {
375+ text : feedzySetupWizardData . mediaUploadText . iframeButton
376+ } ,
377+ multiple : true
378+ } ) . on ( 'select' , function ( ) { // it also has "open" and "close" events
379+ const selectedAttachments = wp_media_uploader . state ( ) . get ( 'selection' ) ;
380+ const countSelected = selectedAttachments ?. toJSON ( ) ?. length ;
381+ button . parents ( '.fz-form-group' ) . find ( '.feedzy-media-preview' ) . remove ( ) ;
382+ // Display image preview when a single image is selected.
383+ if ( 1 === countSelected ) {
384+ const attachment = selectedAttachments . first ( ) . toJSON ( ) ;
385+ let attachmentUrl = attachment . url ;
386+ if ( attachment . sizes . thumbnail ) {
387+ attachmentUrl = attachment . sizes . thumbnail . url ;
388+ }
389+ if ( $ ( '.feedzy-media-preview' ) . length ) {
390+ $ ( '.feedzy-media-preview' ) . find ( 'img' ) . attr ( 'src' , attachmentUrl ) ;
391+ } else {
392+ $ ( '<div class="fz-form-group mb-20 feedzy-media-preview"><img src="' + attachmentUrl + '"></div>' ) . insertBefore ( button . parent ( ) ) ;
393+ }
394+ } else {
395+ $ (
396+ '<div class="fz-form-group mb-20 feedzy-media-preview fz-fallback-images pb-8">' +
397+ selectedAttachments
398+ ?. toJSON ( )
399+ ?. map ( ( { url, sizes } ) => {
400+ if ( sizes ?. thumbnail ) {
401+ url = sizes . thumbnail . url ;
402+ }
403+ return `<img width="150" height="150" src="${ url } " class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy">` ;
404+ } )
405+ . join ( '' ) +
406+ '</div>'
407+ ) . insertBefore ( button . parent ( ) ) ;
408+ }
409+ // Get all selected attachment ids.
410+ const ids = selectedAttachments . map ( function ( image ) {
411+ return image . id ;
412+ } ) . join ( ',' ) ;
413+
414+ button . parent ( ) . find ( '.feedzy-remove-media' ) . addClass ( 'is-show' ) ;
415+ button . parent ( ) . find ( 'input:hidden' ) . val ( ids ) . trigger ( 'change' ) ;
416+ $ ( '.feedzy-open-media' ) . html ( feedzySetupWizardData . mediaUploadText . actionButtonTextTwo ) ;
417+ } ) ;
418+
419+ wp_media_uploader . on ( ' open' , function ( ) {
420+ const selectedVal = button . parent ( ) . find ( 'input:hidden' ) . val ( ) ;
421+ if ( '' === selectedVal ) {
422+ return ;
423+ }
424+ const selection = wp_media_uploader . state ( ) . get ( 'selection' ) ;
425+
426+ selectedVal . split ( ',' ) . forEach ( function ( id ) {
427+ const attachment = wp . media . attachment ( id ) ;
428+ attachment . fetch ( ) ;
429+ selection . add ( attachment ? [ attachment ] : [ ] ) ;
430+ } ) ;
431+ } ) ;
432+
433+ wp_media_uploader . open ( ) ;
434+ } ) ;
435+
436+ $ ( document ) . on ( 'click' , '.feedzy-remove-media' , function ( e ) {
437+ $ ( this )
438+ e . preventDefault ( ) ;
439+ $ ( '.feedzy-media-preview' ) . remove ( ) ;
440+ $ ( this ) . removeClass ( 'is-show' ) ;
441+
442+ // Reset the input.
443+ $ ( 'input[name="feedzy_meta_data[default_thumbnail_id]"]' ) . val ( 0 ) ;
444+ $ ( '.feedzy-open-media' ) . html ( feedzySetupWizardData . mediaUploadText . actionButtonTextOne ) ;
445+ } ) ;
446+
447+ $ ( '#preflight' ) . on ( 'click' , function ( e ) {
448+ e . preventDefault ( ) ;
449+ const $fields = { } ;
450+ // collect all elements.
451+ $ ( '#smartwizard' )
452+ . find ( ':input' )
453+ . each ( function ( index , element ) {
454+ if ( 'undefined' === typeof $ ( element ) . attr ( 'name' ) ) {
455+ return ;
456+ }
457+ $fields [ $ ( element ) . attr ( 'name' ) ] = $ ( element ) . val ( ) ;
458+ } ) ;
459+ $fields [ 'feedzy_meta_data[source]' ] = $ ( '#wizard_feed_source' ) . val ( ) ;
460+ tb_show ( feedzySetupWizardData . dryRun . title , 'TB_inline?' ) ;
461+ $ ( '#TB_ajaxContent' ) . html ( feedzySetupWizardData . dryRun . loading ) ;
462+ $ . post (
463+ ajaxurl ,
464+ {
465+ security : window . feedzySetupWizardData . ajax . security ,
466+ fields : $ . param ( $fields ) ,
467+ action : 'feedzy' ,
468+ _action : 'dry_run' ,
469+ } ,
470+ function ( data ) {
471+ $ ( '#TB_ajaxContent' ) . addClass ( 'loaded' ) ;
472+ $ ( '#TB_ajaxContent div' ) . html ( data . data . output ) ;
473+ } ,
474+ ) ;
475+ } ) ;
362476} ) ;
0 commit comments