Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carousel: handle posts without featured images #731

Merged
merged 5 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 69 additions & 64 deletions src/blocks/carousel/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,15 @@ class Edit extends Component {
}
);
const dateFormat = __experimentalGetSettings().formats.date;
const hasNoPosts = latestPosts && ! latestPosts.length;
const hasOnePost = latestPosts && latestPosts.length === 1;
return (
<Fragment>
<div className={ classes } ref={ this.carouselRef }>
{ latestPosts && ! latestPosts.length && (
<Placeholder>{ __( 'Sorry, no posts were found.' ) }</Placeholder>
{ hasNoPosts && (
<Placeholder className="component-placeholder__align-center">
<div style={ { margin: 'auto' } }>{ __( 'Sorry, no posts were found.' ) }</div>
</Placeholder>
) }
{ ! latestPosts && (
<Placeholder icon={ <Spinner /> } className="component-placeholder__align-center" />
Expand All @@ -160,70 +164,71 @@ class Edit extends Component {
</Fragment>
) }
<div className="swiper-wrapper">
{ latestPosts.map(
post =>
post.newspack_featured_image_src && (
<article className="post-has-image swiper-slide" key={ post.id }>
<figure className="post-thumbnail">
{ post.newspack_featured_image_src && (
<a href="#" rel="bookmark">
<img src={ post.newspack_featured_image_src.large } alt="" />
</a>
) }
</figure>
<div className="entry-wrapper">
{ post.newspack_post_sponsors && (
<span className="cat-links sponsor-label">
<span className="flag">
{ post.newspack_post_sponsors[ 0 ].flag }
</span>
</span>
) }
{ showCategory &&
post.newspack_category_info.length &&
! post.newspack_post_sponsors && (
<div className="cat-links">
<a href="#">{ decodeEntities( post.newspack_category_info ) }</a>
</div>
) }
<h3 className="entry-title">
<a href="#">{ decodeEntities( post.title.rendered.trim() ) }</a>
</h3>
<div className="entry-meta">
{ post.newspack_post_sponsors &&
formatSponsorLogos( post.newspack_post_sponsors ) }
{ post.newspack_post_sponsors &&
formatSponsorByline( post.newspack_post_sponsors ) }
{ showAuthor &&
showAvatar &&
! post.newspack_post_sponsors &&
formatAvatars( post.newspack_author_info ) }
{ showAuthor &&
! post.newspack_post_sponsors &&
formatByline( post.newspack_author_info ) }
{ showDate && (
<time className="entry-date published" key="pub-date">
{ dateI18n( dateFormat, post.date_gmt ) }
</time>
) }
{ latestPosts.map( post => (
<article className="post-has-image swiper-slide" key={ post.id }>
<figure className="post-thumbnail">
<a href="#" rel="bookmark">
{ post.newspack_featured_image_src ? (
<img src={ post.newspack_featured_image_src.large } alt="" />
) : (
<div className="wp-block-newspack-blocks-carousel__placeholder"></div>
) }
</a>
</figure>
<div className="entry-wrapper">
{ post.newspack_post_sponsors && (
<span className="cat-links sponsor-label">
<span className="flag">{ post.newspack_post_sponsors[ 0 ].flag }</span>
</span>
) }
{ showCategory &&
post.newspack_category_info.length &&
! post.newspack_post_sponsors && (
<div className="cat-links">
<a href="#">{ decodeEntities( post.newspack_category_info ) }</a>
</div>
</div>
</article>
)
) }
) }
<h3 className="entry-title">
<a href="#">{ decodeEntities( post.title.rendered.trim() ) }</a>
</h3>
<div className="entry-meta">
{ post.newspack_post_sponsors &&
formatSponsorLogos( post.newspack_post_sponsors ) }
{ post.newspack_post_sponsors &&
formatSponsorByline( post.newspack_post_sponsors ) }
{ showAuthor &&
showAvatar &&
! post.newspack_post_sponsors &&
formatAvatars( post.newspack_author_info ) }
{ showAuthor &&
! post.newspack_post_sponsors &&
formatByline( post.newspack_author_info ) }
{ showDate && (
<time className="entry-date published" key="pub-date">
{ dateI18n( dateFormat, post.date_gmt ) }
</time>
) }
</div>
</div>
</article>
) ) }
</div>
<button
className="amp-carousel-button amp-carousel-button-prev swiper-button-prev"
ref={ this.btnPrevRef }
/>
<button
className="amp-carousel-button amp-carousel-button-next swiper-button-next"
ref={ this.btnNextRef }
/>
<div
className="swiper-pagination-bullets amp-pagination"
ref={ this.paginationRef }
/>
{ ! hasNoPosts && ! hasOnePost && (
<>
<button
className="amp-carousel-button amp-carousel-button-prev swiper-button-prev"
ref={ this.btnPrevRef }
/>
<button
className="amp-carousel-button amp-carousel-button-next swiper-button-next"
ref={ this.btnNextRef }
/>
<div
className="swiper-pagination-bullets amp-pagination"
ref={ this.paginationRef }
/>
</>
) }
</Fragment>
) }
</div>
Expand Down
38 changes: 23 additions & 15 deletions src/blocks/carousel/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ function newspack_blocks_render_block_carousel( $attributes ) {
}
$classes = Newspack_Blocks::block_classes( 'carousel', $attributes, $other );

$article_query = new WP_Query( Newspack_Blocks::build_articles_query( $attributes, apply_filters( 'newspack_blocks_block_name', 'newspack-blocks/carousel' ) ) );
$article_query = new WP_Query( Newspack_Blocks::build_articles_query( $attributes, apply_filters( 'newspack_blocks_block_name', 'newspack-blocks/carousel' ) ) );
if ( false === $article_query->have_posts() ) {
return;
}
$counter = 0;
$article_classes = [
'post-has-image',
Expand All @@ -43,30 +46,32 @@ function newspack_blocks_render_block_carousel( $attributes ) {
if ( $article_query->have_posts() ) :
while ( $article_query->have_posts() ) :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the carousel has no posts, I still see empty carousel UI on the front-end. At this point, if ! $article_query->have_posts(), we should just return early before the UI below line 220 gets rendered.

Screen Shot 2021-04-05 at 8 50 12 PM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 9660922

$article_query->the_post();
$authors = Newspack_Blocks::prepare_authors();
if ( ! has_post_thumbnail() ) {
continue;
}
$authors = Newspack_Blocks::prepare_authors();
$newspack_blocks_post_id[ get_the_ID() ] = true;

// Get sponsors for this post.
$sponsors = Newspack_Blocks::get_all_sponsors( get_the_id() );

$counter++;
$has_featured_image = has_post_thumbnail();
?>

<article data-post-id="<?php the_id(); ?>" class="<?php echo esc_attr( implode( ' ', $article_classes ) ); ?>">
<figure class="post-thumbnail">
<a href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark">
<?php
the_post_thumbnail(
'large',
array(
'object-fit' => 'cover',
'layout' => 'fill',
)
);
?>
<?php if ( $has_featured_image ) : ?>
<?php
the_post_thumbnail(
'large',
array(
'object-fit' => 'cover',
'layout' => 'fill',
)
);
?>
<?php else : ?>
<div class="wp-block-newspack-blocks-carousel__placeholder"></div>
<?php endif; ?>
</a>
</figure>
<div class="entry-wrapper">
Expand Down Expand Up @@ -231,7 +236,7 @@ function newspack_blocks_render_block_carousel( $attributes ) {
'<div class="swiper-pagination-bullets amp-pagination">%s</div>',
implode( '', $buttons )
);
$navigation = sprintf(
$navigation = 1 === $counter ? '' : sprintf(
'<button class="swiper-button swiper-button-prev" aria-label="%s"></button><button class="swiper-button swiper-button-next" aria-label="%s"></button>',
esc_attr__( 'Previous Slide', 'newspack-blocks' ),
esc_attr__( 'Next Slide', 'newspack-blocks' )
Expand All @@ -252,6 +257,9 @@ function newspack_blocks_render_block_carousel( $attributes ) {
$data_attributes[] = sprintf( 'data-autoplay_delay=%s', esc_attr( $delay ) );
}
Newspack_Blocks::enqueue_view_assets( 'carousel' );
if ( 1 === $counter ) {
$selector = '';
}
return sprintf(
'<div class="%1$s" id="wp-block-newspack-carousel__%2$d" %3$s>%4$s%5$s%6$s</div>',
esc_attr( $classes ),
Expand Down
5 changes: 5 additions & 0 deletions src/blocks/carousel/view.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@import '../../shared/sass/variables';
@import '../../shared/sass/mixins';
@import '../../shared/sass/colors';

.wp-block-newspack-blocks-carousel {
position: relative;
Expand Down Expand Up @@ -87,6 +88,10 @@
width: 100%;
}
}
&__placeholder {
height: 420px;
background: $color__background-screen;
}
p {
white-space: normal;
}
Expand Down