Skip to content

Commit fa1a3b4

Browse files
authored
Merge pull request #517 from Automattic/master
Release 09/06/2020
2 parents f5a3e21 + 78571c2 commit fa1a3b4

File tree

4 files changed

+65
-33
lines changed

4 files changed

+65
-33
lines changed

class-newspack-blocks.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static function block_classes( $type, $attributes = array(), $extra = arr
195195
$classes = array_merge( $classes, $extra );
196196
}
197197

198-
return implode( $classes, ' ' );
198+
return implode( ' ', $classes );
199199
}
200200

201201
/**
@@ -320,6 +320,31 @@ public static function build_articles_query( $attributes ) {
320320
if ( ! $newspack_blocks_post_id ) {
321321
$newspack_blocks_post_id = array();
322322
}
323+
324+
// Get all blocks and gather specificPosts ids of all Homepage Articles blocks.
325+
global $newspack_blocks_all_specific_posts_ids;
326+
if ( ! is_array( $newspack_blocks_all_specific_posts_ids ) ) {
327+
$blocks = parse_blocks( get_the_content() );
328+
$block_name = apply_filters( 'newspack_blocks_block_name', 'newspack-blocks/homepage-articles' );
329+
$newspack_blocks_all_specific_posts_ids = array_reduce(
330+
$blocks,
331+
function ( $acc, $block ) use ( $block_name ) {
332+
if (
333+
$block_name === $block['blockName'] &&
334+
isset( $block['attrs']['specificMode'], $block['attrs']['specificPosts'] ) &&
335+
count( $block['attrs']['specificPosts'] )
336+
) {
337+
return array_merge(
338+
$block['attrs']['specificPosts'],
339+
$acc
340+
);
341+
}
342+
return $acc;
343+
},
344+
[]
345+
);
346+
}
347+
323348
$authors = isset( $attributes['authors'] ) ? $attributes['authors'] : array();
324349
$categories = isset( $attributes['categories'] ) ? $attributes['categories'] : array();
325350
$tags = isset( $attributes['tags'] ) ? $attributes['tags'] : array();
@@ -337,6 +362,9 @@ public static function build_articles_query( $attributes ) {
337362
$args['orderby'] = 'post__in';
338363
} else {
339364
$args['posts_per_page'] = $posts_to_show + count( $newspack_blocks_post_id );
365+
if ( count( $newspack_blocks_all_specific_posts_ids ) ) {
366+
$args['post__not_in'] = $newspack_blocks_all_specific_posts_ids;
367+
}
340368
if ( $authors && count( $authors ) ) {
341369
$args['author__in'] = $authors;
342370
}

src/blocks/homepage-articles/edit.js

+27-28
Original file line numberDiff line numberDiff line change
@@ -266,34 +266,33 @@ class Edit extends Component {
266266
return (
267267
<Fragment>
268268
<PanelBody title={ __( 'Display Settings', 'newspack-blocks' ) } initialOpen={ true }>
269-
{ postsToShow && (
270-
<QueryControls
271-
numberOfItems={ postsToShow }
272-
onNumberOfItemsChange={ _postsToShow =>
273-
setAttributes( { postsToShow: _postsToShow } )
274-
}
275-
specificMode={ specificMode }
276-
onSpecificModeChange={ _specificMode =>
277-
setAttributes( { specificMode: _specificMode } )
278-
}
279-
specificPosts={ specificPosts }
280-
onSpecificPostsChange={ _specificPosts =>
281-
setAttributes( { specificPosts: _specificPosts } )
282-
}
283-
authors={ authors }
284-
onAuthorsChange={ _authors => setAttributes( { authors: _authors } ) }
285-
categories={ categories }
286-
onCategoriesChange={ _categories => setAttributes( { categories: _categories } ) }
287-
tags={ tags }
288-
onTagsChange={ _tags => {
289-
setAttributes( { tags: _tags } );
290-
} }
291-
tagExclusions={ tagExclusions }
292-
onTagExclusionsChange={ _tagExclusions =>
293-
setAttributes( { tagExclusions: _tagExclusions } )
294-
}
295-
/>
296-
) }
269+
<QueryControls
270+
numberOfItems={ postsToShow }
271+
onNumberOfItemsChange={ _postsToShow =>
272+
setAttributes( { postsToShow: _postsToShow || 1 } )
273+
}
274+
specificMode={ specificMode }
275+
onSpecificModeChange={ _specificMode =>
276+
setAttributes( { specificMode: _specificMode } )
277+
}
278+
specificPosts={ specificPosts }
279+
onSpecificPostsChange={ _specificPosts =>
280+
setAttributes( { specificPosts: _specificPosts } )
281+
}
282+
authors={ authors }
283+
onAuthorsChange={ _authors => setAttributes( { authors: _authors } ) }
284+
categories={ categories }
285+
onCategoriesChange={ _categories => setAttributes( { categories: _categories } ) }
286+
tags={ tags }
287+
onTagsChange={ _tags => {
288+
setAttributes( { tags: _tags } );
289+
} }
290+
tagExclusions={ tagExclusions }
291+
onTagExclusionsChange={ _tagExclusions =>
292+
setAttributes( { tagExclusions: _tagExclusions } )
293+
}
294+
/>
295+
297296
{ postLayout === 'grid' && (
298297
<RangeControl
299298
label={ __( 'Columns', 'newspack-blocks' ) }

src/blocks/homepage-articles/templates/articles-loop.php

+8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010

1111
call_user_func(
1212
function( $data ) {
13+
global $wp_query;
14+
$main_query = $wp_query;
15+
wp_reset_postdata();
16+
1317
$attributes = $data['attributes'];
1418
$article_query = $data['article_query'];
19+
20+
$wp_query = $article_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
21+
1522
global $newspack_blocks_post_id;
1623
$post_counter = 0;
1724
while ( $article_query->have_posts() ) {
@@ -23,6 +30,7 @@ function( $data ) {
2330
$post_counter++;
2431
echo Newspack_Blocks::template_inc( __DIR__ . '/article.php', array( 'attributes' => $attributes ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2532
}
33+
$wp_query = $main_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
2634
wp_reset_postdata();
2735
},
2836
$data // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable

src/blocks/homepage-articles/view.scss

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
flex-basis: 100%;
3737

3838
@include media( tablet ) {
39-
&:last-child,
40-
& {
41-
margin-bottom: 1em;
42-
}
39+
margin-bottom: 1em;
4340
}
4441
}
4542
}

0 commit comments

Comments
 (0)