Skip to content

Commit

Permalink
fix(homepage-posts): check existing "specific posts" recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe committed Sep 28, 2023
1 parent 814932e commit bcdd667
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,39 @@ public static function add_image_sizes() {
add_image_size( 'newspack-article-block-uncropped', 1200, 9999, false );
}

/**
* Get all "specificPosts" ids from given blocks.
*
* @param array $blocks An array of blocks.
* @param string $block_name Name of the block requesting the query.
*
* @return array All "specificPosts" ids from all eligible blocks.
*/
private static function get_specific_posts_from_blocks( $blocks, $block_name ) {
$specific_posts = [];
foreach ( $blocks as $block ) {
error_log( print_r( array_keys($block ), true));
if ( ! empty( $block['innerBlocks'] ) ) {
$specific_posts = array_merge(
$specific_posts,
self::get_specific_posts_from_blocks( $block['innerBlocks'], $block_name )
);
continue;
}
if (
$block_name === $block['blockName'] &&
! empty( $block['attrs']['specificMode'] ) &&
! empty( $block['attrs']['specificPosts'] )
) {
$specific_posts = array_merge(
$specific_posts,
$block['attrs']['specificPosts']
);
}
}
return $specific_posts;
}

/**
* Builds and returns query args based on block attributes.
*
Expand All @@ -606,23 +639,7 @@ public static function build_articles_query( $attributes, $block_name ) {
global $newspack_blocks_all_specific_posts_ids;
if ( ! is_array( $newspack_blocks_all_specific_posts_ids ) ) {
$blocks = parse_blocks( get_the_content() );
$newspack_blocks_all_specific_posts_ids = array_reduce(
$blocks,
function ( $acc, $block ) use ( $block_name ) {
if (
$block_name === $block['blockName'] &&
isset( $block['attrs']['specificMode'], $block['attrs']['specificPosts'] ) &&
count( $block['attrs']['specificPosts'] )
) {
return array_merge(
$block['attrs']['specificPosts'],
$acc
);
}
return $acc;
},
[]
);
$newspack_blocks_all_specific_posts_ids = self::get_specific_posts_from_blocks( $blocks, $block_name );
}

$post_type = isset( $attributes['postType'] ) ? $attributes['postType'] : [ 'post' ];
Expand Down

0 comments on commit bcdd667

Please sign in to comment.