Skip to content

Commit

Permalink
feat: exclude current post in homepage posts block (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefferson Rabb authored Jun 30, 2020
1 parent 6261e3f commit 030024d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ function ( $acc, $block ) use ( $block_name ) {
}
$args['post__not_in'] = array_merge(
$args['post__not_in'] ?? [],
array_keys( $newspack_blocks_post_id )
array_keys( $newspack_blocks_post_id ),
get_the_ID() ? [ get_the_ID() ] : []
);
if ( $authors && count( $authors ) ) {
$args['author__in'] = $authors;
Expand Down
3 changes: 2 additions & 1 deletion src/blocks/homepage-articles/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const createFetchPostsSaga = blockName => {
yield delay( 300 );

const { getBlocks } = select( 'core/block-editor' );
const { getCurrentPostId } = select( 'core/editor' );

yield put( { type: 'DISABLE_UI' } );

Expand All @@ -130,7 +131,7 @@ const createFetchPostsSaga = blockName => {
return acc;
}, [] );

let exclude = specificPostsId;
let exclude = [ ...specificPostsId, getCurrentPostId() ];
while ( blockQueries.length ) {
const nextBlock = blockQueries.shift();
nextBlock.postsQuery.exclude = exclude;
Expand Down
6 changes: 6 additions & 0 deletions src/blocks/homepage-articles/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ function getRenderedPostsIds() {
);
const postIds = Array.from( postEls ).map( el => el.getAttribute( 'data-post-id' ) );

postIds.push(
document
.querySelector( '.wp-block-newspack-blocks-homepage-articles > div[data-current-post-id]' )
.getAttribute( 'data-current-post-id' )
);

return [ ...new Set( postIds ) ]; // Make values unique with Set
}

Expand Down
4 changes: 2 additions & 2 deletions src/blocks/homepage-articles/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function( $attribute ) {
class="<?php echo esc_attr( $classes ); ?>"
style="<?php echo esc_attr( $styles ); ?>"
>
<div data-posts>
<div data-posts data-current-post-id="<?php the_ID(); ?>">
<?php if ( '' !== $attributes['sectionHeader'] ) : ?>
<h2 class="article-section-title">
<span><?php echo wp_kses_post( $attributes['sectionHeader'] ); ?></span>
Expand Down Expand Up @@ -261,7 +261,7 @@ function newspack_blocks_inject_amp_state() {
if ( ! $newspack_blocks_post_id || ! count( $newspack_blocks_post_id ) ) {
return;
}
$post_ids = implode( ', ', array_keys( $newspack_blocks_post_id ) );
$post_ids = implode( ', ', array_merge( array_keys( $newspack_blocks_post_id ), [ get_the_ID() ] ) );
ob_start();
?>
<amp-state id='newspackHomepagePosts'>
Expand Down

0 comments on commit 030024d

Please sign in to comment.