Skip to content

Commit 8a082bf

Browse files
feat: improve onboarding import process
1 parent 2804edc commit 8a082bf

File tree

5 files changed

+231
-14
lines changed

5 files changed

+231
-14
lines changed

includes/admin/feedzy-rss-feeds-admin.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,10 @@ public function feedzy_load_setup_wizard_page() {
19491949
* Enqueue setup wizard required scripts.
19501950
*/
19511951
public function feedzy_enqueue_setup_wizard_scripts() {
1952+
if ( ! did_action( 'wp_enqueue_media' ) ) {
1953+
wp_enqueue_media();
1954+
}
1955+
19521956
wp_enqueue_style( $this->plugin_name . '_chosen' );
19531957
wp_enqueue_style( $this->plugin_name . '_smart_wizard', FEEDZY_ABSURL . 'css/smart_wizard_all.min.css', array(), $this->version );
19541958
wp_enqueue_style( $this->plugin_name . '_setup_wizard', FEEDZY_ABSURL . 'includes/views/css/style-wizard.css', array( $this->plugin_name . '-settings' ), $this->version, 'all' );
@@ -1974,6 +1978,19 @@ public function feedzy_enqueue_setup_wizard_scripts() {
19741978
'firstButtonText' => __( 'Create Page', 'feedzy-rss-feeds' ),
19751979
'secondButtonText' => __( 'Do not create', 'feedzy-rss-feeds' ),
19761980
),
1981+
'mediaUploadText' => array(
1982+
'iframeTitle' => __( 'Select image', 'feedzy-rss-feeds' ),
1983+
'iframeButton' => __( 'Set default image', 'feedzy-rss-feeds' ),
1984+
'actionButtonTextOne' => __( 'Choose image', 'feedzy-rss-feeds' ),
1985+
'actionButtonTextTwo' => __( 'Replace image', 'feedzy-rss-feeds' ),
1986+
'actionButtonTextThree' => __( 'Remove image', 'feedzy-rss-feeds' ),
1987+
),
1988+
'dryRun' => array(
1989+
'loading' => '<p class="hide-when-loaded">' . __( 'Processing the source and loading the items that will be imported when it runs', 'feedzy-rss-feeds' ) . '...</p>'
1990+
. '<p><b>' . __( 'Please note that if some of these items have already have been imported in previous runs with the same filters, they may be shown here but will not be imported again.', 'feedzy-rss-feeds' ) . '</b></p>'
1991+
. '<p class="loading-img hide-when-loaded"><img src="' . includes_url( 'images/wpspin-2x.gif' ) . '"></p><div></div>',
1992+
'title' => __( 'Importable Items', 'feedzy-rss-feeds' ),
1993+
),
19771994
)
19781995
);
19791996
}

