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

Fix: exclude specific posts from other blocks from query #500

Merged
merged 3 commits into from
Jun 5, 2020

Conversation

adekbadek
Copy link
Member

@adekbadek adekbadek commented May 28, 2020

All Submissions:

Changes proposed in this Pull Request:

Ideally, creation of $all_specific_posts_ids array should happen once per page request (currently it happens once per HP block), but I don't know how to do that – all suggestions welcome!

Closes #498

How to test the changes in this Pull Request:

  1. On master, reproduce Deduplication not handling specific posts #498
  2. Switch to this branch, observe that Deduplication not handling specific posts #498 is not reproducible

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Copy link
Contributor

@claudiulodro claudiulodro left a comment

Choose a reason for hiding this comment

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

For the globals question, you could do something like:

global $all_specific_posts_ids; // Or maybe even a static variable (https://www.tutorialspoint.com/php/php_static_variables.htm).

if ( ! is_array( $all_specific_post_ids ) ) {
	// Get all blocks and gather specificPosts ids of all Homepage Articles blocks.
	$blocks                 = parse_blocks( get_the_content() );
	$block_name             = apply_filters( 'newspack_blocks_block_name', 'newspack-blocks/homepage-articles' );
	$all_specific_posts_ids = array_reduce(
		$blocks,
		function ( $acc, $block ) use ( $block_name ) {
			if (
				$block_name === $block['blockName'] &&
				isset( $block['attrs']['specificMode'] ) &&
				isset( $block['attrs']['specificPosts'] ) &&
				count( $block['attrs']['specificPosts'] )
			) {
				return array_merge(
					$block['attrs']['specificPosts'],
					$acc
				);
			}
			return $acc;
		},
		[]
	);
}

@adekbadek
Copy link
Member Author

Thanks Claudiu, that works! In: dd93454

Copy link
Contributor

@claudiulodro claudiulodro left a comment

Choose a reason for hiding this comment

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

Looks good and works well 👍

Definitely doesn't need to be revised here, but here's a fun protip for future reference:

isset( $block['attrs']['specificMode'] ) && isset( $block['attrs']['specificPosts'] )

can be simplified as

isset( $block['attrs']['specificMode'], $block['attrs']['specificPosts'] )

You can pass any number of things into isset and it'll return true only if they are all set. :)

@adekbadek adekbadek merged commit 31181a5 into master Jun 5, 2020
@adekbadek adekbadek deleted the fix/specific-post-bug branch June 5, 2020 09:02
matticbot pushed a commit that referenced this pull request Jun 9, 2020
## [1.7.1](v1.7.0...v1.7.1) (2020-06-09)

### Bug Fixes

* always display query controls; disallow choosing 0 posts ([#497](#497)) ([b956111](b956111))
* correct order of arguments in implodes ([0f45074](0f45074))
* don't use main wp_query when rendering homepage posts ([9c804f6](9c804f6))
* reset postdata after restoring wp_query ([d9d01a6](d9d01a6))
* **homepage-posts:** exclude specific posts from other blocks from query ([#500](#500)) ([31181a5](31181a5)), closes [#498](#498)
* make bottom margin styles less specific for easier overrides ([#483](#483)) ([93c3aff](93c3aff))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.7.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deduplication not handling specific posts
3 participants