Skip to content

Commit dcc8bf5

Browse files
feat: adds multiple fallback images support (#1033)
* Adds multiple fallback images support * Remove extra help text * fix: random get fallback item * fix: assign global global fallback --------- Co-authored-by: Hardeep Asrani <[email protected]>
1 parent 8d7f755 commit dcc8bf5

File tree

3 files changed

+87
-24
lines changed

3 files changed

+87
-24
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public function enqueue_styles() {
165165
'clearLogButton' => __( 'Clear Log', 'feedzy-rss-feeds' ),
166166
'okButton' => __( 'Ok', 'feedzy-rss-feeds' ),
167167
'removeErrorLogsMsg' => __( 'Removed all error logs.', 'feedzy-rss-feeds' ),
168+
// translators: %d select images count.
169+
'action_btn_text_3' => __( '(%d) images selected', 'feedzy-rss-feeds' ),
168170
'importButton' => sprintf(
169171
'<a href="#" class="page-title-action fz-export-import-btn%1$s"><span class="dashicons %2$s"></span>%3$s</a>',
170172
! feedzy_is_pro() ? ' only-pro' : '',
@@ -523,6 +525,9 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
523525
} else {
524526
if ( 'import_post_content' === $key ) {
525527
$val = feedzy_custom_tag_escape( $val );
528+
} elseif ( 'default_thumbnail_id' === $key && ! empty( $val ) ) {
529+
$val = explode( ',', $val );
530+
$val = array_map( 'intval', $val );
526531
} else {
527532
$val = wp_kses( $val, apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
528533
}
@@ -1443,9 +1448,10 @@ private function run_job( $job, $max ) {
14431448
}
14441449

14451450
// Get default thumbnail ID.
1446-
$default_thumbnail = ! empty( $this->free_settings['general']['default-thumbnail-id'] ) ? (int) $this->free_settings['general']['default-thumbnail-id'] : 0;
1451+
$global_fallback_thumbnail = ! empty( $this->free_settings['general']['default-thumbnail-id'] ) ? (int) $this->free_settings['general']['default-thumbnail-id'] : 0;
14471452
if ( feedzy_is_pro() ) {
14481453
$default_thumbnail = get_post_meta( $job->ID, 'default_thumbnail_id', true );
1454+
$default_thumbnail = ! empty( $default_thumbnail ) ? explode( ',', (string) $default_thumbnail ) : $global_fallback_thumbnail;
14491455
}
14501456

14511457
// Note: this implementation will only work if only one of the fields is allowed to provide
@@ -2092,7 +2098,13 @@ function( $term ) {
20922098

20932099
// Set default thumbnail image.
20942100
if ( ! $img_success && ! empty( $default_thumbnail ) ) {
2095-
$img_success = set_post_thumbnail( $new_post_id, $default_thumbnail );
2101+
if ( is_array( $default_thumbnail ) ) {
2102+
$random_key = array_rand( $default_thumbnail );
2103+
$default_thumbnail_id = isset( $default_thumbnail[ $random_key ] ) ? $default_thumbnail[ $random_key ] : 0;
2104+
} else {
2105+
$default_thumbnail_id = $default_thumbnail;
2106+
}
2107+
$img_success = set_post_thumbnail( $new_post_id, $default_thumbnail_id );
20962108
}
20972109

20982110
if ( ! $img_success ) {

includes/views/import-metabox-edit.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -681,21 +681,35 @@ class="fz-switch-toggle" type="checkbox" value="yes"
681681
<div class="fz-form-group">
682682
<label class="form-label"><?php esc_html_e( 'Select an image to be the fallback featured image.', 'feedzy-rss-feeds' ); ?></label>
683683
<?php
684-
$btn_label = esc_html__( 'Choose image', 'feedzy-rss-feeds' );
685-
if ( $default_thumbnail_id ) :
684+
$btn_label = esc_html__( 'Choose image', 'feedzy-rss-feeds' );
685+
$default_thumbnail_id = ! empty( $default_thumbnail_id ) ? explode( ',', (string) $default_thumbnail_id ) : array();
686+
if ( ! empty( $default_thumbnail_id ) ) :
686687
$btn_label = esc_html__( 'Replace image', 'feedzy-rss-feeds' );
687688
?>
688689
<div class="fz-form-group mb-20 feedzy-media-preview">
689-
<?php echo wp_get_attachment_image( $default_thumbnail_id, 'thumbnail' ); ?>
690+
<?php
691+
if ( count( $default_thumbnail_id ) > 1 ) {
692+
?>
693+
<a href="javascript:;" class="btn btn-outline-primary feedzy-images-selected">
694+
<?php
695+
// translators: %d select images count.
696+
echo esc_html( sprintf( __( '(%d) images selected', 'feedzy-rss-feeds' ), count( $default_thumbnail_id ) ) );
697+
?>
698+
</a>
699+
<?php
700+
} else {
701+
echo wp_get_attachment_image( reset( $default_thumbnail_id ), 'thumbnail' );
702+
}
703+
?>
690704
</div>
691705
<?php endif; ?>
692706
<div class="fz-cta-group pb-8">
693707
<a href="javascript:;" class="feedzy-open-media btn btn-outline-primary"><?php echo esc_html( $btn_label ); ?></a>
694-
<a href="javascript:;" class="feedzy-remove-media btn btn-outline-primary <?php echo $default_thumbnail_id ? esc_attr( 'is-show' ) : ''; ?>"><?php esc_html_e( 'Remove', 'feedzy-rss-feeds' ); ?></a>
695-
<input type="hidden" name="feedzy_meta_data[default_thumbnail_id]" id="feed-post-default-thumbnail" value="<?php echo esc_attr( $default_thumbnail_id ); ?>">
708+
<a href="javascript:;" class="feedzy-remove-media btn btn-outline-primary <?php echo ! empty( $default_thumbnail_id ) ? esc_attr( 'is-show' ) : ''; ?>"><?php esc_html_e( 'Remove', 'feedzy-rss-feeds' ); ?></a>
709+
<input type="hidden" name="feedzy_meta_data[default_thumbnail_id]" id="feed-post-default-thumbnail" value="<?php echo esc_attr( implode( ',', $default_thumbnail_id ) ); ?>">
696710
</div>
697711
<div class="help-text pt-8">
698-
<?php esc_html_e( 'Helpful if you want to set a fallback image for feed items that don\'t have an image. Default it will be considered the one from global settings.', 'feedzy-rss-feeds' ); ?>
712+
<?php esc_html_e( 'Helpful for setting a fallback image for feed items without an image. If multiple fallback images are selected, one of them will be randomly assigned to each post without an image during the import process.', 'feedzy-rss-feeds' ); ?>
699713
</div>
700714
</div>
701715
</div>

includes/views/js/import-metabox-edit.js

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -994,23 +994,60 @@
994994
button: {
995995
text: feedzy.i10n.media_iframe_button
996996
},
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();
10131042
});
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+
} );
10141051
}
10151052
}(jQuery, feedzy));
10161053

0 commit comments

Comments
 (0)