includes/admin/feedzy-rss-feeds-import.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3945,6 +3945,8 @@ private function wizard_import_feed() {
39453945

39463946
$post_type = ! empty( $_POST['post_type'] ) ? sanitize_text_field( wp_unslash( $_POST['post_type'] ) ) : '';
39473947
$post_status = ! empty( $_POST['post_status'] ) ? sanitize_text_field( wp_unslash( $_POST['post_status'] ) ) : '';
3948+
$fallback_image = ! empty( $_POST['fallback_image'] ) ? sanitize_text_field( wp_unslash( $_POST['fallback_image'] ) ) : '';
3949+
$excluded_post_title = ! empty( $_POST['excluded_post_title'] ) ? sanitize_text_field( wp_unslash( $_POST['excluded_post_title'] ) ) : '';
39483950
$wizard_data = get_option( 'feedzy_wizard_data', array() );
39493951
$wizard_data = ! empty( $wizard_data ) ? $wizard_data : array();
39503952
$wizard_data['post_type'] = $post_type;
@@ -3988,7 +3990,25 @@ private function wizard_import_feed() {
39883990
// Update wizard data.
39893991
$wizard_data['job_id'] = $job_id;
39903992
update_option( 'feedzy_wizard_data', $wizard_data );
3991-
3993+
3994+
$filter_conditions = array(
3995+
'match' => 'all',
3996+
'conditions' => array(),
3997+
);
3998+
3999+
if ( ! empty( $excluded_post_title ) ) {
4000+
$filter_conditions['conditions'] = array(
4001+
array(
4002+
'field' => 'title',
4003+
'operator' => 'not_contains',
4004+
'value' => $excluded_post_title,
4005+
),
4006+
);
4007+
}
4008+
4009+
update_post_meta( $job_id, 'filter_conditions', wp_json_encode( $filter_conditions ) );
4010+
update_post_meta( $job_id, 'default_thumbnail_id', $fallback_image );
4011+
39924012
$response = array(
39934013
'status' => true,
39944014
);

includes/layouts/setup-wizard.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
}
2525
$published_status = array( 'publish', 'draft' );
2626

27+
add_thickbox();
2728
?>
2829
<div class="feedzy-wizard-wrap feedzy-wrap">
2930
<div class="feedzy-header--small">
@@ -177,25 +178,61 @@
177178
</select>
178179
</div>
179180
</div>
181+
<div class="form-block">
182+
<div class="fz-form-row">
183+
<div class="fz-form-col-6">
184+
<label class="form-label">
185+
<?php esc_html_e( 'Post status', 'feedzy-rss-feeds' ); ?>
186+
</label>
187+
<div class="mx-320">
188+
<select id="feedzy_post_status" class="form-control feedzy-chosen" name="feedzy_meta_data[import_post_status]">
189+
<?php
190+
foreach ( $published_status as $_status ) {
191+
?>
192+
<option value="<?php echo esc_attr( $_status ); ?>">
193+
<?php echo esc_html( ucfirst( $_status ) ); ?>
194+
</option>
195+
<?php
196+
}
197+
?>
198+
</select>
199+
</div>
200+
</div>
201+
<div class="fz-form-col-6">
202+
<label class="form-label">
203+
<?php esc_html_e( 'Fallback Image', 'feedzy-rss-feeds' ); ?>
204+
</label>
205+
<div class="fz-cta-group pb-8">
206+
<a
207+
href="javascript:;"
208+
class="feedzy-open-media btn btn-outline-primary"
209+
>
210+
<?php esc_html_e( 'Choose image', 'feedzy-rss-feeds' ); ?>
211+
</a>
212+
<a href="javascript:;" class="feedzy-remove-media btn btn-outline-primary"><?php esc_html_e( 'Remove', 'feedzy-rss-feeds' ); ?></a>
213+
<input type="hidden" name="feedzy_meta_data[default_thumbnail_id]" id="feed-post-default-thumbnail" value="">
214+
<div class="help-text pt-8">
215+
<?php esc_html_e( 'Optional. Used as fallback for imported posts without images.', 'feedzy-rss-feeds' ); ?>
216+
</div>
217+
</div>
218+
</div>
219+
</div>
220+
</div>
180221
<div class="form-block">
181222
<label class="form-label">
182-
<?php esc_html_e( 'Post status', 'feedzy-rss-feeds' ); ?>
223+
<?php esc_html_e( 'Exclude posts with title not containing', 'feedzy-rss-feeds' ); ?>
183224
</label>
184-
<div class="mx-320">
185-
<select id="feedzy_post_status" class="form-control feedzy-chosen" name="feedzy_meta_data[import_post_status]">
186-
<?php
187-
foreach ( $published_status as $_status ) {
188-
?>
189-
<option value="<?php echo esc_attr( $_status ); ?>">
190-
<?php echo esc_html( ucfirst( $_status ) ); ?>
191-
</option>
192-
<?php
193-
}
194-
?>
195-
</select>
225+
<div class="fz-form-group">
226+
<input type="text" id="feedzy_exclude_title" name="feedzy_meta_data[exclude_post_title]" class="form-control" value="" />
227+
<div class="help-text pt-8">
228+
<?php esc_html_e( 'Posts will not be imported if their title includes these keywords.', 'feedzy-rss-feeds' ); ?>
229+
</div>
196230
</div>
197231
</div>
198232
<div class="form-block">
233+
<button class="btn btn-ghost" id="preflight">
234+
<?php esc_html_e( 'Preview Import', 'feedzy-rss-feeds' ); ?>
235+
</button>
199236
<button class="btn btn-primary fz-wizard-feed-import">
200237
<?php esc_html_e( 'Create a draft import', 'feedzy-rss-feeds' ); ?> <span class="dashicons dashicons-arrow-right-alt"></span>
201238
</button>

includes/views/css/style-wizard.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,32 @@
471471
.feedzy-accordion-item__title h2 {
472472
margin-bottom: 0;
473473
}
474+
475+
#TB_ajaxContent ul {
476+
list-style: decimal;
477+
margin-left: 20px;
478+
}
479+
480+
#TB_ajaxContent ul li {
481+
padding: 5px !important;
482+
}
483+
484+
#TB_ajaxContent p.loading-img {
485+
text-align: center;
486+
}
487+
488+
#TB_ajaxContent.loaded p.hide-when-loaded {
489+
display: none;
490+
}
491+
492+
#TB_ajaxContent div.dry_run span {
493+
display: block;
494+
}
495+
496+
#TB_ajaxContent div.dry_run span i.pass {
497+
color: #149714;
498+
}
499+
500+
#TB_ajaxContent div.dry_run span i.fail {
501+
color: #ca4a1f;
502+
}

js/feedzy-setup-wizard.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)