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

feat(homepage-posts): support deduplication toggling #1543

Merged
merged 6 commits into from
Oct 4, 2023
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
22 changes: 19 additions & 3 deletions includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,23 @@ public static function add_image_sizes() {
add_image_size( 'newspack-article-block-uncropped', 1200, 9999, false );
}

/**
* Whether the block should be included in the deduplication logic.
*
* @param array $attributes Block attributes.
*
* @return bool
*/
public static function should_deduplicate_block( $attributes ) {
/**
* Filters whether to use deduplication while rendering the given block.
*
* @param bool $deduplicate Whether to deduplicate.
* @param array $attributes The block attributes.
*/
return apply_filters( 'newspack_blocks_should_deduplicate', $attributes['deduplicate'] ?? true, $attributes );
}

/**
* Get all "specificPosts" ids from given blocks.
*
Expand All @@ -605,6 +622,7 @@ private static function get_specific_posts_from_blocks( $blocks, $block_name ) {
}
if (
$block_name === $block['blockName'] &&
self::should_deduplicate_block( $block['attrs'] ) &&
! empty( $block['attrs']['specificMode'] ) &&
! empty( $block['attrs']['specificPosts'] )
) {
Expand Down Expand Up @@ -670,9 +688,7 @@ public static function build_articles_query( $attributes, $block_name ) {
$args['orderby'] = 'post__in';
} else {
$args['posts_per_page'] = $posts_to_show;

$show_rendered_posts = apply_filters( 'newspack_blocks_homepage_shown_rendered_posts', false );
if ( $show_rendered_posts ) {
if ( ! self::should_deduplicate_block( $attributes ) ) {
$args['post__not_in'] = [ get_the_ID() ];
} else {
if ( count( $newspack_blocks_all_specific_posts_ids ) ) {
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/homepage-articles/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@
"type": "array",
"default": [ "publish" ],
"items": { "type": "string" }
},
"deduplicate": {
"type": "boolean",
"default": true
}
}
}
10 changes: 10 additions & 0 deletions src/blocks/homepage-articles/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,16 @@ class Edit extends Component {
/>
)
) }
<ToggleControl
label={ __( 'Use deduplication logic', 'newspack-blocks' ) }
help={ __(
'If unchecked, this block will be excluded from the deduplication logic and may show duplicate posts.',
'newspack-blocks'
) }
checked={ attributes.deduplicate }
onChange={ () => setAttributes( { deduplicate: ! attributes.deduplicate } ) }
className="newspack-blocks-deduplication-toggle"
/>
</PanelBody>
<PanelBody title={ __( 'Featured Image Settings', 'newspack-blocks' ) }>
<PanelRow>
Expand Down
26 changes: 22 additions & 4 deletions src/blocks/homepage-articles/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ function* getPostsForBlock( block ) {
return postsIds;
}

/**
* Whether a block uses deduplication.
*
* @param {string} clientId
*
* @return {boolean} whether the block uses deduplication
*/
function shouldDeduplicate( clientId ) {
const { getBlock } = select( 'core/block-editor' );
const block = getBlock( clientId );
return block?.attributes?.deduplicate;
}

const createFetchPostsSaga = blockNames => {
/**
* "worker" Saga: will be fired on REFLOW actions
Expand Down Expand Up @@ -138,8 +151,8 @@ const createFetchPostsSaga = blockNames => {
const blockQueries = getBlockQueries( blocks, blockNames );

// Use requested specific posts ids as the starting state of exclusion list.
const specificPostsId = blockQueries.reduce( ( acc, { postsQuery } ) => {
if ( postsQuery.include ) {
const specificPostsId = blockQueries.reduce( ( acc, { clientId, postsQuery } ) => {
if ( shouldDeduplicate( clientId ) && postsQuery.include ) {
acc = [ ...acc, ...postsQuery.include ];
}
return acc;
Expand All @@ -148,14 +161,19 @@ const createFetchPostsSaga = blockNames => {
let exclude = sanitizePostList( [ ...specificPostsId, getCurrentPostId() ] );
while ( blockQueries.length ) {
const nextBlock = blockQueries.shift();
nextBlock.postsQuery.exclude = exclude;
const deduplicate = shouldDeduplicate( nextBlock.clientId );
if ( deduplicate ) {
nextBlock.postsQuery.exclude = exclude;
}
let fetchedPostIds = [];
try {
fetchedPostIds = yield call( getPostsForBlock, nextBlock );
} catch ( e ) {
yield put( { type: 'UPDATE_BLOCK_ERROR', clientId: nextBlock.clientId, error: e.message } );
}
exclude = [ ...exclude, ...fetchedPostIds ];
if ( deduplicate ) {
exclude = [ ...exclude, ...fetchedPostIds ];
}
}

yield put( { type: 'ENABLE_UI' } );
Expand Down
3 changes: 1 addition & 2 deletions src/blocks/homepage-articles/templates/articles-loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ function( $data ) {

Newspack_Blocks::filter_excerpt( $attributes );

$enable_post_duplication = apply_filters( 'newspack_blocks_homepage_enable_duplication', false );
while ( $article_query->have_posts() ) {
$article_query->the_post();
if ( ! $enable_post_duplication ) {
if ( Newspack_Blocks::should_deduplicate_block( $attributes ) ) {
$newspack_blocks_post_id[ get_the_ID() ] = true;
}
echo Newspack_Blocks::template_inc( __DIR__ . '/article.php', array( 'attributes' => $attributes ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
Expand Down
1 change: 1 addition & 0 deletions src/blocks/homepage-articles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const POST_QUERY_ATTRIBUTES = [
'categoryExclusions',
'postType',
'includedPostStatuses',
'deduplicate',
];

type HomepageArticlesAttributes = {
Expand Down